@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.2

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 (236) hide show
  1. package/dist/arrow-caps.d.ts.map +1 -1
  2. package/dist/arrow-caps.js +2 -5
  3. package/dist/arrow-caps.js.map +1 -1
  4. package/dist/behaviours/index.d.ts +2 -2
  5. package/dist/behaviours/index.js +2 -2
  6. package/dist/behaviours/kinds.d.ts +11 -5
  7. package/dist/behaviours/kinds.d.ts.map +1 -1
  8. package/dist/behaviours/kinds.js +76 -75
  9. package/dist/behaviours/kinds.js.map +1 -1
  10. package/dist/behaviours/layers.d.ts.map +1 -1
  11. package/dist/behaviours/layers.js +3 -2
  12. package/dist/behaviours/layers.js.map +1 -1
  13. package/dist/behaviours/model.d.ts +7 -4
  14. package/dist/behaviours/model.d.ts.map +1 -1
  15. package/dist/behaviours/model.js +16 -9
  16. package/dist/behaviours/model.js.map +1 -1
  17. package/dist/behaviours/schema.d.ts +118 -4
  18. package/dist/behaviours/schema.d.ts.map +1 -1
  19. package/dist/behaviours/schema.js +18 -3
  20. package/dist/behaviours/schema.js.map +1 -1
  21. package/dist/behaviours/spec.d.ts.map +1 -1
  22. package/dist/behaviours/spec.js +3 -2
  23. package/dist/behaviours/spec.js.map +1 -1
  24. package/dist/bindings.js +9 -1
  25. package/dist/bindings.js.map +1 -1
  26. package/dist/checkpoint.d.ts +31 -2
  27. package/dist/checkpoint.d.ts.map +1 -1
  28. package/dist/checkpoint.js +176 -5
  29. package/dist/checkpoint.js.map +1 -1
  30. package/dist/components/index.d.ts +3 -3
  31. package/dist/components/index.js +3 -3
  32. package/dist/components/properties.d.ts +13 -1
  33. package/dist/components/properties.d.ts.map +1 -1
  34. package/dist/components/properties.js +58 -3
  35. package/dist/components/properties.js.map +1 -1
  36. package/dist/components/variants.d.ts +16 -10
  37. package/dist/components/variants.d.ts.map +1 -1
  38. package/dist/components/variants.js +39 -13
  39. package/dist/components/variants.js.map +1 -1
  40. package/dist/connectors/hit.d.ts.map +1 -1
  41. package/dist/connectors/hit.js +1 -7
  42. package/dist/connectors/hit.js.map +1 -1
  43. package/dist/connectors/pieces.d.ts.map +1 -1
  44. package/dist/connectors/pieces.js +2 -5
  45. package/dist/connectors/pieces.js.map +1 -1
  46. package/dist/copy.d.ts.map +1 -1
  47. package/dist/copy.js +9 -15
  48. package/dist/copy.js.map +1 -1
  49. package/dist/css/blend.d.ts +11 -0
  50. package/dist/css/blend.d.ts.map +1 -0
  51. package/dist/css/blend.js +30 -0
  52. package/dist/css/blend.js.map +1 -0
  53. package/dist/css/index.d.ts +2 -1
  54. package/dist/css/index.js +2 -1
  55. package/dist/cubic.d.ts +7 -0
  56. package/dist/cubic.d.ts.map +1 -0
  57. package/dist/cubic.js +17 -0
  58. package/dist/cubic.js.map +1 -0
  59. package/dist/export-format.d.ts +14 -1
  60. package/dist/export-format.d.ts.map +1 -1
  61. package/dist/export-format.js +26 -1
  62. package/dist/export-format.js.map +1 -1
  63. package/dist/fields/text.d.ts +2 -2
  64. package/dist/fields/text.d.ts.map +1 -1
  65. package/dist/fields/text.js +2 -1
  66. package/dist/fields/text.js.map +1 -1
  67. package/dist/figjam/index.d.ts +2 -2
  68. package/dist/figjam/index.js +2 -2
  69. package/dist/figjam/palette.d.ts +13 -2
  70. package/dist/figjam/palette.d.ts.map +1 -1
  71. package/dist/figjam/palette.js +30 -2
  72. package/dist/figjam/palette.js.map +1 -1
  73. package/dist/generated/index.d.ts +2 -2
  74. package/dist/generated/index.js +2 -2
  75. package/dist/generated/shape-with-text.d.ts +7 -2
  76. package/dist/generated/shape-with-text.d.ts.map +1 -1
  77. package/dist/generated/shape-with-text.js +20 -3
  78. package/dist/generated/shape-with-text.js.map +1 -1
  79. package/dist/geometry.d.ts +30 -46
  80. package/dist/geometry.d.ts.map +1 -1
  81. package/dist/geometry.js +63 -131
  82. package/dist/geometry.js.map +1 -1
  83. package/dist/graph.d.ts +1 -0
  84. package/dist/graph.d.ts.map +1 -1
  85. package/dist/graph.js +15 -3
  86. package/dist/graph.js.map +1 -1
  87. package/dist/guides.d.ts +3 -1
  88. package/dist/guides.d.ts.map +1 -1
  89. package/dist/guides.js +5 -1
  90. package/dist/guides.js.map +1 -1
  91. package/dist/hit-test.d.ts.map +1 -1
  92. package/dist/hit-test.js +42 -29
  93. package/dist/hit-test.js.map +1 -1
  94. package/dist/index.d.ts +2 -2
  95. package/dist/instances/fields.d.ts +2 -2
  96. package/dist/instances/fields.d.ts.map +1 -1
  97. package/dist/instances/fields.js +8 -0
  98. package/dist/instances/fields.js.map +1 -1
  99. package/dist/instances/index.d.ts +1 -3
  100. package/dist/instances/index.d.ts.map +1 -1
  101. package/dist/instances/index.js +36 -30
  102. package/dist/instances/index.js.map +1 -1
  103. package/dist/instances/kept-overrides.js +204 -0
  104. package/dist/instances/kept-overrides.js.map +1 -0
  105. package/dist/instances/main-component.d.ts +3 -1
  106. package/dist/instances/main-component.d.ts.map +1 -1
  107. package/dist/instances/main-component.js +10 -2
  108. package/dist/instances/main-component.js.map +1 -1
  109. package/dist/instances/overrides.d.ts +4 -1
  110. package/dist/instances/overrides.d.ts.map +1 -1
  111. package/dist/instances/overrides.js +18 -2
  112. package/dist/instances/overrides.js.map +1 -1
  113. package/dist/instances/swap.js +288 -0
  114. package/dist/instances/swap.js.map +1 -0
  115. package/dist/instances/sync.js +84 -32
  116. package/dist/instances/sync.js.map +1 -1
  117. package/dist/node-defaults.d.ts.map +1 -1
  118. package/dist/node-defaults.js +1 -1
  119. package/dist/node-defaults.js.map +1 -1
  120. package/dist/outline/flatten.d.ts +15 -0
  121. package/dist/outline/flatten.d.ts.map +1 -0
  122. package/dist/outline/flatten.js +189 -0
  123. package/dist/outline/flatten.js.map +1 -0
  124. package/dist/outline/heads.js +34 -0
  125. package/dist/outline/heads.js.map +1 -0
  126. package/dist/outline/hit.d.ts +21 -0
  127. package/dist/outline/hit.d.ts.map +1 -0
  128. package/dist/outline/hit.js +276 -0
  129. package/dist/outline/hit.js.map +1 -0
  130. package/dist/outline/index.d.ts +5 -0
  131. package/dist/outline/index.js +5 -0
  132. package/dist/outline/point.d.ts +16 -0
  133. package/dist/outline/point.d.ts.map +1 -0
  134. package/dist/outline/point.js +42 -0
  135. package/dist/outline/point.js.map +1 -0
  136. package/dist/outline/shapes.d.ts +35 -0
  137. package/dist/outline/shapes.d.ts.map +1 -0
  138. package/dist/outline/shapes.js +181 -0
  139. package/dist/outline/shapes.js.map +1 -0
  140. package/dist/plugin-data/fields.d.ts +2 -1
  141. package/dist/plugin-data/fields.d.ts.map +1 -1
  142. package/dist/plugin-data/fields.js +19 -4
  143. package/dist/plugin-data/fields.js.map +1 -1
  144. package/dist/scaling/tree.js +1 -1
  145. package/dist/shared-styles.d.ts +14 -1
  146. package/dist/shared-styles.d.ts.map +1 -1
  147. package/dist/shared-styles.js +63 -2
  148. package/dist/shared-styles.js.map +1 -1
  149. package/dist/slots/content.d.ts.map +1 -1
  150. package/dist/slots/content.js +2 -0
  151. package/dist/slots/content.js.map +1 -1
  152. package/dist/slots/frames.d.ts +2 -0
  153. package/dist/slots/frames.d.ts.map +1 -1
  154. package/dist/slots/frames.js +7 -2
  155. package/dist/slots/frames.js.map +1 -1
  156. package/dist/text/index.d.ts +2 -2
  157. package/dist/text/index.js +3 -3
  158. package/dist/text/lengths.d.ts +6 -1
  159. package/dist/text/lengths.d.ts.map +1 -1
  160. package/dist/text/lengths.js +10 -1
  161. package/dist/text/lengths.js.map +1 -1
  162. package/dist/text/overflow.d.ts +2 -2
  163. package/dist/text/overflow.js.map +1 -1
  164. package/dist/text/picture.d.ts +1 -2
  165. package/dist/text/picture.d.ts.map +1 -1
  166. package/dist/text/picture.js +1 -4
  167. package/dist/text/picture.js.map +1 -1
  168. package/dist/transfer/index.js +1 -1
  169. package/dist/transfer/nodes.js +9 -5
  170. package/dist/transfer/nodes.js.map +1 -1
  171. package/dist/types.d.ts +27 -1
  172. package/dist/types.d.ts.map +1 -1
  173. package/dist/types.js.map +1 -1
  174. package/dist/variables/bindings.d.ts +28 -5
  175. package/dist/variables/bindings.d.ts.map +1 -1
  176. package/dist/variables/bindings.js +104 -14
  177. package/dist/variables/bindings.js.map +1 -1
  178. package/dist/variables/fields.d.ts +19 -1
  179. package/dist/variables/fields.d.ts.map +1 -1
  180. package/dist/variables/fields.js +36 -1
  181. package/dist/variables/fields.js.map +1 -1
  182. package/dist/variables/index.d.ts +3 -3
  183. package/dist/variables/index.d.ts.map +1 -1
  184. package/dist/variables/index.js +23 -8
  185. package/dist/variables/index.js.map +1 -1
  186. package/package.json +7 -1
  187. package/src/arrow-caps.ts +2 -5
  188. package/src/behaviours/kinds.ts +51 -34
  189. package/src/behaviours/layers.ts +4 -2
  190. package/src/behaviours/model.ts +22 -11
  191. package/src/behaviours/schema.ts +20 -4
  192. package/src/behaviours/spec.ts +8 -2
  193. package/src/bindings.ts +11 -2
  194. package/src/checkpoint.ts +251 -4
  195. package/src/components/properties.ts +92 -2
  196. package/src/components/variants.ts +51 -30
  197. package/src/connectors/hit.ts +1 -12
  198. package/src/connectors/pieces.ts +2 -5
  199. package/src/copy.ts +9 -15
  200. package/src/css/blend.ts +27 -0
  201. package/src/css/index.ts +1 -0
  202. package/src/cubic.ts +14 -0
  203. package/src/export-format.ts +29 -0
  204. package/src/fields/text.ts +3 -1
  205. package/src/figjam/palette.ts +29 -1
  206. package/src/generated/shape-with-text.ts +28 -2
  207. package/src/geometry.ts +78 -198
  208. package/src/graph.ts +27 -3
  209. package/src/guides.ts +5 -0
  210. package/src/hit-test.ts +67 -54
  211. package/src/instances/fields.ts +8 -0
  212. package/src/instances/index.ts +58 -31
  213. package/src/instances/kept-overrides.ts +280 -0
  214. package/src/instances/main-component.ts +12 -0
  215. package/src/instances/overrides.ts +27 -1
  216. package/src/instances/swap.ts +451 -0
  217. package/src/instances/sync.ts +141 -42
  218. package/src/node-defaults.ts +2 -1
  219. package/src/outline/flatten.ts +190 -0
  220. package/src/outline/heads.ts +52 -0
  221. package/src/outline/hit.ts +369 -0
  222. package/src/outline/index.ts +4 -0
  223. package/src/outline/point.ts +59 -0
  224. package/src/outline/shapes.ts +207 -0
  225. package/src/plugin-data/fields.ts +15 -2
  226. package/src/shared-styles.ts +82 -1
  227. package/src/slots/content.ts +2 -0
  228. package/src/slots/frames.ts +9 -7
  229. package/src/text/lengths.ts +10 -0
  230. package/src/text/overflow.ts +2 -2
  231. package/src/text/picture.ts +0 -4
  232. package/src/transfer/nodes.ts +7 -5
  233. package/src/types.ts +28 -0
  234. package/src/variables/bindings.ts +125 -14
  235. package/src/variables/fields.ts +46 -1
  236. package/src/variables/index.ts +35 -2
@@ -1,5 +1,7 @@
1
1
  // Child cloning and property synchronization shared by instance creation, swap, and sync.
2
- import { isEqual } from 'es-toolkit/predicate'
2
+ import { uniq } from 'es-toolkit/array'
3
+ import { pick } from 'es-toolkit/object'
4
+ import { isEmptyObject, isEqual } from 'es-toolkit/predicate'
3
5
 
4
6
  import { cloneNodeProps, copyEffects, copyFills, copyStrokes, copyStyleRuns } from '../copy'
5
7
  import type { NodeCloneMode } from '../copy'
@@ -7,6 +9,8 @@ import type { SceneGraph } from '../graph'
7
9
  import { scaleNodeChanges } from '../scaling/node'
8
10
  import { ownsSlotContent, slotPropertyId } from '../slots/frames'
9
11
  import type { SceneNode } from '../types'
12
+ import { resolvedNumericBindings } from '../variables/bindings'
13
+ import { effectBindingTarget, isNumericVariableBindingField } from '../variables/fields'
10
14
  import { scaleVariableBindingUnits } from '../variables/units'
11
15
  import { applyEnclosingAssignments, assignedFieldValues, propertyDrivenFields } from './assignments'
12
16
  import { keepOwnContentSize, remeasureChangedText, type SyncedPlacement } from './content-size'
@@ -62,6 +66,8 @@ export function copyProp(
62
66
  setSceneProp(target, key, source.gridPosition ? { ...source.gridPosition } : null)
63
67
  } else if (key === 'targetAspectRatio') {
64
68
  setSceneProp(target, key, source.targetAspectRatio ? { ...source.targetAspectRatio } : null)
69
+ } else if (key === 'booleanOperation') {
70
+ setSceneProp(target, key, source.booleanOperation ?? 'UNION')
65
71
  } else {
66
72
  const value = source[key]
67
73
  setSceneProp(target, key, Array.isArray(value) ? structuredClone(value) : value)
@@ -76,7 +82,16 @@ export function bindingProtection(scopes: ReadonlySet<string>[]): (field: string
76
82
  if (field !== 'boundVariables' || !perField) fields.add(field)
77
83
  }
78
84
  }
79
- return (field) => fields.has(field)
85
+ return (field) => {
86
+ if (fields.has(field)) return true
87
+ // Effects an instance set itself own their bindings too, bound or not, as its fills do.
88
+ const binding = overriddenBindingField(field)
89
+ return binding !== null && fields.has('effects') && effectBindingTarget(binding) !== null
90
+ }
91
+ }
92
+
93
+ function isUnbound(node: SceneNode): boolean {
94
+ return isEmptyObject(node.boundVariables) && isEmptyObject(node.variableBindingScales)
80
95
  }
81
96
 
82
97
  export function syncBindingFields(
@@ -85,7 +100,9 @@ export function syncBindingFields(
85
100
  updates: Partial<SceneNode>,
86
101
  protectedField: (field: string) => boolean
87
102
  ): void {
88
- if (protectedField('boundVariables')) return
103
+ if (protectedField('boundVariables') || (isUnbound(source) && isUnbound(target))) {
104
+ return
105
+ }
89
106
  const bindings = { ...source.boundVariables }
90
107
  const scales = { ...source.variableBindingScales }
91
108
  for (const field of new Set([
@@ -126,6 +143,15 @@ export function sourceInTargetCoordinates(source: SceneNode, targetScale: number
126
143
  }
127
144
  }
128
145
 
146
+ const SYNCED_FIELDS = INSTANCE_SYNC_FIELDS.filter((key) => key !== 'boundVariables').map((key) => ({
147
+ key,
148
+ owner: INSTANCE_SYNC_FIELD_OWNERS[key],
149
+ placement: PLACEMENT_FIELDS.has(key),
150
+ position: key === 'x' || key === 'y'
151
+ }))
152
+
153
+ export const NOTHING_PROTECTED = (_field: string): boolean => false
154
+
129
155
  interface SyncedFieldsInput {
130
156
  compParent: SceneNode
131
157
  instParent: SceneNode
@@ -147,15 +173,15 @@ function copySyncedFields(
147
173
  const placed = placedByLayout
148
174
  ? source
149
175
  : placedByConstraints(source, compParent, instParent, factor)
150
- for (const key of INSTANCE_SYNC_FIELDS) {
151
- if (key === 'boundVariables') continue
152
- if (placedByLayout && (key === 'x' || key === 'y')) continue
153
- if (skip(key)) continue
154
- const owner = INSTANCE_SYNC_FIELD_OWNERS[key]
155
- if (owner && skip(owner)) continue
156
- const from = PLACEMENT_FIELDS.has(key) ? placed : source
176
+ for (const { key, owner, placement, position } of SYNCED_FIELDS) {
177
+ if ((placedByLayout && position) || skip(key) || (owner && skip(owner))) {
178
+ continue
179
+ }
180
+ const from = placement ? placed : source
157
181
  // Most fields already match: comparing first spares cloning them for nothing.
158
- if (isEqual(copy[key], from[key])) continue
182
+ if (copy[key] === from[key] || isEqual(copy[key], from[key])) {
183
+ continue
184
+ }
159
185
  copyProp(updates, from, key)
160
186
  }
161
187
  // New paints bring their style link, unless the copy keeps a style of its own.
@@ -169,18 +195,59 @@ function copySyncedFields(
169
195
  export function updateSyncedProps(
170
196
  graph: SceneGraph,
171
197
  target: SceneNode,
172
- updates: Partial<SceneNode>
198
+ updates: Partial<SceneNode>,
199
+ /** A field the copy keeps as its own, which the sync leaves as it is. */
200
+ kept: (field: string) => boolean = () => false
173
201
  ): void {
174
- const changed = Object.fromEntries(
175
- Object.entries(updates).filter(
176
- ([key, value]) => !isEqual(target[key as keyof SceneNode], value)
177
- )
178
- ) as Partial<SceneNode>
179
- if (changed.fills && updates.fillStyleId !== undefined) changed.fillStyleId = updates.fillStyleId
180
- if (Object.keys(changed).length) graph.updateNode(target.id, changed)
202
+ resolveBoundNumbersInPlace(graph, target, updates, kept)
203
+ let changed: Partial<SceneNode> | undefined
204
+ for (const key of Object.keys(updates)) {
205
+ const current = target[key as keyof SceneNode]
206
+ const value = updates[key as keyof SceneNode]
207
+ if (current !== value && !isEqual(current, value)) {
208
+ changed ??= {}
209
+ Reflect.set(changed, key, value)
210
+ }
211
+ }
212
+ if (!changed) {
213
+ return
214
+ }
215
+ if (changed.fills && updates.fillStyleId !== undefined) {
216
+ changed.fillStyleId = updates.fillStyleId
217
+ }
218
+ graph.updateNode(target.id, changed)
181
219
  }
182
220
 
183
- function cloneChildInCoordinates(
221
+ /**
222
+ * A bound number the sync writes takes the value it resolves to where the copy sits, not the
223
+ * one its component holds: an instance that sets another mode, or sits in one that does, shows
224
+ * that mode's value, as Figma draws it. A field the sync leaves alone, such as one an instance
225
+ * overrides, keeps its own value.
226
+ */
227
+ function resolveBoundNumbersInPlace(
228
+ graph: SceneGraph,
229
+ target: SceneNode,
230
+ updates: Partial<SceneNode>,
231
+ kept: (field: string) => boolean
232
+ ): void {
233
+ const bound = updates.boundVariables ?? target.boundVariables
234
+ // The fields the sync writes, and the ones it binds anew, which keep an equal value only in
235
+ // the component's mode.
236
+ const rebound = updates.boundVariables
237
+ ? Object.keys(bound).filter((field) => bound[field] !== target.boundVariables[field])
238
+ : []
239
+ const synced = uniq([...Object.keys(updates), ...rebound]).filter(
240
+ (field) => isNumericVariableBindingField(field) && bound[field] && !kept(field)
241
+ )
242
+ if (synced.length === 0) return
243
+ // The modes the copy takes in this sync decide what its bindings resolve to.
244
+ if (updates.variableModes && !isEqual(updates.variableModes, target.variableModes))
245
+ graph.updateNode(target.id, { variableModes: updates.variableModes })
246
+ const resolved = resolvedNumericBindings(graph, { ...target, ...updates })
247
+ Object.assign(updates, pick(resolved, synced as (keyof SceneNode)[]))
248
+ }
249
+
250
+ export function cloneChildInCoordinates(
184
251
  graph: SceneGraph,
185
252
  src: SceneNode,
186
253
  sourceParent: SceneNode,
@@ -239,6 +306,9 @@ function childBindingProtection(
239
306
  ) {
240
307
  const fields = enclosingInstanceOverrideFields(graph, child)
241
308
  fields.push(new Set(overrides.descendants.get(child.id)?.keys()))
309
+ if (child.instanceOverrides.self.size === 0 && fields.every((scope) => scope.size === 0)) {
310
+ return NOTHING_PROTECTED
311
+ }
242
312
  const protectedField = bindingProtection(fields)
243
313
  return (field: string) => protectedField(field) || ownsSelfFillField(child, field)
244
314
  }
@@ -333,6 +403,52 @@ function sortInstanceChildren(
333
403
  })
334
404
  }
335
405
 
406
+ export interface MatchedCopy {
407
+ compParent: SceneNode
408
+ instParent: SceneNode
409
+ compChild: SceneNode
410
+ instChild: SceneNode
411
+ }
412
+
413
+ export function syncCopy(
414
+ graph: SceneGraph,
415
+ { compParent, instParent, compChild, instChild }: MatchedCopy,
416
+ protectedField: (field: string) => boolean,
417
+ linkedLayerId?: string
418
+ ): void {
419
+ const driven = propertyDrivenFields(graph, instChild, compChild)
420
+ const kept =
421
+ driven.size === 0 && protectedField === NOTHING_PROTECTED
422
+ ? NOTHING_PROTECTED
423
+ : (key: string) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)
424
+ const componentScale =
425
+ (compChild.componentScale * instParent.componentScale) / compParent.componentScale
426
+ const effectiveSource = sourceWithEffectiveFill(graph, compChild, instChild)
427
+ const source = sourceInTargetCoordinates(effectiveSource, componentScale)
428
+ // Which properties a layer serves is the component's to say; a slot or exposed layer
429
+ // created on the component becomes one in every instance.
430
+ const updates: Partial<SceneNode> = {}
431
+ if (instChild.componentScale !== componentScale) {
432
+ updates.componentScale = componentScale
433
+ }
434
+ if (!isEqual(instChild.componentPropertyReferences, compChild.componentPropertyReferences)) {
435
+ updates.componentPropertyReferences = structuredClone(compChild.componentPropertyReferences)
436
+ }
437
+ if (linkedLayerId !== undefined) {
438
+ updates.componentId = linkedLayerId
439
+ }
440
+ syncBindingFields(instChild, source, updates, protectedField)
441
+ const placement = copySyncedFields(updates, instChild, source, {
442
+ compParent,
443
+ instParent,
444
+ factor: componentScale / compChild.componentScale,
445
+ skip: kept
446
+ })
447
+ Object.assign(updates, assignedFieldValues(graph, instChild, compChild))
448
+ keepOwnContentSize(graph, updates, instChild, source, placement)
449
+ updateSyncedProps(graph, instChild, updates, kept)
450
+ }
451
+
336
452
  /** True when syncing `compParentId` into `instParentId` would form a cycle. */
337
453
  function isCyclicSync(graph: SceneGraph, compParentId: string, instParentId: string): boolean {
338
454
  return compParentId === instParentId || graph.isDescendant(instParentId, compParentId)
@@ -451,28 +567,11 @@ export function syncChildren(
451
567
  const instChild = instChildMap.get(compChildId)
452
568
  if (!compChild || !instChild) continue
453
569
 
454
- const protectedField = childBindingProtection(graph, instChild, overrides)
455
- const driven = propertyDrivenFields(graph, instChild, compChild)
456
- const componentScale =
457
- (compChild.componentScale * instParent.componentScale) / compParent.componentScale
458
- const effectiveSource = sourceWithEffectiveFill(graph, compChild, instChild)
459
- const source = sourceInTargetCoordinates(effectiveSource, componentScale)
460
- // Which properties a layer serves is the component's to say; a slot or exposed layer
461
- // created on the component becomes one in every instance.
462
- const updates: Partial<SceneNode> = {
463
- componentScale,
464
- componentPropertyReferences: structuredClone(compChild.componentPropertyReferences)
465
- }
466
- syncBindingFields(instChild, source, updates, protectedField)
467
- const placement = copySyncedFields(updates, instChild, source, {
468
- compParent,
469
- instParent,
470
- factor: componentScale / compChild.componentScale,
471
- skip: (key) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)
472
- })
473
- Object.assign(updates, assignedFieldValues(graph, instChild, compChild))
474
- keepOwnContentSize(graph, updates, instChild, source, placement)
475
- updateSyncedProps(graph, instChild, updates)
570
+ syncCopy(
571
+ graph,
572
+ { compParent, instParent, compChild, instChild },
573
+ childBindingProtection(graph, instChild, overrides)
574
+ )
476
575
 
477
576
  if (
478
577
  compChild.childIds.length > 0 &&
@@ -187,7 +187,8 @@ export function createDefaultNode(
187
187
  textTruncation: 'DISABLED',
188
188
  autoRename: true,
189
189
  pointCount: 5,
190
- starInnerRadius: 0.38,
190
+ // Figma's: the golden-ratio star, whose inner points line up with its outer ones.
191
+ starInnerRadius: (3 - Math.sqrt(5)) / 2,
191
192
  shapeWithTextType: 'SQUARE',
192
193
  shapeUserHeight: 0,
193
194
  shapeTruncates: false,
@@ -0,0 +1,190 @@
1
+ import { cubicPoint } from '../cubic'
2
+ import { GeometryCommand, readGeometryBlob } from '../geometry'
3
+ import type { PolygonOutlineCommand } from '../polygon'
4
+ import type { Vector } from '../primitives'
5
+ import type { VectorNetwork } from '../types'
6
+
7
+ /**
8
+ * Keeps a huge curve at a tiny tolerance from flattening into an unbounded number of points, while
9
+ * leaving room for the largest arcs at the maximum zoom: a 100,000-unit circle at 25,600% needs
10
+ * about 12,000 steps to stay within the click pad.
11
+ */
12
+ const MAX_CURVE_STEPS = 65_536
13
+
14
+ /**
15
+ * Steps that keep a cubic's polyline within `tolerance` of it: equal steps in t stray at most
16
+ * M / (8n²) from a curve whose second derivative is at most M, and for a cubic
17
+ * M ≤ 6 · max(|p0 − 2p1 + p2|, |p1 − 2p2 + p3|).
18
+ */
19
+ function curveSteps(p0: Vector, p1: Vector, p2: Vector, p3: Vector, tolerance: number): number {
20
+ const bend = Math.max(
21
+ Math.hypot(p0.x - 2 * p1.x + p2.x, p0.y - 2 * p1.y + p2.y),
22
+ Math.hypot(p1.x - 2 * p2.x + p3.x, p1.y - 2 * p2.y + p3.y)
23
+ )
24
+ const steps = Math.ceil(Math.sqrt((6 * bend) / (8 * tolerance)))
25
+ return Math.min(MAX_CURVE_STEPS, Math.max(1, steps))
26
+ }
27
+
28
+ /** Points along a cubic after its start, within `tolerance` of it, ending on `p3`. */
29
+ function cubicPoints(p0: Vector, p1: Vector, p2: Vector, p3: Vector, tolerance: number): Vector[] {
30
+ const steps = curveSteps(p0, p1, p2, p3, tolerance)
31
+ const points: Vector[] = []
32
+ for (let step = 1; step <= steps; step++) points.push(cubicPoint(p0, p1, p2, p3, step / steps))
33
+ return points
34
+ }
35
+
36
+ /** Points along a vector network segment, from its start to its end, in network coordinates. */
37
+ export function segmentPoints(
38
+ network: VectorNetwork,
39
+ index: number,
40
+ tolerance: number
41
+ ): Vector[] | null {
42
+ // `at` counts a negative index from the end and truncates a fraction; a loop naming either
43
+ // refers to no segment.
44
+ if (!Number.isInteger(index) || index < 0) return null
45
+ const segment = network.segments.at(index)
46
+ if (!segment || segment.start < 0 || segment.end < 0) return null
47
+ const p0 = network.vertices.at(segment.start)
48
+ const p3 = network.vertices.at(segment.end)
49
+ if (!p0 || !p3) return null
50
+ const { tangentStart: a, tangentEnd: b } = segment
51
+ if (a.x === 0 && a.y === 0 && b.x === 0 && b.y === 0) return [p0, p3]
52
+ const p1 = { x: p0.x + a.x, y: p0.y + a.y }
53
+ const p2 = { x: p3.x + b.x, y: p3.y + b.y }
54
+ return [p0, ...cubicPoints(p0, p1, p2, p3, tolerance)]
55
+ }
56
+
57
+ /**
58
+ * A loop's points in the order it is walked. Segments in a loop can be stored either way round,
59
+ * so each is followed from the vertex the previous one ended on.
60
+ */
61
+ export function loopPoints(network: VectorNetwork, loop: number[], tolerance: number): Vector[] {
62
+ const { segments } = network
63
+ const first = segments.at(loop[0] ?? -1)
64
+ const second = segments.at(loop[1] ?? -1)
65
+ if (!first) return []
66
+ // Walk the first segment toward the vertex it shares with the next one.
67
+ const firstForward = !second || first.end === second.start || first.end === second.end
68
+ let current = firstForward ? first.start : first.end
69
+ const points: Vector[] = []
70
+ for (const index of loop) {
71
+ const segment = segments.at(index)
72
+ const along = segmentPoints(network, index, tolerance)
73
+ if (!segment || !along) continue
74
+ const forward = segment.start === current
75
+ points.push(...(forward ? along : along.toReversed()))
76
+ current = forward ? segment.end : segment.start
77
+ }
78
+ return points
79
+ }
80
+
81
+ /**
82
+ * The network's segments joined into runs, a run continuing while each segment starts where the
83
+ * last ended, as a path draws them one after another.
84
+ */
85
+ export function segmentRuns(network: VectorNetwork, tolerance: number): Vector[][] {
86
+ const runs: Vector[][] = []
87
+ let run: Vector[] = []
88
+ let end = -1
89
+ for (const [index, segment] of network.segments.entries()) {
90
+ const along = segmentPoints(network, index, tolerance)
91
+ if (!along) continue
92
+ if (segment.start === end && run.length > 0) run.push(...along.slice(1))
93
+ else {
94
+ if (run.length > 1) runs.push(run)
95
+ run = along
96
+ }
97
+ end = segment.end
98
+ }
99
+ if (run.length > 1) runs.push(run)
100
+ return runs
101
+ }
102
+
103
+ /** The contours of a Figma geometry blob, curves flattened to within `tolerance`. */
104
+ export function blobContours(blob: Uint8Array, tolerance: number): Vector[][] {
105
+ const contours: Vector[][] = []
106
+ let contour: Vector[] = []
107
+ const flush = () => {
108
+ if (contour.length > 1) contours.push(contour)
109
+ contour = []
110
+ }
111
+ readGeometryBlob(blob, (command, c) => {
112
+ if (command === GeometryCommand.CLOSE) return flush()
113
+ if (command === GeometryCommand.MOVE) {
114
+ flush()
115
+ contour.push({ x: c[0], y: c[1] })
116
+ return
117
+ }
118
+ const end = { x: c[c.length - 2], y: c[c.length - 1] }
119
+ const last = contour.at(-1)
120
+ if (!last || command === GeometryCommand.LINE) {
121
+ contour.push(end)
122
+ } else if (command === GeometryCommand.QUAD) {
123
+ // A quadratic as the cubic with the same curve.
124
+ const q = { x: c[0], y: c[1] }
125
+ const c1 = { x: last.x + (2 / 3) * (q.x - last.x), y: last.y + (2 / 3) * (q.y - last.y) }
126
+ const c2 = { x: end.x + (2 / 3) * (q.x - end.x), y: end.y + (2 / 3) * (q.y - end.y) }
127
+ contour.push(...cubicPoints(last, c1, c2, end, tolerance))
128
+ } else {
129
+ contour.push(...cubicPoints(last, { x: c[0], y: c[1] }, { x: c[2], y: c[3] }, end, tolerance))
130
+ }
131
+ })
132
+ flush()
133
+ return contours
134
+ }
135
+
136
+ /** The contours of polygon outline commands, curves flattened to within `tolerance`. */
137
+ export function commandContours(
138
+ commands: readonly PolygonOutlineCommand[],
139
+ tolerance: number
140
+ ): Vector[][] {
141
+ const contours: Vector[][] = []
142
+ let contour: Vector[] = []
143
+ for (const command of commands) {
144
+ if (command.type === 'Z' || command.type === 'M') {
145
+ if (contour.length > 1) contours.push(contour)
146
+ contour = command.type === 'M' ? [{ x: command.x, y: command.y }] : []
147
+ continue
148
+ }
149
+ const last = contour.at(-1) ?? { x: 0, y: 0 }
150
+ if (command.type !== 'C') contour.push({ x: command.x, y: command.y })
151
+ else
152
+ contour.push(
153
+ ...cubicPoints(
154
+ last,
155
+ { x: command.x1, y: command.y1 },
156
+ { x: command.x2, y: command.y2 },
157
+ { x: command.x, y: command.y },
158
+ tolerance
159
+ )
160
+ )
161
+ }
162
+ if (contour.length > 1) contours.push(contour)
163
+ return contours
164
+ }
165
+
166
+ /** Points around an ellipse arc from `start` to `end` radians, at most `tolerance` off it. */
167
+ export function arcPoints(
168
+ cx: number,
169
+ cy: number,
170
+ rx: number,
171
+ ry: number,
172
+ start: number,
173
+ end: number,
174
+ tolerance: number
175
+ ): Vector[] {
176
+ const radius = Math.max(rx, ry)
177
+ // A chord over angle θ strays r(1 − cos(θ/2)) from the arc.
178
+ const step =
179
+ radius > tolerance ? 2 * Math.acos(Math.max(-1, 1 - tolerance / radius)) : Math.PI / 2
180
+ const steps = Math.min(
181
+ MAX_CURVE_STEPS,
182
+ Math.max(1, Math.ceil(Math.abs(end - start) / Math.max(step, 1e-3)))
183
+ )
184
+ const points: Vector[] = []
185
+ for (let i = 0; i <= steps; i++) {
186
+ const angle = start + ((end - start) * i) / steps
187
+ points.push({ x: cx + rx * Math.cos(angle), y: cy + ry * Math.sin(angle) })
188
+ }
189
+ return points
190
+ }
@@ -0,0 +1,52 @@
1
+ import {
2
+ arrowLinesSegments,
3
+ circleCapRadius,
4
+ collectArrowEndpoints,
5
+ diamondCapPoints,
6
+ equilateralArrowPoints,
7
+ triangleCapPoints
8
+ } from '../arrow-caps'
9
+ import type { Vector } from '../primitives'
10
+ import type { StrokeCap, VectorNetwork } from '../types'
11
+ import { containsByWinding, distanceToLines, distanceToSegment } from './point'
12
+
13
+ /** The outline of each filled head with corners, at an end pointing along `angle`. */
14
+ const HEAD_OUTLINES: Record<
15
+ 'ARROW_EQUILATERAL' | 'TRIANGLE_FILLED' | 'DIAMOND_FILLED',
16
+ (x: number, y: number, angle: number, weight: number) => Vector[]
17
+ > = {
18
+ ARROW_EQUILATERAL: equilateralArrowPoints,
19
+ TRIANGLE_FILLED: triangleCapPoints,
20
+ DIAMOND_FILLED: diamondCapPoints
21
+ }
22
+
23
+ /**
24
+ * Whether the point lands on an arrow head drawn at the open ends of `network`, in its own
25
+ * coordinates, within `pad`: inside a filled head or near its outline, or near a line arrow's
26
+ * wings, which are stroked at the line's weight.
27
+ */
28
+ export function hitsArrowHeads(
29
+ network: VectorNetwork,
30
+ cap: StrokeCap,
31
+ weight: number,
32
+ point: Vector,
33
+ pad: number
34
+ ): boolean {
35
+ for (const { x, y, angle, cap: head } of collectArrowEndpoints(network, cap)) {
36
+ if (head === 'CIRCLE_FILLED') {
37
+ if (Math.hypot(point.x - x, point.y - y) <= circleCapRadius(weight) + pad) return true
38
+ continue
39
+ }
40
+ if (head === 'ARROW_LINES') {
41
+ const wings = arrowLinesSegments(x, y, angle, weight)
42
+ if (wings.some((wing) => distanceToSegment(point, wing.from, wing.to) <= weight / 2 + pad)) {
43
+ return true
44
+ }
45
+ continue
46
+ }
47
+ const outline = HEAD_OUTLINES[head](x, y, angle, weight)
48
+ if (containsByWinding([outline], 'NONZERO', point)) return true
49
+ if (distanceToLines([[...outline, outline[0]]], point) <= pad) return true
50
+ }
51
+ return false
52
+ }