@lincyaw/workbench 0.2.0-rc.7 → 0.2.0-rc.70
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 +5 -4
- package/src/MediaClipEditor.tsx +4 -3
- package/src/StepProductionEditor.tsx +13 -4
- package/src/approvals.tsx +6 -1
- package/src/audio-recording.ts +27 -5
- package/src/authoring-client.ts +10 -10
- package/src/authoring-draft-fragment.ts +3 -1
- package/src/authoring-window.ts +100 -0
- package/src/body-draft-actions.ts +60 -0
- package/src/caption-timing-drafts.ts +18 -1
- package/src/component-property-drafts.ts +5 -5
- package/src/components.tsx +22 -31
- package/src/context.tsx +5 -3
- package/src/diagnostics.ts +1 -1
- package/src/document-draft-store.ts +45 -18
- package/src/document-drafts.ts +28 -19
- package/src/document-editing.ts +484 -0
- package/src/document-editor/BlockNoteSurface.tsx +25 -9
- package/src/document-editor/ComponentMaterials.tsx +3 -9
- package/src/document-editor/ComponentPropertiesDialog.tsx +4 -4
- package/src/document-editor/DocumentBlockPreview.tsx +4 -2
- package/src/document-editor/DocumentComments.tsx +85 -14
- package/src/document-editor/DocumentEditor.tsx +44 -7
- package/src/document-editor/DocumentStepNarration.tsx +33 -25
- package/src/document-editor/NarrationEditor.tsx +54 -20
- package/src/document-editor/NewCommentDrafts.tsx +114 -31
- package/src/document-editor/PlaceComment.tsx +228 -0
- package/src/document-editor/comment-draft-actions.ts +40 -20
- package/src/document-editor/comment-draft-inspection.ts +2 -2
- package/src/document-editor/comment-drafts.ts +73 -25
- package/src/document-editor/comments.ts +30 -10
- package/src/document-editor/component-label.ts +11 -0
- package/src/document-editor/file-input.ts +2 -2
- package/src/document-editor/focus.ts +0 -2
- package/src/document-editor/insertion-place.ts +60 -0
- package/src/document-editor/messages.ts +3 -3
- package/src/document-editor/narration-schema.tsx +12 -6
- package/src/document-editor/narration.ts +19 -3
- package/src/document-editor/page-body.ts +60 -0
- package/src/document-editor/projection.ts +2 -2
- package/src/document-editor/rich-text.ts +3 -2
- package/src/document-editor/selection.ts +13 -0
- package/src/document-editor/style.css +12 -1
- package/src/document-editor/table-identities.ts +2 -1
- package/src/document-production.ts +491 -0
- package/src/document-session.ts +202 -56
- package/src/document-timing-drafts.ts +22 -2
- package/src/history.ts +5 -5
- package/src/index.ts +6 -5
- package/src/jobs.ts +1 -1
- package/src/media-drafts.ts +43 -1
- package/src/messages.ts +31 -14
- package/src/panels/DocumentView.tsx +71 -28
- package/src/panels/DocumentVoiceDialog.tsx +25 -9
- package/src/panels/DocumentVoiceField.tsx +1 -1
- package/src/panels/ExportPanel.tsx +1 -6
- package/src/panels/HistoryPanel.tsx +32 -2
- package/src/panels/PageDeleteDialog.tsx +4 -4
- package/src/panels/PageRenameDialog.tsx +1 -1
- package/src/panels/PreviewPanel.tsx +1 -4
- package/src/panels/ProductionCaptionSize.tsx +20 -0
- package/src/panels/ProductionCaptionTiming.tsx +12 -9
- package/src/panels/ProductionMediaClips.tsx +23 -7
- package/src/panels/ProductionPanel.tsx +8 -4
- package/src/panels/ProductionRecording.tsx +86 -9
- package/src/panels/ProductionRecordingRange.tsx +21 -19
- package/src/panels/ProductionTimeline.tsx +6 -1
- package/src/panels/ProductionTranscription.tsx +5 -4
- package/src/panels/SourceDocumentPanel.tsx +16 -36
- package/src/panels/TranscriptionAdoption.tsx +40 -34
- package/src/panels/VoiceCloneDialog.tsx +2 -1
- package/src/panels/VoiceoverDialog.tsx +1 -1
- package/src/places.ts +1 -36
- package/src/preview-playback.ts +1 -1
- package/src/production-draft-actions.ts +37 -0
- package/src/production-messages.ts +6 -0
- package/src/production-playback.ts +1 -1
- package/src/production-voice-playback.ts +3 -2
- package/src/sha256.ts +9 -0
- package/src/styles.css +21 -43
- package/src/transcription-drafts.ts +124 -0
- package/src/use-recording-adoption.ts +2 -2
- package/src/voice-drafts.ts +32 -8
- package/src/voiceover-jobs.ts +1 -1
- package/src/workbench-state.ts +157 -0
- package/src/workbench.ts +258 -951
- package/src/workspace-client.ts +21 -11
- package/src/assistant-mascot.ts +0 -62
- package/src/mascot-preference.ts +0 -37
- package/src/panels/ChangeTrail.tsx +0 -79
package/src/document-session.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { hasRole, validateDocument } from '@lincyaw/kernel'
|
|
2
|
-
import type { DocumentAnswer, DocumentChangeAnswer, DocumentImageImportAnswer, DocumentIssue, Member, TeachingDocument } from '@lincyaw/kernel'
|
|
1
|
+
import { hasRole, isRecord, randomUUID, validateDocument } from '@lincyaw/kernel'
|
|
2
|
+
import type { CanonicalSource, ClientBodyDraftReference, DocumentAnswer, DocumentChangeAnswer, DocumentImageImportAnswer, DocumentIssue, Member, TeachingDocument } from '@lincyaw/kernel'
|
|
3
3
|
import { WorkspaceServerError } from './workspace-client.ts'
|
|
4
4
|
import type { WorkspaceClient } from './workspace-client.ts'
|
|
5
|
+
import { waitForDraft } from './document-draft-store.ts'
|
|
5
6
|
import { DocumentDrafts } from './document-drafts.ts'
|
|
7
|
+
import { sha256 } from './sha256.ts'
|
|
6
8
|
import type { DocumentDraftChoice, DocumentDraftCommit, DocumentDraftReference, StoredDocumentDraft } from './document-drafts.ts'
|
|
7
9
|
|
|
8
10
|
export interface DocumentSessionState {
|
|
@@ -28,12 +30,16 @@ export interface DocumentSessionState {
|
|
|
28
30
|
recoveryDrafts?: DocumentDraftChoice[]
|
|
29
31
|
recoveryChanged?: boolean
|
|
30
32
|
}
|
|
33
|
+
export interface DocumentDraftOperation { signal: AbortSignal, guard(): void, source: CanonicalSource, localDraft?: true }
|
|
34
|
+
export interface BodyDraftCandidate { draft: DocumentDraftReference & { current: boolean }, content: TeachingDocument, storage: 'durable' | 'rescue' | 'session', updatedAt: number, baseVersion: string, composing: boolean, pendingSubmission: boolean }
|
|
31
35
|
interface Slot {
|
|
36
|
+
observedSource?: CanonicalSource
|
|
32
37
|
state: DocumentSessionState
|
|
33
38
|
read: number
|
|
34
39
|
compositions: Set<string>
|
|
35
40
|
input: number
|
|
36
41
|
baseHash: string
|
|
42
|
+
baseContent: TeachingDocument
|
|
37
43
|
storage: string
|
|
38
44
|
drafts: DocumentDrafts
|
|
39
45
|
backup?: DocumentDraftCommit
|
|
@@ -54,6 +60,7 @@ interface Slot {
|
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
const serialize = (value: unknown) => JSON.stringify(value, (_, entry) => entry && typeof entry === 'object' && !Array.isArray(entry) ? Object.fromEntries(Object.entries(entry).sort(([a], [b]) => a.localeCompare(b))) : entry)
|
|
63
|
+
const sameSource = (a: CanonicalSource | undefined, b: CanonicalSource) => a?.documentId === b.documentId && a.version === b.version && a.fingerprint === b.fingerprint
|
|
57
64
|
/** Non-body authoring changes keep native selection, comment drafts and body history. */
|
|
58
65
|
function keepsEditorContent(before: TeachingDocument, after: TeachingDocument): boolean {
|
|
59
66
|
const editing = ({ assets: _assets, production: _production, comments: _comments, definitions: _definitions, ...document }: TeachingDocument) => {
|
|
@@ -88,10 +95,21 @@ function keepsEditorContent(before: TeachingDocument, after: TeachingDocument):
|
|
|
88
95
|
}
|
|
89
96
|
|
|
90
97
|
async function digest(value: TeachingDocument): Promise<string> {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
98
|
+
return sha256(serialize(value))
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Replay pending author input on a newer saved document; an overlapping value belongs to the author. */
|
|
102
|
+
function rebaseDocument(base: TeachingDocument, local: TeachingDocument, remote: TeachingDocument): TeachingDocument {
|
|
103
|
+
const merge = (before: unknown, author: unknown, saved: unknown): unknown => {
|
|
104
|
+
if (serialize(author) === serialize(before)) return structuredClone(saved)
|
|
105
|
+
if (serialize(saved) === serialize(before) || serialize(author) === serialize(saved)) return structuredClone(author)
|
|
106
|
+
if (isRecord(before) && isRecord(author) && isRecord(saved)) return Object.fromEntries([...new Set([...Object.keys(before), ...Object.keys(author), ...Object.keys(saved)])]
|
|
107
|
+
.map(key => [key, merge(before[key], author[key], saved[key])] as const).filter(([, value]) => value !== undefined))
|
|
108
|
+
return structuredClone(author)
|
|
109
|
+
}
|
|
110
|
+
const merged = validateDocument(merge(base, local, remote))
|
|
111
|
+
// A structural conflict is one document operation: preserve the author's valid complete document.
|
|
112
|
+
return merged.document && !merged.issues.some(issue => issue.severity === 'error') ? merged.document : structuredClone(local)
|
|
95
113
|
}
|
|
96
114
|
|
|
97
115
|
/** One source actor per document. Runtime compilation and iframe lifecycle never own its input. */
|
|
@@ -125,7 +143,7 @@ export class DocumentSession {
|
|
|
125
143
|
this.#leaveTarget = target
|
|
126
144
|
}
|
|
127
145
|
|
|
128
|
-
async open(directory: string, expectedIdentity?: string): Promise<'identity-changed' | void> {
|
|
146
|
+
async open(directory: string, expectedIdentity?: string, signal?: AbortSignal): Promise<'identity-changed' | void> {
|
|
129
147
|
if (this.#active !== directory) this.interrupt()
|
|
130
148
|
const opening = ++this.#opening
|
|
131
149
|
this.#active = directory
|
|
@@ -134,7 +152,7 @@ export class DocumentSession {
|
|
|
134
152
|
if (cached) { cached.read++; cached.state = { ...cached.state, loading: true } }
|
|
135
153
|
this.#changed(cached ? { ...cached.state } : { directory, loading: true, dirty: false, saving: false, restored: false, composing: false, storageFailure: false, reset: 0, issues: [] })
|
|
136
154
|
try {
|
|
137
|
-
const [info, answer] = await Promise.all([this.#client.readWorkbenchInfo(), this.#client.readDocument(directory)])
|
|
155
|
+
const [info, answer] = await waitForDraft(Promise.all([this.#client.readWorkbenchInfo(), this.#client.readDocument(directory, signal)]), signal)
|
|
138
156
|
if (this.#active !== directory || opening !== this.#opening) return
|
|
139
157
|
if (expectedIdentity !== undefined && JSON.stringify([info.root, answer.content.id]) !== expectedIdentity) return 'identity-changed'
|
|
140
158
|
let slot = this.#slots.get(directory)
|
|
@@ -142,7 +160,7 @@ export class DocumentSession {
|
|
|
142
160
|
const baseHash = await digest(answer.content)
|
|
143
161
|
if (this.#disposed || this.#active !== directory || opening !== this.#opening) return
|
|
144
162
|
const drafts = new DocumentDrafts(() => { if (slot && !this.#disposed) void this.#readDrafts(slot) })
|
|
145
|
-
slot = { input: 0, read: 0, compositions: new Set(), drafts, baseHash, storage: `presmium.document.v1:${JSON.stringify([info.root, answer.content.id])}`, state: { directory, member: info.member, identity: JSON.stringify([info.root, answer.content.id]), content: answer.content, version: answer.version, fingerprint: answer.fingerprint, loading: true, dirty: false, saving: false, restored: false, composing: false, storageFailure: false, reset: 0, issues: answer.issues } }
|
|
163
|
+
slot = { observedSource: this.#source(answer), input: 0, read: 0, compositions: new Set(), drafts, baseHash, baseContent: structuredClone(answer.content), storage: `presmium.document.v1:${JSON.stringify([info.root, answer.content.id])}`, state: { directory, member: info.member, identity: JSON.stringify([info.root, answer.content.id]), content: answer.content, version: answer.version, fingerprint: answer.fingerprint, loading: true, dirty: false, saving: false, restored: false, composing: false, storageFailure: false, reset: 0, issues: answer.issues } }
|
|
146
164
|
this.#slots.set(directory, slot)
|
|
147
165
|
await this.#restore(slot, answer)
|
|
148
166
|
} else {
|
|
@@ -193,23 +211,77 @@ export class DocumentSession {
|
|
|
193
211
|
for (const changed of slot.saveWaiters ?? []) changed()
|
|
194
212
|
if (this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot) this.#changed({ ...slot.state })
|
|
195
213
|
}
|
|
214
|
+
bodyDraftRevision(): number { const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active); return slot?.drafts.revision ?? 0 }
|
|
215
|
+
hasPendingAssetWrite(): boolean { return Boolean(this.#active !== undefined && this.#slots.get(this.#active)?.assetFlight) }
|
|
216
|
+
bodyDraftCapabilities(): { read: boolean, mutate: boolean, current: boolean } {
|
|
217
|
+
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
218
|
+
const read = Boolean(slot?.state.identity && slot.state.content && slot.state.version && !slot.state.loading && !this.#disposed)
|
|
219
|
+
return { read, mutate: Boolean(read && slot && !this.#readOnly(slot) && !slot.state.composing && !slot.flight && !slot.assetFlight && !slot.state.recovering),
|
|
220
|
+
current: Boolean(slot && (slot.state.dirty || slot.state.restored || slot.state.composing || slot.state.error)) }
|
|
221
|
+
}
|
|
222
|
+
async readBodyDrafts(signal: AbortSignal, selected?: ClientBodyDraftReference): Promise<{ candidates: BodyDraftCandidate[], coverage: 'stored-and-session' | 'partial', guard(): void }> {
|
|
223
|
+
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
224
|
+
if (!slot || !this.bodyDraftCapabilities().read || !slot.state.identity || !slot.state.content || !slot.state.version) throw new Error('client-body-drafts-unavailable')
|
|
225
|
+
const identity = slot.state.identity, documentId = slot.state.content.id, input = slot.input, read = slot.read, content = slot.state.content, version = slot.state.version,
|
|
226
|
+
revision = slot.drafts.revision, composing = slot.state.composing, source = JSON.stringify(slot.observedSource), own = slot.draft, flight = slot.flight, assetFlight = slot.assetFlight
|
|
227
|
+
const guard = () => {
|
|
228
|
+
signal.throwIfAborted()
|
|
229
|
+
if (this.#disposed || this.#active !== slot.state.directory || this.#slots.get(slot.state.directory) !== slot || slot.state.identity !== identity
|
|
230
|
+
|| slot.input !== input || slot.read !== read || slot.state.content !== content || slot.state.version !== version || slot.state.composing !== composing
|
|
231
|
+
|| JSON.stringify(slot.observedSource) !== source || slot.draft !== own || slot.flight !== flight || slot.assetFlight !== assetFlight || slot.drafts.revision !== revision) throw new Error('client-body-drafts-changed')
|
|
232
|
+
}
|
|
233
|
+
guard()
|
|
234
|
+
const stored = await slot.drafts.read(identity, documentId, signal)
|
|
235
|
+
guard()
|
|
236
|
+
let candidates: BodyDraftCandidate[] = stored.records.filter(record => !(record.owned && record.id === slot.draft?.id)).map(record => ({
|
|
237
|
+
draft: { id: record.id, revision: record.revision, token: record.token, current: false }, content: record.content, storage: record.source,
|
|
238
|
+
updatedAt: record.updatedAt, baseVersion: record.baseVersion, composing: Boolean(record.composing), pendingSubmission: false,
|
|
239
|
+
}))
|
|
240
|
+
if (this.bodyDraftCapabilities().current) {
|
|
241
|
+
const token = await sha256(JSON.stringify({ domain: 'body-current', identity, id: slot.draft?.id ?? 'current', content, version, input, composing, pendingHash: slot.pendingHash }))
|
|
242
|
+
guard()
|
|
243
|
+
candidates.push({ draft: { id: slot.draft?.id ?? 'current', revision: input + 1, token, current: true }, content: structuredClone(content),
|
|
244
|
+
storage: stored.records.some(record => record.source === 'durable' && record.owned && record.id === slot.draft?.id && record.revision === slot.draft.revision && record.baseVersion === version && Boolean(record.composing) === composing && JSON.stringify(record.content) === JSON.stringify(content)) ? 'durable' : 'session', updatedAt: slot.draft?.updatedAt ?? 0,
|
|
245
|
+
baseVersion: version, composing, pendingSubmission: Boolean(flight || assetFlight) })
|
|
246
|
+
}
|
|
247
|
+
candidates.sort((a, b) => a.draft.token.localeCompare(b.draft.token))
|
|
248
|
+
if (selected) {
|
|
249
|
+
candidates = candidates.filter(value => value.draft.token === selected)
|
|
250
|
+
if (candidates.length !== 1) throw new Error('client-body-drafts-changed')
|
|
251
|
+
}
|
|
252
|
+
guard()
|
|
253
|
+
return { candidates, coverage: stored.failed ? 'partial' : 'stored-and-session', guard }
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
#source(answer: DocumentAnswer): CanonicalSource { return { documentId: answer.content.id, version: answer.version, fingerprint: answer.fingerprint } }
|
|
257
|
+
canonicalSource(): CanonicalSource | undefined { const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active); return slot?.observedSource && { ...slot.observedSource } }
|
|
258
|
+
#checkOperation(operation?: DocumentDraftOperation, answer?: DocumentAnswer): void {
|
|
259
|
+
operation?.signal.throwIfAborted(); operation?.guard()
|
|
260
|
+
if (operation && !operation.localDraft && answer && !sameSource(this.#source(answer), operation.source)) throw new Error('client-source-changed')
|
|
261
|
+
}
|
|
196
262
|
#error(error: unknown): string { return error instanceof WorkspaceServerError ? error.answer.error ?? 'document-request-failed' : 'document-disconnected' }
|
|
197
263
|
#readOnly(slot: Slot): boolean { return this.#client.capabilities?.readOnly === true || Boolean(slot.state.member && !hasRole(slot.state.member, 'editor')) }
|
|
198
264
|
#issues(error: unknown): DocumentIssue[] { const value = error instanceof WorkspaceServerError ? (error.answer as { issues?: DocumentIssue[] }).issues : undefined; return Array.isArray(value) ? value : [] }
|
|
199
265
|
|
|
200
|
-
async refresh(directory = this.#active): Promise<void> {
|
|
266
|
+
async refresh(directory = this.#active, operation?: DocumentDraftOperation): Promise<void> {
|
|
201
267
|
if (directory === undefined) return
|
|
202
268
|
const slot = this.#slots.get(directory)
|
|
203
269
|
if (!slot) { if (directory === this.#active) await this.open(directory); return }
|
|
204
|
-
if (slot.flight || slot.assetFlight) { slot.refreshed = true; return }
|
|
205
|
-
const read = ++slot.read, current = () =>
|
|
270
|
+
if (slot.flight || slot.assetFlight) { slot.refreshed = true; if (operation) throw new Error('client-body-drafts-changed'); return }
|
|
271
|
+
const read = ++slot.read, current = () => {
|
|
272
|
+
const matches = slot.read === read && this.#slots.get(directory) === slot
|
|
273
|
+
if (operation && !matches) throw new Error('client-body-drafts-changed')
|
|
274
|
+
return matches
|
|
275
|
+
}
|
|
206
276
|
try {
|
|
207
|
-
const answer = await this.#client.readDocument(directory)
|
|
277
|
+
const answer = await this.#client.readDocument(directory, operation?.signal)
|
|
208
278
|
if (!current()) return
|
|
209
|
-
if (slot.flight || slot.assetFlight) { slot.refreshed = true; return }
|
|
210
|
-
|
|
279
|
+
if (slot.flight || slot.assetFlight) { slot.refreshed = true; if (operation) throw new Error('client-body-drafts-changed'); return }
|
|
280
|
+
this.#checkOperation(operation, answer)
|
|
281
|
+
await this.#adopt(slot, answer, () => { this.#checkOperation(operation); return current() })
|
|
211
282
|
if (current()) { this.#persist(slot); this.#emit(slot); void this.#readDrafts(slot) }
|
|
212
283
|
} catch (error) {
|
|
284
|
+
if (operation) throw error
|
|
213
285
|
if (current()) { slot.state = { ...slot.state, error: this.#error(error), issues: this.#issues(error) }; this.#emit(slot) }
|
|
214
286
|
}
|
|
215
287
|
}
|
|
@@ -217,6 +289,7 @@ export class DocumentSession {
|
|
|
217
289
|
async #adopt(slot: Slot, answer: DocumentAnswer, current = () => true): Promise<void> {
|
|
218
290
|
if (!current()) return
|
|
219
291
|
if (answer.content.id !== slot.state.content?.id) { slot.state = { ...slot.state, error: 'document-identity-changed' }; return }
|
|
292
|
+
slot.observedSource = this.#source(answer)
|
|
220
293
|
if (answer.version === slot.state.version) {
|
|
221
294
|
if (answer.fingerprint !== slot.state.fingerprint) slot.state = { ...slot.state, fingerprint: answer.fingerprint, issues: answer.issues }
|
|
222
295
|
return
|
|
@@ -235,11 +308,20 @@ export class DocumentSession {
|
|
|
235
308
|
slot.baseHash = remoteHash; slot.pendingHash = undefined
|
|
236
309
|
slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, error: undefined, issues: answer.issues }
|
|
237
310
|
} else if (remoteHash === slot.baseHash) slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, issues: answer.issues }
|
|
238
|
-
else
|
|
311
|
+
else if (!slot.state.composing) {
|
|
312
|
+
const content = rebaseDocument(slot.baseContent, slot.state.content!, answer.content)
|
|
313
|
+
const reset = slot.state.reset + (keepsEditorContent(slot.state.content!, content) ? 0 : 1)
|
|
314
|
+
slot.baseHash = remoteHash
|
|
315
|
+
slot.pendingHash = undefined
|
|
316
|
+
slot.state = { ...slot.state, content, reset, version: answer.version, fingerprint: answer.fingerprint, error: undefined, dirty: true, issues: answer.issues }
|
|
317
|
+
}
|
|
318
|
+
if (slot.state.version === answer.version) slot.baseContent = structuredClone(answer.content)
|
|
239
319
|
return
|
|
240
320
|
}
|
|
241
321
|
const reset = slot.state.reset + (keepsEditorContent(slot.state.content!, answer.content) ? 0 : 1)
|
|
242
322
|
slot.baseHash = remoteHash
|
|
323
|
+
slot.baseContent = structuredClone(answer.content)
|
|
324
|
+
slot.observedSource = this.#source(answer)
|
|
243
325
|
slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, content: answer.content, reset, error: undefined, issues: answer.issues }
|
|
244
326
|
}
|
|
245
327
|
|
|
@@ -283,33 +365,51 @@ export class DocumentSession {
|
|
|
283
365
|
this.#persist(slot); this.#emit(slot)
|
|
284
366
|
}
|
|
285
367
|
|
|
286
|
-
async #save(slot: Slot, immediate = false): Promise<void> {
|
|
368
|
+
async #save(slot: Slot, immediate = false, operation?: DocumentDraftOperation): Promise<void> {
|
|
287
369
|
if (immediate) { clearTimeout(slot.timer); slot.timer = undefined }
|
|
288
370
|
else if (slot.timer !== undefined) return
|
|
289
371
|
if (this.#readOnly(slot) || slot.flight || slot.assetFlight || !slot.state.dirty || slot.state.composing || slot.state.restored || slot.state.error || !slot.state.content || !slot.state.version) return
|
|
372
|
+
const check = () => {
|
|
373
|
+
this.#checkOperation(operation)
|
|
374
|
+
if (operation && (slot.state.version !== operation.source.version || !sameSource(slot.observedSource, operation.source))) throw new Error('client-source-changed')
|
|
375
|
+
}
|
|
376
|
+
check()
|
|
290
377
|
slot.read++
|
|
291
|
-
const flight = { id:
|
|
378
|
+
const flight = { id: randomUUID(), input: slot.input, content: structuredClone(slot.state.content), baseVersion: slot.state.version, hash: undefined as string | undefined }
|
|
292
379
|
slot.flight = flight
|
|
380
|
+
let completed = false
|
|
293
381
|
slot.state = { ...slot.state, saving: true }; this.#emit(slot)
|
|
294
382
|
try {
|
|
295
383
|
flight.hash = await digest(flight.content)
|
|
384
|
+
check()
|
|
385
|
+
if (operation && (slot.input !== flight.input || slot.state.composing)) throw new Error('client-body-drafts-changed')
|
|
296
386
|
slot.pendingHash = flight.hash
|
|
297
387
|
this.#persist(slot)
|
|
298
|
-
const answer = await this.#client.changeDocument(slot.state.directory, { id: flight.id, baseDocumentVersion: flight.baseVersion, replacement: flight.content })
|
|
388
|
+
const answer = await this.#client.changeDocument(slot.state.directory, { id: flight.id, baseDocumentVersion: flight.baseVersion, replacement: flight.content }, operation?.signal)
|
|
299
389
|
if (answer.changeId !== flight.id || answer.document !== slot.state.directory || answer.content.id !== flight.content.id) throw new Error('document-confirmation')
|
|
300
390
|
this.#ack(slot, flight, answer)
|
|
391
|
+
completed = true
|
|
301
392
|
} catch (error) {
|
|
302
|
-
|
|
393
|
+
if (!operation && this.#error(error) === 'document-version-changed' && !slot.state.composing) {
|
|
394
|
+
try {
|
|
395
|
+
await this.#adopt(slot, await this.#client.readDocument(slot.state.directory))
|
|
396
|
+
slot.state = { ...slot.state, saving: false }
|
|
397
|
+
} catch (refreshError) { slot.state = { ...slot.state, saving: false, error: this.#error(refreshError), issues: this.#issues(refreshError) } }
|
|
398
|
+
} else slot.state = { ...slot.state, saving: false, error: this.#error(error), issues: this.#issues(error) }
|
|
399
|
+
if (operation) { slot.state = { ...slot.state, restored: true }; throw error }
|
|
303
400
|
} finally {
|
|
304
401
|
if (slot.flight === flight) slot.flight = undefined
|
|
305
402
|
this.#persist(slot); this.#emit(slot)
|
|
306
403
|
void this.#readDrafts(slot)
|
|
307
|
-
if (slot.refreshed) { slot.refreshed = false; await this.refresh(slot.state.directory) }
|
|
308
|
-
if (!slot.state.error) void this.#save(slot)
|
|
404
|
+
if (slot.refreshed && (!operation || completed)) { slot.refreshed = false; await this.refresh(slot.state.directory) }
|
|
405
|
+
if (!operation && !slot.state.error) void this.#save(slot)
|
|
406
|
+
else if (operation && completed && slot.input !== flight.input && !slot.state.error) this.#schedule(slot)
|
|
309
407
|
}
|
|
310
408
|
}
|
|
311
409
|
#ack(slot: Slot, flight: NonNullable<Slot['flight']>, answer: DocumentChangeAnswer): void {
|
|
410
|
+
slot.observedSource = this.#source(answer)
|
|
312
411
|
slot.baseHash = flight.hash!
|
|
412
|
+
slot.baseContent = structuredClone(answer.content)
|
|
313
413
|
slot.pendingHash = undefined
|
|
314
414
|
const same = slot.input === flight.input
|
|
315
415
|
slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, ...(same ? { content: answer.content } : {}), dirty: !same, saving: false, error: undefined, issues: answer.issues }
|
|
@@ -328,6 +428,8 @@ export class DocumentSession {
|
|
|
328
428
|
const same = slot.input === input, content = same ? answer.content : structuredClone(current)
|
|
329
429
|
if (!same) for (const [id, asset] of added) content.assets[id] = structuredClone(asset)
|
|
330
430
|
slot.baseHash = baseHash
|
|
431
|
+
slot.baseContent = structuredClone(answer.content)
|
|
432
|
+
slot.observedSource = this.#source(answer)
|
|
331
433
|
slot.state = { ...slot.state, content, version: answer.version, fingerprint: answer.fingerprint, dirty: !same, error: undefined, issues: answer.issues }
|
|
332
434
|
return true
|
|
333
435
|
}
|
|
@@ -342,7 +444,9 @@ export class DocumentSession {
|
|
|
342
444
|
const checked = validateDocument(content)
|
|
343
445
|
if (!checked.document || checked.issues.some(issue => issue.severity === 'error')) return false
|
|
344
446
|
slot.baseHash = baseHash
|
|
447
|
+
slot.baseContent = structuredClone(answer.content)
|
|
345
448
|
slot.pendingHash = undefined
|
|
449
|
+
slot.observedSource = this.#source(answer)
|
|
346
450
|
slot.state = { ...slot.state, content: structuredClone(checked.document), version: answer.version, fingerprint: answer.fingerprint,
|
|
347
451
|
dirty: slot.input !== input || slot.state.composing || serialize(content) !== serialize(answer.content), error: undefined, issues: answer.issues }
|
|
348
452
|
return true
|
|
@@ -406,7 +510,7 @@ export class DocumentSession {
|
|
|
406
510
|
const before = structuredClone(slot.state.content), input = slot.input, version = slot.state.version
|
|
407
511
|
let request = this.#imageImports.get(file)
|
|
408
512
|
if (!request || request.identity !== identity || request.version !== version) {
|
|
409
|
-
request = { identity, version, id:
|
|
513
|
+
request = { identity, version, id: randomUUID() }
|
|
410
514
|
this.#imageImports.set(file, request)
|
|
411
515
|
}
|
|
412
516
|
const read = ++slot.read
|
|
@@ -443,39 +547,58 @@ export class DocumentSession {
|
|
|
443
547
|
}
|
|
444
548
|
}
|
|
445
549
|
|
|
446
|
-
async retry(): Promise<void> {
|
|
550
|
+
async retry(operation?: DocumentDraftOperation): Promise<void> {
|
|
447
551
|
if (this.#disposed || this.#active === undefined) return
|
|
448
552
|
const slot = this.#slots.get(this.#active)
|
|
449
553
|
if (!slot) { await this.open(this.#active); return }
|
|
450
554
|
if (slot.flight || slot.assetFlight || slot.state.composing) return
|
|
451
|
-
|
|
452
|
-
slot.state =
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
555
|
+
this.#checkOperation(operation)
|
|
556
|
+
const previousError = slot.state.error, restored = slot.state.restored, input = slot.input
|
|
557
|
+
if (!operation) slot.state = { ...slot.state, restored: false, error: undefined }
|
|
558
|
+
try {
|
|
559
|
+
await this.refresh(slot.state.directory, operation)
|
|
560
|
+
this.#checkOperation(operation)
|
|
561
|
+
if (operation && slot.input !== input) throw new Error('client-body-drafts-changed')
|
|
562
|
+
if (operation) slot.state = { ...slot.state, restored: false, error: undefined }
|
|
563
|
+
if (slot.state.error) { if (!previousError) this.#emit(slot); return }
|
|
564
|
+
await this.#save(slot, true, operation)
|
|
565
|
+
} catch (error) {
|
|
566
|
+
if (operation && slot.input === input && this.#slots.get(slot.state.directory) === slot) { slot.state = { ...slot.state, restored: restored || slot.state.restored, error: slot.state.error ?? previousError }; this.#emit(slot) }
|
|
567
|
+
throw error
|
|
568
|
+
}
|
|
456
569
|
}
|
|
457
|
-
async discard(): Promise<void> {
|
|
570
|
+
async discard(operation?: DocumentDraftOperation): Promise<boolean | void> {
|
|
458
571
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
459
572
|
if (!slot || slot.flight || slot.assetFlight || slot.state.composing) return
|
|
573
|
+
const pendingSave = slot.timer !== undefined
|
|
574
|
+
clearTimeout(slot.timer); slot.timer = undefined
|
|
575
|
+
let discarded = false
|
|
460
576
|
const directory = slot.state.directory, identity = slot.state.identity, documentId = slot.state.content?.id, input = slot.input, read = ++slot.read
|
|
461
|
-
const current = () => !this.#disposed && this.#slots.get(directory) === slot && slot.read === read && slot.input === input
|
|
462
|
-
&& slot.state.identity === identity && !slot.state.composing && !slot.flight && !slot.assetFlight
|
|
577
|
+
const current = () => { this.#checkOperation(operation); return !this.#disposed && this.#slots.get(directory) === slot && slot.read === read && slot.input === input
|
|
578
|
+
&& slot.state.identity === identity && !slot.state.composing && !slot.flight && !slot.assetFlight }
|
|
463
579
|
try {
|
|
464
|
-
const answer = await this.#client.readDocument(directory)
|
|
580
|
+
const answer = await this.#client.readDocument(directory, operation?.signal)
|
|
465
581
|
if (!current()) return
|
|
466
582
|
if (answer.document !== directory || answer.content.id !== documentId) {
|
|
467
583
|
slot.state = { ...slot.state, error: 'document-identity-changed' }; this.#emit(slot)
|
|
468
584
|
return
|
|
469
585
|
}
|
|
586
|
+
this.#checkOperation(operation, answer)
|
|
470
587
|
const baseHash = await digest(answer.content)
|
|
588
|
+
this.#checkOperation(operation)
|
|
471
589
|
if (!current()) return
|
|
590
|
+
slot.observedSource = this.#source(answer)
|
|
591
|
+
discarded = true
|
|
472
592
|
slot.input++; slot.baseHash = baseHash
|
|
473
593
|
slot.pendingHash = undefined
|
|
474
594
|
slot.compositions.clear()
|
|
475
595
|
slot.state = { ...slot.state, content: answer.content, version: answer.version, fingerprint: answer.fingerprint, dirty: false, restored: false, composing: false, reset: slot.state.reset + 1, error: undefined, issues: answer.issues }
|
|
476
596
|
this.#persist(slot); this.#emit(slot)
|
|
597
|
+
if (operation && slot.backup) await waitForDraft(slot.backup.done, operation.signal)
|
|
477
598
|
void this.#readDrafts(slot)
|
|
478
|
-
|
|
599
|
+
return true
|
|
600
|
+
} catch (error) { if (operation) throw error; if (current()) { slot.state = { ...slot.state, error: this.#error(error) }; this.#emit(slot) } }
|
|
601
|
+
finally { if (!discarded && pendingSave && this.#slots.get(directory) === slot && slot.input === input && slot.timer === undefined) this.#schedule(slot) }
|
|
479
602
|
}
|
|
480
603
|
async flush(signal?: AbortSignal): Promise<void> {
|
|
481
604
|
signal?.throwIfAborted()
|
|
@@ -584,7 +707,7 @@ export class DocumentSession {
|
|
|
584
707
|
}
|
|
585
708
|
|
|
586
709
|
/** Selecting a candidate never writes the source or replaces this tab's only snapshot. */
|
|
587
|
-
async recoverDraft(value: DocumentDraftReference): Promise<void> {
|
|
710
|
+
async recoverDraft(value: DocumentDraftReference, request?: DocumentDraftOperation): Promise<'restored' | 'already-saved' | void> {
|
|
588
711
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
589
712
|
if (slot && this.#readOnly(slot)) return
|
|
590
713
|
if (!slot?.state.identity || !slot.state.content || slot.state.loading || slot.flight || slot.assetFlight || slot.state.composing || this.#disposed) return
|
|
@@ -594,47 +717,61 @@ export class DocumentSession {
|
|
|
594
717
|
clearTimeout(slot.timer); slot.timer = undefined
|
|
595
718
|
let adopted = false
|
|
596
719
|
let protectedRecord: { id?: string, revision?: number } | undefined
|
|
597
|
-
const current = () => !this.#disposed && this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.read === read && slot.input === input && slot.recoveryOperation === operation
|
|
598
|
-
&& !slot.state.composing && !slot.flight && !slot.assetFlight && (!protectedRecord || slot.draft?.id === protectedRecord.id && slot.draft?.revision === protectedRecord.revision)
|
|
720
|
+
const current = () => { this.#checkOperation(request); return !this.#disposed && this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.read === read && slot.input === input && slot.recoveryOperation === operation
|
|
721
|
+
&& !slot.state.composing && !slot.flight && !slot.assetFlight && (!protectedRecord || slot.draft?.id === protectedRecord.id && slot.draft?.revision === protectedRecord.revision) }
|
|
599
722
|
slot.state = { ...slot.state, recovering: true, recoveryChanged: false }; this.#emit(slot)
|
|
600
723
|
try {
|
|
724
|
+
this.#checkOperation(request)
|
|
601
725
|
if (!this.#persist(slot)) { this.#emit(slot); return }
|
|
602
726
|
const backup = slot.backup
|
|
603
|
-
if (backup && await backup.done !== 'committed') return
|
|
727
|
+
if (backup && await waitForDraft(backup.done, request?.signal) !== 'committed') return
|
|
604
728
|
if (!current()) return
|
|
605
729
|
protectedRecord = { id: slot.draft?.id, revision: slot.draft?.revision }
|
|
606
|
-
const candidate = (await slot.drafts.read(identity, documentId)).records.find(record => record.id === value.id && record.revision === value.revision && record.token === value.token)
|
|
730
|
+
const candidate = (await slot.drafts.read(identity, documentId, request?.signal)).records.find(record => record.id === value.id && record.revision === value.revision && record.token === value.token)
|
|
607
731
|
if (!current()) return
|
|
732
|
+
this.#checkOperation(request)
|
|
608
733
|
if (!candidate) { slot.state = { ...slot.state, recoveryChanged: true }; return }
|
|
609
|
-
const answer = await this.#client.readDocument(slot.state.directory)
|
|
734
|
+
const answer = await this.#client.readDocument(slot.state.directory, request?.signal)
|
|
610
735
|
if (!current()) return
|
|
611
736
|
if (answer.document !== slot.state.directory || answer.content.id !== documentId) { slot.state = { ...slot.state, error: 'document-identity-changed' }; return }
|
|
612
|
-
|
|
737
|
+
this.#checkOperation(request, answer)
|
|
738
|
+
slot.observedSource = this.#source(answer)
|
|
739
|
+
// Older LAN HTTP drafts stored canonical JSON in place of a SHA-256 fingerprint.
|
|
740
|
+
const [localHash, remoteHash, candidateBaseHash, pendingHash] = await Promise.all([
|
|
741
|
+
digest(candidate.content), digest(answer.content),
|
|
742
|
+
candidate.baseHash.startsWith('json:') ? sha256(candidate.baseHash.slice(5)) : candidate.baseHash,
|
|
743
|
+
candidate.pendingHash?.startsWith('json:') ? sha256(candidate.pendingHash.slice(5)) : candidate.pendingHash,
|
|
744
|
+
])
|
|
613
745
|
if (!current()) return
|
|
746
|
+
this.#checkOperation(request)
|
|
614
747
|
const origins = [...new Map([...(candidate.origins ?? []), { id: candidate.id, revision: candidate.revision, token: candidate.token }].map(value => [JSON.stringify([value.id, value.revision, value.token]), value])).values()]
|
|
615
748
|
if (localHash === remoteHash && !candidate.composing) {
|
|
616
|
-
const settled = await slot.drafts.settle(identity, origins)
|
|
749
|
+
const settled = await slot.drafts.settle(identity, origins, request && { ...request, guard: () => { if (!current()) throw new Error('client-body-drafts-changed') } })
|
|
617
750
|
if (!current()) return
|
|
618
751
|
slot.persistFailure = !settled
|
|
619
|
-
return
|
|
752
|
+
if (!settled) return
|
|
753
|
+
return 'already-saved'
|
|
620
754
|
}
|
|
621
|
-
const conflict = answer.version !== candidate.baseVersion && remoteHash !==
|
|
622
|
-
const version = remoteHash ===
|
|
623
|
-
const baseHash = remoteHash ===
|
|
755
|
+
const conflict = answer.version !== candidate.baseVersion && remoteHash !== candidateBaseHash && remoteHash !== pendingHash
|
|
756
|
+
const version = remoteHash === pendingHash || remoteHash === candidateBaseHash ? answer.version : candidate.baseVersion
|
|
757
|
+
const baseHash = remoteHash === pendingHash ? remoteHash : candidateBaseHash
|
|
624
758
|
const record = await slot.drafts.fork(identity, { content: candidate.content, baseVersion: version, baseHash, input: input + 1,
|
|
625
|
-
pendingHash
|
|
759
|
+
pendingHash, composing: Boolean(candidate.composing), origins }, request && { ...request, guard: () => { if (!current()) throw new Error('client-body-drafts-changed') } })
|
|
626
760
|
if (!current()) { if (record) slot.drafts.archive(record); return }
|
|
627
761
|
if (!record) { slot.persistFailure = true; slot.state = { ...slot.state, storageFailure: true }; return }
|
|
628
762
|
clearTimeout(slot.timer); slot.timer = undefined
|
|
763
|
+
this.#checkOperation(request)
|
|
629
764
|
if (slot.draft) slot.drafts.archive(slot.draft)
|
|
630
|
-
slot.input = input + 1; slot.baseHash = baseHash; slot.pendingHash =
|
|
765
|
+
slot.input = input + 1; slot.baseHash = baseHash; slot.pendingHash = pendingHash; slot.draft = record; slot.draftOrigins = origins
|
|
631
766
|
slot.backup = undefined; slot.persistFailure = false
|
|
632
767
|
adopted = true
|
|
633
768
|
slot.compositions.clear()
|
|
634
769
|
slot.state = { ...slot.state, content: candidate.content, version, fingerprint: answer.fingerprint, dirty: true, restored: true, composing: false,
|
|
635
770
|
backupPending: false, storageFailure: slot.legacyFailure === 'write', reset: slot.state.reset + 1,
|
|
636
771
|
error: conflict ? 'document-version-changed' : candidate.composing ? 'document-composition-interrupted' : undefined, issues: answer.issues }
|
|
772
|
+
return 'restored'
|
|
637
773
|
} catch (error) {
|
|
774
|
+
if (request) throw error
|
|
638
775
|
if (current()) slot.state = { ...slot.state, error: this.#error(error), issues: this.#issues(error) }
|
|
639
776
|
} finally {
|
|
640
777
|
if (!this.#disposed && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.recoveryOperation === operation) {
|
|
@@ -644,25 +781,34 @@ export class DocumentSession {
|
|
|
644
781
|
}
|
|
645
782
|
}
|
|
646
783
|
|
|
647
|
-
async forgetDraft(value: DocumentDraftReference): Promise<void> {
|
|
784
|
+
async forgetDraft(value: DocumentDraftReference, request?: DocumentDraftOperation): Promise<boolean | void> {
|
|
785
|
+
return this.forgetDrafts([value], request)
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/** Retire only the displayed revisions together; another tab's newer input stays recoverable. */
|
|
789
|
+
async forgetDrafts(values: readonly DocumentDraftReference[], request?: DocumentDraftOperation): Promise<boolean | void> {
|
|
648
790
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
649
|
-
if (!slot?.state.identity || !slot.state.content || slot.state.recovering || slot.flight || slot.assetFlight || slot.state.composing || this.#disposed) return
|
|
791
|
+
if (!values.length || !slot?.state.identity || !slot.state.content || slot.state.recovering || slot.flight || slot.assetFlight || slot.state.composing || this.#disposed) return
|
|
650
792
|
const identity = slot.state.identity, documentId = slot.state.content.id, input = slot.input, read = slot.read
|
|
651
793
|
const operation = slot.recoveryOperation = (slot.recoveryOperation ?? 0) + 1
|
|
652
|
-
const current = () => !this.#disposed && this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.input === input && slot.read === read && slot.recoveryOperation === operation && !slot.state.composing && !slot.flight && !slot.assetFlight
|
|
794
|
+
const current = () => { this.#checkOperation(request); return !this.#disposed && this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.input === input && slot.read === read && slot.recoveryOperation === operation && !slot.state.composing && !slot.flight && !slot.assetFlight }
|
|
653
795
|
slot.state = { ...slot.state, recovering: true }; this.#emit(slot)
|
|
654
796
|
try {
|
|
655
|
-
|
|
797
|
+
this.#checkOperation(request)
|
|
798
|
+
const records = (await slot.drafts.read(identity, documentId, request?.signal)).records
|
|
799
|
+
const candidates = values.map(value => records.find(record => record.id === value.id && record.revision === value.revision && record.token === value.token))
|
|
656
800
|
if (!current()) return
|
|
657
|
-
|
|
658
|
-
if (
|
|
801
|
+
this.#checkOperation(request)
|
|
802
|
+
if (candidates.some(candidate => candidate?.owned && candidate.id === slot.draft?.id)) return
|
|
803
|
+
if (candidates.some(candidate => !candidate)) slot.state = { ...slot.state, recoveryChanged: true }
|
|
659
804
|
else {
|
|
660
|
-
const settled = await slot.drafts.settle(identity,
|
|
805
|
+
const settled = await slot.drafts.settle(identity, values, request && { ...request, guard: () => { if (!current()) throw new Error('client-body-drafts-changed') } })
|
|
661
806
|
if (!current()) return
|
|
662
807
|
slot.persistFailure = !settled
|
|
663
808
|
slot.state = { ...slot.state, recoveryChanged: false, storageFailure: Boolean(slot.persistFailure || slot.legacyFailure === 'write') }
|
|
809
|
+
return settled
|
|
664
810
|
}
|
|
665
|
-
} catch { if (current()) { slot.persistFailure = true; slot.state = { ...slot.state, storageFailure: true } } }
|
|
811
|
+
} catch (error) { if (request) throw error; if (current()) { slot.persistFailure = true; slot.state = { ...slot.state, storageFailure: true } } }
|
|
666
812
|
finally {
|
|
667
813
|
if (!this.#disposed && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.recoveryOperation === operation) {
|
|
668
814
|
slot.state = { ...slot.state, recovering: false }; this.#emit(slot); void this.#readDrafts(slot)
|
|
@@ -8,6 +8,8 @@ const KEYS = ['minimumHoldMs', 'trailingPauseMs'] as const
|
|
|
8
8
|
/** Only unaccepted form strings live here. Accepted timing remains in the document actor. */
|
|
9
9
|
export class DocumentTimingDrafts {
|
|
10
10
|
readonly #documents = new Map<string, Map<string, StoredDraft>>()
|
|
11
|
+
readonly #stored = new Map<string, string | null>()
|
|
12
|
+
readonly #unreadable = new Set<string>()
|
|
11
13
|
failed = false
|
|
12
14
|
#storage(identity: string): string { return `presmium.document-timing.v1:${identity}` }
|
|
13
15
|
#load(identity: string): Map<string, StoredDraft> {
|
|
@@ -18,6 +20,7 @@ export class DocumentTimingDrafts {
|
|
|
18
20
|
let raw: string | null = null
|
|
19
21
|
try {
|
|
20
22
|
raw = sessionStorage.getItem(this.#storage(identity))
|
|
23
|
+
this.#stored.set(identity, raw)
|
|
21
24
|
const entries: unknown = JSON.parse(raw ?? '[]')
|
|
22
25
|
if (!Array.isArray(entries) || entries.length > 512) throw new Error('document-timing-draft-data')
|
|
23
26
|
for (const entry of entries) {
|
|
@@ -27,12 +30,12 @@ export class DocumentTimingDrafts {
|
|
|
27
30
|
values.set(entry[0], { text: value.text, error: true, review: true, baseValue: value.baseValue, baseSource: value.baseSource })
|
|
28
31
|
}
|
|
29
32
|
} catch {
|
|
30
|
-
this.failed = true
|
|
33
|
+
this.failed = true; this.#unreadable.add(identity)
|
|
31
34
|
if (raw !== null) try { sessionStorage.setItem(`${this.#storage(identity)}:unreadable`, raw) } catch { /* Keep the original slot untouched when storage is unavailable. */ }
|
|
32
35
|
}
|
|
33
36
|
if (!this.failed) for (const key of this.#documents.keys()) {
|
|
34
37
|
if (this.#documents.size <= 8) break
|
|
35
|
-
if (key !== identity) this.#documents.delete(key)
|
|
38
|
+
if (key !== identity) { this.#documents.delete(key); this.#stored.delete(key) }
|
|
36
39
|
}
|
|
37
40
|
return values
|
|
38
41
|
}
|
|
@@ -45,6 +48,22 @@ export class DocumentTimingDrafts {
|
|
|
45
48
|
has(identity: string, document: TeachingDocument): boolean {
|
|
46
49
|
return [...this.#load(identity).keys()].some(target => Object.hasOwn(document.steps, JSON.parse(target)[0]))
|
|
47
50
|
}
|
|
51
|
+
partial(identity: string): boolean { this.#load(identity); try { return this.#unreadable.has(identity) || sessionStorage.getItem(this.#storage(identity)) !== this.#stored.get(identity) } catch { return true } }
|
|
52
|
+
entries(identity: string): readonly (readonly [string, StoredDraft])[] { return [...this.#load(identity)] }
|
|
53
|
+
/** Retire only after the original session slot accepted the removal. */
|
|
54
|
+
discard(identity: string, key: string): boolean {
|
|
55
|
+
const values = this.#load(identity)
|
|
56
|
+
if (this.#unreadable.has(identity) || !values.has(key)) return false
|
|
57
|
+
try {
|
|
58
|
+
if (sessionStorage.getItem(this.#storage(identity)) !== this.#stored.get(identity)) { this.failed = true; return false }
|
|
59
|
+
const next = [...values].filter(([target]) => target !== key)
|
|
60
|
+
if (next.length) sessionStorage.setItem(this.#storage(identity), JSON.stringify(next))
|
|
61
|
+
else sessionStorage.removeItem(this.#storage(identity))
|
|
62
|
+
this.#stored.set(identity, next.length ? JSON.stringify(next) : null)
|
|
63
|
+
values.delete(key); this.failed = false
|
|
64
|
+
return true
|
|
65
|
+
} catch { this.failed = true; return false }
|
|
66
|
+
}
|
|
48
67
|
set(identity: string, document: TeachingDocument, stepId: string, key: StepDurationKey, draft: StepDurationDraft | undefined): boolean {
|
|
49
68
|
const values = this.#load(identity), target = JSON.stringify([stepId, key]), previous = values.get(target), actual = stepTimingValue(document, stepId, key)
|
|
50
69
|
if (draft && draft.text.length > 256 || draft && !previous && values.size >= 512) { this.failed = true; return true }
|
|
@@ -54,6 +73,7 @@ export class DocumentTimingDrafts {
|
|
|
54
73
|
try {
|
|
55
74
|
if (values.size) sessionStorage.setItem(this.#storage(identity), JSON.stringify([...values]))
|
|
56
75
|
else sessionStorage.removeItem(this.#storage(identity))
|
|
76
|
+
this.#stored.set(identity, values.size ? JSON.stringify([...values]) : null)
|
|
57
77
|
this.failed = false
|
|
58
78
|
} catch { this.failed = true }
|
|
59
79
|
return true
|
package/src/history.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { errorMessage } from '@lincyaw/kernel'
|
|
1
|
+
import { errorMessage, randomUUID } from '@lincyaw/kernel'
|
|
2
2
|
import type { RevertResult, ReviewChange, ReviewSnapshot, ReviewUpdate } from '@lincyaw/kernel'
|
|
3
|
-
import type { WorkbenchStore } from './workbench.ts'
|
|
3
|
+
import type { WorkbenchStore } from './workbench-state.ts'
|
|
4
4
|
import { WorkspaceServerError } from './workspace-client.ts'
|
|
5
5
|
import type { WorkspaceClient } from './workspace-client.ts'
|
|
6
6
|
|
|
@@ -42,13 +42,13 @@ export class DocumentHistory {
|
|
|
42
42
|
this.#review = undefined
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
async read(dir: string): Promise<void> {
|
|
45
|
+
async read(dir: string, signal?: AbortSignal): Promise<void> {
|
|
46
46
|
this.#offsets = [0]
|
|
47
47
|
this.#page = 0
|
|
48
48
|
const read = ++this.#reads, scope = this.#scope
|
|
49
49
|
const current = () => this.#store.state.document === dir && read === this.#reads && scope === this.#scope
|
|
50
50
|
try {
|
|
51
|
-
const [history, { files }] = await Promise.all([this.#client.readRevisions(dir), this.#client.readChanges(dir)])
|
|
51
|
+
const [history, { files }] = await Promise.all([this.#client.readRevisions(dir, undefined, signal), this.#client.readChanges(dir, signal)])
|
|
52
52
|
if (current()) {
|
|
53
53
|
const historyFailure = this.#store.state.historyFailure
|
|
54
54
|
this.#store.set({ history, outside: files, ...this.#readFailure === historyFailure ? { historyFailure: undefined } : {} })
|
|
@@ -129,7 +129,7 @@ export class DocumentHistory {
|
|
|
129
129
|
#change(which: 'undo' | 'redo' | 'revertRevision', expect: string | undefined): Promise<boolean> {
|
|
130
130
|
const binding = this.#store.state.history?.historyBinding
|
|
131
131
|
if (!binding || !expect) return Promise.resolve(false)
|
|
132
|
-
const request = { id:
|
|
132
|
+
const request = { id: randomUUID(), ...binding, expect }
|
|
133
133
|
return this.#run(dir => this.#client[which](dir, request))
|
|
134
134
|
}
|
|
135
135
|
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { DocumentEditing } from './document-editing.ts'
|
|
2
|
+
export { DocumentProduction } from './document-production.ts'
|
|
1
3
|
export * from './components.tsx'
|
|
2
4
|
export { PmSchemaForm, schemaFormPropertyNames } from './schema-form/PmSchemaForm.tsx'
|
|
3
5
|
export type { PmSchemaFormProps } from './schema-form/PmSchemaForm.tsx'
|
|
@@ -14,8 +16,9 @@ export { findPlaceAtLine, findPlaceSource } from './places.ts'
|
|
|
14
16
|
export type { PlaceWithSource } from './places.ts'
|
|
15
17
|
export { connectPreview } from './preview.ts'
|
|
16
18
|
export type { PreviewConnection, PreviewHandlers } from './preview.ts'
|
|
17
|
-
export { documentDirFor, hasSlides,
|
|
18
|
-
export
|
|
19
|
+
export { documentDirFor, hasSlides, Workbench } from './workbench.ts'
|
|
20
|
+
export { isAgentChanging, selectedDocument } from './workbench-state.ts'
|
|
21
|
+
export type { AgentAnswer, AgentRequest, Pointer, PreviewSink, SaveState, WebView, WorkbenchMode, WorkbenchState } from './workbench-state.ts'
|
|
19
22
|
export { OperationLog } from './telemetry.ts'
|
|
20
23
|
export type { OperationBatch, OperationValue } from './telemetry.ts'
|
|
21
24
|
export { isObservedWorkspaceFailure, workspaceClient, WorkspaceServerError } from './workspace-client.ts'
|
|
@@ -26,11 +29,9 @@ export { HistoryPanel } from './panels/HistoryPanel.tsx'
|
|
|
26
29
|
export type { HistoryPanelProps, HistoryRevisionAction } from './panels/HistoryPanel.tsx'
|
|
27
30
|
export { HomePanel } from './panels/HomePanel.tsx'
|
|
28
31
|
export { WorkbenchApprovals } from './approvals.tsx'
|
|
29
|
-
export { assistantMascotAction, useAssistantMascot } from './assistant-mascot.ts'
|
|
30
|
-
export type { AssistantActivity } from './assistant-mascot.ts'
|
|
31
|
-
export { mascotEnabled, setMascotEnabled, useMascotEnabled } from './mascot-preference.ts'
|
|
32
32
|
export { PreviewPanel } from './panels/PreviewPanel.tsx'
|
|
33
33
|
export { SpeechSettingsPanel } from './panels/SpeechSettingsPanel.tsx'
|
|
34
34
|
export { SPEECH_SETTINGS_MESSAGES } from './speech-settings-messages.ts'
|
|
35
35
|
export { connectAuthoringClient } from './authoring-client.ts'
|
|
36
|
+
export { connectAuthoringWindow } from './authoring-window.ts'
|
|
36
37
|
export { SHORTCUTS } from './shortcuts.ts'
|
package/src/jobs.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { errorMessage } from '@lincyaw/kernel'
|
|
2
2
|
import type { CompileJob, JobSource } from '@lincyaw/kernel'
|
|
3
|
-
import type { WorkbenchStore } from './workbench.ts'
|
|
3
|
+
import type { WorkbenchStore } from './workbench-state.ts'
|
|
4
4
|
import type { WorkspaceClient } from './workspace-client.ts'
|
|
5
5
|
|
|
6
6
|
/** Of two copies of a job (an event and an answer cross on the way), the one of the later change. */
|