@lincyaw/workbench 0.2.0-rc.5 → 0.2.0-rc.50

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 (79) hide show
  1. package/package.json +5 -4
  2. package/src/MediaClipEditor.tsx +1 -1
  3. package/src/audio-recording.ts +27 -5
  4. package/src/authoring-client.ts +10 -10
  5. package/src/authoring-draft-fragment.ts +3 -1
  6. package/src/body-draft-actions.ts +60 -0
  7. package/src/component-property-drafts.ts +5 -5
  8. package/src/components.tsx +10 -28
  9. package/src/context.tsx +2 -1
  10. package/src/diagnostics.ts +1 -1
  11. package/src/document-draft-store.ts +45 -18
  12. package/src/document-drafts.ts +28 -19
  13. package/src/document-editing.ts +484 -0
  14. package/src/document-editor/BlockNoteSurface.tsx +25 -9
  15. package/src/document-editor/ComponentMaterials.tsx +3 -9
  16. package/src/document-editor/ComponentPropertiesDialog.tsx +4 -4
  17. package/src/document-editor/DocumentBlockPreview.tsx +4 -2
  18. package/src/document-editor/DocumentComments.tsx +58 -9
  19. package/src/document-editor/DocumentEditor.tsx +44 -7
  20. package/src/document-editor/DocumentStepNarration.tsx +30 -25
  21. package/src/document-editor/NarrationEditor.tsx +54 -20
  22. package/src/document-editor/NewCommentDrafts.tsx +4 -3
  23. package/src/document-editor/PlaceComment.tsx +150 -0
  24. package/src/document-editor/comment-drafts.ts +16 -14
  25. package/src/document-editor/comments.ts +30 -10
  26. package/src/document-editor/component-label.ts +11 -0
  27. package/src/document-editor/file-input.ts +2 -2
  28. package/src/document-editor/focus.ts +0 -2
  29. package/src/document-editor/insertion-place.ts +60 -0
  30. package/src/document-editor/messages.ts +3 -3
  31. package/src/document-editor/narration-schema.tsx +12 -6
  32. package/src/document-editor/narration.ts +19 -3
  33. package/src/document-editor/page-body.ts +60 -0
  34. package/src/document-editor/projection.ts +2 -2
  35. package/src/document-editor/rich-text.ts +3 -2
  36. package/src/document-editor/selection.ts +13 -0
  37. package/src/document-editor/style.css +12 -1
  38. package/src/document-editor/table-identities.ts +2 -1
  39. package/src/document-production.ts +331 -0
  40. package/src/document-session.ts +190 -51
  41. package/src/history.ts +3 -3
  42. package/src/index.ts +5 -5
  43. package/src/jobs.ts +1 -1
  44. package/src/messages.ts +19 -4
  45. package/src/panels/DocumentView.tsx +66 -26
  46. package/src/panels/DocumentVoiceDialog.tsx +2 -2
  47. package/src/panels/DocumentVoiceField.tsx +1 -1
  48. package/src/panels/ExportPanel.tsx +1 -6
  49. package/src/panels/HistoryPanel.tsx +1 -1
  50. package/src/panels/PageDeleteDialog.tsx +4 -4
  51. package/src/panels/PageRenameDialog.tsx +1 -1
  52. package/src/panels/PreviewPanel.tsx +1 -4
  53. package/src/panels/ProductionCaptionSize.tsx +20 -0
  54. package/src/panels/ProductionCaptionTiming.tsx +7 -7
  55. package/src/panels/ProductionMediaClips.tsx +6 -5
  56. package/src/panels/ProductionPanel.tsx +8 -4
  57. package/src/panels/ProductionRecording.tsx +86 -9
  58. package/src/panels/ProductionRecordingRange.tsx +4 -4
  59. package/src/panels/ProductionTimeline.tsx +6 -1
  60. package/src/panels/ProductionTranscription.tsx +5 -4
  61. package/src/panels/SourceDocumentPanel.tsx +12 -15
  62. package/src/panels/TranscriptionAdoption.tsx +9 -8
  63. package/src/panels/VoiceCloneDialog.tsx +2 -1
  64. package/src/panels/VoiceoverDialog.tsx +1 -1
  65. package/src/places.ts +1 -36
  66. package/src/preview-playback.ts +1 -1
  67. package/src/production-messages.ts +6 -0
  68. package/src/production-playback.ts +1 -1
  69. package/src/production-voice-playback.ts +3 -2
  70. package/src/sha256.ts +9 -0
  71. package/src/styles.css +16 -41
  72. package/src/use-recording-adoption.ts +2 -2
  73. package/src/voiceover-jobs.ts +1 -1
  74. package/src/workbench-state.ts +157 -0
  75. package/src/workbench.ts +177 -943
  76. package/src/workspace-client.ts +6 -5
  77. package/src/assistant-mascot.ts +0 -62
  78. package/src/mascot-preference.ts +0 -37
  79. package/src/panels/ChangeTrail.tsx +0 -79
@@ -13,6 +13,7 @@ import type { DocumentRecordingAdoption, VerifiedDocumentRecordingAdoption, Docu
13
13
  import type { DocumentImageImport, DocumentImageImportAnswer } from '@lincyaw/kernel'
14
14
  import type { ProductionAssetAnswer, ProductionAssetQuery, ProductionMediaQuery, ProductionPlan, ProductionPlanQuery } from '@lincyaw/kernel'
15
15
  import type { DocumentTranscriptionRequest, TranscriptionCapabilities, TranscriptionTask, TranscriptionListAnswer, TranscriptionRevision, TranscriptionCancellation, TranscriptionAdoptionDraft, TranscriptionAdoptionPlan, TranscriptionAdoptionApply, TranscriptionAdoptionAnswer } from '@lincyaw/kernel'
16
+ import { randomUUID } from '@lincyaw/kernel'
16
17
 
17
18
  /** A change of the host's pending approvals that this author may answer. */
18
19
  export type ApprovalEvent =
@@ -80,14 +81,14 @@ export interface WorkspaceClient {
80
81
  /** Saves a small set of files in one document with one version and rollback boundary. */
81
82
  saveSourceTransaction(document: string, changes: SourceTransactionChange[], message?: string): Promise<SourceTransactionAnswer>
82
83
  readIr(document: string): Promise<IrAnswer>
83
- readDocument(document: string): Promise<DocumentAnswer>
84
+ readDocument(document: string, signal?: AbortSignal): Promise<DocumentAnswer>
84
85
  readAuthoringCapabilities(document: string, signal?: AbortSignal): Promise<AuthoringCapabilities>
85
86
  planDocumentAction(document: string, request: DocumentAuthoringActionRequest, signal?: AbortSignal): Promise<DocumentAuthoringActionPlan>
86
87
  readDocumentActionPlan(document: string, reference: DocumentAuthoringActionRead, signal?: AbortSignal): Promise<DocumentAuthoringActionInspection>
87
88
  applyDocumentAction(document: string, reference: DocumentAuthoringActionReference, signal?: AbortSignal): Promise<AppliedDocumentAuthoringAction>
88
89
  /** A same-origin display URL; the canonical asset location remains untouched. */
89
90
  documentAssetURL(document: string, assetId: string): string
90
- changeDocument(document: string, change: DocumentChange): Promise<DocumentChangeAnswer>
91
+ changeDocument(document: string, change: DocumentChange, signal?: AbortSignal): Promise<DocumentChangeAnswer>
91
92
  importDocumentImage(document: string, file: File, change: DocumentImageImport, signal?: AbortSignal): Promise<DocumentImageImportAnswer>
92
93
  readDocumentAudio(document: string, assetId: string, baseDocumentVersion: string, signal?: AbortSignal): Promise<DocumentAudioInspectionAnswer>
93
94
  importDocumentAudio(document: string, file: File, change: DocumentAudioImport, signal?: AbortSignal): Promise<DocumentAudioImportAnswer>
@@ -341,7 +342,7 @@ export function workspaceClient(base = 'api/presmium/', subscription?: Workspace
341
342
  if (operation.op === 'delete' && (!planned || JSON.stringify(planned.operation) !== JSON.stringify(operation))) throw new Error('project-deletion-plan-required')
342
343
  const key = JSON.stringify(operation)
343
344
  let intent = projectIntents.get(key)
344
- if (!intent) { intent = { id: crypto.randomUUID() }; projectIntents.set(key, intent) }
345
+ if (!intent) { intent = { id: randomUUID() }; projectIntents.set(key, intent) }
345
346
  while (projectIntents.size > 64) projectIntents.delete(projectIntents.keys().next().value!)
346
347
  try {
347
348
  intent.plan ??= planned
@@ -358,13 +359,13 @@ export function workspaceClient(base = 'api/presmium/', subscription?: Workspace
358
359
  saveSource: (document, path, content, base, message) => post(url('source', document), { path, content, base, ...message ? { message } : {} }),
359
360
  saveSourceTransaction: (document, changes, message) => post(url('source/transaction', document), { changes, ...message ? { message } : {} }),
360
361
  readIr,
361
- readDocument: document => call(url('document', document)),
362
+ readDocument: (document, signal) => call(url('document', document), { signal }),
362
363
  readAuthoringCapabilities: (document, signal) => call(url('document/capabilities', document), { signal }),
363
364
  planDocumentAction: (document, request, signal) => post(url('document/action/plan', document), request, signal),
364
365
  readDocumentActionPlan: (document, reference, signal) => call(url('document/action/plan', document, { planId: reference.planId, digest: reference.digest }), { signal }),
365
366
  applyDocumentAction: (document, reference, signal) => post(url('document/action/apply', document), reference, signal),
366
367
  documentAssetURL: (document, asset) => url('document/asset', document, { asset }),
367
- changeDocument: (document, change) => post(url('document/change', document), change),
368
+ changeDocument: (document, change, signal) => post(url('document/change', document), change, signal),
368
369
  importDocumentImage: (document, file, change, signal) => {
369
370
  const body = new FormData()
370
371
  body.set('change', JSON.stringify(change)); body.set('file', file)
@@ -1,62 +0,0 @@
1
- // What the kitten beside the assistant does, from what the assistant is doing: asking the author to approve
2
- // something, reading the document it was asked about, writing into it, thinking, just finished, or idle (sitting,
3
- // then lying down, then asleep the longer nothing happens).
4
- import { useEffect, useState } from 'react'
5
- import type { MascotAction } from '@lincyaw/design/mascot'
6
- import { useWorkbench, useWorkbenchState } from './context.tsx'
7
-
8
- const DONE_MS = 2400
9
- const LOAF_MS = 90_000
10
- const SLEEP_MS = 300_000
11
-
12
- export interface AssistantActivity {
13
- /** The assistant's turn is running. */
14
- running: boolean
15
- /** It holds a review of the open document: still working in it, and how many changes it has made. */
16
- review?: { running: boolean, changes: number }
17
- /** Operations wait for the author's approval. */
18
- approvals: number
19
- /** How long ago the assistant last stopped, in milliseconds (Infinity if it has not run). */
20
- sinceStopped: number
21
- }
22
-
23
- export function assistantMascotAction(a: AssistantActivity): MascotAction {
24
- if (a.approvals > 0) return 'ask'
25
- if (a.review?.running) return a.review.changes > 0 ? 'write' : 'read'
26
- if (a.running) return 'think'
27
- if (a.review && a.review.changes > 0) return 'ask'
28
- if (a.sinceStopped < DONE_MS) return 'done'
29
- if (a.sinceStopped > SLEEP_MS) return 'sleep'
30
- if (a.sinceStopped > LOAF_MS) return 'loaf'
31
- return 'sit'
32
- }
33
-
34
- /** The kitten's action for the assistant, given whether its turn is running; follows the workbench's review and the
35
- * operations waiting for approval, and moves on by itself as time passes. */
36
- export function useAssistantMascot(running: boolean): MascotAction {
37
- const { client } = useWorkbench()
38
- // two plain values rather than one object, so the selection compares equal while nothing changes
39
- const reviewRunning = useWorkbenchState(state => state.review?.running)
40
- const changes = useWorkbenchState(state => state.review?.changes.length)
41
- const review = reviewRunning === undefined ? undefined : { running: reviewRunning, changes: changes ?? 0 }
42
- const [approvals, setApprovals] = useState<string[]>([])
43
- const [stoppedAt, setStoppedAt] = useState<number>(() => performance.now() - LOAF_MS / 2)
44
- const [, setNow] = useState(0)
45
- const busy = running || Boolean(review?.running)
46
- useEffect(() => client.subscribeApprovals(event => {
47
- if (event.type === 'reset') setApprovals([])
48
- else if (event.type === 'settled') setApprovals(list => list.filter(id => id !== event.id))
49
- else setApprovals(list => list.includes(event.id) ? list : [...list, event.id])
50
- }), [client])
51
- useEffect(() => { if (!busy) setStoppedAt(performance.now()) }, [busy])
52
- // Wake up at the next moment the idle kitten changes what it does.
53
- const since = performance.now() - stoppedAt
54
- useEffect(() => {
55
- if (busy) return
56
- const next = [DONE_MS, LOAF_MS, SLEEP_MS].find(ms => ms > since)
57
- if (next === undefined) return
58
- const timer = setTimeout(() => setNow(performance.now()), next - since + 50)
59
- return () => clearTimeout(timer)
60
- }, [busy, since])
61
- return assistantMascotAction({ running, review, approvals: approvals.length, sinceStopped: busy ? 0 : since })
62
- }
@@ -1,37 +0,0 @@
1
- // Whether the author wants the kitten: one choice for every place it shows, kept in this browser and followed by
2
- // every open tab. It is on until the author turns it off.
3
- import { useSyncExternalStore } from 'react'
4
-
5
- const KEY = 'presmium.mascot'
6
- const listeners = new Set<() => void>()
7
-
8
- function read(): boolean {
9
- try { return localStorage.getItem(KEY) !== 'off' } catch { return true }
10
- }
11
-
12
- let enabled = typeof localStorage === 'undefined' ? true : read()
13
-
14
- function subscribe(listener: () => void): () => void {
15
- listeners.add(listener)
16
- const storage = (event: StorageEvent) => {
17
- if (event.key !== KEY) return
18
- enabled = read()
19
- for (const each of listeners) each()
20
- }
21
- window.addEventListener('storage', storage)
22
- return () => { listeners.delete(listener); window.removeEventListener('storage', storage) }
23
- }
24
-
25
- export function mascotEnabled(): boolean {
26
- return enabled
27
- }
28
-
29
- export function setMascotEnabled(on: boolean): void {
30
- enabled = on
31
- try { localStorage.setItem(KEY, on ? 'on' : 'off') } catch { /* The choice holds for this page only. */ }
32
- for (const listener of listeners) listener()
33
- }
34
-
35
- export function useMascotEnabled(): boolean {
36
- return useSyncExternalStore(subscribe, mascotEnabled, () => true)
37
- }
@@ -1,79 +0,0 @@
1
- import { useEffect, useRef, useState } from 'react'
2
- import type { CSSProperties, RefObject } from 'react'
3
- import type { MascotAction } from '@lincyaw/design/mascot'
4
- import { PmMascot } from '../components.tsx'
5
- import { useMascotEnabled } from '../mascot-preference.ts'
6
- import { useWorkbenchState } from '../context.tsx'
7
- import { pageId, richNodeIds } from '../document-editor/projection.ts'
8
-
9
- /** How long the kitten walks from one changed place to the next, pats it, and stays when it is done (ms). */
10
- const WALK_MS = 700, PAT_MS = 900, DONE_MS = 2200
11
- /** At most this many changed places are visited. */
12
- const STOPS = 6
13
-
14
- type Point = { x: number, y: number }
15
- type Phase = 'walk' | 'pat' | 'done' | 'gone'
16
-
17
- /**
18
- * The kitten visiting a list of points in its host (each the lower left of a changed place): it walks to each, pats it,
19
- * hops when it is done and leaves. A new `id` starts it again from the first point; `where(i)` is asked on every
20
- * render, so the kitten follows the places as the host lays out again. Nothing shows when the kitten is turned off.
21
- */
22
- function KittenTrail({ id, count, where }: { id: number | undefined, count: number, where(index: number): Point | undefined }) {
23
- const enabled = useMascotEnabled()
24
- const [step, setStep] = useState<{ id: number, stop: number, phase: Phase }>()
25
- const facing = useRef<'left' | 'right'>('right'), last = useRef<Point>()
26
- useEffect(() => {
27
- if (id !== undefined && count > 0) setStep(current => current?.id === id ? current : { id, stop: 0, phase: 'walk' })
28
- }, [id, count])
29
- useEffect(() => {
30
- if (!step || step.phase === 'gone') return
31
- const stops = Math.min(STOPS, count)
32
- const next: typeof step = step.phase === 'walk' ? { ...step, phase: 'pat' }
33
- : step.phase === 'pat' ? (step.stop + 1 < stops ? { ...step, stop: step.stop + 1, phase: 'walk' } : { ...step, phase: 'done' })
34
- : { ...step, phase: 'gone' }
35
- const timer = setTimeout(() => setStep(next), step.phase === 'walk' ? WALK_MS : step.phase === 'pat' ? PAT_MS : DONE_MS)
36
- return () => clearTimeout(timer)
37
- }, [step, count])
38
- if (!enabled || !step || step.phase === 'gone' || step.id !== id) return null
39
- const point = where(Math.min(step.stop, count - 1))
40
- if (!point) return null
41
- if (last.current && Math.abs(point.x - last.current.x) > 2 && step.phase === 'walk') facing.current = point.x < last.current.x ? 'left' : 'right'
42
- if (step.phase === 'walk') last.current = point
43
- const action: MascotAction = step.phase === 'walk' ? 'walk' : step.phase === 'pat' ? 'write' : 'done'
44
- return (
45
- <div className="pmw-change-trail" data-phase={step.phase} style={{ '--pmw-trail-x': `${point.x}px`, '--pmw-trail-y': `${point.y}px` } as CSSProperties}>
46
- <div className="pmw-change-trail__facing" data-facing={facing.current}><PmMascot size="small" action={action} /></div>
47
- </div>
48
- )
49
- }
50
-
51
- /** In the preview: the blocks the preview reports blinking, in the active frame's viewport. */
52
- export function PreviewChangeTrail({ stage, frames }: { stage: RefObject<HTMLDivElement | null>, frames: RefObject<HTMLDivElement | null> }) {
53
- const trail = useWorkbenchState(state => state.changeTrail)
54
- return <KittenTrail id={trail?.id} count={trail?.rects.length ?? 0} where={index => {
55
- const rect = trail?.rects[index]
56
- const frame = frames.current?.querySelector<HTMLIFrameElement>('iframe[data-active="true"]:not([hidden])')
57
- const host = stage.current
58
- if (!rect || !frame || !host) return undefined
59
- const offset = frame.getBoundingClientRect(), origin = host.getBoundingClientRect()
60
- return { x: offset.left - origin.left + rect.x, y: offset.top - origin.top + rect.y + rect.height }
61
- }} />
62
- }
63
-
64
- /** In the editor: each changed block (a text block by its first paragraph), else its page's title, on the sheet. */
65
- export function EditorChangeTrail({ sheet }: { sheet: RefObject<HTMLDivElement | null> }) {
66
- const places = useWorkbenchState(state => state.changedPlaces)
67
- const content = useWorkbenchState(state => state.authoring?.content)
68
- return <KittenTrail id={places?.id} count={places?.targets.length ?? 0} where={index => {
69
- const target = places?.targets[index], host = sheet.current
70
- if (!target || !host) return undefined
71
- const block = target.block ? content?.blocks[target.block] : undefined
72
- const ids = [...target.block ? [target.block] : [], ...block?.type === 'text' ? richNodeIds(block.content) : [], pageId(target.section)]
73
- const element = ids.map(id => host.querySelector<HTMLElement>(`[data-id="${CSS.escape(id)}"]`)).find(Boolean)
74
- if (!element) return undefined
75
- const box = element.getBoundingClientRect(), origin = host.getBoundingClientRect()
76
- // the kitten stands on the place's first line, at its left edge
77
- return { x: box.left - origin.left, y: box.top - origin.top + Math.min(box.height, 28) }
78
- }} />
79
- }