@open-pencil/scene-graph 0.15.1 → 0.16.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 (234) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -1
  3. package/dist/chunks/behaviours/kinds.js +273 -0
  4. package/dist/chunks/behaviours/kinds.js.map +1 -0
  5. package/dist/chunks/behaviours/layers.js +607 -0
  6. package/dist/chunks/behaviours/layers.js.map +1 -0
  7. package/dist/chunks/behaviours/spec.js +181 -0
  8. package/dist/chunks/behaviours/spec.js.map +1 -0
  9. package/dist/chunks/buffered-events.js +57 -0
  10. package/dist/chunks/buffered-events.js.map +1 -0
  11. package/dist/chunks/color/contrast.js +57 -0
  12. package/dist/chunks/color/contrast.js.map +1 -0
  13. package/dist/chunks/{components/properties.js → color/index.js} +577 -522
  14. package/dist/chunks/color/index.js.map +1 -0
  15. package/dist/chunks/comments/document.js +32 -0
  16. package/dist/chunks/comments/document.js.map +1 -0
  17. package/dist/chunks/comments/list.js +182 -0
  18. package/dist/chunks/comments/list.js.map +1 -0
  19. package/dist/chunks/comments/merge.js +58 -0
  20. package/dist/chunks/comments/merge.js.map +1 -0
  21. package/dist/chunks/components/exposure.js +2291 -0
  22. package/dist/chunks/components/exposure.js.map +1 -0
  23. package/dist/chunks/components/variants.js +32 -0
  24. package/dist/chunks/components/variants.js.map +1 -0
  25. package/dist/chunks/coordinate.js +93 -55
  26. package/dist/chunks/coordinate.js.map +1 -1
  27. package/dist/chunks/css/grid.js +94 -0
  28. package/dist/chunks/css/grid.js.map +1 -0
  29. package/dist/chunks/css/index.js +160 -0
  30. package/dist/chunks/css/index.js.map +1 -0
  31. package/dist/chunks/fields/geometry.js +14 -0
  32. package/dist/chunks/fields/geometry.js.map +1 -0
  33. package/dist/chunks/group-bounds.js +104 -0
  34. package/dist/chunks/group-bounds.js.map +1 -0
  35. package/dist/chunks/icons/glyph.js +147 -0
  36. package/dist/chunks/icons/glyph.js.map +1 -0
  37. package/dist/chunks/index.js +1724 -663
  38. package/dist/chunks/index.js.map +1 -1
  39. package/dist/color.d.ts +72 -0
  40. package/dist/color.d.ts.map +1 -0
  41. package/dist/color.js +3 -0
  42. package/dist/constants.d.ts +2 -2
  43. package/dist/constants.js +2 -2
  44. package/dist/constants2.d.ts +3 -1
  45. package/dist/constants2.d.ts.map +1 -1
  46. package/dist/coordinate.d.ts +2 -448
  47. package/dist/coordinate.js +2 -2
  48. package/dist/copy.d.ts +1 -1
  49. package/dist/copy.js +1 -1
  50. package/dist/css.d.ts +39 -0
  51. package/dist/css.d.ts.map +1 -0
  52. package/dist/css.js +3 -0
  53. package/dist/geometry.d.ts +1 -1
  54. package/dist/geometry.js +1 -1
  55. package/dist/gradient.d.ts +51 -0
  56. package/dist/gradient.d.ts.map +1 -0
  57. package/dist/gradient.js +132 -0
  58. package/dist/gradient.js.map +1 -0
  59. package/dist/guides.d.ts +2 -2
  60. package/dist/guides.js +39 -0
  61. package/dist/guides.js.map +1 -0
  62. package/dist/hit-test.d.ts +2 -9
  63. package/dist/hit-test.js +176 -34
  64. package/dist/hit-test.js.map +1 -1
  65. package/dist/index.d.ts +4 -8
  66. package/dist/index.js +19 -5
  67. package/dist/instances.d.ts +2 -2
  68. package/dist/instances.js +2 -2
  69. package/dist/layout.d.ts +2 -0
  70. package/dist/layout.js +4 -0
  71. package/dist/node-defaults.d.ts +2 -8
  72. package/dist/node-defaults.js +2 -2
  73. package/dist/order-keys.d.ts +39 -0
  74. package/dist/order-keys.d.ts.map +1 -0
  75. package/dist/order-keys.js +136 -0
  76. package/dist/order-keys.js.map +1 -0
  77. package/dist/parse-path.d.ts +7 -1
  78. package/dist/parse-path.d.ts.map +1 -1
  79. package/dist/parse-path.js +6 -2
  80. package/dist/parse-path.js.map +1 -1
  81. package/dist/polygon.d.ts +53 -0
  82. package/dist/polygon.d.ts.map +1 -0
  83. package/dist/polygon.js +205 -0
  84. package/dist/polygon.js.map +1 -0
  85. package/dist/preview.d.ts +1 -1
  86. package/dist/random.d.ts +7 -0
  87. package/dist/random.d.ts.map +1 -0
  88. package/dist/random.js +2 -0
  89. package/dist/resize.d.ts +11 -2
  90. package/dist/resize.d.ts.map +1 -1
  91. package/dist/resize.js +2 -184
  92. package/dist/snap.d.ts +1 -1
  93. package/dist/text-direction.d.ts +13 -0
  94. package/dist/text-direction.d.ts.map +1 -0
  95. package/dist/text-direction.js +31 -0
  96. package/dist/text-direction.js.map +1 -0
  97. package/dist/text-picture.d.ts +1 -1
  98. package/dist/text-picture.d.ts.map +1 -1
  99. package/dist/text.d.ts +2 -0
  100. package/dist/text.js +3 -0
  101. package/dist/types.d.ts +2121 -15
  102. package/dist/types.d.ts.map +1 -1
  103. package/dist/types.js +2 -1
  104. package/dist/types2.d.ts +2 -2
  105. package/dist/undo.d.ts +2 -2
  106. package/dist/undo.js +2 -2
  107. package/dist/undo2.d.ts +29 -1
  108. package/dist/undo2.d.ts.map +1 -1
  109. package/dist/variables.d.ts +2 -29
  110. package/dist/variables.js +2 -2
  111. package/dist/variant-properties.d.ts +9 -1
  112. package/dist/variant-properties.d.ts.map +1 -1
  113. package/dist/variant-properties.js +36 -1
  114. package/dist/variant-properties.js.map +1 -1
  115. package/dist/vector-network.d.ts +1 -1
  116. package/dist/vector-network.js +1 -1
  117. package/package.json +60 -9
  118. package/src/behaviours/kinds.ts +185 -0
  119. package/src/behaviours/layers.ts +51 -0
  120. package/src/behaviours/model.ts +192 -0
  121. package/src/behaviours/schema.ts +58 -0
  122. package/src/behaviours/spec.ts +223 -0
  123. package/src/buffered-events.ts +64 -0
  124. package/src/color/contrast.ts +31 -0
  125. package/src/color/index.ts +88 -0
  126. package/src/color/management.ts +210 -0
  127. package/src/color/normalize.ts +7 -0
  128. package/src/color/okhcl.ts +201 -0
  129. package/src/comments/document.ts +51 -0
  130. package/src/comments/list.ts +52 -0
  131. package/src/comments/merge.ts +63 -0
  132. package/src/comments/threads.ts +156 -0
  133. package/src/components/exposure.ts +61 -0
  134. package/src/components/properties.ts +18 -15
  135. package/src/components/variants.ts +53 -0
  136. package/src/constants.ts +2 -0
  137. package/src/coordinate.ts +100 -43
  138. package/src/copy.ts +17 -6
  139. package/src/css/grid.ts +70 -0
  140. package/src/css/index.ts +9 -0
  141. package/src/css/layout.ts +53 -0
  142. package/src/css/shadows.ts +68 -0
  143. package/src/css/values.ts +42 -0
  144. package/src/export-format.ts +12 -0
  145. package/src/fields/geometry.ts +11 -0
  146. package/src/fields/text.ts +35 -0
  147. package/src/gradient.ts +166 -0
  148. package/src/group-bounds.ts +131 -0
  149. package/src/guides.ts +39 -0
  150. package/src/hit-test.ts +262 -46
  151. package/src/icons/glyph.ts +128 -0
  152. package/src/icons/model.ts +54 -0
  153. package/src/icons/schema.ts +18 -0
  154. package/src/index.ts +347 -57
  155. package/src/instance-overrides.ts +27 -0
  156. package/src/instances/content-size.ts +58 -0
  157. package/src/instances/copies.ts +239 -0
  158. package/src/instances/cycles.ts +73 -0
  159. package/src/instances/fields.ts +143 -0
  160. package/src/instances/main-component.ts +19 -0
  161. package/src/instances/placement.ts +29 -0
  162. package/src/instances/source-walk.ts +41 -0
  163. package/src/instances/sync.ts +600 -0
  164. package/src/instances/vector-geometry.ts +81 -0
  165. package/src/instances.ts +125 -330
  166. package/src/layout/aspect-ratio.ts +51 -0
  167. package/src/{layout-guides.ts → layout/guides.ts} +41 -12
  168. package/src/layout/index.ts +4 -0
  169. package/src/layout/keywords.ts +73 -0
  170. package/src/layout/sizing.ts +196 -0
  171. package/src/mutation-impact.ts +25 -10
  172. package/src/node-defaults.ts +50 -2
  173. package/src/order-keys.ts +162 -0
  174. package/src/parse-path.ts +10 -2
  175. package/src/plugin-data/field.ts +131 -0
  176. package/src/plugin-data/fields.ts +246 -0
  177. package/src/polygon.ts +211 -0
  178. package/src/preview.ts +1 -1
  179. package/src/random.ts +28 -0
  180. package/src/resize.ts +99 -12
  181. package/src/scaling/index.ts +2 -0
  182. package/src/scaling/node.ts +164 -0
  183. package/src/scaling/tree.ts +50 -0
  184. package/src/shaders/model.ts +98 -0
  185. package/src/shaders/schema.ts +48 -0
  186. package/src/shared-styles.ts +5 -2
  187. package/src/slots/authoring.ts +101 -0
  188. package/src/slots/content.ts +124 -0
  189. package/src/slots/frames.ts +160 -0
  190. package/src/slots/limits.ts +49 -0
  191. package/src/text/direction.ts +55 -0
  192. package/src/text/index.ts +3 -0
  193. package/src/text/lists.ts +138 -0
  194. package/src/text/paragraphs.ts +241 -0
  195. package/src/{text-picture.ts → text/picture.ts} +21 -29
  196. package/src/text/references.ts +50 -0
  197. package/src/transfer/apply.ts +84 -0
  198. package/src/transfer/history.ts +49 -0
  199. package/src/transfer/identity.ts +16 -0
  200. package/src/transfer/index.ts +6 -0
  201. package/src/transfer/nodes.ts +120 -0
  202. package/src/transfer/plan.ts +130 -0
  203. package/src/transfer/references.ts +9 -0
  204. package/src/transfer/remove.ts +39 -0
  205. package/src/transfer/validation.ts +59 -0
  206. package/src/transfer/variables.ts +82 -0
  207. package/src/types.ts +257 -13
  208. package/src/undo.ts +54 -2
  209. package/src/variables/bindings.ts +224 -0
  210. package/src/variables/fields.ts +43 -0
  211. package/src/variables/token.ts +14 -0
  212. package/src/variables/units.ts +15 -0
  213. package/src/variables.ts +118 -69
  214. package/src/variant-properties.ts +50 -0
  215. package/dist/chunks/components/properties.js.map +0 -1
  216. package/dist/chunks/export-scale.js +0 -19
  217. package/dist/chunks/export-scale.js.map +0 -1
  218. package/dist/coordinate.d.ts.map +0 -1
  219. package/dist/coordinate2.d.ts +0 -2
  220. package/dist/guides2.d.ts +0 -11
  221. package/dist/guides2.d.ts.map +0 -1
  222. package/dist/hit-test.d.ts.map +0 -1
  223. package/dist/layout-guides.d.ts +0 -2
  224. package/dist/layout-guides.js +0 -2
  225. package/dist/layout-guides2.d.ts +0 -16
  226. package/dist/layout-guides2.d.ts.map +0 -1
  227. package/dist/node-defaults.d.ts.map +0 -1
  228. package/dist/preview2.d.ts +0 -12
  229. package/dist/preview2.d.ts.map +0 -1
  230. package/dist/resize.js.map +0 -1
  231. package/dist/variables.d.ts.map +0 -1
  232. package/dist/vector-network2.d.ts +0 -32
  233. package/dist/vector-network2.d.ts.map +0 -1
  234. package/src/instance-overrides.test.ts +0 -67
package/src/types.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /* eslint-disable max-lines -- scene node contracts are kept together as the public graph type surface */
2
2
 
3
+ import type { ExportFormatId } from './export-format'
3
4
  import type { CanvasGuide } from './guides'
4
5
  import type { InstanceOverrideState } from './instance-overrides'
5
6
  import type { Color, Matrix, Rect, Vector } from './primitives'
@@ -32,6 +33,14 @@ export type SceneGraphEventHandlers = Partial<{
32
33
  ) => void
33
34
  }>
34
35
 
36
+ /** Hears about nodes as an edit adds and changes them, synchronously and before each change. */
37
+ export interface NodeChangeObserver {
38
+ /** A node is about to change and is still as it was. */
39
+ before: (node: SceneNode) => void
40
+ /** A node was just added. */
41
+ created: (node: SceneNode) => void
42
+ }
43
+
35
44
  export type DocumentColorSpace = 'srgb' | 'display-p3'
36
45
 
37
46
  export interface FigmaSourcePayload {
@@ -187,12 +196,14 @@ export type MaskType = 'ALPHA' | 'VECTOR' | 'LUMINANCE'
187
196
  export interface LayoutGrid {
188
197
  visible?: boolean
189
198
  color?: Color
190
- pattern?: 'COLUMNS' | 'ROWS' | 'GRID'
199
+ /** `STRIPES` is the archive's name for columns or rows, along `axis`. */
200
+ pattern?: 'COLUMNS' | 'ROWS' | 'GRID' | 'STRIPES'
191
201
  axis?: 'X' | 'Y'
192
202
  type?: 'MIN' | 'CENTER' | 'MAX' | 'STRETCH'
193
203
  alignment?: 'MIN' | 'CENTER' | 'MAX' | 'STRETCH'
194
204
  numSections?: number
195
- count?: number
205
+ /** Figma's plugin API reports an Auto count as `null` and accepts `Infinity` for it. */
206
+ count?: number | null
196
207
  offset?: number
197
208
  sectionSize?: number
198
209
  gutterSize?: number
@@ -205,11 +216,9 @@ export interface SharedStyle {
205
216
  type: SharedStyleType
206
217
  }
207
218
 
208
- export interface Stroke {
209
- color: Color
219
+ /** A stroke is a paint with the geometry that decides where it is drawn. */
220
+ export interface Stroke extends Fill {
210
221
  weight: number
211
- opacity: number
212
- visible: boolean
213
222
  align: 'INSIDE' | 'CENTER' | 'OUTSIDE'
214
223
  cap?: StrokeCap
215
224
  join?: StrokeJoin
@@ -236,6 +245,22 @@ export type TextDecorationStyle = 'SOLID' | 'DOTTED' | 'WAVY'
236
245
  export type LeadingTrim = 'NONE' | 'CAP_HEIGHT'
237
246
  export type TextDirection = 'AUTO' | 'LTR' | 'RTL'
238
247
  export type LayoutDirection = 'AUTO' | 'LTR' | 'RTL'
248
+ /** Whether a paragraph is an item of a numbered list, a bulleted list, or no list. */
249
+ export type TextListType = 'NONE' | 'ORDERED' | 'UNORDERED'
250
+
251
+ /** How one paragraph of a text node (a line ending at a newline) sits in a list. */
252
+ /** Spacing a text sets for all its paragraphs and a paragraph can set for itself. */
253
+ export type TextParagraphSpacingField = 'listSpacing' | 'paragraphSpacing' | 'paragraphIndent'
254
+
255
+ export interface TextParagraphStyle {
256
+ listType: TextListType
257
+ /** Nesting level: 1–5 for list items; kept but not drawn for plain paragraphs. */
258
+ indentation: number
259
+ /** This paragraph's own spacing, in place of the text's; absent ones follow the text. */
260
+ listSpacing?: number
261
+ paragraphSpacing?: number
262
+ paragraphIndent?: number
263
+ }
239
264
 
240
265
  export interface FontVariation {
241
266
  axis: string
@@ -247,6 +272,29 @@ export interface FontFeature {
247
272
  enabled: boolean
248
273
  }
249
274
 
275
+ /** Where a stretch of text links to, as Figma's `HyperlinkTarget`: a URL, or a node by its ID. */
276
+ export interface HyperlinkTarget {
277
+ type: 'URL' | 'NODE'
278
+ value: string
279
+ }
280
+
281
+ /** The text fields a range of characters can bind to a variable, as Figma lists them. */
282
+ export const TEXT_BINDABLE_FIELDS = [
283
+ 'fontFamily',
284
+ 'fontSize',
285
+ 'fontStyle',
286
+ 'fontWeight',
287
+ 'letterSpacing',
288
+ 'lineHeight',
289
+ 'paragraphSpacing',
290
+ 'paragraphIndent'
291
+ ] as const
292
+
293
+ export type TextBindableField = (typeof TEXT_BINDABLE_FIELDS)[number]
294
+
295
+ /** Variable IDs bound to text fields, by field. */
296
+ export type TextBoundVariables = Partial<Record<TextBindableField, string>>
297
+
250
298
  export interface CharacterStyleOverride {
251
299
  fontWeight?: number
252
300
  italic?: boolean
@@ -264,6 +312,15 @@ export interface CharacterStyleOverride {
264
312
  fontVariations?: FontVariation[]
265
313
  fontFeatures?: FontFeature[]
266
314
  textLanguage?: string | null
315
+ textCase?: TextCase
316
+ /** A link over the characters; `null`, like leaving it out, links nowhere. */
317
+ hyperlink?: HyperlinkTarget | null
318
+ /** The text style the characters use in place of the text's own; `null` uses none. */
319
+ textStyleId?: string | null
320
+ /** The fill style the characters use in place of the text's own; `null` uses none. */
321
+ fillStyleId?: string | null
322
+ /** The characters' text field bindings, in place of the text's own. */
323
+ boundVariables?: TextBoundVariables
267
324
  }
268
325
 
269
326
  export interface StyleRun {
@@ -279,7 +336,10 @@ export interface ArcData {
279
336
  }
280
337
 
281
338
  export type LayoutMode = 'NONE' | 'HORIZONTAL' | 'VERTICAL' | 'GRID'
339
+ /** A node's sizing along one axis; fill is stored on the child, see `layoutSizing`. */
282
340
  export type LayoutSizing = 'FIXED' | 'HUG' | 'FILL'
341
+ /** Whether an auto-layout frame keeps its size or hugs its children along one of its axes. */
342
+ export type AxisSizingMode = 'FIXED' | 'HUG'
283
343
 
284
344
  export type GridTrackSizing = 'FIXED' | 'FR' | 'AUTO'
285
345
 
@@ -305,8 +365,6 @@ export interface PluginDataEntry {
305
365
  value: string
306
366
  }
307
367
 
308
- export type ExportFormatId = 'png' | 'jpg' | 'webp' | 'svg' | 'pdf'
309
-
310
368
  export interface ExportSetting {
311
369
  scale: number
312
370
  format: ExportFormatId
@@ -332,12 +390,19 @@ export interface TextPathData {
332
390
  }
333
391
 
334
392
  export interface DerivedTextGlyph {
393
+ /** UTF-16 source-text cluster start, when supplied by the shaping source. */
394
+ firstCharacter?: number
335
395
  commandsBlob: Uint8Array
336
396
  x: number
337
397
  y: number
338
398
  fontSize: number
339
399
  /** Figma Glyph.rotation — radians, not degrees. Zero for axis-aligned text. */
340
400
  rotation?: number
401
+ /**
402
+ * Horizontal advance in pixels at `fontSize`, before `scaleX`, when the shaping source gave
403
+ * one; where the glyph ends, so decorations stop at the text rather than the layer's edge.
404
+ */
405
+ advance?: number
341
406
  /**
342
407
  * Accumulated non-uniform resize scale (default 1). Paint order is
343
408
  * translate → scale(scaleX,Y) → rotate → scale(fontSize,-fontSize) so
@@ -405,6 +470,56 @@ export interface EnabledLibraryBinding {
405
470
  enabled: boolean
406
471
  }
407
472
 
473
+ /** The library a document publishes as. */
474
+ export interface SourceLibraryPublication {
475
+ libraryId: string
476
+ revisionId: string
477
+ name: string
478
+ catalogSource?: string
479
+ }
480
+
481
+ /** A reply in a canvas comment thread. */
482
+ export interface CommentReply {
483
+ id: string
484
+ author: string
485
+ /** The author's collaboration color when they wrote it, for their avatar. */
486
+ authorColor?: Color
487
+ /** Markdown: bold, italic, strikethrough, links and lists. */
488
+ text: string
489
+ createdAt: string
490
+ deleted?: boolean
491
+ }
492
+
493
+ /** A canvas comment thread, kept in the document's root plugin data. */
494
+ export interface CommentThread {
495
+ id: string
496
+ pageId: string
497
+ /** Page name when the pin was placed, for readers of the raw data. */
498
+ pageName?: string
499
+ /** Top-level layer under the pin; the pin follows it when it moves. */
500
+ nodeId?: string | null
501
+ nodeName?: string | null
502
+ /** Offset from the layer's top-left corner, in canvas units. */
503
+ offsetX?: number
504
+ offsetY?: number
505
+ /** Canvas position when the pin was last saved; used when the layer is gone. */
506
+ x: number
507
+ y: number
508
+ author: string
509
+ /** The author's collaboration color when they wrote it, for their avatar. */
510
+ authorColor?: Color
511
+ /** Markdown: bold, italic, strikethrough, links and lists. */
512
+ text: string
513
+ createdAt: string
514
+ /** Bumped on every change. */
515
+ updatedAt: string
516
+ resolved: boolean
517
+ resolvedAt?: string | null
518
+ /** Deleted threads stay as tombstones so a stale copy cannot bring them back. */
519
+ deleted?: boolean
520
+ replies: CommentReply[]
521
+ }
522
+
408
523
  export interface SceneNode {
409
524
  id: string
410
525
  type: NodeType
@@ -469,6 +584,19 @@ export interface SceneNode {
469
584
  lineHeight: number | null
470
585
  letterSpacing: number
471
586
  maxLines: number | null
587
+ /**
588
+ * One entry per paragraph of `text`, in order. Missing entries are plain paragraphs, so text
589
+ * without lists keeps this empty.
590
+ */
591
+ textParagraphs: TextParagraphStyle[]
592
+ /** Space after a list item followed by another; a paragraph's own value comes first. */
593
+ listSpacing: number
594
+ /** Space after a paragraph other than between two list items. */
595
+ paragraphSpacing: number
596
+ /** Indent of the first line of each plain paragraph. */
597
+ paragraphIndent: number
598
+ /** Whether list markers hang outside the text box instead of indenting the items. */
599
+ hangingList: boolean
472
600
 
473
601
  styleRuns: StyleRun[]
474
602
  fontVariations: FontVariation[]
@@ -482,8 +610,8 @@ export interface SceneNode {
482
610
  layoutWrap: LayoutWrap
483
611
  primaryAxisAlign: LayoutAlign
484
612
  counterAxisAlign: LayoutCounterAlign
485
- primaryAxisSizing: LayoutSizing
486
- counterAxisSizing: LayoutSizing
613
+ primaryAxisSizing: AxisSizingMode
614
+ counterAxisSizing: AxisSizingMode
487
615
  itemSpacing: number
488
616
  counterAxisSpacing: number
489
617
  paddingTop: number
@@ -497,7 +625,7 @@ export interface SceneNode {
497
625
 
498
626
  vectorNetwork: VectorNetwork | null
499
627
  handleMirroring: HandleMirroring
500
- booleanOperation?: 'UNION' | 'SUBTRACT' | 'INTERSECT' | 'EXCLUDE'
628
+ booleanOperation: 'UNION' | 'SUBTRACT' | 'INTERSECT' | 'EXCLUDE' | undefined
501
629
  fillGeometry: GeometryPath[]
502
630
  strokeGeometry: GeometryPath[]
503
631
 
@@ -513,12 +641,25 @@ export interface SceneNode {
513
641
  borderLeftWeight: number
514
642
  independentStrokeWeights: boolean
515
643
 
644
+ /**
645
+ * The weight and alignment a node keeps for its strokes, as Figma keeps them on the node: they
646
+ * last while it has no strokes, and a stroke added to it takes them. Each stroke still carries
647
+ * its own `weight` and `align`, which win while the node has strokes.
648
+ */
649
+ strokeWeight: number
650
+ strokeAlign: Stroke['align']
651
+
516
652
  strokeMiterLimit: number
517
653
 
518
654
  minWidth: number | null
519
655
  maxWidth: number | null
520
656
  minHeight: number | null
521
657
  maxHeight: number | null
658
+ /**
659
+ * The size whose proportions the layer keeps, as Figma stores it: the width and height it had
660
+ * when the ratio was locked, not a reduced ratio. Null leaves the proportions free.
661
+ */
662
+ targetAspectRatio: Vector | null
522
663
 
523
664
  isMask: boolean
524
665
  maskType: MaskType
@@ -556,11 +697,22 @@ export interface SceneNode {
556
697
  librarySource: LibraryAssetSource | null
557
698
  isPublishable: boolean
558
699
  isSymbolPublishable: boolean
700
+ /**
701
+ * On an instance inside a component: its properties show on instances of that component,
702
+ * as Figma's `isExposedInstance`.
703
+ */
704
+ isExposedInstance: boolean
559
705
  symbolDescription: string
560
706
  symbolLinks: SymbolLink[]
561
707
  variantPropSpecs: VariantPropSpec[]
562
708
 
563
709
  boundVariables: Record<string, string>
710
+ /** Multipliers from bound numeric values to this occurrence's scene units. */
711
+ variableBindingScales: Partial<Record<string, number>>
712
+ /** Numeric units for new declarations owned by this node's occurrence scope. */
713
+ variableAssignmentScales: Partial<Record<string, number>>
714
+ /** Explicit coordinate scale relative to the containing component definition. */
715
+ componentScale: number
564
716
  variableModes: VariableModeMap
565
717
  exportSettings: ExportSetting[]
566
718
 
@@ -580,15 +732,30 @@ export interface SceneNode {
580
732
  textPathBox: Rect | null
581
733
  }
582
734
 
583
- export type ComponentPropertyType = 'VARIANT' | 'TEXT' | 'BOOLEAN' | 'INSTANCE_SWAP'
735
+ export type ComponentPropertyType = 'VARIANT' | 'TEXT' | 'BOOLEAN' | 'INSTANCE_SWAP' | 'SLOT'
584
736
 
585
- export type ComponentPropertyReferenceField = 'VISIBLE' | 'TEXT' | 'INSTANCE_SWAP'
737
+ /** `SLOT_CONTENT` marks a frame whose children are the slot's content. */
738
+ export type ComponentPropertyReferenceField = 'VISIBLE' | 'TEXT' | 'INSTANCE_SWAP' | 'SLOT_CONTENT'
586
739
 
587
740
  export interface ComponentPropertyReference {
588
741
  propertyId: string
589
742
  field: ComponentPropertyReferenceField
590
743
  }
591
744
 
745
+ /** Guidance for a slot property; Figma reports a breach but still accepts the content. */
746
+ export interface SlotSettings {
747
+ minChildren?: number
748
+ maxChildren?: number
749
+ allowPreferredValuesOnly: boolean
750
+ displayEmptyByDefault: boolean
751
+ /** Content added to the slot fills its counter axis. */
752
+ stretchChildOnInsert: boolean
753
+ }
754
+
755
+ /**
756
+ * For a `SLOT` property, an instance assignment (whatever its value) means the instance owns
757
+ * that slot's content: the slot frame's children in the instance, which component sync keeps.
758
+ */
592
759
  export interface ComponentPropertyDefinition {
593
760
  id: string
594
761
  name: string
@@ -596,12 +763,58 @@ export interface ComponentPropertyDefinition {
596
763
  defaultValue: string
597
764
  variantOptions?: string[]
598
765
  preferredValues?: string[]
766
+ description?: string
767
+ slotSettings?: SlotSettings
599
768
  }
600
769
 
601
770
  export type VariableType = 'COLOR' | 'FLOAT' | 'STRING' | 'BOOLEAN'
602
771
  export type VariableValue = Color | number | string | boolean | { aliasId: string }
603
772
  export type VariableModeMap = Record<string, string>
604
773
 
774
+ /** Property pickers a variable is offered in, as Figma names them. */
775
+ export const VARIABLE_SCOPES = [
776
+ 'ALL_SCOPES',
777
+ 'TEXT_CONTENT',
778
+ 'CORNER_RADIUS',
779
+ 'WIDTH_HEIGHT',
780
+ 'GAP',
781
+ 'ALL_FILLS',
782
+ 'FRAME_FILL',
783
+ 'SHAPE_FILL',
784
+ 'TEXT_FILL',
785
+ 'STROKE',
786
+ 'STROKE_FLOAT',
787
+ 'EFFECT_FLOAT',
788
+ 'EFFECT_COLOR',
789
+ 'OPACITY',
790
+ 'FONT_STYLE',
791
+ 'FONT_FAMILY',
792
+ 'FONT_SIZE',
793
+ 'LINE_HEIGHT',
794
+ 'LETTER_SPACING',
795
+ 'PARAGRAPH_SPACING',
796
+ 'PARAGRAPH_INDENT',
797
+ 'FONT_VARIATIONS',
798
+ 'TRANSFORM'
799
+ ] as const
800
+ export type VariableScope = (typeof VARIABLE_SCOPES)[number]
801
+
802
+ export const CODE_SYNTAX_PLATFORMS = ['WEB', 'ANDROID', 'iOS'] as const
803
+ export type CodeSyntaxPlatform = (typeof CODE_SYNTAX_PLATFORMS)[number]
804
+
805
+ /**
806
+ * The CSS unit a numeric token is written in. Lengths (`px`, `rem`) stay in canvas pixels in
807
+ * the document and convert only when written as CSS; the other units store the number as written.
808
+ */
809
+ export const TOKEN_UNITS = ['none', 'px', 'rem', '%', 'ms', 's', 'deg'] as const
810
+ export type TokenUnit = (typeof TOKEN_UNITS)[number]
811
+
812
+ /** A mode value authored as raw CSS. `resolved` is the number the canvas drew for it. */
813
+ export interface TokenExpression {
814
+ css: string
815
+ resolved: number
816
+ }
817
+
605
818
  export interface Variable {
606
819
  id: string
607
820
  name: string
@@ -610,6 +823,18 @@ export interface Variable {
610
823
  valuesByMode: Record<string, VariableValue>
611
824
  description: string
612
825
  hiddenFromPublishing: boolean
826
+ /** Absent means every scope. */
827
+ scopes?: VariableScope[]
828
+ /**
829
+ * Per-platform code snippets, as Figma's Dev Mode shows them. A `WEB` snippet of `--x` or
830
+ * `var(--x)` names the token's CSS custom property; otherwise the name is derived.
831
+ */
832
+ codeSyntax?: Partial<Record<CodeSyntaxPlatform, string>>
833
+ /** FLOAT only. Absent means inferred when written as CSS. */
834
+ unit?: TokenUnit
835
+ /** Raw CSS by mode id, for values a number cannot express (`clamp()`, `calc()`). */
836
+ expressions?: Record<string, TokenExpression>
837
+ pluginData?: PluginDataEntry[]
613
838
  /** Published library key (from NodeChange.key). Used for assetRef resolution in colorVar. */
614
839
  key?: string
615
840
  /** Published library version (from NodeChange.version). Used for assetRef resolution in colorVar. */
@@ -624,12 +849,31 @@ export type NumericNodeProperty = {
624
849
  export interface VariableCollectionMode {
625
850
  modeId: string
626
851
  name: string
852
+ /**
853
+ * Where the mode applies in CSS: a selector (`[data-theme="dark"]`, `.compact`) or an
854
+ * at-rule prelude (`@media (max-width: 640px)`). Absent means the default for its axis.
855
+ */
856
+ condition?: string
627
857
  }
628
858
 
629
859
  export interface VariableCollection {
630
860
  id: string
631
861
  name: string
862
+ /** The default mode comes first, as in Figma. */
632
863
  modes: VariableCollectionMode[]
633
864
  defaultModeId: string
634
865
  variableIds: string[]
866
+ /**
867
+ * The attribute that switches a mode with no condition of its own, `data-theme` for
868
+ * `[data-theme="dark"]`. Absent means one named after the collection.
869
+ */
870
+ modeAttribute?: string
871
+ pluginData?: PluginDataEntry[]
872
+ }
873
+
874
+ /** An HTML attribute name a stylesheet can select on unescaped: `data-theme`, `theme`. */
875
+ export const MODE_ATTRIBUTE_PATTERN = /^[A-Za-z_][\w-]*$/
876
+
877
+ export function isModeAttributeName(name: string): boolean {
878
+ return name.length <= 100 && MODE_ATTRIBUTE_PATTERN.test(name)
635
879
  }
package/src/undo.ts CHANGED
@@ -17,12 +17,14 @@ interface UndoBatch {
17
17
  coalesceKey?: string
18
18
  }
19
19
 
20
- const DEFAULT_HISTORY_LIMIT = 200
20
+ /** How many edits undo keeps by default. */
21
+ export const DEFAULT_HISTORY_LIMIT = 200
21
22
 
22
23
  export class UndoManager {
23
24
  private undoStack: UndoEntry[] = []
24
25
  private redoStack: UndoEntry[] = []
25
26
  private batches: UndoBatch[] = []
27
+ private readonly settlers = new Set<() => void>()
26
28
  private readonly limit: number
27
29
  private readonly onChange: (() => void) | undefined
28
30
 
@@ -53,7 +55,31 @@ export class UndoManager {
53
55
  this.pushUndoEntry(entry)
54
56
  }
55
57
 
58
+ /**
59
+ * Records an entry as a step of its own, outside the batches being built, such as an agent's
60
+ * edit made while a colour picker is open. Batches settle first, so the step follows the
61
+ * changes they hold so far.
62
+ */
63
+ pushStep(entry: UndoEntry): void {
64
+ this.settle()
65
+ this.pushUndoEntry(entry)
66
+ }
67
+
68
+ /**
69
+ * Registers a hook that commits an edit still being coalesced, such as a run of picker changes.
70
+ * Undo and redo run every hook first, so they act on the newest change.
71
+ */
72
+ onBeforeHistory(settle: () => void): () => void {
73
+ this.settlers.add(settle)
74
+ return () => this.settlers.delete(settle)
75
+ }
76
+
77
+ private settle() {
78
+ for (const settle of this.settlers) settle()
79
+ }
80
+
56
81
  undo(): string | null {
82
+ this.settle()
57
83
  const entry = this.undoStack.pop()
58
84
  if (!entry) return null
59
85
  entry.inverse()
@@ -63,6 +89,7 @@ export class UndoManager {
63
89
  }
64
90
 
65
91
  redo(): string | null {
92
+ this.settle()
66
93
  const entry = this.redoStack.pop()
67
94
  if (!entry) return null
68
95
  entry.forward()
@@ -119,14 +146,39 @@ export class UndoManager {
119
146
  return this.batches.length > 0
120
147
  }
121
148
 
149
+ /** Committed history, or changes in an open batch that a settling hook commits first. */
122
150
  get canUndo(): boolean {
123
- return this.undoStack.length > 0
151
+ if (this.undoStack.length > 0) return true
152
+ return this.settlers.size > 0 && this.batches.some((batch) => batch.entries.length > 0)
124
153
  }
125
154
 
126
155
  get canRedo(): boolean {
127
156
  return this.redoStack.length > 0
128
157
  }
129
158
 
159
+ /**
160
+ * The entry `depth` steps below the top of the undo stack, so a caller that pushed entries
161
+ * can tell whether they are still the most recent ones.
162
+ */
163
+ peekUndo(depth = 0): UndoEntry | undefined {
164
+ return this.undoStack.at(-1 - depth)
165
+ }
166
+
167
+ /** The entry `depth` steps below the top of the redo stack, the next one Redo applies at 0. */
168
+ peekRedo(depth = 0): UndoEntry | undefined {
169
+ return this.redoStack.at(-1 - depth)
170
+ }
171
+
172
+ /**
173
+ * Whether `entry` is applied (on the undo stack) or undone (on the redo stack); null once it
174
+ * left the history, trimmed or dropped when an edit cleared Redo.
175
+ */
176
+ stateOf(entry: UndoEntry): 'applied' | 'undone' | null {
177
+ if (this.undoStack.includes(entry)) return 'applied'
178
+ if (this.redoStack.includes(entry)) return 'undone'
179
+ return null
180
+ }
181
+
130
182
  get undoLabel(): string | null {
131
183
  return this.undoStack.at(-1)?.label ?? null
132
184
  }