forked from linkong/planet
release: bump version to 0.42.0
This commit is contained in:
1 parent
eeee788530
commit
b4e8afb272
53 files changed
+6863
-87
No files matched your search
@@ -1,11 +1,21 @@
|
||||
import { memo } from 'react'
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
import Scrollbar from '../Scrollbar/Scrollbar'
|
||||
|
||||
interface MarkdownRendererProps {
|
||||
markdown: string
|
||||
className?: string
|
||||
getHeadingId?: () => (text: string, level: number) => string | undefined
|
||||
transformLink?: (href: string) => MarkdownLink | null
|
||||
}
|
||||
|
||||
function renderInlineMarkdown(text: string): ReactNode[] {
|
||||
interface MarkdownLink {
|
||||
href: string
|
||||
external?: boolean
|
||||
}
|
||||
|
||||
function renderInlineMarkdown(text: string, transformLink?: MarkdownRendererProps['transformLink']): ReactNode[] {
|
||||
const result: ReactNode[] = []
|
||||
const pattern = /(\[[^\]]+\]\([^)]+\)|`[^`]+`|\*\*[^*]+\*\*|\*[^*]+\*)/g
|
||||
let lastIndex = 0
|
||||
@@ -22,8 +32,17 @@ function renderInlineMarkdown(text: string): ReactNode[] {
|
||||
if (matchedText.startsWith('[')) {
|
||||
const linkMatch = matchedText.match(/^\[([^\]]+)\]\(([^)]+)\)$/)
|
||||
if (linkMatch) {
|
||||
const resolvedLink = transformLink?.(linkMatch[2])
|
||||
const href = resolvedLink?.href || linkMatch[2]
|
||||
const isExternal = resolvedLink?.external ?? true
|
||||
|
||||
result.push(
|
||||
<a key={`inline-${key}`} href={linkMatch[2]} target="_blank" rel="noreferrer">
|
||||
<a
|
||||
key={`inline-${key}`}
|
||||
href={href}
|
||||
target={isExternal ? '_blank' : undefined}
|
||||
rel={isExternal ? 'noreferrer' : undefined}
|
||||
>
|
||||
{linkMatch[1]}
|
||||
</a>,
|
||||
)
|
||||
@@ -62,7 +81,13 @@ function renderInlineMarkdown(text: string): ReactNode[] {
|
||||
return result
|
||||
}
|
||||
|
||||
export default function MarkdownRenderer({ markdown, className }: MarkdownRendererProps) {
|
||||
function MarkdownRenderer({
|
||||
markdown,
|
||||
className,
|
||||
getHeadingId,
|
||||
transformLink,
|
||||
}: MarkdownRendererProps) {
|
||||
const resolveHeadingId = getHeadingId?.()
|
||||
const lines = markdown.replace(/\r\n/g, '\n').split('\n')
|
||||
const nodes: ReactNode[] = []
|
||||
let index = 0
|
||||
@@ -92,9 +117,11 @@ export default function MarkdownRenderer({ markdown, className }: MarkdownRender
|
||||
index += 1
|
||||
}
|
||||
nodes.push(
|
||||
<pre key={`block-${index}`}>
|
||||
<code>{codeLines.join('\n')}</code>
|
||||
</pre>,
|
||||
<Scrollbar key={`block-${index}`} className="markdown-renderer__code-scroll">
|
||||
<pre>
|
||||
<code>{codeLines.join('\n')}</code>
|
||||
</pre>
|
||||
</Scrollbar>,
|
||||
)
|
||||
continue
|
||||
}
|
||||
@@ -108,11 +135,13 @@ export default function MarkdownRenderer({ markdown, className }: MarkdownRender
|
||||
const headingMatch = trimmed.match(/^(#{1,6})\s+(.+)$/)
|
||||
if (headingMatch) {
|
||||
const level = headingMatch[1].length
|
||||
const content = renderInlineMarkdown(headingMatch[2])
|
||||
if (level === 1) nodes.push(<h1 key={`block-${index}`}>{content}</h1>)
|
||||
else if (level === 2) nodes.push(<h2 key={`block-${index}`}>{content}</h2>)
|
||||
else if (level === 3) nodes.push(<h3 key={`block-${index}`}>{content}</h3>)
|
||||
else if (level === 4) nodes.push(<h4 key={`block-${index}`}>{content}</h4>)
|
||||
const headingText = headingMatch[2]
|
||||
const content = renderInlineMarkdown(headingText, transformLink)
|
||||
const headingId = resolveHeadingId?.(headingText, level)
|
||||
if (level === 1) nodes.push(<h1 key={`block-${index}`} id={headingId}>{content}</h1>)
|
||||
else if (level === 2) nodes.push(<h2 key={`block-${index}`} id={headingId}>{content}</h2>)
|
||||
else if (level === 3) nodes.push(<h3 key={`block-${index}`} id={headingId}>{content}</h3>)
|
||||
else if (level === 4) nodes.push(<h4 key={`block-${index}`} id={headingId}>{content}</h4>)
|
||||
else nodes.push(<p key={`block-${index}`} className="markdown-renderer__heading-fallback">{content}</p>)
|
||||
index += 1
|
||||
continue
|
||||
@@ -140,7 +169,7 @@ export default function MarkdownRenderer({ markdown, className }: MarkdownRender
|
||||
nodes.push(
|
||||
<ul key={`block-${index}`}>
|
||||
{items.map((item, itemIndex) => (
|
||||
<li key={`item-${itemIndex}`}>{renderInlineMarkdown(item)}</li>
|
||||
<li key={`item-${itemIndex}`}>{renderInlineMarkdown(item, transformLink)}</li>
|
||||
))}
|
||||
</ul>,
|
||||
)
|
||||
@@ -159,7 +188,7 @@ export default function MarkdownRenderer({ markdown, className }: MarkdownRender
|
||||
nodes.push(
|
||||
<ol key={`block-${index}`}>
|
||||
{items.map((item, itemIndex) => (
|
||||
<li key={`item-${itemIndex}`}>{renderInlineMarkdown(item)}</li>
|
||||
<li key={`item-${itemIndex}`}>{renderInlineMarkdown(item, transformLink)}</li>
|
||||
))}
|
||||
</ol>,
|
||||
)
|
||||
@@ -186,12 +215,12 @@ export default function MarkdownRenderer({ markdown, className }: MarkdownRender
|
||||
}
|
||||
|
||||
nodes.push(
|
||||
<div key={`block-${index}`} className="markdown-renderer__table-wrap">
|
||||
<Scrollbar key={`block-${index}`} className="markdown-renderer__table-wrap">
|
||||
<table className="markdown-renderer__table">
|
||||
<thead>
|
||||
<tr>
|
||||
{tableHeaderCells.map((cell, cellIndex) => (
|
||||
<th key={`head-${cellIndex}`}>{renderInlineMarkdown(cell)}</th>
|
||||
<th key={`head-${cellIndex}`}>{renderInlineMarkdown(cell, transformLink)}</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -199,13 +228,13 @@ export default function MarkdownRenderer({ markdown, className }: MarkdownRender
|
||||
{bodyRows.map((row, rowIndex) => (
|
||||
<tr key={`row-${rowIndex}`}>
|
||||
{row.map((cell, cellIndex) => (
|
||||
<td key={`cell-${rowIndex}-${cellIndex}`}>{renderInlineMarkdown(cell)}</td>
|
||||
<td key={`cell-${rowIndex}-${cellIndex}`}>{renderInlineMarkdown(cell, transformLink)}</td>
|
||||
))}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>,
|
||||
</Scrollbar>,
|
||||
)
|
||||
continue
|
||||
}
|
||||
@@ -215,12 +244,14 @@ export default function MarkdownRenderer({ markdown, className }: MarkdownRender
|
||||
paragraphLines.push(lines[index].trim())
|
||||
index += 1
|
||||
}
|
||||
nodes.push(<p key={`block-${index}`}>{renderInlineMarkdown(paragraphLines.join(' '))}</p>)
|
||||
nodes.push(<p key={`block-${index}`}>{renderInlineMarkdown(paragraphLines.join(' '), transformLink)}</p>)
|
||||
}
|
||||
|
||||
return <div className={className ? `markdown-renderer ${className}` : 'markdown-renderer'}>{nodes}</div>
|
||||
}
|
||||
|
||||
export default memo(MarkdownRenderer)
|
||||
|
||||
function parseTableRow(line: string): string[] | null {
|
||||
if (!line.includes('|')) {
|
||||
return null
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
type PointerEvent as ReactPointerEvent,
|
||||
type ReactNode,
|
||||
type RefObject,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
@@ -25,6 +26,7 @@ interface ScrollbarProps {
|
||||
children: ReactNode
|
||||
className?: string
|
||||
minThumbSize?: number
|
||||
viewportRef?: RefObject<HTMLDivElement>
|
||||
}
|
||||
|
||||
interface DragState {
|
||||
@@ -49,8 +51,10 @@ function Scrollbar({
|
||||
children,
|
||||
className = '',
|
||||
minThumbSize = 28,
|
||||
viewportRef: externalViewportRef,
|
||||
}: ScrollbarProps) {
|
||||
const viewportRef = useRef<HTMLDivElement | null>(null)
|
||||
const internalViewportRef = useRef<HTMLDivElement | null>(null)
|
||||
const viewportRef = externalViewportRef ?? internalViewportRef
|
||||
const trackXRef = useRef<HTMLDivElement | null>(null)
|
||||
const trackYRef = useRef<HTMLDivElement | null>(null)
|
||||
const dragStateRef = useRef<DragState | null>(null)
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
.segmented-control {
|
||||
position: relative;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
min-width: 0;
|
||||
height: calc(42px * var(--segmented-control-scale, 1));
|
||||
padding: calc(4px * var(--segmented-control-scale, 1));
|
||||
border: 1px solid var(--segmented-control-border, var(--d-border, #d9e1ec));
|
||||
border-radius: var(--segmented-control-radius, calc(14px * var(--segmented-control-scale, 1)));
|
||||
background: var(--segmented-control-bg, var(--d-segment-bg, #eef3f9));
|
||||
box-shadow: inset 0 1px 2px rgba(15, 23, 42, 0.06);
|
||||
}
|
||||
|
||||
.segmented-control__slider {
|
||||
position: absolute;
|
||||
top: calc(4px * var(--segmented-control-scale, 1));
|
||||
left: calc(4px * var(--segmented-control-scale, 1));
|
||||
z-index: 1;
|
||||
width: calc((100% - (8px * var(--segmented-control-scale, 1))) / var(--segmented-control-items, 2));
|
||||
height: calc(100% - (8px * var(--segmented-control-scale, 1)));
|
||||
border-radius: var(--segmented-control-slider-radius, calc(10px * var(--segmented-control-scale, 1)));
|
||||
background: var(--segmented-control-slider-bg, var(--d-segment-slider, #ffffff));
|
||||
box-shadow: var(--segmented-control-slider-shadow, var(--d-segment-shadow, 0 2px 8px rgba(15, 23, 42, 0.12)));
|
||||
transform: translateX(calc(var(--segmented-control-index, 0) * 100%));
|
||||
transition:
|
||||
transform 0.46s cubic-bezier(0.34, 1.56, 0.64, 1),
|
||||
background 0.22s ease,
|
||||
box-shadow 0.22s ease;
|
||||
}
|
||||
|
||||
.segmented-control__button {
|
||||
position: relative;
|
||||
z-index: 2;
|
||||
flex: 1 1 0;
|
||||
min-width: 0;
|
||||
height: 100%;
|
||||
display: inline-flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: var(--segmented-control-button-gap, calc(2px * var(--segmented-control-scale, 1)));
|
||||
border: 0;
|
||||
border-radius: var(--segmented-control-button-radius, calc(10px * var(--segmented-control-scale, 1)));
|
||||
background: none;
|
||||
color: var(--segmented-control-color, var(--d-lang-btn, #4a5568));
|
||||
font: inherit;
|
||||
font-size: var(--segmented-control-font-size, calc(10px * var(--segmented-control-scale, 1)));
|
||||
font-weight: var(--segmented-control-font-weight, 800);
|
||||
letter-spacing: var(--segmented-control-letter-spacing, 0.04em);
|
||||
cursor: pointer;
|
||||
transition: color 0.18s ease, transform 0.18s ease;
|
||||
}
|
||||
|
||||
.segmented-control__button:hover {
|
||||
color: var(--segmented-control-hover, var(--d-nav-hover, #0d4f9f));
|
||||
}
|
||||
|
||||
.segmented-control__button:active .segmented-control__icon {
|
||||
transform: scale(0.86);
|
||||
}
|
||||
|
||||
.segmented-control__button--active {
|
||||
color: var(--segmented-control-active, var(--d-nav-active, #0b5fc1));
|
||||
}
|
||||
|
||||
.segmented-control__icon {
|
||||
width: var(--segmented-control-icon-size, calc(15px * var(--segmented-control-scale, 1)));
|
||||
height: var(--segmented-control-icon-size, calc(15px * var(--segmented-control-scale, 1)));
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: transform 0.2s ease;
|
||||
}
|
||||
|
||||
.segmented-control__icon svg {
|
||||
width: var(--segmented-control-icon-size, calc(15px * var(--segmented-control-scale, 1)));
|
||||
height: var(--segmented-control-icon-size, calc(15px * var(--segmented-control-scale, 1)));
|
||||
stroke-width: 2.1;
|
||||
}
|
||||
|
||||
.segmented-control__button--active .segmented-control__icon svg {
|
||||
stroke-width: 2.45;
|
||||
}
|
||||
|
||||
.segmented-control__label {
|
||||
min-width: 0;
|
||||
max-width: 100%;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import type { CSSProperties, ReactNode } from 'react'
|
||||
|
||||
import './SegmentedControl.css'
|
||||
|
||||
export interface SegmentedControlOption<T extends string> {
|
||||
value: T
|
||||
label: ReactNode
|
||||
icon?: ReactNode
|
||||
title?: string
|
||||
}
|
||||
|
||||
interface SegmentedControlProps<T extends string> {
|
||||
ariaLabel: string
|
||||
className?: string
|
||||
options: SegmentedControlOption<T>[]
|
||||
scale?: number
|
||||
value: T
|
||||
onChange: (value: T) => void
|
||||
}
|
||||
|
||||
function SegmentedControl<T extends string>({
|
||||
ariaLabel,
|
||||
className = '',
|
||||
options,
|
||||
scale = 1,
|
||||
value,
|
||||
onChange,
|
||||
}: SegmentedControlProps<T>) {
|
||||
const activeIndex = Math.max(0, options.findIndex((option) => option.value === value))
|
||||
const style = {
|
||||
'--segmented-control-items': options.length,
|
||||
'--segmented-control-index': activeIndex,
|
||||
'--segmented-control-scale': scale,
|
||||
} as CSSProperties
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`segmented-control ${className}`.trim()}
|
||||
role="group"
|
||||
aria-label={ariaLabel}
|
||||
style={style}
|
||||
>
|
||||
<span className="segmented-control__slider" aria-hidden="true" />
|
||||
{options.map((option) => {
|
||||
const isActive = option.value === value
|
||||
return (
|
||||
<button
|
||||
key={option.value}
|
||||
type="button"
|
||||
className={isActive ? 'segmented-control__button segmented-control__button--active' : 'segmented-control__button'}
|
||||
data-value={option.value}
|
||||
onClick={() => onChange(option.value)}
|
||||
title={option.title}
|
||||
aria-pressed={isActive}
|
||||
>
|
||||
{option.icon ? (
|
||||
<span className="segmented-control__icon" aria-hidden="true">
|
||||
{option.icon}
|
||||
</span>
|
||||
) : null}
|
||||
<span className="segmented-control__label">{option.label}</span>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SegmentedControl
|
||||
Reference in new issue
Block a user