@pairdraw/scene-graph 0.19.2 → 0.21.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 (285) hide show
  1. package/dist/color/index.d.ts +28 -7
  2. package/dist/color/index.d.ts.map +1 -1
  3. package/dist/color/index.js +40 -15
  4. package/dist/color/index.js.map +1 -1
  5. package/dist/color/management.d.ts +19 -6
  6. package/dist/color/management.d.ts.map +1 -1
  7. package/dist/color/management.js +56 -1
  8. package/dist/color/management.js.map +1 -1
  9. package/dist/color/profile.d.ts +32 -0
  10. package/dist/color/profile.d.ts.map +1 -0
  11. package/dist/color/profile.js +140 -0
  12. package/dist/color/profile.js.map +1 -0
  13. package/dist/comments/index.d.ts +2 -2
  14. package/dist/comments/index.js +2 -2
  15. package/dist/comments/merge.d.ts.map +1 -1
  16. package/dist/comments/merge.js +1 -0
  17. package/dist/comments/merge.js.map +1 -1
  18. package/dist/comments/schema.d.ts +3 -1
  19. package/dist/comments/schema.d.ts.map +1 -1
  20. package/dist/comments/schema.js +19 -2
  21. package/dist/comments/schema.js.map +1 -1
  22. package/dist/comments/threads.d.ts +3 -1
  23. package/dist/comments/threads.d.ts.map +1 -1
  24. package/dist/comments/threads.js +2 -1
  25. package/dist/comments/threads.js.map +1 -1
  26. package/dist/components/index.d.ts +2 -2
  27. package/dist/components/index.js +2 -2
  28. package/dist/components/properties.d.ts.map +1 -1
  29. package/dist/components/properties.js +7 -7
  30. package/dist/components/properties.js.map +1 -1
  31. package/dist/components/variants.d.ts +12 -1
  32. package/dist/components/variants.d.ts.map +1 -1
  33. package/dist/components/variants.js +30 -1
  34. package/dist/components/variants.js.map +1 -1
  35. package/dist/constants.d.ts +25 -1
  36. package/dist/constants.d.ts.map +1 -1
  37. package/dist/constants.js +94 -1
  38. package/dist/constants.js.map +1 -1
  39. package/dist/copy.d.ts.map +1 -1
  40. package/dist/copy.js +17 -1
  41. package/dist/copy.js.map +1 -1
  42. package/dist/css/index.d.ts +2 -2
  43. package/dist/css/index.js +2 -2
  44. package/dist/css/shadows.d.ts +2 -2
  45. package/dist/css/shadows.d.ts.map +1 -1
  46. package/dist/css/shadows.js +4 -4
  47. package/dist/css/shadows.js.map +1 -1
  48. package/dist/css/text.d.ts +17 -1
  49. package/dist/css/text.d.ts.map +1 -1
  50. package/dist/css/text.js +62 -1
  51. package/dist/css/text.js.map +1 -1
  52. package/dist/css/values.d.ts +7 -2
  53. package/dist/css/values.d.ts.map +1 -1
  54. package/dist/css/values.js +8 -4
  55. package/dist/css/values.js.map +1 -1
  56. package/dist/design-memory/index.d.ts +3 -0
  57. package/dist/design-memory/index.js +3 -0
  58. package/dist/design-memory/model.d.ts +33 -0
  59. package/dist/design-memory/model.d.ts.map +1 -0
  60. package/dist/design-memory/model.js +142 -0
  61. package/dist/design-memory/model.js.map +1 -0
  62. package/dist/design-memory/schema.d.ts +76 -0
  63. package/dist/design-memory/schema.d.ts.map +1 -0
  64. package/dist/design-memory/schema.js +72 -0
  65. package/dist/design-memory/schema.js.map +1 -0
  66. package/dist/effects.d.ts +7 -4
  67. package/dist/effects.d.ts.map +1 -1
  68. package/dist/effects.js +9 -4
  69. package/dist/effects.js.map +1 -1
  70. package/dist/empty-values.js +102 -0
  71. package/dist/empty-values.js.map +1 -0
  72. package/dist/fields/components.d.ts +9 -0
  73. package/dist/fields/components.d.ts.map +1 -0
  74. package/dist/fields/components.js +24 -0
  75. package/dist/fields/components.js.map +1 -0
  76. package/dist/fields/index.d.ts +3 -2
  77. package/dist/fields/index.js +3 -2
  78. package/dist/fields/layout.d.ts +3 -1
  79. package/dist/fields/layout.d.ts.map +1 -1
  80. package/dist/fields/layout.js +10 -1
  81. package/dist/fields/layout.js.map +1 -1
  82. package/dist/graph.d.ts +12 -1
  83. package/dist/graph.d.ts.map +1 -1
  84. package/dist/graph.js +39 -7
  85. package/dist/graph.js.map +1 -1
  86. package/dist/grid-values.d.ts +10 -0
  87. package/dist/grid-values.d.ts.map +1 -0
  88. package/dist/grid-values.js +53 -0
  89. package/dist/grid-values.js.map +1 -0
  90. package/dist/hit-test/drop.d.ts +20 -0
  91. package/dist/hit-test/drop.d.ts.map +1 -0
  92. package/dist/hit-test/drop.js +54 -0
  93. package/dist/hit-test/drop.js.map +1 -0
  94. package/dist/hit-test/geometry.js +86 -0
  95. package/dist/hit-test/geometry.js.map +1 -0
  96. package/dist/hit-test/masks.d.ts +14 -0
  97. package/dist/hit-test/masks.d.ts.map +1 -0
  98. package/dist/hit-test/masks.js +41 -0
  99. package/dist/hit-test/masks.js.map +1 -0
  100. package/dist/hit-test/paint.d.ts +6 -0
  101. package/dist/hit-test/paint.d.ts.map +1 -0
  102. package/dist/hit-test/paint.js +19 -0
  103. package/dist/hit-test/paint.js.map +1 -0
  104. package/dist/hit-test.d.ts +4 -15
  105. package/dist/hit-test.d.ts.map +1 -1
  106. package/dist/hit-test.js +5 -173
  107. package/dist/hit-test.js.map +1 -1
  108. package/dist/images.d.ts +3 -2
  109. package/dist/images.d.ts.map +1 -1
  110. package/dist/images.js +18 -2
  111. package/dist/images.js.map +1 -1
  112. package/dist/index.d.ts +2 -2
  113. package/dist/instances/copies.js +2 -1
  114. package/dist/instances/copies.js.map +1 -1
  115. package/dist/instances/fields.d.ts.map +1 -1
  116. package/dist/instances/fields.js +2 -0
  117. package/dist/instances/fields.js.map +1 -1
  118. package/dist/instances/index.d.ts +2 -1
  119. package/dist/instances/index.d.ts.map +1 -1
  120. package/dist/instances/index.js +13 -12
  121. package/dist/instances/index.js.map +1 -1
  122. package/dist/instances/overrides.d.ts +15 -1
  123. package/dist/instances/overrides.d.ts.map +1 -1
  124. package/dist/instances/overrides.js +37 -1
  125. package/dist/instances/overrides.js.map +1 -1
  126. package/dist/instances/source-walk.d.ts +12 -0
  127. package/dist/instances/source-walk.d.ts.map +1 -0
  128. package/dist/instances/swap-overrides.js +3 -3
  129. package/dist/instances/swap-overrides.js.map +1 -1
  130. package/dist/instances/sync.js +9 -7
  131. package/dist/instances/sync.js.map +1 -1
  132. package/dist/node-defaults.d.ts.map +1 -1
  133. package/dist/node-defaults.js +43 -38
  134. package/dist/node-defaults.js.map +1 -1
  135. package/dist/plugin-data/fields.d.ts +90 -0
  136. package/dist/plugin-data/fields.d.ts.map +1 -1
  137. package/dist/plugin-data/fields.js +33 -24
  138. package/dist/plugin-data/fields.js.map +1 -1
  139. package/dist/plugin-data/index.d.ts +2 -2
  140. package/dist/plugin-data/index.js +2 -2
  141. package/dist/plugin-data/schema.d.ts +6 -1
  142. package/dist/plugin-data/schema.d.ts.map +1 -1
  143. package/dist/plugin-data/schema.js +24 -1
  144. package/dist/plugin-data/schema.js.map +1 -1
  145. package/dist/preview.d.ts.map +1 -1
  146. package/dist/preview.js +2 -0
  147. package/dist/preview.js.map +1 -1
  148. package/dist/scaling/node.d.ts.map +1 -1
  149. package/dist/scaling/node.js +5 -1
  150. package/dist/scaling/node.js.map +1 -1
  151. package/dist/shaders/animation.d.ts +21 -0
  152. package/dist/shaders/animation.d.ts.map +1 -0
  153. package/dist/shaders/animation.js +25 -0
  154. package/dist/shaders/animation.js.map +1 -0
  155. package/dist/shaders/index.d.ts +2 -1
  156. package/dist/shaders/index.js +2 -1
  157. package/dist/slots/content.d.ts.map +1 -1
  158. package/dist/slots/content.js +4 -4
  159. package/dist/slots/content.js.map +1 -1
  160. package/dist/statuses/index.d.ts +2 -2
  161. package/dist/statuses/index.js +2 -2
  162. package/dist/statuses/workflow.d.ts +3 -1
  163. package/dist/statuses/workflow.d.ts.map +1 -1
  164. package/dist/statuses/workflow.js +6 -2
  165. package/dist/statuses/workflow.js.map +1 -1
  166. package/dist/token-sources/document.d.ts +26 -0
  167. package/dist/token-sources/document.d.ts.map +1 -0
  168. package/dist/token-sources/document.js +80 -0
  169. package/dist/token-sources/document.js.map +1 -0
  170. package/dist/token-sources/index.d.ts +3 -0
  171. package/dist/token-sources/index.js +3 -0
  172. package/dist/token-sources/schema.d.ts +43 -0
  173. package/dist/token-sources/schema.d.ts.map +1 -0
  174. package/dist/token-sources/schema.js +50 -0
  175. package/dist/token-sources/schema.js.map +1 -0
  176. package/dist/transfer/variables.d.ts.map +1 -1
  177. package/dist/transfer/variables.js +1 -1
  178. package/dist/transfer/variables.js.map +1 -1
  179. package/dist/types.d.ts +135 -15
  180. package/dist/types.d.ts.map +1 -1
  181. package/dist/types.js.map +1 -1
  182. package/dist/variables/bindings.d.ts +6 -5
  183. package/dist/variables/bindings.d.ts.map +1 -1
  184. package/dist/variables/bindings.js +24 -16
  185. package/dist/variables/bindings.js.map +1 -1
  186. package/dist/variables/formula/index.d.ts.map +1 -1
  187. package/dist/variables/formula/index.js +12 -6
  188. package/dist/variables/formula/index.js.map +1 -1
  189. package/dist/variables/index.d.ts +14 -1
  190. package/dist/variables/index.d.ts.map +1 -1
  191. package/dist/variables/index.js +38 -10
  192. package/dist/variables/index.js.map +1 -1
  193. package/dist/variables/paints.d.ts +9 -8
  194. package/dist/variables/paints.d.ts.map +1 -1
  195. package/dist/variables/paints.js +35 -26
  196. package/dist/variables/paints.js.map +1 -1
  197. package/dist/variables/resolver.d.ts +45 -0
  198. package/dist/variables/resolver.d.ts.map +1 -0
  199. package/dist/variables/resolver.js +106 -0
  200. package/dist/variables/resolver.js.map +1 -0
  201. package/dist/variations/copy.d.ts +16 -0
  202. package/dist/variations/copy.d.ts.map +1 -0
  203. package/dist/variations/copy.js +78 -0
  204. package/dist/variations/copy.js.map +1 -0
  205. package/dist/variations/index.d.ts +6 -0
  206. package/dist/variations/index.js +6 -0
  207. package/dist/variations/match.d.ts +23 -0
  208. package/dist/variations/match.d.ts.map +1 -0
  209. package/dist/variations/match.js +80 -0
  210. package/dist/variations/match.js.map +1 -0
  211. package/dist/variations/model.d.ts +39 -0
  212. package/dist/variations/model.d.ts.map +1 -0
  213. package/dist/variations/model.js +90 -0
  214. package/dist/variations/model.js.map +1 -0
  215. package/dist/variations/pick.d.ts +43 -0
  216. package/dist/variations/pick.d.ts.map +1 -0
  217. package/dist/variations/pick.js +211 -0
  218. package/dist/variations/pick.js.map +1 -0
  219. package/dist/variations/schema.d.ts +27 -0
  220. package/dist/variations/schema.d.ts.map +1 -0
  221. package/dist/variations/schema.js +27 -0
  222. package/dist/variations/schema.js.map +1 -0
  223. package/package.json +21 -1
  224. package/src/color/index.ts +43 -9
  225. package/src/color/management.ts +54 -1
  226. package/src/color/profile.ts +131 -0
  227. package/src/comments/merge.ts +3 -0
  228. package/src/comments/schema.ts +32 -2
  229. package/src/comments/threads.ts +5 -2
  230. package/src/components/properties.ts +13 -7
  231. package/src/components/variants.ts +39 -0
  232. package/src/constants.ts +75 -0
  233. package/src/copy.ts +20 -1
  234. package/src/css/index.ts +7 -1
  235. package/src/css/shadows.ts +8 -5
  236. package/src/css/text.ts +62 -0
  237. package/src/css/values.ts +12 -5
  238. package/src/design-memory/index.ts +2 -0
  239. package/src/design-memory/model.ts +143 -0
  240. package/src/design-memory/schema.ts +84 -0
  241. package/src/effects.ts +16 -7
  242. package/src/empty-values.ts +117 -0
  243. package/src/fields/components.ts +21 -0
  244. package/src/fields/index.ts +1 -0
  245. package/src/fields/layout.ts +10 -0
  246. package/src/graph.ts +50 -6
  247. package/src/grid-values.ts +47 -0
  248. package/src/hit-test/drop.ts +90 -0
  249. package/src/hit-test/geometry.ts +131 -0
  250. package/src/hit-test/masks.ts +57 -0
  251. package/src/hit-test/paint.ts +24 -0
  252. package/src/hit-test.ts +8 -277
  253. package/src/images.ts +27 -3
  254. package/src/instances/copies.ts +2 -1
  255. package/src/instances/fields.ts +2 -0
  256. package/src/instances/index.ts +20 -11
  257. package/src/instances/overrides.ts +49 -0
  258. package/src/instances/swap-overrides.ts +3 -2
  259. package/src/instances/sync.ts +11 -7
  260. package/src/node-defaults.ts +51 -41
  261. package/src/plugin-data/fields.ts +34 -32
  262. package/src/plugin-data/index.ts +1 -0
  263. package/src/plugin-data/schema.ts +30 -0
  264. package/src/preview.ts +2 -0
  265. package/src/scaling/node.ts +7 -2
  266. package/src/shaders/animation.ts +39 -0
  267. package/src/shaders/index.ts +1 -0
  268. package/src/slots/content.ts +8 -4
  269. package/src/statuses/workflow.ts +6 -4
  270. package/src/token-sources/document.ts +104 -0
  271. package/src/token-sources/index.ts +2 -0
  272. package/src/token-sources/schema.ts +59 -0
  273. package/src/transfer/variables.ts +2 -1
  274. package/src/types.ts +145 -14
  275. package/src/variables/bindings.ts +35 -18
  276. package/src/variables/formula/index.ts +19 -18
  277. package/src/variables/index.ts +49 -10
  278. package/src/variables/paints.ts +64 -29
  279. package/src/variables/resolver.ts +178 -0
  280. package/src/variations/copy.ts +89 -0
  281. package/src/variations/index.ts +5 -0
  282. package/src/variations/match.ts +100 -0
  283. package/src/variations/model.ts +132 -0
  284. package/src/variations/pick.ts +293 -0
  285. package/src/variations/schema.ts +29 -0
@@ -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
+ )
@@ -32,7 +32,8 @@ function assertResourceOwnership(
32
32
  }
33
33
  for (const variable of variables) {
34
34
  const collection = sets.get(variable.collectionId)
35
- if (!collection?.variableIds.includes(variable.id))
35
+ // A collection lists its live variables: a deleted one is kept out of the list (addVariable).
36
+ if (!collection || (!variable.deleted && !collection.variableIds.includes(variable.id)))
36
37
  throw new Error(`Missing collection membership for ${variable.id}`)
37
38
  const modes = new Set(collection.modes.map((mode) => mode.modeId))
38
39
  for (const mode of Object.keys(variable.valuesByMode)) {
package/src/types.ts CHANGED
@@ -47,7 +47,14 @@ export interface NodeChangeObserver {
47
47
  resume?: () => void
48
48
  }
49
49
 
50
- export type DocumentColorSpace = 'srgb' | 'display-p3'
50
+ /**
51
+ * A color profile: the one a document's colors are stored in, or the one they are shown or
52
+ * exported in. Figma files are sRGB or Display P3.
53
+ */
54
+ export type ColorProfile = 'srgb' | 'display-p3'
55
+
56
+ /** @deprecated use `ColorProfile` */
57
+ export type DocumentColorSpace = ColorProfile
51
58
 
52
59
  /** What a document is for, as Figma keeps design files and FigJam boards apart. */
53
60
  export type DocumentKind = 'design' | 'board'
@@ -64,12 +71,31 @@ export interface FigmaSourcePayload {
64
71
  uniformScaleFactor: number | null
65
72
  }
66
73
 
74
+ /**
75
+ * An edge as its draw.io file wrote it: the style pairs and geometry the author set. A connector
76
+ * read from a file keeps them so saving an unedited edge writes back what draw.io had, instead of
77
+ * re-routing it.
78
+ */
79
+ export interface DrawIOEdgeSource {
80
+ /** The style's names and its pairs, in the order the file wrote them. */
81
+ styleNames: string[]
82
+ styleValues: Partial<Record<string, string>>
83
+ /** The label's place along the edge, as the geometry's `x`. */
84
+ labelX: number
85
+ /** Bend points and free ends, in the edge's parent's axes. */
86
+ points: Vector[]
87
+ sourcePoint: Vector | null
88
+ targetPoint: Vector | null
89
+ }
90
+
67
91
  export interface SourceMetadata {
68
92
  format: 'fig' | null
69
93
  id: string | null
70
94
  orderKey: string | null
71
95
  editedFields: string[]
72
96
  fig: FigmaSourcePayload
97
+ /** The edge cell a connector was read from, kept until the connector is edited. */
98
+ drawio?: DrawIOEdgeSource | null
73
99
  }
74
100
 
75
101
  export type HandleMirroring = 'NONE' | 'ANGLE' | 'ANGLE_AND_LENGTH'
@@ -217,7 +243,7 @@ export interface Fill {
217
243
  imageTransform?: GradientTransform
218
244
  /** Clockwise turn of a Fill, Fit or Tile image, in multiples of 90°; a crop holds its own. */
219
245
  imageRotation?: number
220
- /** An image's adjustments, kept and saved; the canvas does not draw them. */
246
+ /** An image's adjustments, in Figma's plugin API units. */
221
247
  filters?: ImageFilters
222
248
  sourceNodeId?: string
223
249
  scale?: number
@@ -279,7 +305,7 @@ export interface Stroke extends Fill {
279
305
  dashPattern?: number[]
280
306
  }
281
307
 
282
- export type Effect = ShadowEffect | ShaderEffect
308
+ export type Effect = ShadowEffect | ShaderEffect | NoiseEffect | TextureEffect
283
309
 
284
310
  export interface ShadowEffect {
285
311
  type: 'DROP_SHADOW' | 'INNER_SHADOW' | 'LAYER_BLUR' | 'BACKGROUND_BLUR' | 'FOREGROUND_BLUR'
@@ -292,6 +318,50 @@ export interface ShadowEffect {
292
318
  showShadowBehindNode?: boolean
293
319
  }
294
320
 
321
+ /**
322
+ * A Noise effect's grain, in Figma's plugin terms: `MONOTONE` grains in the effect's colour,
323
+ * `DUOTONE` in it and `secondaryColor`, `MULTITONE` in many colours at `opacity`. `noiseSize` is a
324
+ * grain's size in px, which Figma keeps within 0 to 100, and `density` how much of the layer the
325
+ * grains cover, 0 to 1. `seed` picks the pattern; Figma's plugin API neither shows nor sets it.
326
+ */
327
+ export interface NoiseEffectParams {
328
+ noiseType: NoiseType
329
+ noiseSize: Vector
330
+ density: number
331
+ secondaryColor?: Color
332
+ opacity?: number
333
+ seed?: number
334
+ }
335
+
336
+ /**
337
+ * A Texture effect's grain, which roughens the layer's edges: `noiseSize` is the grain's size in
338
+ * px, within 0 to 100 as Figma keeps it, and `clipToShape` keeps the roughened layer inside its
339
+ * own outline. The effect's `radius` is how far the grain moves the edges. `.fig` files name the
340
+ * effect `GRAIN`.
341
+ */
342
+ export interface TextureEffectParams {
343
+ noiseSize: Vector
344
+ clipToShape: boolean
345
+ seed?: number
346
+ }
347
+
348
+ export interface NoiseEffect {
349
+ type: 'NOISE'
350
+ color: Color
351
+ visible: boolean
352
+ blendMode?: BlendMode
353
+ noise: NoiseEffectParams
354
+ }
355
+
356
+ export interface TextureEffect {
357
+ type: 'TEXTURE'
358
+ color: Color
359
+ radius: number
360
+ visible: boolean
361
+ blendMode?: BlendMode
362
+ texture: TextureEffectParams
363
+ }
364
+
295
365
  /** A layer post-processed by shader code the user wrote; the layer is the `pencil:object` sample. */
296
366
  export interface ShaderEffect {
297
367
  type: 'SHADER'
@@ -637,6 +707,16 @@ export interface CommentReply {
637
707
  deleted?: boolean
638
708
  }
639
709
 
710
+ /** A stroke drawn over the commented area in Comment mode, kept with its thread. */
711
+ export interface CommentRegion {
712
+ /** Stroke points, in canvas units relative to the thread's pin anchor. */
713
+ points: [number, number][]
714
+ /** Stroke width in canvas units. */
715
+ width: number
716
+ /** Bounds of the stroke including its width, relative to the same anchor. */
717
+ bounds: { minX: number; minY: number; maxX: number; maxY: number }
718
+ }
719
+
640
720
  /** A canvas comment thread, kept in the document's root plugin data. */
641
721
  export interface CommentThread {
642
722
  id: string
@@ -664,20 +744,52 @@ export interface CommentThread {
664
744
  resolvedAt?: string | null
665
745
  /** Deleted threads stay as tombstones so a stale copy cannot bring them back. */
666
746
  deleted?: boolean
747
+ /** The area the person circled, when the thread was drawn rather than pinned. */
748
+ region?: CommentRegion | null
667
749
  replies: CommentReply[]
668
750
  }
669
751
 
670
- /** How a prototype reaction starts, as `.fig` keeps `PrototypeEvent.interactionType`. */
671
- export type PrototypeTriggerType = 'ON_CLICK' | 'AFTER_TIMEOUT'
752
+ /**
753
+ * How a prototype reaction starts, as `.fig` keeps `PrototypeEvent.interactionType`: a click, the
754
+ * pointer held over the layer (`ON_HOVER`, While hovering) or pressed on it (`ON_PRESS`, While
755
+ * pressing), which undo themselves when it leaves or lifts, the pointer entering or leaving it,
756
+ * or a delay.
757
+ */
758
+ export type PrototypeTriggerType =
759
+ | 'ON_CLICK'
760
+ | 'ON_HOVER'
761
+ | 'ON_PRESS'
762
+ | 'MOUSE_ENTER'
763
+ | 'MOUSE_LEAVE'
764
+ | 'AFTER_TIMEOUT'
672
765
 
673
766
  /** What a reaction does, as `.fig` keeps `ConnectionType` on `PrototypeAction`. */
674
767
  export type PrototypeActionType = 'NODE' | 'BACK' | 'CLOSE'
675
768
 
676
- /** Whether an action moves to a frame or opens it over the screen, as `NavigationType`. */
677
- export type PrototypeNavigationType = 'NAVIGATE' | 'OVERLAY'
769
+ /**
770
+ * What a node action does with its node, as `NavigationType`: move to a frame, open it over the
771
+ * screen, show another variant of the instance's set (Figma's Change to, `SWAP_STATE` in `.fig`),
772
+ * or scroll the layer into view.
773
+ */
774
+ export type PrototypeNavigationType = 'NAVIGATE' | 'OVERLAY' | 'CHANGE_TO' | 'SCROLL_TO'
678
775
 
679
- /** How a reaction animates between frames, as `TransitionType` keeps them. */
680
- export type PrototypeTransitionType = 'INSTANT' | 'DISSOLVE'
776
+ /** Which way a frame's content scrolls in play, as Figma's `overflowDirection`. */
777
+ export type OverflowDirection = 'NONE' | 'HORIZONTAL' | 'VERTICAL' | 'BOTH'
778
+
779
+ /**
780
+ * How a layer moves when its parent frame scrolls in play: with the content, fixed in the frame,
781
+ * or scrolled until it reaches the frame's top/left edge and then stuck there.
782
+ */
783
+ export type ScrollBehavior = 'SCROLLS' | 'FIXED' | 'STICKY_SCROLLS'
784
+
785
+ /**
786
+ * How a reaction animates between frames, as `TransitionType` keeps them: a cut, a crossfade, or
787
+ * Smart animate, which moves the layers two frames share by name and fades the rest.
788
+ */
789
+ export type PrototypeTransitionType = 'INSTANT' | 'DISSOLVE' | 'SMART_ANIMATE'
790
+
791
+ /** How a transition eases, by the names Figma's plugin API gives its presets. */
792
+ export type PrototypeEasingType = 'LINEAR' | 'EASE_IN' | 'EASE_OUT' | 'EASE_IN_AND_OUT'
681
793
 
682
794
  /** Where an overlay sits over the screen, as `OverlayPositionType`. */
683
795
  export type PrototypeOverlayPositionType =
@@ -689,16 +801,21 @@ export type PrototypeOverlayPositionType =
689
801
  | 'BOTTOM_CENTER'
690
802
  | 'BOTTOM_RIGHT'
691
803
 
692
- /** How a reaction animates, and how long it takes in seconds. */
804
+ /** How a reaction animates, how long it takes in seconds, and how it eases. */
693
805
  export interface PrototypeTransition {
694
806
  type: PrototypeTransitionType
695
807
  duration: number
808
+ /** Absent eases as Figma's default does, `DEFAULT_PROTOTYPE_EASING`. */
809
+ easing?: PrototypeEasingType
696
810
  }
697
811
 
698
- /** A reaction's move to another frame, or the overlay it opens. */
812
+ /** A reaction's move to another frame, the overlay it opens, the variant or layer it shows. */
699
813
  export interface PrototypeNodeAction {
700
814
  type: 'NODE'
701
- /** The frame the action shows; `null` keeps the connection when its target is gone. */
815
+ /**
816
+ * The frame the action shows, the variant a Change to shows, or the layer a Scroll to brings
817
+ * into view; `null` keeps the connection when its target is gone.
818
+ */
702
819
  nodeId: string | null
703
820
  navigation: PrototypeNavigationType
704
821
  transition: PrototypeTransition
@@ -718,10 +835,10 @@ export interface PrototypeCloseAction {
718
835
 
719
836
  export type PrototypeAction = PrototypeNodeAction | PrototypeBackAction | PrototypeCloseAction
720
837
 
721
- /** What starts a reaction, and after how long an AFTER_TIMEOUT does. */
838
+ /** What starts a reaction, and after how long an AFTER_TIMEOUT or mouse enter or leave does. */
722
839
  export interface PrototypeTrigger {
723
840
  type: PrototypeTriggerType
724
- /** AFTER_TIMEOUT only: the seconds before the reaction runs. */
841
+ /** AFTER_TIMEOUT, MOUSE_ENTER and MOUSE_LEAVE: the seconds before the reaction runs. */
725
842
  delay: number
726
843
  }
727
844
 
@@ -775,6 +892,10 @@ export interface SceneNode {
775
892
  visible: boolean
776
893
  locked: boolean
777
894
  clipsContent: boolean
895
+ /** Which way the frame's content scrolls in play; a nested frame scrolls only while it clips. */
896
+ overflowDirection: OverflowDirection
897
+ /** How this layer moves while its parent frame scrolls in play. */
898
+ scrollBehavior: ScrollBehavior
778
899
 
779
900
  blendMode: BlendMode
780
901
 
@@ -1174,6 +1295,11 @@ export interface Variable {
1174
1295
  key?: string
1175
1296
  /** Published library version (from NodeChange.version). Used for assetRef resolution in colorVar. */
1176
1297
  version?: string
1298
+ /**
1299
+ * Deleted as Figma deletes a variable: gone from its collection's `variableIds` and from every
1300
+ * list of variables, while layers bound to it still resolve it and the file keeps it.
1301
+ */
1302
+ deleted?: true
1177
1303
  }
1178
1304
 
1179
1305
  /** Scalar numeric node fields accepted by numeric property controls. */
@@ -1213,6 +1339,11 @@ export interface VariableCollection {
1213
1339
  key?: string
1214
1340
  /** Published library version, which references by key carry with it. */
1215
1341
  version?: string
1342
+ /**
1343
+ * Deleted as Figma deletes a collection: gone from every list of collections, with its variables
1344
+ * deleted too, while layers bound to them still resolve them and the file keeps them.
1345
+ */
1346
+ deleted?: true
1216
1347
  }
1217
1348
 
1218
1349
  /** Whether a collection is a library's copy, which takes no variables or modes of the file's. */
@@ -9,7 +9,7 @@ import { pixelLength } from '../text/lengths'
9
9
  import type { SceneNode, StyleRun, TextLength } from '../types'
10
10
  import { NUMERIC_FIELDS, isNumericVariableBindingField } from './fields'
11
11
  import { refreshBoundPaints } from './paints'
12
- import { collectionsResolving } from './resolution'
12
+ import { createVariableResolver, type VariableResolver } from './resolver'
13
13
  import { subtreeNodes } from './subtree'
14
14
  import { variableFontFamily, variableText, variableTruthy } from './text'
15
15
  import { runBindingScale } from './units'
@@ -62,6 +62,11 @@ export function boundNumberValue(field: string, value: number): number | TextLen
62
62
  return TEXT_LENGTH_FIELDS.has(field) ? pixelLength(value) : value
63
63
  }
64
64
 
65
+ /** One resolver per field lookup, unless a caller reconciling many nodes shares its own. */
66
+ function resolverFor(graph: SceneGraph, resolver?: VariableResolver): VariableResolver {
67
+ return resolver ?? createVariableResolver(graph)
68
+ }
69
+
65
70
  /**
66
71
  * The values a node's numeric bindings resolve to in its mode, in scene units, for the fields
67
72
  * whose stored value differs. A width or height an instance overrides keeps its own value.
@@ -69,14 +74,16 @@ export function boundNumberValue(field: string, value: number): number | TextLen
69
74
  export function resolvedNumericBindings(
70
75
  graph: SceneGraph,
71
76
  node: SceneNode,
72
- fallback: VariableModeFallback = 'active'
77
+ fallback: VariableModeFallback = 'active',
78
+ resolver?: VariableResolver
73
79
  ): Partial<SceneNode> {
74
80
  const updates: Partial<SceneNode> = {}
81
+ const reader = resolverFor(graph, resolver).forNode(node.id, fallback)
75
82
  for (const [field, variableId] of Object.entries(node.boundVariables)) {
76
83
  if (!NUMERIC_FIELDS.has(field)) continue
77
84
  if ((field === 'width' || field === 'height') && overriddenInInstance(graph, node.id, field))
78
85
  continue
79
- const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
86
+ const value = reader.number(variableId)
80
87
  if (value === undefined || !Number.isFinite(value)) continue
81
88
  const scale = node.variableBindingScales[field] ?? 1
82
89
  const effective = value * scale
@@ -100,13 +107,15 @@ export function resolvedNumericBindings(
100
107
  export function resolvedValueBindings(
101
108
  graph: SceneGraph,
102
109
  node: SceneNode,
103
- fallback: VariableModeFallback = 'active'
110
+ fallback: VariableModeFallback = 'active',
111
+ resolver?: VariableResolver
104
112
  ): Partial<SceneNode> {
105
113
  const updates: Partial<SceneNode> = {}
114
+ const reader = resolverFor(graph, resolver).forNode(node.id, fallback)
106
115
  const resolve = (field: 'text' | 'fontFamily' | 'visible') => {
107
116
  const variableId = node.boundVariables[field]
108
117
  if (!variableId || overriddenInInstance(graph, node.id, field)) return undefined
109
- return graph.resolveVariableForNode(node.id, variableId, fallback)
118
+ return reader.value(variableId)
110
119
  }
111
120
  const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined
112
121
  if (text !== undefined && text !== node.text) updates.text = text
@@ -126,14 +135,15 @@ export function resolvedValueBindings(
126
135
  export function resolvedIconBinding(
127
136
  graph: SceneGraph,
128
137
  node: SceneNode,
129
- fallback: VariableModeFallback = 'active'
138
+ fallback: VariableModeFallback = 'active',
139
+ resolver?: VariableResolver
130
140
  ): string | undefined {
131
141
  if (node.type !== 'FRAME') return undefined
132
142
  const icon = readIcon(node)
133
143
  if (!icon) return undefined
134
144
  const variableId = node.boundVariables['iconName']
135
145
  if (!variableId || overriddenInInstance(graph, node.id, 'iconName')) return undefined
136
- const resolved = graph.resolveVariableForNode(node.id, variableId, fallback)
146
+ const resolved = resolverFor(graph, resolver).forNode(node.id, fallback).value(variableId)
137
147
  if (typeof resolved !== 'string') return undefined
138
148
  const name = resolved.trim()
139
149
  if (name === '' || name === icon.name || !ICON_NAME.test(name)) return undefined
@@ -151,8 +161,10 @@ const NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeig
151
161
  export function resolvedRunBindings(
152
162
  graph: SceneGraph,
153
163
  node: SceneNode,
154
- fallback: VariableModeFallback = 'active'
164
+ fallback: VariableModeFallback = 'active',
165
+ resolver?: VariableResolver
155
166
  ): Pick<Partial<SceneNode>, 'styleRuns'> {
167
+ const reader = resolverFor(graph, resolver).forNode(node.id, fallback)
156
168
  const styleRuns = node.styleRuns.map((run): StyleRun => {
157
169
  const bound = run.style.boundVariables
158
170
  if (!bound) return run
@@ -160,7 +172,7 @@ export function resolvedRunBindings(
160
172
  for (const field of NUMERIC_RUN_FIELDS) {
161
173
  const variableId = bound[field]
162
174
  if (!variableId) continue
163
- const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
175
+ const value = reader.number(variableId)
164
176
  if (value === undefined || !Number.isFinite(value)) continue
165
177
  const effective = value * runBindingScale(node, style, field)
166
178
  if (!Number.isFinite(effective)) continue
@@ -169,9 +181,7 @@ export function resolvedRunBindings(
169
181
  style = { ...style, [field]: next }
170
182
  }
171
183
  const familyId = bound.fontFamily
172
- const family = familyId
173
- ? variableFontFamily(graph.resolveVariableForNode(node.id, familyId, fallback))
174
- : undefined
184
+ const family = familyId ? variableFontFamily(reader.value(familyId)) : undefined
175
185
  if (family !== undefined && family !== style.fontFamily)
176
186
  style = { ...style, fontFamily: family }
177
187
  return style === run.style ? run : { ...run, style }
@@ -218,8 +228,14 @@ export function variablesResolvingThrough(graph: SceneGraph, ids: Iterable<strin
218
228
  * The collections whose variables the layers in these subtrees resolve: those they bind, and
219
229
  * those the bound variables alias, so a mode set on the subtrees can change what they show.
220
230
  */
221
- export function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>): Set<string> {
222
- return collectionsResolving(graph, subtreeNodes(graph, roots).flatMap(boundVariableIds))
231
+ export function collectionsBoundIn(
232
+ graph: SceneGraph,
233
+ roots: Iterable<string>,
234
+ resolver?: VariableResolver
235
+ ): Set<string> {
236
+ return resolverFor(graph, resolver).dependencies(
237
+ subtreeNodes(graph, roots).flatMap(boundVariableIds)
238
+ )
223
239
  }
224
240
 
225
241
  /**
@@ -249,12 +265,13 @@ function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterab
249
265
  export function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScope): string[] {
250
266
  const changed: string[] = []
251
267
  const nodes = [...scopedNodes(graph, scope)]
252
- for (const node of nodes) refreshBoundPaints(graph, node)
268
+ const resolver = createVariableResolver(graph)
269
+ for (const node of nodes) refreshBoundPaints(graph, node, resolver)
253
270
  for (const node of nodes) {
254
271
  const updates = {
255
- ...resolvedNumericBindings(graph, node),
256
- ...resolvedValueBindings(graph, node),
257
- ...resolvedRunBindings(graph, node)
272
+ ...resolvedNumericBindings(graph, node, 'active', resolver),
273
+ ...resolvedValueBindings(graph, node, 'active', resolver),
274
+ ...resolvedRunBindings(graph, node, 'active', resolver)
258
275
  }
259
276
  if (Object.keys(updates).length === 0) continue
260
277
  graph.updateNode(node.id, updates)
@@ -1,7 +1,7 @@
1
1
  import type { SceneGraph } from '#scene-graph/graph'
2
2
  import type { VariableLiteral, VariableValue } from '#scene-graph/types'
3
3
 
4
- import { resolveVariableInModes } from '../resolution'
4
+ import { createVariableResolver } from '../resolver'
5
5
  import { parseFormulaSyntax, type FormulaReference } from './parse'
6
6
  import { printFormula, type PrintFormulaOptions } from './print'
7
7
  import type { FormulaValueType } from './signatures'
@@ -38,12 +38,15 @@ function qualifiedName(graph: SceneGraph, id: string) {
38
38
  /** Qualified names disambiguate references; the stored formula uses stable IDs. */
39
39
  export function variableFormulaReference(graph: SceneGraph, id: string): string {
40
40
  const variable = graph.variables.get(id)
41
- if (!variable) return `{#${id}}`
41
+ // A deleted variable's name no longer names it, so only its ID does.
42
+ if (!variable || variable.deleted) return `{#${id}}`
42
43
  const unique = (name: string, of: (id: string) => string | undefined) =>
43
- [...graph.variables.keys()].filter((other) => of(other) === name).length === 1
44
+ graph.getLiveVariables().filter((other) => of(other.id) === name).length === 1
44
45
  const plain = variable.name
45
46
  if (
46
47
  !/[{}]/.test(plain) &&
48
+ // A name that starts like an ID would be read as one.
49
+ !plain.startsWith('#') &&
47
50
  plain.trim() === plain &&
48
51
  unique(plain, (other) => graph.variables.get(other)?.name)
49
52
  )
@@ -72,12 +75,13 @@ export function formatVariableFormula(
72
75
  * edited variable's own collection when a name appears in several.
73
76
  */
74
77
  function resolveReference(graph: SceneGraph, collectionId: string, name: string): FormulaReference {
75
- const candidates = [...graph.variables.values()].filter(
76
- (candidate) =>
77
- name === `#${candidate.id}` ||
78
- name === qualifiedName(graph, candidate.id) ||
79
- name === candidate.name
80
- )
78
+ // An ID names its variable before any name does; a deleted variable is reached only by it, as
79
+ // a formula saved before the deletion names it.
80
+ const byId = name.startsWith('#') ? graph.variables.get(name.slice(1)) : undefined
81
+ if (byId) return { id: byId.id, type: byId.type }
82
+ const candidates = graph
83
+ .getLiveVariables()
84
+ .filter((candidate) => name === qualifiedName(graph, candidate.id) || name === candidate.name)
81
85
  const local = candidates.filter((candidate) => candidate.collectionId === collectionId)
82
86
  const matches = local.length === 1 ? local : candidates
83
87
  const match = matches.at(0)
@@ -116,14 +120,11 @@ export function parseVariableFormula(
116
120
  const value: VariableValue = isFormulaOperation(node)
117
121
  ? { expression: 'FORMULA', type: variable.type, value: node }
118
122
  : node
119
- const resolved = resolveVariableInModes(
120
- graph,
121
- variableId,
122
- (collectionId) =>
123
- collectionId === variable.collectionId ? modeId : graph.getActiveModeId(collectionId),
124
- new Set(),
125
- new Map(),
126
- new Map([[variableId, value]])
127
- )
123
+ const resolver = createVariableResolver(graph, { overrides: new Map([[variableId, value]]) })
124
+ const resolved = resolver
125
+ .forModes((collectionId) =>
126
+ collectionId === variable.collectionId ? modeId : resolver.activeMode(collectionId)
127
+ )
128
+ .resolve(variableId)
128
129
  return { value, resolved }
129
130
  }