@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
@@ -34,6 +34,9 @@ export function mergeCommentThreads(
34
34
  const newer = thread.updatedAt >= other.updatedAt ? thread : other
35
35
  byId.set(thread.id, {
36
36
  ...newer,
37
+ // A region is only ever set together with its thread, so a copy that lacks one — written by
38
+ // an older build or read before the person drew — does not erase it.
39
+ region: newer.region ?? thread.region ?? other.region,
37
40
  deleted: thread.deleted || other.deleted,
38
41
  replies: mergeReplies(other.replies, thread.replies)
39
42
  })
@@ -1,6 +1,6 @@
1
1
  import * as v from 'valibot'
2
2
 
3
- import type { CommentThread } from '../types'
3
+ import type { CommentRegion, CommentThread } from '../types'
4
4
 
5
5
  // Lenient on purpose: other tools may write comments, so missing fields get defaults and one
6
6
  // malformed thread is skipped instead of hiding every comment.
@@ -20,6 +20,28 @@ const commentEntry = {
20
20
 
21
21
  const commentReplySchema = v.object({ id: v.string(), ...commentEntry })
22
22
 
23
+ /** Points one thread's region may carry; a file asking for more is not a stroke a person drew. */
24
+ export const MAX_COMMENT_REGION_POINTS = 512
25
+
26
+ const finiteSchema = v.pipe(v.number(), v.finite())
27
+ const commentPointSchema = v.tuple([finiteSchema, finiteSchema])
28
+
29
+ /** The stroke a person circled with: its points and width, and the box they span. */
30
+ const commentRegionSchema: v.GenericSchema<unknown, CommentRegion> = v.object({
31
+ points: v.pipe(
32
+ v.array(commentPointSchema),
33
+ v.minLength(2),
34
+ v.maxLength(MAX_COMMENT_REGION_POINTS)
35
+ ),
36
+ width: v.pipe(finiteSchema, v.minValue(0)),
37
+ bounds: v.object({
38
+ minX: finiteSchema,
39
+ minY: finiteSchema,
40
+ maxX: finiteSchema,
41
+ maxY: finiteSchema
42
+ })
43
+ })
44
+
23
45
  const commentThreadSchema = v.object({
24
46
  id: v.string(),
25
47
  pageId: v.string(),
@@ -45,7 +67,15 @@ export const commentThreadsSchema: v.GenericSchema<unknown, CommentThread[]> = v
45
67
  const parsed = v.safeParse(commentThreadSchema, item)
46
68
  if (!parsed.success) return []
47
69
  const thread = parsed.output
48
- return [{ ...thread, updatedAt: thread.updatedAt || thread.createdAt }]
70
+ // A stroke that cannot be read is dropped; the note it was drawn around is still worth keeping.
71
+ const region = v.safeParse(commentRegionSchema, (item as { region?: unknown }).region)
72
+ return [
73
+ {
74
+ ...thread,
75
+ updatedAt: thread.updatedAt || thread.createdAt,
76
+ ...(region.success ? { region: region.output } : {})
77
+ }
78
+ ]
49
79
  })
50
80
  )
51
81
  )
@@ -1,7 +1,7 @@
1
1
  import type { SceneGraph } from '../graph'
2
2
  import type { Color, Vector } from '../primitives'
3
3
  import { randomHex } from '../random'
4
- import type { CommentReply, CommentThread, SceneNode } from '../types'
4
+ import type { CommentRegion, CommentReply, CommentThread, SceneNode } from '../types'
5
5
 
6
6
  /** Who writes a comment: their name and, in a collaboration room, their color. */
7
7
  export interface CommentAuthor {
@@ -87,12 +87,14 @@ export function createCommentThread(
87
87
  at: Vector
88
88
  /** Pin it on this layer's top-level layer rather than on whatever is at `at`. */
89
89
  node?: SceneNode
90
+ /** The area the person circled, when the comment was drawn rather than clicked. */
91
+ region?: CommentRegion | null
90
92
  author: CommentAuthor
91
93
  text: string
92
94
  now: string
93
95
  }
94
96
  ): CommentThread {
95
- const { pageId, at, node, author, text, now } = options
97
+ const { pageId, at, node, region, author, text, now } = options
96
98
  return {
97
99
  id: newCommentId('c'),
98
100
  pageId,
@@ -104,6 +106,7 @@ export function createCommentThread(
104
106
  createdAt: now,
105
107
  updatedAt: now,
106
108
  resolved: false,
109
+ ...(region ? { region } : {}),
107
110
  replies: []
108
111
  }
109
112
  }
@@ -1,7 +1,7 @@
1
1
  import type { SceneGraph } from '../graph'
2
2
  import { findInstanceAncestor, recordAssignmentOverride } from '../instances'
3
3
  import { instanceMainComponent } from '../instances/main-component'
4
- import { setInstanceOverride } from '../instances/overrides'
4
+ import { ownInstanceOverrides, setInstanceOverride } from '../instances/overrides'
5
5
  import { walkInstanceSources } from '../instances/source-walk'
6
6
  import { randomHex } from '../random'
7
7
  import type {
@@ -103,7 +103,7 @@ function applyTextProperty(
103
103
  if (item.field !== 'TEXT' || item.node.type !== 'TEXT') continue
104
104
  graph.updateNode(item.node.id, { text: value })
105
105
  if (findInstanceAncestor(graph, item.node.id)) {
106
- setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'text', value)
106
+ setInstanceOverride(ownInstanceOverrides(owner), owner.id, item.node.id, 'text', value)
107
107
  }
108
108
  }
109
109
  }
@@ -120,7 +120,7 @@ function applyBooleanProperty(
120
120
  graph.updateNode(item.node.id, { visible: value === 'true' })
121
121
  if (instance) {
122
122
  setInstanceOverride(
123
- owner.instanceOverrides,
123
+ ownInstanceOverrides(owner),
124
124
  owner.id,
125
125
  item.node.id,
126
126
  'visible',
@@ -139,16 +139,22 @@ function applyInstanceSwapProperty(
139
139
  for (const item of targets) {
140
140
  if (item.field !== 'INSTANCE_SWAP' || item.node.type !== 'INSTANCE') continue
141
141
  graph.swapInstanceComponent(item.node.id, target.id)
142
- setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'name', target.name)
143
- setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'componentId', target.id)
142
+ setInstanceOverride(ownInstanceOverrides(owner), owner.id, item.node.id, 'name', target.name)
144
143
  setInstanceOverride(
145
- owner.instanceOverrides,
144
+ ownInstanceOverrides(owner),
145
+ owner.id,
146
+ item.node.id,
147
+ 'componentId',
148
+ target.id
149
+ )
150
+ setInstanceOverride(
151
+ ownInstanceOverrides(owner),
146
152
  owner.id,
147
153
  item.node.id,
148
154
  'sourceComponentId',
149
155
  item.source.id
150
156
  )
151
- graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })
157
+ graph.updateNode(owner.id, { instanceOverrides: ownInstanceOverrides(owner) })
152
158
  }
153
159
  }
154
160
 
@@ -1,4 +1,5 @@
1
1
  import type { SceneGraph } from '../graph'
2
+ import { instanceMainComponent } from '../instances/main-component'
2
3
  import type { SceneNode } from '../types'
3
4
 
4
5
  /** Variants in reading order on the canvas: top to bottom, then left to right, then by name. */
@@ -51,3 +52,41 @@ export function restingVariant(graph: SceneGraph, set: SceneNode): SceneNode | u
51
52
  ) ?? defaultVariant(graph, set)
52
53
  )
53
54
  }
55
+
56
+ /** The component set a variant belongs to, or the set of the variant an instance shows. */
57
+ function variantSetOf(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
58
+ const variant = node.type === 'INSTANCE' ? instanceMainComponent(graph, node) : node
59
+ if (variant?.type !== 'COMPONENT' || !variant.parentId) return undefined
60
+ const set = graph.getNode(variant.parentId)
61
+ return set?.type === 'COMPONENT_SET' ? set : undefined
62
+ }
63
+
64
+ /**
65
+ * The component sets whose variants a prototype's Change to on this layer may show, nearest
66
+ * first: the set of each variant, and of each instance of a variant, from the layer up.
67
+ */
68
+ export function variantSetsAround(graph: SceneGraph, nodeId: string): SceneNode[] {
69
+ const sets: SceneNode[] = []
70
+ graph.closest(nodeId, (node) => {
71
+ const set = variantSetOf(graph, node)
72
+ if (set && !sets.includes(set)) sets.push(set)
73
+ return false
74
+ })
75
+ return sets
76
+ }
77
+
78
+ /**
79
+ * The instance a Change to from this layer switches to another variant of `setId`: the layer
80
+ * itself or its nearest instance ancestor that shows a variant of that set. A main component has
81
+ * none, since it is the definition rather than a copy that changes.
82
+ */
83
+ export function variantInstanceFor(
84
+ graph: SceneGraph,
85
+ nodeId: string,
86
+ setId: string
87
+ ): SceneNode | undefined {
88
+ return graph.closest(
89
+ nodeId,
90
+ (node) => node.type === 'INSTANCE' && variantSetOf(graph, node)?.id === setId
91
+ )
92
+ }
package/src/constants.ts CHANGED
@@ -1,4 +1,11 @@
1
1
  import type { Color } from './primitives'
2
+ import type {
3
+ NoiseEffectParams,
4
+ OverflowDirection,
5
+ PrototypeEasingType,
6
+ ScrollBehavior,
7
+ TextureEffectParams
8
+ } from './types'
2
9
 
3
10
  export const BLACK: Color = { r: 0, g: 0, b: 0, a: 1 }
4
11
  export const WHITE: Color = { r: 1, g: 1, b: 1, a: 1 }
@@ -11,3 +18,71 @@ export const DEFAULT_STROKE_WEIGHT = 1
11
18
  * instances of but never edits: a component library's definitions, and a `.pen` library's.
12
19
  */
13
20
  export const LIBRARY_DEFINITIONS_PAGE_NAME = 'Pairdraw library definitions'
21
+
22
+ /** The grain of a noise effect whose values are missing: none of the layer covered. */
23
+ export const UNSET_NOISE: Readonly<NoiseEffectParams> = {
24
+ noiseType: 'MONOTONE',
25
+ noiseSize: { x: 1, y: 1 },
26
+ density: 0
27
+ }
28
+
29
+ /** A new noise effect's grain, as Figma's effects panel starts one: black at 25%, fully dense. */
30
+ export const DEFAULT_NOISE: Readonly<NoiseEffectParams> = {
31
+ noiseType: 'MONOTONE',
32
+ noiseSize: { x: 0.5, y: 0.5 },
33
+ density: 1
34
+ }
35
+
36
+ /** The colour a new noise effect's grains start in. */
37
+ export const DEFAULT_NOISE_COLOR: Readonly<Color> = { r: 0, g: 0, b: 0, a: 0.25 }
38
+
39
+ /** A new texture effect's grain, as Figma's effects panel starts one, with a radius of 4. */
40
+ export const DEFAULT_TEXTURE: Readonly<TextureEffectParams> = {
41
+ noiseSize: { x: 4, y: 4 },
42
+ clipToShape: false
43
+ }
44
+
45
+ /** The grain of a texture effect whose values are missing; its radius of 0 moves nothing. */
46
+ export const UNSET_TEXTURE: Readonly<TextureEffectParams> = {
47
+ noiseSize: { x: 1, y: 1 },
48
+ clipToShape: true
49
+ }
50
+
51
+ /** The ways a frame's content may scroll in play, as Figma's `overflowDirection` names them. */
52
+ export const OVERFLOW_DIRECTIONS = [
53
+ 'NONE',
54
+ 'HORIZONTAL',
55
+ 'VERTICAL',
56
+ 'BOTH'
57
+ ] as const satisfies readonly OverflowDirection[]
58
+
59
+ /** How a layer moves while its parent frame scrolls in play, in Figma's names. */
60
+ export const SCROLL_BEHAVIORS = [
61
+ 'SCROLLS',
62
+ 'FIXED',
63
+ 'STICKY_SCROLLS'
64
+ ] as const satisfies readonly ScrollBehavior[]
65
+
66
+ /** The easings a prototype transition takes, Figma's presets, in the order its menu lists them. */
67
+ export const PROTOTYPE_EASINGS = [
68
+ 'EASE_OUT',
69
+ 'EASE_IN',
70
+ 'EASE_IN_AND_OUT',
71
+ 'LINEAR'
72
+ ] as const satisfies readonly PrototypeEasingType[]
73
+
74
+ /** The easing a transition without one plays with, Figma's default. */
75
+ export const DEFAULT_PROTOTYPE_EASING: PrototypeEasingType = 'EASE_OUT'
76
+
77
+ /**
78
+ * Each easing as the control points of its cubic Bézier, `[x1, y1, x2, y2]`: what a `.fig` file
79
+ * stores as an action's `easingFunction` and CSS reads as `cubic-bezier()`.
80
+ */
81
+ export const PROTOTYPE_EASING_CURVES: Readonly<
82
+ Record<PrototypeEasingType, readonly [number, number, number, number]>
83
+ > = {
84
+ LINEAR: [0, 0, 1, 1],
85
+ EASE_IN: [0.42, 0, 1, 1],
86
+ EASE_OUT: [0, 0, 0.58, 1],
87
+ EASE_IN_AND_OUT: [0.42, 0, 0.58, 1]
88
+ }
package/src/copy.ts CHANGED
@@ -56,9 +56,28 @@ export function copyStroke(s: Stroke): Stroke {
56
56
  }
57
57
 
58
58
  export function copyEffect(e: Effect): Effect {
59
- if (e.type === 'SHADER') return structuredClone(e)
60
59
  const normalized = normalizeEffects([e])[0] ?? e
61
60
  if (normalized.type === 'SHADER') return structuredClone(normalized)
61
+ if (normalized.type === 'NOISE') {
62
+ return {
63
+ ...normalized,
64
+ color: { ...normalized.color },
65
+ noise: {
66
+ ...normalized.noise,
67
+ noiseSize: { ...normalized.noise.noiseSize },
68
+ secondaryColor: normalized.noise.secondaryColor
69
+ ? { ...normalized.noise.secondaryColor }
70
+ : undefined
71
+ }
72
+ }
73
+ }
74
+ if (normalized.type === 'TEXTURE') {
75
+ return {
76
+ ...normalized,
77
+ color: { ...normalized.color },
78
+ texture: { ...normalized.texture, noiseSize: { ...normalized.texture.noiseSize } }
79
+ }
80
+ }
62
81
  return {
63
82
  ...normalized,
64
83
  color: { ...normalized.color },
package/src/css/index.ts CHANGED
@@ -11,6 +11,12 @@ export {
11
11
  parseCSSFontFamilies,
12
12
  parseCSSFontFamily,
13
13
  parseCSSLetterSpacing,
14
- parseCSSLineHeight
14
+ parseCSSLineHeight,
15
+ GENERIC_FONT_FAMILIES,
16
+ GENERIC_FONT_FAMILY_CLASS,
17
+ GENERIC_FONT_FAMILIES_BY_CLASS,
18
+ genericFontFamilyClass,
19
+ isGenericFontFamily,
20
+ type GenericFontClass
15
21
  } from './text'
16
22
  export { parseCSSColor, parseCSSNumber, splitCSSValue } from './values'
@@ -3,14 +3,14 @@ import valueParser, { type Node } from 'postcss-value-parser'
3
3
  import { tryParseColor } from '../color'
4
4
  import { BLACK } from '../constants'
5
5
  import type { Color } from '../primitives'
6
- import type { Effect } from '../types'
6
+ import type { DocumentColorSpace, Effect } from '../types'
7
7
  import { cssLayers, parseCSSNumber } from './values'
8
8
 
9
9
  /**
10
10
  * One layer: `inset`, two to four lengths, and a color in any order. Without a color it is
11
11
  * black, as `currentColor` usually is; a layer with anything else, such as `none`, is dropped.
12
12
  */
13
- function shadowOf(nodes: Node[]): Effect[] {
13
+ function shadowOf(nodes: Node[], profile: DocumentColorSpace): Effect[] {
14
14
  let inset = false
15
15
  let color: Color | null = null
16
16
  const lengths: number[] = []
@@ -24,7 +24,7 @@ function shadowOf(nodes: Node[]): Effect[] {
24
24
  lengths.push(length)
25
25
  continue
26
26
  }
27
- const parsed = tryParseColor(valueParser.stringify(node))
27
+ const parsed = tryParseColor(valueParser.stringify(node), profile)
28
28
  if (!parsed || color) return []
29
29
  color = parsed
30
30
  }
@@ -46,7 +46,10 @@ function shadowOf(nodes: Node[]): Effect[] {
46
46
  * The shadows of a CSS `box-shadow` or `text-shadow` list, such as
47
47
  * `0 4px 8px rgb(0 0 0 / 0.2), inset 0 1px #fff`: drop shadows, and inner shadows for `inset`.
48
48
  */
49
- export function parseCSSShadows(value: string | undefined): Effect[] {
49
+ export function parseCSSShadows(
50
+ value: string | undefined,
51
+ profile: DocumentColorSpace = 'srgb'
52
+ ): Effect[] {
50
53
  if (!value) return []
51
- return cssLayers(valueParser(value).nodes).flatMap(shadowOf)
54
+ return cssLayers(valueParser(value).nodes).flatMap((nodes) => shadowOf(nodes, profile))
52
55
  }
package/src/css/text.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import valueParser from 'postcss-value-parser'
2
2
 
3
+ import { DEFAULT_FONT_FAMILY } from '../constants'
3
4
  import { percentLength, pixelLength } from '../text/lengths'
4
5
  import type { TextLength } from '../types'
5
6
  import { parseCSSNumber } from './values'
@@ -69,6 +70,67 @@ export function parseCSSFontFamilies(value: string | undefined): string[] {
69
70
  })
70
71
  }
71
72
 
73
+ /** CSS generic `font-family` keywords: names no font provider serves, drawn by a concrete face instead. */
74
+ export const GENERIC_FONT_FAMILIES: ReadonlySet<string> = new Set([
75
+ 'serif',
76
+ 'sans-serif',
77
+ 'monospace',
78
+ 'cursive',
79
+ 'fantasy',
80
+ 'system-ui',
81
+ 'ui-serif',
82
+ 'ui-sans-serif',
83
+ 'ui-monospace',
84
+ 'ui-rounded',
85
+ '-apple-system',
86
+ 'blinkmacsystemfont',
87
+ 'math',
88
+ 'emoji',
89
+ 'fangsong'
90
+ ])
91
+
92
+ /** Whether `family` is a CSS generic keyword rather than a face a provider or the host can load. */
93
+ export function isGenericFontFamily(family: string): boolean {
94
+ return GENERIC_FONT_FAMILIES.has(family.trim().toLowerCase())
95
+ }
96
+
97
+ /** The class a CSS generic keyword stands for, when a concrete face of it can draw the text. */
98
+ export type GenericFontClass = 'sans-serif' | 'serif' | 'monospace'
99
+
100
+ /** CSS generic keywords, by the concrete face class they stand for. */
101
+ export const GENERIC_FONT_FAMILY_CLASS: Readonly<Record<string, GenericFontClass>> = {
102
+ 'sans-serif': 'sans-serif',
103
+ 'ui-sans-serif': 'sans-serif',
104
+ 'system-ui': 'sans-serif',
105
+ '-apple-system': 'sans-serif',
106
+ blinkmacsystemfont: 'sans-serif',
107
+ serif: 'serif',
108
+ 'ui-serif': 'serif',
109
+ monospace: 'monospace',
110
+ 'ui-monospace': 'monospace'
111
+ }
112
+
113
+ /** Known system faces of each generic class, best first. The app bundles Inter for sans-serif. */
114
+ export const GENERIC_FONT_FAMILIES_BY_CLASS = {
115
+ 'sans-serif': [DEFAULT_FONT_FAMILY],
116
+ serif: ['Georgia', 'Times New Roman', 'Times', 'DejaVu Serif', 'Liberation Serif', 'Noto Serif'],
117
+ monospace: [
118
+ 'Menlo',
119
+ 'SF Mono',
120
+ 'Consolas',
121
+ 'Courier New',
122
+ 'DejaVu Sans Mono',
123
+ 'Liberation Mono',
124
+ 'Noto Sans Mono',
125
+ 'Ubuntu Mono'
126
+ ]
127
+ } as const satisfies Record<GenericFontClass, readonly string[]>
128
+
129
+ /** The concrete face class `family` stands for, or null when its name is no CSS generic keyword. */
130
+ export function genericFontFamilyClass(family: string): GenericFontClass | null {
131
+ return GENERIC_FONT_FAMILY_CLASS[family.trim().toLowerCase()] ?? null
132
+ }
133
+
72
134
  /** A CSS dimension's number and lowercase unit, or null. */
73
135
  function dimension(value: string): { number: number; unit: string } | null {
74
136
  const parsed = valueParser.unit(value)
package/src/css/values.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import valueParser, { type Node } from 'postcss-value-parser'
2
2
 
3
- import { parseDisplayableColor } from '../color'
3
+ import { tryParseColor } from '../color'
4
4
  import type { Color } from '../primitives'
5
+ import type { DocumentColorSpace } from '../types'
5
6
 
6
7
  /** Pixels in a rem, as browsers use by default. */
7
8
  const ROOT_FONT_SIZE = 16
@@ -27,11 +28,17 @@ export function parseCSSNumber(value: string | undefined): number | null {
27
28
  }
28
29
  }
29
30
 
30
- /** A CSS color that paints something; null for an invalid or fully transparent one. */
31
- export function parseCSSColor(value: string | undefined): Color | null {
31
+ /**
32
+ * A CSS color that paints something, as components in `profile`; null for an invalid or fully
33
+ * transparent one. A stylesheet's own declarations are read in sRGB, as CSS authors them; a caller
34
+ * reading markup into a document passes the document's profile.
35
+ */
36
+ export function parseCSSColor(
37
+ value: string | undefined,
38
+ profile: DocumentColorSpace = 'srgb'
39
+ ): Color | null {
32
40
  if (!value) return null
33
- // CSS clamps `rgb()` channels and maps wide-gamut colors into sRGB, as a stored color must be.
34
- const color = parseDisplayableColor(value.trim())
41
+ const color = tryParseColor(value.trim(), profile)
35
42
  return color && color.a > 0 ? color : null
36
43
  }
37
44
 
@@ -0,0 +1,2 @@
1
+ export * from './model'
2
+ export * from './schema'
@@ -0,0 +1,143 @@
1
+ import { tryParseColor, colorToHex } from '../color'
2
+ import type { SceneGraph } from '../graph'
3
+ import { readPluginData, withPluginData } from '../plugin-data/field'
4
+ import { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'
5
+ import { randomHex } from '../random'
6
+ import type { PluginDataEntry } from '../types'
7
+ import {
8
+ DESIGN_MEMORY_LIMITS,
9
+ type DesignDecisionAuthor,
10
+ type DesignMemory,
11
+ type DesignMemoryGroup
12
+ } from './schema'
13
+
14
+ const { designMemory } = PAIRDRAW_PLUGIN_DATA
15
+
16
+ const GROUP_LIMITS = {
17
+ colors: DESIGN_MEMORY_LIMITS.colors,
18
+ fonts: DESIGN_MEMORY_LIMITS.fonts,
19
+ fontSizes: DESIGN_MEMORY_LIMITS.numbers,
20
+ spacing: DESIGN_MEMORY_LIMITS.numbers,
21
+ radius: DESIGN_MEMORY_LIMITS.numbers
22
+ } as const satisfies Record<DesignMemoryGroup, number>
23
+
24
+ /**
25
+ * A new memory: every group allows the document's own variables and styles. Off-system colors
26
+ * and fonts refuse an agent's edit; sizes, spacing and radii have real exceptions, so they warn.
27
+ */
28
+ export function createDesignMemory(): DesignMemory {
29
+ return {
30
+ version: 1,
31
+ colors: { fromSystem: true, values: [], enforce: 'refuse' },
32
+ fonts: { fromSystem: true, values: [], enforce: 'refuse' },
33
+ fontSizes: { fromSystem: true, values: [], enforce: 'warn' },
34
+ spacing: { fromSystem: true, values: [], enforce: 'warn' },
35
+ radius: { fromSystem: true, values: [], enforce: 'warn' },
36
+ decisions: [],
37
+ examples: []
38
+ }
39
+ }
40
+
41
+ /** The document's memory, or null when it has none or it is unreadable. */
42
+ export function readDesignMemory(graph: SceneGraph): DesignMemory | null {
43
+ return readPluginData(graph.getNode(graph.rootId)?.pluginData, designMemory) ?? null
44
+ }
45
+
46
+ const MEMORIES = new WeakMap<
47
+ SceneGraph,
48
+ { entries: readonly PluginDataEntry[] | undefined; memory: DesignMemory | null }
49
+ >()
50
+
51
+ /**
52
+ * The document's memory, read once per state of its document node: an edit replaces the node's
53
+ * plugin data, so a check on every agent call parses nothing new.
54
+ */
55
+ export function cachedDesignMemory(graph: SceneGraph): DesignMemory | null {
56
+ const entries = graph.getNode(graph.rootId)?.pluginData
57
+ const known = MEMORIES.get(graph)
58
+ if (known && known.entries === entries) return known.memory
59
+ const memory = readDesignMemory(graph)
60
+ MEMORIES.set(graph, { entries, memory })
61
+ return memory
62
+ }
63
+
64
+ /** Stores the memory on the document node as an undoable change; `null` removes it. */
65
+ export function writeDesignMemory(graph: SceneGraph, memory: DesignMemory | null): void {
66
+ const root = graph.getNode(graph.rootId)
67
+ if (!root) return
68
+ graph.updateNode(root.id, {
69
+ pluginData: withPluginData(root.pluginData, designMemory, memory ?? undefined)
70
+ })
71
+ }
72
+
73
+ /** A value as a group keeps it, or null when it is not one: colors become `#RRGGBB`. */
74
+ export function normalizeDesignValue(
75
+ group: DesignMemoryGroup,
76
+ value: string | number
77
+ ): string | number | null {
78
+ if (group === 'colors') {
79
+ const color = typeof value === 'string' ? tryParseColor(value.trim()) : null
80
+ return color ? colorToHex(color) : null
81
+ }
82
+ if (group === 'fonts') {
83
+ const family = typeof value === 'string' ? value.trim() : ''
84
+ return family && family.length <= DESIGN_MEMORY_LIMITS.fontLength ? family : null
85
+ }
86
+ const number = typeof value === 'number' ? value : Number(value)
87
+ if (!Number.isFinite(number)) return null
88
+ // A text has a size; spacing and radii may be zero.
89
+ return number > 0 || (number === 0 && group !== 'fontSizes') ? number : null
90
+ }
91
+
92
+ /**
93
+ * The memory with `value` allowed in `group`, once; a value the group cannot read, or one past
94
+ * its limit, leaves it as it was.
95
+ */
96
+ export function allowDesignValue(
97
+ memory: DesignMemory,
98
+ group: DesignMemoryGroup,
99
+ value: string | number
100
+ ): DesignMemory {
101
+ const normalized = normalizeDesignValue(group, value)
102
+ const values: readonly (string | number)[] = memory[group].values
103
+ if (normalized === null || values.includes(normalized) || values.length >= GROUP_LIMITS[group])
104
+ return memory
105
+ return { ...memory, [group]: { ...memory[group], values: [...values, normalized] } }
106
+ }
107
+
108
+ /** The memory with `value` no longer allowed in `group` by itself. */
109
+ export function disallowDesignValue(
110
+ memory: DesignMemory,
111
+ group: DesignMemoryGroup,
112
+ value: string | number
113
+ ): DesignMemory {
114
+ const values: readonly (string | number)[] = memory[group].values
115
+ return {
116
+ ...memory,
117
+ [group]: { ...memory[group], values: values.filter((item) => item !== value) }
118
+ }
119
+ }
120
+
121
+ /** The memory with a decision added last; blank text or a full list leaves it as it was. */
122
+ export function addDesignDecision(
123
+ memory: DesignMemory,
124
+ text: string,
125
+ author: DesignDecisionAuthor
126
+ ): DesignMemory {
127
+ const trimmed = text.trim().slice(0, DESIGN_MEMORY_LIMITS.decisionLength).trim()
128
+ if (!trimmed || memory.decisions.length >= DESIGN_MEMORY_LIMITS.decisions) return memory
129
+ return {
130
+ ...memory,
131
+ decisions: [...memory.decisions, { id: randomHex(8), text: trimmed, author }]
132
+ }
133
+ }
134
+
135
+ /** The memory with a layer pinned as an example, once; a full list leaves it as it was. */
136
+ export function pinDesignExample(memory: DesignMemory, nodeId: string): DesignMemory {
137
+ if (
138
+ memory.examples.some((example) => example.nodeId === nodeId) ||
139
+ memory.examples.length >= DESIGN_MEMORY_LIMITS.examples
140
+ )
141
+ return memory
142
+ return { ...memory, examples: [...memory.examples, { nodeId }] }
143
+ }