@pairdraw/scene-graph 0.19.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/dist/behaviours/index.d.ts +2 -2
  2. package/dist/behaviours/index.js +2 -2
  3. package/dist/behaviours/spec.d.ts +6 -1
  4. package/dist/behaviours/spec.d.ts.map +1 -1
  5. package/dist/behaviours/spec.js +62 -5
  6. package/dist/behaviours/spec.js.map +1 -1
  7. package/dist/canvas-layout.js +66 -0
  8. package/dist/canvas-layout.js.map +1 -0
  9. package/dist/components/index.d.ts +2 -2
  10. package/dist/components/index.js +2 -2
  11. package/dist/components/variants.d.ts +12 -1
  12. package/dist/components/variants.d.ts.map +1 -1
  13. package/dist/components/variants.js +30 -1
  14. package/dist/components/variants.js.map +1 -1
  15. package/dist/constants.d.ts +25 -1
  16. package/dist/constants.d.ts.map +1 -1
  17. package/dist/constants.js +94 -1
  18. package/dist/constants.js.map +1 -1
  19. package/dist/coordinate.js +1 -1
  20. package/dist/copy.d.ts.map +1 -1
  21. package/dist/copy.js +17 -1
  22. package/dist/copy.js.map +1 -1
  23. package/dist/css/index.d.ts +2 -2
  24. package/dist/css/index.js +2 -2
  25. package/dist/css/text.d.ts +22 -1
  26. package/dist/css/text.d.ts.map +1 -1
  27. package/dist/css/text.js +81 -1
  28. package/dist/css/text.js.map +1 -1
  29. package/dist/design-memory/index.d.ts +3 -0
  30. package/dist/design-memory/index.js +3 -0
  31. package/dist/design-memory/model.d.ts +33 -0
  32. package/dist/design-memory/model.d.ts.map +1 -0
  33. package/dist/design-memory/model.js +142 -0
  34. package/dist/design-memory/model.js.map +1 -0
  35. package/dist/design-memory/schema.d.ts +76 -0
  36. package/dist/design-memory/schema.d.ts.map +1 -0
  37. package/dist/design-memory/schema.js +72 -0
  38. package/dist/design-memory/schema.js.map +1 -0
  39. package/dist/effects.d.ts +7 -4
  40. package/dist/effects.d.ts.map +1 -1
  41. package/dist/effects.js +9 -4
  42. package/dist/effects.js.map +1 -1
  43. package/dist/empty-values.js +93 -0
  44. package/dist/empty-values.js.map +1 -0
  45. package/dist/fields/components.d.ts +9 -0
  46. package/dist/fields/components.d.ts.map +1 -0
  47. package/dist/fields/components.js +24 -0
  48. package/dist/fields/components.js.map +1 -0
  49. package/dist/fields/index.d.ts +3 -2
  50. package/dist/fields/index.js +3 -2
  51. package/dist/fields/layout.d.ts +3 -1
  52. package/dist/fields/layout.d.ts.map +1 -1
  53. package/dist/fields/layout.js +10 -1
  54. package/dist/fields/layout.js.map +1 -1
  55. package/dist/generated/index.d.ts +2 -2
  56. package/dist/generated/index.js +2 -2
  57. package/dist/generated/shape-with-text.d.ts +7 -2
  58. package/dist/generated/shape-with-text.d.ts.map +1 -1
  59. package/dist/generated/shape-with-text.js +20 -3
  60. package/dist/generated/shape-with-text.js.map +1 -1
  61. package/dist/graph.d.ts +8 -1
  62. package/dist/graph.d.ts.map +1 -1
  63. package/dist/graph.js +36 -7
  64. package/dist/graph.js.map +1 -1
  65. package/dist/grid-values.d.ts +10 -0
  66. package/dist/grid-values.d.ts.map +1 -0
  67. package/dist/grid-values.js +53 -0
  68. package/dist/grid-values.js.map +1 -0
  69. package/dist/group-bounds.d.ts +4 -1
  70. package/dist/group-bounds.d.ts.map +1 -1
  71. package/dist/group-bounds.js +11 -1
  72. package/dist/group-bounds.js.map +1 -1
  73. package/dist/hit-test/drop.d.ts +20 -0
  74. package/dist/hit-test/drop.d.ts.map +1 -0
  75. package/dist/hit-test/drop.js +54 -0
  76. package/dist/hit-test/drop.js.map +1 -0
  77. package/dist/hit-test/geometry.js +86 -0
  78. package/dist/hit-test/geometry.js.map +1 -0
  79. package/dist/hit-test/masks.d.ts +14 -0
  80. package/dist/hit-test/masks.d.ts.map +1 -0
  81. package/dist/hit-test/masks.js +41 -0
  82. package/dist/hit-test/masks.js.map +1 -0
  83. package/dist/hit-test/paint.d.ts +6 -0
  84. package/dist/hit-test/paint.d.ts.map +1 -0
  85. package/dist/hit-test/paint.js +19 -0
  86. package/dist/hit-test/paint.js.map +1 -0
  87. package/dist/hit-test.d.ts +4 -15
  88. package/dist/hit-test.d.ts.map +1 -1
  89. package/dist/hit-test.js +33 -178
  90. package/dist/hit-test.js.map +1 -1
  91. package/dist/index.d.ts +2 -2
  92. package/dist/instances/fields.d.ts.map +1 -1
  93. package/dist/instances/fields.js +2 -0
  94. package/dist/instances/fields.js.map +1 -1
  95. package/dist/instances/index.d.ts +2 -1
  96. package/dist/instances/index.d.ts.map +1 -1
  97. package/dist/instances/index.js +1 -1
  98. package/dist/instances/index.js.map +1 -1
  99. package/dist/instances/source-walk.d.ts +12 -0
  100. package/dist/instances/source-walk.d.ts.map +1 -0
  101. package/dist/node-defaults.d.ts +21 -1
  102. package/dist/node-defaults.d.ts.map +1 -1
  103. package/dist/node-defaults.js +63 -50
  104. package/dist/node-defaults.js.map +1 -1
  105. package/dist/plugin-data/fields.d.ts +75 -0
  106. package/dist/plugin-data/fields.d.ts.map +1 -1
  107. package/dist/plugin-data/fields.js +18 -1
  108. package/dist/plugin-data/fields.js.map +1 -1
  109. package/dist/preview.d.ts.map +1 -1
  110. package/dist/preview.js +2 -0
  111. package/dist/preview.js.map +1 -1
  112. package/dist/resize.d.ts.map +1 -1
  113. package/dist/resize.js +3 -6
  114. package/dist/resize.js.map +1 -1
  115. package/dist/scaling/node.d.ts.map +1 -1
  116. package/dist/scaling/node.js +28 -4
  117. package/dist/scaling/node.js.map +1 -1
  118. package/dist/shared-styles.d.ts +3 -1
  119. package/dist/shared-styles.d.ts.map +1 -1
  120. package/dist/shared-styles.js +7 -1
  121. package/dist/shared-styles.js.map +1 -1
  122. package/dist/text/path.js +11 -0
  123. package/dist/text/path.js.map +1 -0
  124. package/dist/text/picture.d.ts.map +1 -1
  125. package/dist/text/picture.js +5 -5
  126. package/dist/text/picture.js.map +1 -1
  127. package/dist/token-sources/document.d.ts +26 -0
  128. package/dist/token-sources/document.d.ts.map +1 -0
  129. package/dist/token-sources/document.js +80 -0
  130. package/dist/token-sources/document.js.map +1 -0
  131. package/dist/token-sources/index.d.ts +3 -0
  132. package/dist/token-sources/index.js +3 -0
  133. package/dist/token-sources/schema.d.ts +43 -0
  134. package/dist/token-sources/schema.d.ts.map +1 -0
  135. package/dist/token-sources/schema.js +50 -0
  136. package/dist/token-sources/schema.js.map +1 -0
  137. package/dist/transfer/copied.d.ts +14 -0
  138. package/dist/transfer/copied.d.ts.map +1 -0
  139. package/dist/transfer/copied.js +52 -0
  140. package/dist/transfer/copied.js.map +1 -0
  141. package/dist/transfer/index.d.ts +2 -1
  142. package/dist/transfer/index.js +2 -1
  143. package/dist/transfer/nodes.js +7 -0
  144. package/dist/transfer/nodes.js.map +1 -1
  145. package/dist/types.d.ts +104 -15
  146. package/dist/types.d.ts.map +1 -1
  147. package/dist/types.js.map +1 -1
  148. package/dist/undo.d.ts +6 -0
  149. package/dist/undo.d.ts.map +1 -1
  150. package/dist/undo.js +13 -0
  151. package/dist/undo.js.map +1 -1
  152. package/dist/variables/index.d.ts.map +1 -1
  153. package/dist/variables/index.js.map +1 -1
  154. package/dist/variations/copy.d.ts +16 -0
  155. package/dist/variations/copy.d.ts.map +1 -0
  156. package/dist/variations/copy.js +78 -0
  157. package/dist/variations/copy.js.map +1 -0
  158. package/dist/variations/index.d.ts +6 -0
  159. package/dist/variations/index.js +6 -0
  160. package/dist/variations/match.d.ts +23 -0
  161. package/dist/variations/match.d.ts.map +1 -0
  162. package/dist/variations/match.js +80 -0
  163. package/dist/variations/match.js.map +1 -0
  164. package/dist/variations/model.d.ts +39 -0
  165. package/dist/variations/model.d.ts.map +1 -0
  166. package/dist/variations/model.js +90 -0
  167. package/dist/variations/model.js.map +1 -0
  168. package/dist/variations/pick.d.ts +43 -0
  169. package/dist/variations/pick.d.ts.map +1 -0
  170. package/dist/variations/pick.js +211 -0
  171. package/dist/variations/pick.js.map +1 -0
  172. package/dist/variations/schema.d.ts +27 -0
  173. package/dist/variations/schema.d.ts.map +1 -0
  174. package/dist/variations/schema.js +27 -0
  175. package/dist/variations/schema.js.map +1 -0
  176. package/package.json +21 -1
  177. package/src/behaviours/spec.ts +94 -4
  178. package/src/canvas-layout.ts +76 -0
  179. package/src/components/variants.ts +39 -0
  180. package/src/constants.ts +75 -0
  181. package/src/copy.ts +20 -1
  182. package/src/css/index.ts +12 -1
  183. package/src/css/text.ts +86 -0
  184. package/src/design-memory/index.ts +2 -0
  185. package/src/design-memory/model.ts +143 -0
  186. package/src/design-memory/schema.ts +84 -0
  187. package/src/effects.ts +16 -7
  188. package/src/empty-values.ts +101 -0
  189. package/src/fields/components.ts +21 -0
  190. package/src/fields/index.ts +1 -0
  191. package/src/fields/layout.ts +10 -0
  192. package/src/generated/shape-with-text.ts +28 -2
  193. package/src/graph.ts +48 -6
  194. package/src/grid-values.ts +47 -0
  195. package/src/group-bounds.ts +13 -0
  196. package/src/hit-test/drop.ts +90 -0
  197. package/src/hit-test/geometry.ts +131 -0
  198. package/src/hit-test/masks.ts +57 -0
  199. package/src/hit-test/paint.ts +24 -0
  200. package/src/hit-test.ts +78 -291
  201. package/src/instances/fields.ts +2 -0
  202. package/src/instances/index.ts +1 -0
  203. package/src/node-defaults.ts +71 -52
  204. package/src/plugin-data/fields.ts +18 -1
  205. package/src/preview.ts +2 -0
  206. package/src/resize.ts +3 -14
  207. package/src/scaling/node.ts +34 -5
  208. package/src/shared-styles.ts +8 -0
  209. package/src/text/path.ts +14 -0
  210. package/src/text/picture.ts +5 -2
  211. package/src/token-sources/document.ts +104 -0
  212. package/src/token-sources/index.ts +2 -0
  213. package/src/token-sources/schema.ts +59 -0
  214. package/src/transfer/copied.ts +72 -0
  215. package/src/transfer/index.ts +1 -0
  216. package/src/transfer/nodes.ts +10 -0
  217. package/src/types.ts +117 -14
  218. package/src/undo.ts +11 -0
  219. package/src/variables/index.ts +1 -4
  220. package/src/variations/copy.ts +89 -0
  221. package/src/variations/index.ts +5 -0
  222. package/src/variations/match.ts +100 -0
  223. package/src/variations/model.ts +132 -0
  224. package/src/variations/pick.ts +293 -0
  225. package/src/variations/schema.ts +29 -0
@@ -5,6 +5,7 @@ import {
5
5
  DEFAULT_STROKE_WEIGHT
6
6
  } from './constants'
7
7
  import { normalizeEffects } from './effects'
8
+ import { EMPTY_LIST, EMPTY_RECORD, shareEmptyNodeValues } from './empty-values'
8
9
  import { createInstanceOverrideState } from './instances/overrides'
9
10
  import { NO_LETTER_SPACING } from './text/lengths'
10
11
  import type { NodeType, SceneNode, SourceMetadata } from './types'
@@ -14,18 +15,19 @@ export function createDefaultSourceMetadata(): SourceMetadata {
14
15
  format: null,
15
16
  id: null,
16
17
  orderKey: null,
17
- editedFields: [],
18
+ editedFields: EMPTY_LIST,
18
19
  fig: {
19
20
  rawSize: null,
20
21
  rawTransform: null,
21
- rawNodeFields: {},
22
+ rawNodeFields: EMPTY_RECORD,
22
23
  layout: null,
23
- symbolOverrides: [],
24
- componentPropAssignments: [],
25
- derivedSymbolData: [],
24
+ symbolOverrides: EMPTY_LIST,
25
+ componentPropAssignments: EMPTY_LIST,
26
+ derivedSymbolData: EMPTY_LIST,
26
27
  derivedSymbolDataLayoutVersion: null,
27
28
  uniformScaleFactor: null
28
- }
29
+ },
30
+ drawio: null
29
31
  }
30
32
  }
31
33
 
@@ -74,12 +76,36 @@ export function textLayerName(type: NodeType, text: string): string {
74
76
  return lines.join(' ') || defaultNodeName(type)
75
77
  }
76
78
 
79
+ /**
80
+ * The fields auto layout sets on the frame that holds it, with the value each holds when no frame
81
+ * lays anything out. A page holds layers; it is not one, so none of these apply to it and a page
82
+ * that took them would place its layers by a container nobody can see.
83
+ */
84
+ export const FRAME_LAYOUT_DEFAULTS = {
85
+ layoutMode: 'NONE',
86
+ layoutDirection: 'AUTO',
87
+ layoutWrap: 'NO_WRAP',
88
+ primaryAxisAlign: 'MIN',
89
+ counterAxisAlign: 'MIN',
90
+ primaryAxisSizing: 'FIXED',
91
+ counterAxisSizing: 'FIXED',
92
+ itemSpacing: 0,
93
+ counterAxisSpacing: 0,
94
+ paddingTop: 0,
95
+ paddingRight: 0,
96
+ paddingBottom: 0,
97
+ paddingLeft: 0
98
+ } as const satisfies Partial<SceneNode>
99
+
77
100
  export function createDefaultNode(
78
101
  generateId: () => string,
79
102
  type: NodeType,
80
103
  overrides: Partial<SceneNode> = {}
81
104
  ): SceneNode {
82
- const node = {
105
+ // A literal ending in a spread is built one field at a time, and V8 keeps an object given this
106
+ // many fields that way in dictionary mode, about 3 KB more per layer; a spread copy of a
107
+ // finished literal keeps fast fields, in JavaScriptCore as well.
108
+ const defaults = {
83
109
  id: generateId(),
84
110
  type,
85
111
  name: type.charAt(0) + type.slice(1).toLowerCase(),
@@ -93,11 +119,13 @@ export function createDefaultNode(
93
119
  source: createDefaultSourceMetadata(),
94
120
  derivedLayout: null,
95
121
  fills:
96
- type === 'TEXT' ? [{ type: 'SOLID' as const, color: BLACK, opacity: 1, visible: true }] : [],
97
- strokes: [],
98
- effects: [],
99
- layoutGrids: [],
100
- guides: [],
122
+ type === 'TEXT'
123
+ ? [{ type: 'SOLID' as const, color: BLACK, opacity: 1, visible: true }]
124
+ : EMPTY_LIST,
125
+ strokes: EMPTY_LIST,
126
+ effects: EMPTY_LIST,
127
+ layoutGrids: EMPTY_LIST,
128
+ guides: EMPTY_LIST,
101
129
  fillStyleId: null,
102
130
  strokeStyleId: null,
103
131
  textStyleId: null,
@@ -115,6 +143,8 @@ export function createDefaultNode(
115
143
  visible: true,
116
144
  locked: false,
117
145
  clipsContent: false,
146
+ overflowDirection: 'NONE',
147
+ scrollBehavior: 'SCROLLS',
118
148
  text: '',
119
149
  fontSize: 14,
120
150
  fontFamily: DEFAULT_FONT_FAMILY,
@@ -127,27 +157,15 @@ export function createDefaultNode(
127
157
  leadingTrim: 'NONE',
128
158
  lineHeight: null,
129
159
  letterSpacing: NO_LETTER_SPACING,
130
- layoutMode: 'NONE',
131
- layoutDirection: 'AUTO',
132
- layoutWrap: 'NO_WRAP',
133
- primaryAxisAlign: 'MIN',
134
- counterAxisAlign: 'MIN',
135
- primaryAxisSizing: 'FIXED',
136
- counterAxisSizing: 'FIXED',
137
- itemSpacing: 0,
138
- counterAxisSpacing: 0,
139
- paddingTop: 0,
140
- paddingRight: 0,
141
- paddingBottom: 0,
142
- paddingLeft: 0,
160
+ ...FRAME_LAYOUT_DEFAULTS,
143
161
  blendMode: 'PASS_THROUGH',
144
162
  layoutPositioning: 'AUTO',
145
163
  layoutGrow: 0,
146
164
  layoutAlignSelf: 'AUTO',
147
165
  vectorNetwork: null,
148
166
  handleMirroring: 'NONE',
149
- fillGeometry: [],
150
- strokeGeometry: [],
167
+ fillGeometry: EMPTY_LIST,
168
+ strokeGeometry: EMPTY_LIST,
151
169
  arcData: null,
152
170
  textAlignVertical: 'TOP',
153
171
  textAutoResize: 'NONE',
@@ -155,24 +173,24 @@ export function createDefaultNode(
155
173
  textDecoration: 'NONE',
156
174
  textDecorationStyle: 'SOLID',
157
175
  textDecorationThickness: null,
158
- textDecorationFills: [],
176
+ textDecorationFills: EMPTY_LIST,
159
177
  textDecorationSkipInk: true,
160
178
  textUnderlineOffset: null,
161
179
  maxLines: null,
162
- textParagraphs: [],
180
+ textParagraphs: EMPTY_LIST,
163
181
  listSpacing: 0,
164
182
  paragraphSpacing: 0,
165
183
  paragraphIndent: 0,
166
184
  hangingList: false,
167
- styleRuns: [],
168
- fontVariations: [],
185
+ styleRuns: EMPTY_LIST,
186
+ fontVariations: EMPTY_LIST,
169
187
  opticalSizeFromFontSize: true,
170
- fontFeatures: [],
188
+ fontFeatures: EMPTY_LIST,
171
189
  horizontalConstraint: 'MIN',
172
190
  verticalConstraint: 'MIN',
173
191
  strokeCap: 'NONE',
174
192
  strokeJoin: 'MITER',
175
- dashPattern: [],
193
+ dashPattern: EMPTY_LIST,
176
194
  borderTopWeight: 0,
177
195
  borderRightWeight: 0,
178
196
  borderBottomWeight: 0,
@@ -191,14 +209,14 @@ export function createDefaultNode(
191
209
  connectorLineType: 'ELBOWED',
192
210
  connectorStartCap: 'ROUND',
193
211
  connectorEndCap: 'ARROW_LINES',
194
- connectorControlPoints: [],
212
+ connectorControlPoints: EMPTY_LIST,
195
213
  connectorCurveHandles: null,
196
214
  connectorLabelPosition: null,
197
215
  isMask: false,
198
216
  maskType: 'ALPHA',
199
217
  maskIsOutline: false,
200
- gridTemplateColumns: [],
201
- gridTemplateRows: [],
218
+ gridTemplateColumns: EMPTY_LIST,
219
+ gridTemplateRows: EMPTY_LIST,
202
220
  gridColumnGap: 0,
203
221
  gridRowGap: 0,
204
222
  gridPosition: null,
@@ -215,10 +233,10 @@ export function createDefaultNode(
215
233
  shapeTruncates: false,
216
234
  componentId: null,
217
235
  instanceOverrides: createInstanceOverrideState(),
218
- componentPropertyDefinitions: [],
219
- componentPropertyReferences: [],
220
- componentPropertyAssignments: {},
221
- componentPropertyValues: {},
236
+ componentPropertyDefinitions: EMPTY_LIST,
237
+ componentPropertyReferences: EMPTY_LIST,
238
+ componentPropertyAssignments: EMPTY_RECORD,
239
+ componentPropertyValues: EMPTY_RECORD,
222
240
  componentKey: null,
223
241
  sourceLibraryKey: null,
224
242
  publishId: null,
@@ -230,18 +248,18 @@ export function createDefaultNode(
230
248
  isSymbolPublishable: false,
231
249
  isExposedInstance: false,
232
250
  symbolDescription: '',
233
- symbolLinks: [],
234
- variantPropSpecs: [],
235
- boundVariables: {},
236
- variableBindingScales: {},
237
- variableAssignmentScales: {},
251
+ symbolLinks: EMPTY_LIST,
252
+ variantPropSpecs: EMPTY_LIST,
253
+ boundVariables: EMPTY_RECORD,
254
+ variableBindingScales: EMPTY_RECORD,
255
+ variableAssignmentScales: EMPTY_RECORD,
238
256
  componentScale: 1,
239
- variableModes: {},
240
- exportSettings: [],
257
+ variableModes: EMPTY_RECORD,
258
+ exportSettings: EMPTY_LIST,
241
259
  status: null,
242
- pluginData: [],
243
- pluginRelaunchData: [],
244
- prototypeReactions: [],
260
+ pluginData: EMPTY_LIST,
261
+ pluginRelaunchData: EMPTY_LIST,
262
+ prototypeReactions: EMPTY_LIST,
245
263
  internalOnly: false,
246
264
  flipX: false,
247
265
  flipY: false,
@@ -250,10 +268,11 @@ export function createDefaultNode(
250
268
  derivedTextHeight: null,
251
269
  textPathData: null,
252
270
  textPathBox: null,
253
- booleanOperation: undefined,
254
- ...overrides
271
+ booleanOperation: undefined
255
272
  } satisfies CompleteNodeFields
273
+ const node: SceneNode = { ...defaults, ...overrides }
256
274
  node.effects = normalizeEffects(node.effects)
275
+ shareEmptyNodeValues(node, Object.keys(overrides))
257
276
  return node
258
277
  }
259
278
 
@@ -5,6 +5,7 @@ import { behaviourSchema } from '../behaviours/schema'
5
5
  import { browserNodeSchema } from '../browser-node/schema'
6
6
  import type { OkHCLPayload } from '../color/okhcl'
7
7
  import { commentThreadsSchema } from '../comments/schema'
8
+ import { designMemorySchema } from '../design-memory/schema'
8
9
  import { clampExportQuality, isExportFormatId, type ExportFormatId } from '../export-format'
9
10
  import { clampExportScale } from '../export-scale'
10
11
  import { FIG_COMPRESSIONS } from '../fig-compression'
@@ -13,6 +14,7 @@ import type { Color, Rect, Vector } from '../primitives'
13
14
  import { scriptNodeSchema } from '../script/schema'
14
15
  import { shaderPaintSchema } from '../shaders/schema'
15
16
  import { nodeStatusSchema, statusWorkflowSchema } from '../statuses/schema'
17
+ import { tokenSourcesSchema } from '../token-sources/schema'
16
18
  import {
17
19
  MODE_ATTRIBUTE_PATTERN,
18
20
  TOKEN_UNITS,
@@ -25,6 +27,7 @@ import {
25
27
  type TextDirection,
26
28
  type TokenUnit
27
29
  } from '../types'
30
+ import { variationSetSchema, variationStandInSchema } from '../variations/schema'
28
31
  import {
29
32
  jsonPluginDataField,
30
33
  textPluginDataField,
@@ -249,6 +252,10 @@ export const PAIRDRAW_PLUGIN_DATA = {
249
252
  comments: jsonPluginDataField('comments', 'bookkeeping', commentThreadsSchema),
250
253
  /** The statuses layers can be in, on the DOCUMENT node. */
251
254
  statuses: jsonPluginDataField('statuses', 'bookkeeping', statusWorkflowSchema),
255
+ /** The code the document's variables are linked to, on the DOCUMENT node. */
256
+ tokenSources: jsonPluginDataField('tokenSources', 'bookkeeping', tokenSourcesSchema),
257
+ /** The design rules agents follow in the document, on the DOCUMENT node. */
258
+ designMemory: jsonPluginDataField('designMemory', 'bookkeeping', designMemorySchema),
252
259
  /** How tightly the document's `.fig` data is compressed, on the DOCUMENT node. */
253
260
  figCompression: textPluginDataField('figCompression', 'bookkeeping', FIG_COMPRESSIONS),
254
261
  /** A layer's status, any state of the workflow; Figma's `sectionStatusInfo` knows only two. */
@@ -289,7 +296,17 @@ export const PAIRDRAW_PLUGIN_DATA = {
289
296
  * A snapshot of one page: its address, viewport, and when it was refreshed. The picture is
290
297
  * the layer's image fill. An address that is not an http or https page does not decode.
291
298
  */
292
- browserNode: jsonPluginDataField('browserNode', 'content', browserNodeSchema)
299
+ browserNode: jsonPluginDataField('browserNode', 'content', browserNodeSchema),
300
+ /**
301
+ * On a section, that its children are variations of another layer: which one, and how that
302
+ * layer looked when they were made. A copy of the section is an ordinary section.
303
+ */
304
+ variationSet: jsonPluginDataField('variationSet', 'bookkeeping', variationSetSchema),
305
+ /**
306
+ * On a layer of a version, the main component or component set of the original it stands in
307
+ * for, so a pick keeps that component rather than pairing it by name.
308
+ */
309
+ variationStandIn: jsonPluginDataField('variationStandIn', 'bookkeeping', variationStandInSchema)
293
310
  } satisfies Record<string, PluginDataKey>
294
311
 
295
312
  const NOT_CONTENT = Object.values(PAIRDRAW_PLUGIN_DATA).filter((field) => field.role !== 'content')
package/src/preview.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { normalizeEffects } from './effects'
2
+ import { shareEmptyNodeValues } from './empty-values'
2
3
  import { textCacheInvalidationChanges } from './text/picture'
3
4
  import type { SceneNode } from './types'
4
5
  import { normalizeVectorNetwork } from './vector-network'
@@ -80,5 +81,6 @@ export function updateNodePreview(
80
81
  beforeUpdate?.(node, normalizedChanges)
81
82
  graph.positionPreviewVersion++
82
83
  Object.assign(node, normalizedChanges)
84
+ shareEmptyNodeValues(node, Object.keys(normalizedChanges))
83
85
  return normalizedChanges
84
86
  }
package/src/resize.ts CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  } from './copy'
8
8
  import { enforcedAspectRatio } from './layout/aspect-ratio'
9
9
  import type { Rect } from './primitives'
10
+ import { followsPath } from './text/path'
10
11
  import type { ConstraintType, DerivedTextGlyph, SceneNode, Stroke, VectorNetwork } from './types'
11
12
  import { cloneVectorNetwork } from './vector-network'
12
13
 
@@ -332,7 +333,7 @@ export function scaledGeometryChanges(
332
333
  Object.assign(changes, scaledPathTextChanges(orig, sx, sy))
333
334
  // Glyphs scaled as a picture take their strokes along; any other stroke keeps its weight, as
334
335
  // Figma reports it, even where only a scaled outline can draw it.
335
- if (orig.strokes.length > 0 && isScaledPathText(orig)) {
336
+ if (orig.strokes.length > 0 && followsPath(orig)) {
336
337
  changes.strokes = scaleStrokes(orig.strokes, sx, sy)
337
338
  }
338
339
  return changes
@@ -369,25 +370,13 @@ export function shapeGeometryChanges(
369
370
 
370
371
  type PathTextFields = Pick<SceneNode, 'derivedTextGlyphs' | 'textPathData' | 'textPathBox'>
371
372
 
372
- // Path text scales with its path, including baked path text that kept only rotated glyphs;
373
- // other text reflows in the new box, so its glyphs are dropped.
374
- function isScaledPathText(orig: PathTextFields): boolean {
375
- const glyphs = orig.derivedTextGlyphs ?? []
376
- return (
377
- glyphs.length > 0 &&
378
- (!!orig.textPathData ||
379
- !!orig.textPathBox ||
380
- glyphs.some((glyph) => (glyph.rotation ?? 0) !== 0))
381
- )
382
- }
383
-
384
373
  /** The baked glyphs and layout box of path text, scaled with the layer; nothing for other text. */
385
374
  export function scaledPathTextChanges(
386
375
  orig: PathTextFields,
387
376
  sx: number,
388
377
  sy: number
389
378
  ): Pick<Partial<SceneNode>, 'derivedTextGlyphs' | 'textPathBox'> {
390
- if (!isScaledPathText(orig)) return {}
379
+ if (!followsPath(orig)) return {}
391
380
  const changes: Pick<Partial<SceneNode>, 'derivedTextGlyphs' | 'textPathBox'> = {
392
381
  derivedTextGlyphs: scaleDerivedGlyphs(orig.derivedTextGlyphs, sx, sy)
393
382
  }
@@ -1,7 +1,7 @@
1
- import { scaleGeometryPaths } from '../copy'
2
- import { scaledPathTextChanges } from '../resize'
1
+ import { copyDerivedGlyphs, scaleGeometryPaths } from '../copy'
3
2
  import { scaledTextLength } from '../text/lengths'
4
3
  import { TEXT_PARAGRAPH_SPACING_FIELDS } from '../text/paragraphs'
4
+ import { followsPath } from '../text/path'
5
5
  import {
6
6
  type Effect,
7
7
  type Fill,
@@ -14,6 +14,29 @@ import {
14
14
  } from '../types'
15
15
  import { cloneVectorNetwork } from '../vector-network'
16
16
 
17
+ function scaledPathText(node: SceneNode, scale: number): Partial<SceneNode> {
18
+ if (!node.textPathData && !followsPath(node)) {
19
+ return {}
20
+ }
21
+ const box = node.textPathBox
22
+ return {
23
+ derivedTextGlyphs:
24
+ copyDerivedGlyphs(node.derivedTextGlyphs)?.map((glyph) => ({
25
+ ...glyph,
26
+ x: glyph.x * scale,
27
+ y: glyph.y * scale,
28
+ fontSize: glyph.fontSize * scale,
29
+ ...(glyph.advance === undefined ? {} : { advance: glyph.advance * scale })
30
+ })) ?? null,
31
+ textPathBox: box && {
32
+ x: box.x * scale,
33
+ y: box.y * scale,
34
+ width: box.width * scale,
35
+ height: box.height * scale
36
+ }
37
+ }
38
+ }
39
+
17
40
  function scaledOptional(value: number | null, scale: number): number | null {
18
41
  return value == null ? null : value * scale
19
42
  }
@@ -29,8 +52,13 @@ function scaledStrokes(strokes: readonly Stroke[], scale: number): Stroke[] {
29
52
  function scaledEffects(effects: readonly Effect[], scale: number): Effect[] {
30
53
  return effects.map((effect) => {
31
54
  const copy = structuredClone(effect)
32
- // A shader effect has no geometry of its own; only a shadow's measures follow the layer.
33
- if (copy.type === 'SHADER') return copy
55
+ // A shader effect has no geometry of its own, and a noise's grain keeps its size.
56
+ if (copy.type === 'SHADER' || copy.type === 'NOISE') return copy
57
+ // A texture roughens the edges by its radius; its grain keeps its size too.
58
+ if (copy.type === 'TEXTURE') {
59
+ copy.radius *= scale
60
+ return copy
61
+ }
34
62
  copy.offset = { x: copy.offset.x * scale, y: copy.offset.y * scale }
35
63
  copy.radius *= scale
36
64
  copy.spread *= scale
@@ -141,6 +169,7 @@ export function scaleNodeChanges(
141
169
  paragraphSpacing: node.paragraphSpacing * scale,
142
170
  paragraphIndent: node.paragraphIndent * scale,
143
171
  textParagraphs: scaledParagraphStyles(node.textParagraphs, scale),
172
+ derivedTextHeight: scaledOptional(node.derivedTextHeight, scale),
144
173
  itemSpacing: node.itemSpacing * scale,
145
174
  counterAxisSpacing: node.counterAxisSpacing * scale,
146
175
  paddingTop: node.paddingTop * scale,
@@ -165,6 +194,6 @@ export function scaleNodeChanges(
165
194
  vectorNetwork: scaledVectorNetwork(node, scale),
166
195
  fillGeometry: scaleGeometryPaths(node.fillGeometry, scale, scale),
167
196
  strokeGeometry: scaleGeometryPaths(node.strokeGeometry, scale, scale),
168
- ...scaledPathTextChanges(node, scale, scale)
197
+ ...scaledPathText(node, scale)
169
198
  }
170
199
  }
@@ -90,6 +90,14 @@ export function sharedStyleTypeForKind(kind: SharedStyleKind): SharedStyleType {
90
90
  return STYLE_TYPES[kind]
91
91
  }
92
92
 
93
+ /** The document's shared style layers, by the source ID layers link them by. */
94
+ export function sharedStyleLayers(graph: SceneGraph): Map<string, SceneNode> {
95
+ const styles = new Map<string, SceneNode>()
96
+ for (const node of graph.getAllNodes())
97
+ if (node.sharedStyleType !== null && node.source.id) styles.set(node.source.id, node)
98
+ return styles
99
+ }
100
+
93
101
  export function getSharedStyles(graph: SceneGraph, kind: SharedStyleKind): SharedStyle[] {
94
102
  const type = sharedStyleTypeForKind(kind)
95
103
  const styles: SharedStyle[] = []
@@ -0,0 +1,14 @@
1
+ import type { SceneNode } from '../types'
2
+
3
+ /** Whether the node's glyphs were baked along a path, including rotated-as-a-picture text. */
4
+ export function followsPath(
5
+ node: Pick<SceneNode, 'derivedTextGlyphs' | 'textPathData' | 'textPathBox'>
6
+ ): boolean {
7
+ const glyphs = node.derivedTextGlyphs ?? []
8
+ if (glyphs.length === 0) {
9
+ return false
10
+ }
11
+ return (
12
+ !!node.textPathData || !!node.textPathBox || glyphs.some((glyph) => (glyph.rotation ?? 0) !== 0)
13
+ )
14
+ }
@@ -39,13 +39,16 @@ export function textCacheInvalidationChanges(
39
39
  // A successful path-text edit supplies reflowed glyphs in `changes`. Every
40
40
  // other mutation path must drop stale baked glyphs and path identity rather
41
41
  // than pair new text/style with old visible outlines.
42
- if (node.derivedTextGlyphs && glyphsInvalidated && !changes.derivedTextGlyphs) {
42
+ if (glyphsInvalidated && !changes.derivedTextGlyphs) {
43
43
  // Path-text resize supplies transformed glyphs through the resize workflow. A raw box
44
44
  // preview keeps them until that workflow commits or an actual shaping edit occurs.
45
45
  const onlyPathBoxResize =
46
46
  !!node.textPathData && keys.every((key) => key === 'width' || key === 'height')
47
- if (!onlyPathBoxResize) {
47
+ const placesPath = keys.includes('textPathBox') && !!changes.textPathBox
48
+ if (node.derivedTextGlyphs && !onlyPathBoxResize) {
48
49
  invalidated.derivedTextGlyphs = null
50
+ }
51
+ if (node.textPathData && !onlyPathBoxResize && !placesPath) {
49
52
  invalidated.textPathData = null
50
53
  }
51
54
  }
@@ -0,0 +1,104 @@
1
+ import * as v from 'valibot'
2
+
3
+ import type { SceneGraph } from '../graph'
4
+ import { readPluginData, withPluginData } from '../plugin-data/field'
5
+ import { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'
6
+ import type { PluginDataEntry } from '../types'
7
+ import { relativeTokenPathSchema, type TokenSource } from './schema'
8
+
9
+ const { tokenSources } = PAIRDRAW_PLUGIN_DATA
10
+
11
+ /**
12
+ * The code a document's variables are linked to, with what it held at the last sync. The links
13
+ * live in Pairdraw's plugin data on the document node, so they travel with the `.fig` file and
14
+ * Figma ignores them; where each file is on this computer stays with the app, not the document.
15
+ */
16
+ export function readTokenSources(graph: SceneGraph): TokenSource[] {
17
+ return readPluginData(graph.getNode(graph.rootId)?.pluginData, tokenSources) ?? []
18
+ }
19
+
20
+ /**
21
+ * The links with their snapshots keyed by the graph's variable and mode ids. A format that keys
22
+ * variables and modes by name, such as `.pen`, gives them new ids when it reads them back; the
23
+ * names the file stored when it was saved, or the last sync's where the file recorded none, find
24
+ * them again. `names` and the snapshot's token identities are kept as they were. Links whose
25
+ * variables are all gone are left as they are.
26
+ */
27
+ export function rekeyTokenSources(
28
+ graph: SceneGraph,
29
+ sources: readonly TokenSource[]
30
+ ): TokenSource[] {
31
+ const collectionsByName = new Map(
32
+ [...graph.variableCollections.values()].map((collection) => [collection.name, collection])
33
+ )
34
+ const variablesByKey = new Map<string, string>()
35
+ for (const variable of graph.variables.values()) {
36
+ const collection = graph.variableCollections.get(variable.collectionId)
37
+ if (collection) variablesByKey.set(`${collection.name}/${variable.name}`, variable.id)
38
+ }
39
+ const currentModeId = (collectionName: string, modeId: string, modes: TokenSource['modes']) => {
40
+ const name = modes?.[collectionName]?.[modeId]
41
+ if (name === undefined) return modeId
42
+ const mode = collectionsByName.get(collectionName)?.modes.find((entry) => entry.name === name)
43
+ return mode?.modeId ?? modeId
44
+ }
45
+ return sources.map((source) => {
46
+ const ids = new Map<string, string>()
47
+ for (const [id, key] of Object.entries(source.currentNames ?? source.names ?? {})) {
48
+ const current = variablesByKey.get(key)
49
+ if (current !== undefined) ids.set(id, current)
50
+ }
51
+ if (ids.size === 0) return source
52
+ const collectionOf = (id: string) => {
53
+ const variable = graph.variables.get(id)
54
+ return variable ? graph.variableCollections.get(variable.collectionId)?.name : undefined
55
+ }
56
+ const snapshot: TokenSource['snapshot'] = {}
57
+ for (const [oldId, values] of Object.entries(source.snapshot)) {
58
+ const id = ids.get(oldId) ?? oldId
59
+ const collection = collectionOf(id)
60
+ const entry: Record<string, string> = {}
61
+ for (const [modeId, signature] of Object.entries(values)) {
62
+ entry[collection ? currentModeId(collection, modeId, source.modes) : modeId] = signature
63
+ }
64
+ snapshot[id] = { ...snapshot[id], ...entry }
65
+ }
66
+ const names = Object.fromEntries(
67
+ Object.entries(source.names ?? {}).map(([id, key]) => [ids.get(id) ?? id, key])
68
+ )
69
+ const modes = source.modes
70
+ ? Object.fromEntries(
71
+ Object.entries(source.modes).map(([collectionName, byMode]) => [
72
+ collectionName,
73
+ Object.fromEntries(
74
+ Object.entries(byMode).map(([modeId, name]) => [
75
+ currentModeId(collectionName, modeId, source.modes),
76
+ name
77
+ ])
78
+ )
79
+ ])
80
+ )
81
+ : undefined
82
+ return { ...source, snapshot, names, ...(modes ? { modes } : {}) }
83
+ })
84
+ }
85
+
86
+ /**
87
+ * The document node's plugin data holding `sources` in place of its links, for an edit that
88
+ * writes them, undoably or not, as one change to the node.
89
+ */
90
+ export function tokenSourcesPluginData(
91
+ graph: SceneGraph,
92
+ sources: readonly TokenSource[]
93
+ ): PluginDataEntry[] {
94
+ // A path that is not relative would make the reader drop the whole link; the link keeps going
95
+ // by its name instead.
96
+ const stored = sources.map(({ path, ...source }) =>
97
+ path !== undefined && v.is(relativeTokenPathSchema, path) ? { ...source, path } : source
98
+ )
99
+ return withPluginData(
100
+ graph.getNode(graph.rootId)?.pluginData ?? [],
101
+ tokenSources,
102
+ stored.length > 0 ? stored : undefined
103
+ )
104
+ }
@@ -0,0 +1,2 @@
1
+ export * from './document'
2
+ export * from './schema'
@@ -0,0 +1,59 @@
1
+ import * as v from 'valibot'
2
+
3
+ /**
4
+ * A path from the document's folder, never one that names a place on its own: no leading `/` or
5
+ * `\\` (POSIX roots, UNC shares), no `~` (a home folder), and no drive letter or URL scheme such
6
+ * as `C:` or `file:`.
7
+ */
8
+ export const relativeTokenPathSchema = v.pipe(
9
+ v.string(),
10
+ v.nonEmpty(),
11
+ v.maxLength(1024),
12
+ v.check(
13
+ (path) => !/^(?:[/\\~]|[a-z][\w+.-]*:)/i.test(path),
14
+ 'A linked path is relative to the document'
15
+ )
16
+ )
17
+
18
+ const tokenSourceSchema = v.object({
19
+ id: v.pipe(v.string(), v.nonEmpty(), v.maxLength(64)),
20
+ /** A stylesheet, a project folder, or W3C design token files. */
21
+ kind: v.picklist(['css', 'folder', 'dtcg']),
22
+ /** The file or folder name, for people. */
23
+ name: v.pipe(v.string(), v.maxLength(256)),
24
+ /** Where it is from the document's folder, when the document has one. */
25
+ path: v.optional(relativeTokenPathSchema),
26
+ syncedAt: v.optional(v.pipe(v.string(), v.isoTimestamp())),
27
+ /** What the code held at the last sync: a value signature by variable id and mode id. */
28
+ snapshot: v.record(v.string(), v.record(v.string(), v.string())),
29
+ /**
30
+ * The token each variable was at the last sync, `Collection/name` by variable id, so a variable
31
+ * deleted on the canvas is known when the code still has it.
32
+ */
33
+ names: v.optional(v.record(v.string(), v.string())),
34
+ /**
35
+ * The names the document held when it was saved, `Collection/name` by variable id. A format that
36
+ * keys variables by name gives them new ids when it reads them back; this finds a renamed
37
+ * variable's new id while `names` keeps the token it was at the last sync.
38
+ */
39
+ currentNames: v.optional(v.record(v.string(), v.string())),
40
+ /**
41
+ * The mode names the document holds, by collection name and mode id. A format that keys variables
42
+ * and modes by name gives them new ids when it reads them back, and this maps the snapshot onto
43
+ * those.
44
+ */
45
+ modes: v.optional(v.record(v.string(), v.record(v.string(), v.string())))
46
+ })
47
+
48
+ export type TokenSource = v.InferOutput<typeof tokenSourceSchema>
49
+
50
+ /** The code a document's variables are linked to; entries that cannot be read are left out. */
51
+ export const tokenSourcesSchema: v.GenericSchema<unknown, TokenSource[]> = v.pipe(
52
+ v.array(v.unknown()),
53
+ v.transform((items) =>
54
+ items.flatMap((item) => {
55
+ const parsed = v.safeParse(tokenSourceSchema, item)
56
+ return parsed.success ? [parsed.output] : []
57
+ })
58
+ )
59
+ )