@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,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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AAe;EACnD,IAAI,CAAC,gBAAgB,CAAC,eAAe;EACrC,MAAM,UAAU,cAAc,cAAc,eAAe,oBAAoB,SAAS,EAAE;EAC1F,IAAI,SAAS,MAAM,WAAW,iBAAiB,OAAO;EACtD,QAAQ,KAAK,GAAG,cAAc,OAAO,OAAO,QAAQ,cAAc,aAAa,CAAC;CAClF;CAIA,wBAAwB,OAAO,KAAK;CACpC,iBAAiB,OAAO,SAAS,IAAI,KAAK;CAC1C,MAAM,WAAW,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"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
//#region src/variations/schema.ts
|
|
3
|
+
/** The most versions one request makes. */
|
|
4
|
+
const VARIATION_MAX_COUNT = 8;
|
|
5
|
+
/** The longest label a version's name carries. */
|
|
6
|
+
const VARIATION_LABEL_MAX_LENGTH = 40;
|
|
7
|
+
/** The most layers one request copies, counting every version's layers. */
|
|
8
|
+
const VARIATION_MAX_LAYERS = 1e4;
|
|
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
|
+
const variationSetSchema = v.object({
|
|
15
|
+
version: v.literal(1),
|
|
16
|
+
originalId: v.pipe(v.string(), v.nonEmpty()),
|
|
17
|
+
originalFingerprint: v.optional(v.string())
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* On a layer of a version, the id of the main component or component set of the original it
|
|
21
|
+
* stands in for: an instance of the component, or a frame of instances of the set's variants.
|
|
22
|
+
*/
|
|
23
|
+
const variationStandInSchema = v.pipe(v.string(), v.nonEmpty());
|
|
24
|
+
//#endregion
|
|
25
|
+
export { VARIATION_LABEL_MAX_LENGTH, VARIATION_MAX_COUNT, VARIATION_MAX_LAYERS, variationSetSchema, variationStandInSchema };
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","names":[],"sources":["../../src/variations/schema.ts"],"sourcesContent":["import * as v from 'valibot'\n\n/** The most versions one request makes. */\nexport const VARIATION_MAX_COUNT = 8\n\n/** The longest label a version's name carries. */\nexport const VARIATION_LABEL_MAX_LENGTH = 40\n\n/** The most layers one request copies, counting every version's layers. */\nexport const VARIATION_MAX_LAYERS = 10_000\n\n/**\n * What marks a section as a set of variations: the layer they are versions of, and a fingerprint\n * of that layer when they were made, so a front end can say it changed since. The versions are\n * the section's own children, so none of them needs a mark of its own.\n */\nexport const variationSetSchema = v.object({\n version: v.literal(1),\n originalId: v.pipe(v.string(), v.nonEmpty()),\n originalFingerprint: v.optional(v.string())\n})\n\nexport type VariationSet = v.InferOutput<typeof variationSetSchema>\n\n/**\n * On a layer of a version, the id of the main component or component set of the original it\n * stands in for: an instance of the component, or a frame of instances of the set's variants.\n */\nexport const variationStandInSchema = v.pipe(v.string(), v.nonEmpty())\n"],"mappings":";;;AAGA,MAAa,sBAAsB;;AAGnC,MAAa,6BAA6B;;AAG1C,MAAa,uBAAuB;;;;;;AAOpC,MAAa,qBAAqB,EAAE,OAAO;CACzC,SAAS,EAAE,QAAQ,CAAC;CACpB,YAAY,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC;CAC3C,qBAAqB,EAAE,SAAS,EAAE,OAAO,CAAC;AAC5C,CAAC;;;;;AAQD,MAAa,yBAAyB,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairdraw/scene-graph",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -95,6 +95,11 @@
|
|
|
95
95
|
"import": "./dist/css/index.js",
|
|
96
96
|
"default": "./dist/css/index.js"
|
|
97
97
|
},
|
|
98
|
+
"./design-memory": {
|
|
99
|
+
"types": "./dist/design-memory/index.d.ts",
|
|
100
|
+
"import": "./dist/design-memory/index.js",
|
|
101
|
+
"default": "./dist/design-memory/index.js"
|
|
102
|
+
},
|
|
98
103
|
"./effects": {
|
|
99
104
|
"types": "./dist/effects.d.ts",
|
|
100
105
|
"import": "./dist/effects.js",
|
|
@@ -155,6 +160,11 @@
|
|
|
155
160
|
"import": "./dist/guides.js",
|
|
156
161
|
"default": "./dist/guides.js"
|
|
157
162
|
},
|
|
163
|
+
"./grid-values": {
|
|
164
|
+
"types": "./dist/grid-values.d.ts",
|
|
165
|
+
"import": "./dist/grid-values.js",
|
|
166
|
+
"default": "./dist/grid-values.js"
|
|
167
|
+
},
|
|
158
168
|
"./hit-test": {
|
|
159
169
|
"types": "./dist/hit-test.d.ts",
|
|
160
170
|
"import": "./dist/hit-test.js",
|
|
@@ -320,6 +330,11 @@
|
|
|
320
330
|
"import": "./dist/text/picture.js",
|
|
321
331
|
"default": "./dist/text/picture.js"
|
|
322
332
|
},
|
|
333
|
+
"./token-sources": {
|
|
334
|
+
"types": "./dist/token-sources/index.d.ts",
|
|
335
|
+
"import": "./dist/token-sources/index.js",
|
|
336
|
+
"default": "./dist/token-sources/index.js"
|
|
337
|
+
},
|
|
323
338
|
"./transfer": {
|
|
324
339
|
"types": "./dist/transfer/index.d.ts",
|
|
325
340
|
"import": "./dist/transfer/index.js",
|
|
@@ -345,6 +360,11 @@
|
|
|
345
360
|
"import": "./dist/variant-properties.js",
|
|
346
361
|
"default": "./dist/variant-properties.js"
|
|
347
362
|
},
|
|
363
|
+
"./variations": {
|
|
364
|
+
"types": "./dist/variations/index.d.ts",
|
|
365
|
+
"import": "./dist/variations/index.js",
|
|
366
|
+
"default": "./dist/variations/index.js"
|
|
367
|
+
},
|
|
348
368
|
"./vector-network": {
|
|
349
369
|
"types": "./dist/vector-network.d.ts",
|
|
350
370
|
"import": "./dist/vector-network.js",
|
package/src/color/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './contrast'
|
|
2
2
|
export * from './management'
|
|
3
|
+
export * from './profile'
|
|
3
4
|
export { normalizeColor } from './normalize'
|
|
4
5
|
export * from './okhcl'
|
|
5
6
|
export { colorSchema } from './schema'
|
|
@@ -8,22 +9,35 @@ import { parse, formatHex, formatHex8, formatRgb, converter, differenceEuclidean
|
|
|
8
9
|
|
|
9
10
|
import { BLACK } from '../constants'
|
|
10
11
|
import type { Color } from '../primitives'
|
|
12
|
+
import type { DocumentColorSpace } from '../types'
|
|
13
|
+
import { fitIntoProfile, SRGB_SYNTAX_MODES } from './management'
|
|
11
14
|
import { normalizeColor } from './normalize'
|
|
15
|
+
import { colorToProfileCSS } from './profile'
|
|
12
16
|
|
|
13
17
|
const toRGB = converter('rgb')
|
|
14
18
|
|
|
15
|
-
/**
|
|
16
|
-
|
|
19
|
+
/**
|
|
20
|
+
* A CSS color as components in `profile`, or null when `input` is not one. Hex and the other sRGB
|
|
21
|
+
* syntaxes give their numbers as they are, as Figma takes typed or imported hex as the document's
|
|
22
|
+
* own numbers in either profile. A color that names its space, such as `color(display-p3 …)` or
|
|
23
|
+
* `oklch()`, is converted into `profile`, and brought inside it by reducing chroma when the profile
|
|
24
|
+
* cannot show it.
|
|
25
|
+
*/
|
|
26
|
+
export function tryParseColor(input: string, profile: DocumentColorSpace = 'srgb'): Color | null {
|
|
17
27
|
const parsed = parse(input)
|
|
18
28
|
if (!parsed) return null
|
|
19
|
-
|
|
29
|
+
// A component written past the float range, such as `1e999`, is no color.
|
|
30
|
+
if (Object.values(parsed).some((value) => typeof value === 'number' && !Number.isFinite(value)))
|
|
31
|
+
return null
|
|
32
|
+
const a = parsed.alpha ?? 1
|
|
20
33
|
// culori leaves a `none` channel out, which normalizing reads as zero.
|
|
21
|
-
return normalizeColor({
|
|
34
|
+
if (SRGB_SYNTAX_MODES.has(parsed.mode)) return normalizeColor({ ...toRGB(parsed), a })
|
|
35
|
+
return fitIntoProfile(parsed, profile).color
|
|
22
36
|
}
|
|
23
37
|
|
|
24
|
-
/** A CSS color
|
|
25
|
-
export function parseColor(input: string): Color {
|
|
26
|
-
return tryParseColor(input) ?? { ...BLACK }
|
|
38
|
+
/** A CSS color as components in `profile`, or black when `input` is not one. */
|
|
39
|
+
export function parseColor(input: string, profile: DocumentColorSpace = 'srgb'): Color {
|
|
40
|
+
return tryParseColor(input, profile) ?? { ...BLACK }
|
|
27
41
|
}
|
|
28
42
|
|
|
29
43
|
export function colorToHex(color: Color): string {
|
|
@@ -60,12 +74,32 @@ export function colorToCSSCompact(color: Color): string {
|
|
|
60
74
|
return `rgba(${r},${g},${b},${a})`
|
|
61
75
|
}
|
|
62
76
|
|
|
77
|
+
/**
|
|
78
|
+
* A stored color as CSS in its document's profile: opaque ones as hex and translucent ones as
|
|
79
|
+
* `rgba()`, which Tailwind matches to its palette, or `color(display-p3 …)` in Display P3, where
|
|
80
|
+
* hex has no room for a component past 1. Colors already in the profile are written as stored.
|
|
81
|
+
*/
|
|
82
|
+
export function colorToDocumentCSS(color: Color, profile: DocumentColorSpace = 'srgb'): string {
|
|
83
|
+
if (profile === 'srgb') return color.a >= 1 ? colorToHex(color) : colorToCSS(color)
|
|
84
|
+
return colorToProfileCSS(color, profile, 'display-p3')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A stored color as CSS for a preview of it, such as a swatch or a slider thumb: exactly
|
|
89
|
+
* `colorToCSS` in an sRGB document, so nothing a document already showed changes, and in its own
|
|
90
|
+
* profile otherwise, where `rgb()` cannot show a component past 1.
|
|
91
|
+
*/
|
|
92
|
+
export function colorToPreviewCSS(color: Color, profile: DocumentColorSpace = 'srgb'): string {
|
|
93
|
+
return profile === 'srgb' ? colorToCSS(color) : colorToDocumentCSS(color, profile)
|
|
94
|
+
}
|
|
95
|
+
|
|
63
96
|
export function rgba255ToColor(r: number, g: number, b: number, a = 1): Color {
|
|
64
97
|
return { r: r / 255, g: g / 255, b: b / 255, a }
|
|
65
98
|
}
|
|
66
99
|
|
|
67
|
-
|
|
68
|
-
|
|
100
|
+
/** A solid paint of `color`, a CSS color read into `profile` or components already in it. */
|
|
101
|
+
export function colorToFill(color: string | Color, profile: DocumentColorSpace = 'srgb') {
|
|
102
|
+
const rgba = typeof color === 'string' ? parseColor(color, profile) : color
|
|
69
103
|
return {
|
|
70
104
|
type: 'SOLID' as const,
|
|
71
105
|
color: { r: rgba.r, g: rgba.g, b: rgba.b, a: rgba.a },
|
package/src/color/management.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
clampGamut,
|
|
3
|
+
converter,
|
|
4
|
+
formatCss,
|
|
5
|
+
formatRgb,
|
|
6
|
+
inGamut,
|
|
7
|
+
parse,
|
|
8
|
+
toGamut,
|
|
9
|
+
type Color as CuloriColor
|
|
10
|
+
} from 'culori'
|
|
2
11
|
|
|
3
12
|
import type { Color } from '../primitives'
|
|
4
13
|
import type { DocumentColorSpace, Fill, SceneNode, Stroke } from '../types'
|
|
@@ -150,6 +159,50 @@ export function resolveOkHCLForPreview(
|
|
|
150
159
|
}
|
|
151
160
|
}
|
|
152
161
|
|
|
162
|
+
/**
|
|
163
|
+
* `color`, any culori color, as components in `profile`, past 0 and 1 if need be.
|
|
164
|
+
*/
|
|
165
|
+
function exactlyInProfile(color: CuloriColor, profile: DocumentColorSpace): Color {
|
|
166
|
+
const converted = profile === 'display-p3' ? toP3(color) : toRGB(color)
|
|
167
|
+
return { r: converted.r, g: converted.g, b: converted.b, a: converted.alpha ?? 1 }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function isInsideProfile(color: Color, profile: DocumentColorSpace): boolean {
|
|
171
|
+
if (profile === 'display-p3')
|
|
172
|
+
return isDisplayableP3({ mode: 'p3', r: color.r, g: color.g, b: color.b, alpha: color.a })
|
|
173
|
+
return isDisplayableRGB({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a })
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* `color`, any culori color, as components in `profile`, brought inside it the way CSS maps
|
|
178
|
+
* out-of-gamut colors — by reducing chroma in OKLCH so lightness and hue hold — and whether it had
|
|
179
|
+
* to be. A color inside already keeps its components, as mapping it would shift them by float
|
|
180
|
+
* noise. A missing (`none`) component reads as zero.
|
|
181
|
+
*/
|
|
182
|
+
export function fitIntoProfile(
|
|
183
|
+
color: CuloriColor,
|
|
184
|
+
profile: DocumentColorSpace
|
|
185
|
+
): { color: Color; clipped: boolean } {
|
|
186
|
+
const exact = normalizeColor(exactlyInProfile(color, profile))
|
|
187
|
+
const clipped = !isInsideProfile(exact, profile)
|
|
188
|
+
const inside = clipped
|
|
189
|
+
? normalizeColor(
|
|
190
|
+
exactlyInProfile(
|
|
191
|
+
profile === 'display-p3' ? toDisplayableP3(color) : toDisplayableRGB(color),
|
|
192
|
+
profile
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
: exact
|
|
196
|
+
const clamp = (channel: number) => Math.min(1, Math.max(0, channel))
|
|
197
|
+
return {
|
|
198
|
+
color: { r: clamp(inside.r), g: clamp(inside.g), b: clamp(inside.b), a: color.alpha ?? 1 },
|
|
199
|
+
clipped
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Syntaxes CSS reads as sRGB numbers: hex, `rgb()`, `hsl()`, `hwb()`, names, `color(srgb …)`. */
|
|
204
|
+
export const SRGB_SYNTAX_MODES = new Set(['rgb', 'hsl', 'hwb'])
|
|
205
|
+
|
|
153
206
|
/**
|
|
154
207
|
* A CSS color in any space, mapped into sRGB the way CSS maps out-of-gamut colors: by reducing
|
|
155
208
|
* chroma in OKLCH, so a Display P3 red becomes the nearest sRGB red, not a clipped one. A missing
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { converter, formatCss, formatRgb, toGamut } from 'culori'
|
|
2
|
+
|
|
3
|
+
import type { Color } from '../primitives'
|
|
4
|
+
import type { ColorProfile } from '../types'
|
|
5
|
+
import { normalizeColor } from './normalize'
|
|
6
|
+
|
|
7
|
+
const toRGB = converter('rgb')
|
|
8
|
+
const toP3 = converter('p3')
|
|
9
|
+
const toLinearSRGB = converter('lrgb')
|
|
10
|
+
const toDisplayableRGB = toGamut('rgb', 'oklch')
|
|
11
|
+
const toDisplayableP3 = toGamut('p3', 'oklch')
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* How far past 0 or 1 a component may land and still count as inside a gamut. Conversions round
|
|
15
|
+
* off: P3 red read through OKLCH comes back with a blue of -1e-15, which is the same color.
|
|
16
|
+
*/
|
|
17
|
+
const GAMUT_TOLERANCE = 1e-6
|
|
18
|
+
|
|
19
|
+
type CuloriColor = { mode: 'rgb' | 'p3'; r: number; g: number; b: number; alpha?: number }
|
|
20
|
+
|
|
21
|
+
function culoriColor(color: Color, profile: ColorProfile): CuloriColor {
|
|
22
|
+
const mode = profile === 'display-p3' ? 'p3' : 'rgb'
|
|
23
|
+
return { mode, r: color.r, g: color.g, b: color.b, alpha: color.a }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function sceneColor(
|
|
27
|
+
color: { r: number; g: number; b: number; alpha?: number },
|
|
28
|
+
alpha: number
|
|
29
|
+
): Color {
|
|
30
|
+
return { r: color.r, g: color.g, b: color.b, a: color.alpha ?? alpha }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Whether `color`'s components fit 0 to 1, give or take round-off. */
|
|
34
|
+
export function fitsGamut(color: Pick<Color, 'r' | 'g' | 'b'>): boolean {
|
|
35
|
+
return [color.r, color.g, color.b].every(
|
|
36
|
+
(channel) => channel >= -GAMUT_TOLERANCE && channel <= 1 + GAMUT_TOLERANCE
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function clampToGamut(color: Color): Color {
|
|
41
|
+
const clamp = (channel: number) => Math.min(1, Math.max(0, channel))
|
|
42
|
+
return { r: clamp(color.r), g: clamp(color.g), b: clamp(color.b), a: color.a }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* What to do with a color the target profile cannot show: `extend` keeps its components past 0
|
|
47
|
+
* and 1, for a renderer such as Skia that takes extended sRGB; `map` brings it inside by reducing
|
|
48
|
+
* chroma in OKLCH, as CSS maps out-of-gamut colors, keeping its lightness and hue.
|
|
49
|
+
*/
|
|
50
|
+
export type GamutHandling = 'extend' | 'map'
|
|
51
|
+
|
|
52
|
+
/** `color`, whose components are in `from`, as the components in `to` that show the same color. */
|
|
53
|
+
export function convertColor(
|
|
54
|
+
color: Color,
|
|
55
|
+
from: ColorProfile,
|
|
56
|
+
to: ColorProfile,
|
|
57
|
+
gamut: GamutHandling = 'extend'
|
|
58
|
+
): Color {
|
|
59
|
+
if (from === to) return color
|
|
60
|
+
const source = culoriColor(color, from)
|
|
61
|
+
const converted = sceneColor(to === 'display-p3' ? toP3(source) : toRGB(source), color.a)
|
|
62
|
+
if (gamut === 'extend') return converted
|
|
63
|
+
if (fitsGamut(converted)) return clampToGamut(converted)
|
|
64
|
+
const mapped =
|
|
65
|
+
to === 'display-p3' ? toP3(toDisplayableP3(source)) : toRGB(toDisplayableRGB(source))
|
|
66
|
+
return normalizeColor(sceneColor(mapped, color.a))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* `color`, whose components are in `from`, as CSS showing it in `to`: `color(display-p3 …)` for
|
|
71
|
+
* Display P3, `rgb()` for sRGB, where a color outside sRGB clips per channel as a canvas clips it.
|
|
72
|
+
*/
|
|
73
|
+
export function colorToProfileCSS(color: Color, from: ColorProfile, to: ColorProfile): string {
|
|
74
|
+
const shown = convertColor(color, from, to)
|
|
75
|
+
if (to === 'display-p3') {
|
|
76
|
+
return formatCss({ mode: 'p3', r: shown.r, g: shown.g, b: shown.b, alpha: shown.a })
|
|
77
|
+
}
|
|
78
|
+
return formatRgb({ mode: 'rgb', r: shown.r, g: shown.g, b: shown.b, alpha: shown.a })
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The 3×3 matrix, row by row, taking linear components in `profile` to linear sRGB ones: its
|
|
83
|
+
* columns are the profile's primaries in linear sRGB.
|
|
84
|
+
*/
|
|
85
|
+
export function linearSRGBMatrix(profile: ColorProfile): number[] {
|
|
86
|
+
const mode = profile === 'display-p3' ? 'p3' : 'rgb'
|
|
87
|
+
const columns = [
|
|
88
|
+
{ r: 1, g: 0, b: 0 },
|
|
89
|
+
{ r: 0, g: 1, b: 0 },
|
|
90
|
+
{ r: 0, g: 0, b: 1 }
|
|
91
|
+
].map((primary) => toLinearSRGB({ mode, ...primary }))
|
|
92
|
+
return (['r', 'g', 'b'] as const).flatMap((row) => columns.map((column) => column[row]))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Display P3's matrix, made once: renderers convert every paint they draw through it. */
|
|
96
|
+
const P3_TO_LINEAR_SRGB = linearSRGBMatrix('display-p3')
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* `color`, whose components are in `profile`, as the sRGB components of the same color: past 0 and
|
|
100
|
+
* 1 where it lies outside sRGB. Skia takes colors this way and converts them into the space of
|
|
101
|
+
* whatever it draws on, so a wide-gamut color reaches it whole.
|
|
102
|
+
*/
|
|
103
|
+
export function toExtendedSRGB(color: Color, profile: ColorProfile): Color {
|
|
104
|
+
if (profile === 'srgb') return color
|
|
105
|
+
// Renderers call this for every paint they draw, so it skips culori's color objects: Display P3
|
|
106
|
+
// shares sRGB's transfer curve, leaving one matrix between the linear components.
|
|
107
|
+
// Indexed reads of a plain array: destructuring costs more than the conversion.
|
|
108
|
+
const m = P3_TO_LINEAR_SRGB
|
|
109
|
+
const r = decodeSRGB(color.r)
|
|
110
|
+
const g = decodeSRGB(color.g)
|
|
111
|
+
const b = decodeSRGB(color.b)
|
|
112
|
+
return {
|
|
113
|
+
r: encodeSRGB(m[0] * r + m[1] * g + m[2] * b),
|
|
114
|
+
g: encodeSRGB(m[3] * r + m[4] * g + m[5] * b),
|
|
115
|
+
b: encodeSRGB(m[6] * r + m[7] * g + m[8] * b),
|
|
116
|
+
a: color.a
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The sRGB transfer curve, mirrored below zero as extended sRGB and culori's `lrgb` take it. */
|
|
121
|
+
function decodeSRGB(channel: number): number {
|
|
122
|
+
const magnitude = Math.abs(channel)
|
|
123
|
+
if (magnitude <= 0.04045) return channel / 12.92
|
|
124
|
+
return (Math.sign(channel) || 1) * ((magnitude + 0.055) / 1.055) ** 2.4
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function encodeSRGB(channel: number): number {
|
|
128
|
+
const magnitude = Math.abs(channel)
|
|
129
|
+
if (magnitude <= 0.0031308) return channel * 12.92
|
|
130
|
+
return (Math.sign(channel) || 1) * (1.055 * magnitude ** (1 / 2.4) - 0.055)
|
|
131
|
+
}
|