@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.5
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/arrays.d.ts +17 -0
- package/dist/arrays.d.ts.map +1 -0
- package/dist/arrays.js +38 -0
- package/dist/arrays.js.map +1 -0
- package/dist/buffered-events.js +1 -1
- package/dist/buffered-events.js.map +1 -1
- package/dist/clipping.d.ts +7 -1
- package/dist/clipping.d.ts.map +1 -1
- package/dist/clipping.js +23 -19
- package/dist/clipping.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts +1 -3
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +2 -9
- package/dist/components/properties.js.map +1 -1
- package/dist/corners.d.ts +22 -0
- package/dist/corners.d.ts.map +1 -0
- package/dist/corners.js +52 -0
- package/dist/corners.js.map +1 -0
- package/dist/css/grid.d.ts +4 -1
- package/dist/css/grid.d.ts.map +1 -1
- package/dist/css/grid.js +16 -7
- package/dist/css/grid.js.map +1 -1
- package/dist/css/index.d.ts +2 -2
- package/dist/css/index.js +2 -2
- package/dist/css/values.d.ts +6 -1
- package/dist/css/values.d.ts.map +1 -1
- package/dist/font-style.d.ts +2 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +31 -6
- package/dist/font-style.js.map +1 -1
- package/dist/geometry.d.ts +68 -3
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +155 -5
- package/dist/geometry.js.map +1 -1
- package/dist/gradient.d.ts +9 -2
- package/dist/gradient.d.ts.map +1 -1
- package/dist/gradient.js +29 -1
- package/dist/gradient.js.map +1 -1
- package/dist/graph.d.ts +4 -0
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +30 -4
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +4 -3
- package/dist/group-bounds.js.map +1 -1
- package/dist/guides.d.ts +11 -2
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +30 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +57 -5
- package/dist/hit-test.js.map +1 -1
- package/dist/icons/fit.d.ts +17 -0
- package/dist/icons/fit.d.ts.map +1 -0
- package/dist/icons/fit.js +21 -0
- package/dist/icons/fit.js.map +1 -0
- package/dist/icons/glyph.d.ts.map +1 -1
- package/dist/icons/glyph.js +5 -4
- package/dist/icons/glyph.js.map +1 -1
- package/dist/icons/index.d.ts +2 -1
- package/dist/icons/index.js +2 -1
- package/dist/images.d.ts +14 -4
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +45 -5
- package/dist/images.js.map +1 -1
- package/dist/instances/assignments.js +39 -6
- package/dist/instances/assignments.js.map +1 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +39 -2
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +2 -2
- package/dist/instances/kept-overrides.js.map +1 -1
- package/dist/instances/matching.js +68 -0
- package/dist/instances/matching.js.map +1 -0
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +13 -1
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/protection.js +67 -0
- package/dist/instances/protection.js.map +1 -0
- package/dist/instances/swap.js +7 -5
- package/dist/instances/swap.js.map +1 -1
- package/dist/instances/sync.js +10 -116
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/guides.d.ts +17 -1
- package/dist/layout/guides.d.ts.map +1 -1
- package/dist/layout/guides.js +31 -11
- package/dist/layout/guides.js.map +1 -1
- package/dist/layout/index.d.ts +4 -2
- package/dist/layout/index.js +4 -2
- package/dist/layout/limits.d.ts +40 -0
- package/dist/layout/limits.d.ts.map +1 -0
- package/dist/layout/limits.js +54 -0
- package/dist/layout/limits.js.map +1 -0
- package/dist/layout/sizing.d.ts.map +1 -1
- package/dist/layout/sizing.js +0 -1
- package/dist/layout/sizing.js.map +1 -1
- package/dist/layout/spacing.d.ts +10 -0
- package/dist/layout/spacing.d.ts.map +1 -0
- package/dist/layout/spacing.js +12 -0
- package/dist/layout/spacing.js.map +1 -0
- package/dist/masks.d.ts +28 -0
- package/dist/masks.d.ts.map +1 -0
- package/dist/masks.js +53 -0
- package/dist/masks.js.map +1 -0
- package/dist/matrix.d.ts +2 -1
- package/dist/matrix.d.ts.map +1 -1
- package/dist/matrix.js +14 -1
- package/dist/matrix.js.map +1 -1
- package/dist/node-defaults.d.ts +9 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +16 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/outline/flatten.d.ts.map +1 -1
- package/dist/outline/flatten.js +24 -17
- package/dist/outline/flatten.js.map +1 -1
- package/dist/outline/hit.d.ts +2 -8
- package/dist/outline/hit.d.ts.map +1 -1
- package/dist/outline/hit.js +16 -25
- package/dist/outline/hit.js.map +1 -1
- package/dist/outline/index.d.ts +3 -3
- package/dist/outline/index.js +3 -3
- package/dist/outline/shapes.d.ts +3 -1
- package/dist/outline/shapes.d.ts.map +1 -1
- package/dist/outline/shapes.js +18 -39
- package/dist/outline/shapes.js.map +1 -1
- package/dist/paints/index.d.ts +3 -1
- package/dist/paints/index.js +3 -1
- package/dist/paints/pattern.d.ts +13 -0
- package/dist/paints/pattern.d.ts.map +1 -0
- package/dist/paints/pattern.js +47 -0
- package/dist/paints/pattern.js.map +1 -0
- package/dist/paints/shows.d.ts +11 -0
- package/dist/paints/shows.d.ts.map +1 -0
- package/dist/paints/shows.js +16 -0
- package/dist/paints/shows.js.map +1 -0
- package/dist/paths/arc.d.ts +32 -0
- package/dist/paths/arc.d.ts.map +1 -0
- package/dist/paths/arc.js +49 -0
- package/dist/paths/arc.js.map +1 -0
- package/dist/paths/index.d.ts +5 -0
- package/dist/paths/index.js +5 -0
- package/dist/paths/network.d.ts +43 -0
- package/dist/paths/network.d.ts.map +1 -0
- package/dist/paths/network.js +249 -0
- package/dist/paths/network.js.map +1 -0
- package/dist/paths/sink.d.ts +20 -0
- package/dist/paths/sink.d.ts.map +1 -0
- package/dist/paths/sink.js +32 -0
- package/dist/paths/sink.js.map +1 -0
- package/dist/paths/smooth-corners.d.ts +25 -0
- package/dist/paths/smooth-corners.d.ts.map +1 -0
- package/dist/paths/smooth-corners.js +196 -0
- package/dist/paths/smooth-corners.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +4 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +2 -0
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/polygon.d.ts +1 -3
- package/dist/polygon.d.ts.map +1 -1
- package/dist/polygon.js +1 -16
- package/dist/polygon.js.map +1 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +5 -5
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +25 -3
- package/dist/scaling/node.js.map +1 -1
- package/dist/text/auto-resize.d.ts +15 -0
- package/dist/text/auto-resize.d.ts.map +1 -0
- package/dist/text/auto-resize.js +23 -0
- package/dist/text/auto-resize.js.map +1 -0
- package/dist/text/index.d.ts +4 -3
- package/dist/text/index.js +4 -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 +14 -5
- package/dist/text/overflow.d.ts.map +1 -1
- package/dist/text/overflow.js +24 -6
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/path.js +10 -0
- package/dist/text/path.js.map +1 -0
- package/dist/text/picture.js +5 -5
- package/dist/text/picture.js.map +1 -1
- package/dist/transfer/dependencies.d.ts.map +1 -1
- package/dist/transfer/dependencies.js +3 -2
- package/dist/transfer/dependencies.js.map +1 -1
- package/dist/transfer/nodes.js +2 -1
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +21 -8
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/index.d.ts +4 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +4 -3
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/lookup.d.ts +20 -0
- package/dist/variables/lookup.d.ts.map +1 -0
- package/dist/variables/lookup.js +34 -0
- package/dist/variables/lookup.js.map +1 -0
- package/dist/variables/paints.d.ts +35 -4
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +67 -16
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolution.d.ts +3 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +8 -3
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +1 -1
- package/dist/variables/subtree.js.map +1 -1
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +12 -76
- package/dist/vector-network.js.map +1 -1
- package/package.json +25 -1
- package/src/arrays.ts +37 -0
- package/src/buffered-events.ts +2 -1
- package/src/clipping.ts +11 -8
- package/src/components/properties.ts +1 -13
- package/src/corners.ts +93 -0
- package/src/css/grid.ts +21 -7
- package/src/css/index.ts +1 -1
- package/src/font-style.ts +38 -4
- package/src/geometry.ts +219 -6
- package/src/gradient.ts +23 -1
- package/src/graph.ts +22 -4
- package/src/group-bounds.ts +7 -3
- package/src/guides.ts +26 -1
- package/src/hit-test.ts +68 -10
- package/src/icons/fit.ts +24 -0
- package/src/icons/glyph.ts +5 -4
- package/src/icons/index.ts +1 -0
- package/src/images.ts +67 -7
- package/src/instances/assignments.ts +65 -7
- package/src/instances/index.ts +53 -4
- package/src/instances/kept-overrides.ts +2 -5
- package/src/instances/matching.ts +101 -0
- package/src/instances/overrides.ts +18 -0
- package/src/instances/protection.ts +103 -0
- package/src/instances/swap.ts +11 -9
- package/src/instances/sync.ts +11 -184
- package/src/layout/guides.ts +42 -9
- package/src/layout/index.ts +2 -0
- package/src/layout/limits.ts +64 -0
- package/src/layout/sizing.ts +0 -2
- package/src/layout/spacing.ts +11 -0
- package/src/masks.ts +51 -0
- package/src/matrix.ts +15 -2
- package/src/node-defaults.ts +23 -3
- package/src/outline/flatten.ts +30 -18
- package/src/outline/hit.ts +16 -33
- package/src/outline/index.ts +2 -2
- package/src/outline/shapes.ts +19 -36
- package/src/paints/index.ts +2 -0
- package/src/paints/pattern.ts +45 -0
- package/src/paints/shows.ts +13 -0
- package/src/paths/arc.ts +59 -0
- package/src/paths/index.ts +4 -0
- package/src/paths/network.ts +334 -0
- package/src/paths/sink.ts +50 -0
- package/src/paths/smooth-corners.ts +356 -0
- package/src/plugin-data/fields.ts +2 -0
- package/src/polygon.ts +0 -17
- package/src/resize.ts +6 -7
- package/src/scaling/node.ts +29 -3
- package/src/text/auto-resize.ts +25 -0
- package/src/text/index.ts +1 -0
- package/src/text/lengths.ts +11 -0
- package/src/text/overflow.ts +32 -6
- package/src/text/path.ts +13 -0
- package/src/text/picture.ts +5 -5
- package/src/transfer/dependencies.ts +3 -2
- package/src/transfer/nodes.ts +2 -1
- package/src/types.ts +25 -8
- package/src/variables/index.ts +1 -0
- package/src/variables/lookup.ts +31 -0
- package/src/variables/paints.ts +101 -20
- package/src/variables/resolution.ts +14 -2
- package/src/variables/subtree.ts +1 -1
- package/src/vector-network.ts +17 -138
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { appendAll } from '../arrays'
|
|
1
2
|
import type { SceneGraph } from '../graph'
|
|
2
3
|
import { imagePaintHashes } from '../images'
|
|
3
4
|
import { overriddenBindingField } from '../instances/overrides'
|
|
@@ -70,7 +71,7 @@ export function layerDependencies(
|
|
|
70
71
|
(styleLayers ??= sharedStyleLayers(graph)).get(id)
|
|
71
72
|
const visit = (node: SceneNode): void => {
|
|
72
73
|
for (const hash of layerImageHashes(node)) images.add(hash)
|
|
73
|
-
pending
|
|
74
|
+
appendAll(pending, layerVariableIds(node))
|
|
74
75
|
for (const id of Object.keys(node.variableModes))
|
|
75
76
|
if (graph.variableCollections.has(id)) collections.add(id)
|
|
76
77
|
for (const id of referencedStyleIds(node)) {
|
|
@@ -113,7 +114,7 @@ export function variableDependencies(
|
|
|
113
114
|
variables.add(id)
|
|
114
115
|
collections.add(variable.collectionId)
|
|
115
116
|
for (const value of Object.values(variable.valuesByMode)) {
|
|
116
|
-
pending
|
|
117
|
+
appendAll(pending, variableValueReferences(value))
|
|
117
118
|
// A kept expression can name a mode of a collection none of its aliases reach.
|
|
118
119
|
if (!isUnsupportedVariableValue(value)) continue
|
|
119
120
|
for (const modeId of value.modes) {
|
package/src/transfer/nodes.ts
CHANGED
|
@@ -67,7 +67,8 @@ export function prepareNodeTransfer(
|
|
|
67
67
|
result.instanceOverrides = remapInstanceOverrideState(source.instanceOverrides, {
|
|
68
68
|
node,
|
|
69
69
|
variable,
|
|
70
|
-
collection: (id) => refs.collections.get(id)
|
|
70
|
+
collection: (id) => refs.collections.get(id),
|
|
71
|
+
property: (id) => refs.properties.get(id)
|
|
71
72
|
})
|
|
72
73
|
Object.assign(
|
|
73
74
|
result,
|
package/src/types.ts
CHANGED
|
@@ -43,6 +43,8 @@ export interface NodeChangeObserver {
|
|
|
43
43
|
before: (node: SceneNode) => void
|
|
44
44
|
/** A node was just added. */
|
|
45
45
|
created: (node: SceneNode) => void
|
|
46
|
+
/** A node is about to take a preview's changes, which no history keeps. */
|
|
47
|
+
previewed?: (node: SceneNode) => void
|
|
46
48
|
/**
|
|
47
49
|
* A variable or collection is about to change, be added, or be removed, and is still as it
|
|
48
50
|
* was: absent, for one about to be added.
|
|
@@ -192,7 +194,10 @@ export interface Fill {
|
|
|
192
194
|
blendMode?: BlendMode
|
|
193
195
|
gradientStops?: GradientStop[]
|
|
194
196
|
gradientTransform?: GradientTransform
|
|
197
|
+
/** The image an IMAGE paint draws, or the poster frame a VIDEO paint draws in its clip's place. */
|
|
195
198
|
imageHash?: string
|
|
199
|
+
/** A VIDEO paint's clip, which `imageHash`'s poster frame stands in for when drawn. */
|
|
200
|
+
videoHash?: string
|
|
196
201
|
imageScaleMode?: ImageScaleMode
|
|
197
202
|
/** A crop's image-space window; Fill, Fit and Tile ignore it. */
|
|
198
203
|
imageTransform?: GradientTransform
|
|
@@ -304,7 +309,7 @@ export interface Effect {
|
|
|
304
309
|
}
|
|
305
310
|
|
|
306
311
|
export type ConstraintType = 'MIN' | 'CENTER' | 'MAX' | 'STRETCH' | 'SCALE'
|
|
307
|
-
export type TextAutoResize = 'NONE' | 'HEIGHT' | 'WIDTH_AND_HEIGHT'
|
|
312
|
+
export type TextAutoResize = 'NONE' | 'HEIGHT' | 'WIDTH_AND_HEIGHT'
|
|
308
313
|
|
|
309
314
|
/** Where a connector end attaches to a layer: a side, the side Figma picks, or a fixed point. */
|
|
310
315
|
export type ConnectorMagnet =
|
|
@@ -698,7 +703,7 @@ export interface SceneNode {
|
|
|
698
703
|
height: number
|
|
699
704
|
rotation: number
|
|
700
705
|
source: SourceMetadata
|
|
701
|
-
/** Materialized layout hint imported or generated outside the live
|
|
706
|
+
/** Materialized layout hint imported or generated outside the live layout pass. */
|
|
702
707
|
derivedLayout: Partial<Rect> | null
|
|
703
708
|
|
|
704
709
|
fills: Fill[]
|
|
@@ -789,7 +794,8 @@ export interface SceneNode {
|
|
|
789
794
|
primaryAxisSizing: AxisSizingMode
|
|
790
795
|
counterAxisSizing: AxisSizingMode
|
|
791
796
|
itemSpacing: number
|
|
792
|
-
|
|
797
|
+
/** Space between wrapped lines; null follows `itemSpacing`. Read it with `counterAxisSpacingOf`. */
|
|
798
|
+
counterAxisSpacing: number | null
|
|
793
799
|
paddingTop: number
|
|
794
800
|
paddingRight: number
|
|
795
801
|
paddingBottom: number
|
|
@@ -942,9 +948,10 @@ export interface SceneNode {
|
|
|
942
948
|
textPicture: Uint8Array | null
|
|
943
949
|
derivedTextGlyphs: DerivedTextGlyph[] | null
|
|
944
950
|
/**
|
|
945
|
-
* How
|
|
946
|
-
* changes, or read from the lines a file saved, and never saved
|
|
947
|
-
* clip them, as in Figma, so its visual bounds reach
|
|
951
|
+
* How tall a text's lines are, from the top of the first to the bottom of the last: measured at
|
|
952
|
+
* its width when its text or width changes, or read from the lines a file saved, and never saved
|
|
953
|
+
* itself. A fixed-size box does not clip them, as in Figma, so its visual bounds reach past the
|
|
954
|
+
* box on the side away from its vertical alignment (`textLinesReach`). Null when not known.
|
|
948
955
|
*/
|
|
949
956
|
derivedTextHeight: number | null
|
|
950
957
|
/** Format-neutral layout path for text-on-path nodes. */
|
|
@@ -1128,14 +1135,24 @@ export interface Variable {
|
|
|
1128
1135
|
version?: string
|
|
1129
1136
|
}
|
|
1130
1137
|
|
|
1131
|
-
/** Scalar numeric node fields accepted by numeric property controls. */
|
|
1138
|
+
/** Scalar numeric node fields accepted by numeric property controls; some may also be null. */
|
|
1132
1139
|
export type NumericNodeProperty = {
|
|
1133
|
-
[K in keyof SceneNode]-?: SceneNode[K] extends number
|
|
1140
|
+
[K in keyof SceneNode]-?: SceneNode[K] extends number | null
|
|
1141
|
+
? number extends SceneNode[K]
|
|
1142
|
+
? K
|
|
1143
|
+
: never
|
|
1144
|
+
: never
|
|
1134
1145
|
}[keyof SceneNode]
|
|
1135
1146
|
|
|
1136
1147
|
export interface VariableCollectionMode {
|
|
1137
1148
|
modeId: string
|
|
1138
1149
|
name: string
|
|
1150
|
+
/**
|
|
1151
|
+
* The library mode this one came from, for a mode of a collection copied from a library. A
|
|
1152
|
+
* library that renames its mode renames this one with it, so the layers and the document set
|
|
1153
|
+
* to it keep following the library.
|
|
1154
|
+
*/
|
|
1155
|
+
libraryModeId?: string
|
|
1139
1156
|
/**
|
|
1140
1157
|
* Where the mode applies in CSS: a selector (`[data-theme="dark"]`, `.compact`) or an
|
|
1141
1158
|
* at-rule prelude (`@media (max-width: 640px)`). Absent means the default for its axis.
|
package/src/variables/index.ts
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { SceneGraph } from '../graph'
|
|
2
|
+
import type { VariableCollection, VariableCollectionMode } from '../types'
|
|
3
|
+
|
|
4
|
+
/** What a reference by ID or name finds: one match, none, or several that share the name. */
|
|
5
|
+
export type NamedLookup<T> =
|
|
6
|
+
| { kind: 'found'; value: T }
|
|
7
|
+
| { kind: 'missing' }
|
|
8
|
+
| { kind: 'ambiguous'; matches: T[] }
|
|
9
|
+
|
|
10
|
+
function byName<T extends { name: string }>(items: Iterable<T>, name: string): NamedLookup<T> {
|
|
11
|
+
const matches = [...items].filter((item) => item.name === name)
|
|
12
|
+
if (matches.length === 1) return { kind: 'found', value: matches[0] }
|
|
13
|
+
return matches.length === 0 ? { kind: 'missing' } : { kind: 'ambiguous', matches }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The collection with this ID or, failing that, the one with this exact name. */
|
|
17
|
+
export function lookupCollection(graph: SceneGraph, ref: string): NamedLookup<VariableCollection> {
|
|
18
|
+
const byId = graph.variableCollections.get(ref)
|
|
19
|
+
if (byId) return { kind: 'found', value: byId }
|
|
20
|
+
return byName(graph.variableCollections.values(), ref)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The mode of `collection` with this ID or, failing that, the one with this exact name. */
|
|
24
|
+
export function lookupMode(
|
|
25
|
+
collection: VariableCollection,
|
|
26
|
+
ref: string
|
|
27
|
+
): NamedLookup<VariableCollectionMode> {
|
|
28
|
+
const byId = collection.modes.find((mode) => mode.modeId === ref)
|
|
29
|
+
if (byId) return { kind: 'found', value: byId }
|
|
30
|
+
return byName(collection.modes, ref)
|
|
31
|
+
}
|
package/src/variables/paints.ts
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
|
+
import { isEqual } from 'es-toolkit/predicate'
|
|
2
|
+
|
|
1
3
|
import { copyFills, copyStrokes } from '../copy'
|
|
2
4
|
import type { SceneGraph } from '../graph'
|
|
3
5
|
import { hasInstanceOverride } from '../instances/overrides'
|
|
4
6
|
import type { Color } from '../primitives'
|
|
5
7
|
import type { Fill, SceneNode, Stroke } from '../types'
|
|
6
8
|
import { paintBindingTarget, type PaintBindingList } from './fields'
|
|
7
|
-
import { collectionsResolving,
|
|
9
|
+
import { collectionsResolving, getNodeExplicitVariableModeId } from './resolution'
|
|
8
10
|
import { subtreeNodes } from './subtree'
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Figma keeps a bound paint's color and opacity as a cache of its variable: it draws and reads
|
|
12
14
|
* what the paint holds, and writes the variable's color into it again only once the color the
|
|
13
|
-
* variable gives that layer changes,
|
|
14
|
-
* binding last gave its paint
|
|
15
|
-
*
|
|
16
|
-
* paint's own color as the last one
|
|
15
|
+
* variable gives that layer changes, or the mode set for the layer changes, so an edit to the
|
|
16
|
+
* paint sticks until then. The color each binding last gave its paint, and the modes set for the
|
|
17
|
+
* layer then, live here, beside the graph and never saved, by layer and binding field. A layer
|
|
18
|
+
* without an entry, such as one just restored, counts its paint's own color as the last one
|
|
19
|
+
* applied, in the modes it has now.
|
|
17
20
|
*/
|
|
18
|
-
interface AppliedColor {
|
|
21
|
+
export interface AppliedColor {
|
|
19
22
|
readonly variableId: string
|
|
20
23
|
readonly color: Color
|
|
24
|
+
/** The mode set for the layer, or null, in each collection the variable resolves through. */
|
|
25
|
+
readonly modes: Readonly<Record<string, string | null>>
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
const appliedColors = new WeakMap<SceneNode, Map<string, AppliedColor>>()
|
|
@@ -64,9 +69,32 @@ function sameColor(a: Color, b: Color): boolean {
|
|
|
64
69
|
)
|
|
65
70
|
}
|
|
66
71
|
|
|
67
|
-
|
|
72
|
+
/**
|
|
73
|
+
* The modes set for a layer, or null, in the collections a variable resolves through. Figma
|
|
74
|
+
* refreshes a bound paint whenever one of them changes, even to a mode giving the same color.
|
|
75
|
+
*/
|
|
76
|
+
function explicitModes(
|
|
77
|
+
graph: SceneGraph,
|
|
78
|
+
node: SceneNode,
|
|
79
|
+
variableId: string
|
|
80
|
+
): Record<string, string | null> {
|
|
81
|
+
const modes: Record<string, string | null> = {}
|
|
82
|
+
for (const collectionId of collectionsResolving(graph, [variableId]))
|
|
83
|
+
modes[collectionId] = getNodeExplicitVariableModeId(graph, node.id, collectionId)
|
|
84
|
+
return modes
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Whether the color a binding last applied is stale: another color, or set in other modes. */
|
|
88
|
+
function staleApplied(
|
|
89
|
+
node: SceneNode,
|
|
90
|
+
bound: BoundPaint,
|
|
91
|
+
paint: Fill,
|
|
92
|
+
color: Color,
|
|
93
|
+
modes: Readonly<Record<string, string | null>>
|
|
94
|
+
): boolean {
|
|
68
95
|
const entry = appliedColors.get(node)?.get(bound.field)
|
|
69
|
-
|
|
96
|
+
if (entry?.variableId !== bound.variableId) return !sameColor(paintColor(paint), color)
|
|
97
|
+
return !sameColor(entry.color, color) || !isEqual(entry.modes, modes)
|
|
70
98
|
}
|
|
71
99
|
|
|
72
100
|
/**
|
|
@@ -78,13 +106,63 @@ export function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void
|
|
|
78
106
|
if (entries) appliedColors.set(copy, new Map(entries))
|
|
79
107
|
}
|
|
80
108
|
|
|
81
|
-
|
|
109
|
+
/** The colors copied layers' bindings last applied, by layer id, as a copy captured them. */
|
|
110
|
+
export type AppliedBoundPaints = ReadonlyMap<string, ReadonlyMap<string, AppliedColor>>
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The colors the bindings of the layers `nodeIds` names last applied, captured when they are
|
|
114
|
+
* copied, so a paste counts what the copied paints were applied from, not what the source has now.
|
|
115
|
+
*/
|
|
116
|
+
export function captureAppliedBoundPaints(
|
|
117
|
+
graph: SceneGraph,
|
|
118
|
+
nodeIds: Iterable<string>
|
|
119
|
+
): AppliedBoundPaints {
|
|
120
|
+
const captured = new Map<string, ReadonlyMap<string, AppliedColor>>()
|
|
121
|
+
for (const id of nodeIds) {
|
|
122
|
+
const node = graph.nodes.get(id)
|
|
123
|
+
const entries = node && appliedColors.get(node)
|
|
124
|
+
if (entries) captured.set(id, new Map(entries))
|
|
125
|
+
}
|
|
126
|
+
return captured
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Pasted layers keep their paints, edits included, as Figma's copies do. One copied in this
|
|
131
|
+
* document takes the colors its source's bindings had last applied when it was copied, so a paste
|
|
132
|
+
* into another mode refreshes them as a move there would; one from elsewhere counts the colors
|
|
133
|
+
* its variables give it now as applied, as a loaded document does. `copies` maps each copied
|
|
134
|
+
* layer's id to its copy's.
|
|
135
|
+
*/
|
|
136
|
+
export function keepPastedBoundPaints(
|
|
137
|
+
graph: SceneGraph,
|
|
138
|
+
copies: ReadonlyMap<string, string>,
|
|
139
|
+
applied: AppliedBoundPaints
|
|
140
|
+
): void {
|
|
141
|
+
for (const [sourceId, copyId] of copies) {
|
|
142
|
+
const copy = graph.nodes.get(copyId)
|
|
143
|
+
if (!copy || !hasBoundPaints(copy)) continue
|
|
144
|
+
const entries = applied.get(sourceId)
|
|
145
|
+
if (!entries) {
|
|
146
|
+
recordWrittenBoundPaints(graph, copy, { fills: copy.fills, strokes: copy.strokes })
|
|
147
|
+
continue
|
|
148
|
+
}
|
|
149
|
+
appliedColors.set(copy, new Map(entries))
|
|
150
|
+
refresh(graph, copy, NONE_FORCED)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function recordApplied(
|
|
155
|
+
node: SceneNode,
|
|
156
|
+
bound: BoundPaint,
|
|
157
|
+
color: Color,
|
|
158
|
+
modes: Readonly<Record<string, string | null>>
|
|
159
|
+
): void {
|
|
82
160
|
let entries = appliedColors.get(node)
|
|
83
161
|
if (!entries) {
|
|
84
162
|
entries = new Map()
|
|
85
163
|
appliedColors.set(node, entries)
|
|
86
164
|
}
|
|
87
|
-
entries.set(bound.field, { variableId: bound.variableId, color: { ...color } })
|
|
165
|
+
entries.set(bound.field, { variableId: bound.variableId, color: { ...color }, modes })
|
|
88
166
|
}
|
|
89
167
|
|
|
90
168
|
/** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */
|
|
@@ -143,9 +221,10 @@ function refresh(graph: SceneGraph, node: SceneNode, forced: (field: string) =>
|
|
|
143
221
|
const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
|
|
144
222
|
const paint = node[bound.list].at(bound.index)
|
|
145
223
|
if (!color || !paint) continue
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
224
|
+
const modes = explicitModes(graph, node, bound.variableId)
|
|
225
|
+
const stale = staleApplied(node, bound, paint, color, modes)
|
|
226
|
+
recordApplied(node, bound, color, modes)
|
|
227
|
+
if ((!forced(bound.field) && !stale) || showsColor(paint, color)) continue
|
|
149
228
|
if (bound.list === 'fills') {
|
|
150
229
|
fills ??= copyFills(node.fills)
|
|
151
230
|
applyColor(fills[bound.index], color)
|
|
@@ -204,15 +283,15 @@ export function recordWrittenBoundPaints(
|
|
|
204
283
|
for (const bound of boundPaints(node)) {
|
|
205
284
|
if (!written[bound.list]) continue
|
|
206
285
|
const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
|
|
207
|
-
if (color) recordApplied(node, bound, color)
|
|
286
|
+
if (color) recordApplied(node, bound, color, explicitModes(graph, node, bound.variableId))
|
|
208
287
|
}
|
|
209
288
|
}
|
|
210
289
|
|
|
211
290
|
/**
|
|
212
291
|
* Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose
|
|
213
|
-
* mode the move changed, as Figma does when a layer enters a frame or page
|
|
214
|
-
* move between parents
|
|
215
|
-
* comparing those modes.
|
|
292
|
+
* mode set the move changed, as Figma does when a layer enters a frame or page that sets another
|
|
293
|
+
* mode, or one where none was. A move between parents setting the same modes, or of layers that
|
|
294
|
+
* bind no paint, costs no more than comparing those modes.
|
|
216
295
|
*/
|
|
217
296
|
export function refreshMovedBoundPaints(
|
|
218
297
|
graph: SceneGraph,
|
|
@@ -227,7 +306,8 @@ export function refreshMovedBoundPaints(
|
|
|
227
306
|
[...graph.variableCollections.keys()].filter(
|
|
228
307
|
(id) =>
|
|
229
308
|
!previous ||
|
|
230
|
-
|
|
309
|
+
getNodeExplicitVariableModeId(graph, previous, id) !==
|
|
310
|
+
getNodeExplicitVariableModeId(graph, parentId, id)
|
|
231
311
|
)
|
|
232
312
|
)
|
|
233
313
|
if (changed.size === 0) return
|
|
@@ -289,8 +369,9 @@ export function keepCopyBoundPaints(
|
|
|
289
369
|
}
|
|
290
370
|
const color = graph.resolveColorVariableForNode(target.id, variableId)
|
|
291
371
|
if (!color) return
|
|
292
|
-
|
|
293
|
-
|
|
372
|
+
const modes = explicitModes(graph, target, variableId)
|
|
373
|
+
if (!keeps || staleApplied(target, bound, own, color, modes)) applyColor(paint, color)
|
|
374
|
+
recordApplied(target, bound, color, modes)
|
|
294
375
|
})
|
|
295
376
|
}
|
|
296
377
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { appendAll } from '../arrays'
|
|
1
2
|
import type { SceneGraph } from '../graph'
|
|
2
3
|
import type { Color } from '../primitives'
|
|
3
4
|
import type { Variable, VariableLiteral, VariableValue } from '../types'
|
|
@@ -17,13 +18,24 @@ export function getNodeVariableModeId(
|
|
|
17
18
|
nodeId: string,
|
|
18
19
|
collectionId: string
|
|
19
20
|
): string {
|
|
21
|
+
return (
|
|
22
|
+
getNodeExplicitVariableModeId(graph, nodeId, collectionId) ?? defaultModeId(graph, collectionId)
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The nearest mode set on a node or an ancestor for a collection, or null where none is. */
|
|
27
|
+
export function getNodeExplicitVariableModeId(
|
|
28
|
+
graph: SceneGraph,
|
|
29
|
+
nodeId: string,
|
|
30
|
+
collectionId: string
|
|
31
|
+
): string | null {
|
|
20
32
|
let node = graph.nodes.get(nodeId)
|
|
21
33
|
while (node) {
|
|
22
34
|
const modeId = node.variableModes[collectionId]
|
|
23
35
|
if (modeId) return modeId
|
|
24
36
|
node = node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
25
37
|
}
|
|
26
|
-
return
|
|
38
|
+
return null
|
|
27
39
|
}
|
|
28
40
|
|
|
29
41
|
/**
|
|
@@ -44,7 +56,7 @@ export function collectionsResolving(
|
|
|
44
56
|
if (!variable) continue
|
|
45
57
|
collections.add(variable.collectionId)
|
|
46
58
|
for (const value of Object.values(variable.valuesByMode))
|
|
47
|
-
pending
|
|
59
|
+
appendAll(pending, variableValueReferences(value))
|
|
48
60
|
}
|
|
49
61
|
return collections
|
|
50
62
|
}
|
package/src/variables/subtree.ts
CHANGED
|
@@ -9,7 +9,7 @@ export function subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneN
|
|
|
9
9
|
const node = graph.getNode(id)
|
|
10
10
|
if (!node) continue
|
|
11
11
|
nodes.push(node)
|
|
12
|
-
|
|
12
|
+
for (const childId of node.childIds) pending.push(childId)
|
|
13
13
|
}
|
|
14
14
|
return nodes
|
|
15
15
|
}
|
package/src/vector-network.ts
CHANGED
|
@@ -3,15 +3,10 @@ import { isEqual } from 'es-toolkit/predicate'
|
|
|
3
3
|
import { cloneVectorNetwork, copyVectorRegion } from './copy'
|
|
4
4
|
import type { Mat3 } from './matrix'
|
|
5
5
|
import type { VectorPathWindingRule } from './parse-path'
|
|
6
|
+
import { addOpenSegmentsToPath, addRegionToPath } from './paths/network'
|
|
7
|
+
import { svgPathSink } from './paths/sink'
|
|
6
8
|
import type { Vector } from './primitives'
|
|
7
|
-
import type {
|
|
8
|
-
Fill,
|
|
9
|
-
GeometryPath,
|
|
10
|
-
SceneNode,
|
|
11
|
-
VectorNetwork,
|
|
12
|
-
VectorSegment,
|
|
13
|
-
VectorVertex
|
|
14
|
-
} from './types'
|
|
9
|
+
import type { Fill, GeometryPath, SceneNode, VectorNetwork, VectorSegment } from './types'
|
|
15
10
|
|
|
16
11
|
/** Concatenate vector networks while preserving independent geometry. */
|
|
17
12
|
export function mergeVectorNetworks(networks: readonly VectorNetwork[]): VectorNetwork {
|
|
@@ -344,137 +339,19 @@ function coordinate(value: number, decimals: number | null): number {
|
|
|
344
339
|
return Math.round(value * factor) / factor
|
|
345
340
|
}
|
|
346
341
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
forward: boolean,
|
|
351
|
-
decimals: number | null
|
|
352
|
-
): string {
|
|
353
|
-
const start = forward ? vertices[seg.start] : vertices[seg.end]
|
|
354
|
-
const end = forward ? vertices[seg.end] : vertices[seg.start]
|
|
355
|
-
// Tangents are relative to their own vertex (`segmentToAbsolute`), so tracing a segment end to
|
|
356
|
-
// start swaps them but keeps their sign: each control point stays beside its vertex.
|
|
357
|
-
const ts = forward ? seg.tangentStart : seg.tangentEnd
|
|
358
|
-
const te = forward ? seg.tangentEnd : seg.tangentStart
|
|
359
|
-
|
|
360
|
-
const isStraight =
|
|
361
|
-
Math.abs(ts.x) < 0.001 &&
|
|
362
|
-
Math.abs(ts.y) < 0.001 &&
|
|
363
|
-
Math.abs(te.x) < 0.001 &&
|
|
364
|
-
Math.abs(te.y) < 0.001
|
|
365
|
-
|
|
366
|
-
if (isStraight) {
|
|
367
|
-
return `L${coordinate(end.x, decimals)} ${coordinate(end.y, decimals)}`
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
const cp1x = coordinate(start.x + ts.x, decimals)
|
|
371
|
-
const cp1y = coordinate(start.y + ts.y, decimals)
|
|
372
|
-
const cp2x = coordinate(end.x + te.x, decimals)
|
|
373
|
-
const cp2y = coordinate(end.y + te.y, decimals)
|
|
374
|
-
return `C${cp1x} ${cp1y} ${cp2x} ${cp2y} ${coordinate(end.x, decimals)} ${coordinate(end.y, decimals)}`
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
function traceOrderedSegments(
|
|
378
|
-
segmentIndices: readonly number[],
|
|
379
|
-
segments: VectorSegment[],
|
|
380
|
-
vertices: VectorVertex[],
|
|
381
|
-
decimals: number | null
|
|
382
|
-
): string {
|
|
383
|
-
if (segmentIndices.length === 0) return ''
|
|
384
|
-
const first = segments[segmentIndices[0]]
|
|
385
|
-
const second = segmentIndices.length > 1 ? segments[segmentIndices[1]] : null
|
|
386
|
-
// Start at the first segment endpoint that does not connect to the second segment.
|
|
387
|
-
const secondConnectsStart = second?.start === first.start || second?.end === first.start
|
|
388
|
-
const secondConnectsEnd = second?.start === first.end || second?.end === first.end
|
|
389
|
-
const firstForward = !second || secondConnectsEnd || !secondConnectsStart
|
|
390
|
-
const startIndex = firstForward ? first.start : first.end
|
|
391
|
-
let currentIndex = startIndex
|
|
392
|
-
const parts = [
|
|
393
|
-
`M${coordinate(vertices[startIndex].x, decimals)} ${coordinate(vertices[startIndex].y, decimals)}`
|
|
394
|
-
]
|
|
395
|
-
|
|
396
|
-
for (const segmentIndex of segmentIndices) {
|
|
397
|
-
const segment = segments[segmentIndex]
|
|
398
|
-
const isConnected = segment.start === currentIndex || segment.end === currentIndex
|
|
399
|
-
const forward = segment.start === currentIndex || !isConnected
|
|
400
|
-
if (!isConnected) {
|
|
401
|
-
const segmentStart = forward ? segment.start : segment.end
|
|
402
|
-
parts.push(
|
|
403
|
-
`M${coordinate(vertices[segmentStart].x, decimals)} ${coordinate(vertices[segmentStart].y, decimals)}`
|
|
404
|
-
)
|
|
405
|
-
}
|
|
406
|
-
parts.push(segmentToSVG(segment, vertices, forward, decimals))
|
|
407
|
-
currentIndex = forward ? segment.end : segment.start
|
|
408
|
-
}
|
|
409
|
-
if (parts.length === segmentIndices.length + 1 && currentIndex === startIndex) parts.push('Z')
|
|
410
|
-
return parts.join('')
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
type SegmentAdjacency = ReadonlyMap<number, readonly number[]>
|
|
414
|
-
|
|
415
|
-
function buildSegmentAdjacency(segments: VectorSegment[]): SegmentAdjacency {
|
|
416
|
-
const adjacency = new Map<number, number[]>()
|
|
417
|
-
for (let index = 0; index < segments.length; index++) {
|
|
418
|
-
const segment = segments[index]
|
|
419
|
-
for (const vertexIndex of [segment.start, segment.end]) {
|
|
420
|
-
const incidentSegments = adjacency.get(vertexIndex)
|
|
421
|
-
if (incidentSegments) incidentSegments.push(index)
|
|
422
|
-
else adjacency.set(vertexIndex, [index])
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
return adjacency
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
function findConnectedSegment(
|
|
429
|
-
remaining: ReadonlySet<number>,
|
|
430
|
-
adjacency: SegmentAdjacency,
|
|
431
|
-
vertexIndex: number
|
|
432
|
-
): number | undefined {
|
|
433
|
-
return adjacency.get(vertexIndex)?.find((index) => remaining.has(index))
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
function extendSegmentChain(
|
|
437
|
-
chain: number[],
|
|
438
|
-
startVertex: number,
|
|
439
|
-
remaining: Set<number>,
|
|
440
|
-
segments: VectorSegment[],
|
|
441
|
-
adjacency: SegmentAdjacency
|
|
442
|
-
): void {
|
|
443
|
-
let currentIndex = startVertex
|
|
444
|
-
let nextIndex = findConnectedSegment(remaining, adjacency, currentIndex)
|
|
445
|
-
while (nextIndex !== undefined) {
|
|
446
|
-
chain.push(nextIndex)
|
|
447
|
-
remaining.delete(nextIndex)
|
|
448
|
-
const segment = segments[nextIndex]
|
|
449
|
-
currentIndex = segment.start === currentIndex ? segment.end : segment.start
|
|
450
|
-
nextIndex = findConnectedSegment(remaining, adjacency, currentIndex)
|
|
451
|
-
}
|
|
342
|
+
/** Coordinates written at `decimals`, or as they are for `null`. */
|
|
343
|
+
function coordinateFormat(decimals: number | null): (value: number) => number {
|
|
344
|
+
return (value) => coordinate(value, decimals)
|
|
452
345
|
}
|
|
453
346
|
|
|
347
|
+
/** A network's segments as one open path, chained as the canvas draws them. */
|
|
454
348
|
function unfilledSegmentsToPath(network: VectorNetwork, decimals: number | null): string {
|
|
455
|
-
const { vertices, segments } = network
|
|
456
|
-
const remaining = new Set(segments.map((_, index) => index))
|
|
457
|
-
const adjacency = buildSegmentAdjacency(segments)
|
|
458
349
|
const parts: string[] = []
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
const first = segments[firstIndex]
|
|
465
|
-
|
|
466
|
-
const backward: number[] = []
|
|
467
|
-
extendSegmentChain(backward, first.start, remaining, segments, adjacency)
|
|
468
|
-
backward.reverse()
|
|
469
|
-
|
|
470
|
-
const forward: number[] = []
|
|
471
|
-
extendSegmentChain(forward, first.end, remaining, segments, adjacency)
|
|
472
|
-
|
|
473
|
-
parts.push(
|
|
474
|
-
traceOrderedSegments([...backward, firstIndex, ...forward], segments, vertices, decimals)
|
|
475
|
-
)
|
|
476
|
-
}
|
|
477
|
-
|
|
350
|
+
addOpenSegmentsToPath(
|
|
351
|
+
svgPathSink(parts, coordinateFormat(decimals)),
|
|
352
|
+
network.segments,
|
|
353
|
+
network.vertices
|
|
354
|
+
)
|
|
478
355
|
return parts.join('')
|
|
479
356
|
}
|
|
480
357
|
|
|
@@ -500,9 +377,11 @@ export function vectorNetworkToSVGPaths(
|
|
|
500
377
|
/** Each region of a vector network as path data, its loops traced in turn. */
|
|
501
378
|
export function regionSVGPaths(network: VectorNetwork, decimals: number | null = 2): string[] {
|
|
502
379
|
const { vertices, segments, regions } = network
|
|
503
|
-
return regions.map((region) =>
|
|
504
|
-
|
|
505
|
-
|
|
380
|
+
return regions.map((region) => {
|
|
381
|
+
const parts: string[] = []
|
|
382
|
+
addRegionToPath(svgPathSink(parts, coordinateFormat(decimals)), region, segments, vertices)
|
|
383
|
+
return parts.join('')
|
|
384
|
+
})
|
|
506
385
|
}
|
|
507
386
|
|
|
508
387
|
/** The segments of a vector network that no region encloses. */
|