@lincyaw/workbench 0.2.0-rc.30 → 0.2.0-rc.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.
- package/package.json +4 -4
- package/src/document-editor/DocumentStepNarration.tsx +23 -19
- package/src/document-editor/NarrationEditor.tsx +54 -20
- package/src/document-editor/focus.ts +0 -2
- package/src/document-editor/narration-schema.tsx +11 -6
- package/src/document-editor/narration.ts +19 -3
- package/src/panels/TranscriptionAdoption.tsx +3 -2
- package/src/workbench.ts +46 -57
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/workbench",
|
|
3
|
-
"version": "0.2.0-rc.
|
|
3
|
+
"version": "0.2.0-rc.32",
|
|
4
4
|
"description": "Presmium's workbench panels in React (the documents, a document edited on its rendered page with comments and version history) with the workspace service's client and the preview protocol's workbench side; @lincyaw/dsh places them in presmium studio (experimental)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"@tiptap/pm": "3.32.0",
|
|
56
56
|
"katex": "0.18.10",
|
|
57
57
|
"react-resizable-panels": "4.14.5",
|
|
58
|
-
"@lincyaw/
|
|
59
|
-
"@lincyaw/
|
|
58
|
+
"@lincyaw/kernel": "1.0.0-rc.32",
|
|
59
|
+
"@lincyaw/design": "1.0.0-rc.32"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
62
|
"react": "^18.3.1",
|
|
@@ -67,6 +67,6 @@
|
|
|
67
67
|
"@types/react-dom": "18.3.7",
|
|
68
68
|
"react": "^18.3.1",
|
|
69
69
|
"react-dom": "^18.3.1",
|
|
70
|
-
"@lincyaw/compiler": "1.0.0-rc.
|
|
70
|
+
"@lincyaw/compiler": "1.0.0-rc.32"
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { useRef, useState } from 'react'
|
|
2
|
-
import type { NarrationNode, TeachingDocument } from '@lincyaw/kernel'
|
|
2
|
+
import type { Narration, NarrationNode, TeachingDocument } from '@lincyaw/kernel'
|
|
3
3
|
import { randomUUID } from '@lincyaw/kernel'
|
|
4
4
|
import { PmIconButton } from '../components.tsx'
|
|
5
5
|
import { useWorkbench, useWorkbenchState } from '../context.tsx'
|
|
6
6
|
import { isAgentChanging } from '../workbench.ts'
|
|
7
7
|
import { NarrationEditor } from './NarrationEditor.tsx'
|
|
8
8
|
import { DOCUMENT_EDITOR_MESSAGES } from './messages.ts'
|
|
9
|
-
import { narrationFieldFocus } from './narration.ts'
|
|
9
|
+
import { narrationFieldFocus, narrationsToNative, nativeToNarrations } from './narration.ts'
|
|
10
10
|
import type { NativeNarrationEditor } from './narration-schema.tsx'
|
|
11
11
|
import { StepProductionEditor } from '../StepProductionEditor.tsx'
|
|
12
12
|
|
|
@@ -24,28 +24,32 @@ export function narrationDisplay(content: readonly NarrationNode[]): string {
|
|
|
24
24
|
return content.map(node => node.type === 'text' ? node.text : node.type === 'pronunciation' ? node.display : '').join('')
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
function NarrationField({ identity, reset, blockId, stepId,
|
|
28
|
-
identity: string, reset: number, blockId?: string, stepId: string,
|
|
27
|
+
function NarrationField({ identity, reset, blockId, stepId, content, readOnly, advanced, onPause }: {
|
|
28
|
+
identity: string, reset: number, blockId?: string, stepId: string, content: Narration[], readOnly: boolean, advanced: boolean, onPause(): void,
|
|
29
29
|
}) {
|
|
30
30
|
const workbench = useWorkbench(), locale = useWorkbenchState(state => state.locale)
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
return
|
|
35
|
-
}
|
|
36
|
-
const id = narrationId ?? reservedId
|
|
37
|
-
const target = () => narrationId ?? workbench.getDocument(identity)?.steps[stepId]?.narrationIds[slot] ?? id
|
|
31
|
+
const [emptyId] = useState(() => randomUUID())
|
|
32
|
+
const read = () => {
|
|
33
|
+
const document = workbench.getDocument(identity), step = document?.steps[stepId]
|
|
34
|
+
return step ? step.narrationIds.map(id => ({ id, content: document!.narrations[id]!.content })) : undefined
|
|
35
|
+
}
|
|
38
36
|
const registration = useRef<() => void>()
|
|
39
37
|
const fieldEditor = useRef<NativeNarrationEditor>()
|
|
40
|
-
const owner = `narration:${identity}:${reset}:${JSON.stringify(blockId ?? null)}:${stepId}
|
|
41
|
-
return <NarrationEditor identity={identity} reset={reset}
|
|
42
|
-
getContent={
|
|
38
|
+
const owner = `narration:${identity}:${reset}:${JSON.stringify(blockId ?? null)}:${stepId}`
|
|
39
|
+
return <NarrationEditor identity={identity} reset={reset} fieldId={stepId} content={content} locale={locale} readOnly={readOnly} advanced={advanced} blockMenu
|
|
40
|
+
getContent={read} toNative={value => narrationsToNative(value, emptyId)} fromNative={nativeToNarrations}
|
|
41
|
+
cueOwners={() => {
|
|
42
|
+
const document = workbench.getDocument(identity)
|
|
43
|
+
const retained = new Set(Object.values(document?.steps ?? {}).filter(step => step.id !== stepId).flatMap(step => step.narrationIds))
|
|
44
|
+
return new Map([...retained].flatMap(id => document!.narrations[id]!.content.flatMap(node => node.type === 'cue' ? [[node.id, id] as const] : [])))
|
|
45
|
+
}}
|
|
46
|
+
onCueCopy={(from, to) => workbench.copyNarrationCueTiming(from, to, identity)}
|
|
43
47
|
onReady={editor => {
|
|
44
48
|
registration.current?.()
|
|
45
49
|
fieldEditor.current = editor
|
|
46
|
-
registration.current = editor ? workbench.registerDocumentField({ identity, reset, blockId, stepId,
|
|
50
|
+
registration.current = editor ? workbench.registerDocumentField({ identity, reset, blockId, stepId, focus: narrationFieldFocus(editor), editor }) : undefined
|
|
47
51
|
}}
|
|
48
|
-
onFocus={() => { workbench.documentInteraction(); if (fieldEditor.current) workbench.activateDocumentHistory(fieldEditor.current); onPause() }} onChange={value => workbench.
|
|
52
|
+
onFocus={() => { workbench.documentInteraction(); if (fieldEditor.current) workbench.activateDocumentHistory(fieldEditor.current); onPause() }} onChange={value => workbench.setDocumentNarrations(stepId, value, identity)}
|
|
49
53
|
onComposition={composing => workbench.documentComposition(composing, owner, identity)}
|
|
50
54
|
onError={error => workbench.documentError(error)} />
|
|
51
55
|
}
|
|
@@ -79,9 +83,9 @@ export function DocumentStepNarration({ blockId, sectionId, stepId, expanded, ad
|
|
|
79
83
|
{!expanded && text && <p className="pm-ui-block-preview__caption" data-step-caption={stepId}>{text}</p>}
|
|
80
84
|
{expanded && <div id={panelId} className="pm-ui-block-preview__narration-fields">
|
|
81
85
|
{showLabel && <span className="pm-ui-block-preview__step-label">{fieldLabel}</span>}
|
|
82
|
-
|
|
83
|
-
identity={source.identity!} reset={source.reset} blockId={blockId} stepId={stepId}
|
|
84
|
-
content={id
|
|
86
|
+
<NarrationField key={`${source.identity}:${source.reset}:${stepId}`}
|
|
87
|
+
identity={source.identity!} reset={source.reset} blockId={blockId} stepId={stepId}
|
|
88
|
+
content={ids.map(id => ({ id, content: document.narrations[id]!.content }))} readOnly={readOnly} advanced={advanced} onPause={onPause} />
|
|
85
89
|
<StepProductionEditor key={`${source.identity}:${source.reset}:${stepId}`} document={document} stepId={stepId} locale={locale} readOnly={readOnly} advanced={advanced}
|
|
86
90
|
onPause={onPause} onInteraction={() => workbench.documentInteraction()}
|
|
87
91
|
onChange={patch => workbench.setDocumentStepProduction(stepId, patch, source.identity)}
|
|
@@ -1,26 +1,31 @@
|
|
|
1
1
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
2
2
|
import { createRoot } from 'react-dom/client'
|
|
3
|
-
import type { NarrationNode } from '@lincyaw/kernel'
|
|
4
3
|
import { blockNoteDictionary } from './blocknote-messages.ts'
|
|
5
4
|
import { BlockNoteView } from '@blocknote/mantine'
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
5
|
+
import { SideMenuExtension } from '@blocknote/core/extensions'
|
|
6
|
+
import { DragHandleMenu, RemoveBlockItem, SideMenu, SideMenuController, useBlockNoteEditor, useComponentsContext, useCreateBlockNote, useDictionary, useExtension, useExtensionState, usePortalElement } from '@blocknote/react'
|
|
7
|
+
import type { SideMenuProps } from '@blocknote/react'
|
|
8
|
+
import { PmButton, PmDialog, PmIcon, PmInput } from '../components.tsx'
|
|
8
9
|
import type { Locale } from '../messages.ts'
|
|
9
10
|
import { DOCUMENT_EDITOR_THEME } from './BlockNoteSurface.tsx'
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import type { NativeNarrationEditor } from './narration-schema.tsx'
|
|
11
|
+
import { NARRATION_CONTEXT, NARRATION_MESSAGES, createNarrationExtensions, narrationSchema } from './narration-schema.tsx'
|
|
12
|
+
import type { NativeNarrationBlock, NativeNarrationEditor, NativeNarrationInput } from './narration-schema.tsx'
|
|
13
13
|
|
|
14
|
-
export interface NarrationEditorProps {
|
|
14
|
+
export interface NarrationEditorProps<T> {
|
|
15
15
|
identity: string
|
|
16
|
-
|
|
17
|
-
content:
|
|
18
|
-
getContent?():
|
|
16
|
+
fieldId: string
|
|
17
|
+
content: T
|
|
18
|
+
getContent?(): T | undefined
|
|
19
|
+
toNative(content: T): NativeNarrationInput[]
|
|
20
|
+
fromNative(blocks: readonly NativeNarrationBlock[]): T
|
|
21
|
+
blockMenu?: boolean
|
|
22
|
+
cueOwners?(): ReadonlyMap<string, string>
|
|
23
|
+
onCueCopy?(from: string, to: string): void
|
|
19
24
|
locale: Locale
|
|
20
25
|
reset?: number
|
|
21
26
|
readOnly?: boolean
|
|
22
27
|
advanced?: boolean
|
|
23
|
-
onChange(content:
|
|
28
|
+
onChange(content: T): void
|
|
24
29
|
onComposition?(composing: boolean): void
|
|
25
30
|
onFocus?(): void
|
|
26
31
|
onError?(error: Error): void
|
|
@@ -28,7 +33,7 @@ export interface NarrationEditorProps {
|
|
|
28
33
|
}
|
|
29
34
|
|
|
30
35
|
/** A native field can live inside an opaque body node without sharing its DOM event root. */
|
|
31
|
-
export function NarrationEditor(props: NarrationEditorProps) {
|
|
36
|
+
export function NarrationEditor<T>(props: NarrationEditorProps<T>) {
|
|
32
37
|
const mount = useRef<HTMLDivElement>(null), root = useRef<ReturnType<typeof createRoot>>(), current = useRef(props)
|
|
33
38
|
current.current = props
|
|
34
39
|
useEffect(() => {
|
|
@@ -50,14 +55,40 @@ export function NarrationEditor(props: NarrationEditorProps) {
|
|
|
50
55
|
}
|
|
51
56
|
}, [])
|
|
52
57
|
useEffect(() => { root.current?.render(<NarrationSurface {...props} />) })
|
|
53
|
-
return <div ref={mount} contentEditable={false} data-narration-editor={props.
|
|
58
|
+
return <div ref={mount} contentEditable={false} data-narration-editor={props.fieldId} />
|
|
54
59
|
}
|
|
55
60
|
|
|
56
|
-
function
|
|
61
|
+
function NarrationBlockMenu() {
|
|
62
|
+
const components = useComponentsContext()!, dictionary = useDictionary(), portalElement = usePortalElement()
|
|
63
|
+
const sideMenu = useExtension(SideMenuExtension)
|
|
64
|
+
return <components.Generic.Menu.Root position="left" portalElement={portalElement} onOpenChange={open => {
|
|
65
|
+
if (open) sideMenu.freezeMenu()
|
|
66
|
+
else sideMenu.unfreezeMenu()
|
|
67
|
+
}}>
|
|
68
|
+
<components.Generic.Menu.Trigger><components.SideMenu.Button label={dictionary.side_menu.drag_handle_label} icon={<PmIcon name="more" />} /></components.Generic.Menu.Trigger>
|
|
69
|
+
<DragHandleMenu><RemoveBlockItem>{dictionary.drag_handle.delete_menuitem}</RemoveBlockItem></DragHandleMenu>
|
|
70
|
+
</components.Generic.Menu.Root>
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function InsertNarrationBlock() {
|
|
74
|
+
const editor = useBlockNoteEditor(), components = useComponentsContext()!, dictionary = useDictionary()
|
|
75
|
+
const block = useExtensionState(SideMenuExtension, { selector: state => state?.block })
|
|
76
|
+
if (!block) return null
|
|
77
|
+
return <components.SideMenu.Button label={dictionary.side_menu.add_block_label} icon={<PmIcon name="plus" />} onClick={() => {
|
|
78
|
+
const [inserted] = editor.insertBlocks([{ type: 'paragraph' }], block, 'after')
|
|
79
|
+
editor.setTextCursorPosition(inserted!, 'start'); editor.focus()
|
|
80
|
+
}} />
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function NarrationSideMenu(props: SideMenuProps) {
|
|
84
|
+
return <SideMenu {...props}><InsertNarrationBlock /><NarrationBlockMenu /></SideMenu>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function NarrationSurface<T>(props: NarrationEditorProps<T>) {
|
|
57
88
|
const current = useRef(props), pending = useRef<string[]>([])
|
|
58
89
|
current.current = props
|
|
59
90
|
const canonical = props.getContent?.() ?? props.content
|
|
60
|
-
const incoming = JSON.stringify(canonical), sourceKey = JSON.stringify([props.identity, props.
|
|
91
|
+
const incoming = JSON.stringify(canonical), sourceKey = JSON.stringify([props.identity, props.fieldId, props.reset])
|
|
61
92
|
const version = useRef({ key: sourceKey, incoming, epoch: 0 })
|
|
62
93
|
if (version.current.key !== sourceKey) {
|
|
63
94
|
version.current.epoch++; version.current.key = sourceKey; pending.current = []
|
|
@@ -70,10 +101,11 @@ function NarrationSurface(props: NarrationEditorProps) {
|
|
|
70
101
|
const instance = useMemo(() => ({ content: structuredClone(canonical), emitted: incoming }), [sourceKey, version.current.epoch])
|
|
71
102
|
const active = useRef(instance); active.current = instance
|
|
72
103
|
const t = NARRATION_MESSAGES[props.locale]
|
|
73
|
-
const editor = useCreateBlockNote({ schema: narrationSchema, initialContent:
|
|
104
|
+
const editor = useCreateBlockNote({ schema: narrationSchema, initialContent: props.toNative(instance.content),
|
|
74
105
|
trailingBlock: false,
|
|
75
106
|
dictionary: { ...blockNoteDictionary(props.locale), placeholders: { ...blockNoteDictionary(props.locale).placeholders, default: props.advanced === false ? t.emptyCaptions : t.empty } },
|
|
76
|
-
extensions:
|
|
107
|
+
extensions: createNarrationExtensions(() => current.current.cueOwners?.() ?? new Map(), (from, to) => current.current.onCueCopy?.(from, to)),
|
|
108
|
+
domAttributes: { editor: { 'aria-label': props.advanced === false ? t.captions : t.field } },
|
|
77
109
|
}, [instance, props.advanced])
|
|
78
110
|
const baseline = useMemo(() => JSON.stringify(editor.document), [editor])
|
|
79
111
|
const [open, setOpen] = useState(false), [display, setDisplay] = useState(''), [spoken, setSpoken] = useState('')
|
|
@@ -85,7 +117,7 @@ function NarrationSurface(props: NarrationEditorProps) {
|
|
|
85
117
|
if (active.current !== instance) return
|
|
86
118
|
if (current.current.getContent && current.current.getContent() === undefined) return
|
|
87
119
|
try {
|
|
88
|
-
const content = JSON.stringify(editor.document) === baseline ? structuredClone(instance.content) :
|
|
120
|
+
const content = JSON.stringify(editor.document) === baseline ? structuredClone(instance.content) : current.current.fromNative(editor.document)
|
|
89
121
|
const serialized = JSON.stringify(content)
|
|
90
122
|
if (serialized === instance.emitted) return
|
|
91
123
|
instance.emitted = serialized; pending.current.push(serialized)
|
|
@@ -93,10 +125,12 @@ function NarrationSurface(props: NarrationEditorProps) {
|
|
|
93
125
|
} catch (error) { current.current.onError?.(error instanceof Error ? error : new Error(String(error))) }
|
|
94
126
|
}
|
|
95
127
|
return <NARRATION_CONTEXT.Provider value={{ locale: props.locale, readOnly: props.readOnly, advanced: props.advanced }}>
|
|
96
|
-
<div className="pm-ui-narration-editor pm-ui-block-editor pm-ui" onFocus={() => current.current.onFocus?.()}
|
|
128
|
+
<div className="pm-ui-narration-editor pm-ui-block-editor pm-ui" data-block-menu={props.blockMenu || undefined} onFocus={() => current.current.onFocus?.()}
|
|
97
129
|
onCompositionStart={() => current.current.onComposition?.(true)}
|
|
98
130
|
onCompositionEnd={() => queueMicrotask(() => { if (active.current === instance) current.current.onComposition?.(false) })}>
|
|
99
|
-
<BlockNoteView editor={editor} editable={!props.readOnly} theme={DOCUMENT_EDITOR_THEME} formattingToolbar={false} sideMenu={false} slashMenu={false} linkToolbar={false} comments={false} onChange={change}
|
|
131
|
+
<BlockNoteView editor={editor} editable={!props.readOnly} theme={DOCUMENT_EDITOR_THEME} formattingToolbar={false} sideMenu={false} slashMenu={false} linkToolbar={false} comments={false} onChange={change}>
|
|
132
|
+
{props.blockMenu && <SideMenuController sideMenu={NarrationSideMenu} />}
|
|
133
|
+
</BlockNoteView>
|
|
100
134
|
{props.advanced !== false && <div className="pm-ui-narration-editor__actions"><PmButton small variant="ghost" disabled={props.readOnly} onClick={() => { setDisplay(editor.getSelectedText()); setSpoken(''); setOpen(true) }}>{t.pronunciation}</PmButton></div>}
|
|
101
135
|
<PmDialog open={open} label={t.pronunciation} closeLabel={t.cancel} onClose={() => setOpen(false)} actions={<>
|
|
102
136
|
<PmButton small onClick={() => setOpen(false)}>{t.cancel}</PmButton>
|
|
@@ -50,36 +50,41 @@ const atom = createReactInlineContentSpec({
|
|
|
50
50
|
},
|
|
51
51
|
})
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
export function createNarrationExtensions(owners: () => ReadonlyMap<string, string>, copied: (from: string, to: string) => void) {
|
|
54
|
+
const copyId = (id: string) => { const next = randomUUID(); copied(id, next); return next }
|
|
55
|
+
return [createExtension({
|
|
54
56
|
key: 'presmium-narration-cue-identities',
|
|
55
57
|
tiptapExtensions: [Extension.create({ name: 'presmiumNarrationCueIdentities', addProseMirrorPlugins: () => [new Plugin({
|
|
56
58
|
props: { transformPasted: slice => {
|
|
57
59
|
const copied = (fragment: Fragment): Fragment => {
|
|
58
60
|
const nodes: Node[] = []
|
|
59
61
|
fragment.forEach(node => nodes.push(node.type.name === 'narrationAtom' && node.attrs.kind === 'cue'
|
|
60
|
-
? node.type.create({ ...node.attrs, cueId:
|
|
62
|
+
? node.type.create({ ...node.attrs, cueId: copyId(node.attrs.cueId as string) }, node.content, node.marks)
|
|
61
63
|
: node.isLeaf ? node : node.copy(copied(node.content))))
|
|
62
64
|
return Fragment.fromArray(nodes)
|
|
63
65
|
}
|
|
64
66
|
return new Slice(copied(slice.content), slice.openStart, slice.openEnd)
|
|
65
67
|
} },
|
|
66
68
|
appendTransaction: (_transactions, _old, state) => {
|
|
67
|
-
const seen = new Set<string>(), transaction = state.tr
|
|
69
|
+
const seen = new Set<string>(), transaction = state.tr, external = owners()
|
|
68
70
|
state.doc.descendants((node, position) => {
|
|
69
71
|
if (node.type.name !== 'narrationAtom' || node.attrs.kind !== 'cue') return
|
|
70
72
|
const id = node.attrs.cueId as string
|
|
71
|
-
|
|
73
|
+
const location = state.doc.resolve(position)
|
|
74
|
+
let blockId: string | undefined
|
|
75
|
+
for (let depth = location.depth; depth > 0; depth--) if (location.node(depth).type.name === 'blockContainer') { blockId = location.node(depth).attrs.id as string; break }
|
|
76
|
+
if (!id || seen.has(id) || external.has(id) && external.get(id) !== blockId) transaction.setNodeMarkup(position, undefined, { ...node.attrs, cueId: copyId(id) })
|
|
72
77
|
else seen.add(id)
|
|
73
78
|
})
|
|
74
79
|
return transaction.docChanged ? transaction : null
|
|
75
80
|
},
|
|
76
81
|
})] })],
|
|
77
|
-
})
|
|
82
|
+
})]
|
|
83
|
+
}
|
|
78
84
|
export const narrationSchema = BlockNoteSchema.create({
|
|
79
85
|
blockSpecs: { paragraph: defaultBlockSpecs.paragraph },
|
|
80
86
|
inlineContentSpecs: { ...defaultInlineContentSpecs, narrationAtom: atom }, styleSpecs: {},
|
|
81
87
|
})
|
|
82
|
-
export const narrationExtensions = [cueIdentities]
|
|
83
88
|
export type NativeNarrationEditor = BlockNoteEditor<typeof narrationSchema.blockSchema, typeof narrationSchema.inlineContentSchema, typeof narrationSchema.styleSchema>
|
|
84
89
|
export type NativeNarrationBlock = Block<typeof narrationSchema.blockSchema, typeof narrationSchema.inlineContentSchema, typeof narrationSchema.styleSchema>
|
|
85
90
|
export type NativeNarrationInput = PartialBlock<typeof narrationSchema.blockSchema, typeof narrationSchema.inlineContentSchema, typeof narrationSchema.styleSchema>
|
|
@@ -1,18 +1,34 @@
|
|
|
1
|
-
import type { NarrationNode } from '@lincyaw/kernel'
|
|
1
|
+
import type { Narration, NarrationNode } from '@lincyaw/kernel'
|
|
2
2
|
import type { NativeNarrationBlock, NativeNarrationInput, NativeNarrationInline } from './narration-schema.tsx'
|
|
3
3
|
import type { NativeNarrationEditor } from './narration-schema.tsx'
|
|
4
4
|
import { nativeFieldFocus } from './focus.ts'
|
|
5
5
|
|
|
6
|
-
export const narrationFieldFocus = (editor: NativeNarrationEditor) => nativeFieldFocus(editor, () => JSON.stringify(
|
|
6
|
+
export const narrationFieldFocus = (editor: NativeNarrationEditor) => nativeFieldFocus(editor, () => JSON.stringify(nativeToNarrations(editor.document)))
|
|
7
7
|
|
|
8
8
|
export function narrationToNative(id: string, content: readonly NarrationNode[]): NativeNarrationInput[] {
|
|
9
|
-
return [{ id
|
|
9
|
+
return [{ id, type: 'paragraph', content: content.map((node): NativeNarrationInline => {
|
|
10
10
|
if (node.type === 'text') return { type: 'text', text: node.text, styles: {} }
|
|
11
11
|
return { type: 'narrationAtom', content: undefined, props: node.type === 'pronunciation'
|
|
12
12
|
? { kind: 'pronunciation', display: node.display, spoken: node.spoken, cueId: '', payload: '' }
|
|
13
13
|
: { kind: 'cue', cueId: node.id, payload: JSON.stringify(node), display: '', spoken: '' } }
|
|
14
14
|
}) }]
|
|
15
15
|
}
|
|
16
|
+
|
|
17
|
+
export function narrationsToNative(narrations: readonly Narration[], emptyId: string): NativeNarrationInput[] {
|
|
18
|
+
return narrations.length ? narrations.flatMap(narration => narrationToNative(narration.id, narration.content)) : narrationToNative(emptyId, [])
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function nativeToNarrations(blocks: readonly NativeNarrationBlock[]): Narration[] {
|
|
22
|
+
const narrations: Narration[] = []
|
|
23
|
+
const visit = (blocks: readonly NativeNarrationBlock[]) => {
|
|
24
|
+
for (const block of blocks) {
|
|
25
|
+
narrations.push({ id: block.id, content: nativeToNarration([{ ...block, children: [] }]) })
|
|
26
|
+
visit(block.children)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
visit(blocks)
|
|
30
|
+
return narrations.length === 1 && !narrations[0]!.content.length ? [] : narrations
|
|
31
|
+
}
|
|
16
32
|
export function nativeToNarration(blocks: readonly NativeNarrationBlock[]): NarrationNode[] {
|
|
17
33
|
const content: NarrationNode[] = []
|
|
18
34
|
const text = (value: string) => {
|
|
@@ -4,7 +4,7 @@ import type { CanonicalSource, NarrationNode, TeachingDocument, TranscriptionRev
|
|
|
4
4
|
import { PmBadge, PmButton, PmDialog, PmField, PmNotice, PmSelect, PmTextarea } from '../components.tsx'
|
|
5
5
|
import { useWorkbench, useWorkbenchState } from '../context.tsx'
|
|
6
6
|
import { NarrationEditor } from '../document-editor/NarrationEditor.tsx'
|
|
7
|
-
import { narrationToNative } from '../document-editor/narration.ts'
|
|
7
|
+
import { narrationToNative, nativeToNarration } from '../document-editor/narration.ts'
|
|
8
8
|
import type { NativeNarrationEditor } from '../document-editor/narration-schema.tsx'
|
|
9
9
|
import { TRANSCRIPTION_MESSAGES } from '../transcription-messages.ts'
|
|
10
10
|
|
|
@@ -114,7 +114,8 @@ function AdoptionTarget({ identity, candidate, document, sectionId, stepId, disa
|
|
|
114
114
|
}
|
|
115
115
|
if (!displayed(nodes).trim() && !atoms.length) return <span>{t.empty}</span>
|
|
116
116
|
return <>
|
|
117
|
-
<PmField label={t.text} group><NarrationEditor identity={key}
|
|
117
|
+
<PmField label={t.text} group><NarrationEditor identity={key} fieldId={original?.outputNarrationId ?? draft.id} content={nodes} getContent={() => latest.current.nodes ?? nodes} locale={locale} advanced={false} readOnly={disabled || applying}
|
|
118
|
+
toNative={value => narrationToNative(original?.outputNarrationId ?? draft.id, value)} fromNative={nativeToNarration}
|
|
118
119
|
onReady={value => { editor.current = value }} onComposition={setComposing} onFocus={pause} onChange={value => {
|
|
119
120
|
const used = new Set<string>()
|
|
120
121
|
for (const node of value) { const atom = atoms.find(atom => !used.has(atom.key) && same(atom.node, node)); if (atom) used.add(atom.key) }
|
package/src/workbench.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { applyDocumentCaptionTiming, changeDocumentCaptionScale, randomUUID } from '@lincyaw/kernel'
|
|
1
|
+
import { applyDocumentCaptionTiming, changeDocumentCaptionScale, randomUUID, replaceStepNarrations, validateDocument } from '@lincyaw/kernel'
|
|
2
2
|
import type { DocumentCaptionTimingAction } from '@lincyaw/kernel'
|
|
3
3
|
import { reconcileCaptionTiming } from './caption-timing.ts'
|
|
4
4
|
import type { ProjectOperation, ProjectPlan } from '@lincyaw/kernel'
|
|
@@ -24,7 +24,7 @@ import type { DocumentSessionState } from './document-session.ts'
|
|
|
24
24
|
import type { DocumentDraftReference } from './document-drafts.ts'
|
|
25
25
|
import { DocumentReturnPoints, documentReturnStep, documentViewportOffset, restoreDocumentViewport } from './document-return-point.ts'
|
|
26
26
|
import type { DocumentReturnPoint } from './document-return-point.ts'
|
|
27
|
-
import type { DocumentRecordingAdoption, VerifiedDocumentRecordingAdoption, DocumentAudioImportAnswer, DocumentAudioInspectionAnswer, DocumentChangeAnswer, MediaClip,
|
|
27
|
+
import type { DocumentRecordingAdoption, VerifiedDocumentRecordingAdoption, DocumentAudioImportAnswer, DocumentAudioInspectionAnswer, DocumentChangeAnswer, MediaClip, Narration, TeachingDocument, DocumentTranscriptionRequest, TranscriptionTask, TranscriptionAdoptionDraft, TranscriptionAdoptionApply, TranscriptionAdoptionPlan, TranscriptionAdoptionAnswer } from '@lincyaw/kernel'
|
|
28
28
|
import type { NativeDocumentEditor } from './document-editor/schema.tsx'
|
|
29
29
|
import type { DocumentEditorCommands } from './document-editor/DocumentEditor.tsx'
|
|
30
30
|
import type { DocumentInsertionPlace } from './document-editor/insertion-place.ts'
|
|
@@ -279,6 +279,7 @@ export class Workbench {
|
|
|
279
279
|
readonly #documentTimingDrafts = new DocumentTimingDrafts()
|
|
280
280
|
readonly #captionTimingDrafts = new CaptionTimingDrafts()
|
|
281
281
|
readonly #removedCueTimings = new Map<string, { pauseMs: number }>()
|
|
282
|
+
readonly #removedNarrations = new Map<string, { narration: Narration, override?: { narrationStart: 'with-animation' | 'after-animation' } }>()
|
|
282
283
|
#cueTimingScope?: string
|
|
283
284
|
#documentEditor?: NativeDocumentEditor
|
|
284
285
|
#documentEditorCommands?: DocumentEditorCommands
|
|
@@ -378,7 +379,7 @@ export class Workbench {
|
|
|
378
379
|
|| pendingField.field.blockId !== undefined && !authoring.content?.blocks[pendingField.field.blockId]
|
|
379
380
|
|| !authoring.content?.steps[pendingField.field.stepId])) this.#pendingDocumentField = undefined
|
|
380
381
|
const cueTimingScope = JSON.stringify([authoring.identity, authoring.reset])
|
|
381
|
-
if (this.#cueTimingScope !== cueTimingScope) { this.#removedCueTimings.clear(); this.#cueTimingScope = cueTimingScope }
|
|
382
|
+
if (this.#cueTimingScope !== cueTimingScope) { this.#removedCueTimings.clear(); this.#removedNarrations.clear(); this.#cueTimingScope = cueTimingScope }
|
|
382
383
|
const previous = this.#state.authoring
|
|
383
384
|
// A saved timestamp belongs to this client's acknowledged source version.
|
|
384
385
|
// Remote adoption has no server modification time to display in its place.
|
|
@@ -1617,59 +1618,48 @@ export class Workbench {
|
|
|
1617
1618
|
this.#captionTimingDrafts.set(scope, stepId, draft)
|
|
1618
1619
|
this.#set({ captionTimingDraftSteps: this.#captionTimingDrafts.steps(scope, document), captionTimingDraftStorageFailure: this.#captionTimingDrafts.failed(scope) })
|
|
1619
1620
|
}
|
|
1620
|
-
|
|
1621
|
-
setDocumentNarration(stepId: string, narrationId: string | undefined, content: NarrationNode[], identity?: string): string | undefined {
|
|
1621
|
+
copyNarrationCueTiming(from: string, to: string, identity: string): void {
|
|
1622
1622
|
const document = this.getDocument(identity)
|
|
1623
|
-
if (!document || isAgentChanging(this.#state)
|
|
1623
|
+
if (!document || isAgentChanging(this.#state)) return
|
|
1624
|
+
const timing = document.production.cueTimings?.[from] ?? this.#removedCueTimings.get(from)
|
|
1625
|
+
if (timing) this.#removedCueTimings.set(to, { ...timing })
|
|
1626
|
+
}
|
|
1627
|
+
/** Native block identity and order own the current step's complete script. */
|
|
1628
|
+
setDocumentNarrations(stepId: string, narrations: Narration[], identity?: string): void {
|
|
1629
|
+
const document = this.getDocument(identity)
|
|
1630
|
+
if (!document || isAgentChanging(this.#state) || !Object.hasOwn(document.steps, stepId)) return
|
|
1624
1631
|
const step = document.steps[stepId]!
|
|
1625
|
-
if (!document.sections[step.sectionId]?.stepOrder.includes(stepId)) return
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
const target = narrationId ?? step.narrationIds[0]
|
|
1636
|
-
const existing = target !== undefined && Object.hasOwn(document.narrations, target) ? document.narrations[target] : undefined
|
|
1637
|
-
if (existing && !step.narrationIds.includes(existing.id)) throw new Error('document-narration-target')
|
|
1638
|
-
if (!existing && !meaningful) return undefined
|
|
1639
|
-
const id = target ?? randomUUID()
|
|
1640
|
-
if (!id || id.length > 200 || /[\u0000-\u001f]/.test(id) || ['__proto__', 'constructor', 'prototype'].includes(id)) throw new Error('document-narration-target')
|
|
1641
|
-
if (!existing && (document.id === id || [document.sections, document.blocks, document.steps, document.assets, document.definitions].some(map => Object.hasOwn(map, id)))) throw new Error('document-narration-target')
|
|
1642
|
-
if (existing && JSON.stringify(existing.content) === JSON.stringify(content)) return id
|
|
1643
|
-
const next: TeachingDocument = { ...document,
|
|
1644
|
-
narrations: { ...document.narrations, [id]: { ...existing, id, content: structuredClone(content) } },
|
|
1645
|
-
...step.narrationIds.includes(id) ? {} : { steps: { ...document.steps, [stepId]: { ...step, narrationIds: [...step.narrationIds, id] } } },
|
|
1646
|
-
}
|
|
1647
|
-
const cueIds = new Set(content.flatMap(node => node.type === 'cue' ? [node.id] : []))
|
|
1648
|
-
const previousCues = new Set(existing?.content.flatMap(node => node.type === 'cue' ? [node.id] : []) ?? [])
|
|
1649
|
-
if (document.production.cueTimings) {
|
|
1650
|
-
const removed = [...previousCues].filter(cue => !cueIds.has(cue) && Object.hasOwn(document.production.cueTimings!, cue)
|
|
1651
|
-
&& !Object.values(next.narrations).some(narration => narration.content.some(node => node.type === 'cue' && node.id === cue)))
|
|
1652
|
-
if (removed.length) {
|
|
1653
|
-
const cueTimings = { ...document.production.cueTimings }
|
|
1654
|
-
for (const cue of removed) {
|
|
1655
|
-
this.#removedCueTimings.delete(cue)
|
|
1656
|
-
this.#removedCueTimings.set(cue, { ...cueTimings[cue]! })
|
|
1657
|
-
delete cueTimings[cue]
|
|
1658
|
-
}
|
|
1659
|
-
while (this.#removedCueTimings.size > 512) this.#removedCueTimings.delete(this.#removedCueTimings.keys().next().value!)
|
|
1660
|
-
next.production = { ...document.production, cueTimings }
|
|
1661
|
-
}
|
|
1632
|
+
if (!document.sections[step.sectionId]?.stepOrder.includes(stepId)) return
|
|
1633
|
+
const values = narrations.map(narration => {
|
|
1634
|
+
const previous = document.narrations[narration.id] ?? this.#removedNarrations.get(narration.id)?.narration
|
|
1635
|
+
return { ...previous, ...narration }
|
|
1636
|
+
})
|
|
1637
|
+
if (JSON.stringify(step.narrationIds.map(id => document.narrations[id])) === JSON.stringify(values)) return
|
|
1638
|
+
const next = replaceStepNarrations(document, stepId, values)
|
|
1639
|
+
for (const narration of values) {
|
|
1640
|
+
const override = this.#removedNarrations.get(narration.id)?.override
|
|
1641
|
+
if (override && !document.narrations[narration.id]) next.production.narrationOverrides = { ...next.production.narrationOverrides, [narration.id]: { ...override } }
|
|
1662
1642
|
}
|
|
1643
|
+
const cueIds = new Set(values.flatMap(narration => narration.content.flatMap(node => node.type === 'cue' ? [node.id] : [])))
|
|
1663
1644
|
for (const cue of cueIds) {
|
|
1664
1645
|
const timing = this.#removedCueTimings.get(cue)
|
|
1665
|
-
if (
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1646
|
+
if (timing && !Object.hasOwn(next.production.cueTimings ?? {}, cue)) next.production.cueTimings = { ...next.production.cueTimings, [cue]: { ...timing } }
|
|
1647
|
+
}
|
|
1648
|
+
const references = (values: Narration[]) => JSON.stringify(values.map(narration => [narration.id, narration.provenance, narration.content.filter(node => node.type === 'cue')]))
|
|
1649
|
+
if (references(step.narrationIds.map(id => document.narrations[id]!)) !== references(values)
|
|
1650
|
+
&& validateDocument(next).issues.some(issue => issue.severity === 'error')) throw new Error('document-narration-content')
|
|
1651
|
+
for (const id of step.narrationIds) if (!next.steps[stepId]!.narrationIds.includes(id)) {
|
|
1652
|
+
this.#removedNarrations.delete(id)
|
|
1653
|
+
this.#removedNarrations.set(id, { narration: document.narrations[id]!, override: document.production.narrationOverrides?.[id] })
|
|
1654
|
+
}
|
|
1655
|
+
for (const [id, timing] of Object.entries(document.production.cueTimings ?? {})) if (!Object.hasOwn(next.production.cueTimings ?? {}, id)) {
|
|
1656
|
+
this.#removedCueTimings.delete(id)
|
|
1657
|
+
this.#removedCueTimings.set(id, { ...timing })
|
|
1669
1658
|
}
|
|
1659
|
+
while (this.#removedNarrations.size > 512) this.#removedNarrations.delete(this.#removedNarrations.keys().next().value!)
|
|
1660
|
+
while (this.#removedCueTimings.size > 512) this.#removedCueTimings.delete(this.#removedCueTimings.keys().next().value!)
|
|
1670
1661
|
this.stopAuditions()
|
|
1671
1662
|
this.#documentSession.record(next)
|
|
1672
|
-
return id
|
|
1673
1663
|
}
|
|
1674
1664
|
recordDocument(content: TeachingDocument): void { if (!isAgentChanging(this.#state)) this.#documentSession.record(content) }
|
|
1675
1665
|
documentComposition(composing: boolean, owner = 'body', identity?: string): void { this.#documentSession.composition(composing, owner, identity) }
|
|
@@ -1686,31 +1676,30 @@ export class Workbench {
|
|
|
1686
1676
|
this.#documentSelectionGeneration++; this.#cancelOpeningDocument(); this.#stopDocumentReturnScroll?.()
|
|
1687
1677
|
if (this.#state.documentPositionFailure) this.#set({ documentPositionFailure: undefined })
|
|
1688
1678
|
}
|
|
1689
|
-
pendingDocumentField(blockId: string | undefined, identity?: string): Pick<DocumentFieldRegistration, 'stepId'
|
|
1679
|
+
pendingDocumentField(blockId: string | undefined, identity?: string): Pick<DocumentFieldRegistration, 'stepId'> | undefined {
|
|
1690
1680
|
const pending = this.#pendingDocumentField, source = this.#state.authoring
|
|
1691
1681
|
return pending && pending.generation === this.#documentSelectionGeneration && pending.field.blockId === blockId
|
|
1692
1682
|
&& pending.field.identity === (identity ?? source?.identity) && pending.reset === source?.reset ? pending.field : undefined
|
|
1693
1683
|
}
|
|
1694
1684
|
/** A live native field owns its target only while it still has focus in this source. */
|
|
1695
|
-
focusedDocumentField(): Readonly<Pick<DocumentFieldRegistration, 'identity' | 'reset' | 'blockId' | 'stepId'
|
|
1685
|
+
focusedDocumentField(): Readonly<Pick<DocumentFieldRegistration, 'identity' | 'reset' | 'blockId' | 'stepId'>> & { readonly editor: DocumentHistoryEditor } | undefined {
|
|
1696
1686
|
const source = this.#state.authoring, editor = this.#activeDocumentHistory
|
|
1697
1687
|
if (!source?.identity || !source.content || !editor || editor === this.#documentEditor || editor.prosemirrorView.isDestroyed || !editor.prosemirrorView.hasFocus()) return
|
|
1698
1688
|
for (const field of this.#documentFields.values()) {
|
|
1699
1689
|
if (field.editor !== editor || field.identity !== source.identity || field.reset !== source.reset) continue
|
|
1700
1690
|
const step = source.content.steps[field.stepId], section = step && source.content.sections[step.sectionId]
|
|
1701
|
-
if (!step || !section?.stepOrder.includes(field.stepId)
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
return { identity, reset, blockId, stepId, narrationId, slot, editor }
|
|
1691
|
+
if (!step || !section?.stepOrder.includes(field.stepId)) return
|
|
1692
|
+
const { identity, reset, blockId, stepId } = field
|
|
1693
|
+
return { identity, reset, blockId, stepId, editor }
|
|
1705
1694
|
}
|
|
1706
1695
|
return undefined
|
|
1707
1696
|
}
|
|
1708
1697
|
registerDocumentField(field: DocumentFieldRegistration): () => void {
|
|
1709
1698
|
if (field.identity !== this.#state.authoring?.identity || field.reset !== this.#state.authoring.reset) return () => {}
|
|
1710
|
-
const key = JSON.stringify([field.identity, field.blockId, field.stepId
|
|
1699
|
+
const key = JSON.stringify([field.identity, field.blockId, field.stepId])
|
|
1711
1700
|
this.#documentFields.set(key, field)
|
|
1712
1701
|
const pending = this.#pendingDocumentField
|
|
1713
|
-
if (pending && pending.reset === field.reset && JSON.stringify([pending.field.identity, pending.field.blockId, pending.field.stepId
|
|
1702
|
+
if (pending && pending.reset === field.reset && JSON.stringify([pending.field.identity, pending.field.blockId, pending.field.stepId]) === key) {
|
|
1714
1703
|
this.#pendingDocumentField = undefined
|
|
1715
1704
|
if (pending.generation === this.#documentSelectionGeneration && field.identity === this.#state.authoring?.identity && field.reset === this.#state.authoring.reset) field.focus.restore(pending.bookmark)
|
|
1716
1705
|
}
|
|
@@ -2340,7 +2329,7 @@ export class Workbench {
|
|
|
2340
2329
|
this.#voiceover.stop()
|
|
2341
2330
|
this.#production.reset()
|
|
2342
2331
|
this.#set({ isPreparingPresentation: false, presentationFailure: undefined, presentationSnapshot: undefined, productionPlan: undefined, productionFailure: undefined })
|
|
2343
|
-
this.#removedCueTimings.clear(); this.#cueTimingScope = undefined
|
|
2332
|
+
this.#removedCueTimings.clear(); this.#removedNarrations.clear(); this.#cueTimingScope = undefined
|
|
2344
2333
|
this.#documentSession.close()
|
|
2345
2334
|
this.#unsubscribeDocumentHistory?.(); this.#unsubscribeDocumentHistory = undefined; this.#activeDocumentHistory = undefined; this.#documentEditor = undefined
|
|
2346
2335
|
this.#unsubscribeDocumentSelection?.(); this.#unsubscribeDocumentSelection = undefined
|