@arbidocs/blocks 0.5.133 → 0.5.135
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/dist/{ConnectedNativeDocxComparison-H72RQJKH.cjs → ConnectedNativeDocxComparison-W4KIGO54.cjs} +8 -8
- package/dist/{ConnectedNativeDocxComparison-H72RQJKH.cjs.map → ConnectedNativeDocxComparison-W4KIGO54.cjs.map} +1 -1
- package/dist/{ConnectedStreamedWordArtifact-T7PZIRBY.cjs → ConnectedStreamedWordArtifact-764NSXPQ.cjs} +8 -8
- package/dist/{ConnectedStreamedWordArtifact-T7PZIRBY.cjs.map → ConnectedStreamedWordArtifact-764NSXPQ.cjs.map} +1 -1
- package/dist/{FilesViewerDialog-OA3HDLOI.cjs → FilesViewerDialog-IFF2JR2A.cjs} +7 -7
- package/dist/{FilesViewerDialog-OA3HDLOI.cjs.map → FilesViewerDialog-IFF2JR2A.cjs.map} +1 -1
- package/dist/{NativeDocxEditor-XZAEU5QU.cjs → NativeDocxEditor-KM6U4ZLA.cjs} +6 -6
- package/dist/{NativeDocxEditor-XZAEU5QU.cjs.map → NativeDocxEditor-KM6U4ZLA.cjs.map} +1 -1
- package/dist/{chunk-T2DUNNYY.cjs → chunk-C6DF6WB2.cjs} +745 -21
- package/dist/chunk-C6DF6WB2.cjs.map +1 -0
- package/dist/{chunk-64247UHS.cjs → chunk-DBGBAD4J.cjs} +4 -4
- package/dist/{chunk-64247UHS.cjs.map → chunk-DBGBAD4J.cjs.map} +1 -1
- package/dist/{chunk-UOZ237OW.cjs → chunk-FABKMTAI.cjs} +42 -5
- package/dist/chunk-FABKMTAI.cjs.map +1 -0
- package/dist/{chunk-L4Y6UMRC.cjs → chunk-FDRZJFNV.cjs} +5 -5
- package/dist/{chunk-L4Y6UMRC.cjs.map → chunk-FDRZJFNV.cjs.map} +1 -1
- package/dist/{chunk-5JK3232B.cjs → chunk-IEAZX7L6.cjs} +4 -4
- package/dist/{chunk-5JK3232B.cjs.map → chunk-IEAZX7L6.cjs.map} +1 -1
- package/dist/{chunk-2SWS7ESM.cjs → chunk-L62EU3TQ.cjs} +3 -3
- package/dist/{chunk-2SWS7ESM.cjs.map → chunk-L62EU3TQ.cjs.map} +1 -1
- package/dist/{chunk-IN3KXI3K.cjs → chunk-LG36LSVZ.cjs} +6 -6
- package/dist/{chunk-IN3KXI3K.cjs.map → chunk-LG36LSVZ.cjs.map} +1 -1
- package/dist/{chunk-VCEX7COE.cjs → chunk-LIDXIX36.cjs} +10 -5
- package/dist/chunk-LIDXIX36.cjs.map +1 -0
- package/dist/{chunk-RZSBHRQL.cjs → chunk-MMKMQZ2B.cjs} +5 -5
- package/dist/{chunk-RZSBHRQL.cjs.map → chunk-MMKMQZ2B.cjs.map} +1 -1
- package/dist/chunk-MMMGOZYH.cjs +18 -0
- package/dist/{chunk-KWD5Q4XH.cjs.map → chunk-MMMGOZYH.cjs.map} +1 -1
- package/dist/{chunk-V6SFJ44C.cjs → chunk-ZN2JXV65.cjs} +50 -13
- package/dist/chunk-ZN2JXV65.cjs.map +1 -0
- package/dist/connect/docx.cjs +12 -12
- package/dist/connect/index.cjs +108 -94
- package/dist/connect/index.cjs.map +1 -1
- package/dist/connect/viewer.cjs +5 -5
- package/dist/connection/auth/ConnectedSsoCallback.js +21 -5
- package/dist/connection/auth/ConnectedSsoCallback.js.map +2 -2
- package/dist/connection/docx/ConnectedNativeDocxEditor.js +39 -2
- package/dist/connection/docx/ConnectedNativeDocxEditor.js.map +2 -2
- package/dist/connection/runtime/ConnectedAppRuntime.js.map +1 -1
- package/dist/connection/runtime/useWelcomeDialog.d.ts +0 -2
- package/dist/connection/runtime/useWelcomeDialog.js +11 -20
- package/dist/connection/runtime/useWelcomeDialog.js.map +2 -2
- package/dist/connection/viewer/ConnectedDocumentViewer.js +43 -6
- package/dist/connection/viewer/ConnectedDocumentViewer.js.map +2 -2
- package/dist/docx/NativeDocxEditor.d.ts +12 -1
- package/dist/docx/NativeDocxEditor.js +513 -20
- package/dist/docx/NativeDocxEditor.js.map +3 -3
- package/dist/docx/index.cjs +38 -38
- package/dist/docx/recoveryStore.d.ts +8 -0
- package/dist/docx/recoveryStore.js +208 -0
- package/dist/docx/recoveryStore.js.map +7 -0
- package/dist/docx/temporaryRecovery.d.ts +8 -0
- package/dist/docx/temporaryRecovery.js +37 -0
- package/dist/docx/temporaryRecovery.js.map +7 -0
- package/dist/index.cjs +64 -64
- package/dist/shell/AccountBanners.d.ts +2 -7
- package/dist/shell/AccountBanners.js +9 -3
- package/dist/shell/AccountBanners.js.map +2 -2
- package/dist/shell/index.cjs +14 -14
- package/dist/styles.css +1 -1
- package/dist/workflow-canvas/index.cjs +9 -9
- package/node_modules/@arbidocs/coedit/package.json +1 -1
- package/package.json +5 -5
- package/src/connection/billing/project-purchase.css +2 -0
- package/src/docx/styles.css +37 -7
- package/dist/chunk-KWD5Q4XH.cjs +0 -18
- package/dist/chunk-T2DUNNYY.cjs.map +0 -1
- package/dist/chunk-UOZ237OW.cjs.map +0 -1
- package/dist/chunk-V6SFJ44C.cjs.map +0 -1
- package/dist/chunk-VCEX7COE.cjs.map +0 -1
|
@@ -8,13 +8,20 @@ import {
|
|
|
8
8
|
DocxCommentActivity
|
|
9
9
|
} from "./DocxMentions.js";
|
|
10
10
|
import * as Y from "yjs";
|
|
11
|
+
import { materializeBytes } from "@arbidocs/coedit/encoding";
|
|
12
|
+
import { createRecoveryStore } from "./recoveryStore.js";
|
|
13
|
+
import { createTemporaryRecoveryStore } from "./temporaryRecovery.js";
|
|
11
14
|
import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
12
15
|
import {
|
|
13
16
|
DocxEditor,
|
|
14
17
|
useChromeTranslate,
|
|
15
18
|
useDocxEditor
|
|
16
19
|
} from "@docx-editor.dev/react";
|
|
17
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
collaborationModule,
|
|
22
|
+
DOCUMENT_COLLABORATION_VERSIONS,
|
|
23
|
+
formatRefusal
|
|
24
|
+
} from "@arbidocs/coedit";
|
|
18
25
|
import { awaitMatchingEditor } from "./coedit-update.js";
|
|
19
26
|
import {
|
|
20
27
|
createHocuspocusCollaboration
|
|
@@ -69,6 +76,20 @@ import { HouseTemplatePanel } from "./template/HouseTemplatePanel.js";
|
|
|
69
76
|
import { DocxAuthorContext } from "./structural/useTrackedOrNoted.js";
|
|
70
77
|
import { contentPath } from "./content-path.js";
|
|
71
78
|
const drafts = /* @__PURE__ */ new Map();
|
|
79
|
+
const pendingRecoveryWrites = /* @__PURE__ */ new Map();
|
|
80
|
+
const failedRecoveryWrites = /* @__PURE__ */ new Set();
|
|
81
|
+
const unretainedRecoveryFailures = /* @__PURE__ */ new Set();
|
|
82
|
+
const MEMORY_RECOVERY_LIMIT = 32 * 1024 * 1024;
|
|
83
|
+
const memoryRecovery = createTemporaryRecoveryStore(MEMORY_RECOVERY_LIMIT);
|
|
84
|
+
const warnMemoryRecovery = (event) => event.preventDefault();
|
|
85
|
+
const recoveryKey = (workspace, doc, key) => `${workspace}\0${doc}\0${Array.from(key, (b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
86
|
+
const memoryKey = (scope, workspace, doc, key) => scope ? `scope\0${scope}\0${doc}` : recoveryKey(workspace, doc, key);
|
|
87
|
+
function retainMemoryRecovery(key, bytes) {
|
|
88
|
+
const empty = memoryRecovery.size === 0;
|
|
89
|
+
if (!memoryRecovery.add(key, bytes)) return false;
|
|
90
|
+
if (empty) window.addEventListener("beforeunload", warnMemoryRecovery);
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
72
93
|
const stateBytes = (room) => Y.encodeStateAsUpdate(room.ydoc);
|
|
73
94
|
const encodeState = (room) => {
|
|
74
95
|
let binary = "";
|
|
@@ -264,7 +285,8 @@ function NativeDocxEditor({
|
|
|
264
285
|
adapter,
|
|
265
286
|
compact = false,
|
|
266
287
|
stateRef,
|
|
267
|
-
saveRef
|
|
288
|
+
saveRef,
|
|
289
|
+
leaveRef
|
|
268
290
|
}) {
|
|
269
291
|
const [ready, setReady] = useState(), [error, setError] = useState(""), [retry, setRetry] = useState(0);
|
|
270
292
|
const [status, setStatus] = useState("Connecting\u2026"), [dirty, setDirty] = useState(false), [saving, setSaving] = useState(false), [offline, setOffline] = useState(false);
|
|
@@ -275,6 +297,30 @@ function NativeDocxEditor({
|
|
|
275
297
|
const fitting = useRef(true), fitted = useRef(void 0);
|
|
276
298
|
const [fitWidth, setFitWidth] = useState(true), [zoomPercent, setZoomPercent] = useState(100);
|
|
277
299
|
const ref = useRef(null), dirtyRef = useRef(false), draftTimer = useRef(void 0);
|
|
300
|
+
const acknowledgedStateRef = useRef(void 0);
|
|
301
|
+
const [recoveryCopies, setRecoveryCopies] = useState([]);
|
|
302
|
+
const [memoryCopies, setMemoryCopies] = useState([]);
|
|
303
|
+
const [recoveryWarning, setRecoveryWarning] = useState("");
|
|
304
|
+
const recoveryRef = useRef(null);
|
|
305
|
+
const unmountRecoveryRef = useRef(void 0);
|
|
306
|
+
const checkpointedStateRef = useRef(void 0);
|
|
307
|
+
const proactiveTimerRef = useRef(void 0);
|
|
308
|
+
const proactiveCopyRef = useRef(void 0);
|
|
309
|
+
const proactiveGenerationRef = useRef(0);
|
|
310
|
+
const currentRecovery = async () => {
|
|
311
|
+
if (adapter.recoveryIsCurrent && !adapter.recoveryIsCurrent())
|
|
312
|
+
throw new Error("The signed-in recovery identity changed.");
|
|
313
|
+
const saved = recoveryRef.current, identity = await adapter.recoveryIdentity?.();
|
|
314
|
+
if (!identity) throw new Error("Recovery identity is unavailable.");
|
|
315
|
+
if (!saved) {
|
|
316
|
+
identity.keypair.secretKey.fill(0);
|
|
317
|
+
throw new Error("Recovery identity is unavailable.");
|
|
318
|
+
}
|
|
319
|
+
const same = identity.workspaceId === saved.workspaceId && identity.keypair.publicKey.length === saved.publicKey.length && identity.keypair.publicKey.every((byte, index) => byte === saved.publicKey[index]);
|
|
320
|
+
identity.keypair.secretKey.fill(0);
|
|
321
|
+
if (!same) throw new Error("The signed-in recovery identity changed.");
|
|
322
|
+
return saved;
|
|
323
|
+
};
|
|
278
324
|
const isDraft = draftContent !== void 0;
|
|
279
325
|
const streamRef = useRef(stream);
|
|
280
326
|
streamRef.current = stream;
|
|
@@ -313,6 +359,7 @@ function NativeDocxEditor({
|
|
|
313
359
|
useEffect(() => () => clearTimeout(streamTimer.current), []);
|
|
314
360
|
useEffect(() => {
|
|
315
361
|
let cancelled = false;
|
|
362
|
+
let recoveryInvalidated = false;
|
|
316
363
|
let room;
|
|
317
364
|
const cleanups = [];
|
|
318
365
|
setReady(void 0);
|
|
@@ -320,6 +367,92 @@ function NativeDocxEditor({
|
|
|
320
367
|
setStatus("Connecting\u2026");
|
|
321
368
|
setDirty(false);
|
|
322
369
|
dirtyRef.current = false;
|
|
370
|
+
setRecoveryCopies([]);
|
|
371
|
+
setMemoryCopies([]);
|
|
372
|
+
setRecoveryWarning("");
|
|
373
|
+
acknowledgedStateRef.current = void 0;
|
|
374
|
+
checkpointedStateRef.current = void 0;
|
|
375
|
+
proactiveCopyRef.current = void 0;
|
|
376
|
+
proactiveGenerationRef.current += 1;
|
|
377
|
+
if (adapter.onRecoveryInvalidated)
|
|
378
|
+
cleanups.push(
|
|
379
|
+
adapter.onRecoveryInvalidated(() => {
|
|
380
|
+
if (recoveryInvalidated) return;
|
|
381
|
+
recoveryInvalidated = true;
|
|
382
|
+
const recovery = recoveryRef.current;
|
|
383
|
+
if (recovery) {
|
|
384
|
+
try {
|
|
385
|
+
if (!room || !flushInputRef.current) throw new Error("Editor input is unavailable");
|
|
386
|
+
flushInputRef.current();
|
|
387
|
+
runBeforeSave(ref.current);
|
|
388
|
+
room.session.flushPendingJournals();
|
|
389
|
+
const bytes = stateBytes(room), confirmed = acknowledgedStateRef.current, checkpointed = checkpointedStateRef.current;
|
|
390
|
+
const same = (other) => !!other && other.length === bytes.length && other.every((byte, index) => byte === bytes[index]);
|
|
391
|
+
if (!same(confirmed) && !same(checkpointed)) {
|
|
392
|
+
const key = memoryKey(
|
|
393
|
+
adapter.recoveryScope,
|
|
394
|
+
recovery.workspaceId,
|
|
395
|
+
documentId,
|
|
396
|
+
recovery.publicKey
|
|
397
|
+
);
|
|
398
|
+
failedRecoveryWrites.add(
|
|
399
|
+
recoveryKey(recovery.workspaceId, documentId, recovery.publicKey)
|
|
400
|
+
);
|
|
401
|
+
if (retainMemoryRecovery(key, bytes)) {
|
|
402
|
+
checkpointedStateRef.current = bytes;
|
|
403
|
+
adapter.notify?.(
|
|
404
|
+
"A recovery copy is held only in this tab for the previous account or workspace.",
|
|
405
|
+
"error"
|
|
406
|
+
);
|
|
407
|
+
} else {
|
|
408
|
+
unretainedRecoveryFailures.add(
|
|
409
|
+
recoveryKey(recovery.workspaceId, documentId, recovery.publicKey)
|
|
410
|
+
);
|
|
411
|
+
adapter.notify?.(
|
|
412
|
+
"The temporary recovery buffer is full. Local changes could not be retained.",
|
|
413
|
+
"error"
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
} catch {
|
|
418
|
+
adapter.notify?.(
|
|
419
|
+
"Local changes could not be prepared for account or workspace switching.",
|
|
420
|
+
"error"
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
recoveryRef.current = null;
|
|
424
|
+
void recovery.store.close();
|
|
425
|
+
} else if (room && flushInputRef.current && adapter.recoveryScope) {
|
|
426
|
+
try {
|
|
427
|
+
flushInputRef.current();
|
|
428
|
+
runBeforeSave(ref.current);
|
|
429
|
+
room.session.flushPendingJournals();
|
|
430
|
+
const bytes = stateBytes(room), confirmed = acknowledgedStateRef.current;
|
|
431
|
+
if (!confirmed || confirmed.length !== bytes.length || confirmed.some((byte, index) => byte !== bytes[index])) {
|
|
432
|
+
const key = memoryKey(adapter.recoveryScope, "", documentId, new Uint8Array());
|
|
433
|
+
if (retainMemoryRecovery(key, bytes)) {
|
|
434
|
+
checkpointedStateRef.current = bytes;
|
|
435
|
+
adapter.notify?.(
|
|
436
|
+
"A recovery copy is held only in this tab for the previous account or workspace.",
|
|
437
|
+
"error"
|
|
438
|
+
);
|
|
439
|
+
} else
|
|
440
|
+
adapter.notify?.(
|
|
441
|
+
"The temporary recovery buffer is full. Local changes could not be retained.",
|
|
442
|
+
"error"
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
} catch {
|
|
446
|
+
adapter.notify?.(
|
|
447
|
+
"Local changes could not be prepared for account or workspace switching.",
|
|
448
|
+
"error"
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
setRecoveryCopies([]);
|
|
453
|
+
setMemoryCopies([]);
|
|
454
|
+
})
|
|
455
|
+
);
|
|
323
456
|
const updating = (reason) => {
|
|
324
457
|
const cancel = awaitMatchingEditor(reason, () => setRetry((x) => x + 1));
|
|
325
458
|
if (!cancel) return false;
|
|
@@ -379,11 +512,58 @@ function NativeDocxEditor({
|
|
|
379
512
|
return;
|
|
380
513
|
}
|
|
381
514
|
const current = room;
|
|
515
|
+
if (adapter.recoveryIdentity) {
|
|
516
|
+
void adapter.recoveryIdentity().then(async (identity) => {
|
|
517
|
+
if (!identity) return;
|
|
518
|
+
const publicKey = identity.keypair.publicKey.slice(), store = createRecoveryStore(identity.keypair);
|
|
519
|
+
identity.keypair.secretKey.fill(0);
|
|
520
|
+
if (cancelled || recoveryInvalidated || adapter.recoveryIsCurrent && !adapter.recoveryIsCurrent()) {
|
|
521
|
+
await store.close();
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
recoveryRef.current = { store, workspaceId: identity.workspaceId, publicKey };
|
|
525
|
+
try {
|
|
526
|
+
const key = recoveryKey(identity.workspaceId, documentId, publicKey);
|
|
527
|
+
while (pendingRecoveryWrites.get(key)?.size)
|
|
528
|
+
await Promise.all([...pendingRecoveryWrites.get(key)]);
|
|
529
|
+
if (!cancelled)
|
|
530
|
+
setMemoryCopies(
|
|
531
|
+
memoryRecovery.list(
|
|
532
|
+
memoryKey(adapter.recoveryScope, identity.workspaceId, documentId, publicKey)
|
|
533
|
+
)
|
|
534
|
+
);
|
|
535
|
+
if (!cancelled && failedRecoveryWrites.has(key))
|
|
536
|
+
setError(
|
|
537
|
+
"A local recovery copy could not be saved. The prior editor was closed before storage succeeded."
|
|
538
|
+
);
|
|
539
|
+
const copies = await store.list(identity.workspaceId, documentId);
|
|
540
|
+
if (!cancelled) setRecoveryCopies(copies);
|
|
541
|
+
} catch {
|
|
542
|
+
if (!cancelled)
|
|
543
|
+
setRecoveryWarning(
|
|
544
|
+
"Local recovery storage is unavailable. Download your DOCX before leaving if it has unsaved changes."
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
}).catch(() => {
|
|
548
|
+
if (!cancelled)
|
|
549
|
+
setRecoveryWarning(
|
|
550
|
+
"Local recovery storage is unavailable. Download your DOCX before leaving if it has unsaved changes."
|
|
551
|
+
);
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
acknowledgedStateRef.current = stateBytes(current);
|
|
382
555
|
cleanups.push(
|
|
383
556
|
current.session.subscribeStatus((s, _reason, detail) => {
|
|
384
|
-
const disconnected = s === "disconnected";
|
|
557
|
+
const disconnected = s === "disconnected" || s === "error";
|
|
385
558
|
setOffline(disconnected);
|
|
386
559
|
if (s === "error") {
|
|
560
|
+
if (formatRefusal(detail)) {
|
|
561
|
+
setError(
|
|
562
|
+
"The editor and collaboration server need matching versions. Download your DOCX before reloading to preserve any local edits."
|
|
563
|
+
);
|
|
564
|
+
setStatus("Update required");
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
387
567
|
if (updating(detail)) return;
|
|
388
568
|
setError(
|
|
389
569
|
detail || "The collaboration session failed. Export your local document before reconnecting."
|
|
@@ -401,6 +581,7 @@ function NativeDocxEditor({
|
|
|
401
581
|
const message = JSON.parse(payload);
|
|
402
582
|
if (message.type === "persisted") {
|
|
403
583
|
if (!cancelled && message.vector && message.receipt && covered(current, message.vector, message.receipt)) {
|
|
584
|
+
acknowledgedStateRef.current = stateBytes(current);
|
|
404
585
|
dirtyRef.current = false;
|
|
405
586
|
setDirty(false);
|
|
406
587
|
setStatus("All changes saved");
|
|
@@ -445,10 +626,115 @@ function NativeDocxEditor({
|
|
|
445
626
|
cancelled = true;
|
|
446
627
|
cleanups.forEach((f) => f());
|
|
447
628
|
clearTimeout(draftTimer.current);
|
|
629
|
+
clearTimeout(proactiveTimerRef.current);
|
|
448
630
|
fitCleanup.current?.();
|
|
631
|
+
const recovery = recoveryRef.current;
|
|
632
|
+
recoveryRef.current = null;
|
|
633
|
+
if (recovery) {
|
|
634
|
+
const pending = unmountRecoveryRef.current;
|
|
635
|
+
unmountRecoveryRef.current = void 0;
|
|
636
|
+
if (pending)
|
|
637
|
+
void pending.then(
|
|
638
|
+
() => recovery.store.close(),
|
|
639
|
+
() => recovery.store.close()
|
|
640
|
+
);
|
|
641
|
+
else void recovery.store.close();
|
|
642
|
+
}
|
|
449
643
|
room?.destroy();
|
|
450
644
|
};
|
|
451
|
-
}, [documentId, isDraft, snapshot, retry]);
|
|
645
|
+
}, [documentId, isDraft, snapshot, retry, adapter.recoveryScope]);
|
|
646
|
+
useLayoutEffect(() => {
|
|
647
|
+
const room = ready?.room;
|
|
648
|
+
return () => {
|
|
649
|
+
const recovery = recoveryRef.current;
|
|
650
|
+
if (!room) return;
|
|
651
|
+
try {
|
|
652
|
+
if (!flushInputRef.current) throw new Error("Editor input is unavailable");
|
|
653
|
+
flushInputRef.current();
|
|
654
|
+
runBeforeSave(ref.current);
|
|
655
|
+
room.session.flushPendingJournals();
|
|
656
|
+
const bytes = stateBytes(room), confirmed = acknowledgedStateRef.current;
|
|
657
|
+
if (confirmed && confirmed.length === bytes.length && confirmed.every((byte, index) => byte === bytes[index]))
|
|
658
|
+
return;
|
|
659
|
+
const checkpointed = checkpointedStateRef.current;
|
|
660
|
+
if (checkpointed && checkpointed.length === bytes.length && checkpointed.every((byte, index) => byte === bytes[index]))
|
|
661
|
+
return;
|
|
662
|
+
if (!recovery) {
|
|
663
|
+
if (!adapter.recoveryScope) throw new Error("Recovery identity is unavailable");
|
|
664
|
+
const retained = retainMemoryRecovery(
|
|
665
|
+
memoryKey(adapter.recoveryScope, "", documentId, new Uint8Array()),
|
|
666
|
+
bytes
|
|
667
|
+
);
|
|
668
|
+
adapter.notify?.(
|
|
669
|
+
retained ? "A recovery copy is held only in this tab. Reopen the document and download it." : "The temporary recovery buffer is full. Local changes could not be retained.",
|
|
670
|
+
"error"
|
|
671
|
+
);
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
const key = recoveryKey(recovery.workspaceId, documentId, recovery.publicKey);
|
|
675
|
+
if (adapter.recoveryIsCurrent && !adapter.recoveryIsCurrent()) {
|
|
676
|
+
failedRecoveryWrites.add(key);
|
|
677
|
+
adapter.notify?.(
|
|
678
|
+
retainMemoryRecovery(
|
|
679
|
+
memoryKey(
|
|
680
|
+
adapter.recoveryScope,
|
|
681
|
+
recovery.workspaceId,
|
|
682
|
+
documentId,
|
|
683
|
+
recovery.publicKey
|
|
684
|
+
),
|
|
685
|
+
bytes
|
|
686
|
+
) ? "A recovery copy is held only in this tab. Reopen the document and download it." : "The temporary recovery buffer is full. Local changes could not be retained.",
|
|
687
|
+
"error"
|
|
688
|
+
);
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
const writes = pendingRecoveryWrites.get(key) ?? /* @__PURE__ */ new Set();
|
|
692
|
+
const pending = recovery.store.checkpoint(recovery.workspaceId, documentId, bytes).then(
|
|
693
|
+
() => {
|
|
694
|
+
},
|
|
695
|
+
() => {
|
|
696
|
+
failedRecoveryWrites.add(key);
|
|
697
|
+
if (retainMemoryRecovery(
|
|
698
|
+
memoryKey(
|
|
699
|
+
adapter.recoveryScope,
|
|
700
|
+
recovery.workspaceId,
|
|
701
|
+
documentId,
|
|
702
|
+
recovery.publicKey
|
|
703
|
+
),
|
|
704
|
+
bytes
|
|
705
|
+
)) {
|
|
706
|
+
adapter.notify?.(
|
|
707
|
+
"A recovery copy is held only in this tab. Reopen the document and download it.",
|
|
708
|
+
"error"
|
|
709
|
+
);
|
|
710
|
+
} else {
|
|
711
|
+
unretainedRecoveryFailures.add(key);
|
|
712
|
+
adapter.notify?.(
|
|
713
|
+
"Recovery storage and the temporary recovery buffer are full. Local changes could not be retained.",
|
|
714
|
+
"error"
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
).finally(() => {
|
|
719
|
+
writes.delete(pending);
|
|
720
|
+
if (!writes.size) pendingRecoveryWrites.delete(key);
|
|
721
|
+
});
|
|
722
|
+
writes.add(pending);
|
|
723
|
+
pendingRecoveryWrites.set(key, writes);
|
|
724
|
+
unmountRecoveryRef.current = pending;
|
|
725
|
+
} catch {
|
|
726
|
+
if (recovery) {
|
|
727
|
+
const key = recoveryKey(recovery.workspaceId, documentId, recovery.publicKey);
|
|
728
|
+
failedRecoveryWrites.add(key);
|
|
729
|
+
unretainedRecoveryFailures.add(key);
|
|
730
|
+
}
|
|
731
|
+
adapter.notify?.(
|
|
732
|
+
"A local recovery copy could not be prepared before the editor closed.",
|
|
733
|
+
"error"
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
}, [ready?.room, documentId, adapter.recoveryScope]);
|
|
452
738
|
useEffect(() => {
|
|
453
739
|
if (!stateRef) return;
|
|
454
740
|
const room = ready?.room;
|
|
@@ -457,6 +743,43 @@ function NativeDocxEditor({
|
|
|
457
743
|
stateRef.current = null;
|
|
458
744
|
};
|
|
459
745
|
}, [stateRef, ready?.room]);
|
|
746
|
+
useEffect(() => {
|
|
747
|
+
if (!leaveRef) return;
|
|
748
|
+
leaveRef.current = async () => {
|
|
749
|
+
const room = ready?.room;
|
|
750
|
+
if (!room) return "unchanged";
|
|
751
|
+
try {
|
|
752
|
+
if (!flushInputRef.current) throw new Error("Editor input is not ready.");
|
|
753
|
+
flushInputRef.current();
|
|
754
|
+
runBeforeSave(ref.current);
|
|
755
|
+
room.session.flushPendingJournals();
|
|
756
|
+
const bytes = stateBytes(room), confirmed = acknowledgedStateRef.current;
|
|
757
|
+
if (confirmed && confirmed.length === bytes.length && confirmed.every((byte, index) => byte === bytes[index]))
|
|
758
|
+
return "unchanged";
|
|
759
|
+
const checkpointed = checkpointedStateRef.current;
|
|
760
|
+
if (checkpointed && checkpointed.length === bytes.length && checkpointed.every((byte, index) => byte === bytes[index]))
|
|
761
|
+
return "unchanged";
|
|
762
|
+
const recovery = await currentRecovery();
|
|
763
|
+
await recovery.store.checkpoint(recovery.workspaceId, documentId, bytes);
|
|
764
|
+
await currentRecovery();
|
|
765
|
+
flushInputRef.current();
|
|
766
|
+
room.session.flushPendingJournals();
|
|
767
|
+
const after = stateBytes(room);
|
|
768
|
+
if (bytes.length !== after.length || bytes.some((byte, index) => byte !== after[index]))
|
|
769
|
+
throw new Error("The document changed while preserving it; try again.");
|
|
770
|
+
checkpointedStateRef.current = bytes;
|
|
771
|
+
return "saved";
|
|
772
|
+
} catch (error2) {
|
|
773
|
+
setError(
|
|
774
|
+
error2 instanceof Error ? `${error2.message} Keep this editor open or download your DOCX.` : "Recovery failed. Keep this editor open or download your DOCX."
|
|
775
|
+
);
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
return () => {
|
|
780
|
+
leaveRef.current = null;
|
|
781
|
+
};
|
|
782
|
+
}, [leaveRef, ready?.room, documentId, adapter]);
|
|
460
783
|
useEffect(() => {
|
|
461
784
|
adapter.onDirtyChange?.(dirty);
|
|
462
785
|
const beforeUnload = (e) => {
|
|
@@ -483,6 +806,89 @@ function NativeDocxEditor({
|
|
|
483
806
|
a.click();
|
|
484
807
|
setTimeout(() => URL.revokeObjectURL(url), 5e3);
|
|
485
808
|
}
|
|
809
|
+
async function downloadRecovery(id) {
|
|
810
|
+
try {
|
|
811
|
+
const recovery = await currentRecovery();
|
|
812
|
+
const state = await recovery.store.load(recovery.workspaceId, documentId, id);
|
|
813
|
+
if (!state) throw new Error("The recovery copy is unavailable.");
|
|
814
|
+
await currentRecovery();
|
|
815
|
+
const doc = new Y.Doc();
|
|
816
|
+
try {
|
|
817
|
+
Y.applyUpdate(doc, state);
|
|
818
|
+
const url = URL.createObjectURL(
|
|
819
|
+
new Blob([materializeBytes(doc)], {
|
|
820
|
+
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
821
|
+
})
|
|
822
|
+
);
|
|
823
|
+
const a = document.createElement("a");
|
|
824
|
+
a.href = url;
|
|
825
|
+
a.download = title.replace(/\.[^.]+$/, "") + "-recovery.docx";
|
|
826
|
+
a.click();
|
|
827
|
+
setTimeout(() => URL.revokeObjectURL(url), 5e3);
|
|
828
|
+
} finally {
|
|
829
|
+
doc.destroy();
|
|
830
|
+
}
|
|
831
|
+
} catch (error2) {
|
|
832
|
+
setError(error2 instanceof Error ? error2.message : "Could not open the recovery copy.");
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
async function downloadMemoryRecovery(id) {
|
|
836
|
+
try {
|
|
837
|
+
const recovery = await currentRecovery(), key = memoryKey(adapter.recoveryScope, recovery.workspaceId, documentId, recovery.publicKey);
|
|
838
|
+
const state = memoryRecovery.get(key, id);
|
|
839
|
+
if (!state) throw new Error("The temporary recovery copy is unavailable.");
|
|
840
|
+
const doc = new Y.Doc();
|
|
841
|
+
try {
|
|
842
|
+
Y.applyUpdate(doc, state);
|
|
843
|
+
await currentRecovery();
|
|
844
|
+
const url = URL.createObjectURL(
|
|
845
|
+
new Blob([materializeBytes(doc)], {
|
|
846
|
+
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
847
|
+
})
|
|
848
|
+
);
|
|
849
|
+
const a = document.createElement("a");
|
|
850
|
+
a.href = url;
|
|
851
|
+
a.download = title.replace(/\.[^.]+$/, "") + "-recovery.docx";
|
|
852
|
+
a.click();
|
|
853
|
+
setTimeout(() => URL.revokeObjectURL(url), 5e3);
|
|
854
|
+
} finally {
|
|
855
|
+
doc.destroy();
|
|
856
|
+
}
|
|
857
|
+
} catch (error2) {
|
|
858
|
+
setError(
|
|
859
|
+
error2 instanceof Error ? error2.message : "Could not open the temporary recovery copy."
|
|
860
|
+
);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
async function removeMemoryRecovery(id) {
|
|
864
|
+
if (!window.confirm("Delete this temporary recovery copy?")) return;
|
|
865
|
+
try {
|
|
866
|
+
const recovery = await currentRecovery(), key = memoryKey(adapter.recoveryScope, recovery.workspaceId, documentId, recovery.publicKey);
|
|
867
|
+
if (!memoryRecovery.remove(key, id))
|
|
868
|
+
throw new Error("The temporary recovery copy is unavailable.");
|
|
869
|
+
const copies = memoryRecovery.list(key);
|
|
870
|
+
const failureKey = recoveryKey(recovery.workspaceId, documentId, recovery.publicKey);
|
|
871
|
+
if (!copies.length && !unretainedRecoveryFailures.has(failureKey))
|
|
872
|
+
failedRecoveryWrites.delete(failureKey);
|
|
873
|
+
if (memoryRecovery.size === 0) window.removeEventListener("beforeunload", warnMemoryRecovery);
|
|
874
|
+
setMemoryCopies(copies);
|
|
875
|
+
} catch (error2) {
|
|
876
|
+
setError(
|
|
877
|
+
error2 instanceof Error ? error2.message : "Could not remove the temporary recovery copy."
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
async function removeRecovery(id) {
|
|
882
|
+
if (!window.confirm("Delete this local recovery copy?")) return;
|
|
883
|
+
try {
|
|
884
|
+
const recovery = await currentRecovery();
|
|
885
|
+
await recovery.store.remove(recovery.workspaceId, documentId, id);
|
|
886
|
+
await currentRecovery();
|
|
887
|
+
setRecoveryCopies((copies) => copies.filter((copy) => copy !== id));
|
|
888
|
+
} catch (error2) {
|
|
889
|
+
setError(error2 instanceof Error ? error2.message : "Could not remove the recovery copy.");
|
|
890
|
+
}
|
|
891
|
+
}
|
|
486
892
|
useLayoutEffect(() => {
|
|
487
893
|
const editor = ref.current;
|
|
488
894
|
return () => {
|
|
@@ -641,6 +1047,32 @@ function NativeDocxEditor({
|
|
|
641
1047
|
setStatus(
|
|
642
1048
|
isDraft ? "Unsaved note" : offline ? "Offline \xB7 edits buffered locally" : "Saving changes\u2026"
|
|
643
1049
|
);
|
|
1050
|
+
if (offline && ready?.room && adapter.recoveryIdentity) {
|
|
1051
|
+
clearTimeout(proactiveTimerRef.current);
|
|
1052
|
+
const room = ready.room;
|
|
1053
|
+
const generation = ++proactiveGenerationRef.current;
|
|
1054
|
+
proactiveTimerRef.current = setTimeout(() => {
|
|
1055
|
+
void (async () => {
|
|
1056
|
+
try {
|
|
1057
|
+
if (!flushInputRef.current) throw new Error("Editor input is unavailable.");
|
|
1058
|
+
flushInputRef.current();
|
|
1059
|
+
room.session.flushPendingJournals();
|
|
1060
|
+
const bytes = stateBytes(room), recovery = await currentRecovery();
|
|
1061
|
+
const id = await recovery.store.checkpoint(recovery.workspaceId, documentId, bytes);
|
|
1062
|
+
await currentRecovery();
|
|
1063
|
+
if (generation !== proactiveGenerationRef.current) return;
|
|
1064
|
+
const previous = proactiveCopyRef.current;
|
|
1065
|
+
proactiveCopyRef.current = id;
|
|
1066
|
+
checkpointedStateRef.current = bytes;
|
|
1067
|
+
if (previous) await recovery.store.remove(recovery.workspaceId, documentId, previous);
|
|
1068
|
+
} catch {
|
|
1069
|
+
setError(
|
|
1070
|
+
"A local recovery copy could not be saved. Keep this editor open or download your DOCX."
|
|
1071
|
+
);
|
|
1072
|
+
}
|
|
1073
|
+
})();
|
|
1074
|
+
}, 750);
|
|
1075
|
+
}
|
|
644
1076
|
if (isDraft) {
|
|
645
1077
|
clearTimeout(draftTimer.current);
|
|
646
1078
|
draftTimer.current = setTimeout(() => {
|
|
@@ -658,22 +1090,83 @@ function NativeDocxEditor({
|
|
|
658
1090
|
"aria-busy": !!stream?.busy,
|
|
659
1091
|
"data-generation-state": stream ? stream.busy ? "generating" : stream.error ? "stopped" : "generated" : "ready",
|
|
660
1092
|
children: [
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
"
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
1093
|
+
/* @__PURE__ */ jsxs("div", { className: "native-docx-notice-stack", children: [
|
|
1094
|
+
recoveryWarning && /* @__PURE__ */ jsx("div", { className: "native-docx-notice", role: "status", children: recoveryWarning }),
|
|
1095
|
+
recoveryCopies.length > 0 && /* @__PURE__ */ jsxs("div", { className: "native-docx-notice", role: "status", children: [
|
|
1096
|
+
"Local recovery ",
|
|
1097
|
+
recoveryCopies.length === 1 ? "copy" : "copies",
|
|
1098
|
+
" available.",
|
|
1099
|
+
" ",
|
|
1100
|
+
recoveryCopies.map((id, index) => /* @__PURE__ */ jsxs("span", { children: [
|
|
1101
|
+
/* @__PURE__ */ jsxs("button", { onClick: () => void downloadRecovery(id), children: [
|
|
1102
|
+
"Download recovery copy ",
|
|
1103
|
+
index + 1
|
|
1104
|
+
] }),
|
|
1105
|
+
" ",
|
|
1106
|
+
/* @__PURE__ */ jsxs("button", { onClick: () => void removeRecovery(id), children: [
|
|
1107
|
+
"Delete recovery copy ",
|
|
1108
|
+
index + 1
|
|
1109
|
+
] }),
|
|
1110
|
+
" "
|
|
1111
|
+
] }, id))
|
|
1112
|
+
] }),
|
|
1113
|
+
memoryCopies.length > 0 && /* @__PURE__ */ jsxs("div", { className: "native-docx-notice", role: "status", children: [
|
|
1114
|
+
"Recovery copies held only in this tab will be lost when it closes.",
|
|
1115
|
+
" ",
|
|
1116
|
+
memoryCopies.map((id, index) => /* @__PURE__ */ jsxs("span", { children: [
|
|
1117
|
+
/* @__PURE__ */ jsxs("button", { onClick: () => void downloadMemoryRecovery(id), children: [
|
|
1118
|
+
"Download temporary recovery copy ",
|
|
1119
|
+
index + 1
|
|
1120
|
+
] }),
|
|
1121
|
+
" ",
|
|
1122
|
+
/* @__PURE__ */ jsxs("button", { onClick: () => void removeMemoryRecovery(id), children: [
|
|
1123
|
+
"Delete temporary recovery copy ",
|
|
1124
|
+
index + 1
|
|
1125
|
+
] }),
|
|
1126
|
+
" "
|
|
1127
|
+
] }, id))
|
|
1128
|
+
] }),
|
|
1129
|
+
offline && /* @__PURE__ */ jsx("div", { className: "native-docx-notice", children: "Changes are buffered in this browser. Keep this tab open until the connection returns, or download your DOCX." }),
|
|
1130
|
+
error && /* @__PURE__ */ jsxs("div", { className: "native-docx-notice", "data-error": "true", role: "alert", children: [
|
|
1131
|
+
error,
|
|
1132
|
+
" ",
|
|
1133
|
+
compact && ready && /* @__PURE__ */ jsx("button", { onClick: () => void exportDocument(), children: "Download DOCX" }),
|
|
1134
|
+
" ",
|
|
1135
|
+
/* @__PURE__ */ jsxs(
|
|
1136
|
+
"button",
|
|
1137
|
+
{
|
|
1138
|
+
disabled: !stream && dirty,
|
|
1139
|
+
title: dirty ? "Download this document before reopening the editor" : void 0,
|
|
1140
|
+
onClick: () => {
|
|
1141
|
+
if (stream) {
|
|
1142
|
+
setError("");
|
|
1143
|
+
setStreamAttempt((x) => x + 1);
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
if (ready?.room) {
|
|
1147
|
+
try {
|
|
1148
|
+
if (!flushInputRef.current) throw new Error("Editor input is not available");
|
|
1149
|
+
flushInputRef.current();
|
|
1150
|
+
ready.room.session.flushPendingJournals();
|
|
1151
|
+
const previous = acknowledgedStateRef.current, current = stateBytes(ready.room);
|
|
1152
|
+
if (!previous || previous.length !== current.length || previous.some((byte, index) => byte !== current[index]))
|
|
1153
|
+
throw new Error("Room has unconfirmed changes");
|
|
1154
|
+
} catch {
|
|
1155
|
+
setError(
|
|
1156
|
+
"This document has changes that are not confirmed saved. Download your DOCX before reloading."
|
|
1157
|
+
);
|
|
1158
|
+
return;
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
setRetry((x) => x + 1);
|
|
1162
|
+
},
|
|
1163
|
+
children: [
|
|
1164
|
+
/* @__PURE__ */ jsx(RefreshCw, { size: 12 }),
|
|
1165
|
+
"Retry"
|
|
1166
|
+
]
|
|
1167
|
+
}
|
|
1168
|
+
)
|
|
1169
|
+
] })
|
|
677
1170
|
] }),
|
|
678
1171
|
!ready && !error && /* @__PURE__ */ jsx("div", { className: "native-docx-error", role: "status", children: "Opening Word document\u2026" }),
|
|
679
1172
|
ready && /* @__PURE__ */ jsx("div", { className: "native-docx-body docx-editor", children: /* @__PURE__ */ jsx(EditorBoundary, { children: /* @__PURE__ */ jsx(
|