@sparkelf/dsh-genui 0.11.4-patch.2 → 0.13.0-sparkelf.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +76 -0
- package/ICON-NAMES.md +30 -0
- package/README.md +26 -28
- package/README.zh-CN.md +26 -28
- package/SKILL.md +128 -19
- package/THIRD_PARTY_NOTICES.md +42 -0
- package/assets/genui-icon.svg +20 -0
- package/assets/genui-logo.svg +18 -0
- package/lib/assets/icons.js +1 -0
- package/lib/assets/mermaid.js +325 -325
- package/lib/assets/standalone-runtime.js +10 -9
- package/lib/client.js +32 -31
- package/lib/index.js +523 -80
- package/lib/types/client/action-context.d.ts +1 -1
- package/lib/types/client/artifact/types.d.ts +1 -1
- package/lib/types/client/asset-loader.d.ts +2 -2
- package/lib/types/client/blocks/blocks.d.ts +12 -0
- package/lib/types/client/blocks/icon.d.ts +4 -0
- package/lib/types/client/dom-fence.d.ts +2 -1
- package/lib/types/client/fence-render.d.ts +5 -21
- package/lib/types/client/file-address.d.ts +4 -0
- package/lib/types/client/file-link-context.d.ts +49 -0
- package/lib/types/client/file-link.d.ts +17 -0
- package/lib/types/client/gallery.d.ts +8 -2
- package/lib/types/client/genui-runtime/diagnostics.d.ts +1 -1
- package/lib/types/client/genui-runtime/limits.d.ts +4 -0
- package/lib/types/client/genui-runtime/schema.d.ts +14 -1
- package/lib/types/client/guard.d.ts +4 -1
- package/lib/types/client/i18n/en.d.ts +5 -1
- package/lib/types/client/i18n/index.d.ts +1 -1
- package/lib/types/client/icons/asset-icons.d.ts +1 -0
- package/lib/types/client/icons/icon-data-full.d.ts +23 -0
- package/lib/types/client/icons/icon-data.d.ts +24 -0
- package/lib/types/client/icons/icon-lazy.d.ts +18 -0
- package/lib/types/client/icons/icon-names-full.d.ts +12 -0
- package/lib/types/client/index.d.ts +5 -15
- package/lib/types/client/inline.d.ts +1 -1
- package/lib/types/client/mermaid-colors.d.ts +2 -0
- package/lib/types/client/mermaid-core.d.ts +2 -1
- package/lib/types/client/mermaid-lazy.d.ts +5 -1
- package/lib/types/client/panel-command.d.ts +21 -1
- package/lib/types/client/panel.d.ts +6 -1
- package/lib/types/client/primitive-adapter.d.ts +0 -2
- package/lib/types/client/session-resolver.d.ts +13 -2
- package/lib/types/client/source-fence.d.ts +1 -1
- package/lib/types/client/spec.d.ts +127 -3
- package/lib/types/client/standalone/asset-loader.d.ts +1 -1
- package/lib/types/client/standalone/primitive-adapter.d.ts +0 -4
- package/lib/types/plugin/fence-feedback.d.ts +11 -7
- package/lib/types/plugin/index.d.ts +6 -2
- package/lib/types/shared/fence-language.d.ts +14 -0
- package/lib/types/shared/fence-repair.d.ts +2 -1
- package/lib/types/shared/fence-resolve.d.ts +1 -1
- package/package.json +92 -83
- package/src/client/EChartNode.tsx +17 -3
- package/src/client/GenuiBlock.module.css +411 -76
- package/src/client/action-context.ts +5 -15
- package/src/client/artifact/ArtifactExport.module.css +34 -3
- package/src/client/artifact/ExportableGenuiBlock.tsx +23 -2
- package/src/client/artifact/portability.ts +6 -1
- package/src/client/artifact/types.ts +1 -1
- package/src/client/asset-loader.ts +3 -3
- package/src/client/blocks/advanced.tsx +101 -14
- package/src/client/blocks/blocks.tsx +143 -0
- package/src/client/blocks/charts.tsx +22 -6
- package/src/client/blocks/icon.tsx +93 -0
- package/src/client/blocks/image.tsx +1 -0
- package/src/client/blocks/render-node.tsx +84 -52
- package/src/client/dom-fence.tsx +546 -72
- package/src/client/fence-render.tsx +5 -21
- package/src/client/file-address.ts +37 -0
- package/src/client/file-link-context.tsx +116 -0
- package/src/client/file-link.ts +46 -0
- package/src/client/gallery.ts +161 -133
- package/src/client/genui-runtime/diagnostics.ts +1 -1
- package/src/client/genui-runtime/limits.ts +4 -0
- package/src/client/genui-runtime/normalize.ts +42 -6
- package/src/client/genui-runtime/schema.ts +39 -7
- package/src/client/guard.ts +170 -15
- package/src/client/i18n/en.ts +5 -1
- package/src/client/i18n/index.ts +1 -1
- package/src/client/i18n/zh.ts +5 -1
- package/src/client/icons/asset-icons.ts +16 -0
- package/src/client/icons/icon-data-full.ts +5715 -0
- package/src/client/icons/icon-data.ts +507 -0
- package/src/client/icons/icon-lazy.ts +80 -0
- package/src/client/icons/icon-names-full.ts +12 -0
- package/src/client/index.tsx +27 -51
- package/src/client/inline.ts +17 -6
- package/src/client/mermaid-colors.ts +40 -0
- package/src/client/mermaid-core.ts +69 -61
- package/src/client/mermaid-lazy.ts +99 -4
- package/src/client/panel-command.ts +16 -2
- package/src/client/panel.tsx +18 -5
- package/src/client/parse-partial.ts +6 -1
- package/src/client/primitive-adapter.ts +0 -11
- package/src/client/session-resolver.ts +12 -21
- package/src/client/source-fence.ts +4 -2
- package/src/client/spec.ts +149 -2
- package/src/client/standalone/asset-loader.ts +1 -1
- package/src/client/standalone/primitive-adapter.tsx +1 -9
- package/src/client/table-details.ts +4 -3
- package/src/plugin/fence-feedback.ts +60 -14
- package/src/plugin/index.ts +45 -20
- package/src/plugin/tool.ts +11 -2
- package/src/shared/fence-language.ts +30 -0
- package/src/shared/fence-repair.ts +37 -1
- package/src/shared/fence-resolve.ts +1 -1
- package/tsdown.config.ts +7 -8
- package/lib/invariant.js +0 -19
- package/lib/types/plugin/invariant.d.ts +0 -15
- package/src/plugin/invariant.ts +0 -30
|
@@ -1,30 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* GenUI action context, plugin-owned.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* provider around fence renders — the renderer MUST consume that exact
|
|
7
|
-
* context instance so the host's action wiring reaches our components.
|
|
8
|
-
* Pristine hosts (the stock snapshot line) export no such symbol; the DOM
|
|
9
|
-
* render channel then falls back to the local context and installs its own
|
|
10
|
-
* provider (this package wraps every tree it mounts).
|
|
11
|
-
*
|
|
12
|
-
* Absent provider = v1 behavior: interactive components are display-only
|
|
13
|
-
* (their `action` is ignored). The hook never throws.
|
|
4
|
+
* Inline fences and the session panel share this plugin-owned context so
|
|
5
|
+
* component actions reach the matching session conversation.
|
|
14
6
|
*/
|
|
15
7
|
import { createContext, useContext } from 'react'
|
|
16
8
|
import type { Context } from 'react'
|
|
17
|
-
import * as primitives from './primitive-adapter.ts'
|
|
18
9
|
|
|
19
10
|
/** v2 action handler: component action + its collected data. */
|
|
20
11
|
export type GenuiActionHandler = (action: string, payload: Record<string, unknown>) => void
|
|
21
12
|
|
|
22
|
-
/**
|
|
13
|
+
/** Plugin-owned context used by the inline renderer, panel, and embedded renderer. */
|
|
23
14
|
const localContext = createContext<GenuiActionHandler | undefined>(undefined)
|
|
24
15
|
|
|
25
|
-
/**
|
|
26
|
-
export const GenuiActionContext: Context<GenuiActionHandler | undefined> =
|
|
27
|
-
(primitives as unknown as { GenuiActionContext?: Context<GenuiActionHandler | undefined> }).GenuiActionContext ?? localContext
|
|
16
|
+
/** Shared action context owned by this plugin. */
|
|
17
|
+
export const GenuiActionContext: Context<GenuiActionHandler | undefined> = localContext
|
|
28
18
|
|
|
29
19
|
/** Read the installed action handler, if any. */
|
|
30
20
|
export function useGenuiAction(): GenuiActionHandler | undefined {
|
|
@@ -8,7 +8,37 @@
|
|
|
8
8
|
font: 12px/1.4 system-ui, sans-serif;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
.
|
|
11
|
+
/* 导出入口:无边框图标按钮。形状照项目里的 chrome 图标按钮 .panelClose
|
|
12
|
+
(26px 命中区 / 6px 圆角 / 透明底 / 无边框)。 */
|
|
13
|
+
.trigger {
|
|
14
|
+
display: inline-flex;
|
|
15
|
+
align-items: center;
|
|
16
|
+
justify-content: center;
|
|
17
|
+
width: 26px;
|
|
18
|
+
height: 26px;
|
|
19
|
+
padding: 0;
|
|
20
|
+
border: 0;
|
|
21
|
+
border-radius: 6px;
|
|
22
|
+
background: transparent;
|
|
23
|
+
color: var(--dsw-alias-label-tertiary);
|
|
24
|
+
cursor: pointer;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/* 悬浮沿用区块内表格行 / .tab / .accHead 那条配方(--dsl-g-hover):主题文字色
|
|
28
|
+
4% 淡洗 + 文字色升到 label-primary。不用 rgba(255,255,255,…) 与没带兜底的
|
|
29
|
+
--dsw-alias-fill-hover —— 浅色主题下两者都看不见。 */
|
|
30
|
+
.trigger:hover,
|
|
31
|
+
.trigger:focus-visible {
|
|
32
|
+
background: color-mix(in srgb, var(--dsw-alias-label-primary) 4%, transparent);
|
|
33
|
+
color: var(--dsw-alias-label-primary);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* 聚焦轮廓同 .panelClose。 */
|
|
37
|
+
.trigger:focus-visible {
|
|
38
|
+
outline: 1px solid var(--dsw-alias-state-business-primary, #4f8ef7);
|
|
39
|
+
outline-offset: 1px;
|
|
40
|
+
}
|
|
41
|
+
|
|
12
42
|
.item {
|
|
13
43
|
border: 1px solid var(--dsw-alias-border-l1);
|
|
14
44
|
border-radius: 7px;
|
|
@@ -18,9 +48,10 @@
|
|
|
18
48
|
cursor: pointer;
|
|
19
49
|
}
|
|
20
50
|
|
|
21
|
-
|
|
51
|
+
/* 菜单项沿用宿主的 fill-hover;第三方主题可能没有定义该 token(例如浅色
|
|
52
|
+
dream-skin),此时回退到区块内那条 4% 文字色淡洗,悬浮不会没有反馈。 */
|
|
22
53
|
.item:hover {
|
|
23
|
-
background: var(--dsw-alias-fill-hover);
|
|
54
|
+
background: var(--dsw-alias-fill-hover, color-mix(in srgb, var(--dsw-alias-label-primary) 4%, transparent));
|
|
24
55
|
color: var(--dsw-alias-label-primary);
|
|
25
56
|
}
|
|
26
57
|
|
|
@@ -13,6 +13,17 @@ interface ArtifactExportMenuProps {
|
|
|
13
13
|
getArtifact: () => ReturnType<typeof createGenuiArtifact>
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/** 导出入口的图标字形:托盘 + 下箭头(“导出为文件”),14px 描边、随文字取色。 */
|
|
17
|
+
function ExportGlyph() {
|
|
18
|
+
return (
|
|
19
|
+
<svg width="14" height="14" viewBox="0 0 16 16" aria-hidden="true" focusable="false">
|
|
20
|
+
<path d="M8 2.2v7.1" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" />
|
|
21
|
+
<path d="M5 6.4 8 9.4l3-3" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" />
|
|
22
|
+
<path d="M2.9 10.9v1.5a1 1 0 0 0 1 1h8.2a1 1 0 0 0 1-1v-1.5" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" />
|
|
23
|
+
</svg>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
16
27
|
/** 提供 JSON 和 HTML 的本地化导出菜单。 */
|
|
17
28
|
function ArtifactExportMenu({ getArtifact }: ArtifactExportMenuProps) {
|
|
18
29
|
const t = useT()
|
|
@@ -65,8 +76,18 @@ function ArtifactExportMenu({ getArtifact }: ArtifactExportMenuProps) {
|
|
|
65
76
|
<div className={css.chrome} ref={rootRef} data-genui-export>
|
|
66
77
|
{report.externalMedia.length > 0 && <span className={css.notice}>{t('artifact.externalMediaNotice')}</span>}
|
|
67
78
|
{errorMessage !== '' && <span className={css.statusError} aria-live="polite">{errorMessage}</span>}
|
|
68
|
-
|
|
69
|
-
|
|
79
|
+
{/* 无边框图标入口:完成态内容不再多出一个描边按钮。图标按钮的 aria-label
|
|
80
|
+
+ title 组合与项目里的 panelTpl / panelToggle 一致。 */}
|
|
81
|
+
<button
|
|
82
|
+
type="button"
|
|
83
|
+
className={css.trigger}
|
|
84
|
+
aria-label={t('artifact.export')}
|
|
85
|
+
title={t('artifact.export')}
|
|
86
|
+
aria-haspopup="menu"
|
|
87
|
+
aria-expanded={open}
|
|
88
|
+
onClick={() => setOpen(value => !value)}
|
|
89
|
+
>
|
|
90
|
+
<ExportGlyph />
|
|
70
91
|
</button>
|
|
71
92
|
{open && (
|
|
72
93
|
<div className={css.menu} role="menu">
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { echartEngineFor } from '../echarts-engine.ts'
|
|
2
2
|
import type { GenuiNode, GenuiSpec } from '../spec.ts'
|
|
3
3
|
import { GENUI_NATIVE_TYPES } from '../genui-runtime/schema.ts'
|
|
4
|
+
import { ICONS } from '../icons/icon-data.ts'
|
|
4
5
|
import { safeMediaSrc } from '../genui-runtime/value-utils.ts'
|
|
5
6
|
import type { GenuiPortabilityReport, GenuiStandaloneAsset } from './types.ts'
|
|
6
7
|
|
|
7
|
-
const ASSET_ORDER: GenuiStandaloneAsset[] = ['mermaid', 'three', 'echarts-core', 'echarts-full']
|
|
8
|
+
const ASSET_ORDER: GenuiStandaloneAsset[] = ['mermaid', 'three', 'echarts-core', 'echarts-full', 'icons']
|
|
8
9
|
|
|
9
10
|
/** 递归收集当前节点树依赖的引擎和媒体信息。 */
|
|
10
11
|
function scan(items: unknown[], report: GenuiPortabilityReport, assets: Set<GenuiStandaloneAsset>): void {
|
|
@@ -15,6 +16,10 @@ function scan(items: unknown[], report: GenuiPortabilityReport, assets: Set<Genu
|
|
|
15
16
|
if (node.type === 'mermaid') assets.add('mermaid')
|
|
16
17
|
if (node.type === 'scene3d') assets.add('three')
|
|
17
18
|
if (node.type === 'echart') assets.add(echartEngineFor(node as Extract<GenuiNode, { type: 'echart' }>))
|
|
19
|
+
// An icon outside the inlined 62 needs the lazily-fetched catalogue; a
|
|
20
|
+
// standalone export must therefore embed `icons` too, or the glyph degrades
|
|
21
|
+
// to the fallback dot in the exported HTML.
|
|
22
|
+
if (node.type === 'icon' && typeof node.name === 'string' && ICONS[node.name] === undefined) assets.add('icons')
|
|
18
23
|
if (typeof node.action === 'string' || node.type === 'submit' && (typeof node.action === 'string' || typeof node.resetAction === 'string')) report.hasModelActions = true
|
|
19
24
|
if (node.type === 'image' || node.type === 'audio' || node.type === 'video') {
|
|
20
25
|
const src = safeMediaSrc(node.src)
|
|
@@ -6,7 +6,7 @@ export const GENUI_ARTIFACT_FORMAT = 'dsh-genui'
|
|
|
6
6
|
export const GENUI_ARTIFACT_VERSION = 1 as const
|
|
7
7
|
|
|
8
8
|
export type GenuiArtifactTheme = 'light' | 'dark'
|
|
9
|
-
export type GenuiStandaloneAsset = 'mermaid' | 'three' | 'echarts-core' | 'echarts-full'
|
|
9
|
+
export type GenuiStandaloneAsset = 'mermaid' | 'three' | 'echarts-core' | 'echarts-full' | 'icons'
|
|
10
10
|
|
|
11
11
|
export interface GenuiArtifactV1 {
|
|
12
12
|
format: typeof GENUI_ARTIFACT_FORMAT
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
21
|
/** The plugin's entry id — mirrors the loader entry name (package name). */
|
|
22
|
-
const PLUGIN_ID = '@
|
|
22
|
+
const PLUGIN_ID = '@sparkelf/dsh-genui'
|
|
23
23
|
|
|
24
24
|
/** Assets directory served by the node-half route. */
|
|
25
25
|
const ASSET_DIR = `/plugins/${PLUGIN_ID}/assets`
|
|
@@ -62,10 +62,10 @@ const pending = new Map<string, Promise<Record<string, unknown>>>()
|
|
|
62
62
|
* file: repeated requests (several mermaid nodes, re-renders) share one
|
|
63
63
|
* script load; a failed load stays failed for the page (the component shows
|
|
64
64
|
* its fallback).
|
|
65
|
-
* @param name - 'mermaid', 'three', 'echarts-core',
|
|
65
|
+
* @param name - 'mermaid', 'three', 'echarts-core', 'echarts-full', or 'icons'.
|
|
66
66
|
* @returns the registered engine surface.
|
|
67
67
|
*/
|
|
68
|
-
export function loadGenuiAsset<T>(name: 'mermaid' | 'three' | 'echarts-core' | 'echarts-full'): Promise<T> {
|
|
68
|
+
export function loadGenuiAsset<T>(name: 'mermaid' | 'three' | 'echarts-core' | 'echarts-full' | 'icons'): Promise<T> {
|
|
69
69
|
const preloaded = preloadedAsset(name)
|
|
70
70
|
if (preloaded !== undefined) return Promise.resolve(preloaded as T)
|
|
71
71
|
const file = `${name}.js`
|
|
@@ -9,6 +9,7 @@ import { CodeBlock, DiffBlock, JsonTree, writeClipboard } from '../primitive-ada
|
|
|
9
9
|
import { renderInline } from '../inline.ts'
|
|
10
10
|
import css from '../GenuiBlock.module.css'
|
|
11
11
|
import { GENUI_LIMITS } from '../genui-runtime/index.ts'
|
|
12
|
+
import { renderMermaid } from '../mermaid-lazy.ts'
|
|
12
13
|
import { PlotBlock } from '../PlotBlock.tsx'
|
|
13
14
|
import { renderNode } from './render-node.tsx'
|
|
14
15
|
import { codeBlockLabels, diffBlockLabels, jsonTreeLabels } from '../primitive-labels.ts'
|
|
@@ -285,24 +286,108 @@ type MermaidRenderState =
|
|
|
285
286
|
export const MermaidNode = memo(function MermaidNode({ node }: { node: GenuiMermaid }) {
|
|
286
287
|
const t = useT()
|
|
287
288
|
const [state, setState] = useState<MermaidRenderState>({ status: 'loading' })
|
|
289
|
+
const ref = useRef<HTMLDivElement>(null)
|
|
288
290
|
const code = node.code.slice(0, GENUI_LIMITS.maxMermaid)
|
|
289
291
|
useEffect(() => {
|
|
290
292
|
let alive = true
|
|
293
|
+
let visible = typeof IntersectionObserver === 'undefined'
|
|
294
|
+
let finished = false
|
|
295
|
+
let controller: AbortController | undefined
|
|
291
296
|
setState({ status: 'loading' })
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
297
|
+
const start = (): void => {
|
|
298
|
+
const target = ref.current
|
|
299
|
+
if (target === null || !alive || !visible || finished || controller !== undefined) return
|
|
300
|
+
const current = new AbortController()
|
|
301
|
+
controller = current
|
|
302
|
+
void (async () => {
|
|
303
|
+
try {
|
|
304
|
+
const svg = await renderMermaid(code, { signal: current.signal, target })
|
|
305
|
+
if (alive && !current.signal.aborted) {
|
|
306
|
+
finished = true
|
|
307
|
+
setState({ status: 'ready', html: svg })
|
|
308
|
+
}
|
|
309
|
+
} catch {
|
|
310
|
+
if (alive && !current.signal.aborted) {
|
|
311
|
+
finished = true
|
|
312
|
+
setState({ status: 'error' })
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
})()
|
|
316
|
+
}
|
|
317
|
+
// One viewport of lookahead also works inside clipped/nested scroll
|
|
318
|
+
// containers. Keep observing until completion so queued work can leave.
|
|
319
|
+
const observer = typeof IntersectionObserver === 'undefined' ? undefined : new IntersectionObserver(entries => {
|
|
320
|
+
for (const entry of entries) {
|
|
321
|
+
visible = entry.isIntersecting
|
|
322
|
+
if (!visible && !finished) { controller?.abort(); controller = undefined }
|
|
323
|
+
if (visible) start()
|
|
299
324
|
}
|
|
300
|
-
})
|
|
301
|
-
|
|
325
|
+
}, { rootMargin: `${window.innerHeight}px 0px` })
|
|
326
|
+
if (ref.current !== null) observer?.observe(ref.current)
|
|
327
|
+
// Host themes may live on either root. No SVG cache: a theme change
|
|
328
|
+
// invalidates the mounted diagram and re-reads tokens at render time.
|
|
329
|
+
const themeKey = (): string => {
|
|
330
|
+
const style = getComputedStyle(ref.current ?? document.body)
|
|
331
|
+
const bodyStyle = getComputedStyle(document.body)
|
|
332
|
+
return [
|
|
333
|
+
'--dsw-alias-label-primary', '--dsw-alias-bg-layer-2',
|
|
334
|
+
'--dsw-alias-border-l2', '--dsw-alias-label-tertiary',
|
|
335
|
+
].flatMap(token => [style.getPropertyValue(token), bodyStyle.getPropertyValue(token)])
|
|
336
|
+
.concat(style.fontFamily, style.color, style.colorScheme,
|
|
337
|
+
String(window.matchMedia?.('(prefers-color-scheme: dark)').matches))
|
|
338
|
+
.join('\n')
|
|
339
|
+
}
|
|
340
|
+
let previousTheme = themeKey()
|
|
341
|
+
const onTheme = (): void => {
|
|
342
|
+
const nextTheme = themeKey()
|
|
343
|
+
if (!alive || previousTheme === nextTheme) return
|
|
344
|
+
previousTheme = nextTheme
|
|
345
|
+
controller?.abort()
|
|
346
|
+
controller = undefined
|
|
347
|
+
finished = false
|
|
348
|
+
// Preserve ready geometry while recoloring, especially for offscreen
|
|
349
|
+
// history. A new source still clears the old SVG at effect setup.
|
|
350
|
+
setState(previous => previous.status === 'ready' ? previous : { status: 'loading' })
|
|
351
|
+
start()
|
|
352
|
+
}
|
|
353
|
+
const theme = new MutationObserver(onTheme)
|
|
354
|
+
for (let target: HTMLElement | null = ref.current; target !== null; target = target.parentElement) {
|
|
355
|
+
theme.observe(target, {
|
|
356
|
+
attributes: true, attributeFilter: ['class', 'style', 'data-theme', 'data-ds-dark-theme'],
|
|
357
|
+
})
|
|
358
|
+
}
|
|
359
|
+
// Appearance plugins can replace a stylesheet without touching the card
|
|
360
|
+
// or body attributes. Recheck the palette only when those styles change;
|
|
361
|
+
// unrelated changes still stop at the themeKey equality gate.
|
|
362
|
+
theme.observe(document.head, {
|
|
363
|
+
childList: true, characterData: true, subtree: true,
|
|
364
|
+
attributes: true, attributeFilter: ['href', 'rel', 'media', 'disabled'],
|
|
365
|
+
})
|
|
366
|
+
document.head.addEventListener('load', onTheme, true)
|
|
367
|
+
const scheme = window.matchMedia?.('(prefers-color-scheme: dark)')
|
|
368
|
+
scheme?.addEventListener('change', onTheme)
|
|
369
|
+
start()
|
|
370
|
+
return () => {
|
|
371
|
+
alive = false
|
|
372
|
+
controller?.abort()
|
|
373
|
+
observer?.disconnect()
|
|
374
|
+
theme.disconnect()
|
|
375
|
+
document.head.removeEventListener('load', onTheme, true)
|
|
376
|
+
scheme?.removeEventListener('change', onTheme)
|
|
377
|
+
}
|
|
302
378
|
}, [code])
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
379
|
+
return (
|
|
380
|
+
<div
|
|
381
|
+
ref={ref}
|
|
382
|
+
className={state.status === 'ready' ? css.mermaid : css.mermaidFallback}
|
|
383
|
+
data-genui-mermaid={state.status === 'ready' ? '' : undefined}
|
|
384
|
+
dangerouslySetInnerHTML={state.status === 'ready' ? { __html: state.html } : undefined}
|
|
385
|
+
>
|
|
386
|
+
{state.status === 'ready' ? null : state.status === 'error'
|
|
387
|
+
? <><pre>{code}</pre><div className={css.mermaidErr}>{t('block.mermaidError')}</div></>
|
|
388
|
+
: <><pre>{code}</pre><div className={css.mermaidHint}>{t('block.mermaidLoading')}</div></>}
|
|
389
|
+
</div>
|
|
390
|
+
)
|
|
306
391
|
})
|
|
307
392
|
|
|
308
393
|
/** Scene3D: three.js WebGL canvas, lazily imported. */
|
|
@@ -489,8 +574,10 @@ export const QuizNode = memo(function QuizNode({ node, onAction }: {
|
|
|
489
574
|
}}
|
|
490
575
|
>
|
|
491
576
|
{/* Before answering: a radio affordance so the row reads as a
|
|
492
|
-
choice. After: ✓ on the right answer, ✗ on a wrong pick.
|
|
493
|
-
|
|
577
|
+
choice. After: ✓ on the right answer, ✗ on a wrong pick.
|
|
578
|
+
aria-hidden keeps the glyph out of the accessible name — the
|
|
579
|
+
option button must announce as its label alone. */}
|
|
580
|
+
<span className={css.quizMarker} aria-hidden="true">
|
|
494
581
|
{answered ? (opt.correct === true ? '✓' : isChosen ? '✗' : '') : isChosen ? '●' : '○'}
|
|
495
582
|
</span>
|
|
496
583
|
{renderInline(opt.label, false)}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `blocks` node: a grid of filled cells grouped into labelled rows — the
|
|
3
|
+
* declarative form of the block diagrams models used to hand-write as SVG
|
|
4
|
+
* (recursion ladders, "4 equal parts", pipeline lane grids).
|
|
5
|
+
*
|
|
6
|
+
* Three shapes, one data structure:
|
|
7
|
+
* · ladder each row divides its own width (`cells` counts differ)
|
|
8
|
+
* · parts 4 equal cells in one row
|
|
9
|
+
* · lanes `columns` fixes a shared column count; `null` cells are gaps
|
|
10
|
+
*
|
|
11
|
+
* Why this exists instead of `svg`: coordinates are the model's weakest
|
|
12
|
+
* subject. A 15-rect figure with hand-computed x/y aligns by luck; the same
|
|
13
|
+
* figure as rows × cells aligns by construction, and costs ONE node against
|
|
14
|
+
* the 200-node budget instead of ~30.
|
|
15
|
+
* @module @changfenhuang/dsh-genui/client/blocks/blocks
|
|
16
|
+
*/
|
|
17
|
+
import { memo } from 'react'
|
|
18
|
+
import css from '../GenuiBlock.module.css'
|
|
19
|
+
import { renderInline } from '../inline.ts'
|
|
20
|
+
import { GENUI_LIMITS } from '../genui-runtime/index.ts'
|
|
21
|
+
import type { GenuiBlocks } from '../spec.ts'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Contrast-aware ink for opaque literal fills; theme ink for contextual fills.
|
|
25
|
+
*
|
|
26
|
+
* Uses WCAG relative luminance, not a naive RGB average: the average reads
|
|
27
|
+
* mid-tone blues (#339cff) as "dark" and prints white on a surface that
|
|
28
|
+
* actually needs ink on it.
|
|
29
|
+
*/
|
|
30
|
+
export function readableOn(color: string | undefined): string {
|
|
31
|
+
const fill = (color ?? '').trim()
|
|
32
|
+
const hexMatch = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(fill)
|
|
33
|
+
let channels: number[]
|
|
34
|
+
if (hexMatch !== null) {
|
|
35
|
+
let hex = hexMatch[1]!
|
|
36
|
+
if (hex.length === 3) hex = hex.split('').map(c => c + c).join('')
|
|
37
|
+
channels = [0, 2, 4].map(offset => parseInt(hex.slice(offset, offset + 2), 16))
|
|
38
|
+
} else {
|
|
39
|
+
// Let the browser's CSS parser normalize RGB/HSL and their modern syntax.
|
|
40
|
+
// This detached style needs no layout or global theme mutation. Host
|
|
41
|
+
// tokens and translucent fills retain the host ink because their final
|
|
42
|
+
// contrast depends on the surrounding theme/backdrop.
|
|
43
|
+
if (typeof document === 'undefined') return 'var(--dsw-alias-label-primary)'
|
|
44
|
+
const style = document.createElement('span').style
|
|
45
|
+
style.color = fill
|
|
46
|
+
const rgb = /^rgba?\(([\d.]+),\s*([\d.]+),\s*([\d.]+)(?:,\s*([\d.]+))?\)$/.exec(style.color)
|
|
47
|
+
if (rgb === null || (rgb[4] !== undefined && Number(rgb[4]) !== 1)) return 'var(--dsw-alias-label-primary)'
|
|
48
|
+
channels = rgb.slice(1, 4).map(Number)
|
|
49
|
+
}
|
|
50
|
+
const channel = (value: number): number => {
|
|
51
|
+
const v = value / 255
|
|
52
|
+
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4
|
|
53
|
+
}
|
|
54
|
+
const luminance = 0.2126 * channel(channels[0]!)
|
|
55
|
+
+ 0.7152 * channel(channels[1]!)
|
|
56
|
+
+ 0.0722 * channel(channels[2]!)
|
|
57
|
+
// 0.179 is the exact crossover where white and near-black score the same
|
|
58
|
+
// contrast ratio, so anything above it is better with ink than with white.
|
|
59
|
+
return luminance > 0.179 ? '#0d0d0d' : '#ffffff'
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const BlocksNode = memo(function BlocksNode({ node }: { node: GenuiBlocks }) {
|
|
63
|
+
const rows = node.rows.slice(0, GENUI_LIMITS.maxBlocksRows)
|
|
64
|
+
// `columns` aligns every row to one lane grid; without it each row divides
|
|
65
|
+
// its own width (the ladder shape).
|
|
66
|
+
const columns = node.columns === undefined
|
|
67
|
+
? undefined
|
|
68
|
+
: Math.max(1, Math.min(GENUI_LIMITS.maxBlocksCells, Math.trunc(node.columns)))
|
|
69
|
+
const caption = node.caption?.trim()
|
|
70
|
+
return (
|
|
71
|
+
<figure className={css.blocksFigure} data-genui-blocks>
|
|
72
|
+
{node.title !== undefined && <div className={css.blocksTitle}>{renderInline(node.title)}</div>}
|
|
73
|
+
<div className={css.blocksRows}>
|
|
74
|
+
{rows.map((row, i) => {
|
|
75
|
+
const count = columns ?? Math.max(1, Math.min(GENUI_LIMITS.maxBlocksCells, row.cells.length))
|
|
76
|
+
const cells = row.cells.slice(0, count)
|
|
77
|
+
const fill = row.color
|
|
78
|
+
const ink = readableOn(fill)
|
|
79
|
+
const variant = row.variant ?? 'fill'
|
|
80
|
+
const outline = variant === 'outline'
|
|
81
|
+
const plain = variant === 'plain'
|
|
82
|
+
return (
|
|
83
|
+
<div key={i} className={css.blocksRow}>
|
|
84
|
+
{row.label !== undefined && (
|
|
85
|
+
<span className={css.blocksRowLabel}>{renderInline(row.label)}</span>
|
|
86
|
+
)}
|
|
87
|
+
<div
|
|
88
|
+
className={css.blocksCells}
|
|
89
|
+
style={{ gridTemplateColumns: `repeat(${count}, minmax(0, 1fr))` }}
|
|
90
|
+
>
|
|
91
|
+
{cells.map((cell, j) => {
|
|
92
|
+
// `null` is a blank lane slot: no dash, no fill. Write the
|
|
93
|
+
// string "—" when a visible placeholder is wanted.
|
|
94
|
+
if (cell === null) return <span key={j} className={css.blocksGap} aria-hidden />
|
|
95
|
+
// Object form: one cell carries the emphasis (sign bit, active
|
|
96
|
+
// lane) and/or a caption hanging under it.
|
|
97
|
+
const spec = typeof cell === 'string' ? { text: cell } : cell
|
|
98
|
+
const cellInk = spec.color !== undefined ? readableOn(spec.color) : ink
|
|
99
|
+
// `plain` rows paint nothing unless the cell itself asks
|
|
100
|
+
// for a tone/colour — that is what makes a bit string read
|
|
101
|
+
// as digits instead of a table.
|
|
102
|
+
// Precedence: explicit hex > semantic tone (painted by CSS
|
|
103
|
+
// from [data-tone]) > the row's own paint mode.
|
|
104
|
+
const paint = spec.color !== undefined
|
|
105
|
+
? { background: spec.color, color: cellInk }
|
|
106
|
+
: spec.tone !== undefined
|
|
107
|
+
? undefined
|
|
108
|
+
: plain || outline
|
|
109
|
+
? undefined
|
|
110
|
+
: fill !== undefined
|
|
111
|
+
? { background: fill, color: ink }
|
|
112
|
+
: undefined
|
|
113
|
+
return (
|
|
114
|
+
<span key={j} className={css.blocksCellWrap}>
|
|
115
|
+
<span
|
|
116
|
+
className={`${css.blocksCell} ${outline ? css.blocksCellOutline : ''}${plain ? ` ${css.blocksCellPlain}` : ''}`}
|
|
117
|
+
data-tone={spec.tone}
|
|
118
|
+
style={paint}
|
|
119
|
+
>
|
|
120
|
+
{renderInline(spec.text)}
|
|
121
|
+
</span>
|
|
122
|
+
{spec.sub !== undefined && (
|
|
123
|
+
<span className={css.blocksSub}>{renderInline(spec.sub)}</span>
|
|
124
|
+
)}
|
|
125
|
+
</span>
|
|
126
|
+
)
|
|
127
|
+
})}
|
|
128
|
+
{/* `columns` wider than the row: pad the tail so every row's
|
|
129
|
+
cells keep their lane instead of stretching to fill. */}
|
|
130
|
+
{Array.from({ length: Math.max(0, count - cells.length) }, (_, k) => (
|
|
131
|
+
<span key={`pad-${k}`} className={css.blocksGapPad} aria-hidden />
|
|
132
|
+
))}
|
|
133
|
+
</div>
|
|
134
|
+
</div>
|
|
135
|
+
)
|
|
136
|
+
})}
|
|
137
|
+
</div>
|
|
138
|
+
{caption !== undefined && caption !== '' && (
|
|
139
|
+
<figcaption className={css.blocksCaption}>{renderInline(caption)}</figcaption>
|
|
140
|
+
)}
|
|
141
|
+
</figure>
|
|
142
|
+
)
|
|
143
|
+
})
|
|
@@ -345,7 +345,7 @@ export const TableNode = memo(function TableNode({ node, renderDetail, filterVal
|
|
|
345
345
|
.join(' ') || undefined}
|
|
346
346
|
>
|
|
347
347
|
{type === 'badge'
|
|
348
|
-
? <span className={css.cellBadge}>{renderInline(String(cell),
|
|
348
|
+
? <span className={css.cellBadge}>{renderInline(String(cell), 'file')}</span>
|
|
349
349
|
: type === 'bar'
|
|
350
350
|
? <CellBar cell={cell} />
|
|
351
351
|
: type === 'spark'
|
|
@@ -356,8 +356,8 @@ export const TableNode = memo(function TableNode({ node, renderDetail, filterVal
|
|
|
356
356
|
? <span className={css.cellIndex}>{rowIndex + 1}</span>
|
|
357
357
|
: tone === null
|
|
358
358
|
// Format displayed text without changing sorting or exported data.
|
|
359
|
-
? renderInline(String(cell),
|
|
360
|
-
: <span className={`${css.tdDelta} ${tone === 'up' ? css.tdDeltaUp : css.tdDeltaDown}`}>{renderInline(String(cell),
|
|
359
|
+
? renderInline(String(cell), 'file')
|
|
360
|
+
: <span className={`${css.tdDelta} ${tone === 'up' ? css.tdDeltaUp : css.tdDeltaDown}`}>{renderInline(String(cell), 'file')}</span>}
|
|
361
361
|
</td>
|
|
362
362
|
)
|
|
363
363
|
}
|
|
@@ -396,13 +396,21 @@ export const TableNode = memo(function TableNode({ node, renderDetail, filterVal
|
|
|
396
396
|
<button
|
|
397
397
|
type="button"
|
|
398
398
|
className={css.groupToggle}
|
|
399
|
+
aria-label={tr(isCollapsed ? 'block.table.expandGroup' : 'block.table.collapseGroup', {
|
|
400
|
+
label: String(section.header.row[0]).replace(/\[([^\]]+)\]\([^)]+\)/g, '$1'),
|
|
401
|
+
})}
|
|
399
402
|
aria-expanded={!isCollapsed}
|
|
400
403
|
onClick={() => toggleSection(headerIndex)}
|
|
401
404
|
>
|
|
402
405
|
<span className={css.groupChevron} aria-hidden>{isCollapsed ? '▸' : '▾'}</span>
|
|
403
|
-
{renderInline(String(section.header.row[0]), false)}
|
|
404
|
-
<span className={css.groupCount}>{section.children.length}</span>
|
|
405
406
|
</button>
|
|
407
|
+
<span className={css.groupLabel} onClick={event => {
|
|
408
|
+
if (event.target instanceof Element && event.target.closest('button')) return
|
|
409
|
+
toggleSection(headerIndex)
|
|
410
|
+
}}>
|
|
411
|
+
{renderInline(String(section.header.row[0]), 'file')}
|
|
412
|
+
<span className={css.groupCount}>{section.children.length}</span>
|
|
413
|
+
</span>
|
|
406
414
|
</td>
|
|
407
415
|
</tr>
|
|
408
416
|
)}
|
|
@@ -419,12 +427,20 @@ export const TableNode = memo(function TableNode({ node, renderDetail, filterVal
|
|
|
419
427
|
<button
|
|
420
428
|
type="button"
|
|
421
429
|
className={css.detailToggle}
|
|
430
|
+
aria-label={tr(open ? 'block.table.collapseDetails' : 'block.table.expandDetails', {
|
|
431
|
+
label: String(cell).replace(/\[([^\]]+)\]\([^)]+\)/g, '$1'),
|
|
432
|
+
})}
|
|
422
433
|
aria-expanded={open}
|
|
423
434
|
onClick={() => toggleDetail(child.index)}
|
|
424
435
|
>
|
|
425
436
|
<span className={css.detailChevron} data-open={open} aria-hidden>▸</span>
|
|
426
|
-
{renderInline(String(cell), false)}
|
|
427
437
|
</button>
|
|
438
|
+
<span className={css.detailContent} onClick={event => {
|
|
439
|
+
if (event.target instanceof Element && event.target.closest('button')) return
|
|
440
|
+
toggleDetail(child.index)
|
|
441
|
+
}}>
|
|
442
|
+
{renderInline(String(cell), 'file')}
|
|
443
|
+
</span>
|
|
428
444
|
</td>
|
|
429
445
|
)
|
|
430
446
|
: renderCell(cell, j, child.index)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `icon` node: one glyph from the bundled OpenAI Apps SDK UI catalogue,
|
|
3
|
+
* optionally sitting on a tinted rounded-square plate.
|
|
4
|
+
*
|
|
5
|
+
* The plate is the "figure" form — the tinted tile that marks a stage, a
|
|
6
|
+
* semantic role, or a card face in a diagram. Without `tile` the glyph is a
|
|
7
|
+
* plain inline icon that follows the surrounding text colour, so a title can
|
|
8
|
+
* read `icon + text` in a `row` without a second wrapper.
|
|
9
|
+
*
|
|
10
|
+
* Lookup is two-tier (see icon-lazy): 64 common glyphs render synchronously,
|
|
11
|
+
* the remaining 685 arrive from `lib/assets/icons.js` after a one-shot load.
|
|
12
|
+
* An unknown name never breaks the block — it renders a neutral dot.
|
|
13
|
+
* @module @changfenhuang/dsh-genui/client/blocks/icon
|
|
14
|
+
*/
|
|
15
|
+
import { createElement, memo, useEffect, useState, type ReactNode } from 'react'
|
|
16
|
+
import css from '../GenuiBlock.module.css'
|
|
17
|
+
import type { GenuiIcon } from '../spec.ts'
|
|
18
|
+
import type { BundledIcon, IconShape } from '../icons/icon-data.ts'
|
|
19
|
+
import { ensureIcons, isKnownIcon, onIconsLoaded, resolveIcon } from '../icons/icon-lazy.ts'
|
|
20
|
+
|
|
21
|
+
/** Geometry and paint come from the same generated upstream SVG. */
|
|
22
|
+
function renderShape(shape: IconShape, index: number): ReactNode {
|
|
23
|
+
return createElement(shape.tag, { ...shape.attrs, key: index })
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Optical sizes the catalogue ships at; anything else clamps to the nearest. */
|
|
27
|
+
const SIZES = [16, 20, 24, 32] as const
|
|
28
|
+
|
|
29
|
+
function clampSize(size: number | undefined): number {
|
|
30
|
+
if (size === undefined) return 20
|
|
31
|
+
return SIZES.reduce((best, s) => (Math.abs(s - size) < Math.abs(best - size) ? s : best), 20)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Resolve a glyph, triggering the catalogue load when the name is real but not
|
|
36
|
+
* part of the inline set. Returns undefined until the paths are in.
|
|
37
|
+
*/
|
|
38
|
+
function useIcon(name: string): BundledIcon | undefined {
|
|
39
|
+
const [glyph, setGlyph] = useState<BundledIcon | undefined>(() => resolveIcon(name))
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
const immediate = resolveIcon(name)
|
|
43
|
+
if (immediate !== undefined) {
|
|
44
|
+
setGlyph(immediate)
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
// A typo must not download the full catalogue: only real names reach the loader.
|
|
48
|
+
if (!isKnownIcon(name)) {
|
|
49
|
+
setGlyph(undefined)
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
let live = true
|
|
53
|
+
const unsubscribe = onIconsLoaded(() => {
|
|
54
|
+
if (live) setGlyph(resolveIcon(name))
|
|
55
|
+
})
|
|
56
|
+
void ensureIcons().then(() => {
|
|
57
|
+
if (live) setGlyph(resolveIcon(name))
|
|
58
|
+
})
|
|
59
|
+
return () => { live = false; unsubscribe() }
|
|
60
|
+
}, [name])
|
|
61
|
+
|
|
62
|
+
return glyph
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const IconNode = memo(function IconNode({ node }: { node: GenuiIcon }) {
|
|
66
|
+
const icon = useIcon(node.name)
|
|
67
|
+
const size = clampSize(node.size)
|
|
68
|
+
const tone = node.tone ?? 'default'
|
|
69
|
+
const tile = node.tile === true
|
|
70
|
+
// A decorative glyph beside a visible label must not double-announce itself.
|
|
71
|
+
const labelled = typeof node.label === 'string' && node.label.trim() !== ''
|
|
72
|
+
return (
|
|
73
|
+
<span
|
|
74
|
+
className={css.iconNode}
|
|
75
|
+
data-tone={tone}
|
|
76
|
+
data-tile={tile ? 'true' : 'false'}
|
|
77
|
+
// The plate is chrome, not content: size the box from the glyph so a
|
|
78
|
+
// 16px icon in a tile still centres on a 34px plate.
|
|
79
|
+
style={tile ? { width: size + 14, height: size + 14 } : undefined}
|
|
80
|
+
{...(labelled
|
|
81
|
+
? { role: 'img', 'aria-label': node.label }
|
|
82
|
+
: { 'aria-hidden': true })}
|
|
83
|
+
>
|
|
84
|
+
{icon === undefined
|
|
85
|
+
? <span className={css.iconDot} style={{ width: size * 0.4, height: size * 0.4 }} />
|
|
86
|
+
: (
|
|
87
|
+
<svg {...icon.attrs} viewBox={icon.viewBox} width={size} height={size} focusable="false">
|
|
88
|
+
{icon.shapes.map((shape, i) => renderShape(shape, i))}
|
|
89
|
+
</svg>
|
|
90
|
+
)}
|
|
91
|
+
</span>
|
|
92
|
+
)
|
|
93
|
+
})
|
|
@@ -23,6 +23,7 @@ export function ImageNode({ node }: { node: GenuiImage }): ReactNode {
|
|
|
23
23
|
style={{ maxHeight: 'min(70vh, 720px)', objectFit: 'contain' }}
|
|
24
24
|
onError={() => setFailed(true)}
|
|
25
25
|
/>}
|
|
26
|
+
{node.caption !== undefined && <figcaption className={css.mediaCaption}>{renderInline(node.caption)}</figcaption>}
|
|
26
27
|
</figure>
|
|
27
28
|
)
|
|
28
29
|
}
|