@pairdraw/scene-graph 0.19.2 → 0.21.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/color/index.d.ts +28 -7
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +40 -15
- package/dist/color/index.js.map +1 -1
- package/dist/color/management.d.ts +19 -6
- package/dist/color/management.d.ts.map +1 -1
- package/dist/color/management.js +56 -1
- package/dist/color/management.js.map +1 -1
- package/dist/color/profile.d.ts +32 -0
- package/dist/color/profile.d.ts.map +1 -0
- package/dist/color/profile.js +140 -0
- package/dist/color/profile.js.map +1 -0
- package/dist/comments/index.d.ts +2 -2
- package/dist/comments/index.js +2 -2
- package/dist/comments/merge.d.ts.map +1 -1
- package/dist/comments/merge.js +1 -0
- package/dist/comments/merge.js.map +1 -1
- package/dist/comments/schema.d.ts +3 -1
- package/dist/comments/schema.d.ts.map +1 -1
- package/dist/comments/schema.js +19 -2
- package/dist/comments/schema.js.map +1 -1
- package/dist/comments/threads.d.ts +3 -1
- package/dist/comments/threads.d.ts.map +1 -1
- package/dist/comments/threads.js +2 -1
- package/dist/comments/threads.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +7 -7
- package/dist/components/properties.js.map +1 -1
- 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/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/shadows.d.ts +2 -2
- package/dist/css/shadows.d.ts.map +1 -1
- package/dist/css/shadows.js +4 -4
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/text.d.ts +17 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +62 -1
- package/dist/css/text.js.map +1 -1
- package/dist/css/values.d.ts +7 -2
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +8 -4
- package/dist/css/values.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 +102 -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/graph.d.ts +12 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +39 -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/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 +5 -173
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +3 -2
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +18 -2
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/copies.js +2 -1
- package/dist/instances/copies.js.map +1 -1
- 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 +13 -12
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/overrides.d.ts +15 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +37 -1
- package/dist/instances/overrides.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/instances/swap-overrides.js +3 -3
- package/dist/instances/swap-overrides.js.map +1 -1
- package/dist/instances/sync.js +9 -7
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +43 -38
- package/dist/node-defaults.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +90 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +33 -24
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/plugin-data/index.d.ts +2 -2
- package/dist/plugin-data/index.js +2 -2
- package/dist/plugin-data/schema.d.ts +6 -1
- package/dist/plugin-data/schema.d.ts.map +1 -1
- package/dist/plugin-data/schema.js +24 -1
- package/dist/plugin-data/schema.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/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +5 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/shaders/animation.d.ts +21 -0
- package/dist/shaders/animation.d.ts.map +1 -0
- package/dist/shaders/animation.js +25 -0
- package/dist/shaders/animation.js.map +1 -0
- package/dist/shaders/index.d.ts +2 -1
- package/dist/shaders/index.js +2 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +4 -4
- package/dist/slots/content.js.map +1 -1
- package/dist/statuses/index.d.ts +2 -2
- package/dist/statuses/index.js +2 -2
- package/dist/statuses/workflow.d.ts +3 -1
- package/dist/statuses/workflow.d.ts.map +1 -1
- package/dist/statuses/workflow.js +6 -2
- package/dist/statuses/workflow.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/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +1 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +135 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +6 -5
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +24 -16
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/formula/index.d.ts.map +1 -1
- package/dist/variables/formula/index.js +12 -6
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/index.d.ts +14 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +38 -10
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +9 -8
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +35 -26
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolver.d.ts +45 -0
- package/dist/variables/resolver.d.ts.map +1 -0
- package/dist/variables/resolver.js +106 -0
- package/dist/variables/resolver.js.map +1 -0
- 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/color/index.ts +43 -9
- package/src/color/management.ts +54 -1
- package/src/color/profile.ts +131 -0
- package/src/comments/merge.ts +3 -0
- package/src/comments/schema.ts +32 -2
- package/src/comments/threads.ts +5 -2
- package/src/components/properties.ts +13 -7
- package/src/components/variants.ts +39 -0
- package/src/constants.ts +75 -0
- package/src/copy.ts +20 -1
- package/src/css/index.ts +7 -1
- package/src/css/shadows.ts +8 -5
- package/src/css/text.ts +62 -0
- package/src/css/values.ts +12 -5
- 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 +117 -0
- package/src/fields/components.ts +21 -0
- package/src/fields/index.ts +1 -0
- package/src/fields/layout.ts +10 -0
- package/src/graph.ts +50 -6
- package/src/grid-values.ts +47 -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 +8 -277
- package/src/images.ts +27 -3
- package/src/instances/copies.ts +2 -1
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +20 -11
- package/src/instances/overrides.ts +49 -0
- package/src/instances/swap-overrides.ts +3 -2
- package/src/instances/sync.ts +11 -7
- package/src/node-defaults.ts +51 -41
- package/src/plugin-data/fields.ts +34 -32
- package/src/plugin-data/index.ts +1 -0
- package/src/plugin-data/schema.ts +30 -0
- package/src/preview.ts +2 -0
- package/src/scaling/node.ts +7 -2
- package/src/shaders/animation.ts +39 -0
- package/src/shaders/index.ts +1 -0
- package/src/slots/content.ts +8 -4
- package/src/statuses/workflow.ts +6 -4
- 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/variables.ts +2 -1
- package/src/types.ts +145 -14
- package/src/variables/bindings.ts +35 -18
- package/src/variables/formula/index.ts +19 -18
- package/src/variables/index.ts +49 -10
- package/src/variables/paints.ts +64 -29
- package/src/variables/resolver.ts +178 -0
- 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,106 @@
|
|
|
1
|
+
import { collectionsResolving, getActiveModeId, getNodeExplicitVariableModeId, getNodeVariableModeId, resolveVariableInModes, resolvedVariableModes } from "./resolution.js";
|
|
2
|
+
//#region src/variables/resolver.ts
|
|
3
|
+
function isColor(value) {
|
|
4
|
+
return typeof value === "object" && "r" in value;
|
|
5
|
+
}
|
|
6
|
+
/** Whether `modeId` names one of the collection's own modes. */
|
|
7
|
+
function collectionHasMode(graph, collectionId, modeId) {
|
|
8
|
+
return graph.variableCollections.get(collectionId)?.modes.some((mode) => mode.modeId === modeId) ?? false;
|
|
9
|
+
}
|
|
10
|
+
function reader(graph, modeFor, overrides) {
|
|
11
|
+
const memo = /* @__PURE__ */ new Map();
|
|
12
|
+
const storedValues = /* @__PURE__ */ new Map();
|
|
13
|
+
const strict = (variableId) => resolveVariableInModes(graph, variableId, modeFor, /* @__PURE__ */ new Set(), memo, overrides);
|
|
14
|
+
const value = (variableId) => {
|
|
15
|
+
try {
|
|
16
|
+
return strict(variableId);
|
|
17
|
+
} catch {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const stored = (variableId) => {
|
|
22
|
+
if (storedValues.has(variableId)) return storedValues.get(variableId);
|
|
23
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
24
|
+
let id = variableId;
|
|
25
|
+
for (;;) {
|
|
26
|
+
if (visiting.has(id)) return void 0;
|
|
27
|
+
visiting.add(id);
|
|
28
|
+
const variable = graph.variables.get(id);
|
|
29
|
+
if (!variable || !graph.variableCollections.has(variable.collectionId)) return void 0;
|
|
30
|
+
const mode = modeFor(variable.collectionId);
|
|
31
|
+
const valueInMode = variable.valuesByMode[mode];
|
|
32
|
+
if (!Object.hasOwn(variable.valuesByMode, mode)) return void 0;
|
|
33
|
+
if (valueInMode && typeof valueInMode === "object" && "aliasId" in valueInMode) {
|
|
34
|
+
id = valueInMode.aliasId;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
storedValues.set(variableId, valueInMode);
|
|
38
|
+
return valueInMode;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
value,
|
|
43
|
+
resolve: strict,
|
|
44
|
+
stored,
|
|
45
|
+
color: (variableId) => {
|
|
46
|
+
const resolved = value(variableId);
|
|
47
|
+
return resolved !== void 0 && isColor(resolved) ? resolved : void 0;
|
|
48
|
+
},
|
|
49
|
+
number: (variableId) => {
|
|
50
|
+
const resolved = value(variableId);
|
|
51
|
+
return typeof resolved === "number" ? resolved : void 0;
|
|
52
|
+
},
|
|
53
|
+
string: (variableId) => {
|
|
54
|
+
const resolved = value(variableId);
|
|
55
|
+
return typeof resolved === "string" ? resolved : void 0;
|
|
56
|
+
},
|
|
57
|
+
boolean: (variableId) => {
|
|
58
|
+
const resolved = value(variableId);
|
|
59
|
+
return typeof resolved === "boolean" ? resolved : void 0;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* One value resolver for canvas, editor, exports and tools. Create one per operation: the
|
|
65
|
+
* memo is only valid for the graph state it was built against.
|
|
66
|
+
*/
|
|
67
|
+
function createVariableResolver(graph, options = {}) {
|
|
68
|
+
const readers = /* @__PURE__ */ new Map();
|
|
69
|
+
const overrides = options.overrides;
|
|
70
|
+
const cached = (key, create) => {
|
|
71
|
+
let found = readers.get(key);
|
|
72
|
+
if (!found) {
|
|
73
|
+
found = create();
|
|
74
|
+
readers.set(key, found);
|
|
75
|
+
}
|
|
76
|
+
return found;
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
forNode(nodeId, fallback = "active") {
|
|
80
|
+
return cached(`node:${fallback}:${nodeId}`, () => reader(graph, (collectionId) => getNodeVariableModeId(graph, nodeId, collectionId, fallback), overrides));
|
|
81
|
+
},
|
|
82
|
+
active() {
|
|
83
|
+
return cached("active", () => reader(graph, (id) => getActiveModeId(graph, id), overrides));
|
|
84
|
+
},
|
|
85
|
+
forMode(collectionId, modeId) {
|
|
86
|
+
const mode = collectionHasMode(graph, collectionId, modeId) ? modeId : getActiveModeId(graph, collectionId);
|
|
87
|
+
return cached(JSON.stringify([
|
|
88
|
+
"mode",
|
|
89
|
+
collectionId,
|
|
90
|
+
mode
|
|
91
|
+
]), () => reader(graph, (id) => id === collectionId ? mode : getActiveModeId(graph, id), overrides));
|
|
92
|
+
},
|
|
93
|
+
forModes(modeFor) {
|
|
94
|
+
return reader(graph, modeFor, overrides);
|
|
95
|
+
},
|
|
96
|
+
activeMode: (collectionId) => getActiveModeId(graph, collectionId),
|
|
97
|
+
modeFor: (nodeId, collectionId, fallback = "active") => getNodeVariableModeId(graph, nodeId, collectionId, fallback),
|
|
98
|
+
explicitModeFor: (nodeId, collectionId) => getNodeExplicitVariableModeId(graph, nodeId, collectionId),
|
|
99
|
+
modesFor: (nodeId) => resolvedVariableModes(graph, nodeId),
|
|
100
|
+
dependencies: (variableIds) => collectionsResolving(graph, variableIds)
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
export { createVariableResolver };
|
|
105
|
+
|
|
106
|
+
//# sourceMappingURL=resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolver.js","names":[],"sources":["../../src/variables/resolver.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { Color } from '../primitives'\nimport type { VariableLiteral, VariableValue } from '../types'\nimport {\n collectionsResolving,\n getActiveModeId,\n getNodeExplicitVariableModeId,\n getNodeVariableModeId,\n resolveVariableInModes,\n resolvedVariableModes,\n type VariableModeFallback\n} from './resolution'\n\n/** One reader per mode context; completed values are shared inside the resolver. */\nexport interface VariableReader {\n /** Any variable type, following aliases and expressions. Undefined when it cannot resolve. */\n value(variableId: string): VariableLiteral | undefined\n /** Strict form for formula drafts and diagnostics; throws VariableResolutionError. */\n resolve(variableId: string): VariableLiteral\n /** Stored value in this context's mode, with no default fallback; aliases are followed. */\n stored(variableId: string): VariableValue | undefined\n\n color(variableId: string): Color | undefined\n number(variableId: string): number | undefined\n string(variableId: string): string | undefined\n boolean(variableId: string): boolean | undefined\n}\n\nexport interface VariableResolver {\n forNode(nodeId: string, fallback?: VariableModeFallback): VariableReader\n active(): VariableReader\n /**\n * The collection's `modeId`, with every other collection in its active mode. A mode id from\n * another collection is not an explicit mode here, so the collection keeps its active one.\n */\n forMode(collectionId: string, modeId: string): VariableReader\n forModes(modeFor: (collectionId: string) => string): VariableReader\n\n activeMode(collectionId: string): string\n modeFor(nodeId: string, collectionId: string, fallback?: VariableModeFallback): string\n explicitModeFor(nodeId: string, collectionId: string): string | null\n modesFor(nodeId: string): Record<string, string>\n dependencies(variableIds: Iterable<string>): Set<string>\n}\n\nexport interface VariableResolverOptions {\n /** Draft values for formula validation; stored reads still use the graph. */\n overrides?: ReadonlyMap<string, VariableValue>\n}\n\nfunction isColor(value: VariableLiteral): value is Color {\n return typeof value === 'object' && 'r' in value\n}\n\n/** Whether `modeId` names one of the collection's own modes. */\nfunction collectionHasMode(graph: SceneGraph, collectionId: string, modeId: string): boolean {\n const collection = graph.variableCollections.get(collectionId)\n return collection?.modes.some((mode) => mode.modeId === modeId) ?? false\n}\n\nfunction reader(\n graph: SceneGraph,\n modeFor: (collectionId: string) => string,\n overrides?: ReadonlyMap<string, VariableValue>\n): VariableReader {\n const memo = new Map<string, VariableLiteral>()\n const storedValues = new Map<string, VariableValue | undefined>()\n\n const strict = (variableId: string) =>\n resolveVariableInModes(graph, variableId, modeFor, new Set(), memo, overrides)\n\n const value = (variableId: string): VariableLiteral | undefined => {\n try {\n return strict(variableId)\n } catch {\n return undefined\n }\n }\n\n const stored = (variableId: string): VariableValue | undefined => {\n if (storedValues.has(variableId)) return storedValues.get(variableId)\n const visiting = new Set<string>()\n let id = variableId\n for (;;) {\n if (visiting.has(id)) return undefined\n visiting.add(id)\n const variable = graph.variables.get(id)\n if (!variable || !graph.variableCollections.has(variable.collectionId)) return undefined\n const mode = modeFor(variable.collectionId)\n const valueInMode = variable.valuesByMode[mode]\n if (!Object.hasOwn(variable.valuesByMode, mode)) return undefined\n if (valueInMode && typeof valueInMode === 'object' && 'aliasId' in valueInMode) {\n id = valueInMode.aliasId\n continue\n }\n storedValues.set(variableId, valueInMode)\n return valueInMode\n }\n }\n\n return {\n value,\n resolve: strict,\n stored,\n color: (variableId) => {\n const resolved = value(variableId)\n return resolved !== undefined && isColor(resolved) ? resolved : undefined\n },\n number: (variableId) => {\n const resolved = value(variableId)\n return typeof resolved === 'number' ? resolved : undefined\n },\n string: (variableId) => {\n const resolved = value(variableId)\n return typeof resolved === 'string' ? resolved : undefined\n },\n boolean: (variableId) => {\n const resolved = value(variableId)\n return typeof resolved === 'boolean' ? resolved : undefined\n }\n }\n}\n\n/**\n * One value resolver for canvas, editor, exports and tools. Create one per operation: the\n * memo is only valid for the graph state it was built against.\n */\nexport function createVariableResolver(\n graph: SceneGraph,\n options: VariableResolverOptions = {}\n): VariableResolver {\n const readers = new Map<string, VariableReader>()\n const overrides = options.overrides\n\n const cached = (key: string, create: () => VariableReader): VariableReader => {\n let found = readers.get(key)\n if (!found) {\n found = create()\n readers.set(key, found)\n }\n return found\n }\n\n return {\n forNode(nodeId, fallback = 'active') {\n return cached(`node:${fallback}:${nodeId}`, () =>\n reader(\n graph,\n (collectionId) => getNodeVariableModeId(graph, nodeId, collectionId, fallback),\n overrides\n )\n )\n },\n active() {\n return cached('active', () => reader(graph, (id) => getActiveModeId(graph, id), overrides))\n },\n forMode(collectionId, modeId) {\n // A mode id is scoped to its collection, so one from another collection selects nothing:\n // it means \"no explicit mode\" for this collection, not that collection's default mode.\n const mode = collectionHasMode(graph, collectionId, modeId)\n ? modeId\n : getActiveModeId(graph, collectionId)\n return cached(JSON.stringify(['mode', collectionId, mode]), () =>\n reader(graph, (id) => (id === collectionId ? mode : getActiveModeId(graph, id)), overrides)\n )\n },\n forModes(modeFor) {\n return reader(graph, modeFor, overrides)\n },\n activeMode: (collectionId) => getActiveModeId(graph, collectionId),\n modeFor: (nodeId, collectionId, fallback = 'active') =>\n getNodeVariableModeId(graph, nodeId, collectionId, fallback),\n explicitModeFor: (nodeId, collectionId) =>\n getNodeExplicitVariableModeId(graph, nodeId, collectionId),\n modesFor: (nodeId) => resolvedVariableModes(graph, nodeId),\n dependencies: (variableIds) => collectionsResol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AAU,QAAQ,cAAc,WAAW,aACzC,sBAAsB,OAAO,QAAQ,cAAc,QAAQ;EAC7D,kBAAkB,QAAQ,iBACxB,8BAA8B,OAAO,QAAQ,YAAY;EAC3D,WAAW,WAAW,sBAAsB,OAAO,MAAM;EACzD,eAAe,gBAAgB,qBAAqB,OAAO,WAAW;CACxE;AACF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SceneGraph } from "../graph.js";
|
|
2
|
+
import { PluginDataEntry, SceneNode } from "../types.js";
|
|
3
|
+
//#region src/variations/copy.d.ts
|
|
4
|
+
/** `node`'s plugin data marking it as the stand-in for `mainId`, or unmarked for null. */
|
|
5
|
+
declare function withStandIn(node: Pick<SceneNode, 'pluginData'>, mainId: string | null): PluginDataEntry[];
|
|
6
|
+
/**
|
|
7
|
+
* Copies `source` under `parentId` as a version: its root is never a main component or a
|
|
8
|
+
* component set (it becomes a plain frame), and the main components inside it are instances of
|
|
9
|
+
* the components they copy. Instances stay instances. Each layer standing in for a main
|
|
10
|
+
* component or set is marked with its id (`variationStandIn`), which is how a pick knows it. Right after a copy the two trees have the
|
|
11
|
+
* same shape, so each copied layer is found at its source's place.
|
|
12
|
+
*/
|
|
13
|
+
declare function copyVersion(graph: SceneGraph, sourceId: string, parentId: string, overrides: Partial<SceneNode>): SceneNode | null;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { copyVersion, withStandIn };
|
|
16
|
+
//# sourceMappingURL=copy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copy.d.ts","names":[],"sources":["../../src/variations/copy.ts"],"mappings":";;;;iBAUgB,YAAY,MAAM,KAAK,0BAA0B,wBAA/B;;;;;;;;iBAoClB,YACd,OAAO,YACP,kBACA,kBACA,WAAW,QAAQ,aAClB"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createDefaultNode } from "../node-defaults.js";
|
|
2
|
+
import { readPluginData, withPluginData } from "../plugin-data/field.js";
|
|
3
|
+
import { PAIRDRAW_PLUGIN_DATA } from "../plugin-data/fields.js";
|
|
4
|
+
import { MAIN_COMPONENT_FIELDS } from "../fields/components.js";
|
|
5
|
+
import { pick } from "es-toolkit";
|
|
6
|
+
//#region src/variations/copy.ts
|
|
7
|
+
/** `node`'s plugin data marking it as the stand-in for `mainId`, or unmarked for null. */
|
|
8
|
+
function withStandIn(node, mainId) {
|
|
9
|
+
return withPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn, mainId ?? void 0);
|
|
10
|
+
}
|
|
11
|
+
/** What a copied main component or component set clears to become a plain frame. */
|
|
12
|
+
function plainFrameFields() {
|
|
13
|
+
const frame = createDefaultNode(() => "", "FRAME");
|
|
14
|
+
return {
|
|
15
|
+
type: "FRAME",
|
|
16
|
+
...pick(frame, MAIN_COMPONENT_FIELDS)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A copied main component inside the copy shows the component it copied, as an instance in its
|
|
21
|
+
* place, so copies never add main components; one that cannot become an instance, such as one
|
|
22
|
+
* that would hold itself, stays a plain frame.
|
|
23
|
+
*/
|
|
24
|
+
function instanceInPlace(graph, component, copy, index) {
|
|
25
|
+
const parentId = copy.parentId;
|
|
26
|
+
const instance = parentId ? graph.createInstance(component.id, parentId, {
|
|
27
|
+
name: copy.name,
|
|
28
|
+
x: copy.x,
|
|
29
|
+
y: copy.y
|
|
30
|
+
}) : null;
|
|
31
|
+
if (!instance || !parentId) {
|
|
32
|
+
graph.updateNode(copy.id, plainFrameFields());
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
graph.updateNode(instance.id, { pluginData: withStandIn(instance, component.id) });
|
|
36
|
+
graph.insertChildAt(instance.id, parentId, index);
|
|
37
|
+
graph.deleteNode(copy.id);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Copies `source` under `parentId` as a version: its root is never a main component or a
|
|
41
|
+
* component set (it becomes a plain frame), and the main components inside it are instances of
|
|
42
|
+
* the components they copy. Instances stay instances. Each layer standing in for a main
|
|
43
|
+
* component or set is marked with its id (`variationStandIn`), which is how a pick knows it. Right after a copy the two trees have the
|
|
44
|
+
* same shape, so each copied layer is found at its source's place.
|
|
45
|
+
*/
|
|
46
|
+
function copyVersion(graph, sourceId, parentId, overrides) {
|
|
47
|
+
const copy = graph.cloneTree(sourceId, parentId, overrides);
|
|
48
|
+
if (!copy) return null;
|
|
49
|
+
if (copy.type === "COMPONENT" || copy.type === "COMPONENT_SET") graph.updateNode(copy.id, plainFrameFields());
|
|
50
|
+
const pending = [[sourceId, copy.id]];
|
|
51
|
+
for (const [fromId, toId] of pending) {
|
|
52
|
+
const from = graph.getNode(fromId);
|
|
53
|
+
const to = graph.getNode(toId);
|
|
54
|
+
if (!from || !to || from.type === "INSTANCE") continue;
|
|
55
|
+
const copied = [...to.childIds];
|
|
56
|
+
from.childIds.forEach((childId, index) => {
|
|
57
|
+
const child = graph.getNode(childId);
|
|
58
|
+
const childCopy = graph.getNode(copied[index] ?? "");
|
|
59
|
+
if (!child || !childCopy) return;
|
|
60
|
+
const mark = readPluginData(child.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn);
|
|
61
|
+
if (mark !== void 0) graph.updateNode(childCopy.id, { pluginData: withStandIn(childCopy, mark) });
|
|
62
|
+
if (child.type === "COMPONENT") {
|
|
63
|
+
instanceInPlace(graph, child, childCopy, index);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (child.type === "COMPONENT_SET") graph.updateNode(childCopy.id, {
|
|
67
|
+
...plainFrameFields(),
|
|
68
|
+
pluginData: withStandIn(childCopy, child.id)
|
|
69
|
+
});
|
|
70
|
+
pending.push([childId, childCopy.id]);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return graph.getNode(copy.id) ?? null;
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { copyVersion, withStandIn };
|
|
77
|
+
|
|
78
|
+
//# sourceMappingURL=copy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copy.js","names":[],"sources":["../../src/variations/copy.ts"],"sourcesContent":["import { pick } from 'es-toolkit'\n\nimport { MAIN_COMPONENT_FIELDS } from '../fields'\nimport type { SceneGraph } from '../graph'\nimport { createDefaultNode } from '../node-defaults'\nimport { readPluginData, withPluginData } from '../plugin-data/field'\nimport { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'\nimport type { SceneNode } from '../types'\n\n/** `node`'s plugin data marking it as the stand-in for `mainId`, or unmarked for null. */\nexport function withStandIn(node: Pick<SceneNode, 'pluginData'>, mainId: string | null) {\n return withPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn, mainId ?? undefined)\n}\n\n/** What a copied main component or component set clears to become a plain frame. */\nfunction plainFrameFields(): Partial<SceneNode> {\n const frame = createDefaultNode(() => '', 'FRAME')\n return { type: 'FRAME', ...pick(frame, MAIN_COMPONENT_FIELDS) }\n}\n\n/**\n * A copied main component inside the copy shows the component it copied, as an instance in its\n * place, so copies never add main components; one that cannot become an instance, such as one\n * that would hold itself, stays a plain frame.\n */\nfunction instanceInPlace(graph: SceneGraph, component: SceneNode, copy: SceneNode, index: number) {\n const parentId = copy.parentId\n const instance = parentId\n ? graph.createInstance(component.id, parentId, { name: copy.name, x: copy.x, y: copy.y })\n : null\n if (!instance || !parentId) {\n graph.updateNode(copy.id, plainFrameFields())\n return\n }\n graph.updateNode(instance.id, { pluginData: withStandIn(instance, component.id) })\n graph.insertChildAt(instance.id, parentId, index)\n graph.deleteNode(copy.id)\n}\n\n/**\n * Copies `source` under `parentId` as a version: its root is never a main component or a\n * component set (it becomes a plain frame), and the main components inside it are instances of\n * the components they copy. Instances stay instances. Each layer standing in for a main\n * component or set is marked with its id (`variationStandIn`), which is how a pick knows it. Right after a copy the two trees have the\n * same shape, so each copied layer is found at its source's place.\n */\nexport function copyVersion(\n graph: SceneGraph,\n sourceId: string,\n parentId: string,\n overrides: Partial<SceneNode>\n): SceneNode | null {\n const copy = graph.cloneTree(sourceId, parentId, overrides)\n if (!copy) return null\n if (copy.type === 'COMPONENT' || copy.type === 'COMPONENT_SET') {\n graph.updateNode(copy.id, plainFrameFields())\n }\n const pending: Array<[string, string]> = [[sourceId, copy.id]]\n // Pairs are appended while the list is walked, parents before their children.\n for (const [fromId, toId] of pending) {\n const from = graph.getNode(fromId)\n const to = graph.getNode(toId)\n // An instance's layers are its component's copies, kept by sync.\n if (!from || !to || from.type === 'INSTANCE') continue\n const copied = [...to.childIds]\n from.childIds.forEach((childId, index) => {\n const child = graph.getNode(childId)\n const childCopy = graph.getNode(copied[index] ?? '')\n if (!child || !childCopy) return\n // Copying a version copies its stand-ins as stand-ins, which a plain copy drops.\n const mark = readPluginData(child.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn)\n if (mark !== undefined) {\n graph.updateNode(childCopy.id, { pluginData: withStandIn(childCopy, mark) })\n }\n if (child.type === 'COMPONENT') {\n instanceInPlace(graph, child, childCopy, index)\n return\n }\n if (child.type === 'COMPONENT_SET') {\n graph.updateNode(childCopy.id, {\n ...plainFrameFields(),\n pluginData: withStandIn(childCopy, child.id)\n })\n }\n pending.push([childId, childCopy.id])\n })\n }\n return graph.getNode(copy.id) ?? null\n}\n"],"mappings":";;;;;;;AAUA,SAAgB,YAAY,MAAqC,QAAuB;CACtF,OAAO,eAAe,KAAK,YAAY,qBAAqB,kBAAkB,UAAU,KAAA,CAAS;AACnG;;AAGA,SAAS,mBAAuC;CAC9C,MAAM,QAAQ,wBAAwB,IAAI,OAAO;CACjD,OAAO;EAAE,MAAM;EAAS,GAAG,KAAK,OAAO,qBAAqB;CAAE;AAChE;;;;;;AAOA,SAAS,gBAAgB,OAAmB,WAAsB,MAAiB,OAAe;CAChG,MAAM,WAAW,KAAK;CACtB,MAAM,WAAW,WACb,MAAM,eAAe,UAAU,IAAI,UAAU;EAAE,MAAM,KAAK;EAAM,GAAG,KAAK;EAAG,GAAG,KAAK;CAAE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AY,YAAY,WAAW,MAAM,EAAE;GAC7C,CAAC;GAEH,QAAQ,KAAK,CAAC,SAAS,UAAU,EAAE,CAAC;EACtC,CAAC;CACH;CACA,OAAO,MAAM,QAAQ,KAAK,EAAE,KAAK;AACnC"}
|
|
@@ -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,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AAM,0BAAU,IAAI,IAAkC;CACtD,MAAM,wBAAQ,IAAI,IAAY;CAC9B,MAAM,QAAuC,CAAC;CAC9C,MAAM,OAAoB,CAAC;CAC3B,KAAK,MAAM,WAAW,UAAU;EAC9B,MAAM,SAAS,eAAe,QAAQ,YAAY,qBAAqB,gBAAgB;EACvF,MAAM,OAAO,WAAW,KAAA,IAAY,KAAA,IAAY,MAAM,IAAI,MAAM;EAChE,IAAI,QAAQ,CAAC,MAAM,IAAI,KAAK,EAAE,KAAK,SAAS,SAAS,IAAI,GAAG;GAC1D,MAAM,IAAI,KAAK,EAAE;GACjB,MAAM,KAAK,CAAC,SAAS,IAAI,CAAC;GAC1B;EACF;EACA,MAAM,QAAQ,SAAS,OAAO,GAAG,QAAQ,KAAK,QAAQ,QAAQ,SAAS,SAAS,KAAK;EACrF,IAAI,OAAO,MAAM,KAAK,CAAC,SAAS,KAAK,CAAC;OACjC,KAAK,KAAK,OAAO;CACxB;CACA,KAAK,MAAM,WAAW,MAAM;EAC1B,MAAM,QAAQ,SAAS,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,K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AAW,KAAK,QAAQ,IAAI,GAAG;EACjE,MAAM,OAAO,MAAM,QAAQ,EAAE;EAC7B,IAAI,CAAC,MAAM;EACX,OAAO,SACL,MACA;GACE,KAAK;GACL,KAAK;GACL,OAAO,SAAS,KAAK,KAAK;GAC1B,OAAO,SAAS,KAAK,KAAK;GAC1B,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK,MAAM;GACX,KAAK,SAAS;EAChB,CAAC,CAAC,KAAK,IAAQ,CACjB;EACA,QAAQ,KAAK,GAAG,KAAK,QAAQ;CAC/B;CACA,OAAO,KAAK,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG;AAC1C;;AAGA,SAAgB,qBAAqB,OAAmB,MAAiC;CACvF,MAAM,EAAE,UAAU,QAAQ;CAC1B,IAAI,CAAC,YAAY,IAAI,wBAAwB,KAAA,GAAW,OAAO;CAC/D,OAAO,qBAAqB,OAAO,SAAS,EAAE,MAAM,IAAI;AAC1D"}
|
|
@@ -0,0 +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"}
|