@open-pencil/scene-graph 0.16.0-beta.2 → 0.16.0
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/chunks/asset-keys.js +27 -0
- package/dist/chunks/asset-keys.js.map +1 -0
- package/dist/chunks/behaviours/layers.js +5 -1
- package/dist/chunks/behaviours/layers.js.map +1 -1
- package/dist/chunks/color/contrast.js +23 -1
- package/dist/chunks/color/contrast.js.map +1 -1
- package/dist/chunks/color/index.js +39 -3
- package/dist/chunks/color/index.js.map +1 -1
- package/dist/chunks/components/exposure.js +7 -7
- package/dist/chunks/components/exposure.js.map +1 -1
- package/dist/chunks/index.js +93 -7
- package/dist/chunks/index.js.map +1 -1
- package/dist/color.d.ts +7 -1
- package/dist/color.d.ts.map +1 -1
- package/dist/color.js +2 -2
- package/dist/geometry.d.ts +2 -2
- package/dist/images.d.ts +2 -2
- package/dist/images.js +21 -38
- package/dist/images.js.map +1 -1
- package/dist/images2.d.ts +12 -1
- package/dist/images2.d.ts.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +5 -4
- package/dist/types.d.ts +67 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -2
- package/dist/types2.d.ts +2 -2
- package/package.json +2 -1
- package/src/asset-keys.ts +25 -0
- package/src/color/contrast.ts +28 -0
- package/src/geometry.ts +64 -14
- package/src/images.ts +24 -32
- package/src/index.ts +2 -0
- package/src/instances/content-size.ts +13 -8
- package/src/instances/fields.ts +1 -1
- package/src/shaders/model.ts +4 -2
- package/src/shaders/still.ts +41 -0
- package/src/types.ts +14 -0
- package/src/variables/bindings.ts +71 -7
|
@@ -2,7 +2,7 @@ import { copyFills, copyStrokes } from '../copy'
|
|
|
2
2
|
import type { SceneGraph } from '../index'
|
|
3
3
|
import { hasInstanceOverride } from '../instances'
|
|
4
4
|
import type { Color } from '../primitives'
|
|
5
|
-
import type { Fill, SceneNode, Stroke, StyleRun } from '../types'
|
|
5
|
+
import type { Fill, SceneNode, Stroke, StyleRun, VariableValue } from '../types'
|
|
6
6
|
import type { VariableModeFallback } from '../variables'
|
|
7
7
|
import { NUMERIC_FIELDS, isNumericVariableBindingField } from './fields'
|
|
8
8
|
export { isNumericVariableBindingField } from './fields'
|
|
@@ -57,6 +57,41 @@ export function resolvedNumericBindings(
|
|
|
57
57
|
return updates
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* The text a variable bound to text content shows: a string as it is, and a number or boolean
|
|
62
|
+
* as Figma writes it, since Figma lets either be text content.
|
|
63
|
+
*/
|
|
64
|
+
export function variableText(value: VariableValue | undefined): string | undefined {
|
|
65
|
+
if (typeof value === 'string') return value
|
|
66
|
+
if (typeof value === 'boolean') return String(value)
|
|
67
|
+
if (typeof value === 'number' && Number.isFinite(value)) return numberText(value)
|
|
68
|
+
return undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const INT32_MIN = -2_147_483_648
|
|
72
|
+
const INT32_MAX = 2_147_483_647
|
|
73
|
+
|
|
74
|
+
/** How near a whole number a value must be for Figma to write it as one. */
|
|
75
|
+
const WHOLE_NUMBER_TOLERANCE = 1e-6
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A number as Figma desktop 126 writes it as text: one within a millionth of a whole number as
|
|
79
|
+
* that integer, clamped to 32 bits, and anything else with two decimals, its sign kept and an
|
|
80
|
+
* exact tie rounded to even, as C's `%.2f` does (0.125 is "0.12", 0.999 is "1.00", -0.004 is
|
|
81
|
+
* "-0.00").
|
|
82
|
+
*/
|
|
83
|
+
function numberText(value: number): string {
|
|
84
|
+
const whole = Math.round(value)
|
|
85
|
+
if (Math.abs(value - whole) < WHOLE_NUMBER_TOLERANCE)
|
|
86
|
+
return String(Math.min(INT32_MAX, Math.max(INT32_MIN, whole)) || 0)
|
|
87
|
+
const scaled = Math.abs(value) * 100
|
|
88
|
+
const floor = Math.floor(scaled)
|
|
89
|
+
const tie = scaled - floor === 0.5
|
|
90
|
+
const cents = tie ? floor + (floor % 2) : Math.round(scaled)
|
|
91
|
+
const sign = value < 0 ? '-' : ''
|
|
92
|
+
return `${sign}${Math.floor(cents / 100)}.${String(cents % 100).padStart(2, '0')}`
|
|
93
|
+
}
|
|
94
|
+
|
|
60
95
|
/**
|
|
61
96
|
* The text and visibility a node's string and boolean bindings resolve to in its mode, for the
|
|
62
97
|
* fields whose stored value differs. Text or visibility an instance overrides, which typing in a
|
|
@@ -75,8 +110,8 @@ export function resolvedValueBindings(
|
|
|
75
110
|
if (!variableId || hasInstanceOverride(graph, node.id, field)) return undefined
|
|
76
111
|
return graph.resolveVariableForNode(node.id, variableId, fallback)
|
|
77
112
|
}
|
|
78
|
-
const text = node.type === 'TEXT' ? resolve('text') : undefined
|
|
79
|
-
if (
|
|
113
|
+
const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined
|
|
114
|
+
if (text !== undefined && text !== node.text) updates.text = text
|
|
80
115
|
const visible = resolve('visible')
|
|
81
116
|
if (typeof visible === 'boolean' && visible !== node.visible) updates.visible = visible
|
|
82
117
|
return updates
|
|
@@ -194,15 +229,44 @@ function subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneNode[] {
|
|
|
194
229
|
return nodes
|
|
195
230
|
}
|
|
196
231
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
232
|
+
/**
|
|
233
|
+
* The collections whose variables the layers in these subtrees resolve: those they bind, and
|
|
234
|
+
* those the bound variables alias, so a mode set on the subtrees can change what they show.
|
|
235
|
+
*/
|
|
236
|
+
export function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>): Set<string> {
|
|
237
|
+
const pending = subtreeNodes(graph, roots).flatMap(boundVariableIds)
|
|
238
|
+
const seen = new Set<string>()
|
|
239
|
+
const collections = new Set<string>()
|
|
240
|
+
for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
|
|
241
|
+
if (seen.has(id)) continue
|
|
242
|
+
seen.add(id)
|
|
243
|
+
const variable = graph.variables.get(id)
|
|
244
|
+
if (!variable) continue
|
|
245
|
+
collections.add(variable.collectionId)
|
|
246
|
+
for (const value of Object.values(variable.valuesByMode)) {
|
|
247
|
+
if (typeof value === 'object' && 'aliasId' in value) pending.push(value.aliasId)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return collections
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The layers a change to these variables reaches: those binding one of them, or a variable that
|
|
255
|
+
* aliases one, on the layer itself or on its characters.
|
|
256
|
+
*/
|
|
257
|
+
export function layersUsingVariables(graph: SceneGraph, ids: Iterable<string>): SceneNode[] {
|
|
258
|
+
const variables = variablesResolvingThrough(graph, ids)
|
|
201
259
|
return [...graph.getAllNodes()].filter((node) =>
|
|
202
260
|
boundVariableIds(node).some((id) => variables.has(id))
|
|
203
261
|
)
|
|
204
262
|
}
|
|
205
263
|
|
|
264
|
+
function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterable<SceneNode> {
|
|
265
|
+
if (!scope) return graph.getAllNodes()
|
|
266
|
+
if ('subtrees' in scope) return subtreeNodes(graph, scope.subtrees)
|
|
267
|
+
return layersUsingVariables(graph, scope.variables)
|
|
268
|
+
}
|
|
269
|
+
|
|
206
270
|
/**
|
|
207
271
|
* Resolve live numeric, text, and visibility bindings, numbers in scene units, without authoring
|
|
208
272
|
* instance overrides. A scope limits it to the layers a change can reach, so layers whose saved
|