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

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 (80) 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/authoring-window.ts +100 -0
  7. package/src/body-draft-actions.ts +60 -0
  8. package/src/component-property-drafts.ts +5 -5
  9. package/src/components.tsx +10 -28
  10. package/src/context.tsx +2 -1
  11. package/src/diagnostics.ts +1 -1
  12. package/src/document-draft-store.ts +45 -18
  13. package/src/document-drafts.ts +28 -19
  14. package/src/document-editing.ts +484 -0
  15. package/src/document-editor/BlockNoteSurface.tsx +25 -9
  16. package/src/document-editor/ComponentMaterials.tsx +3 -9
  17. package/src/document-editor/ComponentPropertiesDialog.tsx +4 -4
  18. package/src/document-editor/DocumentBlockPreview.tsx +4 -2
  19. package/src/document-editor/DocumentComments.tsx +58 -9
  20. package/src/document-editor/DocumentEditor.tsx +44 -7
  21. package/src/document-editor/DocumentStepNarration.tsx +30 -25
  22. package/src/document-editor/NarrationEditor.tsx +54 -20
  23. package/src/document-editor/NewCommentDrafts.tsx +4 -3
  24. package/src/document-editor/PlaceComment.tsx +150 -0
  25. package/src/document-editor/comment-drafts.ts +16 -14
  26. package/src/document-editor/comments.ts +30 -10
  27. package/src/document-editor/component-label.ts +11 -0
  28. package/src/document-editor/file-input.ts +2 -2
  29. package/src/document-editor/focus.ts +0 -2
  30. package/src/document-editor/insertion-place.ts +60 -0
  31. package/src/document-editor/messages.ts +3 -3
  32. package/src/document-editor/narration-schema.tsx +12 -6
  33. package/src/document-editor/narration.ts +19 -3
  34. package/src/document-editor/page-body.ts +60 -0
  35. package/src/document-editor/projection.ts +2 -2
  36. package/src/document-editor/rich-text.ts +3 -2
  37. package/src/document-editor/selection.ts +13 -0
  38. package/src/document-editor/style.css +12 -1
  39. package/src/document-editor/table-identities.ts +2 -1
  40. package/src/document-production.ts +339 -0
  41. package/src/document-session.ts +193 -53
  42. package/src/history.ts +5 -5
  43. package/src/index.ts +6 -5
  44. package/src/jobs.ts +1 -1
  45. package/src/messages.ts +19 -4
  46. package/src/panels/DocumentView.tsx +66 -26
  47. package/src/panels/DocumentVoiceDialog.tsx +2 -2
  48. package/src/panels/DocumentVoiceField.tsx +1 -1
  49. package/src/panels/ExportPanel.tsx +1 -6
  50. package/src/panels/HistoryPanel.tsx +1 -1
  51. package/src/panels/PageDeleteDialog.tsx +4 -4
  52. package/src/panels/PageRenameDialog.tsx +1 -1
  53. package/src/panels/PreviewPanel.tsx +1 -4
  54. package/src/panels/ProductionCaptionSize.tsx +20 -0
  55. package/src/panels/ProductionCaptionTiming.tsx +7 -7
  56. package/src/panels/ProductionMediaClips.tsx +6 -5
  57. package/src/panels/ProductionPanel.tsx +8 -4
  58. package/src/panels/ProductionRecording.tsx +86 -9
  59. package/src/panels/ProductionRecordingRange.tsx +19 -19
  60. package/src/panels/ProductionTimeline.tsx +6 -1
  61. package/src/panels/ProductionTranscription.tsx +5 -4
  62. package/src/panels/SourceDocumentPanel.tsx +12 -15
  63. package/src/panels/TranscriptionAdoption.tsx +9 -8
  64. package/src/panels/VoiceCloneDialog.tsx +2 -1
  65. package/src/panels/VoiceoverDialog.tsx +1 -1
  66. package/src/places.ts +1 -36
  67. package/src/preview-playback.ts +1 -1
  68. package/src/production-messages.ts +6 -0
  69. package/src/production-playback.ts +1 -1
  70. package/src/production-voice-playback.ts +3 -2
  71. package/src/sha256.ts +9 -0
  72. package/src/styles.css +16 -41
  73. package/src/use-recording-adoption.ts +2 -2
  74. package/src/voiceover-jobs.ts +1 -1
  75. package/src/workbench-state.ts +157 -0
  76. package/src/workbench.ts +246 -948
  77. package/src/workspace-client.ts +21 -11
  78. package/src/assistant-mascot.ts +0 -62
  79. package/src/mascot-preference.ts +0 -37
  80. package/src/panels/ChangeTrail.tsx +0 -79
@@ -1,3 +1,14 @@
1
+ export interface DraftStoreOperation { signal?: AbortSignal, guard?(): void }
2
+ export async function waitForDraft<T>(work: Promise<T>, signal?: AbortSignal): Promise<T> {
3
+ signal?.throwIfAborted()
4
+ if (!signal) return work
5
+ return new Promise<T>((resolve, reject) => {
6
+ const abort = () => { signal.removeEventListener('abort', abort); reject(signal.reason) }
7
+ signal.addEventListener('abort', abort, { once: true })
8
+ work.then(value => { signal.removeEventListener('abort', abort); signal.aborted ? reject(signal.reason) : resolve(value) }, error => { signal.removeEventListener('abort', abort); reject(error) })
9
+ })
10
+ }
11
+
1
12
  export type DraftStoreResult = 'committed' | 'superseded' | 'failed'
2
13
  export interface DraftStoreCommit { generation: number, done: Promise<DraftStoreResult> }
3
14
  interface Operation extends DraftStoreCommit {
@@ -98,31 +109,47 @@ export class DocumentDraftStore {
98
109
 
99
110
  #notify(): void { if (!this.#closing) this.#channel?.postMessage({ scope: this.#scope, ...this.#writerOwner ? { owner: this.#writerOwner } : {}, ...this.#family ? { family: this.#family } : {} }) }
100
111
 
101
- async #commit(key: string | undefined, raw: string | undefined, markers: readonly [string, string][]): Promise<{ committed: boolean, foreign: boolean }> {
112
+ async #commit(key: string | undefined, raw: string | undefined, markers: readonly [string, string][], operation?: DraftStoreOperation): Promise<{ committed: boolean, foreign: boolean }> {
102
113
  try {
103
- const db = await this.#db()
104
- return await new Promise(resolve => {
114
+ operation?.signal?.throwIfAborted(); operation?.guard?.()
115
+ const db = await waitForDraft(this.#db(), operation?.signal)
116
+ operation?.signal?.throwIfAborted(); operation?.guard?.()
117
+ return await new Promise((resolve, reject) => {
105
118
  const tx = db.transaction(ENTRIES, 'readwrite', { durability: 'strict' }), store = tx.objectStore(ENTRIES)
106
- let foreign = false
107
- tx.oncomplete = () => resolve({ committed: true, foreign })
108
- tx.onabort = () => resolve({ committed: false, foreign })
119
+ let foreign = false, failure: unknown
120
+ const abort = () => { try { tx.abort() } catch {} }
121
+ const stop = () => operation?.signal?.removeEventListener('abort', abort)
122
+ operation?.signal?.addEventListener('abort', abort, { once: true })
123
+ tx.oncomplete = () => { stop(); resolve({ committed: true, foreign }) }
124
+ tx.onabort = () => { stop(); failure || operation?.signal?.aborted ? reject(failure ?? operation?.signal?.reason) : resolve({ committed: false, foreign }) }
109
125
  tx.onerror = () => { /* The abort event delivers the terminal transaction result. */ }
110
126
  if (tx.durability !== 'strict') { tx.abort(); return }
127
+ const check = () => { operation?.signal?.throwIfAborted(); operation?.guard?.() }
111
128
  const apply = () => {
129
+ check()
112
130
  for (const [marker, value] of markers) store.put(value, marker)
113
131
  }
114
- if (key === undefined) { apply(); return }
132
+ if (key === undefined) {
133
+ if (operation) {
134
+ const gate = store.get(markers[0]?.[0] ?? 'presmium-draft-guard')
135
+ gate.onsuccess = () => { try { apply() } catch (error) { failure = error; abort() } }
136
+ } else { try { apply() } catch (error) { failure = error; abort() } }
137
+ return
138
+ }
115
139
  const request = store.get(key), expected = this.#confirmed.get(key)
116
140
  request.onsuccess = () => {
117
- foreign = request.result !== expected
118
- if (raw !== undefined) {
119
- if (foreign) { tx.abort(); return }
120
- store.put(raw, key)
121
- } else if (!foreign) store.delete(key)
122
- apply()
141
+ try {
142
+ check()
143
+ foreign = request.result !== expected
144
+ if (raw !== undefined) {
145
+ if (foreign) { tx.abort(); return }
146
+ store.put(raw, key)
147
+ } else if (!foreign) store.delete(key)
148
+ apply()
149
+ } catch (error) { failure = error; abort() }
123
150
  }
124
151
  })
125
- } catch { return { committed: false, foreign: false } }
152
+ } catch (error) { if (operation) throw error; return { committed: false, foreign: false } }
126
153
  }
127
154
 
128
155
  #stage(key: string, raw: string | undefined, markers: readonly [string, string][]): DraftStoreCommit | undefined {
@@ -157,16 +184,16 @@ export class DocumentDraftStore {
157
184
  }
158
185
 
159
186
  /** A candidate copy must be strictly committed before the actor switches to it. */
160
- async create(key: string, raw: string): Promise<boolean> {
187
+ async create(key: string, raw: string, operation?: DraftStoreOperation): Promise<boolean> {
161
188
  if (this.#closing) return false
162
- const result = await this.#commit(key, raw, [])
189
+ const result = await this.#commit(key, raw, [], operation)
163
190
  if (result.committed) { this.#confirmed.set(key, raw); this.#notify() }
164
191
  return result.committed
165
192
  }
166
193
 
167
- async mark(markers: readonly [string, string][]): Promise<boolean> {
194
+ async mark(markers: readonly [string, string][], operation?: DraftStoreOperation): Promise<boolean> {
168
195
  if (this.#closing) return false
169
- const result = await this.#commit(undefined, undefined, markers)
196
+ const result = await this.#commit(undefined, undefined, markers, operation)
170
197
  if (result.committed) this.#notify()
171
198
  return result.committed
172
199
  }
@@ -1,7 +1,8 @@
1
- import { isRecord, validateDocument } from '@lincyaw/kernel'
1
+ import { isRecord, randomUUID, validateDocument } from '@lincyaw/kernel'
2
2
  import type { TeachingDocument } from '@lincyaw/kernel'
3
3
  import { DocumentDraftStore } from './document-draft-store.ts'
4
- import type { DraftStoreCommit } from './document-draft-store.ts'
4
+ import { sha256 as tokenOf } from './sha256.ts'
5
+ import type { DraftStoreCommit, DraftStoreOperation } from './document-draft-store.ts'
5
6
 
6
7
  export interface DocumentDraftReference { id: string, revision: number, token: string }
7
8
  export interface DocumentDraft {
@@ -45,36 +46,36 @@ function documentDraft(value: unknown, documentId: string): DocumentDraft | unde
45
46
  return { content: checked.document, baseVersion: value.baseVersion, baseHash: value.baseHash, input: value.input,
46
47
  pendingHash: value.pendingHash, composing: value.composing, origins: value.origins }
47
48
  }
48
- async function tokenOf(raw: string): Promise<string> {
49
- const bytes = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(raw))
50
- return [...new Uint8Array(bytes)].map(value => value.toString(16).padStart(2, '0')).join('')
51
- }
52
49
 
53
50
  /** Strictly committed document backups, with old localStorage copies identified as unconfirmed rescue material. */
54
51
  export class DocumentDrafts {
55
- readonly #owner = crypto.randomUUID()
52
+ readonly #owner = randomUUID()
56
53
  readonly #owned = new Map<string, { identity: string, revision: number, raw: string }>()
57
54
  #store?: DocumentDraftStore
58
55
  #identity?: string
59
56
  #closed = false
57
+ #revision = 0
58
+ get revision(): number { return this.#revision }
59
+ #touch(): void { if (!this.#closed) { this.#revision++; this.#changed?.() } }
60
60
  readonly #changed?: () => void
61
61
  constructor(changed?: () => void) { this.#changed = changed }
62
62
 
63
63
  #storage(identity: string): DocumentDraftStore | undefined {
64
64
  if (this.#closed || this.#identity !== undefined && this.#identity !== identity) return
65
65
  this.#identity = identity
66
- return this.#store ??= new DocumentDraftStore(identity, this.#changed)
66
+ return this.#store ??= new DocumentDraftStore(identity, () => this.#touch())
67
67
  }
68
68
  matches(identity: string, key: string | null): boolean { return key === null || key.startsWith(prefix(identity)) || key.startsWith(markerPrefix(identity)) }
69
69
 
70
- async read(identity: string, documentId: string): Promise<{ records: RecoverableDocumentDraft[], failed: boolean }> {
70
+ async read(identity: string, documentId: string, signal?: AbortSignal): Promise<{ records: RecoverableDocumentDraft[], failed: boolean }> {
71
+ signal?.throwIfAborted()
71
72
  const store = this.#storage(identity), values: { key: string, raw: unknown, source: 'durable' | 'rescue' }[] = []
72
73
  let failed = false, durable = new Map<string, unknown>()
73
74
  if (!store) return { records: [], failed: true }
74
75
  try {
75
- durable = await store.read([prefix(identity), markerPrefix(identity)])
76
+ durable = await store.read([prefix(identity), markerPrefix(identity)], signal, signal ? 2048 : undefined)
76
77
  for (const [key, raw] of durable) if (key.startsWith(prefix(identity))) values.push({ key, raw, source: 'durable' })
77
- } catch { failed = true }
78
+ } catch { signal?.throwIfAborted(); failed = true }
78
79
  // Existing localStorage input is never removed or silently treated as a strict commit.
79
80
  try {
80
81
  const keys: string[] = []
@@ -86,9 +87,10 @@ export class DocumentDrafts {
86
87
  const raw = localStorage.getItem(key)
87
88
  if (raw !== null) values.push({ key, raw, source: 'rescue' })
88
89
  }
89
- } catch { failed = true }
90
+ } catch { signal?.throwIfAborted(); failed = true }
90
91
  const records = new Map<string, RecoverableDocumentDraft>()
91
92
  for (const { key, raw, source } of values) {
93
+ signal?.throwIfAborted()
92
94
  try {
93
95
  if (typeof raw !== 'string' || raw.length > MAX_CHARS) throw new Error('document-draft-size')
94
96
  const value: unknown = JSON.parse(raw)
@@ -96,14 +98,17 @@ export class DocumentDrafts {
96
98
  || typeof value.owner !== 'string' || !value.owner || value.owner.length > 200 || typeof value.updatedAt !== 'number' || !Number.isSafeInteger(value.updatedAt) || value.updatedAt < 0 || Number.isNaN(new Date(value.updatedAt).getTime())) throw new Error('document-draft-data')
97
99
  const draft = documentDraft(value, documentId)
98
100
  if (!draft) throw new Error('document-draft-document')
101
+ if (draft.baseHash.startsWith('json:')) draft.baseHash = await tokenOf(draft.baseHash.slice(5))
102
+ if (draft.pendingHash?.startsWith('json:')) draft.pendingHash = await tokenOf(draft.pendingHash.slice(5))
99
103
  const token = await tokenOf(raw), marker = settledKey(identity, { id: value.id, revision: value.revision, token })
100
104
  if (durable.get(marker) === token) continue
101
105
  if (source === 'rescue' && localStorage.getItem(marker) === token) continue
102
106
  const id = JSON.stringify([value.id, value.revision, token])
103
107
  if (!records.has(id)) records.set(id, { ...draft, format: 2, identity, id: value.id, owner: value.owner, revision: value.revision, updatedAt: value.updatedAt, token, source,
104
108
  owned: source === 'durable' && value.owner === this.#owner && (this.#owned.get(value.id)?.raw === raw || store.owns(key, raw)) })
105
- } catch { failed = true }
109
+ } catch { signal?.throwIfAborted(); failed = true }
106
110
  }
111
+ signal?.throwIfAborted()
107
112
  return { records: [...records.values()], failed }
108
113
  }
109
114
 
@@ -113,7 +118,7 @@ export class DocumentDrafts {
113
118
  const owned = previous && this.#owned.get(previous.id)
114
119
  if (previous && (!owned || owned.identity !== identity || previous.owner !== this.#owner || previous.revision !== owned.revision)) return
115
120
  const changed = previous && store.conflicted(keyOf(identity, previous.id))
116
- const record: StoredDocumentDraft = { ...draft, format: 2, identity, id: !changed && previous ? previous.id : crypto.randomUUID(), owner: this.#owner, revision: changed ? 1 : (owned?.revision ?? 0) + 1, updatedAt: Date.now() }
121
+ const record: StoredDocumentDraft = { ...draft, format: 2, identity, id: !changed && previous ? previous.id : randomUUID(), owner: this.#owner, revision: changed ? 1 : (owned?.revision ?? 0) + 1, updatedAt: Date.now() }
117
122
  if (!Number.isSafeInteger(record.revision)) return
118
123
  try { const raw = JSON.stringify(record); return raw.length <= MAX_CHARS ? { record, raw } : undefined }
119
124
  catch { return }
@@ -126,21 +131,25 @@ export class DocumentDrafts {
126
131
  const commit = this.#store!.put(keyOf(identity, next.record.id), next.raw)
127
132
  if (!commit) return
128
133
  this.#owned.set(next.record.id, { identity, revision: next.record.revision, raw: next.raw })
134
+ void commit.done.then(() => this.#touch())
129
135
  if (previous && previous.id !== next.record.id) this.archive(previous)
130
136
  return { ...commit, record: next.record, clearing: false }
131
137
  }
132
138
 
133
- async fork(identity: string, draft: DocumentDraft): Promise<StoredDocumentDraft | undefined> {
139
+ async fork(identity: string, draft: DocumentDraft, operation?: DraftStoreOperation): Promise<StoredDocumentDraft | undefined> {
134
140
  const next = this.#record(identity, draft)
135
- if (!next || !await this.#store!.create(keyOf(identity, next.record.id), next.raw)) return
141
+ if (!next || !await this.#store!.create(keyOf(identity, next.record.id), next.raw, operation)) return
136
142
  this.#owned.set(next.record.id, { identity, revision: next.record.revision, raw: next.raw })
143
+ this.#touch()
137
144
  return next.record
138
145
  }
139
146
 
140
147
  /** Immutable, revision-specific markers never delete another tab's newer record. */
141
- async settle(identity: string, values: readonly DocumentDraftReference[]): Promise<boolean> {
148
+ async settle(identity: string, values: readonly DocumentDraftReference[], operation?: DraftStoreOperation): Promise<boolean> {
142
149
  const store = this.#storage(identity)
143
- return Boolean(store && await store.mark(values.map(value => [settledKey(identity, value), value.token])))
150
+ const done = Boolean(store && await store.mark(values.map(value => [settledKey(identity, value), value.token]), operation))
151
+ if (done) this.#touch()
152
+ return done
144
153
  }
145
154
 
146
155
  remove(identity: string, record: StoredDocumentDraft, origins: readonly DocumentDraftReference[]): DocumentDraftCommit | undefined {
@@ -148,7 +157,7 @@ export class DocumentDrafts {
148
157
  if (!store || !owned || owned.identity !== identity || record.owner !== this.#owner || owned.revision !== record.revision) return
149
158
  const commit = store.remove(keyOf(identity, record.id), origins.map(value => [settledKey(identity, value), value.token]))
150
159
  if (!commit) return
151
- void commit.done.then(result => { if (result === 'committed' && this.#owned.get(record.id)?.revision === record.revision) this.#owned.delete(record.id) })
160
+ void commit.done.then(result => { if (result === 'committed' && this.#owned.get(record.id)?.revision === record.revision) this.#owned.delete(record.id); this.#touch() })
152
161
  return { ...commit, record, clearing: true }
153
162
  }
154
163