@pairdraw/scene-graph 0.19.1 → 0.20.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/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/spec.d.ts +6 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +62 -5
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/canvas-layout.js +66 -0
- package/dist/canvas-layout.js.map +1 -0
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/variants.d.ts +12 -1
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +30 -1
- package/dist/components/variants.js.map +1 -1
- package/dist/constants.d.ts +25 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +94 -1
- package/dist/constants.js.map +1 -1
- package/dist/coordinate.js +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +2 -2
- package/dist/css/index.js +2 -2
- package/dist/css/text.d.ts +22 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +81 -1
- package/dist/css/text.js.map +1 -1
- package/dist/design-memory/index.d.ts +3 -0
- package/dist/design-memory/index.js +3 -0
- package/dist/design-memory/model.d.ts +33 -0
- package/dist/design-memory/model.d.ts.map +1 -0
- package/dist/design-memory/model.js +142 -0
- package/dist/design-memory/model.js.map +1 -0
- package/dist/design-memory/schema.d.ts +76 -0
- package/dist/design-memory/schema.d.ts.map +1 -0
- package/dist/design-memory/schema.js +72 -0
- package/dist/design-memory/schema.js.map +1 -0
- package/dist/effects.d.ts +7 -4
- package/dist/effects.d.ts.map +1 -1
- package/dist/effects.js +9 -4
- package/dist/effects.js.map +1 -1
- package/dist/empty-values.js +93 -0
- package/dist/empty-values.js.map +1 -0
- package/dist/fields/components.d.ts +9 -0
- package/dist/fields/components.d.ts.map +1 -0
- package/dist/fields/components.js +24 -0
- package/dist/fields/components.js.map +1 -0
- package/dist/fields/index.d.ts +3 -2
- package/dist/fields/index.js +3 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +10 -1
- package/dist/fields/layout.js.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +2 -2
- package/dist/generated/shape-with-text.d.ts +7 -2
- package/dist/generated/shape-with-text.d.ts.map +1 -1
- package/dist/generated/shape-with-text.js +20 -3
- package/dist/generated/shape-with-text.js.map +1 -1
- package/dist/graph.d.ts +8 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +36 -7
- package/dist/graph.js.map +1 -1
- package/dist/grid-values.d.ts +10 -0
- package/dist/grid-values.d.ts.map +1 -0
- package/dist/grid-values.js +53 -0
- package/dist/grid-values.js.map +1 -0
- package/dist/group-bounds.d.ts +4 -1
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +11 -1
- package/dist/group-bounds.js.map +1 -1
- package/dist/hit-test/drop.d.ts +20 -0
- package/dist/hit-test/drop.d.ts.map +1 -0
- package/dist/hit-test/drop.js +54 -0
- package/dist/hit-test/drop.js.map +1 -0
- package/dist/hit-test/geometry.js +86 -0
- package/dist/hit-test/geometry.js.map +1 -0
- package/dist/hit-test/masks.d.ts +14 -0
- package/dist/hit-test/masks.d.ts.map +1 -0
- package/dist/hit-test/masks.js +41 -0
- package/dist/hit-test/masks.js.map +1 -0
- package/dist/hit-test/paint.d.ts +6 -0
- package/dist/hit-test/paint.d.ts.map +1 -0
- package/dist/hit-test/paint.js +19 -0
- package/dist/hit-test/paint.js.map +1 -0
- package/dist/hit-test.d.ts +4 -15
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +33 -178
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +2 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +2 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +1 -1
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/source-walk.d.ts +12 -0
- package/dist/instances/source-walk.d.ts.map +1 -0
- package/dist/node-defaults.d.ts +21 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +63 -50
- package/dist/node-defaults.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +75 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +18 -1
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +2 -0
- package/dist/preview.js.map +1 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +3 -6
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +28 -4
- package/dist/scaling/node.js.map +1 -1
- package/dist/shared-styles.d.ts +3 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +7 -1
- package/dist/shared-styles.js.map +1 -1
- package/dist/text/path.js +11 -0
- package/dist/text/path.js.map +1 -0
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +5 -5
- package/dist/text/picture.js.map +1 -1
- package/dist/token-sources/document.d.ts +26 -0
- package/dist/token-sources/document.d.ts.map +1 -0
- package/dist/token-sources/document.js +80 -0
- package/dist/token-sources/document.js.map +1 -0
- package/dist/token-sources/index.d.ts +3 -0
- package/dist/token-sources/index.js +3 -0
- package/dist/token-sources/schema.d.ts +43 -0
- package/dist/token-sources/schema.d.ts.map +1 -0
- package/dist/token-sources/schema.js +50 -0
- package/dist/token-sources/schema.js.map +1 -0
- package/dist/transfer/copied.d.ts +14 -0
- package/dist/transfer/copied.d.ts.map +1 -0
- package/dist/transfer/copied.js +52 -0
- package/dist/transfer/copied.js.map +1 -0
- package/dist/transfer/index.d.ts +2 -1
- package/dist/transfer/index.js +2 -1
- package/dist/transfer/nodes.js +7 -0
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +104 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +6 -0
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +13 -0
- package/dist/undo.js.map +1 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js.map +1 -1
- package/dist/variations/copy.d.ts +16 -0
- package/dist/variations/copy.d.ts.map +1 -0
- package/dist/variations/copy.js +78 -0
- package/dist/variations/copy.js.map +1 -0
- package/dist/variations/index.d.ts +6 -0
- package/dist/variations/index.js +6 -0
- package/dist/variations/match.d.ts +23 -0
- package/dist/variations/match.d.ts.map +1 -0
- package/dist/variations/match.js +80 -0
- package/dist/variations/match.js.map +1 -0
- package/dist/variations/model.d.ts +39 -0
- package/dist/variations/model.d.ts.map +1 -0
- package/dist/variations/model.js +90 -0
- package/dist/variations/model.js.map +1 -0
- package/dist/variations/pick.d.ts +43 -0
- package/dist/variations/pick.d.ts.map +1 -0
- package/dist/variations/pick.js +211 -0
- package/dist/variations/pick.js.map +1 -0
- package/dist/variations/schema.d.ts +27 -0
- package/dist/variations/schema.d.ts.map +1 -0
- package/dist/variations/schema.js +27 -0
- package/dist/variations/schema.js.map +1 -0
- package/package.json +21 -1
- package/src/behaviours/spec.ts +94 -4
- package/src/canvas-layout.ts +76 -0
- package/src/components/variants.ts +39 -0
- package/src/constants.ts +75 -0
- package/src/copy.ts +20 -1
- package/src/css/index.ts +12 -1
- package/src/css/text.ts +86 -0
- package/src/design-memory/index.ts +2 -0
- package/src/design-memory/model.ts +143 -0
- package/src/design-memory/schema.ts +84 -0
- package/src/effects.ts +16 -7
- package/src/empty-values.ts +101 -0
- package/src/fields/components.ts +21 -0
- package/src/fields/index.ts +1 -0
- package/src/fields/layout.ts +10 -0
- package/src/generated/shape-with-text.ts +28 -2
- package/src/graph.ts +48 -6
- package/src/grid-values.ts +47 -0
- package/src/group-bounds.ts +13 -0
- package/src/hit-test/drop.ts +90 -0
- package/src/hit-test/geometry.ts +131 -0
- package/src/hit-test/masks.ts +57 -0
- package/src/hit-test/paint.ts +24 -0
- package/src/hit-test.ts +78 -291
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +1 -0
- package/src/node-defaults.ts +71 -52
- package/src/plugin-data/fields.ts +18 -1
- package/src/preview.ts +2 -0
- package/src/resize.ts +3 -14
- package/src/scaling/node.ts +34 -5
- package/src/shared-styles.ts +8 -0
- package/src/text/path.ts +14 -0
- package/src/text/picture.ts +5 -2
- package/src/token-sources/document.ts +104 -0
- package/src/token-sources/index.ts +2 -0
- package/src/token-sources/schema.ts +59 -0
- package/src/transfer/copied.ts +72 -0
- package/src/transfer/index.ts +1 -0
- package/src/transfer/nodes.ts +10 -0
- package/src/types.ts +117 -14
- package/src/undo.ts +11 -0
- package/src/variables/index.ts +1 -4
- package/src/variations/copy.ts +89 -0
- package/src/variations/index.ts +5 -0
- package/src/variations/match.ts +100 -0
- package/src/variations/model.ts +132 -0
- package/src/variations/pick.ts +293 -0
- package/src/variations/schema.ts +29 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { copyVersion, withStandIn } from "./copy.js";
|
|
2
|
+
import { matchLayers, standsIn } from "./match.js";
|
|
3
|
+
import { VARIATION_LABEL_MAX_LENGTH, VARIATION_MAX_COUNT, VARIATION_MAX_LAYERS, VariationSet, variationSetSchema, variationStandInSchema } from "./schema.js";
|
|
4
|
+
import { VariationSetView, originalChangedSince, readVariationSet, variationFingerprint, variationSetForOriginal, variationSetOf, variationSetsOnPage, versionOf, withVariationSet } from "./model.js";
|
|
5
|
+
import { VariationError, VariationPickProblem, applyVariation, discardVariations, variationPickProblem, variationToPick } from "./pick.js";
|
|
6
|
+
export { VARIATION_LABEL_MAX_LENGTH, VARIATION_MAX_COUNT, VARIATION_MAX_LAYERS, VariationError, VariationPickProblem, VariationSet, VariationSetView, applyVariation, copyVersion, discardVariations, matchLayers, originalChangedSince, readVariationSet, standsIn, variationFingerprint, variationPickProblem, variationSetForOriginal, variationSetOf, variationSetSchema, variationSetsOnPage, variationStandInSchema, variationToPick, versionOf, withStandIn, withVariationSet };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { VARIATION_LABEL_MAX_LENGTH, VARIATION_MAX_COUNT, VARIATION_MAX_LAYERS, variationSetSchema, variationStandInSchema } from "./schema.js";
|
|
2
|
+
import { copyVersion, withStandIn } from "./copy.js";
|
|
3
|
+
import { matchLayers, standsIn } from "./match.js";
|
|
4
|
+
import { originalChangedSince, readVariationSet, variationFingerprint, variationSetForOriginal, variationSetOf, variationSetsOnPage, versionOf, withVariationSet } from "./model.js";
|
|
5
|
+
import { VariationError, applyVariation, discardVariations, variationPickProblem, variationToPick } from "./pick.js";
|
|
6
|
+
export { VARIATION_LABEL_MAX_LENGTH, VARIATION_MAX_COUNT, VARIATION_MAX_LAYERS, VariationError, applyVariation, copyVersion, discardVariations, matchLayers, originalChangedSince, readVariationSet, standsIn, variationFingerprint, variationPickProblem, variationSetForOriginal, variationSetOf, variationSetSchema, variationSetsOnPage, variationStandInSchema, variationToPick, versionOf, withStandIn, withVariationSet };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SceneGraph } from "../graph.js";
|
|
2
|
+
import { SceneNode } from "../types.js";
|
|
3
|
+
//#region src/variations/match.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Which layer of the original each layer of a version stands for, as version id → original id.
|
|
6
|
+
* The roots always pair. Under each paired parent, children pair first by type and name in
|
|
7
|
+
* order, then the ones left by type in order, so a renamed or moved layer still finds its
|
|
8
|
+
* counterpart; layers of different types never pair, except a main component's or component
|
|
9
|
+
* set's stand-in, marked as such (`standsIn`), which pairs with it; a marked layer that does
|
|
10
|
+
* not find its component here pairs as any other layer. Only layers paired with their own type are
|
|
11
|
+
* searched further. Nothing is stored: a version is a copy of the original, so its layers mostly
|
|
12
|
+
* keep the names and types they were copied with.
|
|
13
|
+
*/
|
|
14
|
+
declare function matchLayers(graph: SceneGraph, originalId: string, versionId: string): Map<string, string>;
|
|
15
|
+
/**
|
|
16
|
+
* Whether `version` is what a copy holds in place of the main component or component set
|
|
17
|
+
* `original`, as its mark says (`copyVersion`): an instance of the component, or a frame of
|
|
18
|
+
* instances of the set's variants. A name proves nothing, since an instance may share it.
|
|
19
|
+
*/
|
|
20
|
+
declare function standsIn(version: SceneNode, original: SceneNode): boolean;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { matchLayers, standsIn };
|
|
23
|
+
//# sourceMappingURL=match.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"match.d.ts","names":[],"sources":["../../src/variations/match.ts"],"mappings":";;;;;;;;;;;;;iBAiBgB,YACd,OAAO,YACP,oBACA,oBACC;;;;;;iBAqBa,SAAS,SAAS,WAAW,UAAU"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { readPluginData } from "../plugin-data/field.js";
|
|
2
|
+
import { PAIRDRAW_PLUGIN_DATA } from "../plugin-data/fields.js";
|
|
3
|
+
import { groupBy } from "es-toolkit";
|
|
4
|
+
//#region src/variations/match.ts
|
|
5
|
+
/**
|
|
6
|
+
* Which layer of the original each layer of a version stands for, as version id → original id.
|
|
7
|
+
* The roots always pair. Under each paired parent, children pair first by type and name in
|
|
8
|
+
* order, then the ones left by type in order, so a renamed or moved layer still finds its
|
|
9
|
+
* counterpart; layers of different types never pair, except a main component's or component
|
|
10
|
+
* set's stand-in, marked as such (`standsIn`), which pairs with it; a marked layer that does
|
|
11
|
+
* not find its component here pairs as any other layer. Only layers paired with their own type are
|
|
12
|
+
* searched further. Nothing is stored: a version is a copy of the original, so its layers mostly
|
|
13
|
+
* keep the names and types they were copied with.
|
|
14
|
+
*/
|
|
15
|
+
function matchLayers(graph, originalId, versionId) {
|
|
16
|
+
const pairs = /* @__PURE__ */ new Map([[versionId, originalId]]);
|
|
17
|
+
const pending = [[versionId, originalId]];
|
|
18
|
+
for (let next = pending.pop(); next; next = pending.pop()) {
|
|
19
|
+
const [versionParent, originalParent] = next;
|
|
20
|
+
for (const [version, original] of matchChildren(graph.getChildren(versionParent), graph.getChildren(originalParent))) {
|
|
21
|
+
pairs.set(version.id, original.id);
|
|
22
|
+
if (version.type === original.type) pending.push([version.id, original.id]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return pairs;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Whether `version` is what a copy holds in place of the main component or component set
|
|
29
|
+
* `original`, as its mark says (`copyVersion`): an instance of the component, or a frame of
|
|
30
|
+
* instances of the set's variants. A name proves nothing, since an instance may share it.
|
|
31
|
+
*/
|
|
32
|
+
function standsIn(version, original) {
|
|
33
|
+
if (original.type !== "COMPONENT" && original.type !== "COMPONENT_SET") return false;
|
|
34
|
+
return readPluginData(version.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn) === original.id;
|
|
35
|
+
}
|
|
36
|
+
/** Takes the next free layer of a bucket, skipping ones another bucket already gave away. */
|
|
37
|
+
function takeNext(bucket, cursors, taken) {
|
|
38
|
+
if (!bucket) return void 0;
|
|
39
|
+
let index = cursors.get(bucket) ?? 0;
|
|
40
|
+
while (index < bucket.length && taken.has(bucket[index].id)) index++;
|
|
41
|
+
cursors.set(bucket, Math.min(index + 1, bucket.length));
|
|
42
|
+
if (index === bucket.length) return void 0;
|
|
43
|
+
const found = bucket[index];
|
|
44
|
+
taken.add(found.id);
|
|
45
|
+
return found;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Pairs in one pass over each list: stand-ins for main components and sets first, then through
|
|
49
|
+
* buckets by type and name and by type alone.
|
|
50
|
+
*/
|
|
51
|
+
function matchChildren(versions, originals) {
|
|
52
|
+
const mains = new Map(originals.filter((node) => node.type === "COMPONENT" || node.type === "COMPONENT_SET").map((node) => [node.id, node]));
|
|
53
|
+
const byName = groupBy(originals, (node) => `${node.type}\u0000${node.name}`);
|
|
54
|
+
const byType = groupBy(originals, (node) => node.type);
|
|
55
|
+
const cursors = /* @__PURE__ */ new Map();
|
|
56
|
+
const taken = /* @__PURE__ */ new Set();
|
|
57
|
+
const pairs = [];
|
|
58
|
+
const left = [];
|
|
59
|
+
for (const version of versions) {
|
|
60
|
+
const mainId = readPluginData(version.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn);
|
|
61
|
+
const main = mainId === void 0 ? void 0 : mains.get(mainId);
|
|
62
|
+
if (main && !taken.has(main.id) && standsIn(version, main)) {
|
|
63
|
+
taken.add(main.id);
|
|
64
|
+
pairs.push([version, main]);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const named = takeNext(byName[`${version.type}\u0000${version.name}`], cursors, taken);
|
|
68
|
+
if (named) pairs.push([version, named]);
|
|
69
|
+
else left.push(version);
|
|
70
|
+
}
|
|
71
|
+
for (const version of left) {
|
|
72
|
+
const typed = takeNext(byType[version.type], cursors, taken);
|
|
73
|
+
if (typed) pairs.push([version, typed]);
|
|
74
|
+
}
|
|
75
|
+
return pairs;
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { matchLayers, standsIn };
|
|
79
|
+
|
|
80
|
+
//# sourceMappingURL=match.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"match.js","names":[],"sources":["../../src/variations/match.ts"],"sourcesContent":["import { groupBy } from 'es-toolkit'\n\nimport type { SceneGraph } from '../graph'\nimport { readPluginData } from '../plugin-data/field'\nimport { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'\nimport type { SceneNode } from '../types'\n\n/**\n * Which layer of the original each layer of a version stands for, as version id → original id.\n * The roots always pair. Under each paired parent, children pair first by type and name in\n * order, then the ones left by type in order, so a renamed or moved layer still finds its\n * counterpart; layers of different types never pair, except a main component's or component\n * set's stand-in, marked as such (`standsIn`), which pairs with it; a marked layer that does\n * not find its component here pairs as any other layer. Only layers paired with their own type are\n * searched further. Nothing is stored: a version is a copy of the original, so its layers mostly\n * keep the names and types they were copied with.\n */\nexport function matchLayers(\n graph: SceneGraph,\n originalId: string,\n versionId: string\n): Map<string, string> {\n const pairs = new Map<string, string>([[versionId, originalId]])\n const pending: Array<[string, string]> = [[versionId, originalId]]\n for (let next = pending.pop(); next; next = pending.pop()) {\n const [versionParent, originalParent] = next\n for (const [version, original] of matchChildren(\n graph.getChildren(versionParent),\n graph.getChildren(originalParent)\n )) {\n pairs.set(version.id, original.id)\n if (version.type === original.type) pending.push([version.id, original.id])\n }\n }\n return pairs\n}\n\n/**\n * Whether `version` is what a copy holds in place of the main component or component set\n * `original`, as its mark says (`copyVersion`): an instance of the component, or a frame of\n * instances of the set's variants. A name proves nothing, since an instance may share it.\n */\nexport function standsIn(version: SceneNode, original: SceneNode): boolean {\n if (original.type !== 'COMPONENT' && original.type !== 'COMPONENT_SET') return false\n return readPluginData(version.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn) === original.id\n}\n\n/** Takes the next free layer of a bucket, skipping ones another bucket already gave away. */\nfunction takeNext(\n bucket: readonly SceneNode[] | undefined,\n cursors: Map<readonly SceneNode[], number>,\n taken: Set<string>\n): SceneNode | undefined {\n if (!bucket) return undefined\n let index = cursors.get(bucket) ?? 0\n while (index < bucket.length && taken.has(bucket[index].id)) index++\n cursors.set(bucket, Math.min(index + 1, bucket.length))\n if (index === bucket.length) return undefined\n const found = bucket[index]\n taken.add(found.id)\n return found\n}\n\n/**\n * Pairs in one pass over each list: stand-ins for main components and sets first, then through\n * buckets by type and name and by type alone.\n */\nfunction matchChildren(\n versions: SceneNode[],\n originals: SceneNode[]\n): Array<[SceneNode, SceneNode]> {\n const mains = new Map(\n originals\n .filter((node) => node.type === 'COMPONENT' || node.type === 'COMPONENT_SET')\n .map((node) => [node.id, node])\n )\n const byName = groupBy(originals, (node) => `${node.type}\\u0000${node.name}`)\n const byType = groupBy(originals, (node) => node.type)\n const cursors = new Map<readonly SceneNode[], number>()\n const taken = new Set<string>()\n const pairs: Array<[SceneNode, SceneNode]> = []\n const left: SceneNode[] = []\n for (const version of versions) {\n const mainId = readPluginData(version.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn)\n const main = mainId === undefined ? undefined : mains.get(mainId)\n if (main && !taken.has(main.id) && standsIn(version, main)) {\n taken.add(main.id)\n pairs.push([version, main])\n continue\n }\n const named = takeNext(byName[`${version.type}\\u0000${version.name}`], cursors, taken)\n if (named) pairs.push([version, named])\n else left.push(version)\n }\n for (const version of left) {\n const typed = takeNext(byType[version.type], cursors, taken)\n if (typed) pairs.push([version, typed])\n }\n return pairs\n}\n"],"mappings":";;;;;;;;;;;;;;AAiBA,SAAgB,YACd,OACA,YACA,WACqB;CACrB,MAAM,wBAAQ,IAAI,IAAoB,CAAC,CAAC,WAAW,UAAU,CAAC,CAAC;CAC/D,MAAM,UAAmC,CAAC,CAAC,WAAW,UAAU,CAAC;CACjE,KAAK,IAAI,OAAO,QAAQ,IAAI,GAAG,MAAM,OAAO,QAAQ,IAAI,GAAG;EACzD,MAAM,CAAC,eAAe,kBAAkB;EACxC,KAAK,MAAM,CAAC,SAAS,aAAa,cAChC,MAAM,YAAY,aAAa,GAC/B,MAAM,YAAY,cAAc,CAClC,GAAG;GACD,MAAM,IAAI,QAAQ,IAAI,SAAS,EAAE;GACjC,IAAI,QAAQ,SAAS,SAAS,MAAM,QAAQ,KAAK,CAAC,QAAQ,IAAI,SAAS,EAAE,CAAC;EAC5E;CACF;CACA,OAAO;AACT;;;;;;AAOA,SAAgB,SAAS,SAAoB,UAA8B;CACzE,IAAI,SAAS,SAAS,eAAe,SAAS,SAAS,iBAAiB,OAAO;CAC/E,OAAO,eAAe,QAAQ,YAAY,qBAAqB,gBAAgB,MAAM,SAAS;AAChG;;AAGA,SAAS,SACP,QACA,SACA,OACuB;CACvB,IAAI,CAAC,QAAQ,OAAO,KAAA;CACpB,IAAI,QAAQ,QAAQ,IAAI,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S,OAAO,QAAQ,OAAO,SAAS,KAAK;EAC3D,IAAI,OAAO,MAAM,KAAK,CAAC,SAAS,KAAK,CAAC;CACxC;CACA,OAAO;AACT"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { SceneGraph } from "../graph.js";
|
|
2
|
+
import { SceneNode } from "../types.js";
|
|
3
|
+
import { VariationSet } from "./schema.js";
|
|
4
|
+
//#region src/variations/model.d.ts
|
|
5
|
+
/** A set of variations as the document holds it now. */
|
|
6
|
+
interface VariationSetView {
|
|
7
|
+
/** The section holding the versions, which carries the set's mark. */
|
|
8
|
+
section: SceneNode;
|
|
9
|
+
set: VariationSet;
|
|
10
|
+
/** The layer the versions are variations of, or null when it is gone. */
|
|
11
|
+
original: SceneNode | null;
|
|
12
|
+
/** The section's children, in their order. */
|
|
13
|
+
versions: SceneNode[];
|
|
14
|
+
}
|
|
15
|
+
/** The set a section marks, or null when the layer is not a section or carries none. */
|
|
16
|
+
declare function readVariationSet(node: Pick<SceneNode, 'type' | 'pluginData'>): VariationSet | null;
|
|
17
|
+
/** The section's plugin data with its mark replaced by `set`, or removed when it is null. */
|
|
18
|
+
declare function withVariationSet(node: Pick<SceneNode, 'pluginData'>, set: VariationSet | null): SceneNode['pluginData'];
|
|
19
|
+
/** The set a layer belongs to: the marked section itself, one of its versions, or a layer inside one. */
|
|
20
|
+
declare function variationSetOf(graph: SceneGraph, nodeId: string): VariationSetView | null;
|
|
21
|
+
/** The version of `view` that holds `nodeId`, or the version itself; null outside its versions. */
|
|
22
|
+
declare function versionOf(graph: SceneGraph, view: VariationSetView, nodeId: string): SceneNode | null;
|
|
23
|
+
/** The sets on a page; they sit on the page itself, beside the layers they vary. */
|
|
24
|
+
declare function variationSetsOnPage(graph: SceneGraph, pageId: string): VariationSetView[];
|
|
25
|
+
/** The set whose versions vary `originalId`, if there is one on its page. */
|
|
26
|
+
declare function variationSetForOriginal(graph: SceneGraph, originalId: string): VariationSetView | null;
|
|
27
|
+
/**
|
|
28
|
+
* A short hash of what a layer and its layers look like at a glance: their types, names, boxes,
|
|
29
|
+
* visibility, opacity, text, and how many fills and children they have. The layer's own position
|
|
30
|
+
* is left out, so moving it, or its auto layout parent placing it, is no change. It walks the
|
|
31
|
+
* whole subtree, so callers compute it when they show it rather than on every edit; it changes
|
|
32
|
+
* with most edits, which is all a "changed since" notice needs, and is not a full comparison.
|
|
33
|
+
*/
|
|
34
|
+
declare function variationFingerprint(graph: SceneGraph, nodeId: string): string;
|
|
35
|
+
/** Whether the set's original looks different from when its versions were made. */
|
|
36
|
+
declare function originalChangedSince(graph: SceneGraph, view: VariationSetView): boolean;
|
|
37
|
+
//#endregion
|
|
38
|
+
export { VariationSetView, originalChangedSince, readVariationSet, variationFingerprint, variationSetForOriginal, variationSetOf, variationSetsOnPage, versionOf, withVariationSet };
|
|
39
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.d.ts","names":[],"sources":["../../src/variations/model.ts"],"mappings":";;;;;UAOiB;;EAEf,SAAS;EACT,KAAK;;EAEL,UAAU;;EAEV,UAAU;;;iBAII,iBACd,MAAM,KAAK,oCACV;;iBAMa,iBACd,MAAM,KAAK,0BACX,KAAK,sBACJ;;iBAsBa,eAAe,OAAO,YAAY,iBAAiB;;iBAMnD,UACd,OAAO,YACP,MAAM,kBACN,iBACC;;iBAKa,oBAAoB,OAAO,YAAY,iBAAiB;;iBAKxD,wBACd,OAAO,YACP,qBACC;;;;;;;;iBAwBa,qBAAqB,OAAO,YAAY;;iBA6BxC,qBAAqB,OAAO,YAAY,MAAM"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { readPluginData, withPluginData } from "../plugin-data/field.js";
|
|
2
|
+
import { PAIRDRAW_PLUGIN_DATA } from "../plugin-data/fields.js";
|
|
3
|
+
//#region src/variations/model.ts
|
|
4
|
+
/** The set a section marks, or null when the layer is not a section or carries none. */
|
|
5
|
+
function readVariationSet(node) {
|
|
6
|
+
if (node.type !== "SECTION") return null;
|
|
7
|
+
return readPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationSet) ?? null;
|
|
8
|
+
}
|
|
9
|
+
/** The section's plugin data with its mark replaced by `set`, or removed when it is null. */
|
|
10
|
+
function withVariationSet(node, set) {
|
|
11
|
+
return withPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationSet, set ?? void 0);
|
|
12
|
+
}
|
|
13
|
+
function viewOf(graph, section) {
|
|
14
|
+
const set = readVariationSet(section);
|
|
15
|
+
if (!set) return null;
|
|
16
|
+
const original = graph.getNode(set.originalId) ?? null;
|
|
17
|
+
return {
|
|
18
|
+
section,
|
|
19
|
+
set,
|
|
20
|
+
original: original !== null && original.type !== "CANVAS" && graph.pageOf(original.id) !== null && !graph.isDescendant(original.id, section.id) ? original : null,
|
|
21
|
+
versions: graph.getChildren(section.id)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** The set a layer belongs to: the marked section itself, one of its versions, or a layer inside one. */
|
|
25
|
+
function variationSetOf(graph, nodeId) {
|
|
26
|
+
const section = graph.closest(nodeId, (node) => readVariationSet(node) !== null);
|
|
27
|
+
return section ? viewOf(graph, section) : null;
|
|
28
|
+
}
|
|
29
|
+
/** The version of `view` that holds `nodeId`, or the version itself; null outside its versions. */
|
|
30
|
+
function versionOf(graph, view, nodeId) {
|
|
31
|
+
return graph.closest(nodeId, (node) => node.parentId === view.section.id) ?? null;
|
|
32
|
+
}
|
|
33
|
+
/** The sets on a page; they sit on the page itself, beside the layers they vary. */
|
|
34
|
+
function variationSetsOnPage(graph, pageId) {
|
|
35
|
+
return graph.getChildren(pageId).flatMap((node) => viewOf(graph, node) ?? []);
|
|
36
|
+
}
|
|
37
|
+
/** The set whose versions vary `originalId`, if there is one on its page. */
|
|
38
|
+
function variationSetForOriginal(graph, originalId) {
|
|
39
|
+
const pageId = graph.pageOf(originalId);
|
|
40
|
+
if (!pageId) return null;
|
|
41
|
+
return variationSetsOnPage(graph, pageId).find((view) => view.original?.id === originalId) ?? null;
|
|
42
|
+
}
|
|
43
|
+
const FNV_OFFSET = 2166136261;
|
|
44
|
+
const FNV_PRIME = 16777619;
|
|
45
|
+
function hashText(hash, text) {
|
|
46
|
+
let next = hash;
|
|
47
|
+
for (let index = 0; index < text.length; index++) next = Math.imul(next ^ text.charCodeAt(index), FNV_PRIME) >>> 0;
|
|
48
|
+
return next;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A short hash of what a layer and its layers look like at a glance: their types, names, boxes,
|
|
52
|
+
* visibility, opacity, text, and how many fills and children they have. The layer's own position
|
|
53
|
+
* is left out, so moving it, or its auto layout parent placing it, is no change. It walks the
|
|
54
|
+
* whole subtree, so callers compute it when they show it rather than on every edit; it changes
|
|
55
|
+
* with most edits, which is all a "changed since" notice needs, and is not a full comparison.
|
|
56
|
+
*/
|
|
57
|
+
function variationFingerprint(graph, nodeId) {
|
|
58
|
+
let hash = FNV_OFFSET;
|
|
59
|
+
const pending = [nodeId];
|
|
60
|
+
for (let id = pending.pop(); id !== void 0; id = pending.pop()) {
|
|
61
|
+
const node = graph.getNode(id);
|
|
62
|
+
if (!node) continue;
|
|
63
|
+
hash = hashText(hash, [
|
|
64
|
+
node.type,
|
|
65
|
+
node.name,
|
|
66
|
+
id === nodeId ? "" : node.x,
|
|
67
|
+
id === nodeId ? "" : node.y,
|
|
68
|
+
node.width,
|
|
69
|
+
node.height,
|
|
70
|
+
node.rotation,
|
|
71
|
+
node.visible,
|
|
72
|
+
node.opacity,
|
|
73
|
+
node.text,
|
|
74
|
+
node.fills.length,
|
|
75
|
+
node.childIds.length
|
|
76
|
+
].join("\0"));
|
|
77
|
+
pending.push(...node.childIds);
|
|
78
|
+
}
|
|
79
|
+
return hash.toString(16).padStart(8, "0");
|
|
80
|
+
}
|
|
81
|
+
/** Whether the set's original looks different from when its versions were made. */
|
|
82
|
+
function originalChangedSince(graph, view) {
|
|
83
|
+
const { original, set } = view;
|
|
84
|
+
if (!original || set.originalFingerprint === void 0) return false;
|
|
85
|
+
return variationFingerprint(graph, original.id) !== set.originalFingerprint;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
export { originalChangedSince, readVariationSet, variationFingerprint, variationSetForOriginal, variationSetOf, variationSetsOnPage, versionOf, withVariationSet };
|
|
89
|
+
|
|
90
|
+
//# sourceMappingURL=model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.js","names":[],"sources":["../../src/variations/model.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport { readPluginData, withPluginData } from '../plugin-data/field'\nimport { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'\nimport type { SceneNode } from '../types'\nimport type { VariationSet } from './schema'\n\n/** A set of variations as the document holds it now. */\nexport interface VariationSetView {\n /** The section holding the versions, which carries the set's mark. */\n section: SceneNode\n set: VariationSet\n /** The layer the versions are variations of, or null when it is gone. */\n original: SceneNode | null\n /** The section's children, in their order. */\n versions: SceneNode[]\n}\n\n/** The set a section marks, or null when the layer is not a section or carries none. */\nexport function readVariationSet(\n node: Pick<SceneNode, 'type' | 'pluginData'>\n): VariationSet | null {\n if (node.type !== 'SECTION') return null\n return readPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationSet) ?? null\n}\n\n/** The section's plugin data with its mark replaced by `set`, or removed when it is null. */\nexport function withVariationSet(\n node: Pick<SceneNode, 'pluginData'>,\n set: VariationSet | null\n): SceneNode['pluginData'] {\n return withPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationSet, set ?? undefined)\n}\n\nfunction viewOf(graph: SceneGraph, section: SceneNode): VariationSetView | null {\n const set = readVariationSet(section)\n if (!set) return null\n const original = graph.getNode(set.originalId) ?? null\n const usable =\n original !== null &&\n original.type !== 'CANVAS' &&\n graph.pageOf(original.id) !== null &&\n !graph.isDescendant(original.id, section.id)\n return {\n section,\n set,\n original: usable ? original : null,\n versions: graph.getChildren(section.id)\n }\n}\n\n/** The set a layer belongs to: the marked section itself, one of its versions, or a layer inside one. */\nexport function variationSetOf(graph: SceneGraph, nodeId: string): VariationSetView | null {\n const section = graph.closest(nodeId, (node) => readVariationSet(node) !== null)\n return section ? viewOf(graph, section) : null\n}\n\n/** The version of `view` that holds `nodeId`, or the version itself; null outside its versions. */\nexport function versionOf(\n graph: SceneGraph,\n view: VariationSetView,\n nodeId: string\n): SceneNode | null {\n return graph.closest(nodeId, (node) => node.parentId === view.section.id) ?? null\n}\n\n/** The sets on a page; they sit on the page itself, beside the layers they vary. */\nexport function variationSetsOnPage(graph: SceneGraph, pageId: string): VariationSetView[] {\n return graph.getChildren(pageId).flatMap((node) => viewOf(graph, node) ?? [])\n}\n\n/** The set whose versions vary `originalId`, if there is one on its page. */\nexport function variationSetForOriginal(\n graph: SceneGraph,\n originalId: string\n): VariationSetView | null {\n const pageId = graph.pageOf(originalId)\n if (!pageId) return null\n return variationSetsOnPage(graph, pageId).find((view) => view.original?.id === originalId) ?? null\n}\n\nconst FNV_OFFSET = 0x811c9dc5\nconst FNV_PRIME = 0x01000193\n\nfunction hashText(hash: number, text: string): number {\n let next = hash\n for (let index = 0; index < text.length; index++) {\n next = Math.imul(next ^ text.charCodeAt(index), FNV_PRIME) >>> 0\n }\n return next\n}\n\n/**\n * A short hash of what a layer and its layers look like at a glance: their types, names, boxes,\n * visibility, opacity, text, and how many fills and children they have. The layer's own position\n * is left out, so moving it, or its auto layout parent placing it, is no change. It walks the\n * whole subtree, so callers compute it when they show it rather than on every edit; it changes\n * with most edits, which is all a \"changed since\" notice needs, and is not a full comparison.\n */\nexport function variationFingerprint(graph: SceneGraph, nodeId: string): string {\n let hash = FNV_OFFSET\n const pending = [nodeId]\n for (let id = pending.pop(); id !== undefined; id = pending.pop()) {\n const node = graph.getNode(id)\n if (!node) continue\n hash = hashText(\n hash,\n [\n node.type,\n node.name,\n id === nodeId ? '' : node.x,\n id === nodeId ? '' : node.y,\n node.width,\n node.height,\n node.rotation,\n node.visible,\n node.opacity,\n node.text,\n node.fills.length,\n node.childIds.length\n ].join('\\u0000')\n )\n pending.push(...node.childIds)\n }\n return hash.toString(16).padStart(8, '0')\n}\n\n/** Whether the set's original looks different from when its versions were made. */\nexport function originalChangedSince(graph: SceneGraph, view: VariationSetView): boolean {\n const { original, set } = view\n if (!original || set.originalFingerprint === undefined) return false\n return variationFingerprint(graph, original.id) !== set.originalFingerprint\n}\n"],"mappings":";;;;AAkBA,SAAgB,iBACd,MACqB;CACrB,IAAI,KAAK,SAAS,WAAW,OAAO;CACpC,OAAO,eAAe,KAAK,YAAY,qBAAqB,YAAY,KAAK;AAC/E;;AAGA,SAAgB,iBACd,MACA,KACyB;CACzB,OAAO,eAAe,KAAK,YAAY,qBAAqB,cAAc,OAAO,KAAA,CAAS;AAC5F;AAEA,SAAS,OAAO,OAAmB,SAA6C;CAC9E,MAAM,MAAM,iBAAiB,OAAO;CACpC,IAAI,CAAC,KAAK,OAAO;CACjB,MAAM,WAAW,MAAM,QAAQ,IAAI,UAAU,KAAK;CAMlD,OAAO;EACL;EACA;EACA,UAPA,aAAa,QACb,SAAS,SAAS,YAClB,MAAM,OAAO,SAAS,EAAE,MAAM,QAC9B,CAAC,MAAM,aAAa,SAAS,IAAI,QAAQ,EAAE,IAIxB,WAAW;EAC9B,UAAU,MAAM,YAAY,QAAQ,EAAE;CACxC;AACF;;AAGA,SAAgB,eAAe,OAAmB,QAAyC;CACzF,MAAM,UAAU,MAAM,QAAQ,SAAS,SAAS,iBAAiB,IAAI,MAAM,IAAI;CAC/E,OAAO,UAAU,OAAO,OAAO,OAAO,IAAI;AAC5C;;AAGA,SAAgB,UACd,OACA,MACA,QACkB;CAClB,OAAO,MAAM,QAAQ,SAAS,SAAS,KAAK,aAAa,KAAK,QAAQ,EAAE,KAAK;AAC/E;;AAGA,SAAgB,oBAAoB,OAAmB,QAAoC;CACzF,OAAO,MAAM,YAAY,MAAM,CAAC,CAAC,SAAS,SAAS,OAAO,OAAO,IAAI,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1,43 @@
|
|
|
1
|
+
import { SceneGraph } from "../graph.js";
|
|
2
|
+
import { SceneNode } from "../types.js";
|
|
3
|
+
import { VariationSetView } from "./model.js";
|
|
4
|
+
//#region src/variations/pick.d.ts
|
|
5
|
+
/** Why a version cannot replace its original. */
|
|
6
|
+
type VariationPickProblem = 'not-in-set' | 'not-a-version' | 'original-missing' | 'original-in-instance' | 'original-locked' | 'version-type-differs';
|
|
7
|
+
/** A layer that belongs to no set, or a version that cannot be used, with the reason. */
|
|
8
|
+
declare class VariationError extends Error {
|
|
9
|
+
readonly problem: VariationPickProblem;
|
|
10
|
+
constructor(problem: VariationPickProblem, nodeId: string);
|
|
11
|
+
}
|
|
12
|
+
/** Why `versionId` cannot replace the original of `view`, or null when it can. */
|
|
13
|
+
declare function variationPickProblem(graph: SceneGraph, view: VariationSetView, versionId: string): VariationPickProblem | null;
|
|
14
|
+
/**
|
|
15
|
+
* The set and version `versionId` names, a version or a layer inside one, and why it cannot
|
|
16
|
+
* replace its original, or null when it can. Throws when `versionId` is in no set's versions.
|
|
17
|
+
*/
|
|
18
|
+
declare function variationToPick(graph: SceneGraph, versionId: string): {
|
|
19
|
+
view: VariationSetView;
|
|
20
|
+
version: SceneNode;
|
|
21
|
+
problem: VariationPickProblem | null;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Makes the original of the set holding `versionId` look like that version, then removes the
|
|
25
|
+
* set. The original keeps its id, name, place among its siblings, and how its parent lays it
|
|
26
|
+
* out, and so does every layer of it that pairs with one of the version's (`matchLayers`), so
|
|
27
|
+
* what points at them keeps working; layers only the version has move in, and layers only the
|
|
28
|
+
* original had are deleted, apart from main components and sets, which are kept as they are,
|
|
29
|
+
* as when the version holds their stand-ins. Only fields that differ are written and only layers out of place
|
|
30
|
+
* are moved, so a pick sends no more changes than it makes. A main component stays one, keeping
|
|
31
|
+
* its properties and publishing. Comments stay pinned where they were; one on a removed version
|
|
32
|
+
* falls back to its stored point, as on any deleted layer. The caller records the edit as one step and lays out the original and its parent.
|
|
33
|
+
*/
|
|
34
|
+
declare function applyVariation(graph: SceneGraph, versionId: string): {
|
|
35
|
+
originalId: string;
|
|
36
|
+
};
|
|
37
|
+
/** Removes the set holding `nodeId` and every version in it; the original is left as it is. */
|
|
38
|
+
declare function discardVariations(graph: SceneGraph, nodeId: string): {
|
|
39
|
+
originalId: string | null;
|
|
40
|
+
};
|
|
41
|
+
//#endregion
|
|
42
|
+
export { VariationError, VariationPickProblem, applyVariation, discardVariations, variationPickProblem, variationToPick };
|
|
43
|
+
//# sourceMappingURL=pick.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pick.d.ts","names":[],"sources":["../../src/variations/pick.ts"],"mappings":";;;;;KAcY;;cAuBC,uBAAuB;WAEvB,SAAS;EAAT,YAAA,SAAS,sBAClB;;;iBAkEY,qBACd,OAAO,YACP,MAAM,kBACN,oBACC;;;;;iBAgBa,gBACd,OAAO,YACP;EACG,MAAM;EAAkB,SAAS;EAAW,SAAS;;;;;;;;;;;;;iBAgI1C,eAAe,OAAO,YAAY;EAAsB;;;iBA2BxD,kBACd,OAAO,YACP;EACG"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { cloneNodeProps } from "../copy.js";
|
|
2
|
+
import { TRANSFORM_FIELDS } from "../fields/geometry.js";
|
|
3
|
+
import { PARENT_LAYOUT_FIELDS } from "../fields/layout.js";
|
|
4
|
+
import { readPluginData } from "../plugin-data/field.js";
|
|
5
|
+
import { PAIRDRAW_PLUGIN_DATA } from "../plugin-data/fields.js";
|
|
6
|
+
import { findInstanceAncestor, retargetCopiedOverrides } from "../instances/index.js";
|
|
7
|
+
import { MAIN_COMPONENT_FIELDS } from "../fields/components.js";
|
|
8
|
+
import { withStandIn } from "./copy.js";
|
|
9
|
+
import { matchLayers } from "./match.js";
|
|
10
|
+
import { variationSetOf, versionOf } from "./model.js";
|
|
11
|
+
import { isEqual, pick } from "es-toolkit";
|
|
12
|
+
//#region src/variations/pick.ts
|
|
13
|
+
const PROBLEM_MESSAGES = {
|
|
14
|
+
"not-in-set": (nodeId) => `"${nodeId}" is not in a set of variations`,
|
|
15
|
+
"not-a-version": (nodeId) => `"${nodeId}" is not a version: pick one of the set's layers`,
|
|
16
|
+
"original-missing": () => "The layer these are variations of is gone; discard them instead",
|
|
17
|
+
"original-in-instance": () => "The layer these are variations of is inside an instance; vary the instance or its main component",
|
|
18
|
+
"original-locked": () => "The layer these are variations of is locked; unlock it to replace it with a version",
|
|
19
|
+
"version-type-differs": () => "This version is a different kind of layer than the original (such as a frame for an instance), so it cannot replace it; change the layers inside a version instead of replacing the version, or make a new version"
|
|
20
|
+
};
|
|
21
|
+
/** A layer that belongs to no set, or a version that cannot be used, with the reason. */
|
|
22
|
+
var VariationError = class extends Error {
|
|
23
|
+
problem;
|
|
24
|
+
constructor(problem, nodeId) {
|
|
25
|
+
super(PROBLEM_MESSAGES[problem](nodeId));
|
|
26
|
+
this.problem = problem;
|
|
27
|
+
this.name = "VariationError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
/** Never taken from a version: what places a layer in the tree, who it is, and its review. */
|
|
31
|
+
const OWN_FIELDS = /* @__PURE__ */ new Set([
|
|
32
|
+
"id",
|
|
33
|
+
"parentId",
|
|
34
|
+
"childIds",
|
|
35
|
+
"source",
|
|
36
|
+
"status"
|
|
37
|
+
]);
|
|
38
|
+
/** What the original keeps of its own place: its name, where it sits, and how it is shown. */
|
|
39
|
+
const ORIGINAL_PLACE_FIELDS = /* @__PURE__ */ new Set([
|
|
40
|
+
"name",
|
|
41
|
+
...TRANSFORM_FIELDS.filter((field) => field !== "flipX" && field !== "flipY"),
|
|
42
|
+
...PARENT_LAYOUT_FIELDS,
|
|
43
|
+
"visible",
|
|
44
|
+
"locked"
|
|
45
|
+
]);
|
|
46
|
+
/** What a main component original keeps: being a component, and its properties and publishing. */
|
|
47
|
+
const MAIN_COMPONENT_KEPT = /* @__PURE__ */ new Set(["type", ...MAIN_COMPONENT_FIELDS]);
|
|
48
|
+
function standInMark(node) {
|
|
49
|
+
return node ? readPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn) : void 0;
|
|
50
|
+
}
|
|
51
|
+
/** Removes stand-in marks from a layer moving into the original, and from its layers. */
|
|
52
|
+
function clearStandInMarks(graph, rootId) {
|
|
53
|
+
for (const node of subtree(graph, rootId)) if (standInMark(node) !== void 0) graph.updateNode(node.id, { pluginData: withStandIn(node, null) });
|
|
54
|
+
}
|
|
55
|
+
/** The layer at `rootId` and every layer inside it. */
|
|
56
|
+
function* subtree(graph, rootId) {
|
|
57
|
+
const pending = [rootId];
|
|
58
|
+
for (let id = pending.pop(); id !== void 0; id = pending.pop()) {
|
|
59
|
+
const node = graph.getNode(id);
|
|
60
|
+
if (!node) continue;
|
|
61
|
+
pending.push(...node.childIds);
|
|
62
|
+
yield node;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function isMain(node) {
|
|
66
|
+
return node?.type === "COMPONENT" || node?.type === "COMPONENT_SET";
|
|
67
|
+
}
|
|
68
|
+
/** Whether a version whose root is `version` may replace an original of type `original`. */
|
|
69
|
+
function replaces(version, original) {
|
|
70
|
+
return version === original || original === "COMPONENT" && version === "FRAME";
|
|
71
|
+
}
|
|
72
|
+
/** Why `versionId` cannot replace the original of `view`, or null when it can. */
|
|
73
|
+
function variationPickProblem(graph, view, versionId) {
|
|
74
|
+
const version = graph.getNode(versionId);
|
|
75
|
+
if (version?.parentId !== view.section.id) return "not-a-version";
|
|
76
|
+
const { original } = view;
|
|
77
|
+
if (!original) return "original-missing";
|
|
78
|
+
if (original.parentId && findInstanceAncestor(graph, original.parentId)) return "original-in-instance";
|
|
79
|
+
if (original.locked) return "original-locked";
|
|
80
|
+
return replaces(version.type, original.type) ? null : "version-type-differs";
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The set and version `versionId` names, a version or a layer inside one, and why it cannot
|
|
84
|
+
* replace its original, or null when it can. Throws when `versionId` is in no set's versions.
|
|
85
|
+
*/
|
|
86
|
+
function variationToPick(graph, versionId) {
|
|
87
|
+
const view = variationSetOf(graph, versionId);
|
|
88
|
+
if (!view) throw new VariationError("not-in-set", versionId);
|
|
89
|
+
const version = versionOf(graph, view, versionId);
|
|
90
|
+
if (!version) throw new VariationError("not-a-version", versionId);
|
|
91
|
+
return {
|
|
92
|
+
view,
|
|
93
|
+
version,
|
|
94
|
+
problem: variationPickProblem(graph, view, version.id)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The version's fields that differ from what `original` holds, as `original` takes them, or null
|
|
99
|
+
* when none do, so an unchanged layer gets no update at all.
|
|
100
|
+
*/
|
|
101
|
+
function changedFields(versionLayer, original, isRoot) {
|
|
102
|
+
const version = standInMark(versionLayer) === void 0 ? versionLayer : {
|
|
103
|
+
...versionLayer,
|
|
104
|
+
pluginData: withStandIn(versionLayer, null)
|
|
105
|
+
};
|
|
106
|
+
const kept = (key) => OWN_FIELDS.has(key) || isRoot && ORIGINAL_PLACE_FIELDS.has(key) || isRoot && original.type === "COMPONENT" && MAIN_COMPONENT_KEPT.has(key);
|
|
107
|
+
const keys = Object.keys(version).filter((key) => version[key] !== original[key] && !kept(key) && !isEqual(version[key], original[key]));
|
|
108
|
+
return keys.length > 0 ? pick(cloneNodeProps(version, null), keys) : null;
|
|
109
|
+
}
|
|
110
|
+
function renamedReactions(reactions, rename) {
|
|
111
|
+
const renamed = (action) => action.type === "NODE" && action.nodeId !== null && rename(action.nodeId) !== action.nodeId;
|
|
112
|
+
if (!reactions.some((reaction) => reaction.actions.some(renamed))) return null;
|
|
113
|
+
return reactions.map((reaction) => ({
|
|
114
|
+
...reaction,
|
|
115
|
+
actions: reaction.actions.map((action) => action.type === "NODE" && action.nodeId !== null ? {
|
|
116
|
+
...action,
|
|
117
|
+
nodeId: rename(action.nodeId)
|
|
118
|
+
} : action)
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Points what the kept layers say about each other, connector ends and prototype targets, at
|
|
123
|
+
* the original's layers where they named the version's.
|
|
124
|
+
*/
|
|
125
|
+
function renameReferences(graph, rootId, pairs) {
|
|
126
|
+
const rename = (id) => pairs.get(id) ?? id;
|
|
127
|
+
for (const node of subtree(graph, rootId)) {
|
|
128
|
+
const changes = {};
|
|
129
|
+
for (const end of ["connectorStart", "connectorEnd"]) {
|
|
130
|
+
const endpoint = node[end];
|
|
131
|
+
if (endpoint?.nodeId && pairs.has(endpoint.nodeId)) changes[end] = {
|
|
132
|
+
...endpoint,
|
|
133
|
+
nodeId: rename(endpoint.nodeId)
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const reactions = renamedReactions(node.prototypeReactions, rename);
|
|
137
|
+
if (reactions) changes.prototypeReactions = reactions;
|
|
138
|
+
if (Object.keys(changes).length > 0) graph.updateNode(node.id, changes);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Gives `originalLayer` the children of `versionLayer`, in its order: paired layers stay, layers
|
|
143
|
+
* only the version has move in, and layers only the original had go, apart from main components
|
|
144
|
+
* and sets, which keep their place. Returns the pairs to visit next.
|
|
145
|
+
*/
|
|
146
|
+
function applyChildren(graph, pairs, paired, versionLayer, originalLayer) {
|
|
147
|
+
const originalChildren = [...originalLayer.childIds];
|
|
148
|
+
const keptMains = originalChildren.flatMap((childId, index) => !paired.has(childId) && isMain(graph.getNode(childId)) ? [{
|
|
149
|
+
childId,
|
|
150
|
+
index
|
|
151
|
+
}] : []);
|
|
152
|
+
const unpaired = originalChildren.filter((childId) => !paired.has(childId) && !isMain(graph.getNode(childId)));
|
|
153
|
+
for (const childId of unpaired) graph.deleteNode(childId);
|
|
154
|
+
const next = [];
|
|
155
|
+
const order = [];
|
|
156
|
+
for (const childId of versionLayer.childIds) {
|
|
157
|
+
const pairedId = pairs.get(childId);
|
|
158
|
+
const mark = pairedId === void 0 ? standInMark(graph.getNode(childId)) : void 0;
|
|
159
|
+
if (mark !== void 0 && !graph.getNode(mark)) continue;
|
|
160
|
+
order.push(pairedId ?? childId);
|
|
161
|
+
if (pairedId === void 0) clearStandInMarks(graph, childId);
|
|
162
|
+
else if (graph.getNode(childId)?.type === graph.getNode(pairedId)?.type) next.push([childId, pairedId]);
|
|
163
|
+
}
|
|
164
|
+
for (const { childId, index } of keptMains) order.splice(index, 0, childId);
|
|
165
|
+
order.forEach((target, index) => {
|
|
166
|
+
if (graph.getNode(originalLayer.id)?.childIds[index] !== target) graph.insertChildAt(target, originalLayer.id, index);
|
|
167
|
+
});
|
|
168
|
+
return next;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Makes the original of the set holding `versionId` look like that version, then removes the
|
|
172
|
+
* set. The original keeps its id, name, place among its siblings, and how its parent lays it
|
|
173
|
+
* out, and so does every layer of it that pairs with one of the version's (`matchLayers`), so
|
|
174
|
+
* what points at them keeps working; layers only the version has move in, and layers only the
|
|
175
|
+
* original had are deleted, apart from main components and sets, which are kept as they are,
|
|
176
|
+
* as when the version holds their stand-ins. Only fields that differ are written and only layers out of place
|
|
177
|
+
* are moved, so a pick sends no more changes than it makes. A main component stays one, keeping
|
|
178
|
+
* its properties and publishing. Comments stay pinned where they were; one on a removed version
|
|
179
|
+
* falls back to its stored point, as on any deleted layer. The caller records the edit as one step and lays out the original and its parent.
|
|
180
|
+
*/
|
|
181
|
+
function applyVariation(graph, versionId) {
|
|
182
|
+
const { view, version, problem } = variationToPick(graph, versionId);
|
|
183
|
+
if (problem || !view.original) throw new VariationError(problem ?? "original-missing", versionId);
|
|
184
|
+
const original = view.original;
|
|
185
|
+
const pairs = matchLayers(graph, original.id, version.id);
|
|
186
|
+
const paired = new Set(pairs.values());
|
|
187
|
+
const pending = [[version.id, original.id]];
|
|
188
|
+
for (const [versionLayerId, originalLayerId] of pending) {
|
|
189
|
+
const versionLayer = graph.getNode(versionLayerId);
|
|
190
|
+
const originalLayer = graph.getNode(originalLayerId);
|
|
191
|
+
if (!versionLayer || !originalLayer) continue;
|
|
192
|
+
const changes = changedFields(versionLayer, originalLayer, originalLayerId === original.id);
|
|
193
|
+
if (changes) graph.updateNode(originalLayerId, changes);
|
|
194
|
+
pending.push(...applyChildren(graph, pairs, paired, versionLayer, originalLayer));
|
|
195
|
+
}
|
|
196
|
+
retargetCopiedOverrides(graph, pairs);
|
|
197
|
+
renameReferences(graph, original.id, pairs);
|
|
198
|
+
graph.deleteNode(view.section.id);
|
|
199
|
+
return { originalId: original.id };
|
|
200
|
+
}
|
|
201
|
+
/** Removes the set holding `nodeId` and every version in it; the original is left as it is. */
|
|
202
|
+
function discardVariations(graph, nodeId) {
|
|
203
|
+
const view = variationSetOf(graph, nodeId);
|
|
204
|
+
if (!view) throw new VariationError("not-in-set", nodeId);
|
|
205
|
+
graph.deleteNode(view.section.id);
|
|
206
|
+
return { originalId: view.original?.id ?? null };
|
|
207
|
+
}
|
|
208
|
+
//#endregion
|
|
209
|
+
export { VariationError, applyVariation, discardVariations, variationPickProblem, variationToPick };
|
|
210
|
+
|
|
211
|
+
//# sourceMappingURL=pick.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pick.js","names":[],"sources":["../../src/variations/pick.ts"],"sourcesContent":["import { isEqual, pick } from 'es-toolkit'\n\nimport { cloneNodeProps } from '../copy'\nimport { MAIN_COMPONENT_FIELDS, PARENT_LAYOUT_FIELDS, TRANSFORM_FIELDS } from '../fields'\nimport type { SceneGraph } from '../graph'\nimport { findInstanceAncestor, retargetCopiedOverrides } from '../instances'\nimport { readPluginData } from '../plugin-data/field'\nimport { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'\nimport type { NodeType, PrototypeReaction, SceneNode } from '../types'\nimport { withStandIn } from './copy'\nimport { matchLayers } from './match'\nimport { variationSetOf, versionOf, type VariationSetView } from './model'\n\n/** Why a version cannot replace its original. */\nexport type VariationPickProblem =\n | 'not-in-set'\n | 'not-a-version'\n | 'original-missing'\n | 'original-in-instance'\n | 'original-locked'\n | 'version-type-differs'\n\nconst PROBLEM_MESSAGES: Record<VariationPickProblem, (nodeId: string) => string> = {\n 'not-in-set': (nodeId) => `\"${nodeId}\" is not in a set of variations`,\n 'not-a-version': (nodeId) => `\"${nodeId}\" is not a version: pick one of the set's layers`,\n 'original-missing': () => 'The layer these are variations of is gone; discard them instead',\n 'original-in-instance': () =>\n 'The layer these are variations of is inside an instance; vary the instance or its main component',\n 'original-locked': () =>\n 'The layer these are variations of is locked; unlock it to replace it with a version',\n 'version-type-differs': () =>\n 'This version is a different kind of layer than the original (such as a frame for an ' +\n 'instance), so it cannot replace it; change the layers inside a version instead of ' +\n 'replacing the version, or make a new version'\n}\n\n/** A layer that belongs to no set, or a version that cannot be used, with the reason. */\nexport class VariationError extends Error {\n constructor(\n readonly problem: VariationPickProblem,\n nodeId: string\n ) {\n super(PROBLEM_MESSAGES[problem](nodeId))\n this.name = 'VariationError'\n }\n}\n\n/** Never taken from a version: what places a layer in the tree, who it is, and its review. */\nconst OWN_FIELDS: ReadonlySet<string> = new Set<keyof SceneNode>([\n 'id',\n 'parentId',\n 'childIds',\n 'source',\n 'status'\n])\n\n/** What the original keeps of its own place: its name, where it sits, and how it is shown. */\nconst ORIGINAL_PLACE_FIELDS: ReadonlySet<string> = new Set<keyof SceneNode>([\n 'name',\n ...TRANSFORM_FIELDS.filter((field) => field !== 'flipX' && field !== 'flipY'),\n ...PARENT_LAYOUT_FIELDS,\n 'visible',\n 'locked'\n])\n\n/** What a main component original keeps: being a component, and its properties and publishing. */\nconst MAIN_COMPONENT_KEPT: ReadonlySet<string> = new Set<keyof SceneNode>([\n 'type',\n ...MAIN_COMPONENT_FIELDS\n])\n\nfunction standInMark(node: SceneNode | undefined): string | undefined {\n return node ? readPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn) : undefined\n}\n\n/** Removes stand-in marks from a layer moving into the original, and from its layers. */\nfunction clearStandInMarks(graph: SceneGraph, rootId: string): void {\n for (const node of subtree(graph, rootId)) {\n if (standInMark(node) !== undefined) {\n graph.updateNode(node.id, { pluginData: withStandIn(node, null) })\n }\n }\n}\n\n/** The layer at `rootId` and every layer inside it. */\nfunction* subtree(graph: SceneGraph, rootId: string): Generator<SceneNode> {\n const pending = [rootId]\n for (let id = pending.pop(); id !== undefined; id = pending.pop()) {\n const node = graph.getNode(id)\n if (!node) continue\n pending.push(...node.childIds)\n yield node\n }\n}\n\nfunction isMain(node: SceneNode | undefined): boolean {\n return node?.type === 'COMPONENT' || node?.type === 'COMPONENT_SET'\n}\n\n/** Whether a version whose root is `version` may replace an original of type `original`. */\nfunction replaces(version: NodeType, original: NodeType): boolean {\n // A main component's versions are frames.\n return version === original || (original === 'COMPONENT' && version === 'FRAME')\n}\n\n/** Why `versionId` cannot replace the original of `view`, or null when it can. */\nexport function variationPickProblem(\n graph: SceneGraph,\n view: VariationSetView,\n versionId: string\n): VariationPickProblem | null {\n const version = graph.getNode(versionId)\n if (version?.parentId !== view.section.id) return 'not-a-version'\n const { original } = view\n if (!original) return 'original-missing'\n if (original.parentId && findInstanceAncestor(graph, original.parentId)) {\n return 'original-in-instance'\n }\n if (original.locked) return 'original-locked'\n return replaces(version.type, original.type) ? null : 'version-type-differs'\n}\n\n/**\n * The set and version `versionId` names, a version or a layer inside one, and why it cannot\n * replace its original, or null when it can. Throws when `versionId` is in no set's versions.\n */\nexport function variationToPick(\n graph: SceneGraph,\n versionId: string\n): { view: VariationSetView; version: SceneNode; problem: VariationPickProblem | null } {\n const view = variationSetOf(graph, versionId)\n if (!view) throw new VariationError('not-in-set', versionId)\n const version = versionOf(graph, view, versionId)\n if (!version) throw new VariationError('not-a-version', versionId)\n return { view, version, problem: variationPickProblem(graph, view, version.id) }\n}\n\n/**\n * The version's fields that differ from what `original` holds, as `original` takes them, or null\n * when none do, so an unchanged layer gets no update at all.\n */\nfunction changedFields(\n versionLayer: SceneNode,\n original: SceneNode,\n isRoot: boolean\n): Partial<SceneNode> | null {\n // A stand-in mark stays with the version's layer; the original never takes it.\n const version =\n standInMark(versionLayer) === undefined\n ? versionLayer\n : { ...versionLayer, pluginData: withStandIn(versionLayer, null) }\n const kept = (key: string) =>\n OWN_FIELDS.has(key) ||\n (isRoot && ORIGINAL_PLACE_FIELDS.has(key)) ||\n (isRoot && original.type === 'COMPONENT' && MAIN_COMPONENT_KEPT.has(key))\n const keys = (Object.keys(version) as (keyof SceneNode)[]).filter(\n (key) => version[key] !== original[key] && !kept(key) && !isEqual(version[key], original[key])\n )\n return keys.length > 0 ? pick(cloneNodeProps(version, null), keys) : null\n}\n\nfunction renamedReactions(\n reactions: PrototypeReaction[],\n rename: (id: string) => string\n): PrototypeReaction[] | null {\n const renamed = (action: PrototypeReaction['actions'][number]) =>\n action.type === 'NODE' && action.nodeId !== null && rename(action.nodeId) !== action.nodeId\n if (!reactions.some((reaction) => reaction.actions.some(renamed))) return null\n return reactions.map((reaction) => ({\n ...reaction,\n actions: reaction.actions.map((action) =>\n action.type === 'NODE' && action.nodeId !== null\n ? { ...action, nodeId: rename(action.nodeId) }\n : action\n )\n }))\n}\n\n/**\n * Points what the kept layers say about each other, connector ends and prototype targets, at\n * the original's layers where they named the version's.\n */\nfunction renameReferences(graph: SceneGraph, rootId: string, pairs: ReadonlyMap<string, string>) {\n const rename = (id: string) => pairs.get(id) ?? id\n for (const node of subtree(graph, rootId)) {\n const changes: Partial<SceneNode> = {}\n for (const end of ['connectorStart', 'connectorEnd'] as const) {\n const endpoint = node[end]\n if (endpoint?.nodeId && pairs.has(endpoint.nodeId)) {\n changes[end] = { ...endpoint, nodeId: rename(endpoint.nodeId) }\n }\n }\n const reactions = renamedReactions(node.prototypeReactions, rename)\n if (reactions) changes.prototypeReactions = reactions\n if (Object.keys(changes).length > 0) graph.updateNode(node.id, changes)\n }\n}\n\n/**\n * Gives `originalLayer` the children of `versionLayer`, in its order: paired layers stay, layers\n * only the version has move in, and layers only the original had go, apart from main components\n * and sets, which keep their place. Returns the pairs to visit next.\n */\nfunction applyChildren(\n graph: SceneGraph,\n pairs: ReadonlyMap<string, string>,\n paired: ReadonlySet<string>,\n versionLayer: SceneNode,\n originalLayer: SceneNode\n): Array<[string, string]> {\n const originalChildren = [...originalLayer.childIds]\n // A main component or set is never deleted: instances elsewhere draw from it.\n const keptMains = originalChildren.flatMap((childId, index) =>\n !paired.has(childId) && isMain(graph.getNode(childId)) ? [{ childId, index }] : []\n )\n const unpaired = originalChildren.filter(\n (childId) => !paired.has(childId) && !isMain(graph.getNode(childId))\n )\n for (const childId of unpaired) graph.deleteNode(childId)\n\n const next: Array<[string, string]> = []\n const order: string[] = []\n for (const childId of versionLayer.childIds) {\n const pairedId = pairs.get(childId)\n // A stand-in for a component the document no longer has stands for nothing; any other\n // marked layer moving in is an ordinary layer, such as a copy of a stand-in.\n const mark = pairedId === undefined ? standInMark(graph.getNode(childId)) : undefined\n if (mark !== undefined && !graph.getNode(mark)) continue\n order.push(pairedId ?? childId)\n if (pairedId === undefined) clearStandInMarks(graph, childId)\n // A main component's stand-in leaves the component as it is (`standsIn`).\n else if (graph.getNode(childId)?.type === graph.getNode(pairedId)?.type) {\n next.push([childId, pairedId])\n }\n }\n // A kept main component keeps its place among the layers.\n for (const { childId, index } of keptMains) order.splice(index, 0, childId)\n order.forEach((target, index) => {\n // Read the list afresh: a move replaces it. A layer already in place is not moved.\n if (graph.getNode(originalLayer.id)?.childIds[index] !== target) {\n graph.insertChildAt(target, originalLayer.id, index)\n }\n })\n return next\n}\n\n/**\n * Makes the original of the set holding `versionId` look like that version, then removes the\n * set. The original keeps its id, name, place among its siblings, and how its parent lays it\n * out, and so does every layer of it that pairs with one of the version's (`matchLayers`), so\n * what points at them keeps working; layers only the version has move in, and layers only the\n * original had are deleted, apart from main components and sets, which are kept as they are,\n * as when the version holds their stand-ins. Only fields that differ are written and only layers out of place\n * are moved, so a pick sends no more changes than it makes. A main component stays one, keeping\n * its properties and publishing. Comments stay pinned where they were; one on a removed version\n * falls back to its stored point, as on any deleted layer. The caller records the edit as one step and lays out the original and its parent.\n */\nexport function applyVariation(graph: SceneGraph, versionId: string): { originalId: string } {\n const { view, version, problem } = variationToPick(graph, versionId)\n if (problem || !view.original) throw new VariationError(problem ?? 'original-missing', versionId)\n const original = view.original\n\n const pairs = matchLayers(graph, original.id, version.id)\n const paired = new Set(pairs.values())\n const pending: Array<[string, string]> = [[version.id, original.id]]\n // Pairs are appended while the list is walked, parents before their children.\n for (const [versionLayerId, originalLayerId] of pending) {\n const versionLayer = graph.getNode(versionLayerId)\n const originalLayer = graph.getNode(originalLayerId)\n if (!versionLayer || !originalLayer) continue\n const changes = changedFields(versionLayer, originalLayer, originalLayerId === original.id)\n if (changes) graph.updateNode(originalLayerId, changes)\n pending.push(...applyChildren(graph, pairs, paired, versionLayer, originalLayer))\n }\n\n // Instances among the kept layers record overrides of the version's layers; they are the\n // original's now.\n retargetCopiedOverrides(graph, pairs)\n renameReferences(graph, original.id, pairs)\n graph.deleteNode(view.section.id)\n return { originalId: original.id }\n}\n\n/** Removes the set holding `nodeId` and every version in it; the original is left as it is. */\nexport function discardVariations(\n graph: SceneGraph,\n nodeId: string\n): { originalId: string | null } {\n const view = variationSetOf(graph, nodeId)\n if (!view) throw new VariationError('not-in-set', nodeId)\n graph.deleteNode(view.section.id)\n return { originalId: view.original?.id ?? null }\n}\n"],"mappings":";;;;;;;;;;;;AAsBA,MAAM,mBAA6E;CACjF,eAAe,WAAW,IAAI,OAAO;CACrC,kBAAkB,WAAW,IAAI,OAAO;CACxC,0BAA0B;CAC1B,8BACE;CACF,yBACE;CACF,8BACE;AAGJ;;AAGA,IAAa,iBAAb,cAAoC,MAAM;CAE7B;CADX,YACE,SACA,QACA;EACA,MAAM,iBAAiB,QAAQ,CAAC,MAAM,CAAC;EAH9B,KAAA,UAAA;EAIT,KAAK,OAAO;CACd;AACF;;AAGA,MAAM,6BAAkC,IAAI,IAAqB;CAC/D;CACA;CACA;CACA;CACA;AACF,CAAC;;AAGD,MAAM,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AW,KAAK,QAAQ,EAAE;CAChC,OAAO,EAAE,YAAY,SAAS,GAAG;AACnC;;AAGA,SAAgB,kBACd,OACA,QAC+B;CAC/B,MAAM,OAAO,eAAe,OAAO,MAAM;CACzC,IAAI,CAAC,MAAM,MAAM,IAAI,eAAe,cAAc,MAAM;CACxD,MAAM,WAAW,KAAK,QAAQ,EAAE;CAChC,OAAO,EAAE,YAAY,KAAK,UAAU,MAAM,KAAK;AACjD"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
//#region src/variations/schema.d.ts
|
|
3
|
+
/** The most versions one request makes. */
|
|
4
|
+
declare const VARIATION_MAX_COUNT = 8;
|
|
5
|
+
/** The longest label a version's name carries. */
|
|
6
|
+
declare const VARIATION_LABEL_MAX_LENGTH = 40;
|
|
7
|
+
/** The most layers one request copies, counting every version's layers. */
|
|
8
|
+
declare const VARIATION_MAX_LAYERS = 10000;
|
|
9
|
+
/**
|
|
10
|
+
* What marks a section as a set of variations: the layer they are versions of, and a fingerprint
|
|
11
|
+
* of that layer when they were made, so a front end can say it changed since. The versions are
|
|
12
|
+
* the section's own children, so none of them needs a mark of its own.
|
|
13
|
+
*/
|
|
14
|
+
declare const variationSetSchema: v.ObjectSchema<{
|
|
15
|
+
readonly version: v.LiteralSchema<1, undefined>;
|
|
16
|
+
readonly originalId: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.NonEmptyAction<string, undefined>]>;
|
|
17
|
+
readonly originalFingerprint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
18
|
+
}, undefined>;
|
|
19
|
+
type VariationSet = v.InferOutput<typeof variationSetSchema>;
|
|
20
|
+
/**
|
|
21
|
+
* On a layer of a version, the id of the main component or component set of the original it
|
|
22
|
+
* stands in for: an instance of the component, or a frame of instances of the set's variants.
|
|
23
|
+
*/
|
|
24
|
+
declare const variationStandInSchema: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.NonEmptyAction<string, undefined>]>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { VARIATION_LABEL_MAX_LENGTH, VARIATION_MAX_COUNT, VARIATION_MAX_LAYERS, VariationSet, variationSetSchema, variationStandInSchema };
|
|
27
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","names":[],"sources":["../../src/variations/schema.ts"],"mappings":";;;cAGa;;cAGA;;cAGA;;;;;;cAOA,oBAAkB,EAAA;;;;;KAMnB,eAAe,EAAE,mBAAmB;;;;;cAMnC,wBAAsB,EAAA,yBAAA,EAAA,yBAAA,EAAA"}
|