fabricjs-document-engine 1.0.2 → 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 +220 -11
- package/README.zh-CN.md +220 -11
- package/dist/assets/asset-manifest.d.cts +3 -0
- package/dist/assets/asset-manifest.d.ts +3 -0
- package/dist/assets/asset-pipeline.cjs +31 -17
- package/dist/assets/asset-pipeline.d.cts +5 -1
- package/dist/assets/asset-pipeline.d.ts +5 -1
- package/dist/assets/asset-pipeline.js +31 -17
- package/dist/assets/font-check.cjs +9 -1
- package/dist/assets/font-check.js +9 -1
- package/dist/assets/image-check.cjs +89 -6
- package/dist/assets/image-check.d.cts +17 -0
- package/dist/assets/image-check.d.ts +17 -0
- package/dist/assets/image-check.js +89 -7
- package/dist/commands/clipboard.cjs +141 -0
- package/dist/commands/clipboard.d.cts +47 -0
- package/dist/commands/clipboard.d.ts +47 -0
- package/dist/commands/clipboard.js +140 -0
- package/dist/commands/layers.cjs +97 -0
- package/dist/commands/layers.d.cts +39 -0
- package/dist/commands/layers.d.ts +39 -0
- package/dist/commands/layers.js +92 -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 +196 -31
- package/dist/engine/create-document-engine.d.cts +44 -7
- package/dist/engine/create-document-engine.d.ts +44 -7
- package/dist/engine/create-document-engine.js +197 -32
- package/dist/engine/errors.d.cts +1 -1
- package/dist/engine/errors.d.ts +1 -1
- package/dist/export/batch-render.cjs +172 -0
- package/dist/export/batch-render.d.cts +43 -0
- package/dist/export/batch-render.d.ts +43 -0
- package/dist/export/batch-render.js +171 -0
- package/dist/export/export-options.cjs +21 -1
- package/dist/export/export-options.d.cts +29 -0
- package/dist/export/export-options.d.ts +29 -0
- package/dist/export/export-options.js +21 -2
- package/dist/export/preflight-export.cjs +11 -2
- package/dist/export/preflight-export.d.cts +1 -1
- package/dist/export/preflight-export.d.ts +1 -1
- package/dist/export/preflight-export.js +11 -2
- package/dist/export/render-export.cjs +57 -4
- package/dist/export/render-export.js +57 -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 +250 -0
- package/dist/export/svg/embed-assets.js +250 -0
- package/dist/export/svg/overrides.cjs +32 -0
- package/dist/export/svg/overrides.js +32 -0
- package/dist/export/svg/raster-object.cjs +82 -0
- package/dist/export/svg/raster-object.js +81 -0
- package/dist/export/svg/text-decorations.cjs +108 -0
- package/dist/export/svg/text-decorations.js +107 -0
- package/dist/export/svg/text-on-path.cjs +242 -0
- package/dist/export/svg/text-on-path.js +240 -0
- package/dist/fabric/fabric-adapter.cjs +49 -6
- package/dist/fabric/fabric-adapter.js +49 -6
- package/dist/fabric/object-registry.cjs +1 -1
- package/dist/fabric/object-registry.js +1 -1
- 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/import/svg-import.cjs +256 -0
- package/dist/import/svg-import.d.cts +50 -0
- package/dist/import/svg-import.d.ts +50 -0
- package/dist/import/svg-import.js +256 -0
- package/dist/index.cjs +12 -0
- package/dist/index.d.cts +10 -5
- package/dist/index.d.ts +10 -5
- package/dist/index.js +4 -1
- package/dist/migrations/migrate-document.cjs +2 -1
- package/dist/migrations/migrate-document.js +2 -1
- package/dist/pdf/export-pdf.cjs +381 -0
- package/dist/pdf/export-pdf.d.cts +73 -0
- package/dist/pdf/export-pdf.d.ts +73 -0
- package/dist/pdf/export-pdf.js +381 -0
- package/dist/pdf/fonts.cjs +115 -0
- package/dist/pdf/fonts.d.cts +12 -0
- package/dist/pdf/fonts.d.ts +12 -0
- package/dist/pdf/fonts.js +112 -0
- package/dist/pdf/page-layout.cjs +79 -0
- package/dist/pdf/page-layout.d.cts +51 -0
- package/dist/pdf/page-layout.d.ts +51 -0
- package/dist/pdf/page-layout.js +76 -0
- package/dist/pdf.cjs +7 -0
- package/dist/pdf.d.cts +4 -0
- package/dist/pdf.d.ts +4 -0
- package/dist/pdf.js +3 -0
- package/dist/react/use-layers.cjs +53 -0
- package/dist/react/use-layers.d.cts +10 -0
- package/dist/react/use-layers.d.ts +10 -0
- package/dist/react/use-layers.js +53 -0
- package/dist/react.cjs +2 -0
- package/dist/react.d.cts +3 -1
- package/dist/react.d.ts +3 -1
- package/dist/react.js +2 -1
- 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 +41 -0
- package/dist/util/concurrency.js +40 -0
- package/package.json +30 -3
- package/schema/document-v1.schema.json +5 -1
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import { findImageReferences } from "../assets/asset-references.js";
|
|
2
2
|
import { DocumentEngineError } from "../engine/errors.js";
|
|
3
3
|
//#region src/security/content-limits.ts
|
|
4
|
+
const DEFAULT_MAX_CANVAS_SIDE = 16384;
|
|
5
|
+
const DEFAULT_MAX_CANVAS_PIXELS = 67108864;
|
|
6
|
+
const DEFAULT_MAX_IMAGE_PIXELS = 67108864;
|
|
7
|
+
const DEFAULT_MAX_DOCUMENT_LENGTH = 1e8;
|
|
8
|
+
/**
|
|
9
|
+
* Why a drawing surface of this size is refused, or undefined when it fits.
|
|
10
|
+
* Pure arithmetic, run before any canvas is created: a canvas over the
|
|
11
|
+
* browser's limit fails silently or crashes the tab.
|
|
12
|
+
*/
|
|
13
|
+
function canvasSizeProblem(width, height, limits = {}) {
|
|
14
|
+
const maxSide = limits.maxCanvasSide ?? 16384;
|
|
15
|
+
const maxPixels = limits.maxCanvasPixels ?? 67108864;
|
|
16
|
+
const w = Math.ceil(width);
|
|
17
|
+
const h = Math.ceil(height);
|
|
18
|
+
if (w > maxSide || h > maxSide) return `${w} × ${h} pixels is larger than the ${maxSide}-pixel limit per side`;
|
|
19
|
+
if (w * h > maxPixels) return `${w} × ${h} pixels is ${w * h} pixels, more than the limit of ${maxPixels}`;
|
|
20
|
+
}
|
|
4
21
|
const dangerousKeys = /* @__PURE__ */ new Set([
|
|
5
22
|
"__proto__",
|
|
6
23
|
"constructor",
|
|
@@ -14,8 +31,18 @@ function isSafeImageUrl(url) {
|
|
|
14
31
|
if (scheme === "data") return trimmed.startsWith("data:image/");
|
|
15
32
|
return false;
|
|
16
33
|
}
|
|
17
|
-
function withoutDangerousKeys(
|
|
18
|
-
return JSON.parse(
|
|
34
|
+
function withoutDangerousKeys(json) {
|
|
35
|
+
return JSON.parse(json, (key, nested) => dangerousKeys.has(key) ? void 0 : nested);
|
|
36
|
+
}
|
|
37
|
+
function pageSize(document) {
|
|
38
|
+
const canvas = document.canvas;
|
|
39
|
+
return typeof canvas === "object" && canvas !== null ? {
|
|
40
|
+
width: canvas.width,
|
|
41
|
+
height: canvas.height
|
|
42
|
+
} : {
|
|
43
|
+
width: document.width,
|
|
44
|
+
height: document.height
|
|
45
|
+
};
|
|
19
46
|
}
|
|
20
47
|
function deepestNesting(value) {
|
|
21
48
|
let deepest = 0;
|
|
@@ -56,8 +83,16 @@ function secureDocument(input, limits = {}) {
|
|
|
56
83
|
if (typeof input !== "object" || input === null) return input;
|
|
57
84
|
const maxDepth = limits.maxDepth ?? 100;
|
|
58
85
|
const maxObjects = limits.maxObjects ?? 5e4;
|
|
59
|
-
const
|
|
86
|
+
const json = JSON.stringify(input);
|
|
87
|
+
const maxLength = limits.maxDocumentLength ?? 1e8;
|
|
88
|
+
if (json.length > maxLength) refuseWhenUnsafe([unsafe("", `the document is ${json.length} characters of JSON, the limit is ${maxLength}`)]);
|
|
89
|
+
const document = withoutDangerousKeys(json);
|
|
60
90
|
const issues = [];
|
|
91
|
+
const { width, height } = pageSize(document);
|
|
92
|
+
if (typeof width === "number" && typeof height === "number") {
|
|
93
|
+
const problem = canvasSizeProblem(width, height, limits);
|
|
94
|
+
if (problem) issues.push(unsafe("canvas", `the page is ${problem}`));
|
|
95
|
+
}
|
|
61
96
|
const depth = deepestNesting(document);
|
|
62
97
|
if (depth > maxDepth) issues.push(unsafe("", `the document is nested ${depth} levels deep, the limit is ${maxDepth}`));
|
|
63
98
|
if (Array.isArray(document.objects) && issues.length === 0) {
|
|
@@ -81,4 +116,4 @@ function refuseUnsafeImageUrls(objects, isAllowedUrl = isSafeImageUrl) {
|
|
|
81
116
|
refuseWhenUnsafe(issues);
|
|
82
117
|
}
|
|
83
118
|
//#endregion
|
|
84
|
-
export { isSafeImageUrl, refuseUnsafeImageUrls, secureDocument };
|
|
119
|
+
export { DEFAULT_MAX_CANVAS_PIXELS, DEFAULT_MAX_CANVAS_SIDE, DEFAULT_MAX_DOCUMENT_LENGTH, DEFAULT_MAX_IMAGE_PIXELS, canvasSizeProblem, isSafeImageUrl, refuseUnsafeImageUrls, secureDocument };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region src/util/concurrency.ts
|
|
2
|
+
/**
|
|
3
|
+
* Runs `work` for every item with at most `limit` running at once, and
|
|
4
|
+
* returns the results in the order of `items`.
|
|
5
|
+
*/
|
|
6
|
+
async function mapWithConcurrency(items, limit, work) {
|
|
7
|
+
const results = new Array(items.length);
|
|
8
|
+
const workers = Math.max(1, Math.min(Math.floor(limit) || 1, items.length));
|
|
9
|
+
let next = 0;
|
|
10
|
+
async function runWorker() {
|
|
11
|
+
while (next < items.length) {
|
|
12
|
+
const index = next;
|
|
13
|
+
next += 1;
|
|
14
|
+
results[index] = await work(items[index], index);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
await Promise.all(Array.from({ length: workers }, runWorker));
|
|
18
|
+
return results;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Lets the browser paint, handle input and run other queued work, such as a
|
|
22
|
+
* framework's re-render after a progress update, before the next piece of
|
|
23
|
+
* work. A message-channel task runs after the tasks already queued and,
|
|
24
|
+
* unlike `setTimeout`, is not delayed by timer clamping. `scheduler.yield()`
|
|
25
|
+
* is not used: it resumes ahead of other queued tasks, so a UI waiting to
|
|
26
|
+
* show progress would not render until the work had finished.
|
|
27
|
+
*/
|
|
28
|
+
function yieldToEventLoop() {
|
|
29
|
+
if (typeof MessageChannel === "function") return new Promise((resolve) => {
|
|
30
|
+
const channel = new MessageChannel();
|
|
31
|
+
channel.port1.onmessage = () => {
|
|
32
|
+
channel.port1.close();
|
|
33
|
+
resolve();
|
|
34
|
+
};
|
|
35
|
+
channel.port2.postMessage(void 0);
|
|
36
|
+
});
|
|
37
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
exports.mapWithConcurrency = mapWithConcurrency;
|
|
41
|
+
exports.yieldToEventLoop = yieldToEventLoop;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region src/util/concurrency.ts
|
|
2
|
+
/**
|
|
3
|
+
* Runs `work` for every item with at most `limit` running at once, and
|
|
4
|
+
* returns the results in the order of `items`.
|
|
5
|
+
*/
|
|
6
|
+
async function mapWithConcurrency(items, limit, work) {
|
|
7
|
+
const results = new Array(items.length);
|
|
8
|
+
const workers = Math.max(1, Math.min(Math.floor(limit) || 1, items.length));
|
|
9
|
+
let next = 0;
|
|
10
|
+
async function runWorker() {
|
|
11
|
+
while (next < items.length) {
|
|
12
|
+
const index = next;
|
|
13
|
+
next += 1;
|
|
14
|
+
results[index] = await work(items[index], index);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
await Promise.all(Array.from({ length: workers }, runWorker));
|
|
18
|
+
return results;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Lets the browser paint, handle input and run other queued work, such as a
|
|
22
|
+
* framework's re-render after a progress update, before the next piece of
|
|
23
|
+
* work. A message-channel task runs after the tasks already queued and,
|
|
24
|
+
* unlike `setTimeout`, is not delayed by timer clamping. `scheduler.yield()`
|
|
25
|
+
* is not used: it resumes ahead of other queued tasks, so a UI waiting to
|
|
26
|
+
* show progress would not render until the work had finished.
|
|
27
|
+
*/
|
|
28
|
+
function yieldToEventLoop() {
|
|
29
|
+
if (typeof MessageChannel === "function") return new Promise((resolve) => {
|
|
30
|
+
const channel = new MessageChannel();
|
|
31
|
+
channel.port1.onmessage = () => {
|
|
32
|
+
channel.port1.close();
|
|
33
|
+
resolve();
|
|
34
|
+
};
|
|
35
|
+
channel.port2.postMessage(void 0);
|
|
36
|
+
});
|
|
37
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { mapWithConcurrency, yieldToEventLoop };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fabricjs-document-engine",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "Fabric.js save and load, undo and redo, and autosave for an existing canvas: stable object ids, custom objects,
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Fabric.js save and load, undo and redo, and autosave for an existing canvas: stable object ids, custom objects, copy and paste, SVG import, PNG, SVG and PDF export, and recovery.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fabricjs",
|
|
7
7
|
"fabric.js",
|
|
@@ -27,6 +27,12 @@
|
|
|
27
27
|
"object id",
|
|
28
28
|
"export svg",
|
|
29
29
|
"export image",
|
|
30
|
+
"text on path",
|
|
31
|
+
"import svg",
|
|
32
|
+
"layers",
|
|
33
|
+
"copy paste",
|
|
34
|
+
"pdf",
|
|
35
|
+
"export pdf",
|
|
30
36
|
"recovery",
|
|
31
37
|
"indexeddb",
|
|
32
38
|
"react",
|
|
@@ -91,6 +97,16 @@
|
|
|
91
97
|
"default": "./dist/react.cjs"
|
|
92
98
|
}
|
|
93
99
|
},
|
|
100
|
+
"./pdf": {
|
|
101
|
+
"import": {
|
|
102
|
+
"types": "./dist/pdf.d.ts",
|
|
103
|
+
"default": "./dist/pdf.js"
|
|
104
|
+
},
|
|
105
|
+
"require": {
|
|
106
|
+
"types": "./dist/pdf.d.cts",
|
|
107
|
+
"default": "./dist/pdf.cjs"
|
|
108
|
+
}
|
|
109
|
+
},
|
|
94
110
|
"./schema/document-v1.json": "./schema/document-v1.schema.json",
|
|
95
111
|
"./package.json": "./package.json"
|
|
96
112
|
},
|
|
@@ -109,7 +125,9 @@
|
|
|
109
125
|
},
|
|
110
126
|
"peerDependencies": {
|
|
111
127
|
"fabric": "^6.0.0 || ^7.0.0",
|
|
112
|
-
"react": "^18.0.0 || ^19.0.0"
|
|
128
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
129
|
+
"jspdf": "^4.0.0",
|
|
130
|
+
"svg2pdf.js": "^2.7.0"
|
|
113
131
|
},
|
|
114
132
|
"publishConfig": {
|
|
115
133
|
"access": "public",
|
|
@@ -136,10 +154,13 @@
|
|
|
136
154
|
"ajv": "^8.20.0",
|
|
137
155
|
"fabric": "^7.4.0",
|
|
138
156
|
"fabric6": "npm:fabric@^6.9.1",
|
|
157
|
+
"jspdf": "^4.2.1",
|
|
158
|
+
"pdfjs-dist": "^6.3.289",
|
|
139
159
|
"playwright": "^1.63.0",
|
|
140
160
|
"publint": "^0.3.24",
|
|
141
161
|
"react": "^19.3.0",
|
|
142
162
|
"react-dom": "^19.3.0",
|
|
163
|
+
"svg2pdf.js": "^2.8.1",
|
|
143
164
|
"tsdown": "^0.23.0",
|
|
144
165
|
"typescript": "^5.9.3",
|
|
145
166
|
"vitest": "^5.0.2"
|
|
@@ -147,6 +168,12 @@
|
|
|
147
168
|
"peerDependenciesMeta": {
|
|
148
169
|
"react": {
|
|
149
170
|
"optional": true
|
|
171
|
+
},
|
|
172
|
+
"jspdf": {
|
|
173
|
+
"optional": true
|
|
174
|
+
},
|
|
175
|
+
"svg2pdf.js": {
|
|
176
|
+
"optional": true
|
|
150
177
|
}
|
|
151
178
|
}
|
|
152
179
|
}
|
|
@@ -18,7 +18,11 @@
|
|
|
18
18
|
"properties": {
|
|
19
19
|
"width": { "type": "number", "exclusiveMinimum": 0 },
|
|
20
20
|
"height": { "type": "number", "exclusiveMinimum": 0 },
|
|
21
|
-
"background": {}
|
|
21
|
+
"background": {},
|
|
22
|
+
"backgroundImage": { "$ref": "#/$defs/fabricObject" },
|
|
23
|
+
"overlay": {},
|
|
24
|
+
"overlayImage": { "$ref": "#/$defs/fabricObject" },
|
|
25
|
+
"clipPath": { "$ref": "#/$defs/fabricObject" }
|
|
22
26
|
}
|
|
23
27
|
},
|
|
24
28
|
"objects": { "type": "array", "items": { "$ref": "#/$defs/fabricObject" } },
|