@pairdraw/scene-graph 0.16.2 → 0.18.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/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +8 -3
- package/dist/checkpoint.js.map +1 -1
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +3 -3
- package/dist/color/index.js.map +1 -1
- package/dist/color/management.d.ts.map +1 -1
- package/dist/color/management.js +4 -2
- package/dist/color/management.js.map +1 -1
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +3 -1
- package/dist/coordinate.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +7 -3
- package/dist/copy.js.map +1 -1
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +2 -2
- package/dist/css/values.js.map +1 -1
- package/dist/effects.d.ts +16 -0
- package/dist/effects.d.ts.map +1 -0
- package/dist/effects.js +78 -0
- package/dist/effects.js.map +1 -0
- package/dist/geometry.d.ts +14 -7
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +130 -26
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +12 -3
- package/dist/graph.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +100 -8
- package/dist/hit-test.js.map +1 -1
- package/dist/instances/content-size.js +1 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +4 -1
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/sync.js +4 -3
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/index.js +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +4 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +5 -0
- package/dist/preview.js.map +1 -1
- package/dist/resize.d.ts +4 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +18 -12
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +3 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/scaling/tree.d.ts.map +1 -1
- package/dist/scaling/tree.js +8 -5
- package/dist/scaling/tree.js.map +1 -1
- package/dist/slots/authoring.js +3 -6
- package/dist/slots/authoring.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +15 -25
- package/dist/slots/content.js.map +1 -1
- package/dist/slots/frames.js +1 -2
- package/dist/slots/frames.js.map +1 -1
- package/dist/snap.d.ts.map +1 -1
- package/dist/snap.js +12 -4
- package/dist/snap.js.map +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +2 -1
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +3 -2
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +2 -1
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +6 -2
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/index.d.ts +2 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +37 -7
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/units.d.ts +31 -0
- package/dist/variables/units.d.ts.map +1 -0
- package/dist/variables/units.js +79 -4
- package/dist/variables/units.js.map +1 -1
- package/package.json +6 -1
- package/src/checkpoint.ts +8 -2
- package/src/color/index.ts +4 -6
- package/src/color/management.ts +8 -2
- package/src/coordinate.ts +4 -1
- package/src/copy.ts +8 -3
- package/src/css/values.ts +3 -2
- package/src/effects.ts +76 -0
- package/src/geometry.ts +147 -24
- package/src/graph.ts +13 -3
- package/src/hit-test.ts +138 -16
- package/src/instances/index.ts +5 -1
- package/src/instances/sync.ts +5 -3
- package/src/node-defaults.ts +4 -1
- package/src/preview.ts +4 -0
- package/src/resize.ts +40 -20
- package/src/scaling/node.ts +3 -1
- package/src/scaling/tree.ts +3 -2
- package/src/slots/authoring.ts +7 -7
- package/src/slots/content.ts +13 -19
- package/src/slots/frames.ts +3 -3
- package/src/snap.ts +14 -4
- package/src/types.ts +5 -0
- package/src/undo.ts +3 -2
- package/src/variables/bindings.ts +6 -2
- package/src/variables/index.ts +49 -8
- package/src/variables/units.ts +97 -4
package/src/slots/authoring.ts
CHANGED
|
@@ -15,13 +15,13 @@ export const DEFAULT_SLOT_SETTINGS: SlotSettings = {
|
|
|
15
15
|
* instance are not authored there, so a layer under an instance has none.
|
|
16
16
|
*/
|
|
17
17
|
export function slotOwner(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return undefined
|
|
18
|
+
if (!node.parentId) return undefined
|
|
19
|
+
const owner = graph.closest(
|
|
20
|
+
node.parentId,
|
|
21
|
+
(current) =>
|
|
22
|
+
current.type === 'COMPONENT' || current.type === 'INSTANCE' || current.type === 'CANVAS'
|
|
23
|
+
)
|
|
24
|
+
return owner?.type === 'COMPONENT' ? owner : undefined
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
/** Whether a frame inside a main component can become a slot. */
|
package/src/slots/content.ts
CHANGED
|
@@ -17,29 +17,23 @@ export type SlotScope =
|
|
|
17
17
|
| { kind: 'slot'; frame: SceneNode; instance: SceneNode; propertyId: string }
|
|
18
18
|
| { kind: 'locked'; owner: SceneNode }
|
|
19
19
|
|
|
20
|
-
function parentOf(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
|
|
21
|
-
return node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
22
|
-
}
|
|
23
|
-
|
|
24
20
|
function nearestInstance(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
return node.parentId
|
|
22
|
+
? graph.closest(node.parentId, (current) => current.type === 'INSTANCE')
|
|
23
|
+
: undefined
|
|
28
24
|
}
|
|
29
25
|
|
|
30
26
|
export function slotScope(graph: SceneGraph, parentId: string): SlotScope {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
return { kind: 'free' }
|
|
27
|
+
const owner = graph.closest(
|
|
28
|
+
parentId,
|
|
29
|
+
(current) =>
|
|
30
|
+
!!slotPropertyId(current) || current.type === 'INSTANCE' || isGeneratedFrame(current)
|
|
31
|
+
)
|
|
32
|
+
if (!owner) return { kind: 'free' }
|
|
33
|
+
const propertyId = slotPropertyId(owner)
|
|
34
|
+
if (!propertyId) return { kind: 'locked', owner }
|
|
35
|
+
const instance = nearestInstance(graph, owner)
|
|
36
|
+
return instance ? { kind: 'slot', frame: owner, instance, propertyId } : { kind: 'free' }
|
|
43
37
|
}
|
|
44
38
|
|
|
45
39
|
/** Every layer below `node`; `enterInstances` also walks the layers of nested instances. */
|
package/src/slots/frames.ts
CHANGED
|
@@ -18,9 +18,9 @@ export function ownsSlotContent(
|
|
|
18
18
|
propertyId = slotPropertyId(node)
|
|
19
19
|
): boolean {
|
|
20
20
|
if (!propertyId) return false
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const owner = node.parentId
|
|
22
|
+
? graph.closest(node.parentId, (current) => current.type === 'INSTANCE')
|
|
23
|
+
: undefined
|
|
24
24
|
return !!owner && Object.hasOwn(owner.componentPropertyAssignments, propertyId)
|
|
25
25
|
}
|
|
26
26
|
|
package/src/snap.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getNodeWorldBounds } from './coordinate'
|
|
2
2
|
import type { Rect } from './primitives'
|
|
3
3
|
import type { SceneNode } from './types'
|
|
4
4
|
|
|
@@ -25,8 +25,17 @@ export interface SnapResult {
|
|
|
25
25
|
guides: SnapGuide[]
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
// A line turns around its start point and other layers around their centre, as they are drawn.
|
|
28
29
|
function getEdges(node: SceneNode) {
|
|
29
|
-
|
|
30
|
+
const box = getNodeWorldBounds(node)
|
|
31
|
+
return {
|
|
32
|
+
left: box.x,
|
|
33
|
+
right: box.x + box.width,
|
|
34
|
+
top: box.y,
|
|
35
|
+
bottom: box.y + box.height,
|
|
36
|
+
centerX: box.x + box.width / 2,
|
|
37
|
+
centerY: box.y + box.height / 2
|
|
38
|
+
}
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
// Pairwise edge/center matching intentionally keeps both axes in one pass.
|
|
@@ -78,7 +87,8 @@ export function computeSnap(
|
|
|
78
87
|
if (guides[i].axis === 'x') guides.splice(i, 1)
|
|
79
88
|
}
|
|
80
89
|
}
|
|
81
|
-
|
|
90
|
+
// A tie in the other direction is not applied, so it draws no guide.
|
|
91
|
+
if (d === bestDx) {
|
|
82
92
|
const minY = Math.min(m.top, t.top)
|
|
83
93
|
const maxY = Math.max(m.bottom, t.bottom)
|
|
84
94
|
guides.push({ axis: 'x', position: tVal, from: minY, to: maxY })
|
|
@@ -104,7 +114,7 @@ export function computeSnap(
|
|
|
104
114
|
}
|
|
105
115
|
bestDy = d
|
|
106
116
|
}
|
|
107
|
-
if (
|
|
117
|
+
if (d === bestDy) {
|
|
108
118
|
const minX = Math.min(m.left, t.left)
|
|
109
119
|
const maxX = Math.max(m.right, t.right)
|
|
110
120
|
guides.push({ axis: 'y', position: tVal, from: minX, to: maxX })
|
package/src/types.ts
CHANGED
|
@@ -429,6 +429,11 @@ export interface CharacterStyleOverride {
|
|
|
429
429
|
fillStyleId?: string | null
|
|
430
430
|
/** The characters' text field bindings, in place of the text's own. */
|
|
431
431
|
boundVariables?: TextBoundVariables
|
|
432
|
+
/**
|
|
433
|
+
* The scale each number the characters bind resolves with, as a rescale left it; a field without
|
|
434
|
+
* one uses the layer's `variableBindingScales`, as runs did before they kept their own.
|
|
435
|
+
*/
|
|
436
|
+
variableBindingScales?: Partial<Record<TextBindableField, number>>
|
|
432
437
|
}
|
|
433
438
|
|
|
434
439
|
export interface StyleRun {
|
package/src/undo.ts
CHANGED
|
@@ -151,7 +151,8 @@ export class UndoManager {
|
|
|
151
151
|
|
|
152
152
|
/**
|
|
153
153
|
* Adds a change that the newest step caused, as one an editor makes in response to it, so undo
|
|
154
|
-
* and redo apply them together. Inside a batch it joins the batch. False with no step to join
|
|
154
|
+
* and redo apply them together. Inside a batch it joins the batch. False with no step to join,
|
|
155
|
+
* including after an undo, when the newest step is on the redo stack and not applied.
|
|
155
156
|
*/
|
|
156
157
|
extendLastStep(entry: UndoEntry): boolean {
|
|
157
158
|
if (this.currentBatch) {
|
|
@@ -159,7 +160,7 @@ export class UndoManager {
|
|
|
159
160
|
return true
|
|
160
161
|
}
|
|
161
162
|
const last = this.undoStack.at(-1)
|
|
162
|
-
if (!last) return false
|
|
163
|
+
if (!last || this.redoStack.length > 0) return false
|
|
163
164
|
// The step keeps its identity, so callers that hold it still find it in the history.
|
|
164
165
|
const { forward, inverse } = last
|
|
165
166
|
last.forward = () => {
|
|
@@ -11,6 +11,7 @@ import { pixelLength } from '../text/lengths'
|
|
|
11
11
|
import type { Fill, SceneNode, Stroke, StyleRun, TextLength } from '../types'
|
|
12
12
|
import { NUMERIC_FIELDS, isNumericVariableBindingField, paintBindingTarget } from './fields'
|
|
13
13
|
import { variableFontFamily, variableText, variableTruthy } from './text'
|
|
14
|
+
import { runBindingScale } from './units'
|
|
14
15
|
import { variableValueReferences } from './value'
|
|
15
16
|
export {
|
|
16
17
|
ICON_BINDING_FIELDS,
|
|
@@ -137,7 +138,8 @@ const NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeig
|
|
|
137
138
|
|
|
138
139
|
/**
|
|
139
140
|
* The runs a text's character bindings resolve to in its mode, when a bound number or font family
|
|
140
|
-
* differs from what its characters hold
|
|
141
|
+
* differs from what its characters hold, a size or spacing in the run's binding scale for the
|
|
142
|
+
* field, as a rescale left it (`runBindingScale`). Bound font styles are left as stored.
|
|
141
143
|
*/
|
|
142
144
|
export function resolvedRunBindings(
|
|
143
145
|
graph: SceneGraph,
|
|
@@ -153,7 +155,9 @@ export function resolvedRunBindings(
|
|
|
153
155
|
if (!variableId) continue
|
|
154
156
|
const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
|
|
155
157
|
if (value === undefined || !Number.isFinite(value)) continue
|
|
156
|
-
const
|
|
158
|
+
const effective = value * runBindingScale(node, style, field)
|
|
159
|
+
if (!Number.isFinite(effective)) continue
|
|
160
|
+
const next = boundNumberValue(field, effective)
|
|
157
161
|
if (isEqual(style[field], next)) continue
|
|
158
162
|
style = { ...style, [field]: next }
|
|
159
163
|
}
|
package/src/variables/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { findInstanceAncestor } from '../instances'
|
|
|
7
7
|
import { bindingOverrideField, setInstanceOverride } from '../instances/overrides'
|
|
8
8
|
import { isShaderSourcePaint, shaderOfPaint, withShaderPaints } from '../shaders/model'
|
|
9
9
|
import type {
|
|
10
|
+
SceneNode,
|
|
10
11
|
Variable,
|
|
11
12
|
VariableCollection,
|
|
12
13
|
VariableCollectionMode,
|
|
@@ -21,15 +22,32 @@ import {
|
|
|
21
22
|
import {
|
|
22
23
|
BOOLEAN_BINDING_FIELDS,
|
|
23
24
|
ICON_BINDING_FIELDS,
|
|
25
|
+
NUMERIC_FIELDS,
|
|
24
26
|
paintBindingTarget,
|
|
25
27
|
STRING_BINDING_FIELDS,
|
|
26
28
|
TEXT_CONTENT_VARIABLE_TYPES,
|
|
27
29
|
uniformBindingTarget,
|
|
28
|
-
VISIBILITY_VARIABLE_TYPES
|
|
30
|
+
VISIBILITY_VARIABLE_TYPES,
|
|
31
|
+
type PaintBindingList
|
|
29
32
|
} from './fields'
|
|
33
|
+
import { pinnedRunBindingScales } from './units'
|
|
30
34
|
|
|
31
35
|
export * from './resolution'
|
|
32
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Gives the runs that bind `fields` the scale they resolve with as their own, before the layer's
|
|
39
|
+
* scale for those fields changes or goes, and the change to announce.
|
|
40
|
+
*/
|
|
41
|
+
function pinRunBindingScales(
|
|
42
|
+
node: SceneNode,
|
|
43
|
+
fields: readonly string[]
|
|
44
|
+
): Pick<Partial<SceneNode>, 'styleRuns'> {
|
|
45
|
+
const styleRuns = pinnedRunBindingScales(node, fields)
|
|
46
|
+
if (!styleRuns) return {}
|
|
47
|
+
node.styleRuns = styleRuns
|
|
48
|
+
return { styleRuns }
|
|
49
|
+
}
|
|
50
|
+
|
|
33
51
|
export function addVariable(graph: SceneGraph, variable: Variable): void {
|
|
34
52
|
graph.variables.set(variable.id, variable)
|
|
35
53
|
const collection = graph.variableCollections.get(variable.collectionId)
|
|
@@ -54,13 +72,15 @@ export function removeVariable(graph: SceneGraph, id: string): void {
|
|
|
54
72
|
string,
|
|
55
73
|
string
|
|
56
74
|
>
|
|
57
|
-
|
|
58
|
-
node.
|
|
59
|
-
(_, field) => !(field in node.boundVariables)
|
|
75
|
+
const pruned = Object.keys(node.variableBindingScales).filter(
|
|
76
|
+
(field) => !(field in node.boundVariables)
|
|
60
77
|
)
|
|
78
|
+
const runs = pinRunBindingScales(node, pruned)
|
|
79
|
+
node.variableBindingScales = omit(node.variableBindingScales, pruned)
|
|
61
80
|
graph.emitter.emit('node:updated', node.id, {
|
|
62
81
|
boundVariables: { ...node.boundVariables },
|
|
63
|
-
variableBindingScales: { ...node.variableBindingScales }
|
|
82
|
+
variableBindingScales: { ...node.variableBindingScales },
|
|
83
|
+
...runs
|
|
64
84
|
})
|
|
65
85
|
markBoundVariablesOverrideOnInstance(graph, node.id)
|
|
66
86
|
}
|
|
@@ -231,6 +251,20 @@ export function getVariablesByType(graph: SceneGraph, type: VariableType): Varia
|
|
|
231
251
|
return [...graph.variables.values()].filter((v) => v.type === type)
|
|
232
252
|
}
|
|
233
253
|
|
|
254
|
+
const PAINT_BINDING_LISTS = ['fills', 'strokes'] as const satisfies readonly PaintBindingList[]
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Every field a binding takes, from the same sets `checkBindingFieldType` reads, so a refusal for
|
|
258
|
+
* an unknown one names what the caller can send instead of leaving them to guess.
|
|
259
|
+
*/
|
|
260
|
+
const ACCEPTED_BINDING_FIELDS: readonly string[] = [
|
|
261
|
+
...NUMERIC_FIELDS,
|
|
262
|
+
...STRING_BINDING_FIELDS,
|
|
263
|
+
...BOOLEAN_BINDING_FIELDS,
|
|
264
|
+
...ICON_BINDING_FIELDS,
|
|
265
|
+
...PAINT_BINDING_LISTS.flatMap((list) => [`${list}/<n>/color`, `${list}/<n>/uniforms/<name>`])
|
|
266
|
+
]
|
|
267
|
+
|
|
234
268
|
/**
|
|
235
269
|
* Throws when a variable of `type` cannot bind to `field`, or for a field no binding takes. A
|
|
236
270
|
* paint target is checked by its caller, which also validates its index.
|
|
@@ -272,7 +306,9 @@ function checkBindingFieldType(field: string, type: VariableType): void {
|
|
|
272
306
|
uniform !== null
|
|
273
307
|
|
|
274
308
|
if (!isKnownField) {
|
|
275
|
-
throw new Error(
|
|
309
|
+
throw new Error(
|
|
310
|
+
`Unknown binding field "${field}". Accepted: ${ACCEPTED_BINDING_FIELDS.join(', ')}`
|
|
311
|
+
)
|
|
276
312
|
}
|
|
277
313
|
}
|
|
278
314
|
export function bindVariable(
|
|
@@ -345,11 +381,13 @@ export function bindVariable(
|
|
|
345
381
|
}
|
|
346
382
|
graph.willChangeNode(node)
|
|
347
383
|
node.boundVariables = { ...node.boundVariables, [field]: variableId }
|
|
384
|
+
const runs = pinRunBindingScales(node, [field])
|
|
348
385
|
assignVariableBindingUnits(graph, node, field)
|
|
349
386
|
markBoundVariablesOverrideOnInstance(graph, nodeId, field)
|
|
350
387
|
graph.emitter.emit('node:updated', nodeId, {
|
|
351
388
|
boundVariables: { ...node.boundVariables },
|
|
352
|
-
variableBindingScales: { ...node.variableBindingScales }
|
|
389
|
+
variableBindingScales: { ...node.variableBindingScales },
|
|
390
|
+
...runs
|
|
353
391
|
})
|
|
354
392
|
}
|
|
355
393
|
|
|
@@ -359,11 +397,13 @@ export function unbindVariable(graph: SceneGraph, nodeId: string, field: string)
|
|
|
359
397
|
if (!(field in node.boundVariables)) return
|
|
360
398
|
graph.willChangeNode(node)
|
|
361
399
|
node.boundVariables = omit(node.boundVariables, [field])
|
|
400
|
+
const runs = pinRunBindingScales(node, [field])
|
|
362
401
|
node.variableBindingScales = omit(node.variableBindingScales, [field])
|
|
363
402
|
markBoundVariablesOverrideOnInstance(graph, nodeId, field)
|
|
364
403
|
graph.emitter.emit('node:updated', nodeId, {
|
|
365
404
|
boundVariables: { ...node.boundVariables },
|
|
366
|
-
variableBindingScales: { ...node.variableBindingScales }
|
|
405
|
+
variableBindingScales: { ...node.variableBindingScales },
|
|
406
|
+
...runs
|
|
367
407
|
})
|
|
368
408
|
}
|
|
369
409
|
|
|
@@ -397,4 +437,5 @@ export * from './formula/schema'
|
|
|
397
437
|
export * from './schema'
|
|
398
438
|
export * from './text'
|
|
399
439
|
export * from './token'
|
|
440
|
+
export * from './units'
|
|
400
441
|
export * from './value'
|
package/src/variables/units.ts
CHANGED
|
@@ -1,15 +1,108 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { omit, pickBy } from 'es-toolkit/object'
|
|
2
|
+
import { isEmptyObject } from 'es-toolkit/predicate'
|
|
3
|
+
|
|
4
|
+
import type { CharacterStyleOverride, SceneNode, StyleRun, TextBindableField } from '../types'
|
|
2
5
|
import { NUMERIC_FIELDS } from './fields'
|
|
3
6
|
|
|
4
|
-
|
|
7
|
+
const SCALED_RUN_BINDING_FIELD_LIST = ['fontSize', 'letterSpacing', 'lineHeight'] as const
|
|
8
|
+
|
|
9
|
+
/** A size or spacing a text's characters bind that a rescale scales with the layer. */
|
|
10
|
+
export type ScaledRunBindingField = (typeof SCALED_RUN_BINDING_FIELD_LIST)[number]
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Numbers a text's characters bind that a rescale scales with the layer. Each run keeps its own
|
|
14
|
+
* scale for the field, because the rescale scales the layer and its characters alike.
|
|
15
|
+
*/
|
|
16
|
+
export const SCALED_RUN_BINDING_FIELDS: ReadonlySet<string> = new Set(SCALED_RUN_BINDING_FIELD_LIST)
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The scale a run's binding of `field` resolves with: the run's own, else, for a run saved before
|
|
20
|
+
* runs kept one, the layer's scale for a size or spacing, as every run once shared it.
|
|
21
|
+
*/
|
|
22
|
+
export function runBindingScale(
|
|
23
|
+
node: Pick<SceneNode, 'variableBindingScales'>,
|
|
24
|
+
style: CharacterStyleOverride,
|
|
25
|
+
field: TextBindableField
|
|
26
|
+
): number {
|
|
27
|
+
const own = style.variableBindingScales?.[field]
|
|
28
|
+
if (own !== undefined) return own
|
|
29
|
+
return SCALED_RUN_BINDING_FIELDS.has(field) ? (node.variableBindingScales[field] ?? 1) : 1
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A run's style keeping scales only for the numbers it still binds, and no empty scales. */
|
|
33
|
+
export function withBoundBindingScales(style: CharacterStyleOverride): CharacterStyleOverride {
|
|
34
|
+
if (!Object.hasOwn(style, 'variableBindingScales')) return style
|
|
35
|
+
const scales = pickBy(
|
|
36
|
+
style.variableBindingScales ?? {},
|
|
37
|
+
(_, field) => style.boundVariables?.[field] !== undefined
|
|
38
|
+
)
|
|
39
|
+
if (isEmptyObject(scales)) return omit(style, ['variableBindingScales'])
|
|
40
|
+
return { ...style, variableBindingScales: scales }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `runs` with each binding of `fields` holding `next` of the scale it resolves with. */
|
|
44
|
+
function runsWithBindingScales(
|
|
45
|
+
node: SceneNode,
|
|
46
|
+
runs: readonly StyleRun[],
|
|
47
|
+
fields: readonly ScaledRunBindingField[],
|
|
48
|
+
next: (scale: number) => number
|
|
49
|
+
): StyleRun[] | undefined {
|
|
50
|
+
const styleRuns = runs.map((run) => {
|
|
51
|
+
const bound = run.style.boundVariables
|
|
52
|
+
if (!bound) return run
|
|
53
|
+
let scales = run.style.variableBindingScales
|
|
54
|
+
for (const field of fields) {
|
|
55
|
+
if (!bound[field]) continue
|
|
56
|
+
const scale = next(runBindingScale(node, run.style, field))
|
|
57
|
+
if (scales?.[field] !== scale) scales = { ...scales, [field]: scale }
|
|
58
|
+
}
|
|
59
|
+
if (scales === run.style.variableBindingScales) return run
|
|
60
|
+
return { ...run, style: { ...run.style, variableBindingScales: scales } }
|
|
61
|
+
})
|
|
62
|
+
return styleRuns.some((run, index) => run !== runs[index]) ? styleRuns : undefined
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The runs with every size or spacing they bind holding the scale it resolves with as their own,
|
|
67
|
+
* or `undefined` when each already does. Called before the layer's scale for a field changes or
|
|
68
|
+
* goes, it migrates a run that still resolves with the layer's, so the run keeps its look.
|
|
69
|
+
*/
|
|
70
|
+
export function pinnedRunBindingScales(
|
|
71
|
+
node: SceneNode,
|
|
72
|
+
fields: readonly string[] = SCALED_RUN_BINDING_FIELD_LIST
|
|
73
|
+
): StyleRun[] | undefined {
|
|
74
|
+
const scaled = SCALED_RUN_BINDING_FIELD_LIST.filter((field) => fields.includes(field))
|
|
75
|
+
if (scaled.length === 0) return undefined
|
|
76
|
+
return runsWithBindingScales(node, node.styleRuns, scaled, (scale) => scale)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The binding scales of a layer rescaled by `factor`. `styleRuns` are the runs the rescale leaves,
|
|
81
|
+
* when it also scales their values, so each keeps them and gains its own scales.
|
|
82
|
+
*/
|
|
83
|
+
export function scaleVariableBindingUnits(
|
|
84
|
+
node: SceneNode,
|
|
85
|
+
factor: number,
|
|
86
|
+
styleRuns: readonly StyleRun[] = node.styleRuns
|
|
87
|
+
): Partial<SceneNode> {
|
|
5
88
|
const variableBindingScales = { ...node.variableBindingScales }
|
|
6
89
|
const variableAssignmentScales = { ...node.variableAssignmentScales }
|
|
7
90
|
for (const field of NUMERIC_FIELDS) {
|
|
8
|
-
|
|
91
|
+
// A rescale keeps opacity, rotation, and font weight as they are.
|
|
92
|
+
if (field === 'opacity' || field === 'rotation' || field === 'fontWeight') continue
|
|
9
93
|
if (field in node.boundVariables)
|
|
10
94
|
variableBindingScales[field] = (variableBindingScales[field] ?? 1) * factor
|
|
11
95
|
if (node.type === 'INSTANCE')
|
|
12
96
|
variableAssignmentScales[field] = (variableAssignmentScales[field] ?? 1) * factor
|
|
13
97
|
}
|
|
14
|
-
|
|
98
|
+
// Each run scales the sizes and spacings it binds in its own scale.
|
|
99
|
+
const scaledRuns = runsWithBindingScales(
|
|
100
|
+
node,
|
|
101
|
+
styleRuns,
|
|
102
|
+
SCALED_RUN_BINDING_FIELD_LIST,
|
|
103
|
+
(scale) => scale * factor
|
|
104
|
+
)
|
|
105
|
+
return scaledRuns
|
|
106
|
+
? { variableBindingScales, variableAssignmentScales, styleRuns: scaledRuns }
|
|
107
|
+
: { variableBindingScales, variableAssignmentScales }
|
|
15
108
|
}
|