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.
Files changed (118) hide show
  1. package/README.md +220 -11
  2. package/README.zh-CN.md +220 -11
  3. package/dist/assets/asset-manifest.d.cts +3 -0
  4. package/dist/assets/asset-manifest.d.ts +3 -0
  5. package/dist/assets/asset-pipeline.cjs +31 -17
  6. package/dist/assets/asset-pipeline.d.cts +5 -1
  7. package/dist/assets/asset-pipeline.d.ts +5 -1
  8. package/dist/assets/asset-pipeline.js +31 -17
  9. package/dist/assets/font-check.cjs +9 -1
  10. package/dist/assets/font-check.js +9 -1
  11. package/dist/assets/image-check.cjs +89 -6
  12. package/dist/assets/image-check.d.cts +17 -0
  13. package/dist/assets/image-check.d.ts +17 -0
  14. package/dist/assets/image-check.js +89 -7
  15. package/dist/commands/clipboard.cjs +141 -0
  16. package/dist/commands/clipboard.d.cts +47 -0
  17. package/dist/commands/clipboard.d.ts +47 -0
  18. package/dist/commands/clipboard.js +140 -0
  19. package/dist/commands/layers.cjs +97 -0
  20. package/dist/commands/layers.d.cts +39 -0
  21. package/dist/commands/layers.d.ts +39 -0
  22. package/dist/commands/layers.js +92 -0
  23. package/dist/document/document-format.d.cts +15 -0
  24. package/dist/document/document-format.d.ts +15 -0
  25. package/dist/document/validate-document.cjs +8 -0
  26. package/dist/document/validate-document.js +8 -0
  27. package/dist/engine/create-document-engine.cjs +196 -31
  28. package/dist/engine/create-document-engine.d.cts +44 -7
  29. package/dist/engine/create-document-engine.d.ts +44 -7
  30. package/dist/engine/create-document-engine.js +197 -32
  31. package/dist/engine/errors.d.cts +1 -1
  32. package/dist/engine/errors.d.ts +1 -1
  33. package/dist/export/batch-render.cjs +172 -0
  34. package/dist/export/batch-render.d.cts +43 -0
  35. package/dist/export/batch-render.d.ts +43 -0
  36. package/dist/export/batch-render.js +171 -0
  37. package/dist/export/export-options.cjs +21 -1
  38. package/dist/export/export-options.d.cts +29 -0
  39. package/dist/export/export-options.d.ts +29 -0
  40. package/dist/export/export-options.js +21 -2
  41. package/dist/export/preflight-export.cjs +11 -2
  42. package/dist/export/preflight-export.d.cts +1 -1
  43. package/dist/export/preflight-export.d.ts +1 -1
  44. package/dist/export/preflight-export.js +11 -2
  45. package/dist/export/render-export.cjs +57 -4
  46. package/dist/export/render-export.js +57 -5
  47. package/dist/export/svg/clip-masks.cjs +39 -0
  48. package/dist/export/svg/clip-masks.js +37 -0
  49. package/dist/export/svg/embed-assets.cjs +250 -0
  50. package/dist/export/svg/embed-assets.js +250 -0
  51. package/dist/export/svg/overrides.cjs +32 -0
  52. package/dist/export/svg/overrides.js +32 -0
  53. package/dist/export/svg/raster-object.cjs +82 -0
  54. package/dist/export/svg/raster-object.js +81 -0
  55. package/dist/export/svg/text-decorations.cjs +108 -0
  56. package/dist/export/svg/text-decorations.js +107 -0
  57. package/dist/export/svg/text-on-path.cjs +242 -0
  58. package/dist/export/svg/text-on-path.js +240 -0
  59. package/dist/fabric/fabric-adapter.cjs +49 -6
  60. package/dist/fabric/fabric-adapter.js +49 -6
  61. package/dist/fabric/object-registry.cjs +1 -1
  62. package/dist/fabric/object-registry.js +1 -1
  63. package/dist/fabric/page-state.cjs +69 -0
  64. package/dist/fabric/page-state.js +64 -0
  65. package/dist/history/apply-state.cjs +19 -2
  66. package/dist/history/apply-state.js +19 -2
  67. package/dist/history/create-history.cjs +53 -13
  68. package/dist/history/create-history.d.cts +8 -0
  69. package/dist/history/create-history.d.ts +8 -0
  70. package/dist/history/create-history.js +53 -13
  71. package/dist/history/snapshot.cjs +10 -1
  72. package/dist/history/snapshot.js +10 -2
  73. package/dist/import/svg-import.cjs +256 -0
  74. package/dist/import/svg-import.d.cts +50 -0
  75. package/dist/import/svg-import.d.ts +50 -0
  76. package/dist/import/svg-import.js +256 -0
  77. package/dist/index.cjs +12 -0
  78. package/dist/index.d.cts +10 -5
  79. package/dist/index.d.ts +10 -5
  80. package/dist/index.js +4 -1
  81. package/dist/migrations/migrate-document.cjs +2 -1
  82. package/dist/migrations/migrate-document.js +2 -1
  83. package/dist/pdf/export-pdf.cjs +381 -0
  84. package/dist/pdf/export-pdf.d.cts +73 -0
  85. package/dist/pdf/export-pdf.d.ts +73 -0
  86. package/dist/pdf/export-pdf.js +381 -0
  87. package/dist/pdf/fonts.cjs +115 -0
  88. package/dist/pdf/fonts.d.cts +12 -0
  89. package/dist/pdf/fonts.d.ts +12 -0
  90. package/dist/pdf/fonts.js +112 -0
  91. package/dist/pdf/page-layout.cjs +79 -0
  92. package/dist/pdf/page-layout.d.cts +51 -0
  93. package/dist/pdf/page-layout.d.ts +51 -0
  94. package/dist/pdf/page-layout.js +76 -0
  95. package/dist/pdf.cjs +7 -0
  96. package/dist/pdf.d.cts +4 -0
  97. package/dist/pdf.d.ts +4 -0
  98. package/dist/pdf.js +3 -0
  99. package/dist/react/use-layers.cjs +53 -0
  100. package/dist/react/use-layers.d.cts +10 -0
  101. package/dist/react/use-layers.d.ts +10 -0
  102. package/dist/react/use-layers.js +53 -0
  103. package/dist/react.cjs +2 -0
  104. package/dist/react.d.cts +3 -1
  105. package/dist/react.d.ts +3 -1
  106. package/dist/react.js +2 -1
  107. package/dist/recovery/recovery-controller.cjs +148 -29
  108. package/dist/recovery/recovery-controller.d.cts +8 -0
  109. package/dist/recovery/recovery-controller.d.ts +8 -0
  110. package/dist/recovery/recovery-controller.js +148 -29
  111. package/dist/security/content-limits.cjs +43 -3
  112. package/dist/security/content-limits.d.cts +8 -0
  113. package/dist/security/content-limits.d.ts +8 -0
  114. package/dist/security/content-limits.js +39 -4
  115. package/dist/util/concurrency.cjs +41 -0
  116. package/dist/util/concurrency.js +40 -0
  117. package/package.json +30 -3
  118. 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(value) {
18
- return JSON.parse(JSON.stringify(value), (key, nested) => dangerousKeys.has(key) ? void 0 : nested);
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 document = withoutDangerousKeys(input);
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.2",
4
- "description": "Fabric.js save and load, undo and redo, and autosave for an existing canvas: stable object ids, custom objects, export, recovery and save-conflict protection.",
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" } },