fabricjs-document-engine 0.0.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 (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +140 -0
  3. package/dist/document/create-document.cjs +13 -0
  4. package/dist/document/create-document.d.cts +6 -0
  5. package/dist/document/create-document.d.ts +6 -0
  6. package/dist/document/create-document.js +13 -0
  7. package/dist/document/document-format.cjs +4 -0
  8. package/dist/document/document-format.d.cts +31 -0
  9. package/dist/document/document-format.d.ts +31 -0
  10. package/dist/document/document-format.js +4 -0
  11. package/dist/document/ids.cjs +11 -0
  12. package/dist/document/ids.js +11 -0
  13. package/dist/document/validate-document.cjs +70 -0
  14. package/dist/document/validate-document.d.cts +4 -0
  15. package/dist/document/validate-document.d.ts +4 -0
  16. package/dist/document/validate-document.js +70 -0
  17. package/dist/engine/create-document-engine.cjs +218 -0
  18. package/dist/engine/create-document-engine.d.cts +56 -0
  19. package/dist/engine/create-document-engine.d.ts +56 -0
  20. package/dist/engine/create-document-engine.js +218 -0
  21. package/dist/engine/errors.cjs +17 -0
  22. package/dist/engine/errors.d.cts +21 -0
  23. package/dist/engine/errors.d.ts +21 -0
  24. package/dist/engine/errors.js +16 -0
  25. package/dist/engine/event-emitter.cjs +24 -0
  26. package/dist/engine/event-emitter.d.cts +3 -0
  27. package/dist/engine/event-emitter.d.ts +3 -0
  28. package/dist/engine/event-emitter.js +24 -0
  29. package/dist/fabric/fabric-adapter.cjs +68 -0
  30. package/dist/fabric/fabric-adapter.js +67 -0
  31. package/dist/fabric/object-ids.cjs +11 -0
  32. package/dist/fabric/object-ids.js +10 -0
  33. package/dist/fabric/object-registry.cjs +21 -0
  34. package/dist/fabric/object-registry.d.cts +9 -0
  35. package/dist/fabric/object-registry.d.ts +9 -0
  36. package/dist/fabric/object-registry.js +21 -0
  37. package/dist/fabric/walk-objects.cjs +29 -0
  38. package/dist/fabric/walk-objects.js +27 -0
  39. package/dist/index.cjs +10 -0
  40. package/dist/index.d.cts +8 -0
  41. package/dist/index.d.ts +8 -0
  42. package/dist/index.js +5 -0
  43. package/package.json +91 -0
@@ -0,0 +1,21 @@
1
+ import { DocumentEngineError } from "../engine/errors.js";
2
+ import { classRegistry } from "fabric";
3
+ //#region src/fabric/object-registry.ts
4
+ function createObjectRegistry(initialDefinitions = []) {
5
+ const extraProperties = /* @__PURE__ */ new Set(["id"]);
6
+ function register(definition) {
7
+ const { fabricClass } = definition;
8
+ if (typeof fabricClass?.type !== "string" || fabricClass.type.length === 0) throw new DocumentEngineError("INVALID_CUSTOM_OBJECT", "A custom object class needs a static \"type\" string, for example: static type = \"Sticker\"");
9
+ if (typeof fabricClass.fromObject !== "function") throw new DocumentEngineError("INVALID_CUSTOM_OBJECT", `Custom object "${fabricClass.type}" must extend a Fabric class so it has a static fromObject method`);
10
+ classRegistry.setClass(fabricClass);
11
+ for (const property of definition.properties ?? []) extraProperties.add(property);
12
+ }
13
+ initialDefinitions.forEach(register);
14
+ return {
15
+ register,
16
+ propertiesToInclude: () => [...extraProperties],
17
+ findUnknownTypes: (types) => [...types].filter((type) => !classRegistry.has(type))
18
+ };
19
+ }
20
+ //#endregion
21
+ export { createObjectRegistry };
@@ -0,0 +1,29 @@
1
+ //#region src/fabric/walk-objects.ts
2
+ function childrenOf(object) {
3
+ const container = object;
4
+ return typeof container.getObjects === "function" ? container.getObjects() : [];
5
+ }
6
+ function walkObjects(roots, visit) {
7
+ const pending = [...roots].reverse();
8
+ while (pending.length > 0) {
9
+ const object = pending.pop();
10
+ visit(object);
11
+ const children = childrenOf(object);
12
+ for (let index = children.length - 1; index >= 0; index -= 1) pending.push(children[index]);
13
+ }
14
+ }
15
+ function collectSerializedTypes(objects) {
16
+ const types = /* @__PURE__ */ new Set();
17
+ const pending = [...objects];
18
+ while (pending.length > 0) {
19
+ const object = pending.pop();
20
+ types.add(object.type);
21
+ if (object.objects) pending.push(...object.objects);
22
+ if (object.clipPath) pending.push(object.clipPath);
23
+ }
24
+ return types;
25
+ }
26
+ //#endregion
27
+ exports.childrenOf = childrenOf;
28
+ exports.collectSerializedTypes = collectSerializedTypes;
29
+ exports.walkObjects = walkObjects;
@@ -0,0 +1,27 @@
1
+ //#region src/fabric/walk-objects.ts
2
+ function childrenOf(object) {
3
+ const container = object;
4
+ return typeof container.getObjects === "function" ? container.getObjects() : [];
5
+ }
6
+ function walkObjects(roots, visit) {
7
+ const pending = [...roots].reverse();
8
+ while (pending.length > 0) {
9
+ const object = pending.pop();
10
+ visit(object);
11
+ const children = childrenOf(object);
12
+ for (let index = children.length - 1; index >= 0; index -= 1) pending.push(children[index]);
13
+ }
14
+ }
15
+ function collectSerializedTypes(objects) {
16
+ const types = /* @__PURE__ */ new Set();
17
+ const pending = [...objects];
18
+ while (pending.length > 0) {
19
+ const object = pending.pop();
20
+ types.add(object.type);
21
+ if (object.objects) pending.push(...object.objects);
22
+ if (object.clipPath) pending.push(object.clipPath);
23
+ }
24
+ return types;
25
+ }
26
+ //#endregion
27
+ export { childrenOf, collectSerializedTypes, walkObjects };
package/dist/index.cjs ADDED
@@ -0,0 +1,10 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_document_format = require("./document/document-format.cjs");
3
+ const require_validate_document = require("./document/validate-document.cjs");
4
+ const require_errors = require("./engine/errors.cjs");
5
+ const require_create_document_engine = require("./engine/create-document-engine.cjs");
6
+ exports.CURRENT_SCHEMA_VERSION = require_document_format.CURRENT_SCHEMA_VERSION;
7
+ exports.DocumentEngineError = require_errors.DocumentEngineError;
8
+ exports.createDocumentEngine = require_create_document_engine.createDocumentEngine;
9
+ exports.isDocumentEngineError = require_errors.isDocumentEngineError;
10
+ exports.validateDocument = require_validate_document.validateDocument;
@@ -0,0 +1,8 @@
1
+ import { CURRENT_SCHEMA_VERSION, DocumentCanvas, DocumentInfo, FabricDocument, SerializedFabricObject } from "./document/document-format.cjs";
2
+ import { NewDocumentOptions } from "./document/create-document.cjs";
3
+ import { CustomObjectDefinition } from "./fabric/object-registry.cjs";
4
+ import { DocumentEngineError, DocumentEngineErrorDetails, DocumentErrorCode, DocumentIssue, isDocumentEngineError } from "./engine/errors.cjs";
5
+ import { Unsubscribe } from "./engine/event-emitter.cjs";
6
+ import { DocumentEngine, DocumentEngineEvents, DocumentEngineOptions, DocumentStorage, LoadOptions, createDocumentEngine } from "./engine/create-document-engine.cjs";
7
+ import { validateDocument } from "./document/validate-document.cjs";
8
+ export { CURRENT_SCHEMA_VERSION, type CustomObjectDefinition, type DocumentCanvas, type DocumentEngine, DocumentEngineError, type DocumentEngineErrorDetails, type DocumentEngineEvents, type DocumentEngineOptions, type DocumentErrorCode, type DocumentInfo, type DocumentIssue, type DocumentStorage, type FabricDocument, type LoadOptions, type NewDocumentOptions, type SerializedFabricObject, type Unsubscribe, createDocumentEngine, isDocumentEngineError, validateDocument };
@@ -0,0 +1,8 @@
1
+ import { CURRENT_SCHEMA_VERSION, DocumentCanvas, DocumentInfo, FabricDocument, SerializedFabricObject } from "./document/document-format.js";
2
+ import { NewDocumentOptions } from "./document/create-document.js";
3
+ import { CustomObjectDefinition } from "./fabric/object-registry.js";
4
+ import { DocumentEngineError, DocumentEngineErrorDetails, DocumentErrorCode, DocumentIssue, isDocumentEngineError } from "./engine/errors.js";
5
+ import { Unsubscribe } from "./engine/event-emitter.js";
6
+ import { DocumentEngine, DocumentEngineEvents, DocumentEngineOptions, DocumentStorage, LoadOptions, createDocumentEngine } from "./engine/create-document-engine.js";
7
+ import { validateDocument } from "./document/validate-document.js";
8
+ export { CURRENT_SCHEMA_VERSION, type CustomObjectDefinition, type DocumentCanvas, type DocumentEngine, DocumentEngineError, type DocumentEngineErrorDetails, type DocumentEngineEvents, type DocumentEngineOptions, type DocumentErrorCode, type DocumentInfo, type DocumentIssue, type DocumentStorage, type FabricDocument, type LoadOptions, type NewDocumentOptions, type SerializedFabricObject, type Unsubscribe, createDocumentEngine, isDocumentEngineError, validateDocument };
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import { CURRENT_SCHEMA_VERSION } from "./document/document-format.js";
2
+ import { validateDocument } from "./document/validate-document.js";
3
+ import { DocumentEngineError, isDocumentEngineError } from "./engine/errors.js";
4
+ import { createDocumentEngine } from "./engine/create-document-engine.js";
5
+ export { CURRENT_SCHEMA_VERSION, DocumentEngineError, createDocumentEngine, isDocumentEngineError, validateDocument };
package/package.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "name": "fabricjs-document-engine",
3
+ "version": "0.0.0",
4
+ "description": "Turn an existing Fabric.js canvas into a dependable editable document: stable object ids, versioned save and load, custom object registration and clear load errors.",
5
+ "keywords": [
6
+ "fabric",
7
+ "fabricjs",
8
+ "fabric.js",
9
+ "canvas",
10
+ "canvas editor",
11
+ "document",
12
+ "document engine",
13
+ "save",
14
+ "load",
15
+ "serialization",
16
+ "undo",
17
+ "redo",
18
+ "autosave",
19
+ "editor",
20
+ "typescript"
21
+ ],
22
+ "license": "MIT",
23
+ "author": "Sohail Khan (https://me.jscrate.dev)",
24
+ "type": "module",
25
+ "sideEffects": false,
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "main": "./dist/index.cjs",
32
+ "module": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "exports": {
35
+ ".": {
36
+ "import": {
37
+ "types": "./dist/index.d.ts",
38
+ "default": "./dist/index.js"
39
+ },
40
+ "require": {
41
+ "types": "./dist/index.d.cts",
42
+ "default": "./dist/index.cjs"
43
+ }
44
+ },
45
+ "./package.json": "./package.json"
46
+ },
47
+ "scripts": {
48
+ "build": "tsdown && npm run lint:publint && npm run lint:attw",
49
+ "dev": "tsdown --watch",
50
+ "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.tools.json",
51
+ "test": "vitest run",
52
+ "test:unit": "vitest run --project unit",
53
+ "test:browser": "vitest run --project browser-fabric7 --project browser-fabric6",
54
+ "test:watch": "vitest",
55
+ "lint:publint": "publint --strict",
56
+ "lint:attw": "attw --pack . --profile node16",
57
+ "prepublishOnly": "npm run build",
58
+ "release": "changeset publish"
59
+ },
60
+ "peerDependencies": {
61
+ "fabric": "^6.0.0 || ^7.0.0"
62
+ },
63
+ "publishConfig": {
64
+ "access": "public",
65
+ "registry": "https://registry.npmjs.org/"
66
+ },
67
+ "engines": {
68
+ "node": ">=18.0.0"
69
+ },
70
+ "repository": {
71
+ "type": "git",
72
+ "url": "git+https://github.com/re-sohail/fabricjs-document-engine.git"
73
+ },
74
+ "homepage": "https://github.com/re-sohail/fabricjs-document-engine#readme",
75
+ "bugs": {
76
+ "url": "https://github.com/re-sohail/fabricjs-document-engine/issues"
77
+ },
78
+ "devDependencies": {
79
+ "@arethetypeswrong/cli": "^0.18.5",
80
+ "@changesets/cli": "^3.0.3",
81
+ "@types/node": "^22.20.4",
82
+ "@vitest/browser-playwright": "^5.0.2",
83
+ "fabric": "^7.4.0",
84
+ "fabric6": "npm:fabric@^6.9.1",
85
+ "playwright": "^1.63.0",
86
+ "publint": "^0.3.24",
87
+ "tsdown": "^0.23.0",
88
+ "typescript": "^5.9.3",
89
+ "vitest": "^5.0.2"
90
+ }
91
+ }