@h-ai/ui 1.0.0-beta.30 → 1.0.0-beta.32

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.
Files changed (48) hide show
  1. package/README.en.md +10 -1183
  2. package/README.ja.md +9 -1182
  3. package/README.md +12 -1183
  4. package/auto-import.d.ts +4 -0
  5. package/auto-import.js +4 -0
  6. package/dist/components/compounds/Popover.svelte +1 -0
  7. package/dist/components/scenes/ai/AiDocumentEditor.svelte +73 -7
  8. package/dist/components/scenes/ai/document-download.js +49 -86
  9. package/dist/components/scenes/ai/document-parse.js +6 -28
  10. package/dist/components/scenes/ai/document-pdf-pages.d.ts +26 -0
  11. package/dist/components/scenes/ai/document-pdf-pages.js +111 -0
  12. package/dist/components/scenes/ai/document-pdf-worker.d.ts +11 -0
  13. package/dist/components/scenes/ai/document-pdf-worker.js +29 -0
  14. package/dist/components/scenes/ai/document-render-pool.d.ts +19 -0
  15. package/dist/components/scenes/ai/document-render-pool.js +20 -0
  16. package/dist/components/scenes/ai/document-render-worker.d.ts +13 -0
  17. package/dist/components/scenes/ai/document-render-worker.js +7 -0
  18. package/dist/components/scenes/ai/document-types.d.ts +2 -0
  19. package/dist/components/scenes/ai/index.d.ts +3 -0
  20. package/dist/components/scenes/ai/index.js +2 -0
  21. package/dist/components/scenes/ai/mind/AiMindMapEditor.svelte +519 -0
  22. package/dist/components/scenes/ai/mind/AiMindMapEditor.svelte.d.ts +8 -0
  23. package/dist/components/scenes/ai/mind/mind-adapter.d.ts +38 -0
  24. package/dist/components/scenes/ai/mind/mind-adapter.js +59 -0
  25. package/dist/components/scenes/ai/mind/mind-types.d.ts +116 -0
  26. package/dist/components/scenes/ai/mind/mind-types.js +1 -0
  27. package/dist/components/scenes/index.d.ts +1 -0
  28. package/dist/components/scenes/index.js +2 -0
  29. package/dist/components/scenes/tools/ArtifactWorkspace.svelte +561 -0
  30. package/dist/components/scenes/tools/ArtifactWorkspace.svelte.d.ts +17 -0
  31. package/dist/components/scenes/tools/ToolWorkspace.svelte +435 -0
  32. package/dist/components/scenes/tools/ToolWorkspace.svelte.d.ts +17 -0
  33. package/dist/components/scenes/tools/artifact-data.d.ts +35 -0
  34. package/dist/components/scenes/tools/artifact-data.js +127 -0
  35. package/dist/components/scenes/tools/artifact-markdown.d.ts +58 -0
  36. package/dist/components/scenes/tools/artifact-markdown.js +308 -0
  37. package/dist/components/scenes/tools/artifact-preview.d.ts +9 -0
  38. package/dist/components/scenes/tools/artifact-preview.js +31 -0
  39. package/dist/components/scenes/tools/index.d.ts +4 -0
  40. package/dist/components/scenes/tools/index.js +3 -0
  41. package/dist/components/scenes/tools/tool-form.d.ts +74 -0
  42. package/dist/components/scenes/tools/tool-form.js +179 -0
  43. package/dist/components/scenes/tools/workspace-types.d.ts +221 -0
  44. package/dist/components/scenes/tools/workspace-types.js +1 -0
  45. package/dist/messages/en-US.json +99 -1
  46. package/dist/messages/ja-JP.json +99 -1
  47. package/dist/messages/zh-CN.json +99 -1
  48. package/package.json +8 -2
@@ -0,0 +1,561 @@
1
+ <!--
2
+ @component ArtifactWorkspace
3
+ 复用文档、表格和图表组件的通用产物工作台;宿主提供读取、保存和授权。
4
+ @example
5
+ ```ts
6
+ import { mount } from 'svelte'
7
+ import { ArtifactWorkspace } from '@h-ai/ui'
8
+ const component = mount(ArtifactWorkspace, { target: document.body, props: {
9
+ artifacts: [{ id: 'report', title: '报告', kind: 'markdown', version: 1 }],
10
+ onread: async id => ({ id, title: '报告', kind: 'markdown', version: 1, content: '# 简介' }),
11
+ } })
12
+ ```
13
+ -->
14
+ <script lang='ts'>
15
+ import type { MarkdownRewriteRequest } from '../ai/document-types.js'
16
+ import type { MarkdownEditSession } from './artifact-markdown.js'
17
+ import type { ArtifactCreateRequest, ArtifactDocument, ArtifactSummary, ArtifactWorkspaceProps } from './workspace-types.js'
18
+ import { onDestroy, untrack } from 'svelte'
19
+ import { writeTextToClipboard } from '../../../internal/browser-safety.js'
20
+ import { uiM } from '../../../messages.js'
21
+ import { notifyError, notifyInfo, notifyWarning } from '../../../toast.svelte.js'
22
+ import { cn, getDataAttributes } from '../../../utils.js'
23
+ import Confirm from '../../compounds/Confirm.svelte'
24
+ import Button from '../../primitives/Button.svelte'
25
+ import AiDocumentEditor from '../ai/AiDocumentEditor.svelte'
26
+ import AiTableEditor from '../ai/table/AiTableEditor.svelte'
27
+ import Chart from '../charts/Chart.svelte'
28
+ import { artifactMediaUrl, newArtifactContent, readArtifactChart, readArtifactTable } from './artifact-data.js'
29
+ import { captureMarkdownEdit, readMarkdownSelection } from './artifact-markdown.js'
30
+ import { prepareHtmlPreview } from './artifact-preview.js'
31
+
32
+ const { artifacts, selectedId, onread, oncreate, onsave, onhistory, onrewrite, onselect, onerror, onleaveguard, class: className, ...rest }: ArtifactWorkspaceProps = $props()
33
+ const dataAttributes = $derived(getDataAttributes(rest))
34
+ let loaded = $state<ArtifactDocument>()
35
+ let activeId = $state('')
36
+ let draft = $state('')
37
+ let renderedContent = $state('')
38
+ let mode = $state<'preview' | 'source'>('preview')
39
+ let busy = $state(false)
40
+ let saving = $state(false)
41
+ let rewriting = $state(false)
42
+ let readFailed = $state(false)
43
+ let createOpen = $state(false)
44
+ let creating = $state(false)
45
+ let newTitle = $state('')
46
+ let newKind = $state<ArtifactCreateRequest['kind']>('markdown')
47
+ let structureChanged = $state(false)
48
+ let historical = $state(false)
49
+ let history = $state<readonly ArtifactSummary[]>([])
50
+ let historyOpen = $state(false)
51
+ let conflict = $state<number>()
52
+ let proposal = $state<{ content: string, baseDraft: string }>()
53
+ let rewriteInstruction = $state('')
54
+ let sourceSelection = $state('')
55
+ let previewHost = $state<HTMLElement | null>(null)
56
+ let tableEditor = $state<ReturnType<typeof AiTableEditor>>()
57
+ let visualEditable = $state(false)
58
+ let confirmDiscard = $state(false)
59
+ let nextSelection: { id: string, version?: number } | undefined
60
+ let leaveDecision: Promise<boolean> | undefined
61
+ let leaveResolve: ((allowed: boolean) => void) | undefined
62
+ let request: AbortController | undefined
63
+ let action: AbortController | undefined
64
+ let editing: MarkdownEditSession | undefined
65
+ let epoch = $state(0)
66
+ const dirty = $derived(!!loaded && draft !== (loaded.content ?? ''))
67
+ const canEdit = $derived(!!loaded?.editable && !!onsave && !historical && !creating && ['markdown', 'html', 'table', 'chart'].includes(loaded.kind))
68
+ const table = $derived(loaded?.kind === 'table'
69
+ ? readArtifactTable(draft)
70
+ : undefined)
71
+ const chart = $derived(loaded?.kind === 'chart'
72
+ ? readArtifactChart(draft)
73
+ : undefined)
74
+ const html = $derived(loaded?.kind === 'html' && mode === 'preview'
75
+ ? prepareHtmlPreview(draft)
76
+ : '')
77
+ const mediaUrl = $derived(artifactMediaUrl(loaded?.mediaUrl))
78
+ const downloadUrl = $derived(artifactMediaUrl(loaded?.downloadUrl))
79
+ const rewriteActions = $derived([
80
+ {
81
+ id: 'improve',
82
+ label: uiM('artifact_rewrite_improve'),
83
+ },
84
+ {
85
+ id: 'shorten',
86
+ label: uiM('artifact_rewrite_shorten'),
87
+ },
88
+ ])
89
+
90
+ function failed(error: unknown) {
91
+ notifyError(error, uiM('artifact_failed'))
92
+ onerror?.(error)
93
+ }
94
+
95
+ function accept(document: ArtifactDocument) {
96
+ loaded = document
97
+ draft = document.content ?? ''
98
+ renderedContent = draft
99
+ conflict = undefined
100
+ proposal = undefined
101
+ history = []
102
+ historyOpen = false
103
+ structureChanged = false
104
+ sourceSelection = ''
105
+ epoch++
106
+ }
107
+
108
+ async function load(id: string, version?: number) {
109
+ request?.abort()
110
+ action?.abort()
111
+ const current = new AbortController()
112
+ request = current
113
+ activeId = id
114
+ history = []
115
+ historyOpen = false
116
+ busy = true
117
+ saving = false
118
+ rewriting = false
119
+ readFailed = false
120
+ try {
121
+ const document = await onread(id, version, current.signal)
122
+ if (current.signal.aborted)
123
+ return
124
+ accept(document)
125
+ historical = version !== undefined
126
+ mode = 'preview'
127
+ onselect?.(id)
128
+ return current.signal
129
+ }
130
+ catch (error) {
131
+ if (!current.signal.aborted) {
132
+ readFailed = true
133
+ failed(error)
134
+ }
135
+ }
136
+ finally {
137
+ if (!current.signal.aborted)
138
+ busy = false
139
+ }
140
+ return undefined
141
+ }
142
+
143
+ function select(id: string, version?: number) {
144
+ tableEditor?.flushPendingTableChange()
145
+ if (dirty || structureChanged) {
146
+ nextSelection = { id, version }
147
+ confirmDiscard = true
148
+ }
149
+ else {
150
+ void load(id, version)
151
+ }
152
+ }
153
+
154
+ function confirmLeave(): Promise<boolean> {
155
+ tableEditor?.flushPendingTableChange()
156
+ if (!dirty && !structureChanged && !saving && !rewriting && !creating)
157
+ return Promise.resolve(true)
158
+ if (leaveDecision)
159
+ return leaveDecision
160
+ nextSelection = undefined
161
+ confirmDiscard = true
162
+ leaveDecision = new Promise((resolve) => {
163
+ leaveResolve = resolve
164
+ })
165
+ return leaveDecision
166
+ }
167
+
168
+ function resolveDiscard(allowed: boolean) {
169
+ const next = nextSelection
170
+ nextSelection = undefined
171
+ if (leaveResolve && allowed) {
172
+ // 用户已确认丢弃,随后原生提交或导航不应重复弹出硬离开提示。
173
+ if (loaded) {
174
+ draft = loaded.content ?? ''
175
+ renderedContent = draft
176
+ structureChanged = false
177
+ epoch++
178
+ }
179
+ action?.abort()
180
+ saving = false
181
+ rewriting = false
182
+ creating = false
183
+ }
184
+ leaveResolve?.(allowed)
185
+ leaveResolve = undefined
186
+ leaveDecision = undefined
187
+ if (allowed && next)
188
+ void load(next.id, next.version)
189
+ }
190
+
191
+ function beforeUnload(event: BeforeUnloadEvent) {
192
+ tableEditor?.flushPendingTableChange()
193
+ if (dirty || structureChanged || saving || rewriting || creating) {
194
+ event.preventDefault()
195
+ event.returnValue = ''
196
+ }
197
+ }
198
+
199
+ $effect(() => {
200
+ const register = onleaveguard
201
+ register?.(confirmLeave)
202
+ return () => register?.(undefined)
203
+ })
204
+
205
+ $effect(() => {
206
+ const id = selectedId ?? artifacts[0]?.id
207
+ if (id && id !== untrack(() => activeId) && !untrack(() => creating))
208
+ untrack(() => select(id))
209
+ })
210
+
211
+ $effect(() => {
212
+ const host = previewHost
213
+ const source = renderedContent
214
+ if (host && loaded?.kind === 'markdown' && mode === 'preview') {
215
+ editing = untrack(() => captureMarkdownEdit(source, host))
216
+ visualEditable = editing.enabled
217
+ }
218
+ })
219
+
220
+ function readVisual() {
221
+ if (!editing)
222
+ return
223
+ const result = editing.read()
224
+ structureChanged = !result.success
225
+ if (result.success)
226
+ draft = result.content
227
+ else notifyWarning(uiM('artifact_structure_changed'))
228
+ }
229
+
230
+ function changeMode(next: 'preview' | 'source') {
231
+ tableEditor?.flushPendingTableChange()
232
+ if (structureChanged) {
233
+ notifyWarning(uiM('artifact_structure_changed'))
234
+ return
235
+ }
236
+ renderedContent = draft
237
+ sourceSelection = ''
238
+ mode = next
239
+ epoch++
240
+ }
241
+
242
+ async function save() {
243
+ tableEditor?.flushPendingTableChange()
244
+ if (!loaded || !onsave || !canEdit || structureChanged || saving || rewriting)
245
+ return
246
+ action?.abort()
247
+ const current = new AbortController()
248
+ action = current
249
+ saving = true
250
+ try {
251
+ const result = await onsave({ id: loaded.id, baseVersion: loaded.version, content: draft }, current.signal)
252
+ if (current.signal.aborted)
253
+ return
254
+ if (result.status === 'conflict') {
255
+ conflict = result.currentVersion
256
+ notifyWarning(uiM('artifact_conflict'))
257
+ }
258
+ else {
259
+ accept(result.document)
260
+ notifyInfo(uiM('artifact_saved'))
261
+ }
262
+ }
263
+ catch (error) {
264
+ if (!current.signal.aborted)
265
+ failed(error)
266
+ }
267
+ finally {
268
+ if (!current.signal.aborted)
269
+ saving = false
270
+ }
271
+ }
272
+
273
+ async function showHistory() {
274
+ if (!loaded || !onhistory || saving || rewriting)
275
+ return
276
+ action?.abort()
277
+ const current = new AbortController()
278
+ action = current
279
+ try {
280
+ const result = await onhistory(loaded.id, current.signal)
281
+ if (!current.signal.aborted) {
282
+ history = result
283
+ historyOpen = true
284
+ }
285
+ }
286
+ catch (error) {
287
+ if (!current.signal.aborted)
288
+ failed(error)
289
+ }
290
+ }
291
+
292
+ async function rewrite(selection: MarkdownRewriteRequest) {
293
+ if (!loaded || !onrewrite || rewriting || saving || structureChanged)
294
+ return
295
+ const selected = mode === 'source'
296
+ ? { success: true as const, selectedText: selection.selectedText }
297
+ : previewHost && selection.selectionSnapshot
298
+ ? readMarkdownSelection(draft, previewHost, selection.selectionSnapshot)
299
+ : { success: false as const }
300
+ if (!selected.success || !selected.selectedText || !draft.includes(selected.selectedText)) {
301
+ notifyWarning(uiM('artifact_rewrite_source_hint'))
302
+ return
303
+ }
304
+ action?.abort()
305
+ const current = new AbortController()
306
+ action = current
307
+ rewriting = true
308
+ const baseDraft = draft
309
+ try {
310
+ const result = await onrewrite({
311
+ id: loaded.id,
312
+ baseVersion: loaded.version,
313
+ content: draft,
314
+ selectedText: selected.selectedText,
315
+ instruction: rewriteInstruction.trim() || rewriteActions.find(item => item.id === selection.actionId)?.label || selection.actionId,
316
+ }, current.signal)
317
+ if (!current.signal.aborted)
318
+ proposal = { content: result.content, baseDraft }
319
+ }
320
+ catch (error) {
321
+ if (!current.signal.aborted)
322
+ failed(error)
323
+ }
324
+ finally {
325
+ if (!current.signal.aborted)
326
+ rewriting = false
327
+ }
328
+ }
329
+
330
+ function selectSource(event: Event & { currentTarget: HTMLTextAreaElement }) {
331
+ const input = event.currentTarget
332
+ if (input.value !== draft.replace(/\r\n?/g, '\n')) {
333
+ sourceSelection = ''
334
+ return
335
+ }
336
+ // textarea 按 LF 计算选区,转换回原始 CRLF 偏移,提交片段必须逐字来自源码。
337
+ const offsets = [0]
338
+ for (let index = 0; index < draft.length; index++) {
339
+ if (draft[index] === '\r' && draft[index + 1] === '\n')
340
+ index++
341
+ offsets.push(index + 1)
342
+ }
343
+ sourceSelection = draft.slice(offsets[input.selectionStart], offsets[input.selectionEnd])
344
+ }
345
+
346
+ async function useHistorical() {
347
+ if (!loaded)
348
+ return
349
+ const source = draft
350
+ const id = loaded.id
351
+ const accepted = await load(id)
352
+ if (accepted && !accepted.aborted) {
353
+ draft = source
354
+ renderedContent = source
355
+ epoch++
356
+ }
357
+ }
358
+
359
+ async function copyDraft(visible = false) {
360
+ const text = visible
361
+ ? previewHost?.innerText ?? previewHost?.textContent ?? draft
362
+ : draft
363
+ if (await writeTextToClipboard(text))
364
+ notifyInfo(uiM('artifact_copy_success'))
365
+ else notifyWarning(uiM('artifact_copy_failed'))
366
+ }
367
+
368
+ async function create(event: SubmitEvent) {
369
+ event.preventDefault()
370
+ if (!oncreate || creating)
371
+ return
372
+ tableEditor?.flushPendingTableChange()
373
+ if (dirty || structureChanged) {
374
+ notifyWarning(uiM('artifact_save_first'))
375
+ return
376
+ }
377
+ action?.abort()
378
+ const current = new AbortController()
379
+ action = current
380
+ creating = true
381
+ try {
382
+ const document = await oncreate({
383
+ title: newTitle.trim(),
384
+ kind: newKind,
385
+ content: newArtifactContent(newKind, newTitle.trim(), uiM('artifact_template_body'), uiM('artifact_template_name'), uiM('artifact_template_value'), uiM('artifact_template_click')),
386
+ }, current.signal)
387
+ if (!current.signal.aborted) {
388
+ accept(document)
389
+ activeId = document.id
390
+ historical = false
391
+ mode = 'preview'
392
+ createOpen = false
393
+ onselect?.(document.id)
394
+ }
395
+ }
396
+ catch (error) {
397
+ if (!current.signal.aborted)
398
+ failed(error)
399
+ }
400
+ finally {
401
+ if (!current.signal.aborted)
402
+ creating = false
403
+ }
404
+ }
405
+
406
+ onDestroy(() => {
407
+ request?.abort()
408
+ action?.abort()
409
+ leaveResolve?.(false)
410
+ })
411
+ </script>
412
+
413
+ <svelte:window onbeforeunload={beforeUnload} />
414
+
415
+ <section {...dataAttributes} class={cn('hai-artifact-workspace flex min-h-0 min-w-0 flex-1 flex-col gap-3', className)} aria-label={uiM('artifact_workspace')}>
416
+ <div class='flex flex-wrap items-end gap-3'>
417
+ <label class='grid w-full min-w-0 gap-1 text-sm sm:min-w-56 sm:flex-1'>
418
+ {uiM('artifact_choose')}
419
+ <select class='select w-full min-w-0' value={activeId} disabled={saving || creating} onchange={(event) => {
420
+ const next = event.currentTarget.value
421
+ event.currentTarget.value = activeId
422
+ select(next)
423
+ }}>
424
+ {#if !artifacts.length}<option value=''>{uiM('artifact_empty')}</option>{/if}
425
+ {#each artifacts as item (item.id)}<option value={item.id}>{item.title}</option>{/each}
426
+ </select>
427
+ </label>
428
+ {#if oncreate}<Button size='sm' disabled={busy || saving || creating} onclick={() => { createOpen = !createOpen }}>{uiM('artifact_new')}</Button>{/if}
429
+ {#if loaded && !busy && !readFailed}
430
+ <span class='text-sm text-base-content/60'>{uiM('artifact_version', { version: loaded.version })}{dirty
431
+ ? ` · ${uiM('artifact_unsaved')}`
432
+ : ''}</span>
433
+ {#if downloadUrl}<a class='btn btn-ghost btn-sm' href={downloadUrl} download>{uiM('artifact_download')}</a>{/if}
434
+ {#if onhistory}<Button size='sm' variant='ghost' onclick={showHistory} disabled={saving}>{uiM('artifact_history')}</Button>{/if}
435
+ {#if canEdit}<Button size='sm' variant='primary' onclick={save} disabled={!dirty || saving || rewriting || structureChanged} loading={saving}>{uiM('artifact_save')}</Button>{/if}
436
+ {/if}
437
+ </div>
438
+ {#if createOpen && oncreate}
439
+ <form class='flex flex-wrap items-end gap-3 rounded-lg border border-base-300 p-3' onsubmit={create}>
440
+ <label class='grid w-full min-w-0 gap-1 text-sm sm:min-w-56 sm:flex-1'>{uiM('artifact_title')}<input class='input w-full' required maxlength={160} bind:value={newTitle} disabled={creating} /></label>
441
+ <label class='grid gap-1 text-sm'>{uiM('artifact_kind')}
442
+ <select class='select' bind:value={newKind} disabled={creating}>
443
+ <option value='markdown'>Markdown</option><option value='html'>HTML</option>
444
+ <option value='table'>{uiM('artifact_kind_table')}</option><option value='chart'>{uiM('artifact_kind_chart')}</option>
445
+ </select>
446
+ </label>
447
+ <Button type='submit' variant='primary' loading={creating} disabled={creating} submitHint>{uiM('artifact_create')}</Button>
448
+ </form>
449
+ {/if}
450
+ {#if busy}
451
+ <div class='grid min-h-48 place-items-center text-base-content/60' role='status'>{uiM('common_loading')}</div>
452
+ {:else if readFailed}
453
+ <Button onclick={() => load(activeId)}>{uiM('artifact_retry')}</Button>
454
+ {:else if loaded}
455
+ {#if structureChanged}
456
+ <div class='flex flex-wrap items-center gap-2 rounded-lg border border-base-300 p-3 text-sm' role='status'>
457
+ <span class='flex-1'>{uiM('artifact_edit_recovery')}</span>
458
+ <Button size='sm' variant='ghost' onclick={() => copyDraft(true)}>{uiM('artifact_copy_visible')}</Button>
459
+ <Button size='sm' onclick={() => select(activeId)}>{uiM('artifact_reload')}</Button>
460
+ </div>
461
+ {/if}
462
+ {#if conflict !== undefined}
463
+ <div class='flex flex-wrap items-center gap-2 rounded-lg border border-base-300 p-3 text-sm' role='status'>
464
+ <span class='flex-1'>{uiM('artifact_conflict_detail', { version: conflict })}</span>
465
+ <Button size='sm' variant='ghost' onclick={() => copyDraft()}>{uiM('artifact_copy_draft')}</Button>
466
+ <Button size='sm' onclick={() => select(activeId)}>{uiM('artifact_reload')}</Button>
467
+ </div>
468
+ {/if}
469
+ {#if historyOpen}
470
+ <div class='flex flex-wrap items-center gap-2 rounded-lg border border-base-300 p-3' aria-label={uiM('artifact_history')}>
471
+ {#each history as item (`${item.id}:${item.version}`)}<Button size='sm' variant='ghost' onclick={() => select(item.id, item.version)}>{uiM('artifact_version', { version: item.version })}</Button>{/each}
472
+ <Button size='sm' variant='ghost' onclick={() => { historyOpen = false }}>{uiM('common_close')}</Button>
473
+ </div>
474
+ {/if}
475
+ {#if historical}
476
+ <div class='flex flex-wrap items-center gap-2 text-sm' role='status'><span>{uiM('artifact_historical')}</span>
477
+ <Button size='sm' onclick={() => select(activeId)}>{uiM('artifact_latest')}</Button>
478
+ {#if onsave}<Button size='sm' variant='ghost' onclick={useHistorical}>{uiM('artifact_restore_draft')}</Button>{/if}
479
+ </div>
480
+ {/if}
481
+ {#if loaded.content !== undefined}
482
+ <div class='flex flex-wrap items-center gap-2'>
483
+ <button type='button' class={mode === 'preview'
484
+ ? 'btn btn-sm btn-primary'
485
+ : 'btn btn-sm btn-ghost'} onclick={() => changeMode('preview')} aria-pressed={mode === 'preview'}>{uiM('artifact_preview')}</button>
486
+ <button type='button' class={mode === 'source'
487
+ ? 'btn btn-sm btn-primary'
488
+ : 'btn btn-sm btn-ghost'} onclick={() => changeMode('source')} aria-pressed={mode === 'source'}>{uiM('artifact_source')}</button>
489
+ {#if onrewrite && canEdit && loaded.kind === 'markdown'}
490
+ <input class='input input-sm w-full min-w-0 flex-none sm:w-auto sm:flex-1' aria-label={uiM('artifact_rewrite_instruction')} placeholder={uiM('artifact_rewrite_instruction')} bind:value={rewriteInstruction} />
491
+ {#if mode === 'source'}
492
+ <Button size='sm' disabled={!sourceSelection.trim() || saving || rewriting}
493
+ onclick={() => rewrite({ actionId: 'improve', content: draft, selectedText: sourceSelection })}>{uiM('artifact_rewrite_selection')}</Button>
494
+ {/if}
495
+ {/if}
496
+ </div>
497
+ {/if}
498
+ {#if proposal !== undefined}
499
+ <section class='flex min-h-72 flex-col gap-2 rounded-lg border border-base-300 p-3' aria-label={uiM('artifact_proposal')}>
500
+ <div class='flex flex-wrap items-center gap-2'><span class='flex-1 text-sm font-medium'>{uiM(proposal.baseDraft === draft
501
+ ? 'artifact_proposal'
502
+ : 'artifact_proposal_stale')}</span>
503
+ <Button size='sm' variant='primary' disabled={proposal.baseDraft !== draft} onclick={() => {
504
+ if (!proposal || proposal.baseDraft !== draft)
505
+ return
506
+ draft = proposal.content
507
+ sourceSelection = ''
508
+ renderedContent = draft
509
+ proposal = undefined
510
+ epoch++
511
+ }}>{uiM('artifact_apply')}</Button>
512
+ <Button size='sm' variant='ghost' onclick={() => { proposal = undefined }}>{uiM('artifact_discard')}</Button>
513
+ </div>
514
+ <AiDocumentEditor content={proposal.content} showOutline={false} showToolbar={false} class='min-h-56' />
515
+ </section>
516
+ {/if}
517
+ {#if mode === 'source' && loaded.content !== undefined}
518
+ <label class='flex min-h-80 min-w-0 flex-1 flex-col gap-1 text-sm'>
519
+ <span class='sr-only'>{uiM('artifact_source')}</span>
520
+ <textarea class='textarea min-h-80 w-full flex-1 font-mono text-sm' readonly={!canEdit || saving || rewriting} bind:value={draft}
521
+ onselect={selectSource} oninput={() => { sourceSelection = '' }} spellcheck='false'></textarea>
522
+ </label>
523
+ {:else if loaded.kind === 'markdown'}
524
+ <p class='text-xs text-base-content/60'>{uiM('artifact_markdown_hint')}</p>
525
+ {#key epoch}
526
+ <AiDocumentEditor content={renderedContent} title={loaded.title} sourceKind='document' showOutline initialOutlineCollapsed showToolbar
527
+ editable={canEdit && visualEditable && !saving && !rewriting} bind:previewHost onpreviewinput={readVisual}
528
+ onrewrite={onrewrite && canEdit
529
+ ? rewrite
530
+ : undefined} {rewriteActions} rewritePending={rewriting}
531
+ onhistory={onhistory ? showHistory : undefined} class='min-h-80 flex-1' />
532
+ {/key}
533
+ {:else if loaded.kind === 'table' && table}
534
+ <div class='flex min-h-80 min-w-0 flex-1 flex-col' oninput={() => tableEditor?.flushPendingTableChange()}>
535
+ <AiTableEditor title={loaded.title} tableData={table} editable={canEdit && !saving} bind:this={tableEditor}
536
+ ontablechange={(change) => { draft = JSON.stringify(change.nextData, null, 2) }} class='min-h-80 flex-1' />
537
+ </div>
538
+ {:else if loaded.kind === 'chart' && chart}
539
+ <Chart {...chart} title={loaded.title} />
540
+ {:else if loaded.kind === 'html'}
541
+ <p class='text-xs text-base-content/60'>{uiM('artifact_html_hint')}</p>
542
+ <iframe class='min-h-96 w-full flex-1 rounded-lg border border-base-300 bg-white' title={loaded.title}
543
+ srcdoc={html} sandbox='allow-scripts' referrerpolicy='no-referrer'
544
+ allow="camera 'none'; microphone 'none'; geolocation 'none'; clipboard-read 'none'; clipboard-write 'none'"></iframe>
545
+ {:else if loaded.kind === 'image' && mediaUrl}
546
+ <img src={mediaUrl} alt={loaded.title} class='max-h-[70vh] max-w-full self-center rounded-lg object-contain' />
547
+ {:else if loaded.kind === 'audio' && mediaUrl}
548
+ <audio controls preload='metadata' src={mediaUrl} class='w-full'><track kind='captions' /></audio>
549
+ {:else if loaded.kind === 'video' && mediaUrl}
550
+ <video controls preload='metadata' src={mediaUrl} class='max-h-[70vh] w-full rounded-lg'><track kind='captions' /></video>
551
+ {:else}
552
+ <div class='rounded-lg border border-dashed border-base-300 p-6 text-sm text-base-content/60'>{uiM('artifact_download_only')}</div>
553
+ {/if}
554
+ {:else}
555
+ <div class='grid min-h-48 place-items-center text-sm text-base-content/60'>{uiM('artifact_empty')}</div>
556
+ {/if}
557
+ {#if confirmDiscard}
558
+ <Confirm bind:open={confirmDiscard} title={uiM('artifact_unsaved')} message={uiM('artifact_discard_confirm')}
559
+ confirmText={uiM('artifact_discard')} onconfirm={() => resolveDiscard(true)} oncancel={() => resolveDiscard(false)} />
560
+ {/if}
561
+ </section>
@@ -0,0 +1,17 @@
1
+ import type { ArtifactWorkspaceProps } from './workspace-types.js';
2
+ /**
3
+ * ArtifactWorkspace
4
+ * 复用文档、表格和图表组件的通用产物工作台;宿主提供读取、保存和授权。
5
+ * @example
6
+ * ```ts
7
+ * import { mount } from 'svelte'
8
+ * import { ArtifactWorkspace } from '@h-ai/ui'
9
+ * const component = mount(ArtifactWorkspace, { target: document.body, props: {
10
+ * artifacts: [{ id: 'report', title: '报告', kind: 'markdown', version: 1 }],
11
+ * onread: async id => ({ id, title: '报告', kind: 'markdown', version: 1, content: '# 简介' }),
12
+ * } })
13
+ * ```
14
+ */
15
+ declare const ArtifactWorkspace: import("svelte").Component<ArtifactWorkspaceProps, {}, "">;
16
+ type ArtifactWorkspace = ReturnType<typeof ArtifactWorkspace>;
17
+ export default ArtifactWorkspace;