@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.2
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/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 +176 -5
- 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 +13 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +58 -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/copy.d.ts.map +1 -1
- package/dist/copy.js +9 -15
- 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 +2 -1
- package/dist/css/index.js +2 -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 +30 -46
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +63 -131
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +1 -0
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +15 -3
- package/dist/graph.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.map +1 -1
- package/dist/hit-test.js +42 -29
- package/dist/hit-test.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 +36 -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/swap.js +288 -0
- package/dist/instances/swap.js.map +1 -0
- package/dist/instances/sync.js +84 -32
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/node-defaults.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 +2 -1
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +19 -4
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/scaling/tree.js +1 -1
- package/dist/shared-styles.d.ts +14 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +63 -2
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +2 -0
- 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/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/index.js +1 -1
- package/dist/transfer/nodes.js +9 -5
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +27 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +28 -5
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +104 -14
- 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/index.d.ts +3 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +23 -8
- package/dist/variables/index.js.map +1 -1
- package/package.json +7 -1
- package/src/arrow-caps.ts +2 -5
- 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 +251 -4
- package/src/components/properties.ts +92 -2
- package/src/components/variants.ts +51 -30
- package/src/connectors/hit.ts +1 -12
- package/src/connectors/pieces.ts +2 -5
- package/src/copy.ts +9 -15
- package/src/css/blend.ts +27 -0
- package/src/css/index.ts +1 -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 +78 -198
- package/src/graph.ts +27 -3
- package/src/guides.ts +5 -0
- package/src/hit-test.ts +67 -54
- package/src/instances/fields.ts +8 -0
- package/src/instances/index.ts +58 -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/swap.ts +451 -0
- package/src/instances/sync.ts +141 -42
- package/src/node-defaults.ts +2 -1
- 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 +15 -2
- package/src/shared-styles.ts +82 -1
- package/src/slots/content.ts +2 -0
- package/src/slots/frames.ts +9 -7
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +2 -2
- package/src/text/picture.ts +0 -4
- package/src/transfer/nodes.ts +7 -5
- package/src/types.ts +28 -0
- package/src/variables/bindings.ts +125 -14
- package/src/variables/fields.ts +46 -1
- package/src/variables/index.ts +35 -2
package/src/shared-styles.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import { compact, isEqual, omit } from 'es-toolkit'
|
|
1
|
+
import { compact, isEqual, omit, pick } from 'es-toolkit'
|
|
2
2
|
|
|
3
|
+
import { BLACK } from './constants'
|
|
4
|
+
import { copyEffects, copyFills, copyLayoutGrids } from './copy'
|
|
3
5
|
import type { SceneGraph } from './graph'
|
|
6
|
+
import { newStrokeGeometry } from './node-defaults'
|
|
7
|
+
import { randomHex } from './random'
|
|
4
8
|
import { styleRunReferences } from './text/references'
|
|
5
9
|
import type {
|
|
6
10
|
CharacterStyleOverride,
|
|
@@ -145,3 +149,80 @@ export function styleDetachmentChanges(
|
|
|
145
149
|
}
|
|
146
150
|
return next
|
|
147
151
|
}
|
|
152
|
+
|
|
153
|
+
/** The node of the shared style with `id`, when the document holds one of `type`. */
|
|
154
|
+
export function findSharedStyle(
|
|
155
|
+
graph: SceneGraph,
|
|
156
|
+
id: string,
|
|
157
|
+
type: SharedStyleType
|
|
158
|
+
): SceneNode | undefined {
|
|
159
|
+
if (!id) return undefined
|
|
160
|
+
for (const node of graph.getAllNodes()) {
|
|
161
|
+
if (node.sharedStyleType === type && node.source.id === id) return node
|
|
162
|
+
}
|
|
163
|
+
return undefined
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** A new local style's ID, in the shape Figma gives one: `S:` and a 40-digit hex key. */
|
|
167
|
+
export function newSharedStyleId(): string {
|
|
168
|
+
return `S:${randomHex(20)},`
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const INTERNAL_CANVAS_NAME = 'Internal Only Canvas'
|
|
172
|
+
|
|
173
|
+
/** The page that holds the document's styles out of view, as a `.fig` file keeps them; made once. */
|
|
174
|
+
export function internalCanvas(graph: SceneGraph): SceneNode {
|
|
175
|
+
const existing = graph.getPages(true).find((page) => page.internalOnly)
|
|
176
|
+
if (existing) return existing
|
|
177
|
+
return graph.createNode('CANVAS', graph.rootId, {
|
|
178
|
+
name: INTERNAL_CANVAS_NAME,
|
|
179
|
+
width: 0,
|
|
180
|
+
height: 0,
|
|
181
|
+
internalOnly: true
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* A paint style's paints as a layer's strokes: every paint, each keeping the weight, alignment,
|
|
187
|
+
* and line style of the stroke it replaces, or of the layer's first; no paints clear the strokes
|
|
188
|
+
* (Figma desktop 126).
|
|
189
|
+
*/
|
|
190
|
+
function strokesFromStyle(target: SceneNode, style: SceneNode): SceneNode['strokes'] {
|
|
191
|
+
// Without strokes, the layer keeps the weight and alignment a new stroke takes.
|
|
192
|
+
const fallback: Stroke = target.strokes[0] ?? {
|
|
193
|
+
type: 'SOLID',
|
|
194
|
+
color: BLACK,
|
|
195
|
+
opacity: 1,
|
|
196
|
+
visible: true,
|
|
197
|
+
...newStrokeGeometry(target)
|
|
198
|
+
}
|
|
199
|
+
return copyFills(style.fills).map((paint, index) => ({
|
|
200
|
+
...paint,
|
|
201
|
+
...pick(target.strokes[index] ?? fallback, STROKE_LINE_KEYS)
|
|
202
|
+
}))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* What a layer takes from a shared style it starts using: the style's fields and the reference
|
|
207
|
+
* that keeps it attached. Without the style's node, the reference alone.
|
|
208
|
+
*/
|
|
209
|
+
export function sharedStylePatch(
|
|
210
|
+
kind: SharedStyleKind,
|
|
211
|
+
target: SceneNode,
|
|
212
|
+
styleId: string,
|
|
213
|
+
style: SceneNode | null
|
|
214
|
+
): Partial<SceneNode> {
|
|
215
|
+
const patch: Partial<SceneNode> = { [STYLE_REF_KEYS[kind]]: styleId }
|
|
216
|
+
if (!style) return patch
|
|
217
|
+
if (kind === 'fill') patch.fills = copyFills(style.fills)
|
|
218
|
+
else if (kind === 'stroke') patch.strokes = strokesFromStyle(target, style)
|
|
219
|
+
else if (kind === 'effect') patch.effects = copyEffects(style.effects)
|
|
220
|
+
else if (kind === 'grid') patch.layoutGrids = copyLayoutGrids(style.layoutGrids)
|
|
221
|
+
else Object.assign(patch, structuredClone(pick(style, SHARED_STYLE_FIELDS.text)))
|
|
222
|
+
return patch
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** A layer leaving a shared style: it keeps the style's values, without the reference. */
|
|
226
|
+
export function sharedStyleDetachPatch(kind: SharedStyleKind): Partial<SceneNode> {
|
|
227
|
+
return { [STYLE_REF_KEYS[kind]]: null }
|
|
228
|
+
}
|
package/src/slots/content.ts
CHANGED
|
@@ -68,6 +68,7 @@ export function claimSlotContent(
|
|
|
68
68
|
): void {
|
|
69
69
|
const { frame, instance, propertyId } = scope
|
|
70
70
|
if (ownsSlotContent(graph, frame, propertyId)) return
|
|
71
|
+
graph.willChangeNode(instance)
|
|
71
72
|
const overrides = instance.instanceOverrides
|
|
72
73
|
for (const node of descendants(graph, frame, false)) {
|
|
73
74
|
overrides.descendants.delete(node.id)
|
|
@@ -76,6 +77,7 @@ export function claimSlotContent(
|
|
|
76
77
|
continue
|
|
77
78
|
}
|
|
78
79
|
// A nested instance keeps the overrides its former owner applied inside it.
|
|
80
|
+
graph.willChangeNode(node)
|
|
79
81
|
for (const inner of descendants(graph, node, true)) {
|
|
80
82
|
const fields = overrides.descendants.get(inner.id)
|
|
81
83
|
if (!fields) continue
|
package/src/slots/frames.ts
CHANGED
|
@@ -56,6 +56,8 @@ interface ParkedSlot {
|
|
|
56
56
|
name: string
|
|
57
57
|
/** Content layer ids, in layer order. */
|
|
58
58
|
content: string[]
|
|
59
|
+
ownerId: string
|
|
60
|
+
propertyId: string
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
/** Owned slot content parked under an instance while its component changes. */
|
|
@@ -112,13 +114,18 @@ export function detachOwnedSlotContent(graph: SceneGraph, instance: SceneNode):
|
|
|
112
114
|
continue
|
|
113
115
|
const content = [...frame.childIds]
|
|
114
116
|
for (const id of content) graph.insertChildAt(id, instance.id, instance.childIds.length)
|
|
115
|
-
parked.slots.push({ path, name, content })
|
|
117
|
+
parked.slots.push({ path, name, content, ownerId: owner.id, propertyId })
|
|
116
118
|
}
|
|
117
119
|
}
|
|
118
120
|
visit(instance, [])
|
|
119
121
|
return parked
|
|
120
122
|
}
|
|
121
123
|
|
|
124
|
+
function assignsOtherContent(owner: SceneNode, propertyId: string, slot: ParkedSlot): boolean {
|
|
125
|
+
const parkedFromHere = owner.id === slot.ownerId && propertyId === slot.propertyId
|
|
126
|
+
return !parkedFromHere && Object.hasOwn(owner.componentPropertyAssignments, propertyId)
|
|
127
|
+
}
|
|
128
|
+
|
|
122
129
|
/** Move parked content into the slots of the same names and paths, and reassign them. */
|
|
123
130
|
export function restoreOwnedSlotContent(
|
|
124
131
|
graph: SceneGraph,
|
|
@@ -142,12 +149,7 @@ export function restoreOwnedSlotContent(
|
|
|
142
149
|
return !!propertyId && slotName(graph, owner, propertyId) === slot.name
|
|
143
150
|
})
|
|
144
151
|
const propertyId = frame && slotPropertyId(frame)
|
|
145
|
-
if (
|
|
146
|
-
!owner ||
|
|
147
|
-
!frame ||
|
|
148
|
-
!propertyId ||
|
|
149
|
-
Object.hasOwn(owner.componentPropertyAssignments, propertyId)
|
|
150
|
-
) {
|
|
152
|
+
if (!owner || !frame || !propertyId || assignsOtherContent(owner, propertyId, slot)) {
|
|
151
153
|
for (const id of slot.content) graph.deleteNode(id)
|
|
152
154
|
continue
|
|
153
155
|
}
|
package/src/text/lengths.ts
CHANGED
|
@@ -13,6 +13,16 @@ export function lineHeightPixels(lineHeight: TextLength | null, fontSize: number
|
|
|
13
13
|
return lineHeight ? textLengthPixels(lineHeight, fontSize) : null
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* How far apart a line height sets lines at `fontSize`, as Figma steps them: a height in pixels
|
|
18
|
+
* as it is, a percent one to whole pixels (Figma desktop 126); null when it is automatic.
|
|
19
|
+
*/
|
|
20
|
+
export function lineStepPixels(lineHeight: TextLength | null, fontSize: number): number | null {
|
|
21
|
+
if (!lineHeight) return null
|
|
22
|
+
const pixels = textLengthPixels(lineHeight, fontSize)
|
|
23
|
+
return lineHeight.unit === 'PIXELS' ? pixels : Math.round(pixels)
|
|
24
|
+
}
|
|
25
|
+
|
|
16
26
|
/** A length in pixels, as a bound number variable and most authored formats give one. */
|
|
17
27
|
export function pixelLength(value: number): TextLength {
|
|
18
28
|
return { value, unit: 'PIXELS' }
|
package/src/text/overflow.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SceneNode } from '../types'
|
|
2
2
|
|
|
3
|
-
type TextBoundsNode = Pick<
|
|
3
|
+
type TextBoundsNode = Pick<SceneNode, 'type' | 'height' | 'derivedTextHeight'>
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* How far a text's lines reach below its box: a fixed-size box does not clip them, as in Figma,
|
package/src/text/picture.ts
CHANGED
package/src/transfer/nodes.ts
CHANGED
|
@@ -65,7 +65,8 @@ export function prepareNodeTransfer(
|
|
|
65
65
|
result.componentId = source.componentId ? node(source.componentId) : null
|
|
66
66
|
result.instanceOverrides = remapInstanceOverrideState(source.instanceOverrides, {
|
|
67
67
|
node,
|
|
68
|
-
variable
|
|
68
|
+
variable,
|
|
69
|
+
collection: (id) => refs.collections.get(id)
|
|
69
70
|
})
|
|
70
71
|
result.boundVariables = Object.fromEntries(
|
|
71
72
|
Object.entries(source.boundVariables).map(([field, id]) => [field, variable(id)])
|
|
@@ -106,10 +107,11 @@ export function prepareNodeTransfer(
|
|
|
106
107
|
id: property(definition.id),
|
|
107
108
|
defaultValue: propertyValue(definition.id, definition.defaultValue)
|
|
108
109
|
}))
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
propertyId
|
|
112
|
-
|
|
110
|
+
// A reference to a property that neither the transfer nor its destination defines names nothing.
|
|
111
|
+
result.componentPropertyReferences = source.componentPropertyReferences.flatMap((reference) => {
|
|
112
|
+
const propertyId = refs.properties.get(reference.propertyId)
|
|
113
|
+
return propertyId ? [{ ...reference, propertyId }] : []
|
|
114
|
+
})
|
|
113
115
|
result.componentPropertyAssignments = values(source.componentPropertyAssignments)
|
|
114
116
|
// Variant values are keyed by display names, unlike definition-ID assignments.
|
|
115
117
|
result.componentPropertyValues = { ...source.componentPropertyValues }
|
package/src/types.ts
CHANGED
|
@@ -443,9 +443,37 @@ export interface PluginDataEntry {
|
|
|
443
443
|
value: string
|
|
444
444
|
}
|
|
445
445
|
|
|
446
|
+
/** A fixed width or height in pixels an export is scaled to, instead of a multiplier. */
|
|
447
|
+
export interface ExportSize {
|
|
448
|
+
type: 'WIDTH' | 'HEIGHT'
|
|
449
|
+
value: number
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** The colour profile an export is written in; the document's own unless set. */
|
|
453
|
+
export type ExportColorProfile = 'DOCUMENT' | 'SRGB' | 'DISPLAY_P3_V4'
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* One export a layer keeps. The options past `scale` and `format` are Figma's and read as Figma's
|
|
457
|
+
* defaults when left out.
|
|
458
|
+
*/
|
|
446
459
|
export interface ExportSetting {
|
|
447
460
|
scale: number
|
|
448
461
|
format: ExportFormatId
|
|
462
|
+
/** Appended to the file name. */
|
|
463
|
+
suffix?: string
|
|
464
|
+
/** A size that replaces `scale`. */
|
|
465
|
+
size?: ExportSize
|
|
466
|
+
/** Only the layer, without what overlaps it (default true). */
|
|
467
|
+
contentsOnly?: boolean
|
|
468
|
+
/** The layer's box rather than everything it draws, for text (default false). */
|
|
469
|
+
useAbsoluteBounds?: boolean
|
|
470
|
+
colorProfile?: ExportColorProfile
|
|
471
|
+
/** SVG: text as outlines rather than `<text>` (default true). */
|
|
472
|
+
svgOutlineText?: boolean
|
|
473
|
+
/** SVG: every layer's name as its element's `id` (default false). */
|
|
474
|
+
svgIdAttribute?: boolean
|
|
475
|
+
/** SVG: strokes as strokes where possible rather than masked outlines (default true). */
|
|
476
|
+
svgSimplifyStroke?: boolean
|
|
449
477
|
}
|
|
450
478
|
|
|
451
479
|
export interface PluginRelaunchDataEntry {
|
|
@@ -1,18 +1,31 @@
|
|
|
1
|
-
import { isEqual } from 'es-toolkit/predicate'
|
|
1
|
+
import { isEmptyObject, isEqual } from 'es-toolkit/predicate'
|
|
2
2
|
|
|
3
3
|
import type { VariableModeFallback } from '.'
|
|
4
4
|
import { copyFills, copyStrokes } from '../copy'
|
|
5
5
|
import type { SceneGraph } from '../graph'
|
|
6
|
-
import { hasInstanceOverride } from '../instances'
|
|
6
|
+
import { hasInstanceOverride } from '../instances/overrides'
|
|
7
7
|
import type { Color } from '../primitives'
|
|
8
8
|
import { pixelLength } from '../text/lengths'
|
|
9
|
-
import type { Fill, SceneNode, Stroke, StyleRun, TextLength } from '../types'
|
|
10
|
-
import {
|
|
9
|
+
import type { Effect, Fill, SceneNode, Stroke, StyleRun, TextLength } from '../types'
|
|
10
|
+
import {
|
|
11
|
+
effectBindableProperties,
|
|
12
|
+
effectBindingTarget,
|
|
13
|
+
NUMERIC_FIELDS,
|
|
14
|
+
isNumericVariableBindingField,
|
|
15
|
+
paintBindingTarget,
|
|
16
|
+
type EffectBindingProperty
|
|
17
|
+
} from './fields'
|
|
11
18
|
import { variableText, variableTruthy } from './text'
|
|
12
19
|
import { variableValueReferences } from './value'
|
|
13
20
|
export {
|
|
21
|
+
EFFECT_BINDING_PROPERTIES,
|
|
22
|
+
effectBindableProperties,
|
|
23
|
+
effectBindingField,
|
|
24
|
+
effectBindingTarget,
|
|
25
|
+
effectBindingVariableType,
|
|
14
26
|
isNumericVariableBindingField,
|
|
15
27
|
paintBindingTarget,
|
|
28
|
+
type EffectBindingProperty,
|
|
16
29
|
TEXT_CONTENT_VARIABLE_TYPES,
|
|
17
30
|
VISIBILITY_VARIABLE_TYPES,
|
|
18
31
|
type PaintBindingList
|
|
@@ -46,6 +59,12 @@ export function assignVariableBindingUnits(
|
|
|
46
59
|
/** Fields a bound number sets as a length in pixels, as Figma's do, whatever unit they held. */
|
|
47
60
|
const TEXT_LENGTH_FIELDS: ReadonlySet<string> = new Set(['lineHeight', 'letterSpacing'])
|
|
48
61
|
|
|
62
|
+
/** Whether the instance a layer sits in, or the instance itself, overrides one of its fields. */
|
|
63
|
+
function overriddenInInstance(graph: SceneGraph, nodeId: string, field: string): boolean {
|
|
64
|
+
const instance = graph.closest(nodeId, (node) => node.type === 'INSTANCE')
|
|
65
|
+
return !!instance && hasInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)
|
|
66
|
+
}
|
|
67
|
+
|
|
49
68
|
/** A bound number as the field stores it. */
|
|
50
69
|
export function boundNumberValue(field: string, value: number): number | TextLength {
|
|
51
70
|
return TEXT_LENGTH_FIELDS.has(field) ? pixelLength(value) : value
|
|
@@ -63,7 +82,7 @@ export function resolvedNumericBindings(
|
|
|
63
82
|
const updates: Partial<SceneNode> = {}
|
|
64
83
|
for (const [field, variableId] of Object.entries(node.boundVariables)) {
|
|
65
84
|
if (!NUMERIC_FIELDS.has(field)) continue
|
|
66
|
-
if ((field === 'width' || field === 'height') &&
|
|
85
|
+
if ((field === 'width' || field === 'height') && overriddenInInstance(graph, node.id, field))
|
|
67
86
|
continue
|
|
68
87
|
const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
|
|
69
88
|
if (value === undefined || !Number.isFinite(value)) continue
|
|
@@ -94,7 +113,7 @@ export function resolvedValueBindings(
|
|
|
94
113
|
const updates: Partial<SceneNode> = {}
|
|
95
114
|
const resolve = (field: 'text' | 'visible') => {
|
|
96
115
|
const variableId = node.boundVariables[field]
|
|
97
|
-
if (!variableId ||
|
|
116
|
+
if (!variableId || overriddenInInstance(graph, node.id, field)) return undefined
|
|
98
117
|
return graph.resolveVariableForNode(node.id, variableId, fallback)
|
|
99
118
|
}
|
|
100
119
|
const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined
|
|
@@ -181,6 +200,56 @@ export function resolvedPaintBindings(
|
|
|
181
200
|
return changes
|
|
182
201
|
}
|
|
183
202
|
|
|
203
|
+
/** `effect` with one bound property set to its variable's value; a colour keeps its alpha. */
|
|
204
|
+
function withBoundEffectValue(
|
|
205
|
+
effect: Effect,
|
|
206
|
+
property: EffectBindingProperty,
|
|
207
|
+
value: number | Color
|
|
208
|
+
): Effect {
|
|
209
|
+
if (typeof value !== 'number') return property === 'color' ? { ...effect, color: value } : effect
|
|
210
|
+
if (property === 'offsetX') return { ...effect, offset: { ...effect.offset, x: value } }
|
|
211
|
+
if (property === 'offsetY') return { ...effect, offset: { ...effect.offset, y: value } }
|
|
212
|
+
if (property === 'radius' || property === 'spread') return { ...effect, [property]: value }
|
|
213
|
+
return effect
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** A bound effect length in an occurrence's coordinates, which are 1:1 outside instances. */
|
|
217
|
+
function scaledLength(length: number | undefined, node: SceneNode): number | undefined {
|
|
218
|
+
return length === undefined ? undefined : length * node.componentScale
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The effects a node's effect bindings resolve to in its mode, when one differs from what the node
|
|
223
|
+
* holds. Lengths stay as the variables give them, negative ones included, as Figma keeps them;
|
|
224
|
+
* drawing treats a negative blur as none. In an instance they take its scale against its
|
|
225
|
+
* component, whenever they were bound, as Figma scales them; a layer scaled outside an instance
|
|
226
|
+
* goes back to the variable's own value.
|
|
227
|
+
*/
|
|
228
|
+
export function resolvedEffectBindings(
|
|
229
|
+
graph: SceneGraph,
|
|
230
|
+
node: SceneNode,
|
|
231
|
+
fallback: VariableModeFallback = 'active'
|
|
232
|
+
): Pick<Partial<SceneNode>, 'effects'> {
|
|
233
|
+
// Most nodes bind nothing, so no copy is made until a value actually changes.
|
|
234
|
+
let effects: Effect[] | undefined
|
|
235
|
+
for (const [field, variableId] of Object.entries(node.boundVariables)) {
|
|
236
|
+
const target = effectBindingTarget(field)
|
|
237
|
+
if (!target) continue
|
|
238
|
+
const effect = (effects ?? node.effects).at(target.index)
|
|
239
|
+
if (!effect || !effectBindableProperties(effect.type).includes(target.property)) continue
|
|
240
|
+
const value =
|
|
241
|
+
target.property === 'color'
|
|
242
|
+
? graph.resolveColorVariableForNode(node.id, variableId, fallback)
|
|
243
|
+
: scaledLength(graph.resolveNumberVariableForNode(node.id, variableId, fallback), node)
|
|
244
|
+
if (value == null || (typeof value === 'number' && !Number.isFinite(value))) continue
|
|
245
|
+
const next = withBoundEffectValue(effect, target.property, value)
|
|
246
|
+
if (isEqual(next, effect)) continue
|
|
247
|
+
effects ??= [...node.effects]
|
|
248
|
+
effects[target.index] = next
|
|
249
|
+
}
|
|
250
|
+
return effects ? { effects } : {}
|
|
251
|
+
}
|
|
252
|
+
|
|
184
253
|
/**
|
|
185
254
|
* The layers a reconcile looks at: those bound to these variables, directly or through a variable
|
|
186
255
|
* aliasing them, or the layers in these subtrees. Without a scope, every layer.
|
|
@@ -257,21 +326,63 @@ function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterab
|
|
|
257
326
|
}
|
|
258
327
|
|
|
259
328
|
/**
|
|
260
|
-
* Resolve live numeric, text, and
|
|
261
|
-
* instance overrides. A scope limits it to the layers a change can reach, so layers whose
|
|
262
|
-
* values differ from their bindings elsewhere in the document are left as saved.
|
|
329
|
+
* Resolve live numeric, text, visibility, and effect bindings, numbers in scene units, without
|
|
330
|
+
* authoring instance overrides. A scope limits it to the layers a change can reach, so layers whose
|
|
331
|
+
* saved values differ from their bindings elsewhere in the document are left as saved. Returns the
|
|
332
|
+
* layers whose change needs a layout pass; effects are not a layout input, so an effect-only change
|
|
333
|
+
* is written but not returned.
|
|
263
334
|
*/
|
|
264
335
|
export function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScope): string[] {
|
|
265
|
-
|
|
336
|
+
return [...reconcileVariableBindingUpdates(graph, scope).keys()]
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* As `reconcileVariableBindings`, with the layout fields each relaid layer received. Effects
|
|
341
|
+
* are written too, but draw without layout, so a layer whose only change is an effect is left out.
|
|
342
|
+
*/
|
|
343
|
+
export function reconcileVariableBindingUpdates(
|
|
344
|
+
graph: SceneGraph,
|
|
345
|
+
scope?: BindingScope
|
|
346
|
+
): Map<string, Partial<SceneNode>> {
|
|
347
|
+
const relaid = new Map<string, Partial<SceneNode>>()
|
|
266
348
|
for (const node of scopedNodes(graph, scope)) {
|
|
267
|
-
const
|
|
349
|
+
const layoutUpdates = {
|
|
268
350
|
...resolvedNumericBindings(graph, node),
|
|
269
351
|
...resolvedValueBindings(graph, node),
|
|
270
352
|
...resolvedRunBindings(graph, node)
|
|
271
353
|
}
|
|
272
|
-
|
|
354
|
+
const updates = { ...layoutUpdates, ...resolvedEffectBindings(graph, node) }
|
|
355
|
+
if (isEmptyObject(updates)) continue
|
|
273
356
|
graph.updateNode(node.id, updates)
|
|
274
|
-
|
|
357
|
+
if (!isEmptyObject(layoutUpdates)) relaid.set(node.id, layoutUpdates)
|
|
358
|
+
}
|
|
359
|
+
return relaid
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** A layer list whose items carry their own bindings, keyed by position such as `fills/1/color`. */
|
|
363
|
+
export type BoundLayerList = 'fills' | 'strokes' | 'effects'
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* `boundVariables` once the `list` items are rebuilt from `sources`, where `sources[i]` is the
|
|
367
|
+
* position the new item `i` had before, or null for an added item. A binding moves with its item
|
|
368
|
+
* and goes with a removed one, as Figma keeps a binding on its paint or effect.
|
|
369
|
+
*/
|
|
370
|
+
export function listBindingsAfterEdit(
|
|
371
|
+
boundVariables: Readonly<Record<string, string>>,
|
|
372
|
+
list: BoundLayerList,
|
|
373
|
+
sources: readonly (number | null)[]
|
|
374
|
+
): Record<string, string> {
|
|
375
|
+
const prefix = `${list}/`
|
|
376
|
+
const moved: Record<string, string> = {}
|
|
377
|
+
for (const [field, variableId] of Object.entries(boundVariables)) {
|
|
378
|
+
if (!field.startsWith(prefix)) {
|
|
379
|
+
moved[field] = variableId
|
|
380
|
+
continue
|
|
381
|
+
}
|
|
382
|
+
const [index, ...property] = field.slice(prefix.length).split('/')
|
|
383
|
+
const next = sources.indexOf(Number(index))
|
|
384
|
+
if (next !== -1 && property.length > 0)
|
|
385
|
+
moved[`${prefix}${next}/${property.join('/')}`] = variableId
|
|
275
386
|
}
|
|
276
|
-
return
|
|
387
|
+
return moved
|
|
277
388
|
}
|
package/src/variables/fields.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { VariableType } from '../types'
|
|
1
|
+
import type { Effect, VariableType } from '../types'
|
|
2
2
|
|
|
3
3
|
export const NUMERIC_FIELDS = new Set([
|
|
4
4
|
'opacity',
|
|
@@ -79,3 +79,48 @@ export function paintBindingTarget(
|
|
|
79
79
|
if (!match) return null
|
|
80
80
|
return { list: match[1] === 'fills' ? 'fills' : 'strokes', index: Number(match[2]) }
|
|
81
81
|
}
|
|
82
|
+
|
|
83
|
+
/** The effect fields that bind to variables, in the order Figma's messages list them. */
|
|
84
|
+
export const EFFECT_BINDING_PROPERTIES = [
|
|
85
|
+
'color',
|
|
86
|
+
'radius',
|
|
87
|
+
'spread',
|
|
88
|
+
'offsetX',
|
|
89
|
+
'offsetY'
|
|
90
|
+
] as const
|
|
91
|
+
|
|
92
|
+
export type EffectBindingProperty = (typeof EFFECT_BINDING_PROPERTIES)[number]
|
|
93
|
+
|
|
94
|
+
const EFFECT_BINDING_FIELD = /^effects\/(\d+)\/(color|radius|spread|offsetX|offsetY)$/
|
|
95
|
+
|
|
96
|
+
const BLUR_BINDING_PROPERTIES = ['radius'] as const satisfies readonly EffectBindingProperty[]
|
|
97
|
+
|
|
98
|
+
/** The node binding field for one property of the effect at `index`, such as `effects/0/radius`. */
|
|
99
|
+
export function effectBindingField(index: number, property: EffectBindingProperty): string {
|
|
100
|
+
return `effects/${index}/${property}`
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The effect and property a binding field names, such as the second effect's blur for
|
|
105
|
+
* `effects/1/radius`, or null for a field that binds anything else.
|
|
106
|
+
*/
|
|
107
|
+
export function effectBindingTarget(
|
|
108
|
+
field: string
|
|
109
|
+
): { index: number; property: EffectBindingProperty } | null {
|
|
110
|
+
if (!field.startsWith('effects/')) return null
|
|
111
|
+
const match = EFFECT_BINDING_FIELD.exec(field)
|
|
112
|
+
if (!match) return null
|
|
113
|
+
return { index: Number(match[1]), property: match[2] as EffectBindingProperty }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The properties of an effect that bind: every shadow property, and only a blur's radius. */
|
|
117
|
+
export function effectBindableProperties(type: Effect['type']): readonly EffectBindingProperty[] {
|
|
118
|
+
return type === 'DROP_SHADOW' || type === 'INNER_SHADOW'
|
|
119
|
+
? EFFECT_BINDING_PROPERTIES
|
|
120
|
+
: BLUR_BINDING_PROPERTIES
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The variable type an effect property binds to: colour to COLOR, lengths to FLOAT. */
|
|
124
|
+
export function effectBindingVariableType(property: EffectBindingProperty): 'COLOR' | 'FLOAT' {
|
|
125
|
+
return property === 'color' ? 'COLOR' : 'FLOAT'
|
|
126
|
+
}
|
package/src/variables/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { partition } from 'es-toolkit/array'
|
|
1
|
+
import { compact, partition, uniq } from 'es-toolkit/array'
|
|
2
2
|
import { omit, omitBy } from 'es-toolkit/object'
|
|
3
3
|
|
|
4
4
|
import { BLACK } from '../constants'
|
|
@@ -7,6 +7,7 @@ import { findInstanceAncestor } from '../instances'
|
|
|
7
7
|
import { bindingOverrideField, setInstanceOverride } from '../instances/overrides'
|
|
8
8
|
import { siblingOrderKeys } from '../order-keys'
|
|
9
9
|
import type {
|
|
10
|
+
Effect,
|
|
10
11
|
Variable,
|
|
11
12
|
VariableCollection,
|
|
12
13
|
VariableCollectionMode,
|
|
@@ -20,6 +21,10 @@ import {
|
|
|
20
21
|
} from './bindings'
|
|
21
22
|
import {
|
|
22
23
|
BOOLEAN_BINDING_FIELDS,
|
|
24
|
+
effectBindableProperties,
|
|
25
|
+
effectBindingTarget,
|
|
26
|
+
effectBindingVariableType,
|
|
27
|
+
type EffectBindingProperty,
|
|
23
28
|
paintBindingTarget,
|
|
24
29
|
STRING_BINDING_FIELDS,
|
|
25
30
|
TEXT_CONTENT_VARIABLE_TYPES,
|
|
@@ -237,6 +242,25 @@ export function getVariablesByType(graph: SceneGraph, type: VariableType): Varia
|
|
|
237
242
|
return [...graph.variables.values()].filter((v) => v.type === type)
|
|
238
243
|
}
|
|
239
244
|
|
|
245
|
+
/** Checks that an effect binding names an effect the node has, a property it binds, and its type. */
|
|
246
|
+
function assertEffectBinding(
|
|
247
|
+
effects: readonly Effect[],
|
|
248
|
+
target: { index: number; property: EffectBindingProperty },
|
|
249
|
+
type: VariableType,
|
|
250
|
+
field: string
|
|
251
|
+
): void {
|
|
252
|
+
const effect = effects.at(target.index)
|
|
253
|
+
if (!effect) {
|
|
254
|
+
throw new Error(`Index ${target.index} out of range for effects (length ${effects.length})`)
|
|
255
|
+
}
|
|
256
|
+
if (!effectBindableProperties(effect.type).includes(target.property)) {
|
|
257
|
+
throw new Error(`A ${effect.type} effect has no bindable ${target.property} ("${field}")`)
|
|
258
|
+
}
|
|
259
|
+
if (type !== effectBindingVariableType(target.property)) {
|
|
260
|
+
throw new Error(`Cannot bind ${type} variable to effect field "${field}"`)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
240
264
|
export function bindVariable(
|
|
241
265
|
graph: SceneGraph,
|
|
242
266
|
nodeId: string,
|
|
@@ -267,10 +291,14 @@ export function bindVariable(
|
|
|
267
291
|
}
|
|
268
292
|
// Auto-remove top-level dead binding (e.g. 'fills') when setting indexed binding
|
|
269
293
|
if (paint.list in node.boundVariables) {
|
|
294
|
+
graph.willChangeNode(node)
|
|
270
295
|
node.boundVariables = omit(node.boundVariables, [paint.list])
|
|
271
296
|
}
|
|
272
297
|
}
|
|
273
298
|
|
|
299
|
+
const effect = effectBindingTarget(field)
|
|
300
|
+
if (effect) assertEffectBinding(node.effects, effect, variable.type, field)
|
|
301
|
+
|
|
274
302
|
if (isNumericVariableBindingField(field) && variable.type !== 'FLOAT') {
|
|
275
303
|
throw new Error(`Cannot bind ${variable.type} variable to scalar field "${field}"`)
|
|
276
304
|
}
|
|
@@ -292,7 +320,8 @@ export function bindVariable(
|
|
|
292
320
|
isNumericVariableBindingField(field) ||
|
|
293
321
|
STRING_BINDING_FIELDS.has(field) ||
|
|
294
322
|
BOOLEAN_BINDING_FIELDS.has(field) ||
|
|
295
|
-
paint !== null
|
|
323
|
+
paint !== null ||
|
|
324
|
+
effect !== null
|
|
296
325
|
|
|
297
326
|
if (!isKnownField) {
|
|
298
327
|
throw new Error(`Unknown binding field "${field}"`)
|
|
@@ -333,6 +362,8 @@ function markBoundVariablesOverrideOnInstance(
|
|
|
333
362
|
const owner = variableBindingOwner(graph, node)
|
|
334
363
|
if (owner.type !== 'INSTANCE') return
|
|
335
364
|
const nearest = findInstanceAncestor(graph, nodeId)
|
|
365
|
+
const instances = uniq(compact([nearest, owner])).filter((x) => x.id !== nodeId)
|
|
366
|
+
for (const instance of instances) graph.willChangeNode(instance)
|
|
336
367
|
if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, 'boundVariables')
|
|
337
368
|
setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, 'boundVariables')
|
|
338
369
|
if (field)
|
|
@@ -343,6 +374,8 @@ function markBoundVariablesOverrideOnInstance(
|
|
|
343
374
|
bindingOverrideField(field),
|
|
344
375
|
node.boundVariables[field] ?? null
|
|
345
376
|
)
|
|
377
|
+
for (const instance of instances)
|
|
378
|
+
graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })
|
|
346
379
|
}
|
|
347
380
|
|
|
348
381
|
export * from './bindings'
|