@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,3 +1,7 @@
1
+ import { omitBy } from 'es-toolkit/object'
2
+
3
+ import type { ExportSetting } from './types'
4
+
1
5
  /**
2
6
  * Formats a node's export settings may persist. Each id must name a Core IO
3
7
  * export adapter that supports node export; labels and options come from that
@@ -10,3 +14,28 @@ export type ExportFormatId = (typeof EXPORT_FORMAT_IDS)[number]
10
14
  export function isExportFormatId(value: unknown): value is ExportFormatId {
11
15
  return EXPORT_FORMAT_IDS.some((id) => id === value)
12
16
  }
17
+
18
+ /** What each of Figma's export options reads as when an export leaves it out. */
19
+ export const EXPORT_OPTION_DEFAULTS = {
20
+ suffix: '',
21
+ contentsOnly: true,
22
+ useAbsoluteBounds: false,
23
+ colorProfile: 'DOCUMENT',
24
+ svgOutlineText: true,
25
+ svgIdAttribute: false,
26
+ svgSimplifyStroke: true
27
+ } as const satisfies Required<Omit<ExportSetting, 'scale' | 'format' | 'size'>>
28
+
29
+ /** An export as the model keeps it: options that are unset or at Figma's default left out. */
30
+ export function compactExportSetting({
31
+ scale,
32
+ format,
33
+ size,
34
+ ...options
35
+ }: ExportSetting): ExportSetting {
36
+ const changed = omitBy(
37
+ options,
38
+ (value, key) => value === undefined || value === EXPORT_OPTION_DEFAULTS[key]
39
+ )
40
+ return size ? { scale, format, size, ...changed } : { scale, format, ...changed }
41
+ }
@@ -16,7 +16,9 @@ export const TEXT_METRIC_FIELDS = [
16
16
  'listSpacing',
17
17
  'paragraphSpacing',
18
18
  'paragraphIndent',
19
- 'hangingList'
19
+ 'hangingList',
20
+ // A cap-height trim drops the space above and below the glyphs, so the text measures shorter.
21
+ 'leadingTrim'
20
22
  ] as const satisfies readonly (keyof SceneNode)[]
21
23
 
22
24
  export const TEXT_SHAPING_FIELDS = [
@@ -244,6 +244,27 @@ export function figJamOutlineColorName(color: Color | undefined): FigJamColorNam
244
244
  )
245
245
  }
246
246
 
247
+ /**
248
+ * How FigJam draws a section's title on a board, measured in FigJam: a pill in the colour of the
249
+ * outline drawn on top, with white text on the palette's darkest colours and black on the rest;
250
+ * null for a section without an outline from the palette. `colorOf` reads an outline's colour as
251
+ * drawn, such as a variable's value in the active mode.
252
+ */
253
+ export function figJamSectionTitleColors(
254
+ section: Pick<SceneNode, 'strokes'>,
255
+ colorOf: (stroke: Stroke, index: number) => Color = (stroke) => stroke.color
256
+ ): { strokeIndex: number; background: Color; lightText: boolean } | null {
257
+ const strokeIndex = section.strokes.findLastIndex(
258
+ (stroke) => stroke.visible && stroke.opacity > 0
259
+ )
260
+ const outline = section.strokes.at(strokeIndex)
261
+ if (strokeIndex === -1 || !outline) return null
262
+ const color = colorOf(outline, strokeIndex)
263
+ const name = figJamOutlineColorName(color)
264
+ if (!name) return null
265
+ return { strokeIndex, background: { ...color, a: 1 }, lightText: PALETTE[name].lightLabel }
266
+ }
267
+
247
268
  /** The fill mode a shape's first fill shows. */
248
269
  export function figJamFillMode(fill: Fill | undefined): FigJamFillMode {
249
270
  if (!fill || !fill.visible || fill.opacity === 0) return 'none'
@@ -380,9 +401,16 @@ export function figJamSectionColorFields(
380
401
  name: FigJamColorName
381
402
  ): Partial<SceneNode> {
382
403
  const { sectionFill, sectionStroke } = PALETTE[name]
404
+ // The outline shows in full at FigJam's 2, as its menu draws it, unless the line menu hid it.
405
+ const strokes = strokesIn(section.strokes, parseColor(sectionStroke), SECTION_STROKE_WEIGHT).map(
406
+ (stroke): Stroke =>
407
+ stroke.opacity === 0
408
+ ? stroke
409
+ : { ...stroke, opacity: 1, visible: true, weight: SECTION_STROKE_WEIGHT }
410
+ )
383
411
  return {
384
412
  fills: [{ type: 'SOLID', color: parseColor(sectionFill), opacity: 1, visible: true }],
385
- strokes: strokesIn(section.strokes, parseColor(sectionStroke), SECTION_STROKE_WEIGHT),
413
+ strokes,
386
414
  strokeWeight: SECTION_STROKE_WEIGHT
387
415
  }
388
416
  }
@@ -9,9 +9,15 @@ import type { GeneratedFrameKind } from './kind'
9
9
 
10
10
  /** FigJam makes a shape with text 176 px square, from its tool and from its plugin API. */
11
11
  export const SHAPE_WITH_TEXT_SIZE = 176
12
- /** FigJam rounds every corner of a shape by 6 px. */
12
+ /** FigJam rounds every corner of a shape by 6 px, but a rounded rectangle's. */
13
13
  export const SHAPE_WITH_TEXT_CORNER_RADIUS = 6
14
14
 
15
+ /**
16
+ * FigJam keeps a rounded rectangle's corner radius at 0.4 of its shorter side, through resizes,
17
+ * measured in FigJam; Figma draws the shape from that radius.
18
+ */
19
+ const ROUNDED_RECTANGLE_RADIUS = 0.4
20
+
15
21
  const LABEL_FILL: Fill = { type: 'SOLID', color: { ...BLACK }, opacity: 0.8, visible: true }
16
22
 
17
23
  /** How the label sits in its shape: as wide as the label box, as tall as its text. */
@@ -42,7 +48,27 @@ export function shapeWithTextFrameFields(
42
48
  paddingRight: Math.max(0, width - box.x - box.width),
43
49
  paddingBottom: Math.max(0, height - box.y - box.height),
44
50
  paddingLeft: box.x,
45
- clipsContent: false
51
+ clipsContent: false,
52
+ ...(kind === 'ROUNDED_RECTANGLE'
53
+ ? { cornerRadius: ROUNDED_RECTANGLE_RADIUS * Math.min(width, height) }
54
+ : {})
55
+ }
56
+ }
57
+
58
+ /**
59
+ * What turning a shape into `kind` changes, keeping its size, as FigJam's shape switcher does: the
60
+ * label area, and the corners a rounded rectangle leaves go back to FigJam's 6.
61
+ */
62
+ export function shapeWithTextKindFields(
63
+ shape: Pick<SceneNode, 'shapeWithTextType' | 'width' | 'height'>,
64
+ kind: ShapeWithTextType
65
+ ): Partial<SceneNode> {
66
+ const leavesRounded =
67
+ shape.shapeWithTextType === 'ROUNDED_RECTANGLE' && kind !== shape.shapeWithTextType
68
+ return {
69
+ shapeWithTextType: kind,
70
+ ...(leavesRounded ? { cornerRadius: SHAPE_WITH_TEXT_CORNER_RADIUS } : {}),
71
+ ...shapeWithTextFrameFields(kind, shape.width, shape.height)
46
72
  }
47
73
  }
48
74
 
package/src/geometry.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { arrowCapOverflow } from './arrow-caps'
2
2
  import type { Rect, Vector } from './primitives'
3
- import { textLinesOverflow } from './text/overflow'
4
- import type { Effect, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './types'
3
+ import type { Effect, SceneNode, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './types'
4
+
5
+ export { cubicPoint } from './cubic'
5
6
 
6
7
  export function degToRad(degrees: number): number {
7
8
  return (degrees * Math.PI) / 180
@@ -216,6 +217,30 @@ function miterRatio(network: VectorNetwork, join: StrokeJoin | undefined, limit
216
217
  return ratio
217
218
  }
218
219
 
220
+ /**
221
+ * The blur radius Figma draws: a negative one, which only a bound variable can give, draws as no
222
+ * blur, though the layer keeps the value.
223
+ */
224
+ export function drawnBlurRadius(radius: number): number {
225
+ return Math.max(0, radius)
226
+ }
227
+
228
+ const BLUR_SIGMA_PER_RADIUS = 0.43
229
+
230
+ /**
231
+ * The Gaussian sigma that draws a Figma blur radius as Figma does. Measured on Figma 126 exports,
232
+ * layer blur, both shadows, and background blur fall off as a Gaussian of 0.43 × radius; Figma's
233
+ * SVG export writes radius / 2, which is wider (`packages/core/tests/canvas/blur.test.ts`).
234
+ */
235
+ export function blurSigma(radius: number): number {
236
+ return drawnBlurRadius(radius) * BLUR_SIGMA_PER_RADIUS
237
+ }
238
+
239
+ /** The Figma blur radius that draws a Gaussian of `sigma`, the inverse of `blurSigma`. */
240
+ export function blurRadiusForSigma(sigma: number): number {
241
+ return Math.max(0, sigma) / BLUR_SIGMA_PER_RADIUS
242
+ }
243
+
219
244
  export function effectOverflow(effects?: Effect[]) {
220
245
  let left = 0
221
246
  let right = 0
@@ -231,7 +256,7 @@ export function effectOverflow(effects?: Effect[]) {
231
256
  ) {
232
257
  continue
233
258
  }
234
- const blurSpread = effect.radius + effect.spread
259
+ const blurSpread = drawnBlurRadius(effect.radius) + effect.spread
235
260
  left = Math.max(left, blurSpread + Math.max(0, -effect.offset.x))
236
261
  right = Math.max(right, blurSpread + Math.max(0, effect.offset.x))
237
262
  top = Math.max(top, blurSpread + Math.max(0, -effect.offset.y))
@@ -253,59 +278,6 @@ export function computeAbsoluteBounds(
253
278
  return boundsToRect(bounds)
254
279
  }
255
280
 
256
- export function computeVisualBounds(
257
- nodes: Iterable<
258
- {
259
- id: string
260
- width: number
261
- height: number
262
- rotation?: number
263
- strokes?: Stroke[]
264
- strokeCap?: StrokeCap
265
- vectorNetwork?: VectorNetwork | null
266
- effects?: Effect[]
267
- } & StrokeShape
268
- >,
269
- getAbsolutePosition: (id: string) => Vector
270
- ): Rect {
271
- const bounds = createBoundsAccumulator()
272
-
273
- for (const n of nodes) {
274
- const abs = getAbsolutePosition(n.id)
275
- const bbox = rotatedBBox(abs.x, abs.y, n.width, n.height, n.rotation ?? 0)
276
- const stroke = strokeOverflow(n.strokes, n.strokeCap, n.vectorNetwork, n)
277
- const effects = effectOverflow(n.effects)
278
- includePoint(bounds, bbox.left - stroke - effects.left, bbox.top - stroke - effects.top)
279
- includePoint(bounds, bbox.right + stroke + effects.right, bbox.bottom + stroke + effects.bottom)
280
- }
281
-
282
- return boundsToRect(bounds)
283
- }
284
-
285
- export interface VisualBoundsNode {
286
- id: string
287
- width: number
288
- height: number
289
- rotation?: number
290
- flipX?: boolean
291
- flipY?: boolean
292
- strokes?: Stroke[]
293
- strokeCap?: StrokeCap
294
- vectorNetwork?: VectorNetwork | null
295
- effects?: Effect[]
296
- fillGeometry?: Array<{ commandsBlob: Uint8Array }>
297
- strokeGeometry?: Array<{ commandsBlob: Uint8Array }>
298
- childIds?: string[]
299
- visible?: boolean
300
- type?: string
301
- clipsContent?: boolean
302
- fontSize?: number
303
- textDecoration?: string
304
- textUnderlineOffset?: number | null
305
- textDecorationThickness?: number | null
306
- derivedTextHeight?: number | null
307
- }
308
-
309
281
  export function unionVisualBounds(
310
282
  a: VisualBounds | null,
311
283
  b: VisualBounds | null
@@ -328,163 +300,71 @@ export function intersectVisualBounds(a: VisualBounds, b: VisualBounds): VisualB
328
300
  return minX < maxX && minY < maxY ? { minX, minY, maxX, maxY } : null
329
301
  }
330
302
 
303
+ /** The path commands of a Figma geometry blob, by their byte code. */
304
+ export const GeometryCommand = { CLOSE: 0, MOVE: 1, LINE: 2, QUAD: 3, CUBIC: 4 } as const
305
+ export type GeometryCommand = (typeof GeometryCommand)[keyof typeof GeometryCommand]
306
+
331
307
  /**
332
- * Coordinate pairs per path blob command: 0=close, 1=move, 2=line,
333
- * 3=quad (glyph-derived blobs are quad-heavy), 4=cubic. Unknown → null so
334
- * walkers stop instead of desyncing into coordinate bytes.
308
+ * Coordinate pairs per command: glyph-derived blobs are quad-heavy, so every code needs its
309
+ * count. Unknown codes are null, so readers stop instead of desyncing into coordinate bytes.
335
310
  */
336
- export function geometryCommandCoordCount(command: number): number | null {
337
- if (command === 0) return 0
338
- if (command === 1 || command === 2) return 1
339
- if (command === 3) return 2
340
- if (command === 4) return 3
311
+ function commandCoordCount(command: number): number | null {
312
+ if (command === GeometryCommand.CLOSE) return 0
313
+ if (command === GeometryCommand.MOVE || command === GeometryCommand.LINE) return 1
314
+ if (command === GeometryCommand.QUAD) return 2
315
+ if (command === GeometryCommand.CUBIC) return 3
341
316
  return null
342
317
  }
343
318
 
344
- export function geometryBlobBounds(paths: Array<{ commandsBlob: Uint8Array }>): Rect | null {
345
- const bounds = createBoundsAccumulator()
346
-
347
- for (const path of paths) {
348
- const blob = path.commandsBlob
349
- const dv = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)
350
- let offset = 0
351
- while (offset < blob.length) {
352
- const command = blob[offset++]
353
- const coords = geometryCommandCoordCount(command)
354
- if (coords == null) break
355
- for (let i = 0; i < coords; i++) {
356
- if (offset + 8 > blob.length) break
357
- const x = dv.getFloat32(offset, true)
358
- const y = dv.getFloat32(offset + 4, true)
359
- includePoint(bounds, x, y)
360
- offset += 8
361
- }
319
+ /**
320
+ * Walks a Figma geometry blob: `visit` gets each command, its coordinates as x, y pairs in one
321
+ * array reused between calls, and the byte offset of the first coordinate. It stops at an
322
+ * unknown command or one cut short.
323
+ */
324
+ export function readGeometryBlob(
325
+ blob: Uint8Array,
326
+ visit: (command: GeometryCommand, coords: readonly number[], offset: number) => void
327
+ ): void {
328
+ const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)
329
+ const coords: number[] = []
330
+ let offset = 0
331
+ while (offset < blob.length) {
332
+ const command = blob[offset]
333
+ const count = commandCoordCount(command)
334
+ if (count === null || offset + 1 + count * 8 > blob.length) return
335
+ offset += 1
336
+ coords.length = 0
337
+ for (let i = 0; i < count; i++) {
338
+ coords.push(view.getFloat32(offset + i * 8, true), view.getFloat32(offset + i * 8 + 4, true))
362
339
  }
363
- }
364
-
365
- return bounds.minX === Infinity ? null : boundsToRect(bounds)
366
- }
367
-
368
- function transformLocalPoint(node: VisualBoundsNode, point: Vector): Vector {
369
- let x = node.flipX ? node.width - point.x : point.x
370
- let y = node.flipY ? node.height - point.y : point.y
371
- const rotation = node.rotation ?? 0
372
- if (rotation !== 0) {
373
- const rotated = rotatePoint(x, y, node.width / 2, node.height / 2, degToRad(rotation))
374
- x = rotated.x
375
- y = rotated.y
376
- }
377
- return { x, y }
378
- }
379
-
380
- function transformedLocalBounds(node: VisualBoundsNode, local: Rect, abs: Vector): VisualBounds {
381
- const points = [
382
- { x: local.x, y: local.y },
383
- { x: local.x + local.width, y: local.y },
384
- { x: local.x + local.width, y: local.y + local.height },
385
- { x: local.x, y: local.y + local.height }
386
- ].map((point) => transformLocalPoint(node, point))
387
-
388
- return {
389
- minX: abs.x + Math.min(...points.map((point) => point.x)),
390
- minY: abs.y + Math.min(...points.map((point) => point.y)),
391
- maxX: abs.x + Math.max(...points.map((point) => point.x)),
392
- maxY: abs.y + Math.max(...points.map((point) => point.y))
340
+ visit(command as GeometryCommand, coords, offset)
341
+ offset += count * 8
393
342
  }
394
343
  }
395
344
 
396
- export function nodeVisualBounds(
397
- node: VisualBoundsNode,
398
- getAbsolutePosition: (id: string) => Vector
399
- ): VisualBounds {
400
- const abs = getAbsolutePosition(node.id)
401
- const base = computeVisualBounds([node], getAbsolutePosition)
402
- let bounds: VisualBounds = {
403
- minX: base.x,
404
- minY: base.y,
405
- maxX: base.x + base.width,
406
- maxY: base.y + base.height
407
- }
408
-
409
- const hasNonInsideStroke = node.strokes?.some(
410
- (stroke) => stroke.visible && stroke.align !== 'INSIDE'
411
- )
412
- const localGeometry = geometryBlobBounds([
413
- ...(node.fillGeometry ?? []),
414
- ...(hasNonInsideStroke ? (node.strokeGeometry ?? []) : [])
415
- ])
416
- if (localGeometry) {
417
- bounds = unionVisualBounds(bounds, transformedLocalBounds(node, localGeometry, abs)) ?? bounds
418
- }
419
-
420
- const linesOverflow = textLinesOverflow(node)
421
- if (linesOverflow > 0) {
422
- const lines = { x: 0, y: 0, width: node.width, height: node.height + linesOverflow }
423
- bounds = unionVisualBounds(bounds, transformedLocalBounds(node, lines, abs)) ?? bounds
345
+ export function geometryBlobBounds(paths: Array<{ commandsBlob: Uint8Array }>): Rect | null {
346
+ const bounds = createBoundsAccumulator()
347
+ for (const path of paths) {
348
+ readGeometryBlob(path.commandsBlob, (_, coords) => {
349
+ for (let i = 0; i < coords.length; i += 2) includePoint(bounds, coords[i], coords[i + 1])
350
+ })
424
351
  }
425
352
 
426
- bounds.maxY += textDecorationOverflow(node)
427
-
428
- return bounds
353
+ return bounds.minX === Infinity ? null : boundsToRect(bounds)
429
354
  }
430
355
 
431
356
  /** How far a text node's underline or strikethrough reaches below its box. */
432
- export function textDecorationOverflow(node: VisualBoundsNode): number {
433
- if (node.type !== 'TEXT' || !node.textDecoration || node.textDecoration === 'NONE') return 0
434
- const fontSize = node.fontSize ?? 14
357
+ export function textDecorationOverflow(
358
+ node: Pick<
359
+ SceneNode,
360
+ 'type' | 'fontSize' | 'textDecoration' | 'textUnderlineOffset' | 'textDecorationThickness'
361
+ >
362
+ ): number {
363
+ if (node.type !== 'TEXT' || node.textDecoration === 'NONE') {
364
+ return 0
365
+ }
366
+ const { fontSize } = node
435
367
  const underlineOffset = node.textUnderlineOffset ?? fontSize * 0.18
436
368
  const thickness = node.textDecorationThickness ?? Math.max(1, fontSize / 16)
437
369
  return underlineOffset + thickness + fontSize * 0.35
438
370
  }
439
-
440
- function collectDescendantVisualBounds(
441
- nodeId: string,
442
- getNode: (id: string) => VisualBoundsNode | undefined,
443
- getAbsolutePosition: (id: string) => Vector,
444
- clip: VisualBounds | null = null
445
- ): VisualBounds | null {
446
- const node = getNode(nodeId)
447
- if (!node?.visible) return null
448
-
449
- const own = nodeVisualBounds(node, getAbsolutePosition)
450
- let bounds = clip ? intersectVisualBounds(own, clip) : own
451
-
452
- const isClippableContainer =
453
- node.type === 'FRAME' || node.type === 'COMPONENT' || node.type === 'INSTANCE'
454
- let childClip = clip
455
- if (isClippableContainer && node.clipsContent) {
456
- const abs = getAbsolutePosition(node.id)
457
- const nodeClip = {
458
- minX: abs.x,
459
- minY: abs.y,
460
- maxX: abs.x + node.width,
461
- maxY: abs.y + node.height
462
- }
463
- childClip = childClip ? intersectVisualBounds(childClip, nodeClip) : nodeClip
464
- if (!childClip) return bounds
465
- }
466
-
467
- for (const childId of node.childIds ?? []) {
468
- bounds = unionVisualBounds(
469
- bounds,
470
- collectDescendantVisualBounds(childId, getNode, getAbsolutePosition, childClip)
471
- )
472
- }
473
-
474
- return bounds
475
- }
476
-
477
- export function computeDescendantVisualBounds(
478
- nodeIds: string[],
479
- getNode: (id: string) => VisualBoundsNode | undefined,
480
- getAbsolutePosition: (id: string) => Vector
481
- ): VisualBounds | null {
482
- let bounds: VisualBounds | null = null
483
- for (const nodeId of nodeIds) {
484
- bounds = unionVisualBounds(
485
- bounds,
486
- collectDescendantVisualBounds(nodeId, getNode, getAbsolutePosition)
487
- )
488
- }
489
- return bounds
490
- }
package/src/graph.ts CHANGED
@@ -25,7 +25,8 @@ import { CONTAINER_TYPES, createDefaultNode } from './node-defaults'
25
25
  import { updateNodePreview, type NodePreviewObserver } from './preview'
26
26
  import type { Color, Rect, Vector } from './primitives'
27
27
  import { markSourceFieldsEdited } from './source-metadata'
28
- import { invalidateTextCaches } from './text/picture'
28
+ import { textCacheInvalidationChanges } from './text/picture'
29
+ import { remapCopiedReferences } from './transfer/copied'
29
30
  import type {
30
31
  DocumentColorSpace,
31
32
  DocumentKind,
@@ -576,8 +577,15 @@ export class SceneGraph {
576
577
  if (node.componentId) this.unindexInstance(node.componentId, id)
577
578
  if (changes.componentId) this.indexInstance(changes.componentId, id)
578
579
  }
579
- if (node.type === 'TEXT') invalidateTextCaches(node, changes)
580
580
  this.markEdited(node, Object.keys(changes))
581
+ // Cache resets that change the node travel with the change, under the caller's own values,
582
+ // so a layout put-back cannot drop their event.
583
+ if (node.type === 'TEXT') {
584
+ const resets = omitBy(textCacheInvalidationChanges(node, changes), (value, key) =>
585
+ Object.is(node[key], value)
586
+ )
587
+ changes = { ...resets, ...changes }
588
+ }
581
589
  if (changes.vectorNetwork) {
582
590
  changes = { ...changes, vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork) }
583
591
  }
@@ -589,6 +597,7 @@ export class SceneGraph {
589
597
  Object.assign(node, changes)
590
598
  if (changes.fills) removeStaleBindings(node, 'fills', changes)
591
599
  if (changes.strokes) removeStaleBindings(node, 'strokes', changes)
600
+ if (changes.effects) removeStaleBindings(node, 'effects', changes)
592
601
  for (const key of absent) Reflect.deleteProperty(node, key)
593
602
  this.publishNodeUpdate(id, changes)
594
603
  }
@@ -742,6 +751,20 @@ export class SceneGraph {
742
751
  sourceId: string,
743
752
  parentId: string,
744
753
  overrides: Partial<SceneNode> = {}
754
+ ): SceneNode | null {
755
+ const ids = new Map<string, string>()
756
+ const clone = this.cloneSubtree(sourceId, parentId, overrides, ids)
757
+ // Copies point at one another, as a paste's do: an instance at a component cloned with it,
758
+ // as Figma's clone does (desktop 126), and its override records at its copies' new ids.
759
+ remapCopiedReferences(this, ids)
760
+ return clone
761
+ }
762
+
763
+ private cloneSubtree(
764
+ sourceId: string,
765
+ parentId: string,
766
+ overrides: Partial<SceneNode>,
767
+ ids: Map<string, string>
745
768
  ): SceneNode | null {
746
769
  const src = this.nodes.get(sourceId)
747
770
  if (!src) return null
@@ -753,9 +776,10 @@ export class SceneGraph {
753
776
  props.status = null
754
777
  const clone = this.createNode(src.type, parentId, { ...props, ...overrides })
755
778
  if (src.status) markSourceFieldsEdited(clone, ['status'])
779
+ ids.set(src.id, clone.id)
756
780
 
757
781
  for (const childId of src.childIds) {
758
- this.cloneTree(childId, clone.id)
782
+ this.cloneSubtree(childId, clone.id, {}, ids)
759
783
  }
760
784
 
761
785
  return clone
package/src/guides.ts CHANGED
@@ -9,6 +9,11 @@ export interface CanvasGuide {
9
9
  figGuid?: GUID
10
10
  }
11
11
 
12
+ /** A new guide's id, unique across documents as a copied guide keeps its id. */
13
+ export function newGuideId(): string {
14
+ return `guide:${crypto.randomUUID()}`
15
+ }
16
+
12
17
  const ownersByGraph = new WeakMap<SceneGraph, Set<string>>()
13
18
 
14
19
  function ownerSet(graph: SceneGraph): Set<string> {