@open-pencil/scene-graph 0.17.0-beta.2 → 0.17.0-beta.3

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 (244) hide show
  1. package/dist/asset-keys.d.ts +11 -1
  2. package/dist/asset-keys.d.ts.map +1 -1
  3. package/dist/asset-keys.js +16 -1
  4. package/dist/asset-keys.js.map +1 -1
  5. package/dist/checkpoint.d.ts +3 -3
  6. package/dist/checkpoint.d.ts.map +1 -1
  7. package/dist/checkpoint.js +53 -23
  8. package/dist/checkpoint.js.map +1 -1
  9. package/dist/components/index.d.ts +2 -2
  10. package/dist/components/index.js +2 -2
  11. package/dist/components/properties.d.ts +6 -1
  12. package/dist/components/properties.d.ts.map +1 -1
  13. package/dist/components/properties.js +9 -1
  14. package/dist/components/properties.js.map +1 -1
  15. package/dist/constants.d.ts +6 -1
  16. package/dist/constants.d.ts.map +1 -1
  17. package/dist/constants.js +17 -1
  18. package/dist/constants.js.map +1 -1
  19. package/dist/constraints.d.ts +25 -0
  20. package/dist/constraints.d.ts.map +1 -0
  21. package/dist/constraints.js +51 -0
  22. package/dist/constraints.js.map +1 -0
  23. package/dist/coordinate.d.ts +64 -1
  24. package/dist/coordinate.d.ts.map +1 -1
  25. package/dist/coordinate.js +217 -1
  26. package/dist/coordinate.js.map +1 -1
  27. package/dist/copy.d.ts.map +1 -1
  28. package/dist/copy.js +8 -1
  29. package/dist/copy.js.map +1 -1
  30. package/dist/css/index.d.ts +3 -2
  31. package/dist/css/index.js +3 -2
  32. package/dist/css/text.d.ts +28 -1
  33. package/dist/css/text.d.ts.map +1 -1
  34. package/dist/css/text.js +84 -17
  35. package/dist/css/text.js.map +1 -1
  36. package/dist/css/transform.d.ts +34 -0
  37. package/dist/css/transform.d.ts.map +1 -0
  38. package/dist/css/transform.js +320 -0
  39. package/dist/css/transform.js.map +1 -0
  40. package/dist/css/values.d.ts.map +1 -1
  41. package/dist/css/values.js +11 -1
  42. package/dist/css/values.js.map +1 -1
  43. package/dist/figjam/palette.d.ts +1 -1
  44. package/dist/figjam/palette.js +1 -1
  45. package/dist/figjam/palette.js.map +1 -1
  46. package/dist/geometry.d.ts +8 -1
  47. package/dist/geometry.d.ts.map +1 -1
  48. package/dist/geometry.js +19 -8
  49. package/dist/geometry.js.map +1 -1
  50. package/dist/graph.d.ts +48 -12
  51. package/dist/graph.d.ts.map +1 -1
  52. package/dist/graph.js +110 -24
  53. package/dist/graph.js.map +1 -1
  54. package/dist/group-bounds.d.ts +14 -4
  55. package/dist/group-bounds.d.ts.map +1 -1
  56. package/dist/group-bounds.js +76 -11
  57. package/dist/group-bounds.js.map +1 -1
  58. package/dist/hit-test.d.ts +5 -0
  59. package/dist/hit-test.d.ts.map +1 -1
  60. package/dist/hit-test.js +18 -12
  61. package/dist/hit-test.js.map +1 -1
  62. package/dist/images.d.ts +27 -3
  63. package/dist/images.d.ts.map +1 -1
  64. package/dist/images.js +57 -3
  65. package/dist/images.js.map +1 -1
  66. package/dist/index.d.ts +2 -2
  67. package/dist/instances/index.d.ts.map +1 -1
  68. package/dist/instances/index.js +3 -0
  69. package/dist/instances/index.js.map +1 -1
  70. package/dist/instances/placement.js +55 -12
  71. package/dist/instances/placement.js.map +1 -1
  72. package/dist/instances/swap.js +3 -0
  73. package/dist/instances/swap.js.map +1 -1
  74. package/dist/instances/sync.js +34 -13
  75. package/dist/instances/sync.js.map +1 -1
  76. package/dist/node-defaults.d.ts.map +1 -1
  77. package/dist/node-defaults.js +1 -0
  78. package/dist/node-defaults.js.map +1 -1
  79. package/dist/order-keys.d.ts +3 -1
  80. package/dist/order-keys.d.ts.map +1 -1
  81. package/dist/order-keys.js +6 -1
  82. package/dist/order-keys.js.map +1 -1
  83. package/dist/plugin-data/fields.d.ts +2 -0
  84. package/dist/plugin-data/fields.d.ts.map +1 -1
  85. package/dist/plugin-data/fields.js +2 -0
  86. package/dist/plugin-data/fields.js.map +1 -1
  87. package/dist/resize.d.ts +22 -11
  88. package/dist/resize.d.ts.map +1 -1
  89. package/dist/resize.js +227 -93
  90. package/dist/resize.js.map +1 -1
  91. package/dist/scaling/index.d.ts +3 -2
  92. package/dist/scaling/index.js +3 -2
  93. package/dist/scaling/node.d.ts.map +1 -1
  94. package/dist/scaling/node.js +5 -1
  95. package/dist/scaling/node.js.map +1 -1
  96. package/dist/scaling/snapshot.d.ts +25 -0
  97. package/dist/scaling/snapshot.d.ts.map +1 -0
  98. package/dist/scaling/snapshot.js +39 -0
  99. package/dist/scaling/snapshot.js.map +1 -0
  100. package/dist/scaling/tree.d.ts +24 -2
  101. package/dist/scaling/tree.d.ts.map +1 -1
  102. package/dist/scaling/tree.js +37 -14
  103. package/dist/scaling/tree.js.map +1 -1
  104. package/dist/shaders/model.d.ts.map +1 -1
  105. package/dist/shaders/model.js +2 -1
  106. package/dist/shaders/model.js.map +1 -1
  107. package/dist/shared-styles.d.ts +3 -1
  108. package/dist/shared-styles.d.ts.map +1 -1
  109. package/dist/shared-styles.js +7 -1
  110. package/dist/shared-styles.js.map +1 -1
  111. package/dist/slots/authoring.d.ts +3 -2
  112. package/dist/slots/authoring.d.ts.map +1 -1
  113. package/dist/slots/authoring.js +5 -3
  114. package/dist/slots/authoring.js.map +1 -1
  115. package/dist/slots/content.d.ts.map +1 -1
  116. package/dist/slots/content.js +4 -1
  117. package/dist/slots/content.js.map +1 -1
  118. package/dist/text/direction.d.ts +3 -1
  119. package/dist/text/direction.d.ts.map +1 -1
  120. package/dist/text/direction.js +5 -1
  121. package/dist/text/direction.js.map +1 -1
  122. package/dist/transfer/apply.d.ts.map +1 -1
  123. package/dist/transfer/apply.js +1 -6
  124. package/dist/transfer/apply.js.map +1 -1
  125. package/dist/transfer/copied.d.ts +35 -3
  126. package/dist/transfer/copied.d.ts.map +1 -1
  127. package/dist/transfer/copied.js +44 -13
  128. package/dist/transfer/copied.js.map +1 -1
  129. package/dist/transfer/dependencies.d.ts +38 -0
  130. package/dist/transfer/dependencies.d.ts.map +1 -0
  131. package/dist/transfer/dependencies.js +100 -0
  132. package/dist/transfer/dependencies.js.map +1 -0
  133. package/dist/transfer/history.js +0 -1
  134. package/dist/transfer/history.js.map +1 -1
  135. package/dist/transfer/index.d.ts +5 -3
  136. package/dist/transfer/index.js +5 -3
  137. package/dist/transfer/nodes.d.ts.map +1 -1
  138. package/dist/transfer/nodes.js +7 -14
  139. package/dist/transfer/nodes.js.map +1 -1
  140. package/dist/transfer/plan.d.ts +0 -1
  141. package/dist/transfer/plan.js +3 -5
  142. package/dist/transfer/plan.js.map +1 -1
  143. package/dist/transfer/remove.js +5 -2
  144. package/dist/transfer/remove.js.map +1 -1
  145. package/dist/transfer/resources.d.ts +21 -0
  146. package/dist/transfer/resources.d.ts.map +1 -0
  147. package/dist/transfer/resources.js +38 -0
  148. package/dist/transfer/resources.js.map +1 -0
  149. package/dist/transfer/variables.d.ts +8 -1
  150. package/dist/transfer/variables.d.ts.map +1 -1
  151. package/dist/transfer/variables.js +24 -1
  152. package/dist/transfer/variables.js.map +1 -1
  153. package/dist/types.d.ts +47 -2
  154. package/dist/types.d.ts.map +1 -1
  155. package/dist/types.js.map +1 -1
  156. package/dist/undo.d.ts +28 -3
  157. package/dist/undo.d.ts.map +1 -1
  158. package/dist/undo.js +24 -7
  159. package/dist/undo.js.map +1 -1
  160. package/dist/variables/bindings.d.ts +17 -14
  161. package/dist/variables/bindings.d.ts.map +1 -1
  162. package/dist/variables/bindings.js +30 -72
  163. package/dist/variables/bindings.js.map +1 -1
  164. package/dist/variables/fields.d.ts +1 -1
  165. package/dist/variables/fields.js +1 -1
  166. package/dist/variables/fields.js.map +1 -1
  167. package/dist/variables/formula/check.d.ts +1 -1
  168. package/dist/variables/formula/index.d.ts +2 -2
  169. package/dist/variables/formula/index.js +1 -1
  170. package/dist/variables/formula/index.js.map +1 -1
  171. package/dist/variables/formula/print.d.ts +1 -1
  172. package/dist/variables/formula/signatures.js +1 -1
  173. package/dist/variables/index.d.ts +6 -6
  174. package/dist/variables/index.d.ts.map +1 -1
  175. package/dist/variables/index.js +40 -24
  176. package/dist/variables/index.js.map +1 -1
  177. package/dist/variables/paints.d.ts +57 -0
  178. package/dist/variables/paints.d.ts.map +1 -0
  179. package/dist/variables/paints.js +231 -0
  180. package/dist/variables/paints.js.map +1 -0
  181. package/dist/variables/resolution.d.ts +14 -11
  182. package/dist/variables/resolution.d.ts.map +1 -1
  183. package/dist/variables/resolution.js +36 -17
  184. package/dist/variables/resolution.js.map +1 -1
  185. package/dist/variables/subtree.js +17 -0
  186. package/dist/variables/subtree.js.map +1 -0
  187. package/dist/vector-network.d.ts.map +1 -1
  188. package/dist/vector-network.js +2 -8
  189. package/dist/vector-network.js.map +1 -1
  190. package/package.json +8 -1
  191. package/src/asset-keys.ts +20 -0
  192. package/src/checkpoint.ts +87 -32
  193. package/src/components/properties.ts +9 -0
  194. package/src/constants.ts +17 -0
  195. package/src/constraints.ts +60 -0
  196. package/src/coordinate.ts +220 -0
  197. package/src/copy.ts +9 -4
  198. package/src/css/index.ts +17 -1
  199. package/src/css/text.ts +85 -17
  200. package/src/css/transform.ts +266 -0
  201. package/src/css/values.ts +8 -0
  202. package/src/figjam/palette.ts +1 -1
  203. package/src/geometry.ts +20 -9
  204. package/src/graph.ts +128 -42
  205. package/src/group-bounds.ts +84 -17
  206. package/src/hit-test.ts +30 -14
  207. package/src/images.ts +71 -3
  208. package/src/instances/index.ts +4 -0
  209. package/src/instances/placement.ts +63 -10
  210. package/src/instances/swap.ts +5 -0
  211. package/src/instances/sync.ts +48 -21
  212. package/src/node-defaults.ts +1 -0
  213. package/src/order-keys.ts +6 -0
  214. package/src/plugin-data/fields.ts +6 -0
  215. package/src/resize.ts +267 -119
  216. package/src/scaling/index.ts +9 -1
  217. package/src/scaling/node.ts +9 -1
  218. package/src/scaling/snapshot.ts +51 -0
  219. package/src/scaling/tree.ts +63 -19
  220. package/src/shaders/model.ts +2 -3
  221. package/src/shared-styles.ts +8 -0
  222. package/src/slots/authoring.ts +5 -3
  223. package/src/slots/content.ts +4 -1
  224. package/src/text/direction.ts +5 -0
  225. package/src/transfer/apply.ts +1 -13
  226. package/src/transfer/copied.ts +83 -15
  227. package/src/transfer/dependencies.ts +126 -0
  228. package/src/transfer/history.ts +0 -6
  229. package/src/transfer/index.ts +20 -2
  230. package/src/transfer/nodes.ts +10 -26
  231. package/src/transfer/plan.ts +3 -11
  232. package/src/transfer/remove.ts +5 -2
  233. package/src/transfer/resources.ts +54 -0
  234. package/src/transfer/variables.ts +34 -0
  235. package/src/types.ts +55 -1
  236. package/src/undo.ts +48 -10
  237. package/src/variables/bindings.ts +44 -91
  238. package/src/variables/fields.ts +1 -1
  239. package/src/variables/formula/index.ts +3 -1
  240. package/src/variables/index.ts +56 -38
  241. package/src/variables/paints.ts +296 -0
  242. package/src/variables/resolution.ts +39 -28
  243. package/src/variables/subtree.ts +15 -0
  244. package/src/vector-network.ts +4 -2
package/src/images.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import { sha1 } from '@noble/hashes/legacy.js'
2
2
  import { bytesToHex } from '@noble/hashes/utils.js'
3
3
  import { imageSize as readImageHeader } from 'image-size'
4
+ import { fromUint8Array, isValid, toUint8Array } from 'js-base64'
4
5
 
5
6
  import type { Matrix, Size } from './primitives'
7
+ import type { Fill } from './types'
6
8
 
7
9
  /**
8
10
  * The name an image's bytes are stored under: the hex SHA-1 of the bytes, as Figma names images,
@@ -13,14 +15,15 @@ export function computeImageHash(data: Uint8Array): string {
13
15
  return bytesToHex(sha1(data))
14
16
  }
15
17
 
16
- export type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp'
18
+ export type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp' | 'image/avif'
17
19
 
18
20
  /** The image types documents store, by the name `image-size` gives each format. */
19
21
  const IMAGE_MIME_TYPES: Partial<Record<string, ImageMimeType>> = {
20
22
  png: 'image/png',
21
23
  jpg: 'image/jpeg',
22
24
  gif: 'image/gif',
23
- webp: 'image/webp'
25
+ webp: 'image/webp',
26
+ avif: 'image/avif'
24
27
  }
25
28
 
26
29
  /** The raster types a dropped or pasted file can be imported as an image fill from. */
@@ -44,7 +47,7 @@ export interface ImageHeader extends Size {
44
47
  }
45
48
 
46
49
  /**
47
- * The type and size of a PNG, JPEG, GIF, or WebP, read from its header by `image-size`; null for
50
+ * The type and size of a PNG, JPEG, GIF, WebP, or AVIF, read from its header by `image-size`; null for
48
51
  * bytes it cannot read and for other formats.
49
52
  */
50
53
  export function imageHeader(data: Uint8Array): ImageHeader | null {
@@ -66,6 +69,49 @@ export function imageMimeType(data: Uint8Array): ImageMimeType {
66
69
  return imageHeader(data)?.type ?? 'image/png'
67
70
  }
68
71
 
72
+ export interface DataURL {
73
+ /** The media type in lowercase, without parameters; `text/plain` when the URL names none. */
74
+ mimeType: string
75
+ bytes: Uint8Array
76
+ }
77
+
78
+ /**
79
+ * The media type and bytes a `data:` URL carries: a Base64 body decoded, and a percent-encoded
80
+ * one unescaped as UTF-8 text, as SVG and other markup are written. Null for anything else: Base64
81
+ * cut short in transit, and escapes that are not UTF-8, such as a binary image escaped byte by
82
+ * byte, which a reader that must take any data URL, such as `.pen` loading, gets from `fetch`.
83
+ */
84
+ export function parseDataURL(url: string): DataURL | null {
85
+ const match = /^\s*data:([^,]*),(.*?)\s*$/is.exec(url)
86
+ if (!match) return null
87
+ const [, header = '', body = ''] = match
88
+ const params = header.split(';').map((param) => param.trim())
89
+ const base64 = params.length > 1 && params.at(-1)?.toLowerCase() === 'base64'
90
+ const mimeType = params[0]?.toLowerCase() || 'text/plain'
91
+ let text: string
92
+ try {
93
+ // A `%` that starts no escape is a literal percent sign, as in `width="100%"`.
94
+ text = decodeURIComponent(body.replace(/%(?![\da-f]{2})/gi, '%25'))
95
+ } catch {
96
+ return null
97
+ }
98
+ if (!base64) return { mimeType, bytes: new TextEncoder().encode(text) }
99
+ const compact = text.replace(/\s+/g, '')
100
+ // Base64 one character past a whole group of four cannot be bytes: it was cut short.
101
+ if (compact.replace(/={1,2}$/, '').length % 4 === 1 || !isValid(compact)) return null
102
+ return { mimeType, bytes: toUint8Array(compact) }
103
+ }
104
+
105
+ /** Encoded image bytes as a Base64 data URL of the type their header names. */
106
+ export function imageDataURL(data: Uint8Array): string {
107
+ return `data:${imageMimeType(data)};base64,${fromUint8Array(data)}`
108
+ }
109
+
110
+ /** SVG markup as a percent-encoded data URL, as CSS `url()` and `<img src>` take one. */
111
+ export function svgDataURL(svg: string): string {
112
+ return `data:image/svg+xml,${encodeURIComponent(svg)}`
113
+ }
114
+
69
115
  /** An image's size in pixels, read from its header; null for bytes it cannot read. */
70
116
  export function imageSize(data: Uint8Array): Size | null {
71
117
  const header = imageHeader(data)
@@ -87,3 +133,25 @@ export function coverImageTransform(
87
133
  const m11 = height / scale / imageHeight
88
134
  return { m00, m01: 0, m02: (1 - m00) / 2, m10: 0, m11, m12: (1 - m11) / 2 }
89
135
  }
136
+
137
+ /** The images `paints` show, by their hashes. */
138
+ export function imagePaintHashes(paints: Iterable<Fill>): string[] {
139
+ return [...paints].flatMap((paint) =>
140
+ paint.type === 'IMAGE' && paint.imageHash ? [paint.imageHash] : []
141
+ )
142
+ }
143
+
144
+ /**
145
+ * Copies the images `hashes` name from `from` into `to`, each as bytes of its own. An image is
146
+ * named by its content, so one `to` already holds stays.
147
+ */
148
+ export function copyImages(
149
+ from: ReadonlyMap<string, Uint8Array>,
150
+ to: Map<string, Uint8Array>,
151
+ hashes: Iterable<string>
152
+ ): void {
153
+ for (const hash of hashes) {
154
+ const bytes = from.get(hash)
155
+ if (bytes && !to.has(hash)) to.set(hash, new Uint8Array(bytes))
156
+ }
157
+ }
@@ -6,6 +6,7 @@ import {
6
6
  type ParkedSlotContent
7
7
  } from '../slots/frames'
8
8
  import type { SceneNode } from '../types'
9
+ import { applyBoundPaintColorsIn, keepCopyBoundPaints } from '../variables/paints'
9
10
  import { keepOwnContentSize } from './content-size'
10
11
  import { createInstanceSyncContext, groupCopyRemovals, removeLeftoverCopies } from './copies'
11
12
  import { canCreateInstance } from './cycles'
@@ -65,6 +66,8 @@ export function createInstance(
65
66
  const instance = graph.createNode('INSTANCE', parentId, { ...props, ...overrides, componentId })
66
67
 
67
68
  cloneChildrenWithMapping(graph, component.id, instance.id)
69
+ // A copy shows its bound paints in its own context, not as its component's paints hold them.
70
+ applyBoundPaintColorsIn(graph, instance.id)
68
71
 
69
72
  return instance
70
73
  }
@@ -152,6 +155,7 @@ export function swapInstanceComponent(
152
155
  if (!keepName && (!previousComponent || instance.name === previousComponent.name))
153
156
  updates.name = component.name
154
157
 
158
+ keepCopyBoundPaints(graph, instance, updates)
155
159
  const slotContent = detachOwnedSlotContent(graph, instance)
156
160
  graph.updateNode(instanceId, updates)
157
161
  followComponentLayers(graph, instance, component, parkedContent(slotContent))
@@ -1,29 +1,82 @@
1
+ import { getNodeLocalMatrix } from '../coordinate'
2
+ import type { SceneGraph } from '../graph'
3
+ import { groupFitChanges } from '../group-bounds'
1
4
  import { enforcedAspectRatio } from '../layout/aspect-ratio'
2
- import { layoutConstrainedChildRect } from '../resize'
5
+ import Matrix, { type Mat3 } from '../matrix'
6
+ import { FITTED_CONTAINER_TYPES } from '../node-defaults'
7
+ import { constrainedChildRect, flipChanges } from '../resize'
3
8
  import type { SceneNode } from '../types'
4
9
 
5
- /** The fields that say where a copy sits in its parent. */
6
- export const PLACEMENT_FIELDS: ReadonlySet<string> = new Set(['x', 'y', 'width', 'height'])
10
+ /** The fields that say where a copy sits in its parent, and which way its constraints turned it. */
11
+ export const PLACEMENT_FIELDS: ReadonlySet<string> = new Set([
12
+ 'x',
13
+ 'y',
14
+ 'width',
15
+ 'height',
16
+ 'flipX',
17
+ 'flipY'
18
+ ])
19
+
20
+ /** The frame around the groups and booleans holding a layer, and their space mapped into its. */
21
+ function frameAround(
22
+ graph: SceneGraph,
23
+ parent: SceneNode
24
+ ): { frame: SceneNode; toFrame: Mat3 } | null {
25
+ let toFrame = Matrix.identity()
26
+ const frame = graph.closest(parent.id, (node) => {
27
+ if (!FITTED_CONTAINER_TYPES.has(node.type)) return true
28
+ toFrame = Matrix.multiply(getNodeLocalMatrix(node), toFrame)
29
+ return false
30
+ })
31
+ return frame ? { frame, toFrame } : null
32
+ }
7
33
 
8
34
  /**
9
35
  * The component's layer placed in the copy's parent: where its constraints put it when the
10
- * instance's frame has another size than the component's, as Figma resizes an instance.
36
+ * instance's frame has another size than the component's, as Figma resizes an instance. A group
37
+ * or boolean owns no constraints: it keeps the component's place, and a layer in it is placed in
38
+ * the frame around it and written back in the copy's group, which `fitCopiedGroup` then fits.
39
+ * Returns `source` itself when the layer stays where the component's is.
11
40
  */
12
41
  export function placedByConstraints(
42
+ graph: SceneGraph,
13
43
  source: SceneNode,
14
44
  compParent: SceneNode,
15
45
  instParent: SceneNode,
16
46
  factor: number
17
47
  ): SceneNode {
18
- const before = { width: compParent.width * factor, height: compParent.height * factor }
19
- if (before.width === instParent.width && before.height === instParent.height) return source
20
- const rect = layoutConstrainedChildRect(
21
- source,
48
+ if (FITTED_CONTAINER_TYPES.has(source.type)) return source
49
+ const comp = frameAround(graph, compParent)
50
+ const inst = frameAround(graph, instParent)
51
+ if (!comp || !inst) return source
52
+ const before = { width: comp.frame.width * factor, height: comp.frame.height * factor }
53
+ if (before.width === inst.frame.width && before.height === inst.frame.height) return source
54
+ // `source` is at the copy's scale, so the component's groups map it at that scale too.
55
+ const toFrame = Matrix.multiply(
56
+ Matrix.scaled(factor, factor),
57
+ Matrix.multiply(comp.toFrame, Matrix.scaled(1 / factor, 1 / factor))
58
+ )
59
+ const { flipsX, flipsY, ...box } = constrainedChildRect(
60
+ { ...Matrix.mapPoint(toFrame, source), width: source.width, height: source.height },
22
61
  before,
23
- instParent,
62
+ inst.frame,
24
63
  source.horizontalConstraint,
25
64
  source.verticalConstraint,
26
65
  enforcedAspectRatio(source)
27
66
  )
28
- return { ...source, ...rect }
67
+ const back = Matrix.invert(inst.toFrame)
68
+ const position = back ? Matrix.mapPoint(back, box) : box
69
+ const placed = { ...source, ...box, ...position, ...flipChanges(source, source, flipsX, flipsY) }
70
+ const moved = [...PLACEMENT_FIELDS].some(
71
+ (key) => placed[key as keyof SceneNode] !== source[key as keyof SceneNode]
72
+ )
73
+ return moved ? placed : source
74
+ }
75
+
76
+ /** Fits a copied group or boolean to its layers after the sync placed them by their constraints. */
77
+ export function fitCopiedGroup(graph: SceneGraph, groupId: string): void {
78
+ const group = graph.getNode(groupId)
79
+ if (!group || !FITTED_CONTAINER_TYPES.has(group.type)) return
80
+ const changes = groupFitChanges(graph, group, graph.getChildren(groupId))
81
+ for (const [id, change] of changes ?? []) graph.updateNode(id, change)
29
82
  }
@@ -7,6 +7,7 @@ import {
7
7
  import type { SceneGraph } from '../graph'
8
8
  import { ownsSlotContent, slotPropertyId } from '../slots/frames'
9
9
  import type { SceneNode } from '../types'
10
+ import { applyBoundPaintColorsIn, refreshBoundPaintsIn } from '../variables/paints'
10
11
  import { applyEnclosingAssignments } from './assignments'
11
12
  import { keepOverrides, restoreOverrides, type OverrideScope } from './kept-overrides'
12
13
  import { ownsSelfFillField } from './main-component'
@@ -291,6 +292,8 @@ function cloneLayer(
291
292
  if (layer.childIds.length > 0) {
292
293
  cloneChildrenWithMapping(swap.graph, layer.id, clone.id)
293
294
  }
295
+ // A new copy shows its variables where it sits; overrides restored after this keep theirs.
296
+ applyBoundPaintColorsIn(swap.graph, clone.id)
294
297
  return clone
295
298
  }
296
299
 
@@ -395,6 +398,8 @@ function followLayers(swap: Swap, compParent: SceneNode, instParent: SceneNode):
395
398
  // A new copy takes the values the instances around it assign, as one a sync adds does.
396
399
  applyEnclosingAssignments(graph, copy)
397
400
  restoreOverrides(overrideScope(swap), copy)
401
+ // A mode restored on the copy changes what its unclaimed bound paints resolve to.
402
+ refreshBoundPaintsIn(graph, copy.id)
398
403
  }
399
404
  })
400
405
  }
@@ -10,7 +10,16 @@ import { scaleNodeChanges } from '../scaling/node'
10
10
  import { ownsSlotContent, slotPropertyId } from '../slots/frames'
11
11
  import type { SceneNode } from '../types'
12
12
  import { resolvedNumericBindings } from '../variables/bindings'
13
- import { effectBindingTarget, isNumericVariableBindingField } from '../variables/fields'
13
+ import {
14
+ effectBindingTarget,
15
+ isNumericVariableBindingField,
16
+ paintBindingTarget
17
+ } from '../variables/fields'
18
+ import {
19
+ applyBoundPaintColorsIn,
20
+ copyAppliedBoundPaints,
21
+ keepCopyBoundPaints
22
+ } from '../variables/paints'
14
23
  import { scaleVariableBindingUnits } from '../variables/units'
15
24
  import { applyEnclosingAssignments, assignedFieldValues, propertyDrivenFields } from './assignments'
16
25
  import { keepOwnContentSize, remeasureChangedText, type SyncedPlacement } from './content-size'
@@ -31,7 +40,7 @@ import {
31
40
  setInstanceOverride,
32
41
  type InstanceOverrideState
33
42
  } from './overrides'
34
- import { PLACEMENT_FIELDS, placedByConstraints } from './placement'
43
+ import { fitCopiedGroup, PLACEMENT_FIELDS, placedByConstraints } from './placement'
35
44
  import { followVectorGeometry } from './vector-geometry'
36
45
 
37
46
  function setSceneProp<K extends keyof SceneNode>(
@@ -84,9 +93,12 @@ export function bindingProtection(scopes: ReadonlySet<string>[]): (field: string
84
93
  }
85
94
  return (field) => {
86
95
  if (fields.has(field)) return true
87
- // Effects an instance set itself own their bindings too, bound or not, as its fills do.
96
+ // Paints and effects an instance set itself own their bindings too, bound or not.
88
97
  const binding = overriddenBindingField(field)
89
- return binding !== null && fields.has('effects') && effectBindingTarget(binding) !== null
98
+ if (binding === null) return false
99
+ const paint = paintBindingTarget(binding)
100
+ if (paint) return fields.has(paint.list)
101
+ return fields.has('effects') && effectBindingTarget(binding) !== null
90
102
  }
91
103
  }
92
104
 
@@ -163,6 +175,7 @@ interface SyncedFieldsInput {
163
175
 
164
176
  /** The fields a copy takes from its component's layer, placed in the copy's parent. */
165
177
  function copySyncedFields(
178
+ graph: SceneGraph,
166
179
  updates: Partial<SceneNode>,
167
180
  copy: SceneNode,
168
181
  source: SceneNode,
@@ -172,7 +185,7 @@ function copySyncedFields(
172
185
  const placedByLayout = instParent.layoutMode !== 'NONE' && source.layoutPositioning !== 'ABSOLUTE'
173
186
  const placed = placedByLayout
174
187
  ? source
175
- : placedByConstraints(source, compParent, instParent, factor)
188
+ : placedByConstraints(graph, source, compParent, instParent, factor)
176
189
  for (const { key, owner, placement, position } of SYNCED_FIELDS) {
177
190
  if ((placedByLayout && position) || skip(key) || (owner && skip(owner))) {
178
191
  continue
@@ -200,6 +213,10 @@ export function updateSyncedProps(
200
213
  kept: (field: string) => boolean = () => false
201
214
  ): void {
202
215
  resolveBoundNumbersInPlace(graph, target, updates, kept)
216
+ if (updates.fills || updates.strokes || updates.boundVariables) {
217
+ applySyncedModes(graph, target, updates)
218
+ keepCopyBoundPaints(graph, target, updates)
219
+ }
203
220
  let changed: Partial<SceneNode> | undefined
204
221
  for (const key of Object.keys(updates)) {
205
222
  const current = target[key as keyof SceneNode]
@@ -240,13 +257,17 @@ function resolveBoundNumbersInPlace(
240
257
  (field) => isNumericVariableBindingField(field) && bound[field] && !kept(field)
241
258
  )
242
259
  if (synced.length === 0) return
243
- // The modes the copy takes in this sync decide what its bindings resolve to.
244
- if (updates.variableModes && !isEqual(updates.variableModes, target.variableModes))
245
- graph.updateNode(target.id, { variableModes: updates.variableModes })
260
+ applySyncedModes(graph, target, updates)
246
261
  const resolved = resolvedNumericBindings(graph, { ...target, ...updates })
247
262
  Object.assign(updates, pick(resolved, synced as (keyof SceneNode)[]))
248
263
  }
249
264
 
265
+ /** The modes the copy takes in this sync decide what its bindings resolve to. */
266
+ function applySyncedModes(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>): void {
267
+ if (updates.variableModes && !isEqual(updates.variableModes, target.variableModes))
268
+ graph.updateNode(target.id, { variableModes: updates.variableModes })
269
+ }
270
+
250
271
  export function cloneChildInCoordinates(
251
272
  graph: SceneGraph,
252
273
  src: SceneNode,
@@ -256,10 +277,12 @@ export function cloneChildInCoordinates(
256
277
  ): SceneNode {
257
278
  const componentScale =
258
279
  (src.componentScale * targetParent.componentScale) / sourceParent.componentScale
259
- return graph.createNode(src.type, targetParent.id, {
280
+ const clone = graph.createNode(src.type, targetParent.id, {
260
281
  ...cloneNodeProps(sourceInTargetCoordinates(src, componentScale), src.id, mode),
261
282
  componentScale
262
283
  })
284
+ copyAppliedBoundPaints(src, clone)
285
+ return clone
263
286
  }
264
287
 
265
288
  export function cloneChildrenWithMapping(
@@ -415,7 +438,7 @@ export function syncCopy(
415
438
  { compParent, instParent, compChild, instChild }: MatchedCopy,
416
439
  protectedField: (field: string) => boolean,
417
440
  linkedLayerId?: string
418
- ): void {
441
+ ): boolean {
419
442
  const driven = propertyDrivenFields(graph, instChild, compChild)
420
443
  const kept =
421
444
  driven.size === 0 && protectedField === NOTHING_PROTECTED
@@ -438,7 +461,7 @@ export function syncCopy(
438
461
  updates.componentId = linkedLayerId
439
462
  }
440
463
  syncBindingFields(instChild, source, updates, protectedField)
441
- const placement = copySyncedFields(updates, instChild, source, {
464
+ const placement = copySyncedFields(graph, updates, instChild, source, {
442
465
  compParent,
443
466
  instParent,
444
467
  factor: componentScale / compChild.componentScale,
@@ -447,6 +470,7 @@ export function syncCopy(
447
470
  Object.assign(updates, assignedFieldValues(graph, instChild, compChild))
448
471
  keepOwnContentSize(graph, updates, instChild, source, placement)
449
472
  updateSyncedProps(graph, instChild, updates, kept)
473
+ return placement.placed !== source
450
474
  }
451
475
 
452
476
  /** True when syncing `compParentId` into `instParentId` would form a cycle. */
@@ -510,6 +534,7 @@ function placeMissingChildren(
510
534
  cloneChildrenWithMapping(graph, compChildId, clone.id)
511
535
  }
512
536
  applyEnclosingAssignments(graph, clone)
537
+ applyBoundPaintColorsIn(graph, clone.id)
513
538
  instChildMap.set(compChildId, clone)
514
539
  usedInstChildIds.add(clone.id)
515
540
  context.matched.add(clone.id)
@@ -525,13 +550,13 @@ export function syncChildren(
525
550
  instParentId: string,
526
551
  overrides: InstanceOverrideState,
527
552
  context: InstanceSyncContext = createInstanceSyncContext(instParentId, overrides)
528
- ): void {
553
+ ): boolean {
529
554
  // An instance inside its own component would clone the component into itself without end.
530
- if (isCyclicSync(graph, compParentId, instParentId)) return
555
+ if (isCyclicSync(graph, compParentId, instParentId)) return false
531
556
 
532
557
  const compParent = graph.nodes.get(compParentId)
533
558
  const instParent = graph.nodes.get(instParentId)
534
- if (!compParent || !instParent) return
559
+ if (!compParent || !instParent) return false
535
560
 
536
561
  const instChildMap = new Map<string, SceneNode>()
537
562
  const usedInstChildIds = new Set<string>()
@@ -562,17 +587,16 @@ export function syncChildren(
562
587
  placeMissingChildren(graph, compParent, instParent, instChildMap, usedInstChildIds, context)
563
588
 
564
589
  // Pass 4: Synchronize properties and recurse
590
+ let constrained = false
565
591
  for (const compChildId of compParent.childIds) {
566
592
  const compChild = graph.nodes.get(compChildId)
567
593
  const instChild = instChildMap.get(compChildId)
568
594
  if (!compChild || !instChild) continue
569
595
 
570
- syncCopy(
571
- graph,
572
- { compParent, instParent, compChild, instChild },
573
- childBindingProtection(graph, instChild, overrides)
574
- )
575
-
596
+ const match = { compParent, instParent, compChild, instChild }
597
+ const protection = childBindingProtection(graph, instChild, overrides)
598
+ // True when its constraints placed the copy elsewhere than the component's layer.
599
+ if (syncCopy(graph, match, protection)) constrained = true
576
600
  if (
577
601
  compChild.childIds.length > 0 &&
578
602
  !hasNodeInstanceOverride(overrides, instParentId, instChild.id, 'componentId') &&
@@ -580,10 +604,13 @@ export function syncChildren(
580
604
  // its bindings are not synced.
581
605
  !ownsSlotContent(graph, instChild, slotPropertyId(compChild))
582
606
  ) {
583
- syncChildren(graph, compChildId, instChild.id, overrides, context)
607
+ if (syncChildren(graph, compChildId, instChild.id, overrides, context)) constrained = true
584
608
  }
585
609
  }
586
610
 
587
611
  // Pass 5: Sort instance children to match component child order
588
612
  sortInstanceChildren(graph, instParent, instParentId, compParent.childIds, overrides)
613
+ // A group fits layers its constraints moved; else it keeps the component's box, as a boolean must.
614
+ if (constrained) fitCopiedGroup(graph, instParentId)
615
+ return constrained
589
616
  }
@@ -205,6 +205,7 @@ export function createDefaultNode(
205
205
  sharedSymbolVersion: null,
206
206
  publishedVersion: null,
207
207
  librarySource: null,
208
+ libraryLayerId: null,
208
209
  isPublishable: false,
209
210
  isSymbolPublishable: false,
210
211
  isExposedInstance: false,
package/src/order-keys.ts CHANGED
@@ -9,6 +9,12 @@ const ORDER_KEY_MAX = 126 // '~'
9
9
  const ORDER_KEY_MID = 'O'
10
10
  const INDEX_KEY_BASE = ORDER_KEY_MAX - ORDER_KEY_MIN + 1
11
11
 
12
+ /** Sorts order keys as Figma does: by plain string comparison. */
13
+ export function compareOrderKeys(a: string, b: string): number {
14
+ if (a === b) return 0
15
+ return a < b ? -1 : 1
16
+ }
17
+
12
18
  /** The order key for position `index` of a freshly numbered sibling list. */
13
19
  export function fractionalPosition(index: number): string {
14
20
  const numTildes = Math.floor(index / INDEX_KEY_BASE)
@@ -171,6 +171,12 @@ export const OPEN_PENCIL_PLUGIN_DATA = {
171
171
  textPathBox: jsonPluginDataField('textPathBox', 'format', textPathBoxSchema),
172
172
  /** The library asset a node was inserted from. */
173
173
  librarySource: jsonPluginDataField('librarySource', 'bookkeeping', librarySourceSchema),
174
+ /** The library source layer a library definition's layer copies, on every definition layer. */
175
+ libraryLayer: jsonPluginDataField(
176
+ 'libraryLayer',
177
+ 'bookkeeping',
178
+ v.pipe(v.string(), v.nonEmpty(), v.maxLength(1024))
179
+ ),
174
180
  /**
175
181
  * The document root's id, on the DOCUMENT node, whose own GUID is always Figma's `0:0`; the
176
182
  * root keeps it across a save as every layer keeps its GUID.