@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/arrow-caps.d.ts.map +1 -1
- package/dist/arrow-caps.js +2 -5
- package/dist/arrow-caps.js.map +1 -1
- package/dist/asset-keys.d.ts +11 -1
- package/dist/asset-keys.d.ts.map +1 -1
- package/dist/asset-keys.js +16 -1
- package/dist/asset-keys.js.map +1 -1
- package/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/kinds.d.ts +11 -5
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +76 -75
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/layers.d.ts.map +1 -1
- package/dist/behaviours/layers.js +3 -2
- package/dist/behaviours/layers.js.map +1 -1
- package/dist/behaviours/model.d.ts +7 -4
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js +16 -9
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +118 -4
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +18 -3
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +3 -2
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/bindings.js +9 -1
- package/dist/bindings.js.map +1 -1
- package/dist/checkpoint.d.ts +31 -2
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +211 -10
- package/dist/checkpoint.js.map +1 -1
- package/dist/components/index.d.ts +3 -3
- package/dist/components/index.js +3 -3
- package/dist/components/properties.d.ts +18 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +66 -3
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +16 -10
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +39 -13
- package/dist/components/variants.js.map +1 -1
- package/dist/connectors/hit.d.ts.map +1 -1
- package/dist/connectors/hit.js +1 -7
- package/dist/connectors/hit.js.map +1 -1
- package/dist/connectors/pieces.d.ts.map +1 -1
- package/dist/connectors/pieces.js +2 -5
- package/dist/connectors/pieces.js.map +1 -1
- package/dist/constants.d.ts +6 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +17 -1
- package/dist/constants.js.map +1 -1
- package/dist/constraints.d.ts +25 -0
- package/dist/constraints.d.ts.map +1 -0
- package/dist/constraints.js +51 -0
- package/dist/constraints.js.map +1 -0
- package/dist/coordinate.d.ts +64 -1
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +217 -1
- package/dist/coordinate.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -16
- package/dist/copy.js.map +1 -1
- package/dist/css/blend.d.ts +11 -0
- package/dist/css/blend.d.ts.map +1 -0
- package/dist/css/blend.js +30 -0
- package/dist/css/blend.js.map +1 -0
- package/dist/css/index.d.ts +4 -2
- package/dist/css/index.js +4 -2
- package/dist/css/text.d.ts +28 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +84 -17
- package/dist/css/text.js.map +1 -1
- package/dist/css/transform.d.ts +34 -0
- package/dist/css/transform.d.ts.map +1 -0
- package/dist/css/transform.js +320 -0
- package/dist/css/transform.js.map +1 -0
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +11 -1
- package/dist/css/values.js.map +1 -1
- package/dist/cubic.d.ts +7 -0
- package/dist/cubic.d.ts.map +1 -0
- package/dist/cubic.js +17 -0
- package/dist/cubic.js.map +1 -0
- package/dist/export-format.d.ts +14 -1
- package/dist/export-format.d.ts.map +1 -1
- package/dist/export-format.js +26 -1
- package/dist/export-format.js.map +1 -1
- package/dist/fields/text.d.ts +2 -2
- package/dist/fields/text.d.ts.map +1 -1
- package/dist/fields/text.js +2 -1
- package/dist/fields/text.js.map +1 -1
- package/dist/figjam/index.d.ts +2 -2
- package/dist/figjam/index.js +2 -2
- package/dist/figjam/palette.d.ts +13 -2
- package/dist/figjam/palette.d.ts.map +1 -1
- package/dist/figjam/palette.js +30 -2
- package/dist/figjam/palette.js.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +2 -2
- package/dist/generated/shape-with-text.d.ts +7 -2
- package/dist/generated/shape-with-text.d.ts.map +1 -1
- package/dist/generated/shape-with-text.js +20 -3
- package/dist/generated/shape-with-text.js.map +1 -1
- package/dist/geometry.d.ts +37 -46
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +80 -137
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +49 -12
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +125 -27
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts +14 -4
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +76 -11
- package/dist/group-bounds.js.map +1 -1
- package/dist/guides.d.ts +3 -1
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +5 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts +5 -0
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +57 -38
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +27 -3
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +57 -3
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +8 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +1 -3
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +39 -30
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +204 -0
- package/dist/instances/kept-overrides.js.map +1 -0
- package/dist/instances/main-component.d.ts +3 -1
- package/dist/instances/main-component.d.ts.map +1 -1
- package/dist/instances/main-component.js +10 -2
- package/dist/instances/main-component.js.map +1 -1
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +18 -2
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/placement.js +55 -12
- package/dist/instances/placement.js.map +1 -1
- package/dist/instances/swap.js +291 -0
- package/dist/instances/swap.js.map +1 -0
- package/dist/instances/sync.js +112 -39
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +2 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/order-keys.d.ts +3 -1
- package/dist/order-keys.d.ts.map +1 -1
- package/dist/order-keys.js +6 -1
- package/dist/order-keys.js.map +1 -1
- package/dist/outline/flatten.d.ts +15 -0
- package/dist/outline/flatten.d.ts.map +1 -0
- package/dist/outline/flatten.js +189 -0
- package/dist/outline/flatten.js.map +1 -0
- package/dist/outline/heads.js +34 -0
- package/dist/outline/heads.js.map +1 -0
- package/dist/outline/hit.d.ts +21 -0
- package/dist/outline/hit.d.ts.map +1 -0
- package/dist/outline/hit.js +276 -0
- package/dist/outline/hit.js.map +1 -0
- package/dist/outline/index.d.ts +5 -0
- package/dist/outline/index.js +5 -0
- package/dist/outline/point.d.ts +16 -0
- package/dist/outline/point.d.ts.map +1 -0
- package/dist/outline/point.js +42 -0
- package/dist/outline/point.js.map +1 -0
- package/dist/outline/shapes.d.ts +35 -0
- package/dist/outline/shapes.d.ts.map +1 -0
- package/dist/outline/shapes.js +181 -0
- package/dist/outline/shapes.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +4 -1
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +21 -4
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/resize.d.ts +22 -11
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +227 -93
- package/dist/resize.js.map +1 -1
- package/dist/scaling/index.d.ts +3 -2
- package/dist/scaling/index.js +3 -2
- 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/scaling/snapshot.d.ts +25 -0
- package/dist/scaling/snapshot.d.ts.map +1 -0
- package/dist/scaling/snapshot.js +39 -0
- package/dist/scaling/snapshot.js.map +1 -0
- package/dist/scaling/tree.d.ts +24 -2
- package/dist/scaling/tree.d.ts.map +1 -1
- package/dist/scaling/tree.js +38 -15
- package/dist/scaling/tree.js.map +1 -1
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +2 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts +16 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +69 -2
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/authoring.d.ts +3 -2
- package/dist/slots/authoring.d.ts.map +1 -1
- package/dist/slots/authoring.js +5 -3
- package/dist/slots/authoring.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +6 -1
- package/dist/slots/content.js.map +1 -1
- package/dist/slots/frames.d.ts +2 -0
- package/dist/slots/frames.d.ts.map +1 -1
- package/dist/slots/frames.js +7 -2
- package/dist/slots/frames.js.map +1 -1
- package/dist/text/direction.d.ts +3 -1
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +5 -1
- package/dist/text/direction.js.map +1 -1
- package/dist/text/index.d.ts +2 -2
- package/dist/text/index.js +3 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +2 -2
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/picture.d.ts +1 -2
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +1 -4
- package/dist/text/picture.js.map +1 -1
- package/dist/transfer/apply.d.ts.map +1 -1
- package/dist/transfer/apply.js +1 -6
- package/dist/transfer/apply.js.map +1 -1
- package/dist/transfer/copied.d.ts +35 -3
- package/dist/transfer/copied.d.ts.map +1 -1
- package/dist/transfer/copied.js +44 -13
- package/dist/transfer/copied.js.map +1 -1
- package/dist/transfer/dependencies.d.ts +38 -0
- package/dist/transfer/dependencies.d.ts.map +1 -0
- package/dist/transfer/dependencies.js +100 -0
- package/dist/transfer/dependencies.js.map +1 -0
- package/dist/transfer/history.js +0 -1
- package/dist/transfer/history.js.map +1 -1
- package/dist/transfer/index.d.ts +5 -3
- package/dist/transfer/index.js +5 -3
- package/dist/transfer/nodes.d.ts.map +1 -1
- package/dist/transfer/nodes.js +16 -19
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/transfer/plan.d.ts +0 -1
- package/dist/transfer/plan.js +3 -5
- package/dist/transfer/plan.js.map +1 -1
- package/dist/transfer/remove.js +5 -2
- package/dist/transfer/remove.js.map +1 -1
- package/dist/transfer/resources.d.ts +21 -0
- package/dist/transfer/resources.d.ts.map +1 -0
- package/dist/transfer/resources.js +38 -0
- package/dist/transfer/resources.js.map +1 -0
- package/dist/transfer/variables.d.ts +8 -1
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +24 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +73 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +28 -3
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +24 -7
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +37 -11
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +122 -74
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +19 -1
- package/dist/variables/fields.d.ts.map +1 -1
- package/dist/variables/fields.js +36 -1
- package/dist/variables/fields.js.map +1 -1
- package/dist/variables/formula/check.d.ts +1 -1
- package/dist/variables/formula/index.d.ts +2 -2
- package/dist/variables/formula/index.js +1 -1
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/formula/print.d.ts +1 -1
- package/dist/variables/formula/signatures.js +1 -1
- package/dist/variables/index.d.ts +7 -7
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +57 -26
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +57 -0
- package/dist/variables/paints.d.ts.map +1 -0
- package/dist/variables/paints.js +231 -0
- package/dist/variables/paints.js.map +1 -0
- package/dist/variables/resolution.d.ts +14 -11
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +36 -17
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +17 -0
- package/dist/variables/subtree.js.map +1 -0
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +2 -8
- package/dist/vector-network.js.map +1 -1
- package/package.json +14 -1
- package/src/arrow-caps.ts +2 -5
- package/src/asset-keys.ts +20 -0
- package/src/behaviours/kinds.ts +51 -34
- package/src/behaviours/layers.ts +4 -2
- package/src/behaviours/model.ts +22 -11
- package/src/behaviours/schema.ts +20 -4
- package/src/behaviours/spec.ts +8 -2
- package/src/bindings.ts +11 -2
- package/src/checkpoint.ts +319 -17
- package/src/components/properties.ts +101 -2
- package/src/components/variants.ts +51 -30
- package/src/connectors/hit.ts +1 -12
- package/src/connectors/pieces.ts +2 -5
- package/src/constants.ts +17 -0
- package/src/constraints.ts +60 -0
- package/src/coordinate.ts +220 -0
- package/src/copy.ts +18 -19
- package/src/css/blend.ts +27 -0
- package/src/css/index.ts +18 -1
- package/src/css/text.ts +85 -17
- package/src/css/transform.ts +266 -0
- package/src/css/values.ts +8 -0
- package/src/cubic.ts +14 -0
- package/src/export-format.ts +29 -0
- package/src/fields/text.ts +3 -1
- package/src/figjam/palette.ts +29 -1
- package/src/generated/shape-with-text.ts +28 -2
- package/src/geometry.ts +97 -206
- package/src/graph.ts +155 -45
- package/src/group-bounds.ts +84 -17
- package/src/guides.ts +5 -0
- package/src/hit-test.ts +93 -64
- package/src/images.ts +71 -3
- package/src/instances/fields.ts +8 -0
- package/src/instances/index.ts +62 -31
- package/src/instances/kept-overrides.ts +280 -0
- package/src/instances/main-component.ts +12 -0
- package/src/instances/overrides.ts +27 -1
- package/src/instances/placement.ts +63 -10
- package/src/instances/swap.ts +456 -0
- package/src/instances/sync.ts +176 -50
- package/src/node-defaults.ts +3 -1
- package/src/order-keys.ts +6 -0
- package/src/outline/flatten.ts +190 -0
- package/src/outline/heads.ts +52 -0
- package/src/outline/hit.ts +369 -0
- package/src/outline/index.ts +4 -0
- package/src/outline/point.ts +59 -0
- package/src/outline/shapes.ts +207 -0
- package/src/plugin-data/fields.ts +21 -2
- package/src/resize.ts +267 -119
- package/src/scaling/index.ts +9 -1
- package/src/scaling/node.ts +9 -1
- package/src/scaling/snapshot.ts +51 -0
- package/src/scaling/tree.ts +63 -19
- package/src/shaders/model.ts +2 -3
- package/src/shared-styles.ts +90 -1
- package/src/slots/authoring.ts +5 -3
- package/src/slots/content.ts +6 -1
- package/src/slots/frames.ts +9 -7
- package/src/text/direction.ts +5 -0
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +2 -2
- package/src/text/picture.ts +0 -4
- package/src/transfer/apply.ts +1 -13
- package/src/transfer/copied.ts +83 -15
- package/src/transfer/dependencies.ts +126 -0
- package/src/transfer/history.ts +0 -6
- package/src/transfer/index.ts +20 -2
- package/src/transfer/nodes.ts +17 -31
- package/src/transfer/plan.ts +3 -11
- package/src/transfer/remove.ts +5 -2
- package/src/transfer/resources.ts +54 -0
- package/src/transfer/variables.ts +34 -0
- package/src/types.ts +83 -1
- package/src/undo.ts +48 -10
- package/src/variables/bindings.ts +152 -88
- package/src/variables/fields.ts +46 -1
- package/src/variables/formula/index.ts +3 -1
- package/src/variables/index.ts +90 -39
- package/src/variables/paints.ts +296 -0
- package/src/variables/resolution.ts +39 -28
- package/src/variables/subtree.ts +15 -0
- package/src/vector-network.ts +4 -2
package/src/checkpoint.ts
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
import { isEqual } from 'es-toolkit'
|
|
2
2
|
|
|
3
3
|
import type { SceneGraph } from './graph'
|
|
4
|
+
import { createSceneMutationImpact, recordSceneMutations } from './mutation-impact'
|
|
5
|
+
import type { SceneNode, Variable, VariableCollection } from './types'
|
|
6
|
+
|
|
7
|
+
const NODE_COUNT_ERROR = 'Atomic tools must not create or remove nodes or variables'
|
|
8
|
+
const NODE_IDENTITY_ERROR = 'Atomic tools must not change node hierarchy or identity'
|
|
9
|
+
const VARIABLE_IDENTITY_ERROR = 'Atomic tools must not change variable identity'
|
|
10
|
+
const DOCUMENT_ERROR = 'Atomic tools must not change collections, assets or document metadata'
|
|
11
|
+
|
|
12
|
+
/** Throws when an edit replaced a variable or changed its identity, collection, or type. */
|
|
13
|
+
function assertVariableIdentity(
|
|
14
|
+
graph: SceneGraph,
|
|
15
|
+
saved: ReadonlyMap<string, Variable>,
|
|
16
|
+
refs: ReadonlyMap<string, Variable>
|
|
17
|
+
): void {
|
|
18
|
+
for (const [id, before] of saved) {
|
|
19
|
+
const after = graph.variables.get(id)
|
|
20
|
+
if (
|
|
21
|
+
!after ||
|
|
22
|
+
after !== refs.get(id) ||
|
|
23
|
+
before.id !== after.id ||
|
|
24
|
+
before.collectionId !== after.collectionId ||
|
|
25
|
+
before.type !== after.type
|
|
26
|
+
)
|
|
27
|
+
throw new Error(VARIABLE_IDENTITY_ERROR)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
4
30
|
|
|
5
31
|
/** Capture rollback state without changing graph or surviving object identities. */
|
|
6
32
|
export function captureGraphCheckpoint(graph: SceneGraph) {
|
|
@@ -11,7 +37,6 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
|
|
|
11
37
|
nodes: graph.nodes,
|
|
12
38
|
variables: graph.variables,
|
|
13
39
|
variableCollections: graph.variableCollections,
|
|
14
|
-
activeMode: graph.activeMode,
|
|
15
40
|
instanceIndex: graph.instanceIndex,
|
|
16
41
|
enabledLibraries: graph.enabledLibraries,
|
|
17
42
|
rootId: graph.rootId,
|
|
@@ -27,7 +52,7 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
|
|
|
27
52
|
snapshot.nodes.size !== graph.nodes.size ||
|
|
28
53
|
snapshot.variables.size !== graph.variables.size
|
|
29
54
|
)
|
|
30
|
-
throw new Error(
|
|
55
|
+
throw new Error(NODE_COUNT_ERROR)
|
|
31
56
|
for (const [id, before] of snapshot.nodes) {
|
|
32
57
|
const after = graph.nodes.get(id)
|
|
33
58
|
if (
|
|
@@ -39,27 +64,16 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
|
|
|
39
64
|
before.componentId !== after.componentId ||
|
|
40
65
|
!isEqual(before.childIds, after.childIds)
|
|
41
66
|
) {
|
|
42
|
-
throw new Error(
|
|
67
|
+
throw new Error(NODE_IDENTITY_ERROR)
|
|
43
68
|
}
|
|
44
69
|
}
|
|
45
|
-
|
|
46
|
-
const after = graph.variables.get(id)
|
|
47
|
-
if (
|
|
48
|
-
!after ||
|
|
49
|
-
after !== variableRefs.get(id) ||
|
|
50
|
-
before.id !== after.id ||
|
|
51
|
-
before.collectionId !== after.collectionId ||
|
|
52
|
-
before.type !== after.type
|
|
53
|
-
)
|
|
54
|
-
throw new Error('Atomic tools must not change variable identity')
|
|
55
|
-
}
|
|
70
|
+
assertVariableIdentity(graph, snapshot.variables, variableRefs)
|
|
56
71
|
assertDocumentUnchanged()
|
|
57
72
|
}
|
|
58
73
|
|
|
59
74
|
function assertDocumentUnchanged() {
|
|
60
75
|
if (
|
|
61
76
|
!isEqual(snapshot.variableCollections, graph.variableCollections) ||
|
|
62
|
-
!isEqual(snapshot.activeMode, graph.activeMode) ||
|
|
63
77
|
!isEqual(snapshot.instanceIndex, graph.instanceIndex) ||
|
|
64
78
|
!isEqual(snapshot.enabledLibraries, graph.enabledLibraries) ||
|
|
65
79
|
images.size !== graph.images.size ||
|
|
@@ -69,7 +83,7 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
|
|
|
69
83
|
graph.figKiwiVersion !== snapshot.figKiwiVersion ||
|
|
70
84
|
!isEqual(graph.figSchemaDeflated, snapshot.figSchemaDeflated)
|
|
71
85
|
) {
|
|
72
|
-
throw new Error(
|
|
86
|
+
throw new Error(DOCUMENT_ERROR)
|
|
73
87
|
}
|
|
74
88
|
}
|
|
75
89
|
|
|
@@ -78,10 +92,12 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
|
|
|
78
92
|
restoreObjects(graph.nodes, snapshot.nodes, nodeRefs)
|
|
79
93
|
restoreObjects(graph.variables, snapshot.variables, variableRefs)
|
|
80
94
|
restoreObjects(graph.variableCollections, snapshot.variableCollections, collectionRefs)
|
|
81
|
-
restoreMap(graph.activeMode, structuredClone(snapshot.activeMode))
|
|
82
95
|
restoreMap(graph.instanceIndex, structuredClone(snapshot.instanceIndex))
|
|
83
96
|
restoreMap(graph.enabledLibraries, structuredClone(snapshot.enabledLibraries))
|
|
84
97
|
restoreMap(graph.images, images)
|
|
98
|
+
graph.connectorIds.clear()
|
|
99
|
+
for (const node of graph.nodes.values())
|
|
100
|
+
if (node.type === 'CONNECTOR') graph.connectorIds.add(node.id)
|
|
85
101
|
graph.rootId = snapshot.rootId
|
|
86
102
|
graph.figKiwiVersion = snapshot.figKiwiVersion
|
|
87
103
|
graph.figSchemaDeflated = structuredClone(snapshot.figSchemaDeflated)
|
|
@@ -117,7 +133,293 @@ function restoreObjects<T extends object>(
|
|
|
117
133
|
}
|
|
118
134
|
}
|
|
119
135
|
|
|
136
|
+
/** Copies `id`'s record the first time it is reported: `null` for one about to be added. */
|
|
137
|
+
function copyRecord<T>(copies: Map<string, T | null>, live: ReadonlyMap<string, T>, id: string) {
|
|
138
|
+
if (!copies.has(id)) copies.set(id, structuredClone(live.get(id) ?? null))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Whether `target` holds exactly the objects `refs` held, in any order. */
|
|
142
|
+
function sameEntries<T>(target: ReadonlyMap<string, T>, refs: ReadonlyMap<string, T>): boolean {
|
|
143
|
+
if (target.size !== refs.size) return false
|
|
144
|
+
for (const [id, ref] of refs) if (target.get(id) !== ref) return false
|
|
145
|
+
return true
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Puts back the records `refs` held, in their order, with the copied ones rewritten in place to
|
|
150
|
+
* their copies, as views may hold them.
|
|
151
|
+
*/
|
|
152
|
+
function restoreCopied<T extends object>(
|
|
153
|
+
target: Map<string, T>,
|
|
154
|
+
copies: ReadonlyMap<string, T | null>,
|
|
155
|
+
refs: ReadonlyMap<string, T>
|
|
156
|
+
) {
|
|
157
|
+
target.clear()
|
|
158
|
+
for (const [id, original] of refs) {
|
|
159
|
+
const saved = copies.get(id)
|
|
160
|
+
if (saved) {
|
|
161
|
+
for (const key of Object.keys(original) as (keyof T)[]) {
|
|
162
|
+
if (!(key in saved)) Reflect.deleteProperty(original, key)
|
|
163
|
+
}
|
|
164
|
+
Object.assign(original, structuredClone(saved))
|
|
165
|
+
}
|
|
166
|
+
target.set(id, original)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
120
170
|
function restoreMap<K, V>(target: Map<K, V>, saved: Map<K, V>) {
|
|
121
171
|
target.clear()
|
|
122
172
|
for (const [key, value] of saved) target.set(key, value)
|
|
123
173
|
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Rollback state for an edit that may only change properties, which costs what the edit touches
|
|
177
|
+
* rather than a copy of the document: a node is copied the first time the graph reports it about
|
|
178
|
+
* to change (`SceneGraph.observeNodeChanges`), together with its ancestors below the page, which
|
|
179
|
+
* keep instance override maps the graph writes into in place.
|
|
180
|
+
*/
|
|
181
|
+
export interface TouchedCheckpoint {
|
|
182
|
+
/** Copies of the reported nodes and their ancestors below the page, as they were before. */
|
|
183
|
+
readonly nodes: ReadonlyMap<string, SceneNode>
|
|
184
|
+
/** Nodes the graph reported changing or creating. */
|
|
185
|
+
readonly reported: ReadonlySet<string>
|
|
186
|
+
/** Copies of the variables reported changing (`SceneGraph.willChangeVariable`), as they were. */
|
|
187
|
+
readonly variables: ReadonlyMap<string, Variable | null>
|
|
188
|
+
readonly variableCollections: ReadonlyMap<string, VariableCollection | null>
|
|
189
|
+
/** Throws unless the edit only changed properties of existing nodes and variables. */
|
|
190
|
+
assertPropertiesOnly(): void
|
|
191
|
+
/** Stops observing and puts back everything the edit changed, keeping object identities. */
|
|
192
|
+
restore(): void
|
|
193
|
+
/** Stops observing the graph. */
|
|
194
|
+
stop(): void
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Starts observing `graph` for an edit that may only change properties. Node identities are
|
|
199
|
+
* kept as pointers, so replacing, adding, or removing a node anywhere is found; a node's fields
|
|
200
|
+
* are compared and restored only where the graph reported it, so code that writes into a node
|
|
201
|
+
* directly calls `SceneGraph.willChangeNode` first, and a node event without such a report fails
|
|
202
|
+
* the edit.
|
|
203
|
+
*/
|
|
204
|
+
export function captureTouchedCheckpoint(graph: SceneGraph): TouchedCheckpoint {
|
|
205
|
+
const nodeRefs = new Map(graph.nodes)
|
|
206
|
+
const variableRefs = new Map(graph.variables)
|
|
207
|
+
const collectionRefs = new Map(graph.variableCollections)
|
|
208
|
+
const saved = structuredClone({
|
|
209
|
+
enabledLibraries: graph.enabledLibraries
|
|
210
|
+
})
|
|
211
|
+
// Instances follow their nodes' components, which a property edit cannot change, so the sets
|
|
212
|
+
// are kept rather than their ids copied deeply.
|
|
213
|
+
const instanceIndex = new Map(
|
|
214
|
+
[...graph.instanceIndex].map(([id, instances]) => [id, new Set(instances)])
|
|
215
|
+
)
|
|
216
|
+
const variables = new Map<string, Variable | null>()
|
|
217
|
+
const variableCollections = new Map<string, VariableCollection | null>()
|
|
218
|
+
// Property edits cannot edit assets. Retain existing buffers, not copies of every bitmap.
|
|
219
|
+
const images = new Map(graph.images)
|
|
220
|
+
const { rootId, figKiwiVersion, figSchemaDeflated, documentColorSpace } = graph
|
|
221
|
+
const nodes = new Map<string, SceneNode>()
|
|
222
|
+
const reported = new Set<string>()
|
|
223
|
+
const created = new Set<string>()
|
|
224
|
+
const impact = createSceneMutationImpact()
|
|
225
|
+
|
|
226
|
+
/** Copies a node as the edit found it: the original object, should the edit have replaced it. */
|
|
227
|
+
function copy(node: SceneNode) {
|
|
228
|
+
nodes.set(node.id, structuredClone(nodeRefs.get(node.id) ?? node))
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const skipped = (node: SceneNode) => nodes.has(node.id) || created.has(node.id)
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Copies `node` and each ancestor not copied yet, up to but excluding its page. An ancestor
|
|
235
|
+
* copied already had its own ancestors copied with it, so the walk stops there.
|
|
236
|
+
*/
|
|
237
|
+
function copyLineage(node: SceneNode) {
|
|
238
|
+
if (skipped(node)) return
|
|
239
|
+
copy(node)
|
|
240
|
+
if (!node.parentId) return
|
|
241
|
+
graph.closest(node.parentId, (ancestor) => {
|
|
242
|
+
if (ancestor.type === 'CANVAS' || ancestor.id === rootId || skipped(ancestor)) return true
|
|
243
|
+
copy(ancestor)
|
|
244
|
+
return false
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const unobserve = graph.observeNodeChanges({
|
|
249
|
+
created: (node) => {
|
|
250
|
+
created.add(node.id)
|
|
251
|
+
reported.add(node.id)
|
|
252
|
+
},
|
|
253
|
+
before: (node) => {
|
|
254
|
+
reported.add(node.id)
|
|
255
|
+
copyLineage(node)
|
|
256
|
+
},
|
|
257
|
+
beforeRecord: (kind, id) => {
|
|
258
|
+
if (kind === 'variable') copyRecord(variables, graph.variables, id)
|
|
259
|
+
else copyRecord(variableCollections, graph.variableCollections, id)
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
const unbindEvents = recordSceneMutations(graph, impact)
|
|
263
|
+
let observing = true
|
|
264
|
+
|
|
265
|
+
function stop() {
|
|
266
|
+
if (!observing) return
|
|
267
|
+
observing = false
|
|
268
|
+
unobserve()
|
|
269
|
+
unbindEvents()
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function assertNodes() {
|
|
273
|
+
if (
|
|
274
|
+
created.size > 0 ||
|
|
275
|
+
impact.deletedNodeIds.size > 0 ||
|
|
276
|
+
graph.nodes.size !== nodeRefs.size ||
|
|
277
|
+
graph.variables.size !== variableRefs.size
|
|
278
|
+
)
|
|
279
|
+
throw new Error(NODE_COUNT_ERROR)
|
|
280
|
+
for (const [id, node] of graph.nodes) {
|
|
281
|
+
const original = nodeRefs.get(id)
|
|
282
|
+
if (!original) throw new Error(NODE_COUNT_ERROR)
|
|
283
|
+
if (original !== node) throw new Error(NODE_IDENTITY_ERROR)
|
|
284
|
+
}
|
|
285
|
+
for (const [id, before] of nodes) {
|
|
286
|
+
const after = graph.nodes.get(id)
|
|
287
|
+
if (
|
|
288
|
+
!after ||
|
|
289
|
+
before.id !== after.id ||
|
|
290
|
+
before.type !== after.type ||
|
|
291
|
+
before.parentId !== after.parentId ||
|
|
292
|
+
before.componentId !== after.componentId ||
|
|
293
|
+
!isEqual(before.childIds, after.childIds)
|
|
294
|
+
)
|
|
295
|
+
throw new Error(NODE_IDENTITY_ERROR)
|
|
296
|
+
}
|
|
297
|
+
for (const id of [
|
|
298
|
+
...impact.changedNodeIds,
|
|
299
|
+
...impact.previousParentIds,
|
|
300
|
+
...impact.currentParentIds
|
|
301
|
+
]) {
|
|
302
|
+
if (!nodes.has(id) && !created.has(id)) {
|
|
303
|
+
throw new Error(
|
|
304
|
+
`Atomic tools must report layer changes to the scene graph (SceneGraph.willChangeNode): ${id}`
|
|
305
|
+
)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function assertPropertiesOnly() {
|
|
311
|
+
assertNodes()
|
|
312
|
+
if (!sameEntries(graph.variables, variableRefs)) throw new Error(VARIABLE_IDENTITY_ERROR)
|
|
313
|
+
for (const [id, before] of variables) {
|
|
314
|
+
const after = graph.variables.get(id)
|
|
315
|
+
if (
|
|
316
|
+
!before ||
|
|
317
|
+
!after ||
|
|
318
|
+
before.id !== after.id ||
|
|
319
|
+
before.collectionId !== after.collectionId ||
|
|
320
|
+
before.type !== after.type
|
|
321
|
+
)
|
|
322
|
+
throw new Error(VARIABLE_IDENTITY_ERROR)
|
|
323
|
+
}
|
|
324
|
+
if (
|
|
325
|
+
!sameEntries(graph.variableCollections, collectionRefs) ||
|
|
326
|
+
[...variableCollections].some(
|
|
327
|
+
([id, before]) => !isEqual(before, graph.variableCollections.get(id) ?? null)
|
|
328
|
+
) ||
|
|
329
|
+
!isEqual(instanceIndex, graph.instanceIndex) ||
|
|
330
|
+
!isEqual(saved.enabledLibraries, graph.enabledLibraries) ||
|
|
331
|
+
images.size !== graph.images.size ||
|
|
332
|
+
[...images].some(([id, bytes]) => graph.images.get(id) !== bytes) ||
|
|
333
|
+
graph.rootId !== rootId ||
|
|
334
|
+
graph.documentColorSpace !== documentColorSpace ||
|
|
335
|
+
graph.figKiwiVersion !== figKiwiVersion ||
|
|
336
|
+
// Every writer assigns a new schema rather than writing into it.
|
|
337
|
+
graph.figSchemaDeflated !== figSchemaDeflated
|
|
338
|
+
)
|
|
339
|
+
throw new Error(DOCUMENT_ERROR)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Puts the node map back as it was, in its order, if the edit changed its entries. */
|
|
343
|
+
function restoreNodeMap(): { removed: Map<string, SceneNode>; readded: Set<string> } {
|
|
344
|
+
const removed = new Map<string, SceneNode>()
|
|
345
|
+
const readded = new Set<string>()
|
|
346
|
+
let changed = graph.nodes.size !== nodeRefs.size
|
|
347
|
+
if (!changed) {
|
|
348
|
+
// In lockstep, so a node removed and set again, which moves to the end, is put back too.
|
|
349
|
+
const live = graph.nodes.entries()
|
|
350
|
+
for (const [id, node] of nodeRefs) {
|
|
351
|
+
const next = live.next()
|
|
352
|
+
if (next.done || next.value[0] !== id || next.value[1] !== node) {
|
|
353
|
+
changed = true
|
|
354
|
+
break
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (!changed) return { removed, readded }
|
|
359
|
+
const failed = new Map(graph.nodes)
|
|
360
|
+
graph.nodes.clear()
|
|
361
|
+
for (const [id, node] of nodeRefs) {
|
|
362
|
+
graph.nodes.set(id, node)
|
|
363
|
+
if (!failed.has(id)) readded.add(id)
|
|
364
|
+
}
|
|
365
|
+
for (const [id, node] of failed) if (!nodeRefs.has(id)) removed.set(id, node)
|
|
366
|
+
return { removed, readded }
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function restore() {
|
|
370
|
+
stop()
|
|
371
|
+
const { removed, readded } = restoreNodeMap()
|
|
372
|
+
for (const [id, copy] of nodes) {
|
|
373
|
+
const original = nodeRefs.get(id)
|
|
374
|
+
if (!original) continue
|
|
375
|
+
for (const key of Object.keys(original)) {
|
|
376
|
+
if (!Object.hasOwn(copy, key)) Reflect.deleteProperty(original, key)
|
|
377
|
+
}
|
|
378
|
+
Object.assign(original, structuredClone(copy))
|
|
379
|
+
}
|
|
380
|
+
restoreCopied(graph.variables, variables, variableRefs)
|
|
381
|
+
restoreCopied(graph.variableCollections, variableCollections, collectionRefs)
|
|
382
|
+
restoreMap(
|
|
383
|
+
graph.instanceIndex,
|
|
384
|
+
new Map([...instanceIndex].map(([id, instances]) => [id, new Set(instances)]))
|
|
385
|
+
)
|
|
386
|
+
restoreMap(graph.enabledLibraries, structuredClone(saved.enabledLibraries))
|
|
387
|
+
restoreMap(graph.images, images)
|
|
388
|
+
graph.rootId = rootId
|
|
389
|
+
graph.figKiwiVersion = figKiwiVersion
|
|
390
|
+
graph.figSchemaDeflated = figSchemaDeflated
|
|
391
|
+
graph.documentColorSpace = documentColorSpace
|
|
392
|
+
graph.clearAbsPosCache()
|
|
393
|
+
const touched = new Set([
|
|
394
|
+
...nodes.keys(),
|
|
395
|
+
...created,
|
|
396
|
+
...readded,
|
|
397
|
+
...removed.keys(),
|
|
398
|
+
...impact.changedNodeIds,
|
|
399
|
+
...impact.previousParentIds,
|
|
400
|
+
...impact.currentParentIds
|
|
401
|
+
])
|
|
402
|
+
for (const id of touched) {
|
|
403
|
+
if (graph.nodes.get(id)?.type === 'CONNECTOR') graph.connectorIds.add(id)
|
|
404
|
+
else graph.connectorIds.delete(id)
|
|
405
|
+
}
|
|
406
|
+
// Notify consumers only after the complete hierarchy and indexes have been restored.
|
|
407
|
+
for (const [id, node] of removed) graph.emitter.emit('node:deleted', id, node.parentId)
|
|
408
|
+
for (const id of touched) {
|
|
409
|
+
const node = graph.nodes.get(id)
|
|
410
|
+
if (!node) continue
|
|
411
|
+
if (readded.has(id)) graph.emitter.emit('node:created', node)
|
|
412
|
+
else graph.emitter.emit('node:updated', id, node)
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
return {
|
|
417
|
+
nodes,
|
|
418
|
+
reported,
|
|
419
|
+
variables,
|
|
420
|
+
variableCollections,
|
|
421
|
+
assertPropertiesOnly,
|
|
422
|
+
restore,
|
|
423
|
+
stop
|
|
424
|
+
}
|
|
425
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { SceneGraph } from '../graph'
|
|
2
|
-
import { findInstanceAncestor } from '../instances'
|
|
3
|
-
import { instanceMainComponent } from '../instances/main-component'
|
|
2
|
+
import { findInstanceAncestor, instanceMainComponent } from '../instances/main-component'
|
|
4
3
|
import { setInstanceOverride } from '../instances/overrides'
|
|
5
4
|
import { walkInstanceSources } from '../instances/source-walk'
|
|
6
5
|
import { randomHex } from '../random'
|
|
@@ -16,6 +15,15 @@ export interface ComponentPropertyTarget {
|
|
|
16
15
|
source: SceneNode
|
|
17
16
|
}
|
|
18
17
|
|
|
18
|
+
/**
|
|
19
|
+
* The component that defines a main component's properties: its set when it is a variant,
|
|
20
|
+
* since variants own no properties of their own (desktop 126), else the component itself.
|
|
21
|
+
*/
|
|
22
|
+
export function propertyOwner(graph: SceneGraph, component: SceneNode): SceneNode {
|
|
23
|
+
const parent = component.parentId ? graph.getNode(component.parentId) : undefined
|
|
24
|
+
return component.type === 'COMPONENT' && parent?.type === 'COMPONENT_SET' ? parent : component
|
|
25
|
+
}
|
|
26
|
+
|
|
19
27
|
export function componentPropertyOwners(graph: SceneGraph, instance: SceneNode): SceneNode[] {
|
|
20
28
|
if (instance.type !== 'INSTANCE') return []
|
|
21
29
|
const component = instanceMainComponent(graph, instance)
|
|
@@ -172,6 +180,60 @@ export function applyComponentPropertyValue(
|
|
|
172
180
|
return definition.type === 'INSTANCE_SWAP' ? target : instance
|
|
173
181
|
}
|
|
174
182
|
|
|
183
|
+
export function reapplyComponentPropertyAssignments(
|
|
184
|
+
graph: SceneGraph,
|
|
185
|
+
instanceId: string
|
|
186
|
+
): ComponentPropertyDefinition[] {
|
|
187
|
+
const instance = graph.getNode(instanceId)
|
|
188
|
+
if (instance?.type !== 'INSTANCE') {
|
|
189
|
+
return []
|
|
190
|
+
}
|
|
191
|
+
const definitions = new Map(
|
|
192
|
+
componentPropertyDefinitions(graph, instance).map((definition) => [definition.id, definition])
|
|
193
|
+
)
|
|
194
|
+
const applied: ComponentPropertyDefinition[] = []
|
|
195
|
+
for (const [propertyId, value] of Object.entries(instance.componentPropertyAssignments)) {
|
|
196
|
+
const definition = definitions.get(propertyId)
|
|
197
|
+
if (definition && definition.type !== 'VARIANT') {
|
|
198
|
+
applyComponentPropertyValue(graph, instanceId, definition, value)
|
|
199
|
+
applied.push(definition)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return applied
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const ASSIGNMENT_OVERRIDE_PREFIX = 'componentPropertyAssignments/'
|
|
206
|
+
|
|
207
|
+
export function assignmentOverrideField(propertyId: string): string {
|
|
208
|
+
return `${ASSIGNMENT_OVERRIDE_PREFIX}${propertyId}`
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function overriddenAssignmentId(field: string): string | undefined {
|
|
212
|
+
return field.startsWith(ASSIGNMENT_OVERRIDE_PREFIX)
|
|
213
|
+
? field.slice(ASSIGNMENT_OVERRIDE_PREFIX.length)
|
|
214
|
+
: undefined
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function recordAssignmentOverride(
|
|
218
|
+
graph: SceneGraph,
|
|
219
|
+
instanceId: string,
|
|
220
|
+
propertyId: string
|
|
221
|
+
): void {
|
|
222
|
+
const instance = graph.getNode(instanceId)
|
|
223
|
+
const owner = instance?.parentId ? findInstanceAncestor(graph, instance.parentId) : undefined
|
|
224
|
+
if (!instance || !owner) {
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
setInstanceOverride(
|
|
228
|
+
owner.instanceOverrides,
|
|
229
|
+
owner.id,
|
|
230
|
+
instance.id,
|
|
231
|
+
assignmentOverrideField(propertyId),
|
|
232
|
+
instance.componentPropertyAssignments[propertyId]
|
|
233
|
+
)
|
|
234
|
+
graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })
|
|
235
|
+
}
|
|
236
|
+
|
|
175
237
|
function componentSubtree(graph: SceneGraph, componentId: string): SceneNode[] {
|
|
176
238
|
const component = graph.getNode(componentId)
|
|
177
239
|
if (!component) return []
|
|
@@ -235,6 +297,43 @@ export function removeComponentProperty(
|
|
|
235
297
|
return true
|
|
236
298
|
}
|
|
237
299
|
|
|
300
|
+
/**
|
|
301
|
+
* Moves the boolean, text, instance swap, and slot properties a set's variants define themselves
|
|
302
|
+
* onto the set, under their IDs, so the layers and instances naming them still do: a variant's
|
|
303
|
+
* properties are its set's, as Figma keeps them, and combining components moves theirs to the
|
|
304
|
+
* set (desktop 126). A property the set already has under that ID is not added again. Returns
|
|
305
|
+
* the IDs of the set and the variants it changed.
|
|
306
|
+
*/
|
|
307
|
+
export function moveVariantPropertiesToSet(graph: SceneGraph, setId: string): string[] {
|
|
308
|
+
const set = graph.getNode(setId)
|
|
309
|
+
if (set?.type !== 'COMPONENT_SET') return []
|
|
310
|
+
const changed: string[] = []
|
|
311
|
+
const known = new Set(set.componentPropertyDefinitions.map((definition) => definition.id))
|
|
312
|
+
const moved: ComponentPropertyDefinition[] = []
|
|
313
|
+
for (const variant of graph.getChildren(setId)) {
|
|
314
|
+
const own = variant.componentPropertyDefinitions.filter(
|
|
315
|
+
(definition) => definition.type !== 'VARIANT'
|
|
316
|
+
)
|
|
317
|
+
if (variant.type !== 'COMPONENT' || own.length === 0) continue
|
|
318
|
+
for (const definition of own) {
|
|
319
|
+
if (known.has(definition.id)) continue
|
|
320
|
+
known.add(definition.id)
|
|
321
|
+
moved.push(definition)
|
|
322
|
+
}
|
|
323
|
+
graph.updateNode(variant.id, {
|
|
324
|
+
componentPropertyDefinitions: variant.componentPropertyDefinitions.filter(
|
|
325
|
+
(definition) => definition.type === 'VARIANT'
|
|
326
|
+
)
|
|
327
|
+
})
|
|
328
|
+
changed.push(variant.id)
|
|
329
|
+
}
|
|
330
|
+
if (moved.length === 0) return changed
|
|
331
|
+
graph.updateNode(setId, {
|
|
332
|
+
componentPropertyDefinitions: [...moved, ...set.componentPropertyDefinitions]
|
|
333
|
+
})
|
|
334
|
+
return [setId, ...changed]
|
|
335
|
+
}
|
|
336
|
+
|
|
238
337
|
/** A new component property ID, in the `prop:` form the editor, plugin API, and design JSX share. */
|
|
239
338
|
export function createComponentPropertyId(): string {
|
|
240
339
|
return `prop:${randomHex(8)}`
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { compact, uniq } from 'es-toolkit/array'
|
|
2
|
+
|
|
1
3
|
import type { SceneGraph } from '../graph'
|
|
2
|
-
import type { SceneNode } from '../types'
|
|
4
|
+
import type { ComponentPropertyDefinition, SceneNode } from '../types'
|
|
3
5
|
|
|
4
6
|
/** Variants in reading order on the canvas: top to bottom, then left to right, then by name. */
|
|
5
7
|
export function compareCanvasPosition(a: SceneNode, b: SceneNode): number {
|
|
@@ -7,47 +9,66 @@ export function compareCanvasPosition(a: SceneNode, b: SceneNode): number {
|
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
+
* Whether `a` comes before `b` reading the set as Figma does: variants whose rows overlap, edges
|
|
13
|
+
* touching included, read left to right, and others top to bottom.
|
|
14
|
+
*/
|
|
15
|
+
function readsBefore(a: SceneNode, b: SceneNode): boolean {
|
|
16
|
+
const sameRow = a.y <= b.y + b.height && b.y <= a.y + a.height
|
|
17
|
+
return sameRow ? a.x < b.x || (a.x === b.x && a.y < b.y) : a.y < b.y
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A component set's default variant, the one an instance starts as: the first in reading order,
|
|
22
|
+
* and the first in layer order of those at the same place (measured in Figma desktop 126).
|
|
12
23
|
*/
|
|
13
24
|
export function defaultVariant(graph: SceneGraph, set: SceneNode): SceneNode | undefined {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
25
|
+
let first: SceneNode | undefined
|
|
26
|
+
for (const child of graph.getChildren(set.id)) {
|
|
27
|
+
if (child.type !== 'COMPONENT') continue
|
|
28
|
+
if (!first || readsBefore(child, first)) first = child
|
|
29
|
+
}
|
|
30
|
+
return first
|
|
18
31
|
}
|
|
19
32
|
|
|
20
33
|
/**
|
|
21
|
-
* The value a variant property starts at:
|
|
22
|
-
*
|
|
34
|
+
* The value a variant property starts at: the default variant's, as Figma reads it whatever
|
|
35
|
+
* default the document records, and follows when another variant moves to the top left. The
|
|
36
|
+
* recorded default stands only while the default variant has no value for the property.
|
|
23
37
|
*/
|
|
24
38
|
export function variantDefaultValue(
|
|
25
39
|
graph: SceneGraph,
|
|
26
40
|
set: SceneNode,
|
|
27
|
-
property: { name: string; defaultValue: string
|
|
41
|
+
property: { name: string; defaultValue: string }
|
|
28
42
|
): string {
|
|
29
|
-
|
|
30
|
-
return
|
|
43
|
+
const value = defaultVariant(graph, set)?.componentPropertyValues[property.name]
|
|
44
|
+
return value || property.defaultValue
|
|
31
45
|
}
|
|
32
46
|
|
|
33
47
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
48
|
+
* A variant property's values: the ones the document records, then any its variants use that it
|
|
49
|
+
* does not, since older documents record none and a set's page may have loaded only some
|
|
50
|
+
* variants.
|
|
36
51
|
*/
|
|
37
|
-
export function
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
export function variantOptions(
|
|
53
|
+
graph: SceneGraph,
|
|
54
|
+
set: SceneNode,
|
|
55
|
+
property: { name: string; variantOptions?: string[] }
|
|
56
|
+
): string[] {
|
|
57
|
+
const used = graph
|
|
58
|
+
.getChildren(set.id)
|
|
59
|
+
.map((child) =>
|
|
60
|
+
child.type === 'COMPONENT' ? child.componentPropertyValues[property.name] : ''
|
|
61
|
+
)
|
|
62
|
+
return compact(uniq([...(property.variantOptions ?? []), ...used]))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A set's definition with a variant property's values and default filled in from its variants. */
|
|
66
|
+
export function resolvedVariantDefinition(
|
|
67
|
+
graph: SceneGraph,
|
|
68
|
+
set: SceneNode,
|
|
69
|
+
definition: ComponentPropertyDefinition
|
|
70
|
+
): ComponentPropertyDefinition {
|
|
71
|
+
if (definition.type !== 'VARIANT') return definition
|
|
72
|
+
const options = { ...definition, variantOptions: variantOptions(graph, set, definition) }
|
|
73
|
+
return { ...options, defaultValue: variantDefaultValue(graph, set, options) }
|
|
53
74
|
}
|
package/src/connectors/hit.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getWorldMatrix } from '../coordinate'
|
|
2
2
|
import type { SceneGraph } from '../graph'
|
|
3
3
|
import Matrix from '../matrix'
|
|
4
|
-
import
|
|
4
|
+
import { distanceToSegment } from '../outline/point'
|
|
5
5
|
import type { SceneNode } from '../types'
|
|
6
6
|
import { isStraight, pointAt, type ConnectorPiece } from './pieces'
|
|
7
7
|
import { connectorSublayers } from './sublayers'
|
|
@@ -11,17 +11,6 @@ const CONNECTOR_HIT_SLOP = 4
|
|
|
11
11
|
/** Points sampled along a curved segment to measure distance to it. */
|
|
12
12
|
const CURVE_SAMPLES = 24
|
|
13
13
|
|
|
14
|
-
function distanceToSegment(p: Vector, a: Vector, b: Vector): number {
|
|
15
|
-
const dx = b.x - a.x
|
|
16
|
-
const dy = b.y - a.y
|
|
17
|
-
const lengthSquared = dx * dx + dy * dy
|
|
18
|
-
const t =
|
|
19
|
-
lengthSquared === 0
|
|
20
|
-
? 0
|
|
21
|
-
: Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / lengthSquared))
|
|
22
|
-
return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy))
|
|
23
|
-
}
|
|
24
|
-
|
|
25
14
|
/**
|
|
26
15
|
* Whether a canvas point lands on a connector's line: within half its stroke and a few units of
|
|
27
16
|
* slop, plus `pad`. A connector is picked by its line, not by the box around its route.
|
package/src/connectors/pieces.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cubicPoint } from '../cubic'
|
|
1
2
|
import { computeBounds } from '../geometry'
|
|
2
3
|
import type { Rect, Vector } from '../primitives'
|
|
3
4
|
import type { StrokeCap, VectorNetwork } from '../types'
|
|
@@ -47,11 +48,7 @@ export function pointAt(piece: ConnectorPiece, t: number): Vector {
|
|
|
47
48
|
const { from: a, to: b } = piece
|
|
48
49
|
if (isStraight(piece)) return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t }
|
|
49
50
|
const [c1, c2] = controls(piece)
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
x: u * u * u * a.x + 3 * u * u * t * c1.x + 3 * u * t * t * c2.x + t * t * t * b.x,
|
|
53
|
-
y: u * u * u * a.y + 3 * u * u * t * c1.y + 3 * u * t * t * c2.y + t * t * t * b.y
|
|
54
|
-
}
|
|
51
|
+
return cubicPoint(a, c1, c2, b, t)
|
|
55
52
|
}
|
|
56
53
|
|
|
57
54
|
function derivativeAt(piece: ConnectorPiece, t: number): Vector {
|