@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.6
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/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/comments/schema.d.ts.map +1 -1
- package/dist/comments/schema.js +1 -0
- package/dist/comments/schema.js.map +1 -1
- package/dist/comments/threads.d.ts +2 -0
- package/dist/comments/threads.d.ts.map +1 -1
- package/dist/comments/threads.js +11 -5
- package/dist/comments/threads.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts +1 -3
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +3 -10
- package/dist/components/properties.js.map +1 -1
- package/dist/constants.d.ts +12 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +42 -1
- package/dist/constants.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +2 -0
- package/dist/copy.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 +40 -7
- package/dist/css/grid.js.map +1 -1
- package/dist/css/index.d.ts +3 -3
- package/dist/css/index.js +3 -3
- package/dist/css/layout.d.ts +13 -1
- package/dist/css/layout.d.ts.map +1 -1
- package/dist/css/layout.js +26 -1
- package/dist/css/layout.js.map +1 -1
- package/dist/css/values.d.ts +23 -1
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +39 -1
- package/dist/css/values.js.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 +159 -25
- 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 +9 -2
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +46 -10
- 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/ids.js +1 -1
- package/dist/ids.js.map +1 -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/index.d.ts +2 -2
- package/dist/instances/assignments.js +39 -6
- package/dist/instances/assignments.js.map +1 -1
- package/dist/instances/index.d.ts +6 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +58 -9
- 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 +34 -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 +5 -2
- package/dist/layout/index.js +5 -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/layout/strokes.d.ts +10 -0
- package/dist/layout/strokes.d.ts.map +1 -0
- package/dist/layout/strokes.js +30 -0
- package/dist/layout/strokes.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 +65 -38
- 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/parse-path.d.ts +4 -1
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js +15 -1
- package/dist/parse-path.js.map +1 -1
- 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 +63 -0
- package/dist/paths/network.d.ts.map +1 -0
- package/dist/paths/network.js +315 -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/primitives/index.d.ts +1 -7
- package/dist/primitives/index.d.ts.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/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +1 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +2 -9
- package/dist/shared-styles.js.map +1 -1
- package/dist/strokes.d.ts +39 -0
- package/dist/strokes.d.ts.map +1 -0
- package/dist/strokes.js +104 -0
- package/dist/strokes.js.map +1 -0
- 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 +5 -3
- package/dist/text/index.js +5 -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/text/runs.d.ts +29 -0
- package/dist/text/runs.d.ts.map +1 -0
- package/dist/text/runs.js +72 -0
- package/dist/text/runs.js.map +1 -0
- 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 +56 -10
- 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 +13 -79
- package/dist/vector-network.js.map +1 -1
- package/package.json +28 -7
- package/src/buffered-events.ts +2 -1
- package/src/clipping.ts +11 -8
- package/src/comments/schema.ts +1 -0
- package/src/comments/threads.ts +13 -5
- package/src/components/properties.ts +3 -14
- package/src/constants.ts +25 -1
- package/src/copy.ts +2 -0
- package/src/corners.ts +93 -0
- package/src/css/grid.ts +49 -7
- package/src/css/index.ts +13 -2
- package/src/css/layout.ts +36 -0
- package/src/css/values.ts +49 -0
- package/src/font-style.ts +38 -4
- package/src/geometry.ts +223 -28
- package/src/gradient.ts +23 -1
- package/src/graph.ts +35 -10
- 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 +6 -4
- package/src/icons/index.ts +1 -0
- package/src/ids.ts +1 -1
- package/src/images.ts +67 -7
- package/src/instances/assignments.ts +65 -7
- package/src/instances/index.ts +77 -13
- 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 +12 -9
- package/src/instances/sync.ts +52 -184
- package/src/layout/guides.ts +42 -9
- package/src/layout/index.ts +3 -0
- package/src/layout/limits.ts +64 -0
- package/src/layout/sizing.ts +0 -2
- package/src/layout/spacing.ts +11 -0
- package/src/layout/strokes.ts +23 -0
- package/src/masks.ts +51 -0
- package/src/matrix.ts +15 -2
- package/src/node-defaults.ts +71 -36
- package/src/outline/flatten.ts +31 -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/parse-path.ts +14 -0
- package/src/paths/arc.ts +59 -0
- package/src/paths/index.ts +4 -0
- package/src/paths/network.ts +411 -0
- package/src/paths/sink.ts +50 -0
- package/src/paths/smooth-corners.ts +357 -0
- package/src/plugin-data/fields.ts +2 -0
- package/src/polygon.ts +0 -17
- package/src/primitives/index.ts +0 -5
- package/src/resize.ts +6 -7
- package/src/scaling/node.ts +29 -3
- package/src/shaders/model.ts +2 -1
- package/src/shared-styles.ts +3 -4
- package/src/strokes.ts +121 -0
- package/src/text/auto-resize.ts +25 -0
- package/src/text/index.ts +2 -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/text/runs.ts +80 -0
- package/src/transfer/dependencies.ts +4 -2
- package/src/transfer/nodes.ts +2 -1
- package/src/types.ts +62 -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 +15 -2
- package/src/variables/subtree.ts +1 -1
- package/src/vector-network.ts +19 -142
- package/dist/random.d.ts +0 -7
- package/dist/random.d.ts.map +0 -1
- package/dist/random.js +0 -25
- package/dist/random.js.map +0 -1
- package/src/random.ts +0 -28
package/src/text/lengths.ts
CHANGED
|
@@ -23,6 +23,17 @@ export function lineStepPixels(lineHeight: TextLength | null, fontSize: number):
|
|
|
23
23
|
return lineHeight.unit === 'PIXELS' ? pixels : Math.round(pixels)
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
/** How far an automatic line height steps lines when no font metrics measure them. */
|
|
27
|
+
const ESTIMATED_LINE_STEP = 1.2
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* How far apart lines step where no font metrics measure them: as `lineStepPixels` steps them,
|
|
31
|
+
* and 1.2 times the font size when automatic.
|
|
32
|
+
*/
|
|
33
|
+
export function estimatedLineStep(lineHeight: TextLength | null, fontSize: number): number {
|
|
34
|
+
return lineStepPixels(lineHeight, fontSize) ?? fontSize * ESTIMATED_LINE_STEP
|
|
35
|
+
}
|
|
36
|
+
|
|
26
37
|
/** A length in pixels, as a bound number variable and most authored formats give one. */
|
|
27
38
|
export function pixelLength(value: number): TextLength {
|
|
28
39
|
return { value, unit: 'PIXELS' }
|
package/src/text/overflow.ts
CHANGED
|
@@ -1,12 +1,38 @@
|
|
|
1
|
+
import type { VisualBounds } from '../geometry'
|
|
1
2
|
import type { SceneNode } from '../types'
|
|
2
3
|
|
|
3
|
-
type
|
|
4
|
+
type TextBoxNode = Pick<SceneNode, 'height' | 'textAlignVertical'>
|
|
5
|
+
type TextBoundsNode = TextBoxNode & Pick<SceneNode, 'type' | 'derivedTextHeight'>
|
|
6
|
+
|
|
7
|
+
export type TextLinesReach = Pick<VisualBounds, 'minY' | 'maxY'>
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Where the top of laid-out lines `contentHeight` tall sits in a text's box, by its vertical
|
|
11
|
+
* alignment. Lines taller than the box overflow on the side away from the alignment, as Figma
|
|
12
|
+
* lays them out.
|
|
13
|
+
*/
|
|
14
|
+
export function textVerticalOffset(node: TextBoxNode, contentHeight: number): number {
|
|
15
|
+
const available = node.height - contentHeight
|
|
16
|
+
if (node.textAlignVertical === 'CENTER') {
|
|
17
|
+
return available / 2
|
|
18
|
+
}
|
|
19
|
+
if (node.textAlignVertical === 'BOTTOM') {
|
|
20
|
+
return available
|
|
21
|
+
}
|
|
22
|
+
return 0
|
|
23
|
+
}
|
|
4
24
|
|
|
5
25
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
26
|
+
* The rows a text draws its lines over, in its own axes: its box, grown by the lines a fixed-size
|
|
27
|
+
* box lets run past it above or below, as in Figma.
|
|
8
28
|
*/
|
|
9
|
-
export function
|
|
10
|
-
if (node.type !== 'TEXT' || !node.derivedTextHeight)
|
|
11
|
-
|
|
29
|
+
export function textLinesReach(node: TextBoundsNode): TextLinesReach {
|
|
30
|
+
if (node.type !== 'TEXT' || !node.derivedTextHeight) {
|
|
31
|
+
return { minY: 0, maxY: node.height }
|
|
32
|
+
}
|
|
33
|
+
const linesTop = textVerticalOffset(node, node.derivedTextHeight)
|
|
34
|
+
return {
|
|
35
|
+
minY: Math.min(0, linesTop),
|
|
36
|
+
maxY: Math.max(node.height, linesTop + node.derivedTextHeight)
|
|
37
|
+
}
|
|
12
38
|
}
|
package/src/text/path.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SceneNode } from '../types'
|
|
2
|
+
|
|
3
|
+
export function followsPath(
|
|
4
|
+
node: Pick<SceneNode, 'derivedTextGlyphs' | 'textPathData' | 'textPathBox'>
|
|
5
|
+
): boolean {
|
|
6
|
+
const glyphs = node.derivedTextGlyphs ?? []
|
|
7
|
+
if (glyphs.length === 0) {
|
|
8
|
+
return false
|
|
9
|
+
}
|
|
10
|
+
return (
|
|
11
|
+
!!node.textPathData || !!node.textPathBox || glyphs.some((glyph) => (glyph.rotation ?? 0) !== 0)
|
|
12
|
+
)
|
|
13
|
+
}
|
package/src/text/picture.ts
CHANGED
|
@@ -36,16 +36,16 @@ export function textCacheInvalidationChanges(
|
|
|
36
36
|
keys.some((key) => GLYPH_AFFECTING_KEYS.has(key)) ||
|
|
37
37
|
TEXT_LAYOUT_FIELDS.some((key) => keys.includes(key))
|
|
38
38
|
if (glyphsInvalidated && !('derivedLayout' in changes)) invalidated.derivedLayout = null
|
|
39
|
-
|
|
40
|
-
// other mutation path must drop stale baked glyphs and path identity rather
|
|
41
|
-
// than pair new text/style with old visible outlines.
|
|
42
|
-
if (node.derivedTextGlyphs && glyphsInvalidated && !changes.derivedTextGlyphs) {
|
|
39
|
+
if (glyphsInvalidated && !changes.derivedTextGlyphs) {
|
|
43
40
|
// Path-text resize supplies transformed glyphs through the resize workflow. A raw box
|
|
44
41
|
// preview keeps them until that workflow commits or an actual shaping edit occurs.
|
|
45
42
|
const onlyPathBoxResize =
|
|
46
43
|
!!node.textPathData && keys.every((key) => key === 'width' || key === 'height')
|
|
47
|
-
|
|
44
|
+
const placesPath = keys.includes('textPathBox') && !!changes.textPathBox
|
|
45
|
+
if (node.derivedTextGlyphs && !onlyPathBoxResize) {
|
|
48
46
|
invalidated.derivedTextGlyphs = null
|
|
47
|
+
}
|
|
48
|
+
if (node.textPathData && !onlyPathBoxResize && !placesPath) {
|
|
49
49
|
invalidated.textPathData = null
|
|
50
50
|
}
|
|
51
51
|
}
|
package/src/text/runs.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { isEqual } from 'es-toolkit/predicate'
|
|
2
|
+
|
|
3
|
+
import type { CharacterStyleOverride, SceneNode, StyleRun } from '../types'
|
|
4
|
+
|
|
5
|
+
/** The run that styles `text[index]`, if one does. */
|
|
6
|
+
export function styleRunAt(runs: readonly StyleRun[], index: number): StyleRun | undefined {
|
|
7
|
+
return runs.find((run) => index >= run.start && index < run.start + run.length)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** The language `text[index]` is shaped in: its run's, or the text's own. */
|
|
11
|
+
export function textLanguageAt(
|
|
12
|
+
node: Pick<SceneNode, 'styleRuns' | 'textLanguage'>,
|
|
13
|
+
index: number
|
|
14
|
+
): string | null {
|
|
15
|
+
const language = styleRunAt(node.styleRuns, index)?.style.textLanguage
|
|
16
|
+
return language !== undefined ? language : node.textLanguage
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Adds `style` over `[start, start + length)` to runs built in text order, extending the last
|
|
21
|
+
* run instead when it ends at `start` with an equal style. An empty style or range adds nothing,
|
|
22
|
+
* since those characters keep the text's own style.
|
|
23
|
+
*/
|
|
24
|
+
export function appendStyleRun(
|
|
25
|
+
runs: StyleRun[],
|
|
26
|
+
start: number,
|
|
27
|
+
length: number,
|
|
28
|
+
style: CharacterStyleOverride
|
|
29
|
+
): void {
|
|
30
|
+
if (length <= 0 || Object.keys(style).length === 0) return
|
|
31
|
+
const previous = runs.at(-1)
|
|
32
|
+
if (previous && previous.start + previous.length === start && isEqual(previous.style, style))
|
|
33
|
+
previous.length += length
|
|
34
|
+
else runs.push({ start, length, style })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A stretch of text in one style: a run's, or the text's own where `style` is null. */
|
|
38
|
+
export interface StyleRunPiece {
|
|
39
|
+
start: number
|
|
40
|
+
end: number
|
|
41
|
+
style: CharacterStyleOverride | null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The first `length` characters cut at their runs, in order and covering every character once:
|
|
46
|
+
* a run's part that an earlier run already covers, or that lies past the text, is left out.
|
|
47
|
+
* `boundary` moves each cut to where the text may be cut, such as out of a surrogate pair.
|
|
48
|
+
*/
|
|
49
|
+
export function styleRunPieces(
|
|
50
|
+
runs: readonly StyleRun[],
|
|
51
|
+
length: number,
|
|
52
|
+
boundary: (index: number) => number = (index) => index
|
|
53
|
+
): StyleRunPiece[] {
|
|
54
|
+
const pieces: StyleRunPiece[] = []
|
|
55
|
+
let at = 0
|
|
56
|
+
for (const run of runs.toSorted((a, b) => a.start - b.start)) {
|
|
57
|
+
const start = Math.min(length, Math.max(at, boundary(run.start)))
|
|
58
|
+
const end = Math.min(length, boundary(run.start + run.length))
|
|
59
|
+
if (start > at) pieces.push({ start: at, end: start, style: null })
|
|
60
|
+
if (end > start) pieces.push({ start, end, style: run.style })
|
|
61
|
+
at = Math.max(start, end)
|
|
62
|
+
}
|
|
63
|
+
if (at < length) pieces.push({ start: at, end: length, style: null })
|
|
64
|
+
return pieces
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Each of the first `length` characters' overrides, merged from the runs over it in order. */
|
|
68
|
+
export function characterOverrides(
|
|
69
|
+
runs: readonly StyleRun[],
|
|
70
|
+
length: number
|
|
71
|
+
): CharacterStyleOverride[] {
|
|
72
|
+
const styles = Array.from({ length }, (): CharacterStyleOverride => ({}))
|
|
73
|
+
for (const run of runs) {
|
|
74
|
+
const end = Math.min(length, run.start + run.length)
|
|
75
|
+
for (let index = Math.max(0, run.start); index < end; index++) {
|
|
76
|
+
styles[index] = { ...styles[index], ...run.style }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return styles
|
|
80
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { appendAll } from '@open-pencil/std/arrays'
|
|
2
|
+
|
|
1
3
|
import type { SceneGraph } from '../graph'
|
|
2
4
|
import { imagePaintHashes } from '../images'
|
|
3
5
|
import { overriddenBindingField } from '../instances/overrides'
|
|
@@ -70,7 +72,7 @@ export function layerDependencies(
|
|
|
70
72
|
(styleLayers ??= sharedStyleLayers(graph)).get(id)
|
|
71
73
|
const visit = (node: SceneNode): void => {
|
|
72
74
|
for (const hash of layerImageHashes(node)) images.add(hash)
|
|
73
|
-
pending
|
|
75
|
+
appendAll(pending, layerVariableIds(node))
|
|
74
76
|
for (const id of Object.keys(node.variableModes))
|
|
75
77
|
if (graph.variableCollections.has(id)) collections.add(id)
|
|
76
78
|
for (const id of referencedStyleIds(node)) {
|
|
@@ -113,7 +115,7 @@ export function variableDependencies(
|
|
|
113
115
|
variables.add(id)
|
|
114
116
|
collections.add(variable.collectionId)
|
|
115
117
|
for (const value of Object.values(variable.valuesByMode)) {
|
|
116
|
-
pending
|
|
118
|
+
appendAll(pending, variableValueReferences(value))
|
|
117
119
|
// A kept expression can name a mode of a collection none of its aliases reach.
|
|
118
120
|
if (!isUnsupportedVariableValue(value)) continue
|
|
119
121
|
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
|
|
@@ -282,6 +287,33 @@ export interface ProgressiveBlur {
|
|
|
282
287
|
endOffset: Vector
|
|
283
288
|
}
|
|
284
289
|
|
|
290
|
+
/**
|
|
291
|
+
* A Noise effect's grain, in Figma's plugin terms: `MONOTONE` grains in the effect's colour,
|
|
292
|
+
* `DUOTONE` in it and `secondaryColor`, `MULTITONE` in many colours at `opacity`. `noiseSize` is a
|
|
293
|
+
* grain's size in px, which Figma keeps within 0 to 100, and `density` how much of the layer the
|
|
294
|
+
* grains cover, 0 to 1. `seed` picks the pattern; Figma's plugin API neither shows nor sets it.
|
|
295
|
+
*/
|
|
296
|
+
export interface NoiseEffectParams {
|
|
297
|
+
noiseType: NoiseType
|
|
298
|
+
noiseSize: Vector
|
|
299
|
+
density: number
|
|
300
|
+
secondaryColor?: Color
|
|
301
|
+
opacity?: number
|
|
302
|
+
seed?: number
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* A Texture effect's grain, which roughens the layer's edges: `noiseSize` is the grain's size in
|
|
307
|
+
* px, within 0 to 100 as Figma keeps it, and `clipToShape` keeps the roughened layer inside its
|
|
308
|
+
* own outline. The effect's `radius` is how far the grain moves the edges. `.fig` files name the
|
|
309
|
+
* effect `GRAIN`.
|
|
310
|
+
*/
|
|
311
|
+
export interface TextureEffectParams {
|
|
312
|
+
noiseSize: Vector
|
|
313
|
+
clipToShape: boolean
|
|
314
|
+
seed?: number
|
|
315
|
+
}
|
|
316
|
+
|
|
285
317
|
export interface Effect {
|
|
286
318
|
type:
|
|
287
319
|
| 'DROP_SHADOW'
|
|
@@ -290,6 +322,8 @@ export interface Effect {
|
|
|
290
322
|
| 'BACKGROUND_BLUR'
|
|
291
323
|
| 'FOREGROUND_BLUR'
|
|
292
324
|
| 'GLASS'
|
|
325
|
+
| 'NOISE'
|
|
326
|
+
| 'TEXTURE'
|
|
293
327
|
color: Color
|
|
294
328
|
offset: Vector
|
|
295
329
|
radius: number
|
|
@@ -301,10 +335,14 @@ export interface Effect {
|
|
|
301
335
|
glass?: GlassEffectParams
|
|
302
336
|
/** Set on a layer or background blur that is progressive, and only there. */
|
|
303
337
|
progressive?: ProgressiveBlur
|
|
338
|
+
/** Set on a `NOISE` effect, and only there. */
|
|
339
|
+
noise?: NoiseEffectParams
|
|
340
|
+
/** Set on a `TEXTURE` effect, and only there. */
|
|
341
|
+
texture?: TextureEffectParams
|
|
304
342
|
}
|
|
305
343
|
|
|
306
344
|
export type ConstraintType = 'MIN' | 'CENTER' | 'MAX' | 'STRETCH' | 'SCALE'
|
|
307
|
-
export type TextAutoResize = 'NONE' | 'HEIGHT' | 'WIDTH_AND_HEIGHT'
|
|
345
|
+
export type TextAutoResize = 'NONE' | 'HEIGHT' | 'WIDTH_AND_HEIGHT'
|
|
308
346
|
|
|
309
347
|
/** Where a connector end attaches to a layer: a side, the side Figma picks, or a fixed point. */
|
|
310
348
|
export type ConnectorMagnet =
|
|
@@ -649,6 +687,8 @@ export interface CommentReply {
|
|
|
649
687
|
author: string
|
|
650
688
|
/** The author's collaboration color when they wrote it, for their avatar. */
|
|
651
689
|
authorColor?: Color
|
|
690
|
+
/** The agent that wrote it, for `author` or as itself. */
|
|
691
|
+
agent?: string
|
|
652
692
|
/** Markdown: bold, italic, strikethrough, links and lists. */
|
|
653
693
|
text: string
|
|
654
694
|
createdAt: string
|
|
@@ -673,6 +713,8 @@ export interface CommentThread {
|
|
|
673
713
|
author: string
|
|
674
714
|
/** The author's collaboration color when they wrote it, for their avatar. */
|
|
675
715
|
authorColor?: Color
|
|
716
|
+
/** The agent that wrote it, for `author` or as itself. */
|
|
717
|
+
agent?: string
|
|
676
718
|
/** Markdown: bold, italic, strikethrough, links and lists. */
|
|
677
719
|
text: string
|
|
678
720
|
createdAt: string
|
|
@@ -698,7 +740,7 @@ export interface SceneNode {
|
|
|
698
740
|
height: number
|
|
699
741
|
rotation: number
|
|
700
742
|
source: SourceMetadata
|
|
701
|
-
/** Materialized layout hint imported or generated outside the live
|
|
743
|
+
/** Materialized layout hint imported or generated outside the live layout pass. */
|
|
702
744
|
derivedLayout: Partial<Rect> | null
|
|
703
745
|
|
|
704
746
|
fills: Fill[]
|
|
@@ -789,7 +831,8 @@ export interface SceneNode {
|
|
|
789
831
|
primaryAxisSizing: AxisSizingMode
|
|
790
832
|
counterAxisSizing: AxisSizingMode
|
|
791
833
|
itemSpacing: number
|
|
792
|
-
|
|
834
|
+
/** Space between wrapped lines; null follows `itemSpacing`. Read it with `counterAxisSpacingOf`. */
|
|
835
|
+
counterAxisSpacing: number | null
|
|
793
836
|
paddingTop: number
|
|
794
837
|
paddingRight: number
|
|
795
838
|
paddingBottom: number
|
|
@@ -942,9 +985,10 @@ export interface SceneNode {
|
|
|
942
985
|
textPicture: Uint8Array | null
|
|
943
986
|
derivedTextGlyphs: DerivedTextGlyph[] | null
|
|
944
987
|
/**
|
|
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
|
|
988
|
+
* How tall a text's lines are, from the top of the first to the bottom of the last: measured at
|
|
989
|
+
* its width when its text or width changes, or read from the lines a file saved, and never saved
|
|
990
|
+
* itself. A fixed-size box does not clip them, as in Figma, so its visual bounds reach past the
|
|
991
|
+
* box on the side away from its vertical alignment (`textLinesReach`). Null when not known.
|
|
948
992
|
*/
|
|
949
993
|
derivedTextHeight: number | null
|
|
950
994
|
/** Format-neutral layout path for text-on-path nodes. */
|
|
@@ -1128,14 +1172,24 @@ export interface Variable {
|
|
|
1128
1172
|
version?: string
|
|
1129
1173
|
}
|
|
1130
1174
|
|
|
1131
|
-
/** Scalar numeric node fields accepted by numeric property controls. */
|
|
1175
|
+
/** Scalar numeric node fields accepted by numeric property controls; some may also be null. */
|
|
1132
1176
|
export type NumericNodeProperty = {
|
|
1133
|
-
[K in keyof SceneNode]-?: SceneNode[K] extends number
|
|
1177
|
+
[K in keyof SceneNode]-?: SceneNode[K] extends number | null
|
|
1178
|
+
? number extends SceneNode[K]
|
|
1179
|
+
? K
|
|
1180
|
+
: never
|
|
1181
|
+
: never
|
|
1134
1182
|
}[keyof SceneNode]
|
|
1135
1183
|
|
|
1136
1184
|
export interface VariableCollectionMode {
|
|
1137
1185
|
modeId: string
|
|
1138
1186
|
name: string
|
|
1187
|
+
/**
|
|
1188
|
+
* The library mode this one came from, for a mode of a collection copied from a library. A
|
|
1189
|
+
* library that renames its mode renames this one with it, so the layers and the document set
|
|
1190
|
+
* to it keep following the library.
|
|
1191
|
+
*/
|
|
1192
|
+
libraryModeId?: string
|
|
1139
1193
|
/**
|
|
1140
1194
|
* Where the mode applies in CSS: a selector (`[data-theme="dark"]`, `.compact`) or an
|
|
1141
1195
|
* 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,5 @@
|
|
|
1
|
+
import { appendAll } from '@open-pencil/std/arrays'
|
|
2
|
+
|
|
1
3
|
import type { SceneGraph } from '../graph'
|
|
2
4
|
import type { Color } from '../primitives'
|
|
3
5
|
import type { Variable, VariableLiteral, VariableValue } from '../types'
|
|
@@ -17,13 +19,24 @@ export function getNodeVariableModeId(
|
|
|
17
19
|
nodeId: string,
|
|
18
20
|
collectionId: string
|
|
19
21
|
): string {
|
|
22
|
+
return (
|
|
23
|
+
getNodeExplicitVariableModeId(graph, nodeId, collectionId) ?? defaultModeId(graph, collectionId)
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The nearest mode set on a node or an ancestor for a collection, or null where none is. */
|
|
28
|
+
export function getNodeExplicitVariableModeId(
|
|
29
|
+
graph: SceneGraph,
|
|
30
|
+
nodeId: string,
|
|
31
|
+
collectionId: string
|
|
32
|
+
): string | null {
|
|
20
33
|
let node = graph.nodes.get(nodeId)
|
|
21
34
|
while (node) {
|
|
22
35
|
const modeId = node.variableModes[collectionId]
|
|
23
36
|
if (modeId) return modeId
|
|
24
37
|
node = node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
25
38
|
}
|
|
26
|
-
return
|
|
39
|
+
return null
|
|
27
40
|
}
|
|
28
41
|
|
|
29
42
|
/**
|
|
@@ -44,7 +57,7 @@ export function collectionsResolving(
|
|
|
44
57
|
if (!variable) continue
|
|
45
58
|
collections.add(variable.collectionId)
|
|
46
59
|
for (const value of Object.values(variable.valuesByMode))
|
|
47
|
-
pending
|
|
60
|
+
appendAll(pending, variableValueReferences(value))
|
|
48
61
|
}
|
|
49
62
|
return collections
|
|
50
63
|
}
|
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
|
}
|