@sparkelf/dsh-genui 0.0.0-stage → 0.11.4-patch.1
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 +597 -0
- package/LICENSE +21 -0
- package/README.md +292 -2
- package/README.zh-CN.md +307 -0
- package/SKILL.md +261 -0
- package/cordis.patch.yml +9 -0
- package/demo-prompts.md +118 -0
- package/lib/assets/echarts-core.js +43 -0
- package/lib/assets/echarts-full.js +72 -0
- package/lib/assets/mermaid.js +3591 -0
- package/lib/assets/standalone-runtime.js +1506 -0
- package/lib/assets/three.js +3844 -0
- package/lib/client.js +405 -0
- package/lib/index.js +5187 -0
- package/lib/invariant.js +19 -0
- package/lib/types/client/EChartNode.d.ts +13 -0
- package/lib/types/client/ErrorBoundary.d.ts +30 -0
- package/lib/types/client/GenuiBlock.d.ts +9 -0
- package/lib/types/client/PlotBlock.d.ts +44 -0
- package/lib/types/client/TemplateDrawer.d.ts +7 -0
- package/lib/types/client/achievement-store.d.ts +35 -0
- package/lib/types/client/achievement-toast.d.ts +2 -0
- package/lib/types/client/achievements.d.ts +63 -0
- package/lib/types/client/action-context.d.ts +7 -0
- package/lib/types/client/artifact/ExportableGenuiBlock.d.ts +6 -0
- package/lib/types/client/artifact/create.d.ts +5 -0
- package/lib/types/client/artifact/download.d.ts +9 -0
- package/lib/types/client/artifact/html.d.ts +5 -0
- package/lib/types/client/artifact/index.d.ts +8 -0
- package/lib/types/client/artifact/json.d.ts +3 -0
- package/lib/types/client/artifact/parse.d.ts +3 -0
- package/lib/types/client/artifact/portability.d.ts +4 -0
- package/lib/types/client/artifact/standalone-theme.d.ts +2 -0
- package/lib/types/client/artifact/types.d.ts +33 -0
- package/lib/types/client/asset-echarts-core.d.ts +1 -0
- package/lib/types/client/asset-echarts.d.ts +17 -0
- package/lib/types/client/asset-loader.d.ts +33 -0
- package/lib/types/client/asset-mermaid.d.ts +1 -0
- package/lib/types/client/asset-three.d.ts +1 -0
- package/lib/types/client/blocks/advanced.d.ts +101 -0
- package/lib/types/client/blocks/basic.d.ts +22 -0
- package/lib/types/client/blocks/charts.d.ts +53 -0
- package/lib/types/client/blocks/checkbox.d.ts +17 -0
- package/lib/types/client/blocks/diagram/geometry.d.ts +73 -0
- package/lib/types/client/blocks/diagram/index.d.ts +5 -0
- package/lib/types/client/blocks/diagram/layout.d.ts +41 -0
- package/lib/types/client/blocks/diagram/theme.d.ts +37 -0
- package/lib/types/client/blocks/forms.d.ts +77 -0
- package/lib/types/client/blocks/image.d.ts +6 -0
- package/lib/types/client/blocks/render-node.d.ts +10 -0
- package/lib/types/client/blocks/state.d.ts +63 -0
- package/lib/types/client/blocks/svg.d.ts +4 -0
- package/lib/types/client/dom-fence.d.ts +30 -0
- package/lib/types/client/echarts-engine.d.ts +4 -0
- package/lib/types/client/echarts-lazy.d.ts +18 -0
- package/lib/types/client/embed.d.ts +10 -0
- package/lib/types/client/fence-render.d.ts +106 -0
- package/lib/types/client/gallery.d.ts +7 -0
- package/lib/types/client/genui-runtime/diagnostics.d.ts +18 -0
- package/lib/types/client/genui-runtime/index.d.ts +1 -0
- package/lib/types/client/genui-runtime/limits.d.ts +54 -0
- package/lib/types/client/genui-runtime/normalize.d.ts +15 -0
- package/lib/types/client/genui-runtime/schema.d.ts +87 -0
- package/lib/types/client/genui-runtime/value-utils.d.ts +18 -0
- package/lib/types/client/guard.d.ts +144 -0
- package/lib/types/client/i18n/en.d.ts +307 -0
- package/lib/types/client/i18n/index.d.ts +44 -0
- package/lib/types/client/i18n/runtime.d.ts +64 -0
- package/lib/types/client/i18n/zh.d.ts +8 -0
- package/lib/types/client/index.d.ts +36 -0
- package/lib/types/client/inline.d.ts +5 -0
- package/lib/types/client/interaction-store.d.ts +48 -0
- package/lib/types/client/mermaid-core.d.ts +8 -0
- package/lib/types/client/mermaid-lazy.d.ts +9 -0
- package/lib/types/client/mermaid-safe.d.ts +52 -0
- package/lib/types/client/panel-command.d.ts +34 -0
- package/lib/types/client/panel-store.d.ts +94 -0
- package/lib/types/client/panel.d.ts +18 -0
- package/lib/types/client/parse-partial.d.ts +63 -0
- package/lib/types/client/primitive-adapter.d.ts +4 -0
- package/lib/types/client/primitive-labels.d.ts +18 -0
- package/lib/types/client/safe-math.d.ts +43 -0
- package/lib/types/client/scene3d-core.d.ts +21 -0
- package/lib/types/client/scene3d-lazy.d.ts +8 -0
- package/lib/types/client/session-resolver.d.ts +8 -0
- package/lib/types/client/source-fence.d.ts +29 -0
- package/lib/types/client/spec.d.ts +709 -0
- package/lib/types/client/standalone/asset-loader.d.ts +2 -0
- package/lib/types/client/standalone/bootstrap-types.d.ts +9 -0
- package/lib/types/client/standalone/katex-style.d.ts +1 -0
- package/lib/types/client/standalone/primitive-adapter.d.ts +54 -0
- package/lib/types/client/standalone/runtime.d.ts +2 -0
- package/lib/types/client/submission-registry.d.ts +51 -0
- package/lib/types/client/svg-fence.d.ts +2 -0
- package/lib/types/client/table-details.d.ts +20 -0
- package/lib/types/client/templates.d.ts +41 -0
- package/lib/types/client/toolview.d.ts +7 -0
- package/lib/types/client/walk-spec.d.ts +3 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/plugin/chart-contract.d.ts +9 -0
- package/lib/types/plugin/fence-feedback.d.ts +151 -0
- package/lib/types/plugin/genui-diagnostic.d.ts +13 -0
- package/lib/types/plugin/index.d.ts +35 -0
- package/lib/types/plugin/inline-content-diagnostic.d.ts +8 -0
- package/lib/types/plugin/invariant.d.ts +15 -0
- package/lib/types/plugin/tool.d.ts +27 -0
- package/lib/types/shared/fence-repair.d.ts +78 -0
- package/lib/types/shared/fence-resolve.d.ts +39 -0
- package/package.json +249 -4
- package/src/client/EChartNode.tsx +466 -0
- package/src/client/ErrorBoundary.tsx +67 -0
- package/src/client/GenuiBlock.module.css +2221 -0
- package/src/client/GenuiBlock.tsx +235 -0
- package/src/client/PlotBlock.module.css +171 -0
- package/src/client/PlotBlock.tsx +492 -0
- package/src/client/TemplateDrawer.module.css +152 -0
- package/src/client/TemplateDrawer.tsx +147 -0
- package/src/client/achievement-store.ts +151 -0
- package/src/client/achievement-toast.tsx +71 -0
- package/src/client/achievements.ts +160 -0
- package/src/client/action-context.ts +32 -0
- package/src/client/artifact/ArtifactExport.module.css +64 -0
- package/src/client/artifact/ExportableGenuiBlock.tsx +100 -0
- package/src/client/artifact/create.ts +69 -0
- package/src/client/artifact/download.ts +42 -0
- package/src/client/artifact/html.ts +157 -0
- package/src/client/artifact/index.ts +15 -0
- package/src/client/artifact/json.ts +6 -0
- package/src/client/artifact/parse.ts +32 -0
- package/src/client/artifact/portability.ts +47 -0
- package/src/client/artifact/standalone-theme.ts +94 -0
- package/src/client/artifact/types.ts +44 -0
- package/src/client/asset-echarts-core.ts +39 -0
- package/src/client/asset-echarts.ts +27 -0
- package/src/client/asset-loader.ts +96 -0
- package/src/client/asset-mermaid.ts +13 -0
- package/src/client/asset-three.ts +12 -0
- package/src/client/blocks/advanced.tsx +537 -0
- package/src/client/blocks/basic.tsx +121 -0
- package/src/client/blocks/charts.tsx +999 -0
- package/src/client/blocks/checkbox.tsx +61 -0
- package/src/client/blocks/diagram/geometry.ts +151 -0
- package/src/client/blocks/diagram/index.tsx +293 -0
- package/src/client/blocks/diagram/layout.ts +202 -0
- package/src/client/blocks/diagram/theme.ts +127 -0
- package/src/client/blocks/forms.tsx +531 -0
- package/src/client/blocks/image.tsx +28 -0
- package/src/client/blocks/render-node.tsx +494 -0
- package/src/client/blocks/state.ts +65 -0
- package/src/client/blocks/svg.tsx +66 -0
- package/src/client/dom-fence.tsx +1123 -0
- package/src/client/echarts-engine.ts +9 -0
- package/src/client/echarts-lazy.ts +41 -0
- package/src/client/embed.ts +19 -0
- package/src/client/fence-render.tsx +266 -0
- package/src/client/gallery.ts +215 -0
- package/src/client/genui-runtime/diagnostics.ts +165 -0
- package/src/client/genui-runtime/index.ts +1 -0
- package/src/client/genui-runtime/limits.ts +54 -0
- package/src/client/genui-runtime/normalize.ts +329 -0
- package/src/client/genui-runtime/schema.ts +352 -0
- package/src/client/genui-runtime/value-utils.ts +67 -0
- package/src/client/guard.ts +1965 -0
- package/src/client/i18n/en.ts +335 -0
- package/src/client/i18n/index.ts +125 -0
- package/src/client/i18n/runtime.ts +130 -0
- package/src/client/i18n/zh.ts +330 -0
- package/src/client/index.tsx +228 -0
- package/src/client/inline.ts +106 -0
- package/src/client/interaction-store.ts +127 -0
- package/src/client/mermaid-core.ts +158 -0
- package/src/client/mermaid-lazy.ts +31 -0
- package/src/client/mermaid-safe.ts +122 -0
- package/src/client/panel-command.ts +138 -0
- package/src/client/panel-store.ts +471 -0
- package/src/client/panel.tsx +287 -0
- package/src/client/parse-partial.ts +170 -0
- package/src/client/primitive-adapter.ts +13 -0
- package/src/client/primitive-labels.ts +55 -0
- package/src/client/safe-math.ts +298 -0
- package/src/client/scene3d-core.ts +195 -0
- package/src/client/scene3d-lazy.ts +26 -0
- package/src/client/session-resolver.ts +33 -0
- package/src/client/source-fence.ts +70 -0
- package/src/client/spec.ts +896 -0
- package/src/client/standalone/asset-loader.ts +9 -0
- package/src/client/standalone/bootstrap-types.ts +11 -0
- package/src/client/standalone/katex-style.ts +1 -0
- package/src/client/standalone/primitive-adapter.tsx +94 -0
- package/src/client/standalone/runtime.tsx +27 -0
- package/src/client/submission-registry.ts +150 -0
- package/src/client/svg-fence.tsx +39 -0
- package/src/client/table-details.ts +151 -0
- package/src/client/templates.ts +322 -0
- package/src/client/toolview.tsx +67 -0
- package/src/client/walk-spec.ts +43 -0
- package/src/css-modules.d.ts +5 -0
- package/src/index.ts +9 -0
- package/src/plugin/chart-contract.ts +13 -0
- package/src/plugin/fence-feedback.ts +655 -0
- package/src/plugin/genui-diagnostic.ts +112 -0
- package/src/plugin/index.ts +224 -0
- package/src/plugin/inline-content-diagnostic.ts +178 -0
- package/src/plugin/invariant.ts +30 -0
- package/src/plugin/node-shims.d.ts +31 -0
- package/src/plugin/tool.ts +424 -0
- package/src/shared/fence-repair.ts +536 -0
- package/src/shared/fence-resolve.ts +80 -0
- package/tsconfig.json +35 -0
- package/tsdown.config.ts +275 -0
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Form family: radio aggregation + submit grading, switch, slider, IME-safe
|
|
3
|
+
* input/select/textarea. All state flows through the shared AnswersState.
|
|
4
|
+
* @module @changfenhuang/dsh-genui/client/blocks/forms
|
|
5
|
+
*/
|
|
6
|
+
import { renderInline } from '../inline.ts'
|
|
7
|
+
import { useEffect, useId, useRef, useState } from 'react'
|
|
8
|
+
import css from '../GenuiBlock.module.css'
|
|
9
|
+
import { GENUI_LIMITS } from '../genui-runtime/index.ts'
|
|
10
|
+
import { useT } from '../i18n/index.ts'
|
|
11
|
+
import { resolveSubmitState } from '../submission-registry.ts'
|
|
12
|
+
import type { RadioSubmissionMember } from '../submission-registry.ts'
|
|
13
|
+
import type { AnswersState, GenuiBlockProps } from './state.ts'
|
|
14
|
+
import type { GenuiInput, GenuiRadio, GenuiSelect, GenuiSlider, GenuiSubmit, GenuiSwitch, GenuiTextarea } from '../spec.ts'
|
|
15
|
+
|
|
16
|
+
export function RadioNode({ node, onAction, answers }: {
|
|
17
|
+
node: GenuiRadio
|
|
18
|
+
onAction?: GenuiBlockProps['onAction']
|
|
19
|
+
answers?: AnswersState | undefined
|
|
20
|
+
}) {
|
|
21
|
+
const action = node.action
|
|
22
|
+
const group = node.group
|
|
23
|
+
const grouped = group !== undefined
|
|
24
|
+
const options = node.options.slice(0, GENUI_LIMITS.maxOptions)
|
|
25
|
+
// No default selection unless the model explicitly sets `selected` — a
|
|
26
|
+
// pre-checked first option silently swallows the user's "keep the default"
|
|
27
|
+
// answer (the interaction state only records selected values). A DURABLE answer
|
|
28
|
+
// (restored from localStorage) wins over both. The parent key includes the
|
|
29
|
+
// reset round, so 重新作答 remounts this radio with a clean selection —
|
|
30
|
+
// no sync effect needed.
|
|
31
|
+
const restoredIndex = group !== undefined && answers?.answers[group] !== undefined
|
|
32
|
+
? options.indexOf(answers!.answers[group]!)
|
|
33
|
+
: -1
|
|
34
|
+
const [selected, setSelected] = useState<number | null>(restoredIndex >= 0 ? restoredIndex : (node.selected ?? null))
|
|
35
|
+
const uid = useId()
|
|
36
|
+
const locked = grouped && answers?.locked === true
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (group === undefined) return
|
|
39
|
+
// A model-provided default selection IS the answer — but only when the
|
|
40
|
+
// group has no durable answer yet (a restored user choice must win).
|
|
41
|
+
if (node.selected !== undefined && options[node.selected] !== undefined && answers?.answers[group] === undefined) {
|
|
42
|
+
answers?.setAnswer(group, options[node.selected]!)
|
|
43
|
+
}
|
|
44
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
45
|
+
}, [group, node.label, node.answer, node.explanation, node.options, node.selected])
|
|
46
|
+
return (
|
|
47
|
+
<div className={css.fieldGroup} role="radiogroup" aria-label={node.label}>
|
|
48
|
+
{node.label !== undefined && <span className={css.fieldLabel}>{renderInline(node.label, false)}</span>}
|
|
49
|
+
{options.map((opt, i) => (
|
|
50
|
+
<label key={i} className={css.radio}>
|
|
51
|
+
<input
|
|
52
|
+
type="radio"
|
|
53
|
+
name={`genui-radio-${uid}`}
|
|
54
|
+
checked={selected === i}
|
|
55
|
+
disabled={locked}
|
|
56
|
+
onChange={() => {
|
|
57
|
+
setSelected(i)
|
|
58
|
+
if (grouped) {
|
|
59
|
+
// Aggregation mode: record, do NOT round-trip per click.
|
|
60
|
+
answers?.setAnswer(group, opt)
|
|
61
|
+
} else if (action !== undefined && onAction !== undefined) {
|
|
62
|
+
onAction(action, { type: 'radio', value: opt })
|
|
63
|
+
}
|
|
64
|
+
}}
|
|
65
|
+
/>
|
|
66
|
+
<span>{renderInline(opt, false)}</span>
|
|
67
|
+
</label>
|
|
68
|
+
))}
|
|
69
|
+
</div>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 根据静态 radio member 获取正确选项的标签。 */
|
|
74
|
+
export function correctLabelOf(m: RadioSubmissionMember): string | undefined {
|
|
75
|
+
if (m.answer === undefined) return undefined
|
|
76
|
+
if (typeof m.answer === 'number') return m.options[m.answer]
|
|
77
|
+
return m.answer
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 汇总当前 block 的表单状态,并根据 resolver 的结果提交或本地判卷。 */
|
|
81
|
+
export function SubmitNode({ node, onAction, answers }: {
|
|
82
|
+
node: GenuiSubmit
|
|
83
|
+
onAction?: GenuiBlockProps['onAction']
|
|
84
|
+
answers?: AnswersState | undefined
|
|
85
|
+
}) {
|
|
86
|
+
const t = useT()
|
|
87
|
+
const recorded = answers?.answers ?? {}
|
|
88
|
+
const multiRecorded = answers?.multiAnswers ?? {}
|
|
89
|
+
const fields = answers?.fields ?? {}
|
|
90
|
+
const expected = node.groups
|
|
91
|
+
// One shared notion of "filled fields" for answered/ready/payload: non-blank
|
|
92
|
+
// values only, secrets (password inputs) never collected into submit.
|
|
93
|
+
const filledFields = Object.fromEntries(
|
|
94
|
+
Object.entries(fields).filter(([id, v]) => v.trim() !== '' && !answers?.secretFields.has(id)),
|
|
95
|
+
)
|
|
96
|
+
const resolved = answers === undefined ? { scope: [], answered: 0, total: expected?.length ?? 0, localGradeEligible: false, hasOutOfScopePayload: false }
|
|
97
|
+
: resolveSubmitState({
|
|
98
|
+
registry: answers.registry,
|
|
99
|
+
...(expected === undefined ? {} : { groups: expected }),
|
|
100
|
+
state: { answers: recorded, multiAnswers: multiRecorded, fields, secretFields: answers.secretFields },
|
|
101
|
+
})
|
|
102
|
+
const { scope, answered, total } = resolved
|
|
103
|
+
const canSendAction = node.action !== undefined && onAction !== undefined
|
|
104
|
+
const shouldGradeLocally = resolved.localGradeEligible && (!resolved.hasOutOfScopePayload || !canSendAction)
|
|
105
|
+
const submitted = answers?.locked === true
|
|
106
|
+
const collectedAnswers: Record<string, string | string[]> = {
|
|
107
|
+
...recorded,
|
|
108
|
+
...multiRecorded,
|
|
109
|
+
}
|
|
110
|
+
// Ready = enough answers AND the click can do something: either local
|
|
111
|
+
// grading, or a real action name + provider. A submit with neither is a
|
|
112
|
+
// display-only control — honest disabled affordance.
|
|
113
|
+
const ready = answered > 0 && answered >= total
|
|
114
|
+
&& (shouldGradeLocally || canSendAction)
|
|
115
|
+
|
|
116
|
+
if (submitted) {
|
|
117
|
+
// ── local grading result ──
|
|
118
|
+
const graded = scope.filter((member): member is RadioSubmissionMember => member.kind === 'radio' && recorded[member.key] !== undefined && member.answer !== undefined)
|
|
119
|
+
const score = graded.filter(member => recorded[member.key] === correctLabelOf(member)).length
|
|
120
|
+
return (
|
|
121
|
+
<div className={css.gradeWrap} data-genui-grade>
|
|
122
|
+
<div className={css.gradeScore}>
|
|
123
|
+
<span className={css.gradeScoreValue}>{score} / {graded.length}</span>
|
|
124
|
+
<span className={css.gradeScoreLabel}>{t('block.score')}{graded.length < scope.length ? t('block.scoreUngraded', { count: scope.length - graded.length }) : ''}</span>
|
|
125
|
+
</div>
|
|
126
|
+
<div className={css.gradeList}>
|
|
127
|
+
{scope.map(m => {
|
|
128
|
+
if (m.kind !== 'radio') return null
|
|
129
|
+
const entry = recorded[m.key]
|
|
130
|
+
if (entry === undefined) return null
|
|
131
|
+
const correct = correctLabelOf(m)
|
|
132
|
+
if (correct === undefined) {
|
|
133
|
+
return (
|
|
134
|
+
<div key={m.key} className={css.gradeItem}>
|
|
135
|
+
<span className={css.gradeQ}>{renderInline(m.label)}</span>
|
|
136
|
+
<span className={css.gradeAns}>{t('block.yourAnswer')}{renderInline(entry)}</span>
|
|
137
|
+
</div>
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
const isCorrect = entry === correct
|
|
141
|
+
return (
|
|
142
|
+
<div key={m.key} className={`${css.gradeItem} ${isCorrect ? css.gradeItemOk : css.gradeItemNo}`}>
|
|
143
|
+
<span className={css.gradeQ}>{renderInline(m.label)}</span>
|
|
144
|
+
<span className={css.gradeTag}>{isCorrect ? '✓' : '✗'}</span>
|
|
145
|
+
<span className={css.gradeAns}>
|
|
146
|
+
{t('block.yourAnswer')}{renderInline(entry)}
|
|
147
|
+
{!isCorrect && <span className={css.gradeRight}>{t('block.correctAnswer')}{renderInline(correct ?? '')}</span>}
|
|
148
|
+
</span>
|
|
149
|
+
{m.explanation !== undefined && <span className={css.gradeExp}>{renderInline(m.explanation)}</span>}
|
|
150
|
+
</div>
|
|
151
|
+
)
|
|
152
|
+
})}
|
|
153
|
+
</div>
|
|
154
|
+
<button
|
|
155
|
+
type="button"
|
|
156
|
+
className={`${css.button} ${css.ghost} ${css.submit}`}
|
|
157
|
+
onClick={() => {
|
|
158
|
+
answers?.clear()
|
|
159
|
+
if (node.resetAction !== undefined && onAction !== undefined) {
|
|
160
|
+
onAction(node.resetAction, { type: 'submit-reset', groups: expected ?? Object.keys(recorded) })
|
|
161
|
+
}
|
|
162
|
+
}}
|
|
163
|
+
>
|
|
164
|
+
{t('block.quizRetry')}
|
|
165
|
+
</button>
|
|
166
|
+
</div>
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return (
|
|
171
|
+
<div className={css.submitRow}>
|
|
172
|
+
<button
|
|
173
|
+
type="button"
|
|
174
|
+
className={`${css.button} ${css.primary} ${css.submit}`}
|
|
175
|
+
disabled={!ready}
|
|
176
|
+
onClick={ready ? () => {
|
|
177
|
+
if (shouldGradeLocally) {
|
|
178
|
+
// Local grading: immediate in-place result, no model round trip.
|
|
179
|
+
answers?.setLocked(true)
|
|
180
|
+
} else if (node.action !== undefined && onAction !== undefined) {
|
|
181
|
+
// `ready` already guarantees this branch, but the narrow keeps
|
|
182
|
+
// the optional-action type honest.
|
|
183
|
+
onAction(node.action, {
|
|
184
|
+
type: 'submit',
|
|
185
|
+
answers: collectedAnswers,
|
|
186
|
+
...(Object.keys(filledFields).length > 0 ? { fields: filledFields } : {}),
|
|
187
|
+
total,
|
|
188
|
+
answered,
|
|
189
|
+
})
|
|
190
|
+
}
|
|
191
|
+
} : undefined}
|
|
192
|
+
>
|
|
193
|
+
{renderInline(node.label, false)}
|
|
194
|
+
</button>
|
|
195
|
+
{total > 0 && <span className={css.submitHint} aria-live="polite">{t('block.selectedCount', { answered, total })}</span>}
|
|
196
|
+
</div>
|
|
197
|
+
)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Switch: toggle with local state. */
|
|
201
|
+
export function SwitchNode({ node, onAction }: { node: GenuiSwitch; onAction?: GenuiBlockProps['onAction'] }) {
|
|
202
|
+
const [on, setOn] = useState(node.checked === true)
|
|
203
|
+
const action = node.action
|
|
204
|
+
return (
|
|
205
|
+
<label className={css.switchRow}>
|
|
206
|
+
<span className={css.switchLabel}>{renderInline(node.label, false)}</span>
|
|
207
|
+
<button
|
|
208
|
+
type="button"
|
|
209
|
+
role="switch"
|
|
210
|
+
aria-checked={on}
|
|
211
|
+
className={`${css.switch} ${on ? css.switchOn : ''}`}
|
|
212
|
+
onClick={() => {
|
|
213
|
+
const next = !on
|
|
214
|
+
setOn(next)
|
|
215
|
+
if (action !== undefined && onAction !== undefined) onAction(action, { type: 'switch', checked: next })
|
|
216
|
+
}}
|
|
217
|
+
>
|
|
218
|
+
<span className={css.switchKnob} />
|
|
219
|
+
</button>
|
|
220
|
+
</label>
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Slider: range input for numeric form values (v2.9). Field-aligned: with an
|
|
225
|
+
* `id` the value persists across refresh and joins the sibling submit's
|
|
226
|
+
* `fields` collection (stored as the numeric string); a model-provided
|
|
227
|
+
* default registers at mount; a restored durable value wins. Dragging fires
|
|
228
|
+
* the action (block-level debounce collapses the drag into one delivery). */
|
|
229
|
+
export function SliderNode({ node, onAction, answers }: {
|
|
230
|
+
node: GenuiSlider
|
|
231
|
+
onAction?: GenuiBlockProps['onAction']
|
|
232
|
+
answers?: AnswersState | undefined
|
|
233
|
+
}) {
|
|
234
|
+
const action = node.action
|
|
235
|
+
const id = node.id
|
|
236
|
+
const restored = id !== undefined && answers?.fields[id] !== undefined
|
|
237
|
+
? Number(answers!.fields[id]!)
|
|
238
|
+
: NaN
|
|
239
|
+
const initial = Number.isFinite(restored) ? restored : node.value ?? node.min ?? 0
|
|
240
|
+
const [value, setValue] = useState<number>(initial)
|
|
241
|
+
const mounted = useRef(false)
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
if (mounted.current) return
|
|
244
|
+
mounted.current = true
|
|
245
|
+
if (id !== undefined) answers?.setField(id, String(initial))
|
|
246
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
247
|
+
}, [])
|
|
248
|
+
const send = (v: number): void => {
|
|
249
|
+
if (action !== undefined && onAction !== undefined) {
|
|
250
|
+
onAction(action, { type: 'slider', value: v, ...(id !== undefined ? { id } : {}) })
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return (
|
|
254
|
+
<label className={css.sliderRow}>
|
|
255
|
+
{node.label !== undefined && <span className={css.fieldLabel}>{renderInline(node.label, false)}</span>}
|
|
256
|
+
<input
|
|
257
|
+
type="range"
|
|
258
|
+
className={css.sliderInput}
|
|
259
|
+
min={node.min}
|
|
260
|
+
max={node.max}
|
|
261
|
+
step={node.step ?? 1}
|
|
262
|
+
value={value}
|
|
263
|
+
aria-label={node.label}
|
|
264
|
+
onChange={e => {
|
|
265
|
+
const v = Number(e.currentTarget.value)
|
|
266
|
+
setValue(v)
|
|
267
|
+
if (id !== undefined) answers?.setField(id, String(v))
|
|
268
|
+
send(v)
|
|
269
|
+
}}
|
|
270
|
+
/>
|
|
271
|
+
<span className={css.sliderValue}>{Math.round(value * 100) / 100}</span>
|
|
272
|
+
</label>
|
|
273
|
+
)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Reuse the DSH main input's three-layer IME protection (verified in the
|
|
277
|
+
* host InputBar): composition start arms a ref, composition end clears it
|
|
278
|
+
* 10ms later (Safari sends the closing keydown BEFORE compositionend), and
|
|
279
|
+
* every submit keydown re-checks the ref, the native `isComposing` flag,
|
|
280
|
+
* and `keyCode === 229`. A Chinese selection Enter must never submit. */
|
|
281
|
+
export function useImeComposing(): {
|
|
282
|
+
isComposing: () => boolean
|
|
283
|
+
onCompositionStart: () => void
|
|
284
|
+
onCompositionEnd: () => void
|
|
285
|
+
} {
|
|
286
|
+
const composing = useRef(false)
|
|
287
|
+
const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
288
|
+
useEffect(() => {
|
|
289
|
+
return () => {
|
|
290
|
+
if (timer.current !== null) clearTimeout(timer.current)
|
|
291
|
+
}
|
|
292
|
+
}, [])
|
|
293
|
+
return {
|
|
294
|
+
isComposing: () => composing.current,
|
|
295
|
+
onCompositionStart: () => {
|
|
296
|
+
composing.current = true
|
|
297
|
+
if (timer.current !== null) {
|
|
298
|
+
clearTimeout(timer.current)
|
|
299
|
+
timer.current = null
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
onCompositionEnd: () => {
|
|
303
|
+
if (timer.current !== null) clearTimeout(timer.current)
|
|
304
|
+
timer.current = setTimeout(() => {
|
|
305
|
+
composing.current = false
|
|
306
|
+
}, 10)
|
|
307
|
+
},
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function isImeSubmitKeydown(e: React.KeyboardEvent): boolean {
|
|
312
|
+
const native = e.nativeEvent
|
|
313
|
+
return native.isComposing === true || native.keyCode === 229
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Select: single choice from a dropdown, field-aligned (v2.8). With an `id`
|
|
317
|
+
* the chosen option persists across refresh and joins the sibling submit's
|
|
318
|
+
* `fields` collection; a model-provided `selected` default registers at
|
|
319
|
+
* mount; a restored durable value wins over both. Without any default a
|
|
320
|
+
* placeholder option shows — nothing is silently pre-registered (same
|
|
321
|
+
* philosophy as radio). */
|
|
322
|
+
export function SelectNode({ node, onAction, answers }: {
|
|
323
|
+
node: GenuiSelect
|
|
324
|
+
onAction?: GenuiBlockProps['onAction']
|
|
325
|
+
answers?: AnswersState | undefined
|
|
326
|
+
}) {
|
|
327
|
+
const t = useT()
|
|
328
|
+
const action = node.action
|
|
329
|
+
const id = node.id
|
|
330
|
+
const options = node.options.slice(0, GENUI_LIMITS.maxOptions)
|
|
331
|
+
const restored = id !== undefined && answers?.fields[id] !== undefined
|
|
332
|
+
? options.indexOf(answers!.fields[id]!)
|
|
333
|
+
: -1
|
|
334
|
+
const defaultValue = restored >= 0
|
|
335
|
+
? options[restored]!
|
|
336
|
+
: node.selected !== undefined && options[node.selected] !== undefined
|
|
337
|
+
? options[node.selected]!
|
|
338
|
+
: null
|
|
339
|
+
const [value, setValue] = useState<string | null>(defaultValue)
|
|
340
|
+
// Field invariant: a spec-provided default registers at mount.
|
|
341
|
+
const mounted = useRef(false)
|
|
342
|
+
useEffect(() => {
|
|
343
|
+
if (mounted.current) return
|
|
344
|
+
mounted.current = true
|
|
345
|
+
if (id !== undefined && defaultValue !== null) {
|
|
346
|
+
answers?.setField(id, defaultValue)
|
|
347
|
+
}
|
|
348
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
349
|
+
}, [])
|
|
350
|
+
const send = (v: string): void => {
|
|
351
|
+
if (action !== undefined && onAction !== undefined) {
|
|
352
|
+
onAction(action, { type: 'select', value: v, ...(id !== undefined ? { id } : {}) })
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return (
|
|
356
|
+
<label className={css.field}>
|
|
357
|
+
{node.label !== undefined && <span>{renderInline(node.label, false)}</span>}
|
|
358
|
+
<select
|
|
359
|
+
className={css.select}
|
|
360
|
+
value={value ?? ''}
|
|
361
|
+
onChange={e => {
|
|
362
|
+
const v = e.currentTarget.value
|
|
363
|
+
setValue(v)
|
|
364
|
+
if (id !== undefined) answers?.setField(id, v)
|
|
365
|
+
send(v)
|
|
366
|
+
}}
|
|
367
|
+
>
|
|
368
|
+
{value === null && <option value="" hidden disabled>{t('block.selectPlaceholder')}</option>}
|
|
369
|
+
{options.map((o, i) => <option key={i} value={o}>{o}</option>)}
|
|
370
|
+
</select>
|
|
371
|
+
</label>
|
|
372
|
+
)
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** Input: single-line field. Controlled (value tracked for persistence and
|
|
376
|
+
* submit collection when `id` is set). With `action`: Enter submits
|
|
377
|
+
* immediately (`{type:'input', value, submit:true}`), blur sends too —
|
|
378
|
+
* the user never has to click elsewhere for the value to reach the model.
|
|
379
|
+
* Enter during IME composition never submits. `inputType: 'password'`
|
|
380
|
+
* stays masked; its value is never persisted and never joins submit
|
|
381
|
+
* collection (secrets stay out of localStorage), while its own `action`
|
|
382
|
+
* still delivers on explicit user submit. */
|
|
383
|
+
export function InputNode({ node, onAction, answers }: {
|
|
384
|
+
node: GenuiInput
|
|
385
|
+
onAction?: GenuiBlockProps['onAction']
|
|
386
|
+
answers?: AnswersState | undefined
|
|
387
|
+
}) {
|
|
388
|
+
const action = node.action
|
|
389
|
+
const id = node.id
|
|
390
|
+
const secret = node.inputType === 'password'
|
|
391
|
+
const restored = !secret && id !== undefined ? answers?.fields[id] : undefined
|
|
392
|
+
// Initial value: durable state wins over the spec default. Secrets always
|
|
393
|
+
// restore as blank so a password can never survive a refresh.
|
|
394
|
+
const [value, setValue] = useState<string>(() => {
|
|
395
|
+
const initial = secret ? '' : (restored ?? node.value ?? '')
|
|
396
|
+
// Match the native color input's opaque hex value, including its black default.
|
|
397
|
+
return node.inputType === 'color'
|
|
398
|
+
? /^#[0-9a-f]{6}$/i.test(initial) ? initial.toLowerCase() : '#000000'
|
|
399
|
+
: initial
|
|
400
|
+
})
|
|
401
|
+
// Last value actually DELIVERED to the model: blur only sends when the
|
|
402
|
+
// value changed since the last delivery (a focus-in/focus-out with no edit
|
|
403
|
+
// used to fire a pointless action round trip). Seeded with the mount value
|
|
404
|
+
// so the very first unedited blur also stays silent.
|
|
405
|
+
const lastSent = useRef<string | null>(value)
|
|
406
|
+
const send = (submit: boolean): void => {
|
|
407
|
+
if (action !== undefined && onAction !== undefined) {
|
|
408
|
+
lastSent.current = value
|
|
409
|
+
onAction(action, { type: 'input', value, ...(id !== undefined ? { id } : {}), ...(submit ? { submit: true } : {}) })
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
const ime = useImeComposing()
|
|
413
|
+
// Register the displayed initial value, including the native color default.
|
|
414
|
+
const mounted = useRef(false)
|
|
415
|
+
useEffect(() => {
|
|
416
|
+
if (mounted.current) return
|
|
417
|
+
mounted.current = true
|
|
418
|
+
if (!secret && id !== undefined && value.trim() !== '') {
|
|
419
|
+
answers?.setField(id, value)
|
|
420
|
+
}
|
|
421
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
422
|
+
}, [])
|
|
423
|
+
// Secret fields are filtered from persistence and submit collection.
|
|
424
|
+
useEffect(() => {
|
|
425
|
+
if (secret && id !== undefined) answers?.registerSecretField(id)
|
|
426
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
427
|
+
}, [secret, id])
|
|
428
|
+
return (
|
|
429
|
+
<label className={css.field}>
|
|
430
|
+
{node.label !== undefined && <span>{renderInline(node.label, false)}</span>}
|
|
431
|
+
<input
|
|
432
|
+
className={css.input}
|
|
433
|
+
type={node.inputType ?? 'text'}
|
|
434
|
+
style={node.inputType === 'color' ? {
|
|
435
|
+
width: 64,
|
|
436
|
+
height: 40,
|
|
437
|
+
padding: 4,
|
|
438
|
+
boxSizing: 'border-box',
|
|
439
|
+
alignSelf: 'flex-start',
|
|
440
|
+
cursor: 'pointer',
|
|
441
|
+
} : undefined}
|
|
442
|
+
placeholder={node.placeholder}
|
|
443
|
+
value={value}
|
|
444
|
+
onChange={e => {
|
|
445
|
+
const v = e.currentTarget.value
|
|
446
|
+
setValue(v)
|
|
447
|
+
if (id !== undefined) answers?.setField(id, v)
|
|
448
|
+
}}
|
|
449
|
+
onBlur={() => {
|
|
450
|
+
if (value !== lastSent.current) send(false)
|
|
451
|
+
}}
|
|
452
|
+
onCompositionStart={ime.onCompositionStart}
|
|
453
|
+
onCompositionEnd={ime.onCompositionEnd}
|
|
454
|
+
onKeyDown={e => {
|
|
455
|
+
if (e.key !== 'Enter') return
|
|
456
|
+
if (ime.isComposing() || isImeSubmitKeydown(e)) return
|
|
457
|
+
e.preventDefault()
|
|
458
|
+
send(true)
|
|
459
|
+
}}
|
|
460
|
+
/>
|
|
461
|
+
</label>
|
|
462
|
+
)
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** Textarea: multi-line input; with `action`, blurring sends the value and
|
|
466
|
+
* Ctrl/Cmd+Enter submits immediately. Controlled when `id` is set (durable
|
|
467
|
+
* value + submit collection). Ctrl/Cmd+Enter during IME composition never
|
|
468
|
+
* submits. */
|
|
469
|
+
export function TextareaNode({ node, onAction, answers }: {
|
|
470
|
+
node: GenuiTextarea
|
|
471
|
+
onAction?: GenuiBlockProps['onAction']
|
|
472
|
+
answers?: AnswersState | undefined
|
|
473
|
+
}) {
|
|
474
|
+
const action = node.action
|
|
475
|
+
const id = node.id
|
|
476
|
+
// Durable state wins over the spec default (same contract as InputNode).
|
|
477
|
+
const restored = id !== undefined ? answers?.fields[id] : undefined
|
|
478
|
+
const [value, setValue] = useState<string>(() => restored ?? node.value ?? '')
|
|
479
|
+
// Last value delivered to the model: blur sends only on change. Seeded
|
|
480
|
+
// with the mount value so an unedited blur stays silent.
|
|
481
|
+
const lastSent = useRef<string | null>(value)
|
|
482
|
+
const send = (submit: boolean): void => {
|
|
483
|
+
if (action !== undefined && onAction !== undefined) {
|
|
484
|
+
lastSent.current = value
|
|
485
|
+
onAction(action, { type: 'textarea', value, ...(id !== undefined ? { id } : {}), ...(submit ? { submit: true } : {}) })
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
const ime = useImeComposing()
|
|
489
|
+
// Field invariant: a non-blank initial value registers at mount. When a
|
|
490
|
+
// durable value was restored, registering it is a no-op (setField dedupes);
|
|
491
|
+
// otherwise the spec default registers — never the other way around.
|
|
492
|
+
const mounted = useRef(false)
|
|
493
|
+
useEffect(() => {
|
|
494
|
+
if (mounted.current) return
|
|
495
|
+
mounted.current = true
|
|
496
|
+
if (id !== undefined && value.trim() !== '') {
|
|
497
|
+
answers?.setField(id, value)
|
|
498
|
+
}
|
|
499
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
500
|
+
}, [])
|
|
501
|
+
return (
|
|
502
|
+
<label className={css.field}>
|
|
503
|
+
{node.label !== undefined && <span>{renderInline(node.label, false)}</span>}
|
|
504
|
+
<textarea
|
|
505
|
+
className={css.textarea}
|
|
506
|
+
placeholder={node.placeholder}
|
|
507
|
+
rows={node.rows ?? 4}
|
|
508
|
+
value={value}
|
|
509
|
+
onChange={e => {
|
|
510
|
+
const v = e.currentTarget.value
|
|
511
|
+
setValue(v)
|
|
512
|
+
if (id !== undefined) answers?.setField(id, v)
|
|
513
|
+
}}
|
|
514
|
+
onBlur={() => {
|
|
515
|
+
if (value !== lastSent.current) send(false)
|
|
516
|
+
}}
|
|
517
|
+
onCompositionStart={ime.onCompositionStart}
|
|
518
|
+
onCompositionEnd={ime.onCompositionEnd}
|
|
519
|
+
onKeyDown={e => {
|
|
520
|
+
if (!(e.metaKey || e.ctrlKey) || e.key !== 'Enter') return
|
|
521
|
+
if (ime.isComposing() || isImeSubmitKeydown(e)) return
|
|
522
|
+
e.preventDefault()
|
|
523
|
+
send(true)
|
|
524
|
+
}}
|
|
525
|
+
/>
|
|
526
|
+
</label>
|
|
527
|
+
)
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/** Accordion: collapsible sections with local open state. Headings and
|
|
531
|
+
* bodies are wired via useId (`aria-controls`/`aria-labelledby`). */
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { renderInline } from '../inline.ts'
|
|
2
|
+
import { useState, type ReactNode } from 'react'
|
|
3
|
+
import css from '../GenuiBlock.module.css'
|
|
4
|
+
import { useT } from '../i18n/index.ts'
|
|
5
|
+
import type { GenuiImage } from '../spec.ts'
|
|
6
|
+
|
|
7
|
+
/** Native image display; source safety is enforced by repairGenuiSpec/safeMediaSrc. */
|
|
8
|
+
export function ImageNode({ node }: { node: GenuiImage }): ReactNode {
|
|
9
|
+
const t = useT()
|
|
10
|
+
const [failed, setFailed] = useState(false)
|
|
11
|
+
|
|
12
|
+
return (
|
|
13
|
+
<figure className={css.media}>
|
|
14
|
+
{node.alt !== undefined && <figcaption className={css.mediaLabel}>{renderInline(node.alt)}</figcaption>}
|
|
15
|
+
{failed
|
|
16
|
+
? <div className={css.mediaError} role="alert">{t('block.imageError')}</div>
|
|
17
|
+
: <img
|
|
18
|
+
className={css.mediaPlayer}
|
|
19
|
+
src={node.src}
|
|
20
|
+
alt={node.alt ?? t('block.image')}
|
|
21
|
+
loading="lazy"
|
|
22
|
+
decoding="async"
|
|
23
|
+
style={{ maxHeight: 'min(70vh, 720px)', objectFit: 'contain' }}
|
|
24
|
+
onError={() => setFailed(true)}
|
|
25
|
+
/>}
|
|
26
|
+
</figure>
|
|
27
|
+
)
|
|
28
|
+
}
|