@pairdraw/scene-graph 0.16.2 → 0.17.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.
Files changed (98) hide show
  1. package/dist/checkpoint.d.ts.map +1 -1
  2. package/dist/checkpoint.js +8 -3
  3. package/dist/checkpoint.js.map +1 -1
  4. package/dist/color/index.d.ts.map +1 -1
  5. package/dist/color/index.js +3 -3
  6. package/dist/color/index.js.map +1 -1
  7. package/dist/color/management.d.ts.map +1 -1
  8. package/dist/color/management.js +4 -2
  9. package/dist/color/management.js.map +1 -1
  10. package/dist/coordinate.d.ts.map +1 -1
  11. package/dist/coordinate.js +3 -1
  12. package/dist/coordinate.js.map +1 -1
  13. package/dist/copy.d.ts.map +1 -1
  14. package/dist/copy.js +1 -0
  15. package/dist/copy.js.map +1 -1
  16. package/dist/css/values.d.ts.map +1 -1
  17. package/dist/css/values.js +2 -2
  18. package/dist/css/values.js.map +1 -1
  19. package/dist/geometry.d.ts +14 -7
  20. package/dist/geometry.d.ts.map +1 -1
  21. package/dist/geometry.js +130 -26
  22. package/dist/geometry.js.map +1 -1
  23. package/dist/graph.d.ts.map +1 -1
  24. package/dist/graph.js +7 -3
  25. package/dist/graph.js.map +1 -1
  26. package/dist/hit-test.d.ts.map +1 -1
  27. package/dist/hit-test.js +100 -8
  28. package/dist/hit-test.js.map +1 -1
  29. package/dist/instances/content-size.js +1 -1
  30. package/dist/instances/index.d.ts.map +1 -1
  31. package/dist/instances/index.js +4 -1
  32. package/dist/instances/index.js.map +1 -1
  33. package/dist/instances/sync.js +4 -3
  34. package/dist/instances/sync.js.map +1 -1
  35. package/dist/layout/index.js +1 -1
  36. package/dist/resize.d.ts +4 -1
  37. package/dist/resize.d.ts.map +1 -1
  38. package/dist/resize.js +18 -12
  39. package/dist/resize.js.map +1 -1
  40. package/dist/scaling/node.d.ts.map +1 -1
  41. package/dist/scaling/node.js +3 -1
  42. package/dist/scaling/node.js.map +1 -1
  43. package/dist/scaling/tree.d.ts.map +1 -1
  44. package/dist/scaling/tree.js +8 -5
  45. package/dist/scaling/tree.js.map +1 -1
  46. package/dist/slots/authoring.js +3 -6
  47. package/dist/slots/authoring.js.map +1 -1
  48. package/dist/slots/content.d.ts.map +1 -1
  49. package/dist/slots/content.js +15 -25
  50. package/dist/slots/content.js.map +1 -1
  51. package/dist/slots/frames.js +1 -2
  52. package/dist/slots/frames.js.map +1 -1
  53. package/dist/snap.d.ts.map +1 -1
  54. package/dist/snap.js +12 -4
  55. package/dist/snap.js.map +1 -1
  56. package/dist/types.d.ts +5 -0
  57. package/dist/types.d.ts.map +1 -1
  58. package/dist/types.js.map +1 -1
  59. package/dist/undo.d.ts +2 -1
  60. package/dist/undo.d.ts.map +1 -1
  61. package/dist/undo.js +3 -2
  62. package/dist/undo.js.map +1 -1
  63. package/dist/variables/bindings.d.ts +2 -1
  64. package/dist/variables/bindings.d.ts.map +1 -1
  65. package/dist/variables/bindings.js +6 -2
  66. package/dist/variables/bindings.js.map +1 -1
  67. package/dist/variables/index.d.ts +2 -1
  68. package/dist/variables/index.d.ts.map +1 -1
  69. package/dist/variables/index.js +23 -5
  70. package/dist/variables/index.js.map +1 -1
  71. package/dist/variables/units.d.ts +31 -0
  72. package/dist/variables/units.d.ts.map +1 -0
  73. package/dist/variables/units.js +79 -4
  74. package/dist/variables/units.js.map +1 -1
  75. package/package.json +1 -1
  76. package/src/checkpoint.ts +8 -2
  77. package/src/color/index.ts +3 -6
  78. package/src/color/management.ts +8 -2
  79. package/src/coordinate.ts +4 -1
  80. package/src/copy.ts +2 -0
  81. package/src/css/values.ts +3 -2
  82. package/src/geometry.ts +147 -24
  83. package/src/graph.ts +9 -3
  84. package/src/hit-test.ts +138 -16
  85. package/src/instances/index.ts +5 -1
  86. package/src/instances/sync.ts +5 -3
  87. package/src/resize.ts +40 -20
  88. package/src/scaling/node.ts +3 -1
  89. package/src/scaling/tree.ts +3 -2
  90. package/src/slots/authoring.ts +7 -7
  91. package/src/slots/content.ts +13 -19
  92. package/src/slots/frames.ts +3 -3
  93. package/src/snap.ts +14 -4
  94. package/src/types.ts +5 -0
  95. package/src/undo.ts +3 -2
  96. package/src/variables/bindings.ts +6 -2
  97. package/src/variables/index.ts +29 -6
  98. package/src/variables/units.ts +97 -4
@@ -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
- let current = node.parentId ? graph.getNode(node.parentId) : undefined
19
- while (current && current.type !== 'CANVAS') {
20
- if (current.type === 'COMPONENT') return current
21
- if (current.type === 'INSTANCE') return undefined
22
- current = current.parentId ? graph.getNode(current.parentId) : undefined
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. */
@@ -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
- let current = parentOf(graph, node)
26
- while (current && current.type !== 'INSTANCE') current = parentOf(graph, current)
27
- return current
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
- let current = graph.nodes.get(parentId)
32
- while (current) {
33
- const propertyId = slotPropertyId(current)
34
- if (propertyId) {
35
- const instance = nearestInstance(graph, current)
36
- return instance ? { kind: 'slot', frame: current, instance, propertyId } : { kind: 'free' }
37
- }
38
- if (current.type === 'INSTANCE' || isGeneratedFrame(current))
39
- return { kind: 'locked', owner: current }
40
- current = parentOf(graph, current)
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. */
@@ -18,9 +18,9 @@ export function ownsSlotContent(
18
18
  propertyId = slotPropertyId(node)
19
19
  ): boolean {
20
20
  if (!propertyId) return false
21
- let owner = node.parentId ? graph.nodes.get(node.parentId) : undefined
22
- while (owner && owner.type !== 'INSTANCE')
23
- owner = owner.parentId ? graph.nodes.get(owner.parentId) : undefined
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 { rotatedBBox } from './geometry'
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
- return rotatedBBox(node.x, node.y, node.width, node.height, node.rotation)
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
- if (Math.abs(d) === Math.abs(bestDx)) {
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 (Math.abs(d) === Math.abs(bestDy)) {
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. Bound font styles are left as stored.
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 next = boundNumberValue(field, value)
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
  }
@@ -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,
@@ -27,9 +28,24 @@ import {
27
28
  uniformBindingTarget,
28
29
  VISIBILITY_VARIABLE_TYPES
29
30
  } from './fields'
31
+ import { pinnedRunBindingScales } from './units'
30
32
 
31
33
  export * from './resolution'
32
34
 
35
+ /**
36
+ * Gives the runs that bind `fields` the scale they resolve with as their own, before the layer's
37
+ * scale for those fields changes or goes, and the change to announce.
38
+ */
39
+ function pinRunBindingScales(
40
+ node: SceneNode,
41
+ fields: readonly string[]
42
+ ): Pick<Partial<SceneNode>, 'styleRuns'> {
43
+ const styleRuns = pinnedRunBindingScales(node, fields)
44
+ if (!styleRuns) return {}
45
+ node.styleRuns = styleRuns
46
+ return { styleRuns }
47
+ }
48
+
33
49
  export function addVariable(graph: SceneGraph, variable: Variable): void {
34
50
  graph.variables.set(variable.id, variable)
35
51
  const collection = graph.variableCollections.get(variable.collectionId)
@@ -54,13 +70,15 @@ export function removeVariable(graph: SceneGraph, id: string): void {
54
70
  string,
55
71
  string
56
72
  >
57
- node.variableBindingScales = omitBy(
58
- node.variableBindingScales,
59
- (_, field) => !(field in node.boundVariables)
73
+ const pruned = Object.keys(node.variableBindingScales).filter(
74
+ (field) => !(field in node.boundVariables)
60
75
  )
76
+ const runs = pinRunBindingScales(node, pruned)
77
+ node.variableBindingScales = omit(node.variableBindingScales, pruned)
61
78
  graph.emitter.emit('node:updated', node.id, {
62
79
  boundVariables: { ...node.boundVariables },
63
- variableBindingScales: { ...node.variableBindingScales }
80
+ variableBindingScales: { ...node.variableBindingScales },
81
+ ...runs
64
82
  })
65
83
  markBoundVariablesOverrideOnInstance(graph, node.id)
66
84
  }
@@ -345,11 +363,13 @@ export function bindVariable(
345
363
  }
346
364
  graph.willChangeNode(node)
347
365
  node.boundVariables = { ...node.boundVariables, [field]: variableId }
366
+ const runs = pinRunBindingScales(node, [field])
348
367
  assignVariableBindingUnits(graph, node, field)
349
368
  markBoundVariablesOverrideOnInstance(graph, nodeId, field)
350
369
  graph.emitter.emit('node:updated', nodeId, {
351
370
  boundVariables: { ...node.boundVariables },
352
- variableBindingScales: { ...node.variableBindingScales }
371
+ variableBindingScales: { ...node.variableBindingScales },
372
+ ...runs
353
373
  })
354
374
  }
355
375
 
@@ -359,11 +379,13 @@ export function unbindVariable(graph: SceneGraph, nodeId: string, field: string)
359
379
  if (!(field in node.boundVariables)) return
360
380
  graph.willChangeNode(node)
361
381
  node.boundVariables = omit(node.boundVariables, [field])
382
+ const runs = pinRunBindingScales(node, [field])
362
383
  node.variableBindingScales = omit(node.variableBindingScales, [field])
363
384
  markBoundVariablesOverrideOnInstance(graph, nodeId, field)
364
385
  graph.emitter.emit('node:updated', nodeId, {
365
386
  boundVariables: { ...node.boundVariables },
366
- variableBindingScales: { ...node.variableBindingScales }
387
+ variableBindingScales: { ...node.variableBindingScales },
388
+ ...runs
367
389
  })
368
390
  }
369
391
 
@@ -397,4 +419,5 @@ export * from './formula/schema'
397
419
  export * from './schema'
398
420
  export * from './text'
399
421
  export * from './token'
422
+ export * from './units'
400
423
  export * from './value'
@@ -1,15 +1,108 @@
1
- import type { SceneNode } from '../types'
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
- export function scaleVariableBindingUnits(node: SceneNode, factor: number): Partial<SceneNode> {
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
- if (field === 'opacity' || field === 'rotation') continue
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
- return { variableBindingScales, variableAssignmentScales }
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
  }