fabricjs-document-engine 1.1.0 → 1.2.0
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/README.md +39 -1
- package/README.zh-CN.md +39 -1
- package/dist/assets/asset-pipeline.cjs +10 -8
- package/dist/assets/asset-pipeline.d.cts +1 -1
- package/dist/assets/asset-pipeline.d.ts +1 -1
- package/dist/assets/asset-pipeline.js +10 -8
- package/dist/assets/image-check.cjs +4 -3
- package/dist/assets/image-check.d.cts +1 -1
- package/dist/assets/image-check.d.ts +1 -1
- package/dist/assets/image-check.js +4 -3
- package/dist/commands/clipboard.cjs +5 -0
- package/dist/commands/clipboard.js +5 -0
- package/dist/document/document-format.d.cts +15 -0
- package/dist/document/document-format.d.ts +15 -0
- package/dist/document/validate-document.cjs +8 -0
- package/dist/document/validate-document.js +8 -0
- package/dist/engine/create-document-engine.cjs +115 -21
- package/dist/engine/create-document-engine.d.cts +24 -6
- package/dist/engine/create-document-engine.d.ts +24 -6
- package/dist/engine/create-document-engine.js +116 -22
- package/dist/engine/errors.d.cts +1 -1
- package/dist/engine/errors.d.ts +1 -1
- package/dist/export/export-options.cjs +3 -0
- package/dist/export/export-options.d.cts +6 -0
- package/dist/export/export-options.d.ts +6 -0
- package/dist/export/export-options.js +3 -0
- package/dist/export/preflight-export.d.cts +1 -1
- package/dist/export/preflight-export.d.ts +1 -1
- package/dist/export/render-export.cjs +42 -4
- package/dist/export/render-export.js +43 -5
- package/dist/export/svg/clip-masks.cjs +39 -0
- package/dist/export/svg/clip-masks.js +37 -0
- package/dist/export/svg/embed-assets.cjs +1 -0
- package/dist/export/svg/embed-assets.js +1 -0
- package/dist/export/svg/raster-object.cjs +82 -0
- package/dist/export/svg/raster-object.js +81 -0
- package/dist/{pdf → export/svg}/text-decorations.cjs +3 -3
- package/dist/{pdf → export/svg}/text-decorations.js +3 -3
- package/dist/export/svg/text-on-path.cjs +0 -10
- package/dist/export/svg/text-on-path.js +1 -10
- package/dist/fabric/fabric-adapter.cjs +5 -3
- package/dist/fabric/fabric-adapter.js +5 -3
- package/dist/fabric/page-state.cjs +69 -0
- package/dist/fabric/page-state.js +64 -0
- package/dist/history/apply-state.cjs +19 -2
- package/dist/history/apply-state.js +19 -2
- package/dist/history/create-history.cjs +53 -13
- package/dist/history/create-history.d.cts +8 -0
- package/dist/history/create-history.d.ts +8 -0
- package/dist/history/create-history.js +53 -13
- package/dist/history/snapshot.cjs +10 -1
- package/dist/history/snapshot.js +10 -2
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/migrations/migrate-document.cjs +2 -1
- package/dist/migrations/migrate-document.js +2 -1
- package/dist/pdf/export-pdf.cjs +8 -67
- package/dist/pdf/export-pdf.js +8 -67
- package/dist/recovery/recovery-controller.cjs +148 -29
- package/dist/recovery/recovery-controller.d.cts +8 -0
- package/dist/recovery/recovery-controller.d.ts +8 -0
- package/dist/recovery/recovery-controller.js +148 -29
- package/dist/security/content-limits.cjs +43 -3
- package/dist/security/content-limits.d.cts +8 -0
- package/dist/security/content-limits.d.ts +8 -0
- package/dist/security/content-limits.js +39 -4
- package/dist/util/concurrency.cjs +16 -3
- package/dist/util/concurrency.js +16 -3
- package/package.json +1 -1
- package/schema/document-v1.schema.json +5 -1
package/dist/pdf/export-pdf.js
CHANGED
|
@@ -3,12 +3,13 @@ import { readObjectId } from "../fabric/object-ids.js";
|
|
|
3
3
|
import { walkObjects } from "../fabric/walk-objects.js";
|
|
4
4
|
import { withSvgOverrides } from "../export/svg/overrides.js";
|
|
5
5
|
import { isTextOnPath, textOnPathToSVG } from "../export/svg/text-on-path.js";
|
|
6
|
+
import { rasterMarkup, safeMultiplier } from "../export/svg/raster-object.js";
|
|
7
|
+
import { decoratedTextToSVG, hasStraightDecorations } from "../export/svg/text-decorations.js";
|
|
6
8
|
import { repairFabricSvg, withExportView } from "../export/render-export.js";
|
|
7
9
|
import { embedSvgAssets } from "../export/svg/embed-assets.js";
|
|
8
10
|
import { disposeObjects } from "../export/batch-render.js";
|
|
9
11
|
import { jsPdfStyle, loadFonts, registerFonts, standardFamily } from "./fonts.js";
|
|
10
12
|
import { layoutPage } from "./page-layout.js";
|
|
11
|
-
import { decoratedTextToSVG, hasStraightDecorations } from "./text-decorations.js";
|
|
12
13
|
//#region src/pdf/export-pdf.ts
|
|
13
14
|
const RASTER_REASONS = {
|
|
14
15
|
shadow: "it has a shadow, which PDF vectors cannot draw",
|
|
@@ -16,7 +17,8 @@ const RASTER_REASONS = {
|
|
|
16
17
|
gradientStroke: "its outline is a gradient or pattern",
|
|
17
18
|
uniformStroke: "it keeps its outline width while scaled (strokeUniform)",
|
|
18
19
|
font: "its font has no TrueType file in fonts",
|
|
19
|
-
characters: "it uses characters the built-in PDF fonts do not have; add a TrueType font that has them"
|
|
20
|
+
characters: "it uses characters the built-in PDF fonts do not have; add a TrueType font that has them",
|
|
21
|
+
mask: "it uses an inverted or nested clip path, which PDF vectors cannot draw"
|
|
20
22
|
};
|
|
21
23
|
function stop(signal) {
|
|
22
24
|
if (signal?.aborted) throw new DocumentEngineError("EXPORT_ABORTED", "The PDF export was cancelled");
|
|
@@ -74,6 +76,7 @@ function rasterReason(object, registered, missingFonts) {
|
|
|
74
76
|
walkObjects([object], (child) => {
|
|
75
77
|
if (reason) return;
|
|
76
78
|
if (child.shadow) reason = "shadow";
|
|
79
|
+
else if (child.clipPath?.inverted || child.clipPath?.clipPath) reason = "mask";
|
|
77
80
|
else if (child.globalCompositeOperation && child.globalCompositeOperation !== "source-over") reason = "blend";
|
|
78
81
|
else if (typeof child.stroke === "object" && child.stroke !== null && child.strokeWidth > 0) reason = "gradientStroke";
|
|
79
82
|
else if (child.strokeUniform && child.stroke && child.strokeWidth > 0) {
|
|
@@ -90,69 +93,6 @@ function rasterReason(object, registered, missingFonts) {
|
|
|
90
93
|
});
|
|
91
94
|
return reason;
|
|
92
95
|
}
|
|
93
|
-
function shadowPadding(object) {
|
|
94
|
-
let padding = 0;
|
|
95
|
-
walkObjects([object], (child) => {
|
|
96
|
-
const shadow = child.shadow;
|
|
97
|
-
if (shadow) padding = Math.max(padding, shadow.blur + Math.max(Math.abs(shadow.offsetX), Math.abs(shadow.offsetY)));
|
|
98
|
-
});
|
|
99
|
-
return padding + 2;
|
|
100
|
-
}
|
|
101
|
-
function usesBlendMode(object) {
|
|
102
|
-
let blends = false;
|
|
103
|
-
walkObjects([object], (child) => {
|
|
104
|
-
if (child.globalCompositeOperation && child.globalCompositeOperation !== "source-over") blends = true;
|
|
105
|
-
});
|
|
106
|
-
return blends;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* An object as a picture placed where it sits on the canvas. Usually the
|
|
110
|
-
* object alone, so the vectors underneath show through. An object with a
|
|
111
|
-
* blend mode mixes with what lies below it, so its picture includes
|
|
112
|
-
* everything below it within its bounds, which covers those vectors exactly.
|
|
113
|
-
*/
|
|
114
|
-
function rasterMarkup(canvas, object, multiplier) {
|
|
115
|
-
object.setCoords();
|
|
116
|
-
const bounds = object.getBoundingRect();
|
|
117
|
-
const padding = shadowPadding(object);
|
|
118
|
-
const left = Math.max(0, Math.floor(bounds.left - padding));
|
|
119
|
-
const top = Math.max(0, Math.floor(bounds.top - padding));
|
|
120
|
-
const right = Math.min(canvas.getWidth(), Math.ceil(bounds.left + bounds.width + padding));
|
|
121
|
-
const bottom = Math.min(canvas.getHeight(), Math.ceil(bounds.top + bounds.height + padding));
|
|
122
|
-
if (right <= left || bottom <= top) return "";
|
|
123
|
-
const blends = usesBlendMode(object);
|
|
124
|
-
const stack = canvas.getObjects();
|
|
125
|
-
const position = stack.indexOf(object);
|
|
126
|
-
const saved = {
|
|
127
|
-
backgroundColor: canvas.backgroundColor,
|
|
128
|
-
backgroundImage: canvas.backgroundImage,
|
|
129
|
-
overlayColor: canvas.overlayColor,
|
|
130
|
-
overlayImage: canvas.overlayImage
|
|
131
|
-
};
|
|
132
|
-
if (!blends) {
|
|
133
|
-
canvas.backgroundColor = "";
|
|
134
|
-
canvas.backgroundImage = void 0;
|
|
135
|
-
}
|
|
136
|
-
canvas.overlayColor = "";
|
|
137
|
-
canvas.overlayImage = void 0;
|
|
138
|
-
let element;
|
|
139
|
-
try {
|
|
140
|
-
const filter = (candidate) => blends ? stack.indexOf(candidate) <= position : candidate === object;
|
|
141
|
-
element = canvas.toCanvasElement(multiplier, {
|
|
142
|
-
left,
|
|
143
|
-
top,
|
|
144
|
-
width: right - left,
|
|
145
|
-
height: bottom - top,
|
|
146
|
-
filter
|
|
147
|
-
});
|
|
148
|
-
} finally {
|
|
149
|
-
Object.assign(canvas, saved);
|
|
150
|
-
}
|
|
151
|
-
const url = element.toDataURL("image/png");
|
|
152
|
-
element.width = 0;
|
|
153
|
-
element.height = 0;
|
|
154
|
-
return `<image x="${left}" y="${top}" width="${right - left}" height="${bottom - top}" preserveAspectRatio="none" xlink:href="${url}" />\n`;
|
|
155
|
-
}
|
|
156
96
|
function quoteFamily(name) {
|
|
157
97
|
return /^[a-z-]+$/i.test(name) ? name : `"${name.replace(/"/g, "")}"`;
|
|
158
98
|
}
|
|
@@ -238,7 +178,7 @@ function clipTo(context, draw) {
|
|
|
238
178
|
}
|
|
239
179
|
async function drawRasterPage(canvas, context) {
|
|
240
180
|
const { layout, options } = context;
|
|
241
|
-
const multiplier = layout.scale * (options.dpi ?? 300) / 72;
|
|
181
|
+
const multiplier = safeMultiplier(layout.scale * (options.dpi ?? 300) / 72, canvas.getWidth(), canvas.getHeight(), options.limits);
|
|
242
182
|
const element = withExportView(canvas, options.background ?? "keep", "png", () => canvas.toCanvasElement(multiplier));
|
|
243
183
|
try {
|
|
244
184
|
await clipTo(context, () => {
|
|
@@ -262,7 +202,7 @@ async function drawVectorPage(canvas, context) {
|
|
|
262
202
|
const reason = rasterReason(object, registered, missingFonts);
|
|
263
203
|
if (!reason) continue;
|
|
264
204
|
if (mode === "hybrid") {
|
|
265
|
-
rasters.set(object, rasterMarkup(canvas, object, multiplier));
|
|
205
|
+
rasters.set(object, rasterMarkup(canvas, object, multiplier, options.limits));
|
|
266
206
|
warnings.push({
|
|
267
207
|
code: "PDF_RASTERIZED",
|
|
268
208
|
message: `An object was drawn as a picture at ${options.dpi ?? 300} dpi because ${RASTER_REASONS[reason]}`,
|
|
@@ -295,6 +235,7 @@ async function drawVectorPage(canvas, context) {
|
|
|
295
235
|
});
|
|
296
236
|
const embedded = await embedSvgAssets(repairFabricSvg(svg), canvas, {
|
|
297
237
|
textOnPath: "vector",
|
|
238
|
+
textDecorations: "shapes",
|
|
298
239
|
embedImages: true,
|
|
299
240
|
maxEmbeddedImageBytes: Number.MAX_SAFE_INTEGER,
|
|
300
241
|
embedFonts: {}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_asset_pipeline = require("../assets/asset-pipeline.cjs");
|
|
2
|
+
const require_ids = require("../document/ids.cjs");
|
|
2
3
|
//#region src/recovery/recovery-controller.ts
|
|
3
4
|
async function toStoredFiles(files) {
|
|
4
5
|
const entries = await Promise.all(Object.entries(files).map(async ([url, blob]) => [url, {
|
|
@@ -12,22 +13,55 @@ function fromStoredFiles(files) {
|
|
|
12
13
|
for (const [url, file] of Object.entries(files ?? {})) restored[url] = file instanceof Blob ? file : new Blob([file.bytes], { type: file.type });
|
|
13
14
|
return restored;
|
|
14
15
|
}
|
|
15
|
-
function fromStoredRecord(value) {
|
|
16
|
+
function fromStoredRecord(value, sessionId, active) {
|
|
16
17
|
if (!isRecoveryRecord(value)) return void 0;
|
|
17
18
|
const stored = value;
|
|
18
19
|
return {
|
|
19
20
|
...stored,
|
|
21
|
+
sessionId,
|
|
22
|
+
active,
|
|
20
23
|
files: fromStoredFiles(stored.files)
|
|
21
24
|
};
|
|
22
25
|
}
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const LEGACY_SESSION = "legacy";
|
|
27
|
+
const checkpointPrefix = "checkpoint:";
|
|
28
|
+
const closingPrefix = "closing:";
|
|
29
|
+
const loadingPrefix = "loading:";
|
|
30
|
+
const legacyDocumentPrefix = "document:";
|
|
31
|
+
const legacyUnloadPrefix = "unload:";
|
|
32
|
+
const legacyLoadingKey = "loading";
|
|
33
|
+
function branchKeys({ sessionId, documentId }) {
|
|
34
|
+
if (sessionId === LEGACY_SESSION) return {
|
|
35
|
+
checkpoint: `${legacyDocumentPrefix}${documentId}`,
|
|
36
|
+
closing: `${legacyUnloadPrefix}${documentId}`
|
|
37
|
+
};
|
|
38
|
+
const document = encodeURIComponent(documentId);
|
|
39
|
+
return {
|
|
40
|
+
checkpoint: `${checkpointPrefix}${sessionId}:${document}`,
|
|
41
|
+
closing: `${closingPrefix}${sessionId}:${document}`
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** The branch a key belongs to, or undefined for keys that are not copies. */
|
|
45
|
+
function parseBranch(key) {
|
|
46
|
+
for (const prefix of [checkpointPrefix, closingPrefix]) {
|
|
47
|
+
if (!key.startsWith(prefix)) continue;
|
|
48
|
+
const rest = key.slice(prefix.length);
|
|
49
|
+
const separator = rest.indexOf(":");
|
|
50
|
+
if (separator <= 0) return void 0;
|
|
51
|
+
return {
|
|
52
|
+
sessionId: rest.slice(0, separator),
|
|
53
|
+
documentId: decodeURIComponent(rest.slice(separator + 1))
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
for (const prefix of [legacyDocumentPrefix, legacyUnloadPrefix]) if (key.startsWith(prefix)) return {
|
|
57
|
+
sessionId: LEGACY_SESSION,
|
|
58
|
+
documentId: key.slice(prefix.length)
|
|
59
|
+
};
|
|
28
60
|
}
|
|
29
|
-
|
|
30
|
-
|
|
61
|
+
const LOCK_PREFIX = "fabricjs-document-engine/recovery/";
|
|
62
|
+
function lockManager() {
|
|
63
|
+
const locks = globalThis.navigator?.locks;
|
|
64
|
+
return typeof locks?.request === "function" && typeof locks.query === "function" ? locks : void 0;
|
|
31
65
|
}
|
|
32
66
|
function newerRecord(checkpoint, unloadCopy) {
|
|
33
67
|
if (!unloadCopy) return checkpoint;
|
|
@@ -58,6 +92,24 @@ async function restoreRecordedFiles(record) {
|
|
|
58
92
|
}
|
|
59
93
|
function createRecoveryController(options) {
|
|
60
94
|
const { store } = options;
|
|
95
|
+
const sessionId = require_ids.createId();
|
|
96
|
+
const own = (documentId) => ({
|
|
97
|
+
sessionId,
|
|
98
|
+
documentId
|
|
99
|
+
});
|
|
100
|
+
const adopted = /* @__PURE__ */ new Map();
|
|
101
|
+
const locks = lockManager();
|
|
102
|
+
let releaseLock;
|
|
103
|
+
locks?.request(`${LOCK_PREFIX}${sessionId}`, () => new Promise((resolve) => releaseLock = resolve)).catch(() => void 0);
|
|
104
|
+
async function liveSessions() {
|
|
105
|
+
const live = /* @__PURE__ */ new Set([sessionId]);
|
|
106
|
+
if (!locks) return live;
|
|
107
|
+
try {
|
|
108
|
+
const { held = [] } = await locks.query();
|
|
109
|
+
for (const lock of held) if (lock.name?.startsWith(LOCK_PREFIX)) live.add(lock.name.slice(34));
|
|
110
|
+
} catch {}
|
|
111
|
+
return live;
|
|
112
|
+
}
|
|
61
113
|
const interval = options.interval ?? 2e3;
|
|
62
114
|
const capturedFiles = /* @__PURE__ */ new Map();
|
|
63
115
|
const readyFiles = /* @__PURE__ */ new Map();
|
|
@@ -88,6 +140,8 @@ function createRecoveryController(options) {
|
|
|
88
140
|
function createRecord(document, files) {
|
|
89
141
|
return {
|
|
90
142
|
documentId: document.id,
|
|
143
|
+
sessionId,
|
|
144
|
+
active: true,
|
|
91
145
|
savedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
92
146
|
baseRevision: document.revision ?? 0,
|
|
93
147
|
document,
|
|
@@ -99,7 +153,7 @@ function createRecoveryController(options) {
|
|
|
99
153
|
if (!options.shouldWrite()) return;
|
|
100
154
|
const document = options.createDocument();
|
|
101
155
|
const record = createRecord(document, await captureTabOnlyFiles(document));
|
|
102
|
-
await store.set(
|
|
156
|
+
await store.set(branchKeys(own(document.id)).checkpoint, {
|
|
103
157
|
...record,
|
|
104
158
|
files: await toStoredFiles(record.files)
|
|
105
159
|
});
|
|
@@ -109,20 +163,52 @@ function createRecoveryController(options) {
|
|
|
109
163
|
cancel();
|
|
110
164
|
if (!options.shouldWrite()) return;
|
|
111
165
|
const document = options.createDocument();
|
|
166
|
+
const keys = branchKeys(own(document.id));
|
|
112
167
|
if (store.setNow) {
|
|
113
|
-
store.setNow(
|
|
168
|
+
store.setNow(keys.closing, createRecord(document, {}));
|
|
114
169
|
return;
|
|
115
170
|
}
|
|
116
171
|
const record = createRecord(document, Object.fromEntries(readyFiles));
|
|
117
|
-
toStoredFiles(record.files).then((files) => store.set(
|
|
172
|
+
toStoredFiles(record.files).then((files) => store.set(keys.checkpoint, {
|
|
118
173
|
...record,
|
|
119
174
|
files
|
|
120
175
|
})).then(() => options.onCheckpoint(record), options.onError);
|
|
121
176
|
}
|
|
122
|
-
async function
|
|
177
|
+
async function readBranch(branch, live) {
|
|
178
|
+
const keys = branchKeys(branch);
|
|
179
|
+
const [checkpoint, closing] = await Promise.all([store.get(keys.checkpoint), store.get(keys.closing)]);
|
|
180
|
+
const active = live.has(branch.sessionId);
|
|
181
|
+
return newerRecord(fromStoredRecord(checkpoint, branch.sessionId, active), fromStoredRecord(closing, branch.sessionId, active));
|
|
182
|
+
}
|
|
183
|
+
/** Every branch in the store, de-duplicated, in one pass over the keys. */
|
|
184
|
+
async function branches(documentId) {
|
|
185
|
+
const found = /* @__PURE__ */ new Map();
|
|
186
|
+
for (const key of await store.keys()) {
|
|
187
|
+
const branch = parseBranch(key);
|
|
188
|
+
if (!branch || documentId !== void 0 && branch.documentId !== documentId) continue;
|
|
189
|
+
found.set(`${branch.sessionId}\u0000${branch.documentId}`, branch);
|
|
190
|
+
}
|
|
191
|
+
return [...found.values()];
|
|
192
|
+
}
|
|
193
|
+
async function readAll(documentId) {
|
|
123
194
|
await queue;
|
|
124
|
-
const
|
|
125
|
-
return
|
|
195
|
+
const live = await liveSessions();
|
|
196
|
+
return (await Promise.all((await branches(documentId)).map((branch) => readBranch(branch, live)))).filter((record) => record !== void 0).sort((first, second) => second.savedAt.localeCompare(first.savedAt));
|
|
197
|
+
}
|
|
198
|
+
async function read(documentId, branchSession) {
|
|
199
|
+
if (branchSession !== void 0) {
|
|
200
|
+
await queue;
|
|
201
|
+
return readBranch({
|
|
202
|
+
sessionId: branchSession,
|
|
203
|
+
documentId
|
|
204
|
+
}, await liveSessions());
|
|
205
|
+
}
|
|
206
|
+
return (await readAll(documentId))[0];
|
|
207
|
+
}
|
|
208
|
+
async function deleteBranch(branch) {
|
|
209
|
+
const keys = branchKeys(branch);
|
|
210
|
+
await store.delete(keys.closing);
|
|
211
|
+
await store.delete(keys.checkpoint);
|
|
126
212
|
}
|
|
127
213
|
function cancel() {
|
|
128
214
|
if (timer !== void 0) clearTimeout(timer);
|
|
@@ -151,31 +237,64 @@ function createRecoveryController(options) {
|
|
|
151
237
|
flush,
|
|
152
238
|
writeNow: writeCheckpointBeforeThePageCloses,
|
|
153
239
|
cancel,
|
|
240
|
+
sessionId,
|
|
154
241
|
remove: (documentId) => enqueue(async () => {
|
|
155
|
-
await
|
|
156
|
-
await
|
|
242
|
+
await deleteBranch(own(documentId));
|
|
243
|
+
await deleteBranch({
|
|
244
|
+
sessionId: LEGACY_SESSION,
|
|
245
|
+
documentId
|
|
246
|
+
});
|
|
247
|
+
for (const other of adopted.get(documentId) ?? []) await deleteBranch({
|
|
248
|
+
sessionId: other,
|
|
249
|
+
documentId
|
|
250
|
+
});
|
|
251
|
+
adopted.delete(documentId);
|
|
157
252
|
}),
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (key.startsWith(documentKeyPrefix)) documentIds.add(key.slice(9));
|
|
164
|
-
if (key.startsWith(unloadKeyPrefix)) documentIds.add(key.slice(7));
|
|
165
|
-
}
|
|
166
|
-
return (await Promise.all([...documentIds].map(read))).filter((record) => record !== void 0).sort((first, second) => second.savedAt.localeCompare(first.savedAt));
|
|
253
|
+
adopt(documentId, other) {
|
|
254
|
+
if (other === sessionId) return;
|
|
255
|
+
const sessions = adopted.get(documentId) ?? /* @__PURE__ */ new Set();
|
|
256
|
+
sessions.add(other);
|
|
257
|
+
adopted.set(documentId, sessions);
|
|
167
258
|
},
|
|
168
|
-
|
|
259
|
+
discard: (documentId, branchSession) => enqueue(async () => {
|
|
260
|
+
const targets = branchSession === void 0 ? await branches(documentId) : [{
|
|
261
|
+
sessionId: branchSession,
|
|
262
|
+
documentId
|
|
263
|
+
}];
|
|
264
|
+
for (const branch of targets) await deleteBranch(branch);
|
|
265
|
+
}),
|
|
266
|
+
read,
|
|
267
|
+
list: () => readAll(),
|
|
268
|
+
markLoadStarted: (documentId) => store.set(`${loadingPrefix}${sessionId}`, {
|
|
169
269
|
documentId,
|
|
170
270
|
startedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
171
271
|
}).catch(options.onError),
|
|
172
|
-
markLoadFinished: () =>
|
|
272
|
+
markLoadFinished: async () => {
|
|
273
|
+
try {
|
|
274
|
+
await store.delete(`${loadingPrefix}${sessionId}`);
|
|
275
|
+
const live = await liveSessions();
|
|
276
|
+
for (const key of await store.keys()) {
|
|
277
|
+
const markerSession = key === legacyLoadingKey ? LEGACY_SESSION : key.startsWith(loadingPrefix) ? key.slice(8) : void 0;
|
|
278
|
+
if (markerSession !== void 0 && !live.has(markerSession)) await store.delete(key);
|
|
279
|
+
}
|
|
280
|
+
} catch (error) {
|
|
281
|
+
options.onError(error);
|
|
282
|
+
}
|
|
283
|
+
},
|
|
173
284
|
async interruptedLoad() {
|
|
174
|
-
const
|
|
175
|
-
|
|
285
|
+
const live = await liveSessions();
|
|
286
|
+
let newest;
|
|
287
|
+
for (const key of await store.keys()) {
|
|
288
|
+
const markerSession = key === legacyLoadingKey ? LEGACY_SESSION : key.startsWith(loadingPrefix) ? key.slice(8) : void 0;
|
|
289
|
+
if (markerSession === void 0 || live.has(markerSession)) continue;
|
|
290
|
+
const marker = await store.get(key);
|
|
291
|
+
if (marker && typeof marker.startedAt === "string" && (!newest || marker.startedAt > newest.startedAt)) newest = marker;
|
|
292
|
+
}
|
|
293
|
+
return newest;
|
|
176
294
|
},
|
|
177
295
|
destroy() {
|
|
178
296
|
cancel();
|
|
297
|
+
releaseLock?.();
|
|
179
298
|
pageEvents?.removeEventListener("pagehide", handlePageHidden);
|
|
180
299
|
pageEvents?.document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
181
300
|
}
|
|
@@ -7,6 +7,14 @@ export interface RecoveryOptions {
|
|
|
7
7
|
}
|
|
8
8
|
export interface RecoveryRecord {
|
|
9
9
|
documentId: string;
|
|
10
|
+
/**
|
|
11
|
+
* The editor session (one per engine, so one per tab) that wrote this copy.
|
|
12
|
+
* Two tabs editing the same document keep separate copies. `legacy` marks
|
|
13
|
+
* a copy written before 1.2.
|
|
14
|
+
*/
|
|
15
|
+
sessionId: string;
|
|
16
|
+
/** True while the session that wrote the copy is still open in a tab, when the browser can tell. */
|
|
17
|
+
active: boolean;
|
|
10
18
|
savedAt: string;
|
|
11
19
|
baseRevision: number;
|
|
12
20
|
document: FabricDocument;
|
|
@@ -7,6 +7,14 @@ export interface RecoveryOptions {
|
|
|
7
7
|
}
|
|
8
8
|
export interface RecoveryRecord {
|
|
9
9
|
documentId: string;
|
|
10
|
+
/**
|
|
11
|
+
* The editor session (one per engine, so one per tab) that wrote this copy.
|
|
12
|
+
* Two tabs editing the same document keep separate copies. `legacy` marks
|
|
13
|
+
* a copy written before 1.2.
|
|
14
|
+
*/
|
|
15
|
+
sessionId: string;
|
|
16
|
+
/** True while the session that wrote the copy is still open in a tab, when the browser can tell. */
|
|
17
|
+
active: boolean;
|
|
10
18
|
savedAt: string;
|
|
11
19
|
baseRevision: number;
|
|
12
20
|
document: FabricDocument;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { rewriteUrls } from "../assets/asset-pipeline.js";
|
|
2
|
+
import { createId } from "../document/ids.js";
|
|
2
3
|
//#region src/recovery/recovery-controller.ts
|
|
3
4
|
async function toStoredFiles(files) {
|
|
4
5
|
const entries = await Promise.all(Object.entries(files).map(async ([url, blob]) => [url, {
|
|
@@ -12,22 +13,55 @@ function fromStoredFiles(files) {
|
|
|
12
13
|
for (const [url, file] of Object.entries(files ?? {})) restored[url] = file instanceof Blob ? file : new Blob([file.bytes], { type: file.type });
|
|
13
14
|
return restored;
|
|
14
15
|
}
|
|
15
|
-
function fromStoredRecord(value) {
|
|
16
|
+
function fromStoredRecord(value, sessionId, active) {
|
|
16
17
|
if (!isRecoveryRecord(value)) return void 0;
|
|
17
18
|
const stored = value;
|
|
18
19
|
return {
|
|
19
20
|
...stored,
|
|
21
|
+
sessionId,
|
|
22
|
+
active,
|
|
20
23
|
files: fromStoredFiles(stored.files)
|
|
21
24
|
};
|
|
22
25
|
}
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const LEGACY_SESSION = "legacy";
|
|
27
|
+
const checkpointPrefix = "checkpoint:";
|
|
28
|
+
const closingPrefix = "closing:";
|
|
29
|
+
const loadingPrefix = "loading:";
|
|
30
|
+
const legacyDocumentPrefix = "document:";
|
|
31
|
+
const legacyUnloadPrefix = "unload:";
|
|
32
|
+
const legacyLoadingKey = "loading";
|
|
33
|
+
function branchKeys({ sessionId, documentId }) {
|
|
34
|
+
if (sessionId === LEGACY_SESSION) return {
|
|
35
|
+
checkpoint: `${legacyDocumentPrefix}${documentId}`,
|
|
36
|
+
closing: `${legacyUnloadPrefix}${documentId}`
|
|
37
|
+
};
|
|
38
|
+
const document = encodeURIComponent(documentId);
|
|
39
|
+
return {
|
|
40
|
+
checkpoint: `${checkpointPrefix}${sessionId}:${document}`,
|
|
41
|
+
closing: `${closingPrefix}${sessionId}:${document}`
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** The branch a key belongs to, or undefined for keys that are not copies. */
|
|
45
|
+
function parseBranch(key) {
|
|
46
|
+
for (const prefix of [checkpointPrefix, closingPrefix]) {
|
|
47
|
+
if (!key.startsWith(prefix)) continue;
|
|
48
|
+
const rest = key.slice(prefix.length);
|
|
49
|
+
const separator = rest.indexOf(":");
|
|
50
|
+
if (separator <= 0) return void 0;
|
|
51
|
+
return {
|
|
52
|
+
sessionId: rest.slice(0, separator),
|
|
53
|
+
documentId: decodeURIComponent(rest.slice(separator + 1))
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
for (const prefix of [legacyDocumentPrefix, legacyUnloadPrefix]) if (key.startsWith(prefix)) return {
|
|
57
|
+
sessionId: LEGACY_SESSION,
|
|
58
|
+
documentId: key.slice(prefix.length)
|
|
59
|
+
};
|
|
28
60
|
}
|
|
29
|
-
|
|
30
|
-
|
|
61
|
+
const LOCK_PREFIX = "fabricjs-document-engine/recovery/";
|
|
62
|
+
function lockManager() {
|
|
63
|
+
const locks = globalThis.navigator?.locks;
|
|
64
|
+
return typeof locks?.request === "function" && typeof locks.query === "function" ? locks : void 0;
|
|
31
65
|
}
|
|
32
66
|
function newerRecord(checkpoint, unloadCopy) {
|
|
33
67
|
if (!unloadCopy) return checkpoint;
|
|
@@ -58,6 +92,24 @@ async function restoreRecordedFiles(record) {
|
|
|
58
92
|
}
|
|
59
93
|
function createRecoveryController(options) {
|
|
60
94
|
const { store } = options;
|
|
95
|
+
const sessionId = createId();
|
|
96
|
+
const own = (documentId) => ({
|
|
97
|
+
sessionId,
|
|
98
|
+
documentId
|
|
99
|
+
});
|
|
100
|
+
const adopted = /* @__PURE__ */ new Map();
|
|
101
|
+
const locks = lockManager();
|
|
102
|
+
let releaseLock;
|
|
103
|
+
locks?.request(`${LOCK_PREFIX}${sessionId}`, () => new Promise((resolve) => releaseLock = resolve)).catch(() => void 0);
|
|
104
|
+
async function liveSessions() {
|
|
105
|
+
const live = /* @__PURE__ */ new Set([sessionId]);
|
|
106
|
+
if (!locks) return live;
|
|
107
|
+
try {
|
|
108
|
+
const { held = [] } = await locks.query();
|
|
109
|
+
for (const lock of held) if (lock.name?.startsWith(LOCK_PREFIX)) live.add(lock.name.slice(34));
|
|
110
|
+
} catch {}
|
|
111
|
+
return live;
|
|
112
|
+
}
|
|
61
113
|
const interval = options.interval ?? 2e3;
|
|
62
114
|
const capturedFiles = /* @__PURE__ */ new Map();
|
|
63
115
|
const readyFiles = /* @__PURE__ */ new Map();
|
|
@@ -88,6 +140,8 @@ function createRecoveryController(options) {
|
|
|
88
140
|
function createRecord(document, files) {
|
|
89
141
|
return {
|
|
90
142
|
documentId: document.id,
|
|
143
|
+
sessionId,
|
|
144
|
+
active: true,
|
|
91
145
|
savedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
92
146
|
baseRevision: document.revision ?? 0,
|
|
93
147
|
document,
|
|
@@ -99,7 +153,7 @@ function createRecoveryController(options) {
|
|
|
99
153
|
if (!options.shouldWrite()) return;
|
|
100
154
|
const document = options.createDocument();
|
|
101
155
|
const record = createRecord(document, await captureTabOnlyFiles(document));
|
|
102
|
-
await store.set(
|
|
156
|
+
await store.set(branchKeys(own(document.id)).checkpoint, {
|
|
103
157
|
...record,
|
|
104
158
|
files: await toStoredFiles(record.files)
|
|
105
159
|
});
|
|
@@ -109,20 +163,52 @@ function createRecoveryController(options) {
|
|
|
109
163
|
cancel();
|
|
110
164
|
if (!options.shouldWrite()) return;
|
|
111
165
|
const document = options.createDocument();
|
|
166
|
+
const keys = branchKeys(own(document.id));
|
|
112
167
|
if (store.setNow) {
|
|
113
|
-
store.setNow(
|
|
168
|
+
store.setNow(keys.closing, createRecord(document, {}));
|
|
114
169
|
return;
|
|
115
170
|
}
|
|
116
171
|
const record = createRecord(document, Object.fromEntries(readyFiles));
|
|
117
|
-
toStoredFiles(record.files).then((files) => store.set(
|
|
172
|
+
toStoredFiles(record.files).then((files) => store.set(keys.checkpoint, {
|
|
118
173
|
...record,
|
|
119
174
|
files
|
|
120
175
|
})).then(() => options.onCheckpoint(record), options.onError);
|
|
121
176
|
}
|
|
122
|
-
async function
|
|
177
|
+
async function readBranch(branch, live) {
|
|
178
|
+
const keys = branchKeys(branch);
|
|
179
|
+
const [checkpoint, closing] = await Promise.all([store.get(keys.checkpoint), store.get(keys.closing)]);
|
|
180
|
+
const active = live.has(branch.sessionId);
|
|
181
|
+
return newerRecord(fromStoredRecord(checkpoint, branch.sessionId, active), fromStoredRecord(closing, branch.sessionId, active));
|
|
182
|
+
}
|
|
183
|
+
/** Every branch in the store, de-duplicated, in one pass over the keys. */
|
|
184
|
+
async function branches(documentId) {
|
|
185
|
+
const found = /* @__PURE__ */ new Map();
|
|
186
|
+
for (const key of await store.keys()) {
|
|
187
|
+
const branch = parseBranch(key);
|
|
188
|
+
if (!branch || documentId !== void 0 && branch.documentId !== documentId) continue;
|
|
189
|
+
found.set(`${branch.sessionId}\u0000${branch.documentId}`, branch);
|
|
190
|
+
}
|
|
191
|
+
return [...found.values()];
|
|
192
|
+
}
|
|
193
|
+
async function readAll(documentId) {
|
|
123
194
|
await queue;
|
|
124
|
-
const
|
|
125
|
-
return
|
|
195
|
+
const live = await liveSessions();
|
|
196
|
+
return (await Promise.all((await branches(documentId)).map((branch) => readBranch(branch, live)))).filter((record) => record !== void 0).sort((first, second) => second.savedAt.localeCompare(first.savedAt));
|
|
197
|
+
}
|
|
198
|
+
async function read(documentId, branchSession) {
|
|
199
|
+
if (branchSession !== void 0) {
|
|
200
|
+
await queue;
|
|
201
|
+
return readBranch({
|
|
202
|
+
sessionId: branchSession,
|
|
203
|
+
documentId
|
|
204
|
+
}, await liveSessions());
|
|
205
|
+
}
|
|
206
|
+
return (await readAll(documentId))[0];
|
|
207
|
+
}
|
|
208
|
+
async function deleteBranch(branch) {
|
|
209
|
+
const keys = branchKeys(branch);
|
|
210
|
+
await store.delete(keys.closing);
|
|
211
|
+
await store.delete(keys.checkpoint);
|
|
126
212
|
}
|
|
127
213
|
function cancel() {
|
|
128
214
|
if (timer !== void 0) clearTimeout(timer);
|
|
@@ -151,31 +237,64 @@ function createRecoveryController(options) {
|
|
|
151
237
|
flush,
|
|
152
238
|
writeNow: writeCheckpointBeforeThePageCloses,
|
|
153
239
|
cancel,
|
|
240
|
+
sessionId,
|
|
154
241
|
remove: (documentId) => enqueue(async () => {
|
|
155
|
-
await
|
|
156
|
-
await
|
|
242
|
+
await deleteBranch(own(documentId));
|
|
243
|
+
await deleteBranch({
|
|
244
|
+
sessionId: LEGACY_SESSION,
|
|
245
|
+
documentId
|
|
246
|
+
});
|
|
247
|
+
for (const other of adopted.get(documentId) ?? []) await deleteBranch({
|
|
248
|
+
sessionId: other,
|
|
249
|
+
documentId
|
|
250
|
+
});
|
|
251
|
+
adopted.delete(documentId);
|
|
157
252
|
}),
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (key.startsWith(documentKeyPrefix)) documentIds.add(key.slice(9));
|
|
164
|
-
if (key.startsWith(unloadKeyPrefix)) documentIds.add(key.slice(7));
|
|
165
|
-
}
|
|
166
|
-
return (await Promise.all([...documentIds].map(read))).filter((record) => record !== void 0).sort((first, second) => second.savedAt.localeCompare(first.savedAt));
|
|
253
|
+
adopt(documentId, other) {
|
|
254
|
+
if (other === sessionId) return;
|
|
255
|
+
const sessions = adopted.get(documentId) ?? /* @__PURE__ */ new Set();
|
|
256
|
+
sessions.add(other);
|
|
257
|
+
adopted.set(documentId, sessions);
|
|
167
258
|
},
|
|
168
|
-
|
|
259
|
+
discard: (documentId, branchSession) => enqueue(async () => {
|
|
260
|
+
const targets = branchSession === void 0 ? await branches(documentId) : [{
|
|
261
|
+
sessionId: branchSession,
|
|
262
|
+
documentId
|
|
263
|
+
}];
|
|
264
|
+
for (const branch of targets) await deleteBranch(branch);
|
|
265
|
+
}),
|
|
266
|
+
read,
|
|
267
|
+
list: () => readAll(),
|
|
268
|
+
markLoadStarted: (documentId) => store.set(`${loadingPrefix}${sessionId}`, {
|
|
169
269
|
documentId,
|
|
170
270
|
startedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
171
271
|
}).catch(options.onError),
|
|
172
|
-
markLoadFinished: () =>
|
|
272
|
+
markLoadFinished: async () => {
|
|
273
|
+
try {
|
|
274
|
+
await store.delete(`${loadingPrefix}${sessionId}`);
|
|
275
|
+
const live = await liveSessions();
|
|
276
|
+
for (const key of await store.keys()) {
|
|
277
|
+
const markerSession = key === legacyLoadingKey ? LEGACY_SESSION : key.startsWith(loadingPrefix) ? key.slice(8) : void 0;
|
|
278
|
+
if (markerSession !== void 0 && !live.has(markerSession)) await store.delete(key);
|
|
279
|
+
}
|
|
280
|
+
} catch (error) {
|
|
281
|
+
options.onError(error);
|
|
282
|
+
}
|
|
283
|
+
},
|
|
173
284
|
async interruptedLoad() {
|
|
174
|
-
const
|
|
175
|
-
|
|
285
|
+
const live = await liveSessions();
|
|
286
|
+
let newest;
|
|
287
|
+
for (const key of await store.keys()) {
|
|
288
|
+
const markerSession = key === legacyLoadingKey ? LEGACY_SESSION : key.startsWith(loadingPrefix) ? key.slice(8) : void 0;
|
|
289
|
+
if (markerSession === void 0 || live.has(markerSession)) continue;
|
|
290
|
+
const marker = await store.get(key);
|
|
291
|
+
if (marker && typeof marker.startedAt === "string" && (!newest || marker.startedAt > newest.startedAt)) newest = marker;
|
|
292
|
+
}
|
|
293
|
+
return newest;
|
|
176
294
|
},
|
|
177
295
|
destroy() {
|
|
178
296
|
cancel();
|
|
297
|
+
releaseLock?.();
|
|
179
298
|
pageEvents?.removeEventListener("pagehide", handlePageHidden);
|
|
180
299
|
pageEvents?.document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
181
300
|
}
|