sketchmark 2.1.1 → 2.1.3

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 (47) hide show
  1. package/bin/editor-ui.cjs +368 -123
  2. package/bin/editor-ui.d.ts +11 -0
  3. package/bin/vendor/mp4-muxer.LICENSE.txt +21 -0
  4. package/bin/vendor/mp4-muxer.mjs +1879 -0
  5. package/dist/src/browser-export.d.ts +10 -0
  6. package/dist/src/browser-export.js +220 -0
  7. package/package.json +59 -46
  8. package/dist/src/builders/index.d.ts +0 -64
  9. package/dist/src/builders/index.js +0 -212
  10. package/dist/src/compounds.d.ts +0 -13
  11. package/dist/src/compounds.js +0 -118
  12. package/dist/src/deck.d.ts +0 -4
  13. package/dist/src/deck.js +0 -91
  14. package/dist/src/export/index.d.ts +0 -8
  15. package/dist/src/export/index.js +0 -15
  16. package/dist/src/kernel.d.ts +0 -8
  17. package/dist/src/kernel.js +0 -68
  18. package/dist/src/motion.d.ts +0 -4
  19. package/dist/src/motion.js +0 -262
  20. package/dist/src/patch.d.ts +0 -5
  21. package/dist/src/patch.js +0 -72
  22. package/dist/src/player/index.d.ts +0 -68
  23. package/dist/src/player/index.js +0 -600
  24. package/dist/src/project.d.ts +0 -11
  25. package/dist/src/project.js +0 -107
  26. package/dist/src/render/raw-three.d.ts +0 -7
  27. package/dist/src/render/raw-three.js +0 -17
  28. package/dist/src/render/three-html.d.ts +0 -2
  29. package/dist/src/render/three-html.js +0 -257
  30. package/dist/src/render/three-preview-svg.d.ts +0 -3
  31. package/dist/src/render/three-preview-svg.js +0 -102
  32. package/dist/src/scenes.d.ts +0 -4
  33. package/dist/src/scenes.js +0 -26
  34. package/dist/src/sequences.d.ts +0 -43
  35. package/dist/src/sequences.js +0 -109
  36. package/dist/src/shapes/builtins.d.ts +0 -2
  37. package/dist/src/shapes/builtins.js +0 -393
  38. package/dist/src/shapes/common.d.ts +0 -9
  39. package/dist/src/shapes/common.js +0 -76
  40. package/dist/src/shapes/geometry.d.ts +0 -22
  41. package/dist/src/shapes/geometry.js +0 -166
  42. package/dist/src/shapes/index.d.ts +0 -2
  43. package/dist/src/shapes/index.js +0 -18
  44. package/dist/src/shapes/registry.d.ts +0 -8
  45. package/dist/src/shapes/registry.js +0 -31
  46. package/dist/src/shapes/types.d.ts +0 -32
  47. package/dist/src/shapes/types.js +0 -2
@@ -1,118 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compileCompounds = compileCompounds;
4
- const builders_1 = require("./builders");
5
- const utils_1 = require("./utils");
6
- function compileCompounds(input) {
7
- const document = (0, utils_1.clone)(input);
8
- const elements = compileElementList(document.elements ?? []);
9
- const scenes = {};
10
- for (const [sceneId, scene] of Object.entries(document.scenes ?? {})) {
11
- scenes[sceneId] = {
12
- ...scene,
13
- elements: compileElementList(scene.elements ?? [])
14
- };
15
- }
16
- return {
17
- ...document,
18
- elements,
19
- ...(Object.keys(scenes).length ? { scenes } : {})
20
- };
21
- }
22
- function compileElementList(elements) {
23
- return elements.flatMap((element) => compileElement(element));
24
- }
25
- function compileElement(element) {
26
- switch (element.type) {
27
- case "node":
28
- return (0, builders_1.node)({
29
- id: stringProp(element, "id"),
30
- label: stringProp(element, "label"),
31
- x: numberProp(element, "x"),
32
- y: numberProp(element, "y"),
33
- width: numberProp(element, "width"),
34
- height: numberProp(element, "height"),
35
- radius: optionalNumber(element, "radius"),
36
- fill: optionalString(element, "fill"),
37
- stroke: optionalString(element, "stroke"),
38
- strokeWidth: optionalNumber(element, "strokeWidth"),
39
- fontSize: optionalNumber(element, "fontSize"),
40
- textFill: optionalString(element, "textFill")
41
- });
42
- case "flow":
43
- return (0, builders_1.flow)({
44
- id: stringProp(element, "id"),
45
- from: element.from,
46
- to: element.to,
47
- stroke: optionalString(element, "stroke"),
48
- strokeWidth: optionalNumber(element, "strokeWidth"),
49
- label: optionalString(element, "label"),
50
- labelX: optionalNumber(element, "labelX"),
51
- labelY: optionalNumber(element, "labelY")
52
- });
53
- case "packet":
54
- return [
55
- (0, builders_1.packet)({
56
- id: stringProp(element, "id"),
57
- on: stringProp(element, "on"),
58
- radius: optionalNumber(element, "radius"),
59
- fill: optionalString(element, "fill"),
60
- progress: element.progress
61
- })
62
- ];
63
- case "callout":
64
- return (0, builders_1.callout)({
65
- id: stringProp(element, "id"),
66
- text: stringProp(element, "text"),
67
- x: numberProp(element, "x"),
68
- y: numberProp(element, "y"),
69
- width: numberProp(element, "width"),
70
- height: numberProp(element, "height"),
71
- target: element.target,
72
- fill: optionalString(element, "fill"),
73
- textFill: optionalString(element, "textFill"),
74
- stroke: optionalString(element, "stroke")
75
- });
76
- case "row":
77
- return (0, builders_1.row)({
78
- x: numberProp(element, "x"),
79
- y: numberProp(element, "y"),
80
- gap: optionalNumber(element, "gap"),
81
- children: (element.children ?? []).map((child) => compileElement(child))
82
- });
83
- case "column":
84
- return (0, builders_1.column)({
85
- x: numberProp(element, "x"),
86
- y: numberProp(element, "y"),
87
- gap: optionalNumber(element, "gap"),
88
- children: (element.children ?? []).map((child) => compileElement(child))
89
- });
90
- default: {
91
- const next = (0, utils_1.clone)(element);
92
- if (next.type === "group" && Array.isArray(next.children)) {
93
- next.children = compileElementList(next.children);
94
- }
95
- return [next];
96
- }
97
- }
98
- }
99
- function stringProp(element, key) {
100
- const value = element[key];
101
- if (typeof value !== "string" || !value)
102
- throw new Error(`Compound '${element.type}' requires string '${key}'.`);
103
- return value;
104
- }
105
- function optionalString(element, key) {
106
- const value = element[key];
107
- return typeof value === "string" ? value : undefined;
108
- }
109
- function numberProp(element, key) {
110
- const value = element[key];
111
- if (typeof value !== "number" || !Number.isFinite(value))
112
- throw new Error(`Compound '${element.type}' requires number '${key}'.`);
113
- return value;
114
- }
115
- function optionalNumber(element, key) {
116
- const value = element[key];
117
- return typeof value === "number" && Number.isFinite(value) ? value : undefined;
118
- }
@@ -1,4 +0,0 @@
1
- import type { ResolvedVisualDocument, VisualDocument } from "./types";
2
- export declare function documentForDeckStep(document: VisualDocument, sceneId: string, stepIndex: number): VisualDocument;
3
- export declare function resolvedFrameForDeckStep(document: VisualDocument, sceneId: string, stepIndex: number, time?: number): ResolvedVisualDocument;
4
- export declare function renderDeckToHtml(document: VisualDocument, sceneId: string): string;
package/dist/src/deck.js DELETED
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.documentForDeckStep = documentForDeckStep;
4
- exports.resolvedFrameForDeckStep = resolvedFrameForDeckStep;
5
- exports.renderDeckToHtml = renderDeckToHtml;
6
- const scenes_1 = require("./scenes");
7
- const normalize_1 = require("./normalize");
8
- const utils_1 = require("./utils");
9
- const svg_1 = require("./render/svg");
10
- function documentForDeckStep(document, sceneId, stepIndex) {
11
- const scene = document.scenes?.[sceneId];
12
- if (!scene)
13
- throw new Error(`Unknown scene '${sceneId}'.`);
14
- const frame = (0, scenes_1.documentForScene)(document, sceneId);
15
- const activeSteps = (scene.steps ?? []).slice(0, Math.max(0, stepIndex + 1));
16
- if (!activeSteps.length)
17
- return frame;
18
- const hidden = new Set();
19
- const shown = new Set();
20
- for (const step of activeSteps) {
21
- for (const id of step.hide ?? [])
22
- hidden.add(id);
23
- for (const id of step.show ?? []) {
24
- hidden.delete(id);
25
- shown.add(id);
26
- }
27
- }
28
- frame.elements = applyDeckVisibility(frame.elements ?? [], hidden, shown);
29
- return frame;
30
- }
31
- function resolvedFrameForDeckStep(document, sceneId, stepIndex, time = 0) {
32
- return (0, normalize_1.resolveVisualFrame)(documentForDeckStep(document, sceneId, stepIndex), time);
33
- }
34
- function renderDeckToHtml(document, sceneId) {
35
- const scene = document.scenes?.[sceneId];
36
- if (!scene)
37
- throw new Error(`Unknown scene '${sceneId}'.`);
38
- const steps = scene.steps ?? [];
39
- const frameCount = Math.max(1, steps.length + 1);
40
- const frames = Array.from({ length: frameCount }, (_, index) => {
41
- const stepIndex = index - 1;
42
- const frame = stepIndex < 0 ? (0, scenes_1.documentForScene)(document, sceneId) : documentForDeckStep(document, sceneId, stepIndex);
43
- return (0, svg_1.renderToSvg)(frame);
44
- });
45
- const labels = ["Base", ...steps.map((step) => step.id)];
46
- return `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escapeHtml(sceneId)} Deck</title><style>
47
- html,body{margin:0;width:100%;height:100%;background:#111827;color:#e5e7eb;font-family:Inter,Arial,sans-serif}
48
- body{display:grid;grid-template-rows:1fr auto}
49
- main{display:grid;place-items:center;padding:24px}
50
- #stage svg{max-width:min(100vw,${document.canvas.width}px);max-height:calc(100vh - 112px);background:white;box-shadow:0 20px 50px rgba(0,0,0,.35)}
51
- footer{display:flex;align-items:center;gap:12px;padding:12px 16px;background:#020617;border-top:1px solid #1f2937}
52
- button{border:0;border-radius:6px;padding:8px 12px;background:#2563eb;color:#fff;font-weight:700}
53
- button:disabled{opacity:.45}
54
- code{color:#93c5fd}
55
- </style></head><body><main><div id="stage"></div></main><footer><button id="prev">Prev</button><button id="next">Next</button><span id="count"></span><code id="label"></code></footer><script>
56
- const frames=${JSON.stringify(frames)};
57
- const labels=${JSON.stringify(labels)};
58
- let index=0;
59
- const stage=document.getElementById("stage");
60
- const prev=document.getElementById("prev");
61
- const next=document.getElementById("next");
62
- const count=document.getElementById("count");
63
- const label=document.getElementById("label");
64
- function show(){
65
- stage.innerHTML=frames[index];
66
- prev.disabled=index===0;
67
- next.disabled=index===frames.length-1;
68
- count.textContent=(index+1)+" / "+frames.length;
69
- label.textContent=labels[index] || "";
70
- }
71
- prev.addEventListener("click",()=>{ index=Math.max(0,index-1); show(); });
72
- next.addEventListener("click",()=>{ index=Math.min(frames.length-1,index+1); show(); });
73
- window.addEventListener("keydown",(event)=>{ if(event.key==="ArrowRight") next.click(); if(event.key==="ArrowLeft") prev.click(); });
74
- show();
75
- </script></body></html>`;
76
- }
77
- function applyDeckVisibility(elements, hidden, shown) {
78
- return elements.map((element) => {
79
- const next = (0, utils_1.clone)(element);
80
- if (next.id && hidden.has(next.id))
81
- next.opacity = 0;
82
- if (next.id && shown.has(next.id) && next.opacity === 0)
83
- next.opacity = 1;
84
- if (next.type === "group" && Array.isArray(next.children))
85
- next.children = applyDeckVisibility(next.children, hidden, shown);
86
- return next;
87
- });
88
- }
89
- function escapeHtml(value) {
90
- return String(value).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
91
- }
@@ -1,8 +0,0 @@
1
- import type { ExportOptions, VisualDocument } from "../types";
2
- export interface ExportResult {
3
- format: string;
4
- content: string;
5
- mimeType: string;
6
- warnings?: string[];
7
- }
8
- export declare function exportVisual(document: VisualDocument, options?: ExportOptions): ExportResult;
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.exportVisual = exportVisual;
4
- const html_1 = require("../render/html");
5
- const svg_1 = require("../render/svg");
6
- function exportVisual(document, options = {}) {
7
- const format = options.format ?? "svg";
8
- if (format === "svg") {
9
- return { format, content: (0, svg_1.renderToSvg)(document, options), mimeType: "image/svg+xml" };
10
- }
11
- if (format === "html") {
12
- return { format, content: (0, html_1.renderToHtml)(document, options), mimeType: "text/html" };
13
- }
14
- throw new Error(`${format.toUpperCase()} export is reserved for the advanced renderer/export adapter. The primitive JSON core currently exports svg and html directly.`);
15
- }
@@ -1,8 +0,0 @@
1
- import type { KernelVisualDocument, ResolvedVisualDocument, ValidationResult, VisualDocument } from "./types";
2
- import { type NormalizeOptions } from "./normalize";
3
- export interface LowerOptions extends NormalizeOptions {
4
- }
5
- export declare function lowerVisualDocument(document: VisualDocument, options?: LowerOptions): KernelVisualDocument;
6
- export declare function resolveKernelFrame(document: VisualDocument, time?: number): KernelVisualDocument;
7
- export declare function lowerResolvedVisualDocument(document: ResolvedVisualDocument): KernelVisualDocument;
8
- export declare function validateKernelVisualDocument(document: KernelVisualDocument): ValidationResult;
@@ -1,68 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.lowerVisualDocument = lowerVisualDocument;
4
- exports.resolveKernelFrame = resolveKernelFrame;
5
- exports.lowerResolvedVisualDocument = lowerResolvedVisualDocument;
6
- exports.validateKernelVisualDocument = validateKernelVisualDocument;
7
- const normalize_1 = require("./normalize");
8
- const utils_1 = require("./utils");
9
- const shapes_1 = require("./shapes");
10
- const KERNEL_2D_TYPES = new Set(["group", "path", "text", "image", "point"]);
11
- const KERNEL_3D_TYPES = new Set(["group3d", "mesh3d", "line3d", "text3d", "point3d", "light"]);
12
- function lowerVisualDocument(document, options = {}) {
13
- return lowerResolvedVisualDocument((0, normalize_1.normalizeVisualDocument)(document, options));
14
- }
15
- function resolveKernelFrame(document, time = 0) {
16
- return lowerResolvedVisualDocument((0, normalize_1.resolveVisualFrame)(document, time));
17
- }
18
- function lowerResolvedVisualDocument(document) {
19
- return {
20
- version: document.version,
21
- canvas: (0, utils_1.clone)(document.canvas),
22
- imports: cloneOptional(document.imports),
23
- assets: cloneOptional(document.assets),
24
- exports: cloneOptional(document.exports),
25
- elements: lowerElements(document.elements ?? [])
26
- };
27
- }
28
- function validateKernelVisualDocument(document) {
29
- const issues = [];
30
- const visit = (element, path) => {
31
- if (!KERNEL_2D_TYPES.has(element.type) && !KERNEL_3D_TYPES.has(element.type)) {
32
- issues.push({ path, code: "unsupported_kernel_type", message: `Kernel element type '${element.type}' is not supported.` });
33
- return;
34
- }
35
- if (element.type === "path" && typeof element.d !== "string") {
36
- issues.push({ path, code: "missing_kernel_path", message: "Kernel path requires string 'd'." });
37
- }
38
- if (element.type === "text" && (!isFiniteNumber(element.x) || !isFiniteNumber(element.y))) {
39
- issues.push({ path, code: "missing_kernel_text_position", message: "Kernel text requires numeric x and y." });
40
- }
41
- if (element.type === "image" && (!isFiniteNumber(element.x) || !isFiniteNumber(element.y) || !isFiniteNumber(element.width) || !isFiniteNumber(element.height))) {
42
- issues.push({ path, code: "missing_kernel_image_box", message: "Kernel image requires numeric x, y, width, and height." });
43
- }
44
- if (element.type === "point" && (!isFiniteNumber(element.x) || !isFiniteNumber(element.y))) {
45
- issues.push({ path, code: "missing_kernel_point", message: "Kernel point requires numeric x and y." });
46
- }
47
- if (element.type === "mesh3d" && (!Array.isArray(element.vertices) || !Array.isArray(element.indices))) {
48
- issues.push({ path, code: "missing_kernel_mesh", message: "Kernel mesh3d requires vertices and indices." });
49
- }
50
- if ((element.type === "group" || element.type === "group3d") && Array.isArray(element.children)) {
51
- element.children.forEach((child, index) => visit(child, `${path}/children/${index}`));
52
- }
53
- };
54
- (document.elements ?? []).forEach((element, index) => visit(element, `/elements/${index}`));
55
- return { ok: issues.length === 0, issues, warnings: [] };
56
- }
57
- function lowerElements(elements) {
58
- return elements.flatMap((element) => lowerElement(element));
59
- }
60
- function lowerElement(element) {
61
- return (0, shapes_1.lowerAuthoringElement)(element, { lowerElements });
62
- }
63
- function cloneOptional(value) {
64
- return value === undefined ? undefined : (0, utils_1.clone)(value);
65
- }
66
- function isFiniteNumber(value) {
67
- return typeof value === "number" && Number.isFinite(value);
68
- }
@@ -1,4 +0,0 @@
1
- import type { MotionDriver, MotionExpressionInput, MotionValue, VisualDocument, VisualElement } from "./types";
2
- export declare function applyMotionDrivers(document: VisualDocument, time: number): VisualDocument;
3
- export declare function evaluateDriver(driver: MotionDriver, elements: VisualElement[], time: number): MotionValue | undefined;
4
- export declare function evaluateExpression(input: MotionExpressionInput, elements: VisualElement[], time: number): MotionValue;
@@ -1,262 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyMotionDrivers = applyMotionDrivers;
4
- exports.evaluateDriver = evaluateDriver;
5
- exports.evaluateExpression = evaluateExpression;
6
- const path_sampling_1 = require("./path-sampling");
7
- const shapes_1 = require("./shapes");
8
- const utils_1 = require("./utils");
9
- const utils_2 = require("./utils");
10
- function applyMotionDrivers(document, time) {
11
- const next = (0, utils_1.clone)(document);
12
- const drivers = next.motion?.drivers ?? [];
13
- if (!drivers.length)
14
- return next;
15
- const elements = next.elements ?? [];
16
- for (const driver of drivers) {
17
- const value = evaluateDriver(driver, elements, time);
18
- if (value !== undefined)
19
- applyDriverValue(elements, driver.target, value);
20
- }
21
- return next;
22
- }
23
- function evaluateDriver(driver, elements, time) {
24
- if (driver.expr !== undefined)
25
- return evaluateExpression(driver.expr, elements, time);
26
- if (Array.isArray(driver.keyframes))
27
- return interpolateKeyframes(driver.keyframes, time, driver.ease);
28
- return undefined;
29
- }
30
- function evaluateExpression(input, elements, time) {
31
- if (typeof input === "number" || typeof input === "string" || isMotionPoint(input))
32
- return (0, utils_1.clone)(input);
33
- if (!input || typeof input !== "object" || Array.isArray(input))
34
- return 0;
35
- switch (input.op) {
36
- case "time":
37
- return time;
38
- case "prop":
39
- return propertyValue(elements, input.target, input.property);
40
- case "point": {
41
- const [x, y] = input.args;
42
- return [numberValue(evaluateExpression(x, elements, time)), numberValue(evaluateExpression(y, elements, time))];
43
- }
44
- case "add":
45
- return input.args.reduce((total, item) => addValues(total, evaluateExpression(item, elements, time)), 0);
46
- case "sub": {
47
- const [first, ...rest] = input.args;
48
- return rest.reduce((total, item) => subValues(total, evaluateExpression(item, elements, time)), evaluateExpression(first ?? 0, elements, time));
49
- }
50
- case "mul":
51
- return input.args.reduce((total, item) => mulValues(total, evaluateExpression(item, elements, time)), 1);
52
- case "div": {
53
- const [first, ...rest] = input.args;
54
- return rest.reduce((total, item) => divValues(total, evaluateExpression(item, elements, time)), evaluateExpression(first ?? 0, elements, time));
55
- }
56
- case "mod": {
57
- const [left, right] = input.args;
58
- const divisor = numberValue(evaluateExpression(right ?? 1, elements, time));
59
- return divisor === 0 ? 0 : numberValue(evaluateExpression(left ?? 0, elements, time)) % divisor;
60
- }
61
- case "min":
62
- return Math.min(...input.args.map((item) => numberValue(evaluateExpression(item, elements, time))));
63
- case "max":
64
- return Math.max(...input.args.map((item) => numberValue(evaluateExpression(item, elements, time))));
65
- case "sin":
66
- return Math.sin(numberValue(evaluateExpression(input.arg, elements, time)));
67
- case "cos":
68
- return Math.cos(numberValue(evaluateExpression(input.arg, elements, time)));
69
- case "abs":
70
- return absValue(evaluateExpression(input.arg, elements, time));
71
- case "clamp":
72
- return Math.max(numberValue(evaluateExpression(input.min, elements, time)), Math.min(numberValue(evaluateExpression(input.max, elements, time)), numberValue(evaluateExpression(input.value, elements, time))));
73
- case "lerp":
74
- return interpolateValue(evaluateExpression(input.from, elements, time), evaluateExpression(input.to, elements, time), numberValue(evaluateExpression(input.t, elements, time)));
75
- case "bbox": {
76
- const target = findElement(elements, input.target);
77
- const box = target ? (0, utils_1.elementBox)(target) : undefined;
78
- return box ? box[input.property] : 0;
79
- }
80
- case "anchor": {
81
- const target = findElement(elements, input.target);
82
- const box = target ? (0, utils_1.elementBox)(target) : undefined;
83
- const point = box ? (0, utils_1.anchorPoint)(box, input.anchor) : [0, 0];
84
- if (input.offset === undefined)
85
- return point;
86
- return point2Value(addValues(point, evaluateExpression(input.offset, elements, time)));
87
- }
88
- case "pointAt":
89
- return pointOnTarget(elements, input.target, numberValue(evaluateExpression(input.progress, elements, time))) ?? [0, 0];
90
- case "pathLength": {
91
- const path = firstKernelPathForTarget(elements, input.target);
92
- return path ? (0, path_sampling_1.pathLength)(path.d) : 0;
93
- }
94
- default:
95
- return 0;
96
- }
97
- }
98
- function interpolateKeyframes(keyframes, time, ease) {
99
- const frames = keyframes
100
- .filter((frame) => Array.isArray(frame) && (0, utils_1.isFiniteNumber)(frame[0]))
101
- .slice()
102
- .sort((left, right) => left[0] - right[0]);
103
- if (!frames.length)
104
- return undefined;
105
- if (time <= frames[0][0])
106
- return (0, utils_1.clone)(frames[0][1]);
107
- for (let index = 1; index < frames.length; index += 1) {
108
- const prev = frames[index - 1];
109
- const next = frames[index];
110
- if (time <= next[0]) {
111
- const span = Math.max(0.000001, next[0] - prev[0]);
112
- return interpolateValue(prev[1], next[1], (0, utils_2.easing)(ease, (time - prev[0]) / span));
113
- }
114
- }
115
- return (0, utils_1.clone)(frames[frames.length - 1][1]);
116
- }
117
- function interpolateValue(from, to, t) {
118
- const eased = Math.max(0, Math.min(1, t));
119
- if (typeof from === "number" && typeof to === "number")
120
- return from + (to - from) * eased;
121
- if (isMotionPoint(from) && isMotionPoint(to))
122
- return from.map((value, index) => value + (Number(to[index]) - value) * eased);
123
- if (typeof from === "string" && typeof to === "string") {
124
- const fromColor = parseHexColor(from);
125
- const toColor = parseHexColor(to);
126
- if (fromColor && toColor) {
127
- return `#${hex(Math.round(fromColor[0] + (toColor[0] - fromColor[0]) * eased))}${hex(Math.round(fromColor[1] + (toColor[1] - fromColor[1]) * eased))}${hex(Math.round(fromColor[2] + (toColor[2] - fromColor[2]) * eased))}`;
128
- }
129
- }
130
- return eased < 1 ? (0, utils_1.clone)(from) : (0, utils_1.clone)(to);
131
- }
132
- function applyDriverValue(elements, targetPath, value) {
133
- const separator = targetPath.indexOf(".");
134
- if (separator <= 0)
135
- return;
136
- const id = targetPath.slice(0, separator);
137
- const property = targetPath.slice(separator + 1);
138
- const target = findElement(elements, id);
139
- if (!target)
140
- return;
141
- const record = target;
142
- if (property === "position" && (0, utils_1.isPoint2)(value)) {
143
- if ("cx" in record || target.type === "circle" || target.type === "ellipse" || target.type === "arc") {
144
- record.cx = value[0];
145
- record.cy = value[1];
146
- }
147
- else {
148
- record.x = value[0];
149
- record.y = value[1];
150
- }
151
- return;
152
- }
153
- record[property] = (0, utils_1.clone)(value);
154
- }
155
- function propertyValue(elements, id, property) {
156
- const target = findElement(elements, id);
157
- if (!target)
158
- return 0;
159
- const record = target;
160
- if (property === "position") {
161
- if ((0, utils_1.isFiniteNumber)(record.cx) && (0, utils_1.isFiniteNumber)(record.cy))
162
- return [record.cx, record.cy];
163
- if ((0, utils_1.isFiniteNumber)(record.x) && (0, utils_1.isFiniteNumber)(record.y))
164
- return [record.x, record.y];
165
- }
166
- const value = record[property];
167
- return typeof value === "number" || typeof value === "string" || isMotionPoint(value) ? (0, utils_1.clone)(value) : 0;
168
- }
169
- function pointOnTarget(elements, id, progress) {
170
- const path = firstKernelPathForTarget(elements, id);
171
- return path ? (0, path_sampling_1.pointOnPath)(path.d, progress) : undefined;
172
- }
173
- function firstKernelPathForTarget(elements, id) {
174
- const target = findElement(elements, id);
175
- if (!target)
176
- return undefined;
177
- const definition = (0, shapes_1.getInternalShapeDefinition)(target.type);
178
- if (!definition?.pathQueryable)
179
- return undefined;
180
- const lowered = lowerFollowTarget(target);
181
- return firstKernelPath(lowered);
182
- }
183
- function lowerFollowTarget(target) {
184
- const lowerElements = (items) => items.flatMap((item) => (0, shapes_1.lowerAuthoringElement)(item, { lowerElements }));
185
- return lowerElements([target]);
186
- }
187
- function firstKernelPath(elements) {
188
- for (const element of elements) {
189
- if (element.type === "path")
190
- return element;
191
- if ((element.type === "group" || element.type === "group3d") && Array.isArray(element.children)) {
192
- const nested = firstKernelPath(element.children);
193
- if (nested)
194
- return nested;
195
- }
196
- }
197
- return undefined;
198
- }
199
- function findElement(elements, id) {
200
- return (0, utils_1.flattenElements)(elements).find((element) => element.id === id);
201
- }
202
- function numberValue(value) {
203
- return typeof value === "number" && Number.isFinite(value) ? value : 0;
204
- }
205
- function addValues(left, right) {
206
- if (isMotionPoint(left) || isMotionPoint(right))
207
- return combinePointValues(left, right, (a, b) => a + b);
208
- return numberValue(left) + numberValue(right);
209
- }
210
- function subValues(left, right) {
211
- if (isMotionPoint(left) || isMotionPoint(right))
212
- return combinePointValues(left, right, (a, b) => a - b);
213
- return numberValue(left) - numberValue(right);
214
- }
215
- function mulValues(left, right) {
216
- if (isMotionPoint(left) || isMotionPoint(right))
217
- return combinePointValues(left, right, (a, b) => a * b);
218
- return numberValue(left) * numberValue(right);
219
- }
220
- function divValues(left, right) {
221
- if (isMotionPoint(left) || isMotionPoint(right))
222
- return combinePointValues(left, right, (a, b) => a / safeDenominator(b));
223
- return numberValue(left) / safeDenominator(numberValue(right));
224
- }
225
- function absValue(value) {
226
- if (isMotionPoint(value))
227
- return value.map((item) => Math.abs(item));
228
- return Math.abs(numberValue(value));
229
- }
230
- function combinePointValues(left, right, fn) {
231
- const length = (isMotionPoint(left) && left.length === 3) || (isMotionPoint(right) && right.length === 3) ? 3 : 2;
232
- const values = Array.from({ length }, (_, index) => fn(componentValue(left, index), componentValue(right, index)));
233
- return values;
234
- }
235
- function componentValue(value, index) {
236
- if (isMotionPoint(value))
237
- return (0, utils_1.isFiniteNumber)(value[index]) ? value[index] : 0;
238
- return numberValue(value);
239
- }
240
- function point2Value(value) {
241
- if (isMotionPoint(value))
242
- return [value[0], value[1]];
243
- const scalar = numberValue(value);
244
- return [scalar, scalar];
245
- }
246
- function safeDenominator(value) {
247
- if (Math.abs(value) >= 0.000001)
248
- return value;
249
- return value < 0 ? -0.000001 : 0.000001;
250
- }
251
- function isMotionPoint(value) {
252
- return Array.isArray(value) && (value.length === 2 || value.length === 3) && value.every((item) => typeof item === "number" && Number.isFinite(item));
253
- }
254
- function parseHexColor(value) {
255
- const match = /^#([0-9a-f]{6})$/i.exec(value.trim());
256
- if (!match)
257
- return undefined;
258
- return [parseInt(match[1].slice(0, 2), 16), parseInt(match[1].slice(2, 4), 16), parseInt(match[1].slice(4, 6), 16)];
259
- }
260
- function hex(value) {
261
- return value.toString(16).padStart(2, "0");
262
- }
@@ -1,5 +0,0 @@
1
- import type { VisualDocument, VisualPatchOperation } from "./types";
2
- export interface PatchResult {
3
- document: VisualDocument;
4
- }
5
- export declare function applyVisualPatch(document: VisualDocument, operations: VisualPatchOperation | VisualPatchOperation[]): PatchResult;
package/dist/src/patch.js DELETED
@@ -1,72 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyVisualPatch = applyVisualPatch;
4
- const utils_1 = require("./utils");
5
- const validate_1 = require("./validate");
6
- function applyVisualPatch(document, operations) {
7
- const ops = Array.isArray(operations) ? operations : [operations];
8
- const next = (0, utils_1.clone)(document);
9
- next.elements = next.elements ?? [];
10
- for (const operation of ops) {
11
- applyOperation(next, operation);
12
- }
13
- const result = (0, validate_1.validateVisualDocument)(next);
14
- if (!result.ok) {
15
- const first = result.issues[0];
16
- throw new Error(first ? `${first.path}: ${first.message}` : "Patch produced an invalid document.");
17
- }
18
- return { document: next };
19
- }
20
- function applyOperation(document, operation) {
21
- const elements = document.elements ?? [];
22
- if (operation.op === "add") {
23
- if (operation.element.id && findElement(elements, operation.element.id))
24
- throw new Error(`Element '${operation.element.id}' already exists.`);
25
- const index = clampIndex(operation.index ?? elements.length, elements.length);
26
- elements.splice(index, 0, (0, utils_1.clone)(operation.element));
27
- return;
28
- }
29
- const target = "id" in operation ? findElement(elements, operation.id) : undefined;
30
- if (!target)
31
- throw new Error(`Element '${"id" in operation ? operation.id : ""}' was not found.`);
32
- if (operation.op === "update") {
33
- Object.assign(target.element, (0, utils_1.clone)(operation.set));
34
- return;
35
- }
36
- if (operation.op === "remove") {
37
- target.parent.splice(target.index, 1);
38
- return;
39
- }
40
- if (operation.op === "replace") {
41
- target.parent[target.index] = (0, utils_1.clone)(operation.element);
42
- return;
43
- }
44
- if (operation.op === "move") {
45
- const record = target.element;
46
- for (const key of ["x", "y", "cx", "cy"]) {
47
- if (typeof operation[key] === "number")
48
- record[key] = operation[key];
49
- }
50
- return;
51
- }
52
- if (operation.op === "reorder") {
53
- const [item] = target.parent.splice(target.index, 1);
54
- target.parent.splice(clampIndex(operation.index, target.parent.length), 0, item);
55
- }
56
- }
57
- function findElement(elements, id) {
58
- for (let index = 0; index < elements.length; index += 1) {
59
- const element = elements[index];
60
- if (element.id === id)
61
- return { element, parent: elements, index };
62
- if (element.type === "group" && Array.isArray(element.children)) {
63
- const found = findElement(element.children, id);
64
- if (found)
65
- return found;
66
- }
67
- }
68
- return undefined;
69
- }
70
- function clampIndex(index, length) {
71
- return Math.max(0, Math.min(length, Math.floor(index)));
72
- }