@open-pencil/scene-graph 0.15.1 → 0.16.0-beta.1
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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/chunks/behaviours/kinds.js +273 -0
- package/dist/chunks/behaviours/kinds.js.map +1 -0
- package/dist/chunks/behaviours/layers.js +596 -0
- package/dist/chunks/behaviours/layers.js.map +1 -0
- package/dist/chunks/behaviours/spec.js +181 -0
- package/dist/chunks/behaviours/spec.js.map +1 -0
- package/dist/chunks/buffered-events.js +57 -0
- package/dist/chunks/buffered-events.js.map +1 -0
- package/dist/chunks/color/contrast.js +57 -0
- package/dist/chunks/color/contrast.js.map +1 -0
- package/dist/chunks/{components/properties.js → color/index.js} +573 -521
- package/dist/chunks/color/index.js.map +1 -0
- package/dist/chunks/comments/document.js +32 -0
- package/dist/chunks/comments/document.js.map +1 -0
- package/dist/chunks/comments/list.js +182 -0
- package/dist/chunks/comments/list.js.map +1 -0
- package/dist/chunks/comments/merge.js +58 -0
- package/dist/chunks/comments/merge.js.map +1 -0
- package/dist/chunks/components/exposure.js +2021 -0
- package/dist/chunks/components/exposure.js.map +1 -0
- package/dist/chunks/components/variants.js +32 -0
- package/dist/chunks/components/variants.js.map +1 -0
- package/dist/chunks/coordinate.js +93 -55
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/css/grid.js +94 -0
- package/dist/chunks/css/grid.js.map +1 -0
- package/dist/chunks/css/index.js +160 -0
- package/dist/chunks/css/index.js.map +1 -0
- package/dist/chunks/fields/geometry.js +14 -0
- package/dist/chunks/fields/geometry.js.map +1 -0
- package/dist/chunks/fields/text.js +36 -0
- package/dist/chunks/fields/text.js.map +1 -0
- package/dist/chunks/group-bounds.js +104 -0
- package/dist/chunks/group-bounds.js.map +1 -0
- package/dist/chunks/icons/glyph.js +147 -0
- package/dist/chunks/icons/glyph.js.map +1 -0
- package/dist/chunks/index.js +1711 -661
- package/dist/chunks/index.js.map +1 -1
- package/dist/color.d.ts +72 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +3 -0
- package/dist/constants.d.ts +2 -2
- package/dist/constants.js +2 -2
- package/dist/constants2.d.ts +3 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +2 -448
- package/dist/coordinate.js +2 -2
- package/dist/copy.d.ts +1 -1
- package/dist/copy.js +1 -1
- package/dist/css.d.ts +39 -0
- package/dist/css.d.ts.map +1 -0
- package/dist/css.js +3 -0
- package/dist/geometry.d.ts +1 -1
- package/dist/geometry.js +1 -1
- package/dist/gradient.d.ts +51 -0
- package/dist/gradient.d.ts.map +1 -0
- package/dist/gradient.js +132 -0
- package/dist/gradient.js.map +1 -0
- package/dist/guides.d.ts +2 -2
- package/dist/guides.js +39 -0
- package/dist/guides.js.map +1 -0
- package/dist/hit-test.d.ts +2 -9
- package/dist/hit-test.js +154 -18
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +4 -8
- package/dist/index.js +20 -5
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout.d.ts +2 -0
- package/dist/layout.js +4 -0
- package/dist/node-defaults.d.ts +2 -8
- package/dist/node-defaults.js +2 -2
- package/dist/order-keys.d.ts +39 -0
- package/dist/order-keys.d.ts.map +1 -0
- package/dist/order-keys.js +136 -0
- package/dist/order-keys.js.map +1 -0
- package/dist/parse-path.d.ts +7 -1
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js +6 -2
- package/dist/parse-path.js.map +1 -1
- package/dist/polygon.d.ts +53 -0
- package/dist/polygon.d.ts.map +1 -0
- package/dist/polygon.js +205 -0
- package/dist/polygon.js.map +1 -0
- package/dist/preview.d.ts +1 -1
- package/dist/random.d.ts +7 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +2 -0
- package/dist/resize.d.ts +11 -2
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +2 -184
- package/dist/snap.d.ts +1 -1
- package/dist/text-direction.d.ts +13 -0
- package/dist/text-direction.d.ts.map +1 -0
- package/dist/text-direction.js +31 -0
- package/dist/text-direction.js.map +1 -0
- package/dist/text-picture.d.ts +1 -1
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text.d.ts +2 -0
- package/dist/text.js +3 -0
- package/dist/types.d.ts +2063 -13
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -1
- package/dist/types2.d.ts +2 -2
- package/dist/undo.d.ts +2 -2
- package/dist/undo.js +2 -2
- package/dist/undo2.d.ts +29 -1
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +2 -29
- package/dist/variables.js +2 -2
- package/dist/variant-properties.d.ts +9 -1
- package/dist/variant-properties.d.ts.map +1 -1
- package/dist/variant-properties.js +36 -1
- package/dist/variant-properties.js.map +1 -1
- package/dist/vector-network.d.ts +1 -1
- package/dist/vector-network.js +1 -1
- package/package.json +60 -9
- package/src/behaviours/kinds.ts +185 -0
- package/src/behaviours/layers.ts +51 -0
- package/src/behaviours/model.ts +192 -0
- package/src/behaviours/schema.ts +58 -0
- package/src/behaviours/spec.ts +223 -0
- package/src/buffered-events.ts +64 -0
- package/src/color/contrast.ts +31 -0
- package/src/color/index.ts +88 -0
- package/src/color/management.ts +210 -0
- package/src/color/normalize.ts +7 -0
- package/src/color/okhcl.ts +201 -0
- package/src/comments/document.ts +51 -0
- package/src/comments/list.ts +52 -0
- package/src/comments/merge.ts +63 -0
- package/src/comments/threads.ts +156 -0
- package/src/components/exposure.ts +61 -0
- package/src/components/properties.ts +18 -15
- package/src/components/variants.ts +53 -0
- package/src/constants.ts +2 -0
- package/src/coordinate.ts +100 -43
- package/src/copy.ts +13 -5
- package/src/css/grid.ts +70 -0
- package/src/css/index.ts +9 -0
- package/src/css/layout.ts +53 -0
- package/src/css/shadows.ts +68 -0
- package/src/css/values.ts +42 -0
- package/src/export-format.ts +12 -0
- package/src/fields/geometry.ts +11 -0
- package/src/fields/text.ts +35 -0
- package/src/gradient.ts +166 -0
- package/src/group-bounds.ts +131 -0
- package/src/guides.ts +39 -0
- package/src/hit-test.ts +219 -25
- package/src/icons/glyph.ts +128 -0
- package/src/icons/model.ts +54 -0
- package/src/icons/schema.ts +18 -0
- package/src/index.ts +343 -55
- package/src/instance-overrides.ts +27 -0
- package/src/instances/copies.ts +239 -0
- package/src/instances/cycles.ts +73 -0
- package/src/instances/fields.ts +143 -0
- package/src/instances/main-component.ts +19 -0
- package/src/instances/placement.ts +29 -0
- package/src/instances/source-walk.ts +41 -0
- package/src/instances/sync.ts +599 -0
- package/src/instances.ts +122 -330
- package/src/layout/aspect-ratio.ts +51 -0
- package/src/{layout-guides.ts → layout/guides.ts} +1 -1
- package/src/layout/index.ts +4 -0
- package/src/layout/keywords.ts +73 -0
- package/src/layout/sizing.ts +196 -0
- package/src/mutation-impact.ts +25 -10
- package/src/node-defaults.ts +50 -2
- package/src/order-keys.ts +162 -0
- package/src/parse-path.ts +10 -2
- package/src/plugin-data/field.ts +131 -0
- package/src/plugin-data/fields.ts +246 -0
- package/src/polygon.ts +211 -0
- package/src/preview.ts +1 -1
- package/src/random.ts +28 -0
- package/src/resize.ts +99 -12
- package/src/scaling/index.ts +2 -0
- package/src/scaling/node.ts +164 -0
- package/src/scaling/tree.ts +50 -0
- package/src/shaders/model.ts +98 -0
- package/src/shaders/schema.ts +48 -0
- package/src/slots/authoring.ts +101 -0
- package/src/slots/content.ts +124 -0
- package/src/slots/frames.ts +160 -0
- package/src/slots/limits.ts +49 -0
- package/src/text/direction.ts +55 -0
- package/src/text/index.ts +2 -0
- package/src/text/lists.ts +138 -0
- package/src/text/paragraphs.ts +241 -0
- package/src/{text-picture.ts → text/picture.ts} +21 -29
- package/src/transfer/apply.ts +84 -0
- package/src/transfer/history.ts +49 -0
- package/src/transfer/identity.ts +16 -0
- package/src/transfer/index.ts +6 -0
- package/src/transfer/nodes.ts +108 -0
- package/src/transfer/plan.ts +107 -0
- package/src/transfer/references.ts +9 -0
- package/src/transfer/remove.ts +39 -0
- package/src/transfer/validation.ts +54 -0
- package/src/transfer/variables.ts +82 -0
- package/src/types.ts +221 -11
- package/src/undo.ts +54 -2
- package/src/variables/bindings.ts +186 -0
- package/src/variables/fields.ts +43 -0
- package/src/variables/token.ts +14 -0
- package/src/variables/units.ts +15 -0
- package/src/variables.ts +118 -69
- package/src/variant-properties.ts +50 -0
- package/dist/chunks/components/properties.js.map +0 -1
- package/dist/chunks/export-scale.js +0 -19
- package/dist/chunks/export-scale.js.map +0 -1
- package/dist/coordinate.d.ts.map +0 -1
- package/dist/coordinate2.d.ts +0 -2
- package/dist/guides2.d.ts +0 -11
- package/dist/guides2.d.ts.map +0 -1
- package/dist/hit-test.d.ts.map +0 -1
- package/dist/layout-guides.d.ts +0 -2
- package/dist/layout-guides.js +0 -2
- package/dist/layout-guides2.d.ts +0 -16
- package/dist/layout-guides2.d.ts.map +0 -1
- package/dist/node-defaults.d.ts.map +0 -1
- package/dist/preview2.d.ts +0 -12
- package/dist/preview2.d.ts.map +0 -1
- package/dist/resize.js.map +0 -1
- package/dist/variables.d.ts.map +0 -1
- package/dist/vector-network2.d.ts +0 -32
- package/dist/vector-network2.d.ts.map +0 -1
- package/src/instance-overrides.test.ts +0 -67
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const NUMERIC_FIELDS = new Set([
|
|
2
|
+
'opacity',
|
|
3
|
+
'width',
|
|
4
|
+
'height',
|
|
5
|
+
'minWidth',
|
|
6
|
+
'maxWidth',
|
|
7
|
+
'minHeight',
|
|
8
|
+
'maxHeight',
|
|
9
|
+
'x',
|
|
10
|
+
'y',
|
|
11
|
+
'rotation',
|
|
12
|
+
'cornerRadius',
|
|
13
|
+
'topLeftRadius',
|
|
14
|
+
'topRightRadius',
|
|
15
|
+
'bottomLeftRadius',
|
|
16
|
+
'bottomRightRadius',
|
|
17
|
+
'strokeWeight',
|
|
18
|
+
'borderTopWeight',
|
|
19
|
+
'borderBottomWeight',
|
|
20
|
+
'borderLeftWeight',
|
|
21
|
+
'borderRightWeight',
|
|
22
|
+
'fontSize',
|
|
23
|
+
'letterSpacing',
|
|
24
|
+
'lineHeight',
|
|
25
|
+
'paddingLeft',
|
|
26
|
+
'paddingRight',
|
|
27
|
+
'paddingTop',
|
|
28
|
+
'paddingBottom',
|
|
29
|
+
'itemSpacing',
|
|
30
|
+
'counterAxisSpacing',
|
|
31
|
+
'gridRowGap',
|
|
32
|
+
'gridColumnGap'
|
|
33
|
+
])
|
|
34
|
+
|
|
35
|
+
export function isNumericVariableBindingField(field: string): boolean {
|
|
36
|
+
return NUMERIC_FIELDS.has(field)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Text content and font family are the string-valued bindings a node accepts. */
|
|
40
|
+
export const STRING_BINDING_FIELDS: ReadonlySet<string> = new Set(['text', 'fontFamily'])
|
|
41
|
+
|
|
42
|
+
/** Visibility is the only boolean-valued binding a node accepts. */
|
|
43
|
+
export const BOOLEAN_BINDING_FIELDS: ReadonlySet<string> = new Set(['visible'])
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { TokenUnit } from '../types'
|
|
2
|
+
|
|
3
|
+
/** `rem` tokens are stored in canvas pixels and shown against this root font size. */
|
|
4
|
+
export const ROOT_FONT_SIZE_PX = 16
|
|
5
|
+
|
|
6
|
+
/** The stored number for a value typed in a unit: `1.5` in `rem` is 24 canvas pixels. */
|
|
7
|
+
export function tokenNumberFromUnit(value: number, unit: TokenUnit): number {
|
|
8
|
+
return unit === 'rem' ? value * ROOT_FONT_SIZE_PX : value
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** A stored number as it reads in its unit: 24 canvas pixels in `rem` is `1.5`. */
|
|
12
|
+
export function tokenNumberInUnit(value: number, unit: TokenUnit): number {
|
|
13
|
+
return unit === 'rem' ? value / ROOT_FONT_SIZE_PX : value
|
|
14
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SceneNode } from '../types'
|
|
2
|
+
import { NUMERIC_FIELDS } from './fields'
|
|
3
|
+
|
|
4
|
+
export function scaleVariableBindingUnits(node: SceneNode, factor: number): Partial<SceneNode> {
|
|
5
|
+
const variableBindingScales = { ...node.variableBindingScales }
|
|
6
|
+
const variableAssignmentScales = { ...node.variableAssignmentScales }
|
|
7
|
+
for (const field of NUMERIC_FIELDS) {
|
|
8
|
+
if (field === 'opacity' || field === 'rotation') continue
|
|
9
|
+
if (field in node.boundVariables)
|
|
10
|
+
variableBindingScales[field] = (variableBindingScales[field] ?? 1) * factor
|
|
11
|
+
if (node.type === 'INSTANCE')
|
|
12
|
+
variableAssignmentScales[field] = (variableAssignmentScales[field] ?? 1) * factor
|
|
13
|
+
}
|
|
14
|
+
return { variableBindingScales, variableAssignmentScales }
|
|
15
|
+
}
|
package/src/variables.ts
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
|
+
import { partition } from 'es-toolkit/array'
|
|
1
2
|
import { omit, omitBy } from 'es-toolkit/object'
|
|
2
3
|
|
|
3
4
|
import { BLACK } from './constants'
|
|
4
5
|
import type { SceneGraph } from './index'
|
|
5
6
|
import { setInstanceOverride } from './instance-overrides'
|
|
7
|
+
import { findInstanceAncestor } from './instances'
|
|
6
8
|
import type { Color } from './primitives'
|
|
7
|
-
import type {
|
|
9
|
+
import type {
|
|
10
|
+
Variable,
|
|
11
|
+
VariableCollection,
|
|
12
|
+
VariableCollectionMode,
|
|
13
|
+
VariableType,
|
|
14
|
+
VariableValue
|
|
15
|
+
} from './types'
|
|
16
|
+
import {
|
|
17
|
+
isNumericVariableBindingField,
|
|
18
|
+
variableBindingOwner,
|
|
19
|
+
assignVariableBindingUnits
|
|
20
|
+
} from './variables/bindings'
|
|
21
|
+
import { BOOLEAN_BINDING_FIELDS, STRING_BINDING_FIELDS } from './variables/fields'
|
|
8
22
|
|
|
9
23
|
export function addVariable(graph: SceneGraph, variable: Variable): void {
|
|
10
24
|
graph.variables.set(variable.id, variable)
|
|
@@ -25,11 +39,19 @@ export function removeVariable(graph: SceneGraph, id: string): void {
|
|
|
25
39
|
for (const node of graph.nodes.values()) {
|
|
26
40
|
const hadBinding = Object.values(node.boundVariables).includes(id)
|
|
27
41
|
if (!hadBinding) continue
|
|
42
|
+
graph.willChangeNode(node)
|
|
28
43
|
node.boundVariables = omitBy(node.boundVariables, (varId) => varId === id) as Record<
|
|
29
44
|
string,
|
|
30
45
|
string
|
|
31
46
|
>
|
|
32
|
-
|
|
47
|
+
node.variableBindingScales = omitBy(
|
|
48
|
+
node.variableBindingScales,
|
|
49
|
+
(_, field) => !(field in node.boundVariables)
|
|
50
|
+
)
|
|
51
|
+
graph.emitter.emit('node:updated', node.id, {
|
|
52
|
+
boundVariables: { ...node.boundVariables },
|
|
53
|
+
variableBindingScales: { ...node.variableBindingScales }
|
|
54
|
+
})
|
|
33
55
|
markBoundVariablesOverrideOnInstance(graph, node.id)
|
|
34
56
|
}
|
|
35
57
|
}
|
|
@@ -114,10 +136,17 @@ export function getActiveModeId(graph: SceneGraph, collectionId: string): string
|
|
|
114
136
|
return collection?.defaultModeId ?? ''
|
|
115
137
|
}
|
|
116
138
|
|
|
139
|
+
/**
|
|
140
|
+
* What a node without an explicit mode falls back to: the mode the editor shows (`active`), or
|
|
141
|
+
* the collection's default (`default`), which is what a saved document means to other tools.
|
|
142
|
+
*/
|
|
143
|
+
export type VariableModeFallback = 'active' | 'default'
|
|
144
|
+
|
|
117
145
|
export function getNodeVariableModeId(
|
|
118
146
|
graph: SceneGraph,
|
|
119
147
|
nodeId: string,
|
|
120
|
-
collectionId: string
|
|
148
|
+
collectionId: string,
|
|
149
|
+
fallback: VariableModeFallback = 'active'
|
|
121
150
|
): string {
|
|
122
151
|
let node = graph.nodes.get(nodeId)
|
|
123
152
|
while (node) {
|
|
@@ -125,6 +154,8 @@ export function getNodeVariableModeId(
|
|
|
125
154
|
if (modeId) return modeId
|
|
126
155
|
node = node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
127
156
|
}
|
|
157
|
+
if (fallback === 'default')
|
|
158
|
+
return graph.variableCollections.get(collectionId)?.defaultModeId ?? ''
|
|
128
159
|
return getActiveModeId(graph, collectionId)
|
|
129
160
|
}
|
|
130
161
|
|
|
@@ -152,6 +183,20 @@ export function addMode(
|
|
|
152
183
|
}
|
|
153
184
|
}
|
|
154
185
|
|
|
186
|
+
/** Add a mode with a new ID from `generateId`; undo and redo replay it with `addMode`. */
|
|
187
|
+
export function createMode(
|
|
188
|
+
graph: SceneGraph,
|
|
189
|
+
generateId: () => string,
|
|
190
|
+
collectionId: string,
|
|
191
|
+
name: string,
|
|
192
|
+
sourceMode?: string
|
|
193
|
+
): string | undefined {
|
|
194
|
+
if (!graph.variableCollections.has(collectionId)) return undefined
|
|
195
|
+
const modeId = generateId()
|
|
196
|
+
addMode(graph, collectionId, modeId, name, sourceMode)
|
|
197
|
+
return modeId
|
|
198
|
+
}
|
|
199
|
+
|
|
155
200
|
export function removeMode(graph: SceneGraph, collectionId: string, modeId: string): void {
|
|
156
201
|
const collection = graph.variableCollections.get(collectionId)
|
|
157
202
|
if (!collection || collection.modes.length <= 1) return
|
|
@@ -185,6 +230,13 @@ export function setDefaultMode(graph: SceneGraph, collectionId: string, modeId:
|
|
|
185
230
|
if (!collection) return
|
|
186
231
|
if (!collection.modes.some((m) => m.modeId === modeId)) return
|
|
187
232
|
collection.defaultModeId = modeId
|
|
233
|
+
collection.modes = modesDefaultFirst(collection)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Figma has no default-mode field: the first mode is the default. */
|
|
237
|
+
export function modesDefaultFirst(collection: VariableCollection): VariableCollectionMode[] {
|
|
238
|
+
const [defaults, rest] = partition(collection.modes, (m) => m.modeId === collection.defaultModeId)
|
|
239
|
+
return [...defaults, ...rest]
|
|
188
240
|
}
|
|
189
241
|
|
|
190
242
|
export function resolveVariable(
|
|
@@ -232,11 +284,12 @@ export function resolveNumberVariable(graph: SceneGraph, variableId: string): nu
|
|
|
232
284
|
export function resolveColorVariableForNode(
|
|
233
285
|
graph: SceneGraph,
|
|
234
286
|
nodeId: string,
|
|
235
|
-
variableId: string
|
|
287
|
+
variableId: string,
|
|
288
|
+
fallback: VariableModeFallback = 'active'
|
|
236
289
|
): Color | undefined {
|
|
237
290
|
const variable = graph.variables.get(variableId)
|
|
238
291
|
if (!variable) return undefined
|
|
239
|
-
const modeId = getNodeVariableModeId(graph, nodeId, variable.collectionId)
|
|
292
|
+
const modeId = getNodeVariableModeId(graph, nodeId, variable.collectionId, fallback)
|
|
240
293
|
const value = resolveVariable(graph, variableId, modeId)
|
|
241
294
|
if (value && typeof value === 'object' && 'r' in value) return value
|
|
242
295
|
return undefined
|
|
@@ -245,15 +298,39 @@ export function resolveColorVariableForNode(
|
|
|
245
298
|
export function resolveNumberVariableForNode(
|
|
246
299
|
graph: SceneGraph,
|
|
247
300
|
nodeId: string,
|
|
248
|
-
variableId: string
|
|
301
|
+
variableId: string,
|
|
302
|
+
fallback: VariableModeFallback = 'active'
|
|
249
303
|
): number | undefined {
|
|
250
304
|
const variable = graph.variables.get(variableId)
|
|
251
305
|
if (!variable) return undefined
|
|
252
|
-
const modeId = getNodeVariableModeId(graph, nodeId, variable.collectionId)
|
|
306
|
+
const modeId = getNodeVariableModeId(graph, nodeId, variable.collectionId, fallback)
|
|
253
307
|
const value = resolveVariable(graph, variableId, modeId)
|
|
254
308
|
return typeof value === 'number' ? value : undefined
|
|
255
309
|
}
|
|
256
310
|
|
|
311
|
+
/** A variable's value in the mode a node is in, aliases followed. */
|
|
312
|
+
export function resolveVariableForNode(
|
|
313
|
+
graph: SceneGraph,
|
|
314
|
+
nodeId: string,
|
|
315
|
+
variableId: string,
|
|
316
|
+
fallback: VariableModeFallback = 'active'
|
|
317
|
+
): VariableValue | undefined {
|
|
318
|
+
const variable = graph.variables.get(variableId)
|
|
319
|
+
if (!variable) return undefined
|
|
320
|
+
const modeId = getNodeVariableModeId(graph, nodeId, variable.collectionId, fallback)
|
|
321
|
+
return resolveVariable(graph, variableId, modeId)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export function resolveStringVariableForNode(
|
|
325
|
+
graph: SceneGraph,
|
|
326
|
+
nodeId: string,
|
|
327
|
+
variableId: string,
|
|
328
|
+
fallback: VariableModeFallback = 'active'
|
|
329
|
+
): string | undefined {
|
|
330
|
+
const value = resolveVariableForNode(graph, nodeId, variableId, fallback)
|
|
331
|
+
return typeof value === 'string' ? value : undefined
|
|
332
|
+
}
|
|
333
|
+
|
|
257
334
|
export function getVariablesForCollection(graph: SceneGraph, collectionId: string): Variable[] {
|
|
258
335
|
const collection = graph.variableCollections.get(collectionId)
|
|
259
336
|
if (!collection) return []
|
|
@@ -266,44 +343,6 @@ export function getVariablesByType(graph: SceneGraph, type: VariableType): Varia
|
|
|
266
343
|
return [...graph.variables.values()].filter((v) => v.type === type)
|
|
267
344
|
}
|
|
268
345
|
|
|
269
|
-
const SCALAR_BINDING_FIELDS: ReadonlySet<string> = new Set([
|
|
270
|
-
'opacity',
|
|
271
|
-
'width',
|
|
272
|
-
'height',
|
|
273
|
-
'cornerRadius',
|
|
274
|
-
'fontSize',
|
|
275
|
-
'letterSpacing',
|
|
276
|
-
'lineHeight',
|
|
277
|
-
'itemSpacing',
|
|
278
|
-
'strokeWeight',
|
|
279
|
-
'paddingLeft',
|
|
280
|
-
'paddingRight',
|
|
281
|
-
'paddingTop',
|
|
282
|
-
'paddingBottom',
|
|
283
|
-
'counterAxisSpacing',
|
|
284
|
-
'topLeftRadius',
|
|
285
|
-
'topRightRadius',
|
|
286
|
-
'bottomLeftRadius',
|
|
287
|
-
'bottomRightRadius',
|
|
288
|
-
'rotation',
|
|
289
|
-
'x',
|
|
290
|
-
'y',
|
|
291
|
-
'minWidth',
|
|
292
|
-
'maxWidth',
|
|
293
|
-
'minHeight',
|
|
294
|
-
'maxHeight',
|
|
295
|
-
'borderTopWeight',
|
|
296
|
-
'borderBottomWeight',
|
|
297
|
-
'borderLeftWeight',
|
|
298
|
-
'borderRightWeight',
|
|
299
|
-
'gridRowGap',
|
|
300
|
-
'gridColumnGap'
|
|
301
|
-
])
|
|
302
|
-
|
|
303
|
-
const STRING_BINDING_FIELDS: ReadonlySet<string> = new Set(['fontFamily'])
|
|
304
|
-
|
|
305
|
-
const BOOLEAN_BINDING_FIELDS: ReadonlySet<string> = new Set(['visible'])
|
|
306
|
-
|
|
307
346
|
export function bindVariable(
|
|
308
347
|
graph: SceneGraph,
|
|
309
348
|
nodeId: string,
|
|
@@ -339,7 +378,7 @@ export function bindVariable(
|
|
|
339
378
|
}
|
|
340
379
|
}
|
|
341
380
|
|
|
342
|
-
if (
|
|
381
|
+
if (isNumericVariableBindingField(field) && variable.type !== 'FLOAT') {
|
|
343
382
|
throw new Error(`Cannot bind ${variable.type} variable to scalar field "${field}"`)
|
|
344
383
|
}
|
|
345
384
|
|
|
@@ -352,7 +391,7 @@ export function bindVariable(
|
|
|
352
391
|
}
|
|
353
392
|
|
|
354
393
|
const isKnownField =
|
|
355
|
-
|
|
394
|
+
isNumericVariableBindingField(field) ||
|
|
356
395
|
STRING_BINDING_FIELDS.has(field) ||
|
|
357
396
|
BOOLEAN_BINDING_FIELDS.has(field) ||
|
|
358
397
|
colorFieldMatch
|
|
@@ -361,39 +400,49 @@ export function bindVariable(
|
|
|
361
400
|
throw new Error(`Unknown binding field "${field}"`)
|
|
362
401
|
}
|
|
363
402
|
|
|
403
|
+
graph.willChangeNode(node)
|
|
364
404
|
node.boundVariables = { ...node.boundVariables, [field]: variableId }
|
|
365
|
-
graph
|
|
366
|
-
markBoundVariablesOverrideOnInstance(graph, nodeId)
|
|
405
|
+
assignVariableBindingUnits(graph, node, field)
|
|
406
|
+
markBoundVariablesOverrideOnInstance(graph, nodeId, field)
|
|
407
|
+
graph.emitter.emit('node:updated', nodeId, {
|
|
408
|
+
boundVariables: { ...node.boundVariables },
|
|
409
|
+
variableBindingScales: { ...node.variableBindingScales }
|
|
410
|
+
})
|
|
367
411
|
}
|
|
368
412
|
|
|
369
413
|
export function unbindVariable(graph: SceneGraph, nodeId: string, field: string): void {
|
|
370
414
|
const node = graph.nodes.get(nodeId)
|
|
371
415
|
if (!node) return
|
|
372
416
|
if (!(field in node.boundVariables)) return
|
|
417
|
+
graph.willChangeNode(node)
|
|
373
418
|
node.boundVariables = omit(node.boundVariables, [field])
|
|
374
|
-
|
|
375
|
-
markBoundVariablesOverrideOnInstance(graph, nodeId)
|
|
419
|
+
node.variableBindingScales = omit(node.variableBindingScales, [field])
|
|
420
|
+
markBoundVariablesOverrideOnInstance(graph, nodeId, field)
|
|
421
|
+
graph.emitter.emit('node:updated', nodeId, {
|
|
422
|
+
boundVariables: { ...node.boundVariables },
|
|
423
|
+
variableBindingScales: { ...node.variableBindingScales }
|
|
424
|
+
})
|
|
376
425
|
}
|
|
377
426
|
|
|
378
|
-
function markBoundVariablesOverrideOnInstance(
|
|
427
|
+
function markBoundVariablesOverrideOnInstance(
|
|
428
|
+
graph: SceneGraph,
|
|
429
|
+
nodeId: string,
|
|
430
|
+
field?: string
|
|
431
|
+
): void {
|
|
379
432
|
const node = graph.nodes.get(nodeId)
|
|
380
433
|
if (!node) return
|
|
381
434
|
|
|
382
|
-
|
|
383
|
-
if (
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
break
|
|
396
|
-
}
|
|
397
|
-
current = parent
|
|
398
|
-
}
|
|
435
|
+
const owner = variableBindingOwner(graph, node)
|
|
436
|
+
if (owner.type !== 'INSTANCE') return
|
|
437
|
+
const nearest = findInstanceAncestor(graph, nodeId)
|
|
438
|
+
if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, 'boundVariables')
|
|
439
|
+
setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, 'boundVariables')
|
|
440
|
+
if (field)
|
|
441
|
+
setInstanceOverride(
|
|
442
|
+
owner.instanceOverrides,
|
|
443
|
+
owner.id,
|
|
444
|
+
nodeId,
|
|
445
|
+
`boundVariables/${field}`,
|
|
446
|
+
node.boundVariables[field] ?? null
|
|
447
|
+
)
|
|
399
448
|
}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { uniq } from 'es-toolkit/array'
|
|
2
|
+
|
|
1
3
|
import type { ComponentPropertyDefinition, SceneNode } from './types'
|
|
4
|
+
import { buildVariantName, parseVariantName } from './variant-name'
|
|
2
5
|
|
|
3
6
|
export interface DerivedVariantProperties {
|
|
4
7
|
definitions: ComponentPropertyDefinition[]
|
|
@@ -46,3 +49,50 @@ export function deriveSlashVariantProperties(
|
|
|
46
49
|
|
|
47
50
|
return { definitions, variants }
|
|
48
51
|
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Variant properties from names written as `Property=Value` pairs, as Figma names variants:
|
|
55
|
+
* `State=On, Size=Large` gives State and Size. Every component must name at least one pair;
|
|
56
|
+
* a property a component leaves out takes an empty value.
|
|
57
|
+
*/
|
|
58
|
+
export function deriveNamedVariantProperties(
|
|
59
|
+
components: ReadonlyArray<Pick<SceneNode, 'id' | 'name'>>,
|
|
60
|
+
createPropertyId: () => string
|
|
61
|
+
): DerivedVariantProperties | null {
|
|
62
|
+
const parsed = components.map((component) => parseVariantName(component.name))
|
|
63
|
+
if (parsed.some((values) => Object.keys(values).length === 0)) return null
|
|
64
|
+
const names = uniq(parsed.flatMap((values) => Object.keys(values)))
|
|
65
|
+
const definitions: ComponentPropertyDefinition[] = names.map((name) => {
|
|
66
|
+
const options = uniq(parsed.map((values) => values[name] ?? ''))
|
|
67
|
+
return {
|
|
68
|
+
id: createPropertyId(),
|
|
69
|
+
name,
|
|
70
|
+
type: 'VARIANT',
|
|
71
|
+
defaultValue: options[0] ?? '',
|
|
72
|
+
variantOptions: options
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
const variants = new Map(
|
|
76
|
+
components.map((component, index) => {
|
|
77
|
+
const componentPropertyValues = Object.fromEntries(
|
|
78
|
+
names.map((name) => [name, parsed[index][name] ?? ''])
|
|
79
|
+
)
|
|
80
|
+
return [
|
|
81
|
+
component.id,
|
|
82
|
+
{ componentPropertyValues, name: buildVariantName(componentPropertyValues) }
|
|
83
|
+
]
|
|
84
|
+
})
|
|
85
|
+
)
|
|
86
|
+
return { definitions, variants }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Variant properties from component names: slash paths first, else `Property=Value` pairs. */
|
|
90
|
+
export function deriveVariantProperties(
|
|
91
|
+
components: ReadonlyArray<Pick<SceneNode, 'id' | 'name'>>,
|
|
92
|
+
createPropertyId: () => string
|
|
93
|
+
): DerivedVariantProperties | null {
|
|
94
|
+
return (
|
|
95
|
+
deriveSlashVariantProperties(components, createPropertyId) ??
|
|
96
|
+
deriveNamedVariantProperties(components, createPropertyId)
|
|
97
|
+
)
|
|
98
|
+
}
|