@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.6

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 (345) hide show
  1. package/dist/buffered-events.js +1 -1
  2. package/dist/buffered-events.js.map +1 -1
  3. package/dist/clipping.d.ts +7 -1
  4. package/dist/clipping.d.ts.map +1 -1
  5. package/dist/clipping.js +23 -19
  6. package/dist/clipping.js.map +1 -1
  7. package/dist/comments/schema.d.ts.map +1 -1
  8. package/dist/comments/schema.js +1 -0
  9. package/dist/comments/schema.js.map +1 -1
  10. package/dist/comments/threads.d.ts +2 -0
  11. package/dist/comments/threads.d.ts.map +1 -1
  12. package/dist/comments/threads.js +11 -5
  13. package/dist/comments/threads.js.map +1 -1
  14. package/dist/components/index.d.ts +2 -2
  15. package/dist/components/index.js +2 -2
  16. package/dist/components/properties.d.ts +1 -3
  17. package/dist/components/properties.d.ts.map +1 -1
  18. package/dist/components/properties.js +3 -10
  19. package/dist/components/properties.js.map +1 -1
  20. package/dist/constants.d.ts +12 -2
  21. package/dist/constants.d.ts.map +1 -1
  22. package/dist/constants.js +42 -1
  23. package/dist/constants.js.map +1 -1
  24. package/dist/copy.d.ts.map +1 -1
  25. package/dist/copy.js +2 -0
  26. package/dist/copy.js.map +1 -1
  27. package/dist/corners.d.ts +22 -0
  28. package/dist/corners.d.ts.map +1 -0
  29. package/dist/corners.js +52 -0
  30. package/dist/corners.js.map +1 -0
  31. package/dist/css/grid.d.ts +4 -1
  32. package/dist/css/grid.d.ts.map +1 -1
  33. package/dist/css/grid.js +40 -7
  34. package/dist/css/grid.js.map +1 -1
  35. package/dist/css/index.d.ts +3 -3
  36. package/dist/css/index.js +3 -3
  37. package/dist/css/layout.d.ts +13 -1
  38. package/dist/css/layout.d.ts.map +1 -1
  39. package/dist/css/layout.js +26 -1
  40. package/dist/css/layout.js.map +1 -1
  41. package/dist/css/values.d.ts +23 -1
  42. package/dist/css/values.d.ts.map +1 -1
  43. package/dist/css/values.js +39 -1
  44. package/dist/css/values.js.map +1 -1
  45. package/dist/font-style.d.ts +2 -1
  46. package/dist/font-style.d.ts.map +1 -1
  47. package/dist/font-style.js +31 -6
  48. package/dist/font-style.js.map +1 -1
  49. package/dist/geometry.d.ts +68 -3
  50. package/dist/geometry.d.ts.map +1 -1
  51. package/dist/geometry.js +159 -25
  52. package/dist/geometry.js.map +1 -1
  53. package/dist/gradient.d.ts +9 -2
  54. package/dist/gradient.d.ts.map +1 -1
  55. package/dist/gradient.js +29 -1
  56. package/dist/gradient.js.map +1 -1
  57. package/dist/graph.d.ts +9 -2
  58. package/dist/graph.d.ts.map +1 -1
  59. package/dist/graph.js +46 -10
  60. package/dist/graph.js.map +1 -1
  61. package/dist/group-bounds.d.ts.map +1 -1
  62. package/dist/group-bounds.js +4 -3
  63. package/dist/group-bounds.js.map +1 -1
  64. package/dist/guides.d.ts +11 -2
  65. package/dist/guides.d.ts.map +1 -1
  66. package/dist/guides.js +30 -1
  67. package/dist/guides.js.map +1 -1
  68. package/dist/hit-test.d.ts.map +1 -1
  69. package/dist/hit-test.js +57 -5
  70. package/dist/hit-test.js.map +1 -1
  71. package/dist/icons/fit.d.ts +17 -0
  72. package/dist/icons/fit.d.ts.map +1 -0
  73. package/dist/icons/fit.js +21 -0
  74. package/dist/icons/fit.js.map +1 -0
  75. package/dist/icons/glyph.d.ts.map +1 -1
  76. package/dist/icons/glyph.js +5 -4
  77. package/dist/icons/glyph.js.map +1 -1
  78. package/dist/icons/index.d.ts +2 -1
  79. package/dist/icons/index.js +2 -1
  80. package/dist/ids.js +1 -1
  81. package/dist/ids.js.map +1 -1
  82. package/dist/images.d.ts +14 -4
  83. package/dist/images.d.ts.map +1 -1
  84. package/dist/images.js +45 -5
  85. package/dist/images.js.map +1 -1
  86. package/dist/index.d.ts +2 -2
  87. package/dist/instances/assignments.js +39 -6
  88. package/dist/instances/assignments.js.map +1 -1
  89. package/dist/instances/index.d.ts +6 -1
  90. package/dist/instances/index.d.ts.map +1 -1
  91. package/dist/instances/index.js +58 -9
  92. package/dist/instances/index.js.map +1 -1
  93. package/dist/instances/kept-overrides.js +2 -2
  94. package/dist/instances/kept-overrides.js.map +1 -1
  95. package/dist/instances/matching.js +68 -0
  96. package/dist/instances/matching.js.map +1 -0
  97. package/dist/instances/overrides.d.ts +4 -1
  98. package/dist/instances/overrides.d.ts.map +1 -1
  99. package/dist/instances/overrides.js +13 -1
  100. package/dist/instances/overrides.js.map +1 -1
  101. package/dist/instances/protection.js +67 -0
  102. package/dist/instances/protection.js.map +1 -0
  103. package/dist/instances/swap.js +7 -5
  104. package/dist/instances/swap.js.map +1 -1
  105. package/dist/instances/sync.js +34 -116
  106. package/dist/instances/sync.js.map +1 -1
  107. package/dist/layout/guides.d.ts +17 -1
  108. package/dist/layout/guides.d.ts.map +1 -1
  109. package/dist/layout/guides.js +31 -11
  110. package/dist/layout/guides.js.map +1 -1
  111. package/dist/layout/index.d.ts +5 -2
  112. package/dist/layout/index.js +5 -2
  113. package/dist/layout/limits.d.ts +40 -0
  114. package/dist/layout/limits.d.ts.map +1 -0
  115. package/dist/layout/limits.js +54 -0
  116. package/dist/layout/limits.js.map +1 -0
  117. package/dist/layout/sizing.d.ts.map +1 -1
  118. package/dist/layout/sizing.js +0 -1
  119. package/dist/layout/sizing.js.map +1 -1
  120. package/dist/layout/spacing.d.ts +10 -0
  121. package/dist/layout/spacing.d.ts.map +1 -0
  122. package/dist/layout/spacing.js +12 -0
  123. package/dist/layout/spacing.js.map +1 -0
  124. package/dist/layout/strokes.d.ts +10 -0
  125. package/dist/layout/strokes.d.ts.map +1 -0
  126. package/dist/layout/strokes.js +30 -0
  127. package/dist/layout/strokes.js.map +1 -0
  128. package/dist/masks.d.ts +28 -0
  129. package/dist/masks.d.ts.map +1 -0
  130. package/dist/masks.js +53 -0
  131. package/dist/masks.js.map +1 -0
  132. package/dist/matrix.d.ts +2 -1
  133. package/dist/matrix.d.ts.map +1 -1
  134. package/dist/matrix.js +14 -1
  135. package/dist/matrix.js.map +1 -1
  136. package/dist/node-defaults.d.ts +9 -1
  137. package/dist/node-defaults.d.ts.map +1 -1
  138. package/dist/node-defaults.js +65 -38
  139. package/dist/node-defaults.js.map +1 -1
  140. package/dist/outline/flatten.d.ts.map +1 -1
  141. package/dist/outline/flatten.js +24 -17
  142. package/dist/outline/flatten.js.map +1 -1
  143. package/dist/outline/hit.d.ts +2 -8
  144. package/dist/outline/hit.d.ts.map +1 -1
  145. package/dist/outline/hit.js +16 -25
  146. package/dist/outline/hit.js.map +1 -1
  147. package/dist/outline/index.d.ts +3 -3
  148. package/dist/outline/index.js +3 -3
  149. package/dist/outline/shapes.d.ts +3 -1
  150. package/dist/outline/shapes.d.ts.map +1 -1
  151. package/dist/outline/shapes.js +18 -39
  152. package/dist/outline/shapes.js.map +1 -1
  153. package/dist/paints/index.d.ts +3 -1
  154. package/dist/paints/index.js +3 -1
  155. package/dist/paints/pattern.d.ts +13 -0
  156. package/dist/paints/pattern.d.ts.map +1 -0
  157. package/dist/paints/pattern.js +47 -0
  158. package/dist/paints/pattern.js.map +1 -0
  159. package/dist/paints/shows.d.ts +11 -0
  160. package/dist/paints/shows.d.ts.map +1 -0
  161. package/dist/paints/shows.js +16 -0
  162. package/dist/paints/shows.js.map +1 -0
  163. package/dist/parse-path.d.ts +4 -1
  164. package/dist/parse-path.d.ts.map +1 -1
  165. package/dist/parse-path.js +15 -1
  166. package/dist/parse-path.js.map +1 -1
  167. package/dist/paths/arc.d.ts +32 -0
  168. package/dist/paths/arc.d.ts.map +1 -0
  169. package/dist/paths/arc.js +49 -0
  170. package/dist/paths/arc.js.map +1 -0
  171. package/dist/paths/index.d.ts +5 -0
  172. package/dist/paths/index.js +5 -0
  173. package/dist/paths/network.d.ts +63 -0
  174. package/dist/paths/network.d.ts.map +1 -0
  175. package/dist/paths/network.js +315 -0
  176. package/dist/paths/network.js.map +1 -0
  177. package/dist/paths/sink.d.ts +20 -0
  178. package/dist/paths/sink.d.ts.map +1 -0
  179. package/dist/paths/sink.js +32 -0
  180. package/dist/paths/sink.js.map +1 -0
  181. package/dist/paths/smooth-corners.d.ts +25 -0
  182. package/dist/paths/smooth-corners.d.ts.map +1 -0
  183. package/dist/paths/smooth-corners.js +196 -0
  184. package/dist/paths/smooth-corners.js.map +1 -0
  185. package/dist/plugin-data/fields.d.ts +4 -0
  186. package/dist/plugin-data/fields.d.ts.map +1 -1
  187. package/dist/plugin-data/fields.js +2 -0
  188. package/dist/plugin-data/fields.js.map +1 -1
  189. package/dist/polygon.d.ts +1 -3
  190. package/dist/polygon.d.ts.map +1 -1
  191. package/dist/polygon.js +1 -16
  192. package/dist/polygon.js.map +1 -1
  193. package/dist/primitives/index.d.ts +1 -7
  194. package/dist/primitives/index.d.ts.map +1 -1
  195. package/dist/resize.d.ts.map +1 -1
  196. package/dist/resize.js +5 -5
  197. package/dist/resize.js.map +1 -1
  198. package/dist/scaling/node.d.ts.map +1 -1
  199. package/dist/scaling/node.js +25 -3
  200. package/dist/scaling/node.js.map +1 -1
  201. package/dist/shaders/model.d.ts.map +1 -1
  202. package/dist/shaders/model.js +1 -1
  203. package/dist/shaders/model.js.map +1 -1
  204. package/dist/shared-styles.d.ts.map +1 -1
  205. package/dist/shared-styles.js +2 -9
  206. package/dist/shared-styles.js.map +1 -1
  207. package/dist/strokes.d.ts +39 -0
  208. package/dist/strokes.d.ts.map +1 -0
  209. package/dist/strokes.js +104 -0
  210. package/dist/strokes.js.map +1 -0
  211. package/dist/text/auto-resize.d.ts +15 -0
  212. package/dist/text/auto-resize.d.ts.map +1 -0
  213. package/dist/text/auto-resize.js +23 -0
  214. package/dist/text/auto-resize.js.map +1 -0
  215. package/dist/text/index.d.ts +5 -3
  216. package/dist/text/index.js +5 -3
  217. package/dist/text/lengths.d.ts +6 -1
  218. package/dist/text/lengths.d.ts.map +1 -1
  219. package/dist/text/lengths.js +10 -1
  220. package/dist/text/lengths.js.map +1 -1
  221. package/dist/text/overflow.d.ts +14 -5
  222. package/dist/text/overflow.d.ts.map +1 -1
  223. package/dist/text/overflow.js +24 -6
  224. package/dist/text/overflow.js.map +1 -1
  225. package/dist/text/path.js +10 -0
  226. package/dist/text/path.js.map +1 -0
  227. package/dist/text/picture.js +5 -5
  228. package/dist/text/picture.js.map +1 -1
  229. package/dist/text/runs.d.ts +29 -0
  230. package/dist/text/runs.d.ts.map +1 -0
  231. package/dist/text/runs.js +72 -0
  232. package/dist/text/runs.js.map +1 -0
  233. package/dist/transfer/dependencies.d.ts.map +1 -1
  234. package/dist/transfer/dependencies.js +3 -2
  235. package/dist/transfer/dependencies.js.map +1 -1
  236. package/dist/transfer/nodes.js +2 -1
  237. package/dist/transfer/nodes.js.map +1 -1
  238. package/dist/types.d.ts +56 -10
  239. package/dist/types.d.ts.map +1 -1
  240. package/dist/types.js.map +1 -1
  241. package/dist/variables/index.d.ts +4 -3
  242. package/dist/variables/index.d.ts.map +1 -1
  243. package/dist/variables/index.js +4 -3
  244. package/dist/variables/index.js.map +1 -1
  245. package/dist/variables/lookup.d.ts +20 -0
  246. package/dist/variables/lookup.d.ts.map +1 -0
  247. package/dist/variables/lookup.js +34 -0
  248. package/dist/variables/lookup.js.map +1 -0
  249. package/dist/variables/paints.d.ts +35 -4
  250. package/dist/variables/paints.d.ts.map +1 -1
  251. package/dist/variables/paints.js +67 -16
  252. package/dist/variables/paints.js.map +1 -1
  253. package/dist/variables/resolution.d.ts +3 -1
  254. package/dist/variables/resolution.d.ts.map +1 -1
  255. package/dist/variables/resolution.js +8 -3
  256. package/dist/variables/resolution.js.map +1 -1
  257. package/dist/variables/subtree.js +1 -1
  258. package/dist/variables/subtree.js.map +1 -1
  259. package/dist/vector-network.d.ts.map +1 -1
  260. package/dist/vector-network.js +13 -79
  261. package/dist/vector-network.js.map +1 -1
  262. package/package.json +28 -7
  263. package/src/buffered-events.ts +2 -1
  264. package/src/clipping.ts +11 -8
  265. package/src/comments/schema.ts +1 -0
  266. package/src/comments/threads.ts +13 -5
  267. package/src/components/properties.ts +3 -14
  268. package/src/constants.ts +25 -1
  269. package/src/copy.ts +2 -0
  270. package/src/corners.ts +93 -0
  271. package/src/css/grid.ts +49 -7
  272. package/src/css/index.ts +13 -2
  273. package/src/css/layout.ts +36 -0
  274. package/src/css/values.ts +49 -0
  275. package/src/font-style.ts +38 -4
  276. package/src/geometry.ts +223 -28
  277. package/src/gradient.ts +23 -1
  278. package/src/graph.ts +35 -10
  279. package/src/group-bounds.ts +7 -3
  280. package/src/guides.ts +26 -1
  281. package/src/hit-test.ts +68 -10
  282. package/src/icons/fit.ts +24 -0
  283. package/src/icons/glyph.ts +6 -4
  284. package/src/icons/index.ts +1 -0
  285. package/src/ids.ts +1 -1
  286. package/src/images.ts +67 -7
  287. package/src/instances/assignments.ts +65 -7
  288. package/src/instances/index.ts +77 -13
  289. package/src/instances/kept-overrides.ts +2 -5
  290. package/src/instances/matching.ts +101 -0
  291. package/src/instances/overrides.ts +18 -0
  292. package/src/instances/protection.ts +103 -0
  293. package/src/instances/swap.ts +12 -9
  294. package/src/instances/sync.ts +52 -184
  295. package/src/layout/guides.ts +42 -9
  296. package/src/layout/index.ts +3 -0
  297. package/src/layout/limits.ts +64 -0
  298. package/src/layout/sizing.ts +0 -2
  299. package/src/layout/spacing.ts +11 -0
  300. package/src/layout/strokes.ts +23 -0
  301. package/src/masks.ts +51 -0
  302. package/src/matrix.ts +15 -2
  303. package/src/node-defaults.ts +71 -36
  304. package/src/outline/flatten.ts +31 -18
  305. package/src/outline/hit.ts +16 -33
  306. package/src/outline/index.ts +2 -2
  307. package/src/outline/shapes.ts +19 -36
  308. package/src/paints/index.ts +2 -0
  309. package/src/paints/pattern.ts +45 -0
  310. package/src/paints/shows.ts +13 -0
  311. package/src/parse-path.ts +14 -0
  312. package/src/paths/arc.ts +59 -0
  313. package/src/paths/index.ts +4 -0
  314. package/src/paths/network.ts +411 -0
  315. package/src/paths/sink.ts +50 -0
  316. package/src/paths/smooth-corners.ts +357 -0
  317. package/src/plugin-data/fields.ts +2 -0
  318. package/src/polygon.ts +0 -17
  319. package/src/primitives/index.ts +0 -5
  320. package/src/resize.ts +6 -7
  321. package/src/scaling/node.ts +29 -3
  322. package/src/shaders/model.ts +2 -1
  323. package/src/shared-styles.ts +3 -4
  324. package/src/strokes.ts +121 -0
  325. package/src/text/auto-resize.ts +25 -0
  326. package/src/text/index.ts +2 -0
  327. package/src/text/lengths.ts +11 -0
  328. package/src/text/overflow.ts +32 -6
  329. package/src/text/path.ts +13 -0
  330. package/src/text/picture.ts +5 -5
  331. package/src/text/runs.ts +80 -0
  332. package/src/transfer/dependencies.ts +4 -2
  333. package/src/transfer/nodes.ts +2 -1
  334. package/src/types.ts +62 -8
  335. package/src/variables/index.ts +1 -0
  336. package/src/variables/lookup.ts +31 -0
  337. package/src/variables/paints.ts +101 -20
  338. package/src/variables/resolution.ts +15 -2
  339. package/src/variables/subtree.ts +1 -1
  340. package/src/vector-network.ts +19 -142
  341. package/dist/random.d.ts +0 -7
  342. package/dist/random.d.ts.map +0 -1
  343. package/dist/random.js +0 -25
  344. package/dist/random.js.map +0 -1
  345. package/src/random.ts +0 -28
@@ -1 +1 @@
1
- {"version":3,"file":"resize.js","names":[],"sources":["../src/resize.ts"],"sourcesContent":["import { CONSTRAINT_PARENT_TYPES } from './constraints'\nimport { getNodeLocalMatrix } from './coordinate'\nimport {\n cloneVectorNetwork,\n copyDerivedGlyphs,\n copyGeometryPaths,\n copyStroke,\n copyStrokes,\n scaleGeometryPaths\n} from './copy'\nimport { groupFitChanges } from './group-bounds'\nimport { enforcedAspectRatio } from './layout/aspect-ratio'\nimport Matrix, { type Mat3 } from './matrix'\nimport { FITTED_CONTAINER_TYPES } from './node-defaults'\nimport type { Rect } from './primitives'\nimport type { ConstraintType, DerivedTextGlyph, SceneNode, Stroke, VectorNetwork } from './types'\n\n/**\n * Drag-start state a live resize re-scales from. Path text needs more than the\n * layout rect: its OUTSIDE stroke silhouettes and glyph outlines are baked\n * geometry that must scale with the node, and textPathBox is the layout box the\n * glyphs were reflowed against.\n */\nexport type ResizeSnapshot = Pick<\n SceneNode,\n | 'type'\n | 'x'\n | 'y'\n | 'width'\n | 'height'\n | 'flipX'\n | 'flipY'\n | 'vectorNetwork'\n | 'fillGeometry'\n | 'strokeGeometry'\n | 'derivedTextGlyphs'\n | 'strokes'\n | 'textPathData'\n | 'textPathBox'\n>\n\ninterface ResizeGraph {\n readonly rootId: string\n getNode(id: string): SceneNode | undefined\n}\n\n/**\n * Containers whose resize moves their children: the constraint parents, and groups and booleans,\n * which scale theirs.\n */\nconst CONSTRAINT_CONTAINER_TYPES = new Set<SceneNode['type']>([\n ...CONSTRAINT_PARENT_TYPES,\n ...FITTED_CONTAINER_TYPES\n])\n\ntype AxisPlacement = { position: number; size: number; flipped?: boolean }\n\n/** A child's box after its parent resizes, and the axes on which it turned over. */\nexport type ConstrainedRect = Rect & { flipsX?: boolean; flipsY?: boolean }\n\n/**\n * Where a constraint places a child on one axis when its parent goes from `parentBefore` to\n * `parentAfter`, as Figma does (live-measured, #1060, #1816): Right and Left & right move or grow\n * it by the change in the parent's size rounded to whole pixels, Center by the change in its\n * rounded half, and Scale by the exact ratio. The child's own box is never rounded, so a resize\n * to the same size changes nothing. Left & right that would leave no size keeps the axis as it\n * was; past zero, the child turns over and its box runs back from the edge it was pinned to, as\n * the layer is mirrored in Figma.\n */\nfunction constrainedAxis(\n position: number,\n size: number,\n parentBefore: number,\n parentAfter: number,\n constraint: ConstraintType\n): AxisPlacement {\n const delta = Math.round(parentAfter) - Math.round(parentBefore)\n if (constraint === 'MAX') return { position: position + delta, size }\n if (constraint === 'CENTER') {\n return { position: position + Math.round(parentAfter / 2) - Math.round(parentBefore / 2), size }\n }\n if (constraint === 'STRETCH') {\n const stretched = size + delta\n if (stretched === 0) return { position, size }\n if (stretched > 0) return { position, size: stretched }\n return { position: position + stretched, size: -stretched, flipped: true }\n }\n if (constraint === 'SCALE' && parentBefore > 0) {\n const scale = parentAfter / parentBefore\n return { position: position * scale, size: size * scale }\n }\n return { position, size }\n}\n\nconst FLEXIBLE_CONSTRAINTS: ReadonlySet<ConstraintType> = new Set(['STRETCH', 'SCALE'])\n\n/** The axis at `size`, keeping the edge or centre its constraint pins where it was placed. */\nfunction resizedInPlace(\n placed: AxisPlacement,\n size: number,\n constraint: ConstraintType\n): AxisPlacement {\n if (constraint === 'MAX') return { position: placed.position + placed.size - size, size }\n if (constraint === 'CENTER') return { position: placed.position + (placed.size - size) / 2, size }\n return { position: placed.position, size }\n}\n\n/** Keep a placed box's constrained edges or center when its content determines its size. */\nexport function resizedChildRect(\n placed: Rect,\n size: Pick<Rect, 'width' | 'height'>,\n horizontal: ConstraintType,\n vertical: ConstraintType\n): Rect {\n const x = resizedInPlace({ position: placed.x, size: placed.width }, size.width, horizontal)\n const y = resizedInPlace({ position: placed.y, size: placed.height }, size.height, vertical)\n return { x: x.position, y: y.position, width: x.size, height: y.size }\n}\n\n/**\n * A locked aspect ratio holds when exactly one axis follows the parent, as in Figma: that axis\n * sizes the other. With both axes following the parent, or neither, the ratio gives way.\n */\nfunction keepLockedRatio(\n x: AxisPlacement,\n y: AxisPlacement,\n horizontal: ConstraintType,\n vertical: ConstraintType,\n aspectRatio: number | null\n): { x: AxisPlacement; y: AxisPlacement } {\n const flexibleX = FLEXIBLE_CONSTRAINTS.has(horizontal)\n if (aspectRatio === null || flexibleX === FLEXIBLE_CONSTRAINTS.has(vertical)) return { x, y }\n return flexibleX\n ? { x, y: resizedInPlace(y, x.size / aspectRatio, vertical) }\n : { x: resizedInPlace(x, y.size * aspectRatio, horizontal), y }\n}\n\n/**\n * A child's box after its parent resizes, by a handle, `resize()`, layout, or an instance taking\n * another size than its component, keeping a locked aspect ratio as Figma does.\n */\nexport function constrainedChildRect(\n child: Rect,\n parentBefore: Pick<Rect, 'width' | 'height'>,\n parentAfter: Pick<Rect, 'width' | 'height'>,\n horizontal: ConstraintType,\n vertical: ConstraintType,\n aspectRatio: number | null = null\n): ConstrainedRect {\n const { x, y } = keepLockedRatio(\n constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal),\n constrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical),\n horizontal,\n vertical,\n aspectRatio\n )\n const rect: ConstrainedRect = { x: x.position, y: y.position, width: x.size, height: y.size }\n if (x.flipped) rect.flipsX = true\n if (y.flipped) rect.flipsY = true\n return rect\n}\n\nexport function scaledChildRect(\n child: Rect,\n parentBefore: Pick<Rect, 'width' | 'height'>,\n parentAfter: Pick<Rect, 'width' | 'height'>\n): Rect {\n return constrainedChildRect(child, parentBefore, parentAfter, 'SCALE', 'SCALE')\n}\n\n/** How much a side grows; a flat side, as a straight line's, has nothing to scale along it. */\nfunction sideScale(original: number, size: number): number {\n return original > 0 ? size / original : 1\n}\n\nexport function scaleVectorNetworkForResize(\n vectorNetwork: VectorNetwork | null,\n originalWidth: number,\n originalHeight: number,\n width: number,\n height: number\n): VectorNetwork | null {\n if (!vectorNetwork || (originalWidth <= 0 && originalHeight <= 0)) return null\n\n const scaleX = sideScale(originalWidth, width)\n const scaleY = sideScale(originalHeight, height)\n if (scaleX === 1 && scaleY === 1) return null\n\n return {\n vertices: vectorNetwork.vertices.map((vertex) => ({\n ...vertex,\n x: vertex.x * scaleX,\n y: vertex.y * scaleY\n })),\n segments: vectorNetwork.segments.map((segment) => ({\n ...segment,\n tangentStart: {\n x: segment.tangentStart.x * scaleX,\n y: segment.tangentStart.y * scaleY\n },\n tangentEnd: {\n x: segment.tangentEnd.x * scaleX,\n y: segment.tangentEnd.y * scaleY\n }\n })),\n regions: vectorNetwork.regions\n }\n}\n\n/** Snapshot one node's resize-relevant state (rect + geometry that must scale with it). */\nexport function createResizeSnapshot(node: SceneNode): ResizeSnapshot {\n return {\n type: node.type,\n x: node.x,\n y: node.y,\n width: node.width,\n height: node.height,\n flipX: node.flipX,\n flipY: node.flipY,\n vectorNetwork: node.vectorNetwork ? cloneVectorNetwork(node.vectorNetwork) : null,\n fillGeometry: copyGeometryPaths(node.fillGeometry),\n strokeGeometry: copyGeometryPaths(node.strokeGeometry),\n derivedTextGlyphs: copyDerivedGlyphs(node.derivedTextGlyphs),\n strokes: copyStrokes(node.strokes),\n textPathData: node.textPathData ? structuredClone(node.textPathData) : null,\n textPathBox: node.textPathBox ? { ...node.textPathBox } : null\n }\n}\n\nfunction scaleDerivedGlyphs(\n glyphs: DerivedTextGlyph[] | null,\n sx: number,\n sy: number\n): DerivedTextGlyph[] | null {\n if (!glyphs?.length) return glyphs\n return glyphs.map((g) => ({\n ...g,\n x: g.x * sx,\n y: g.y * sy,\n scaleX: (g.scaleX ?? 1) * sx,\n scaleY: (g.scaleY ?? 1) * sy,\n commandsBlob: new Uint8Array(g.commandsBlob)\n }))\n}\n\nfunction hasPath(orig: Pick<ResizeSnapshot, 'vectorNetwork'>): boolean {\n return (orig.vectorNetwork?.segments.length ?? 0) > 0\n}\n\nfunction scaleStrokes(strokes: Stroke[], sx: number, sy: number): Stroke[] {\n if (strokes.length === 0) return strokes\n // Weight is a scalar; average scale is the usual 2D → 1D compromise.\n const weightScale = (Math.abs(sx) + Math.abs(sy)) / 2\n return strokes.map((s) => ({ ...copyStroke(s), weight: s.weight * weightScale }))\n}\n\n/**\n * All geometry that must track a width/height change. Historically only\n * vectorNetwork + fillGeometry were updated — path-text strokeGeometry and\n * glyphs stayed at the pre-resize size, so shrinking a sticker made the white\n * OUTSIDE outlines look massively thick (DomeSticker resize bug).\n */\nexport function scaledGeometryChanges(\n orig: Pick<\n ResizeSnapshot,\n | 'type'\n | 'vectorNetwork'\n | 'fillGeometry'\n | 'strokeGeometry'\n | 'derivedTextGlyphs'\n | 'strokes'\n | 'textPathData'\n | 'textPathBox'\n >,\n origWidth: number,\n origHeight: number,\n width: number,\n height: number\n): Partial<SceneNode> {\n if (origWidth <= 0 && origHeight <= 0) return {}\n // A straight line's other side still scales.\n const sx = sideScale(origWidth, width)\n const sy = sideScale(origHeight, height)\n if (sx === 1 && sy === 1) return {}\n\n const changes: Partial<SceneNode> = {}\n const resizedVN = scaleVectorNetworkForResize(\n orig.vectorNetwork,\n origWidth,\n origHeight,\n width,\n height\n )\n if (resizedVN) changes.vectorNetwork = resizedVN\n\n if (orig.fillGeometry.length > 0) {\n changes.fillGeometry = scaleGeometryPaths(orig.fillGeometry, sx, sy)\n }\n // A stroke keeps its weight through a resize, as in Figma, so a saved stroke outline goes where\n // the stroke can be drawn again at its weight: from a path, or from a shape's own outline.\n // Text and a vector with only its outline keep scaling the outline, which is all they have.\n const outlineOnly = orig.type === 'TEXT' || (orig.type === 'VECTOR' && !hasPath(orig))\n if (orig.strokeGeometry.length > 0) {\n changes.strokeGeometry = outlineOnly ? scaleGeometryPaths(orig.strokeGeometry, sx, sy) : []\n }\n // Path text scales with its path, including baked path text that kept only rotated glyphs;\n // other text reflows in the new box, so its glyphs are dropped.\n const glyphs = orig.derivedTextGlyphs ?? []\n const pathText =\n !!orig.textPathData || !!orig.textPathBox || glyphs.some((glyph) => (glyph.rotation ?? 0) !== 0)\n if (glyphs.length > 0 && pathText) {\n changes.derivedTextGlyphs = scaleDerivedGlyphs(glyphs, sx, sy)\n // Keep the layout/selection box in sync with the scaled glyphs. reflow\n // overrides this when it applies; this covers the fallback where reflow\n // returns null (no path data) so the box doesn't carry a stale size.\n if (orig.textPathBox) {\n changes.textPathBox = {\n x: orig.textPathBox.x * sx,\n y: orig.textPathBox.y * sy,\n width: orig.textPathBox.width * sx,\n height: orig.textPathBox.height * sy\n }\n }\n }\n // Glyphs scaled as a picture take their strokes along; any other stroke keeps its weight, as\n // Figma reports it, even where only a scaled outline can draw it.\n if (orig.strokes.length > 0 && glyphs.length > 0 && pathText) {\n changes.strokes = scaleStrokes(orig.strokes, sx, sy)\n }\n return changes\n}\n\n/** Copies of the geometry a resize may change, as the snapshot held it before the resize. */\nfunction snapshotGeometry(orig: ResizeSnapshot): Partial<SceneNode> {\n return {\n vectorNetwork: orig.vectorNetwork ? cloneVectorNetwork(orig.vectorNetwork) : null,\n fillGeometry: copyGeometryPaths(orig.fillGeometry),\n strokeGeometry: copyGeometryPaths(orig.strokeGeometry),\n derivedTextGlyphs: copyDerivedGlyphs(orig.derivedTextGlyphs),\n strokes: copyStrokes(orig.strokes),\n textPathBox: orig.textPathBox ? { ...orig.textPathBox } : null\n }\n}\n\n/** Whether a box of `size` scales the snapshot's geometry, a flat side left out. */\nfunction scalesGeometry(orig: ResizeSnapshot, size: Pick<Rect, 'width' | 'height'>): boolean {\n return sideScale(orig.width, size.width) !== 1 || sideScale(orig.height, size.height) !== 1\n}\n\n/**\n * A child's geometry for its constrained `box`, scaled from the snapshot. At a box that scales\n * nothing, after an earlier frame of the same drag scaled it, the child takes its snapshot\n * geometry again, as nothing else would undo that scaling.\n */\nfunction constrainedGeometryChanges(\n orig: ResizeSnapshot,\n current: Pick<SceneNode, 'width' | 'height'>,\n box: Pick<Rect, 'width' | 'height'>\n): Partial<SceneNode> {\n if (scalesGeometry(orig, box))\n return scaledGeometryChanges(orig, orig.width, orig.height, box.width, box.height)\n return scalesGeometry(orig, current) ? snapshotGeometry(orig) : {}\n}\n\nexport function collectResizeDescendants(\n graph: ResizeGraph,\n rootId: string\n): Map<string, ResizeSnapshot> | null {\n const root = graph.getNode(rootId)\n if (!root || !CONSTRAINT_CONTAINER_TYPES.has(root.type)) return null\n const snapshots = new Map<string, ResizeSnapshot>()\n\n const collect = (parentId: string) => {\n const parent = graph.getNode(parentId)\n if (!parent) return\n for (const childId of parent.childIds) {\n const child = graph.getNode(childId)\n if (!child) continue\n snapshots.set(childId, createResizeSnapshot(child))\n collect(childId)\n }\n }\n\n collect(rootId)\n return snapshots.size > 0 ? snapshots : null\n}\n\n/**\n * The flips a child takes when its constraints place it from `from`, written only where they\n * differ from the ones it has now, so a drag that crosses back undoes its own turn.\n */\nexport function flipChanges(\n from: Pick<SceneNode, 'flipX' | 'flipY'>,\n current: Pick<SceneNode, 'flipX' | 'flipY'>,\n flipsX = false,\n flipsY = false\n): Partial<Pick<SceneNode, 'flipX' | 'flipY'>> {\n const changes: Partial<Pick<SceneNode, 'flipX' | 'flipY'>> = {}\n const flipX = from.flipX !== flipsX\n const flipY = from.flipY !== flipsY\n if (flipX !== current.flipX) changes.flipX = flipX\n if (flipY !== current.flipY) changes.flipY = flipY\n return changes\n}\n\ntype Size = Pick<Rect, 'width' | 'height'>\n\n/** One constrained resize: the graph, where each layer was when it began, and what it changes. */\ninterface ResizePlan {\n graph: ResizeGraph\n originals: ReadonlyMap<string, ResizeSnapshot>\n changes: Map<string, Partial<SceneNode>>\n}\n\n/** The layer as the resize found it, or as the resize leaves it with its changes so far. */\nfunction startOf(plan: ResizePlan, id: string): SceneNode | undefined {\n const node = plan.graph.getNode(id)\n const original = plan.originals.get(id)\n return node && original ? { ...node, ...original } : node\n}\n\nfunction plannedOf(plan: ResizePlan, id: string): SceneNode | undefined {\n const node = startOf(plan, id)\n const changes = plan.changes.get(id)\n return node && changes ? { ...node, ...changes } : node\n}\n\nfunction mergeChanges(plan: ResizePlan, id: string, changes: Partial<SceneNode>) {\n plan.changes.set(id, { ...plan.changes.get(id), ...changes })\n}\n\n/** Writes where a layer's constraints put it, then places its own children. */\nfunction placeConstrained(\n plan: ResizePlan,\n child: SceneNode,\n original: ResizeSnapshot,\n rect: ConstrainedRect,\n toParent: Mat3 | null\n) {\n const { flipsX, flipsY, ...box } = rect\n const position = toParent ? Matrix.mapPoint(toParent, box) : box\n plan.changes.set(child.id, {\n ...box,\n ...position,\n ...flipChanges(original, child, flipsX, flipsY),\n ...constrainedGeometryChanges(original, child, box)\n })\n // The final pass sees layout containers after Yoga has resolved HUG/FILL sizing.\n placeChildren(plan, child.id, original, child.layoutMode === 'NONE' ? box : child)\n}\n\n/**\n * Places the layers in a group or boolean whose frame resized. The group owns no constraints: each\n * layer in it keeps its place in the frame by its own, through nested groups, and the groups then\n * fit around them, as in Figma (live, #1835). A layer is placed in the frame's space and written\n * back in its group's.\n */\nfunction placeGroupedLayers(\n plan: ResizePlan,\n group: SceneNode,\n toFrame: Mat3,\n frameBefore: Size,\n frameAfter: Size\n) {\n for (const childId of group.childIds) {\n const child = plan.graph.getNode(childId)\n const original = plan.originals.get(childId)\n const start = startOf(plan, childId)\n if (!child || !original || !start) continue\n if (FITTED_CONTAINER_TYPES.has(child.type)) {\n const inner = Matrix.multiply(toFrame, getNodeLocalMatrix(start))\n placeGroupedLayers(plan, start, inner, frameBefore, frameAfter)\n continue\n }\n const inFrame = Matrix.mapPoint(toFrame, original)\n const rect = constrainedChildRect(\n { ...inFrame, width: original.width, height: original.height },\n frameBefore,\n frameAfter,\n child.horizontalConstraint,\n child.verticalConstraint,\n enforcedAspectRatio(child)\n )\n placeConstrained(plan, child, original, rect, Matrix.invert(toFrame))\n }\n fitPlannedGroup(plan, group.id)\n}\n\nconst PLACEMENT_KEYS = ['x', 'y', 'width', 'height'] as const\n\n/**\n * Fits a group to its layers where the resize put them, when any of them moved: a boolean's box\n * is its result's, which only the renderer measures, so one whose operands stay keeps its box. A\n * drag places from the start each time, so the group's box is always written.\n */\nfunction fitPlannedGroup(plan: ResizePlan, groupId: string) {\n const group = plannedOf(plan, groupId)\n if (!group) return\n const { x, y, width, height } = group\n mergeChanges(plan, groupId, { x, y, width, height })\n const moved = group.childIds.some((id) => {\n const start = startOf(plan, id)\n const planned = plannedOf(plan, id)\n return start && planned && PLACEMENT_KEYS.some((key) => start[key] !== planned[key])\n })\n if (!moved) return\n const reader = { rootId: plan.graph.rootId, getNode: (id: string) => plannedOf(plan, id) }\n const children = group.childIds.flatMap((id) => reader.getNode(id) ?? [])\n const fit = groupFitChanges(reader, group, children)\n for (const [id, changes] of fit ?? []) mergeChanges(plan, id, changes)\n}\n\n/**\n * Places the children of a layer that went from `parentBefore` to `parentAfter`: a frame's by their\n * constraints, the layers in its groups and booleans by theirs, a group's or boolean's own scaled.\n */\nfunction placeChildren(plan: ResizePlan, parentId: string, parentBefore: Size, parentAfter: Size) {\n const parent = plan.graph.getNode(parentId)\n if (!parent) return\n const scalesChildren = FITTED_CONTAINER_TYPES.has(parent.type)\n for (const childId of parent.childIds) {\n const original = plan.originals.get(childId)\n const child = plan.graph.getNode(childId)\n if (!original || !child) continue\n const isInFlow = parent.layoutMode !== 'NONE' && child.layoutPositioning !== 'ABSOLUTE'\n if (isInFlow) {\n placeChildren(plan, childId, original, child)\n continue\n }\n if (!scalesChildren && FITTED_CONTAINER_TYPES.has(child.type)) {\n const start = { ...child, ...original }\n placeGroupedLayers(plan, start, getNodeLocalMatrix(start), parentBefore, parentAfter)\n continue\n }\n const rect: ConstrainedRect = scalesChildren\n ? scaledChildRect(original, parentBefore, parentAfter)\n : constrainedChildRect(\n original,\n parentBefore,\n parentAfter,\n child.horizontalConstraint,\n child.verticalConstraint,\n enforcedAspectRatio(child)\n )\n placeConstrained(plan, child, original, rect, null)\n }\n}\n\n/**\n * Where the descendants of a layer go when it resizes from `rootBefore` to `rootAfter`, from the\n * places `originals` recorded.\n */\nexport function computeConstrainedResizeChanges(\n graph: ResizeGraph,\n rootId: string,\n rootBefore: Size,\n rootAfter: Size,\n originals: ReadonlyMap<string, ResizeSnapshot>\n): Map<string, Partial<SceneNode>> {\n const plan: ResizePlan = { graph, originals, changes: new Map() }\n placeChildren(plan, rootId, rootBefore, rootAfter)\n return plan.changes\n}\n"],"mappings":";;;;;;;;;;;;AAkDA,MAAM,6CAA6B,IAAI,IAAuB,CAC5D,GAAG,yBACH,GAAG,sBACL,CAAC;;;;;;;;;;AAgBD,SAAS,gBACP,UACA,MACA,cACA,aACA,YACe;CACf,MAAM,QAAQ,KAAK,MAAM,WAAW,IAAI,KAAK,MAAM,YAAY;CAC/D,IAAI,eAAe,OAAO,OAAO;EAAE,UAAU,WAAW;EAAO;CAAK;CACpE,IAAI,eAAe,UACjB,OAAO;EAAE,UAAU,WAAW,KAAK,MAAM,cAAc,CAAC,IAAI,KAAK,MAAM,eAAe,CAAC;EAAG;CAAK;CAEjG,IAAI,eAAe,WAAW;EAC5B,MAAM,YAAY,OAAO;EACzB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3C,MAAM,QAAQ,KAAK,MAAM,QAAQ,OAAO;EACxC,IAAI,CAAC,YAAY,CAAC,OAAO;EAEzB,IADiB,OAAO,eAAe,UAAU,MAAM,sBAAsB,YAC/D;GACZ,cAAc,MAAM,SAAS,UAAU,KAAK;GAC5C;EACF;EACA,IAAI,CAAC,kBAAkB,uBAAuB,IAAI,MAAM,IAAI,GAAG;GAC7D,MAAM,QAAQ;IAAE,GAAG;IAAO,GAAG;GAAS;GACtC,mBAAmB,MAAM,OAAO,mBAAmB,KAAK,GAAG,cAAc,WAAW;GACpF;EACF;EAWA,iBAAiB,MAAM,OAAO,UAVA,iBAC1B,gBAAgB,UAAU,cAAc,WAAW,IACnD,qBACE,UACA,cACA,aACA,MAAM,sBACN,MAAM,oBACN,oBAAoB,KAAK,CAC3B,GAC0C,IAAI;CACpD;AACF;;;;;AAMA,SAAgB,gCACd,OACA,QACA,YACA,WACA,WACiC;CACjC,MAAM,OAAmB;EAAE;EAAO;EAAW,yBAAS,IAAI,IAAI;CAAE;CAChE,cAAc,MAAM,QAAQ,YAAY,SAAS;CACjD,OAAO,KAAK;AACd"}
1
+ {"version":3,"file":"resize.js","names":[],"sources":["../src/resize.ts"],"sourcesContent":["import { CONSTRAINT_PARENT_TYPES } from './constraints'\nimport { getNodeLocalMatrix } from './coordinate'\nimport {\n cloneVectorNetwork,\n copyDerivedGlyphs,\n copyGeometryPaths,\n copyStroke,\n copyStrokes,\n scaleGeometryPaths\n} from './copy'\nimport { groupFitChanges } from './group-bounds'\nimport { enforcedAspectRatio } from './layout/aspect-ratio'\nimport Matrix, { type Mat3 } from './matrix'\nimport { FITTED_CONTAINER_TYPES } from './node-defaults'\nimport type { Rect } from './primitives'\nimport { followsPath } from './text/path'\nimport type { ConstraintType, DerivedTextGlyph, SceneNode, Stroke, VectorNetwork } from './types'\n\n/**\n * Drag-start state a live resize re-scales from. Path text needs more than the\n * layout rect: its OUTSIDE stroke silhouettes and glyph outlines are baked\n * geometry that must scale with the node, and textPathBox is the layout box the\n * glyphs were reflowed against.\n */\nexport type ResizeSnapshot = Pick<\n SceneNode,\n | 'type'\n | 'x'\n | 'y'\n | 'width'\n | 'height'\n | 'flipX'\n | 'flipY'\n | 'vectorNetwork'\n | 'fillGeometry'\n | 'strokeGeometry'\n | 'derivedTextGlyphs'\n | 'strokes'\n | 'textPathData'\n | 'textPathBox'\n>\n\ninterface ResizeGraph {\n readonly rootId: string\n getNode(id: string): SceneNode | undefined\n}\n\n/**\n * Containers whose resize moves their children: the constraint parents, and groups and booleans,\n * which scale theirs.\n */\nconst CONSTRAINT_CONTAINER_TYPES = new Set<SceneNode['type']>([\n ...CONSTRAINT_PARENT_TYPES,\n ...FITTED_CONTAINER_TYPES\n])\n\ntype AxisPlacement = { position: number; size: number; flipped?: boolean }\n\n/** A child's box after its parent resizes, and the axes on which it turned over. */\nexport type ConstrainedRect = Rect & { flipsX?: boolean; flipsY?: boolean }\n\n/**\n * Where a constraint places a child on one axis when its parent goes from `parentBefore` to\n * `parentAfter`, as Figma does (live-measured, #1060, #1816): Right and Left & right move or grow\n * it by the change in the parent's size rounded to whole pixels, Center by the change in its\n * rounded half, and Scale by the exact ratio. The child's own box is never rounded, so a resize\n * to the same size changes nothing. Left & right that would leave no size keeps the axis as it\n * was; past zero, the child turns over and its box runs back from the edge it was pinned to, as\n * the layer is mirrored in Figma.\n */\nfunction constrainedAxis(\n position: number,\n size: number,\n parentBefore: number,\n parentAfter: number,\n constraint: ConstraintType\n): AxisPlacement {\n const delta = Math.round(parentAfter) - Math.round(parentBefore)\n if (constraint === 'MAX') return { position: position + delta, size }\n if (constraint === 'CENTER') {\n return { position: position + Math.round(parentAfter / 2) - Math.round(parentBefore / 2), size }\n }\n if (constraint === 'STRETCH') {\n const stretched = size + delta\n if (stretched === 0) return { position, size }\n if (stretched > 0) return { position, size: stretched }\n return { position: position + stretched, size: -stretched, flipped: true }\n }\n if (constraint === 'SCALE' && parentBefore > 0) {\n const scale = parentAfter / parentBefore\n return { position: position * scale, size: size * scale }\n }\n return { position, size }\n}\n\nconst FLEXIBLE_CONSTRAINTS: ReadonlySet<ConstraintType> = new Set(['STRETCH', 'SCALE'])\n\n/** The axis at `size`, keeping the edge or centre its constraint pins where it was placed. */\nfunction resizedInPlace(\n placed: AxisPlacement,\n size: number,\n constraint: ConstraintType\n): AxisPlacement {\n if (constraint === 'MAX') return { position: placed.position + placed.size - size, size }\n if (constraint === 'CENTER') return { position: placed.position + (placed.size - size) / 2, size }\n return { position: placed.position, size }\n}\n\n/** Keep a placed box's constrained edges or center when its content determines its size. */\nexport function resizedChildRect(\n placed: Rect,\n size: Pick<Rect, 'width' | 'height'>,\n horizontal: ConstraintType,\n vertical: ConstraintType\n): Rect {\n const x = resizedInPlace({ position: placed.x, size: placed.width }, size.width, horizontal)\n const y = resizedInPlace({ position: placed.y, size: placed.height }, size.height, vertical)\n return { x: x.position, y: y.position, width: x.size, height: y.size }\n}\n\n/**\n * A locked aspect ratio holds when exactly one axis follows the parent, as in Figma: that axis\n * sizes the other. With both axes following the parent, or neither, the ratio gives way.\n */\nfunction keepLockedRatio(\n x: AxisPlacement,\n y: AxisPlacement,\n horizontal: ConstraintType,\n vertical: ConstraintType,\n aspectRatio: number | null\n): { x: AxisPlacement; y: AxisPlacement } {\n const flexibleX = FLEXIBLE_CONSTRAINTS.has(horizontal)\n if (aspectRatio === null || flexibleX === FLEXIBLE_CONSTRAINTS.has(vertical)) return { x, y }\n return flexibleX\n ? { x, y: resizedInPlace(y, x.size / aspectRatio, vertical) }\n : { x: resizedInPlace(x, y.size * aspectRatio, horizontal), y }\n}\n\n/**\n * A child's box after its parent resizes, by a handle, `resize()`, layout, or an instance taking\n * another size than its component, keeping a locked aspect ratio as Figma does.\n */\nexport function constrainedChildRect(\n child: Rect,\n parentBefore: Pick<Rect, 'width' | 'height'>,\n parentAfter: Pick<Rect, 'width' | 'height'>,\n horizontal: ConstraintType,\n vertical: ConstraintType,\n aspectRatio: number | null = null\n): ConstrainedRect {\n const { x, y } = keepLockedRatio(\n constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal),\n constrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical),\n horizontal,\n vertical,\n aspectRatio\n )\n const rect: ConstrainedRect = { x: x.position, y: y.position, width: x.size, height: y.size }\n if (x.flipped) rect.flipsX = true\n if (y.flipped) rect.flipsY = true\n return rect\n}\n\nexport function scaledChildRect(\n child: Rect,\n parentBefore: Pick<Rect, 'width' | 'height'>,\n parentAfter: Pick<Rect, 'width' | 'height'>\n): Rect {\n return constrainedChildRect(child, parentBefore, parentAfter, 'SCALE', 'SCALE')\n}\n\n/** How much a side grows; a flat side, as a straight line's, has nothing to scale along it. */\nfunction sideScale(original: number, size: number): number {\n return original > 0 ? size / original : 1\n}\n\nexport function scaleVectorNetworkForResize(\n vectorNetwork: VectorNetwork | null,\n originalWidth: number,\n originalHeight: number,\n width: number,\n height: number\n): VectorNetwork | null {\n if (!vectorNetwork || (originalWidth <= 0 && originalHeight <= 0)) return null\n\n const scaleX = sideScale(originalWidth, width)\n const scaleY = sideScale(originalHeight, height)\n if (scaleX === 1 && scaleY === 1) return null\n\n return {\n vertices: vectorNetwork.vertices.map((vertex) => ({\n ...vertex,\n x: vertex.x * scaleX,\n y: vertex.y * scaleY\n })),\n segments: vectorNetwork.segments.map((segment) => ({\n ...segment,\n tangentStart: {\n x: segment.tangentStart.x * scaleX,\n y: segment.tangentStart.y * scaleY\n },\n tangentEnd: {\n x: segment.tangentEnd.x * scaleX,\n y: segment.tangentEnd.y * scaleY\n }\n })),\n regions: vectorNetwork.regions\n }\n}\n\n/** Snapshot one node's resize-relevant state (rect + geometry that must scale with it). */\nexport function createResizeSnapshot(node: SceneNode): ResizeSnapshot {\n return {\n type: node.type,\n x: node.x,\n y: node.y,\n width: node.width,\n height: node.height,\n flipX: node.flipX,\n flipY: node.flipY,\n vectorNetwork: node.vectorNetwork ? cloneVectorNetwork(node.vectorNetwork) : null,\n fillGeometry: copyGeometryPaths(node.fillGeometry),\n strokeGeometry: copyGeometryPaths(node.strokeGeometry),\n derivedTextGlyphs: copyDerivedGlyphs(node.derivedTextGlyphs),\n strokes: copyStrokes(node.strokes),\n textPathData: node.textPathData ? structuredClone(node.textPathData) : null,\n textPathBox: node.textPathBox ? { ...node.textPathBox } : null\n }\n}\n\nfunction scaleDerivedGlyphs(\n glyphs: DerivedTextGlyph[] | null,\n sx: number,\n sy: number\n): DerivedTextGlyph[] | null {\n if (!glyphs?.length) return glyphs\n return glyphs.map((g) => ({\n ...g,\n x: g.x * sx,\n y: g.y * sy,\n scaleX: (g.scaleX ?? 1) * sx,\n scaleY: (g.scaleY ?? 1) * sy,\n commandsBlob: new Uint8Array(g.commandsBlob)\n }))\n}\n\nfunction hasPath(orig: Pick<ResizeSnapshot, 'vectorNetwork'>): boolean {\n return (orig.vectorNetwork?.segments.length ?? 0) > 0\n}\n\nfunction scaleStrokes(strokes: Stroke[], sx: number, sy: number): Stroke[] {\n if (strokes.length === 0) return strokes\n // Weight is a scalar; average scale is the usual 2D → 1D compromise.\n const weightScale = (Math.abs(sx) + Math.abs(sy)) / 2\n return strokes.map((s) => ({ ...copyStroke(s), weight: s.weight * weightScale }))\n}\n\n/**\n * All geometry that must track a width/height change. Historically only\n * vectorNetwork + fillGeometry were updated — path-text strokeGeometry and\n * glyphs stayed at the pre-resize size, so shrinking a sticker made the white\n * OUTSIDE outlines look massively thick (DomeSticker resize bug).\n */\nexport function scaledGeometryChanges(\n orig: Pick<\n ResizeSnapshot,\n | 'type'\n | 'vectorNetwork'\n | 'fillGeometry'\n | 'strokeGeometry'\n | 'derivedTextGlyphs'\n | 'strokes'\n | 'textPathData'\n | 'textPathBox'\n >,\n origWidth: number,\n origHeight: number,\n width: number,\n height: number\n): Partial<SceneNode> {\n if (origWidth <= 0 && origHeight <= 0) return {}\n // A straight line's other side still scales.\n const sx = sideScale(origWidth, width)\n const sy = sideScale(origHeight, height)\n if (sx === 1 && sy === 1) return {}\n\n const changes: Partial<SceneNode> = {}\n const resizedVN = scaleVectorNetworkForResize(\n orig.vectorNetwork,\n origWidth,\n origHeight,\n width,\n height\n )\n if (resizedVN) changes.vectorNetwork = resizedVN\n\n if (orig.fillGeometry.length > 0) {\n changes.fillGeometry = scaleGeometryPaths(orig.fillGeometry, sx, sy)\n }\n // A stroke keeps its weight through a resize, as in Figma, so a saved stroke outline goes where\n // the stroke can be drawn again at its weight: from a path, or from a shape's own outline.\n // Text and a vector with only its outline keep scaling the outline, which is all they have.\n const outlineOnly = orig.type === 'TEXT' || (orig.type === 'VECTOR' && !hasPath(orig))\n if (orig.strokeGeometry.length > 0) {\n changes.strokeGeometry = outlineOnly ? scaleGeometryPaths(orig.strokeGeometry, sx, sy) : []\n }\n // Path text scales with its path, including baked path text that kept only rotated glyphs;\n // other text reflows in the new box, so its glyphs are dropped.\n const pathText = followsPath(orig)\n if (pathText) {\n changes.derivedTextGlyphs = scaleDerivedGlyphs(orig.derivedTextGlyphs, sx, sy)\n // Keep the layout/selection box in sync with the scaled glyphs. reflow\n // overrides this when it applies; this covers the fallback where reflow\n // returns null (no path data) so the box doesn't carry a stale size.\n if (orig.textPathBox) {\n changes.textPathBox = {\n x: orig.textPathBox.x * sx,\n y: orig.textPathBox.y * sy,\n width: orig.textPathBox.width * sx,\n height: orig.textPathBox.height * sy\n }\n }\n }\n // Glyphs scaled as a picture take their strokes along; any other stroke keeps its weight, as\n // Figma reports it, even where only a scaled outline can draw it.\n if (orig.strokes.length > 0 && pathText) {\n changes.strokes = scaleStrokes(orig.strokes, sx, sy)\n }\n return changes\n}\n\n/** Copies of the geometry a resize may change, as the snapshot held it before the resize. */\nfunction snapshotGeometry(orig: ResizeSnapshot): Partial<SceneNode> {\n return {\n vectorNetwork: orig.vectorNetwork ? cloneVectorNetwork(orig.vectorNetwork) : null,\n fillGeometry: copyGeometryPaths(orig.fillGeometry),\n strokeGeometry: copyGeometryPaths(orig.strokeGeometry),\n derivedTextGlyphs: copyDerivedGlyphs(orig.derivedTextGlyphs),\n strokes: copyStrokes(orig.strokes),\n textPathBox: orig.textPathBox ? { ...orig.textPathBox } : null\n }\n}\n\n/** Whether a box of `size` scales the snapshot's geometry, a flat side left out. */\nfunction scalesGeometry(orig: ResizeSnapshot, size: Pick<Rect, 'width' | 'height'>): boolean {\n return sideScale(orig.width, size.width) !== 1 || sideScale(orig.height, size.height) !== 1\n}\n\n/**\n * A child's geometry for its constrained `box`, scaled from the snapshot. At a box that scales\n * nothing, after an earlier frame of the same drag scaled it, the child takes its snapshot\n * geometry again, as nothing else would undo that scaling.\n */\nfunction constrainedGeometryChanges(\n orig: ResizeSnapshot,\n current: Pick<SceneNode, 'width' | 'height'>,\n box: Pick<Rect, 'width' | 'height'>\n): Partial<SceneNode> {\n if (scalesGeometry(orig, box))\n return scaledGeometryChanges(orig, orig.width, orig.height, box.width, box.height)\n return scalesGeometry(orig, current) ? snapshotGeometry(orig) : {}\n}\n\nexport function collectResizeDescendants(\n graph: ResizeGraph,\n rootId: string\n): Map<string, ResizeSnapshot> | null {\n const root = graph.getNode(rootId)\n if (!root || !CONSTRAINT_CONTAINER_TYPES.has(root.type)) return null\n const snapshots = new Map<string, ResizeSnapshot>()\n\n const collect = (parentId: string) => {\n const parent = graph.getNode(parentId)\n if (!parent) return\n for (const childId of parent.childIds) {\n const child = graph.getNode(childId)\n if (!child) continue\n snapshots.set(childId, createResizeSnapshot(child))\n collect(childId)\n }\n }\n\n collect(rootId)\n return snapshots.size > 0 ? snapshots : null\n}\n\n/**\n * The flips a child takes when its constraints place it from `from`, written only where they\n * differ from the ones it has now, so a drag that crosses back undoes its own turn.\n */\nexport function flipChanges(\n from: Pick<SceneNode, 'flipX' | 'flipY'>,\n current: Pick<SceneNode, 'flipX' | 'flipY'>,\n flipsX = false,\n flipsY = false\n): Partial<Pick<SceneNode, 'flipX' | 'flipY'>> {\n const changes: Partial<Pick<SceneNode, 'flipX' | 'flipY'>> = {}\n const flipX = from.flipX !== flipsX\n const flipY = from.flipY !== flipsY\n if (flipX !== current.flipX) changes.flipX = flipX\n if (flipY !== current.flipY) changes.flipY = flipY\n return changes\n}\n\ntype Size = Pick<Rect, 'width' | 'height'>\n\n/** One constrained resize: the graph, where each layer was when it began, and what it changes. */\ninterface ResizePlan {\n graph: ResizeGraph\n originals: ReadonlyMap<string, ResizeSnapshot>\n changes: Map<string, Partial<SceneNode>>\n}\n\n/** The layer as the resize found it, or as the resize leaves it with its changes so far. */\nfunction startOf(plan: ResizePlan, id: string): SceneNode | undefined {\n const node = plan.graph.getNode(id)\n const original = plan.originals.get(id)\n return node && original ? { ...node, ...original } : node\n}\n\nfunction plannedOf(plan: ResizePlan, id: string): SceneNode | undefined {\n const node = startOf(plan, id)\n const changes = plan.changes.get(id)\n return node && changes ? { ...node, ...changes } : node\n}\n\nfunction mergeChanges(plan: ResizePlan, id: string, changes: Partial<SceneNode>) {\n plan.changes.set(id, { ...plan.changes.get(id), ...changes })\n}\n\n/** Writes where a layer's constraints put it, then places its own children. */\nfunction placeConstrained(\n plan: ResizePlan,\n child: SceneNode,\n original: ResizeSnapshot,\n rect: ConstrainedRect,\n toParent: Mat3 | null\n) {\n const { flipsX, flipsY, ...box } = rect\n const position = toParent ? Matrix.mapPoint(toParent, box) : box\n plan.changes.set(child.id, {\n ...box,\n ...position,\n ...flipChanges(original, child, flipsX, flipsY),\n ...constrainedGeometryChanges(original, child, box)\n })\n // The final pass sees layout containers after layout has resolved HUG/FILL sizing.\n placeChildren(plan, child.id, original, child.layoutMode === 'NONE' ? box : child)\n}\n\n/**\n * Places the layers in a group or boolean whose frame resized. The group owns no constraints: each\n * layer in it keeps its place in the frame by its own, through nested groups, and the groups then\n * fit around them, as in Figma (live, #1835). A layer is placed in the frame's space and written\n * back in its group's.\n */\nfunction placeGroupedLayers(\n plan: ResizePlan,\n group: SceneNode,\n toFrame: Mat3,\n frameBefore: Size,\n frameAfter: Size\n) {\n for (const childId of group.childIds) {\n const child = plan.graph.getNode(childId)\n const original = plan.originals.get(childId)\n const start = startOf(plan, childId)\n if (!child || !original || !start) continue\n if (FITTED_CONTAINER_TYPES.has(child.type)) {\n const inner = Matrix.multiply(toFrame, getNodeLocalMatrix(start))\n placeGroupedLayers(plan, start, inner, frameBefore, frameAfter)\n continue\n }\n const inFrame = Matrix.mapPoint(toFrame, original)\n const rect = constrainedChildRect(\n { ...inFrame, width: original.width, height: original.height },\n frameBefore,\n frameAfter,\n child.horizontalConstraint,\n child.verticalConstraint,\n enforcedAspectRatio(child)\n )\n placeConstrained(plan, child, original, rect, Matrix.invert(toFrame))\n }\n fitPlannedGroup(plan, group.id)\n}\n\nconst PLACEMENT_KEYS = ['x', 'y', 'width', 'height'] as const\n\n/**\n * Fits a group to its layers where the resize put them, when any of them moved: a boolean's box\n * is its result's, which only the renderer measures, so one whose operands stay keeps its box. A\n * drag places from the start each time, so the group's box is always written.\n */\nfunction fitPlannedGroup(plan: ResizePlan, groupId: string) {\n const group = plannedOf(plan, groupId)\n if (!group) return\n const { x, y, width, height } = group\n mergeChanges(plan, groupId, { x, y, width, height })\n const moved = group.childIds.some((id) => {\n const start = startOf(plan, id)\n const planned = plannedOf(plan, id)\n return start && planned && PLACEMENT_KEYS.some((key) => start[key] !== planned[key])\n })\n if (!moved) return\n const reader = { rootId: plan.graph.rootId, getNode: (id: string) => plannedOf(plan, id) }\n const children = group.childIds.flatMap((id) => reader.getNode(id) ?? [])\n const fit = groupFitChanges(reader, group, children)\n for (const [id, changes] of fit ?? []) mergeChanges(plan, id, changes)\n}\n\n/**\n * Places the children of a layer that went from `parentBefore` to `parentAfter`: a frame's by their\n * constraints, the layers in its groups and booleans by theirs, a group's or boolean's own scaled.\n */\nfunction placeChildren(plan: ResizePlan, parentId: string, parentBefore: Size, parentAfter: Size) {\n const parent = plan.graph.getNode(parentId)\n if (!parent) return\n const scalesChildren = FITTED_CONTAINER_TYPES.has(parent.type)\n for (const childId of parent.childIds) {\n const original = plan.originals.get(childId)\n const child = plan.graph.getNode(childId)\n if (!original || !child) continue\n const isInFlow = parent.layoutMode !== 'NONE' && child.layoutPositioning !== 'ABSOLUTE'\n if (isInFlow) {\n placeChildren(plan, childId, original, child)\n continue\n }\n if (!scalesChildren && FITTED_CONTAINER_TYPES.has(child.type)) {\n const start = { ...child, ...original }\n placeGroupedLayers(plan, start, getNodeLocalMatrix(start), parentBefore, parentAfter)\n continue\n }\n const rect: ConstrainedRect = scalesChildren\n ? scaledChildRect(original, parentBefore, parentAfter)\n : constrainedChildRect(\n original,\n parentBefore,\n parentAfter,\n child.horizontalConstraint,\n child.verticalConstraint,\n enforcedAspectRatio(child)\n )\n placeConstrained(plan, child, original, rect, null)\n }\n}\n\n/**\n * Where the descendants of a layer go when it resizes from `rootBefore` to `rootAfter`, from the\n * places `originals` recorded.\n */\nexport function computeConstrainedResizeChanges(\n graph: ResizeGraph,\n rootId: string,\n rootBefore: Size,\n rootAfter: Size,\n originals: ReadonlyMap<string, ResizeSnapshot>\n): Map<string, Partial<SceneNode>> {\n const plan: ResizePlan = { graph, originals, changes: new Map() }\n placeChildren(plan, rootId, rootBefore, rootAfter)\n return plan.changes\n}\n"],"mappings":";;;;;;;;;;;;;AAmDA,MAAM,6CAA6B,IAAI,IAAuB,CAC5D,GAAG,yBACH,GAAG,sBACL,CAAC;;;;;;;;;;AAgBD,SAAS,gBACP,UACA,MACA,cACA,aACA,YACe;CACf,MAAM,QAAQ,KAAK,MAAM,WAAW,IAAI,KAAK,MAAM,YAAY;CAC/D,IAAI,eAAe,OAAO,OAAO;EAAE,UAAU,WAAW;EAAO;CAAK;CACpE,IAAI,eAAe,UACjB,OAAO;EAAE,UAAU,WAAW,KAAK,MAAM,cAAc,CAAC,IAAI,KAAK,MAAM,eAAe,CAAC;EAAG;CAAK;CAEjG,IAAI,eAAe,WAAW;EAC5B,MAAM,YAAY,OAAO;EACzB,IAAI,cAAc,GAAG,OAAO;GAAE;GAAU;EAAK;EAC7C,IAAI,YAAY,GAAG,OAAO;GAAE;GAAU,MAAM;EAAU;EACtD,OAAO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IAAE,GAAG;IAAO,GAAG;GAAS;GACtC,mBAAmB,MAAM,OAAO,mBAAmB,KAAK,GAAG,cAAc,WAAW;GACpF;EACF;EAWA,iBAAiB,MAAM,OAAO,UAVA,iBAC1B,gBAAgB,UAAU,cAAc,WAAW,IACnD,qBACE,UACA,cACA,aACA,MAAM,sBACN,MAAM,oBACN,oBAAoB,KAAK,CAC3B,GAC0C,IAAI;CACpD;AACF;;;;;AAMA,SAAgB,gCACd,OACA,QACA,YACA,WACA,WACiC;CACjC,MAAM,OAAmB;EAAE;EAAO;EAAW,yBAAS,IAAI,IAAI;CAAE;CAChE,cAAc,MAAM,QAAQ,YAAY,SAAS;CACjD,OAAO,KAAK;AACd"}
@@ -1 +1 @@
1
- {"version":3,"file":"node.d.ts","names":[],"sources":["../../src/scaling/node.ts"],"mappings":";;iBAyHgB,iBACd,MAAM,WACN,eACA,yBACC,QAAQ"}
1
+ {"version":3,"file":"node.d.ts","names":[],"sources":["../../src/scaling/node.ts"],"mappings":";;iBAiJgB,iBACd,MAAM,WACN,eACA,yBACC,QAAQ"}
@@ -1,5 +1,6 @@
1
1
  import { scaledTextLength } from "../text/lengths.js";
2
- import { cloneVectorNetwork, scaleGeometryPaths } from "../copy.js";
2
+ import { cloneVectorNetwork, copyDerivedGlyphs, scaleGeometryPaths } from "../copy.js";
3
+ import { followsPath } from "../text/path.js";
3
4
  import { TEXT_PARAGRAPH_SPACING_FIELDS } from "../text/paragraphs.js";
4
5
  //#region src/scaling/node.ts
5
6
  function scaledOptional(value, scale) {
@@ -95,6 +96,25 @@ function scaledVectorNetwork(node, scale) {
95
96
  for (const region of network.regions) if (region.fills) region.fills = scaledFills(region.fills, scale);
96
97
  return network;
97
98
  }
99
+ function scaledPathText(node, scale) {
100
+ if (!node.textPathData && !followsPath(node)) return {};
101
+ const box = node.textPathBox;
102
+ return {
103
+ derivedTextGlyphs: copyDerivedGlyphs(node.derivedTextGlyphs)?.map((glyph) => ({
104
+ ...glyph,
105
+ x: glyph.x * scale,
106
+ y: glyph.y * scale,
107
+ fontSize: glyph.fontSize * scale,
108
+ ...glyph.advance === void 0 ? {} : { advance: glyph.advance * scale }
109
+ })) ?? null,
110
+ textPathBox: box && {
111
+ x: box.x * scale,
112
+ y: box.y * scale,
113
+ width: box.width * scale,
114
+ height: box.height * scale
115
+ }
116
+ };
117
+ }
98
118
  function scaleNodeChanges(node, scale, scalePosition) {
99
119
  return {
100
120
  x: scalePosition ? node.x * scale : node.x,
@@ -120,8 +140,9 @@ function scaleNodeChanges(node, scale, scalePosition) {
120
140
  paragraphSpacing: node.paragraphSpacing * scale,
121
141
  paragraphIndent: node.paragraphIndent * scale,
122
142
  textParagraphs: scaledParagraphStyles(node.textParagraphs, scale),
143
+ derivedTextHeight: scaledOptional(node.derivedTextHeight, scale),
123
144
  itemSpacing: node.itemSpacing * scale,
124
- counterAxisSpacing: node.counterAxisSpacing * scale,
145
+ counterAxisSpacing: node.counterAxisSpacing === null ? null : node.counterAxisSpacing * scale,
125
146
  paddingTop: node.paddingTop * scale,
126
147
  paddingRight: node.paddingRight * scale,
127
148
  paddingBottom: node.paddingBottom * scale,
@@ -143,7 +164,8 @@ function scaleNodeChanges(node, scale, scalePosition) {
143
164
  layoutGrids: scaledLayoutGrids(node.layoutGrids, scale),
144
165
  vectorNetwork: scaledVectorNetwork(node, scale),
145
166
  fillGeometry: scaleGeometryPaths(node.fillGeometry, scale, scale),
146
- strokeGeometry: scaleGeometryPaths(node.strokeGeometry, scale, scale)
167
+ strokeGeometry: scaleGeometryPaths(node.strokeGeometry, scale, scale),
168
+ ...scaledPathText(node, scale)
147
169
  };
148
170
  }
149
171
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"node.js","names":[],"sources":["../../src/scaling/node.ts"],"sourcesContent":["import { cloneVectorNetwork, scaleGeometryPaths } from '../copy'\nimport { scaledTextLength } from '../text/lengths'\nimport { TEXT_PARAGRAPH_SPACING_FIELDS } from '../text/paragraphs'\nimport {\n type Effect,\n type Fill,\n type GridTrack,\n type LayoutGrid,\n type SceneNode,\n type Stroke,\n type StyleRun,\n type TextParagraphStyle\n} from '../types'\n\nfunction scaledOptional(value: number | null, scale: number): number | null {\n return value == null ? null : value * scale\n}\n\nfunction scaledStrokes(strokes: readonly Stroke[], scale: number): Stroke[] {\n return strokes.map((stroke) => ({\n ...structuredClone(stroke),\n weight: stroke.weight * scale,\n dashPattern: stroke.dashPattern?.map((value) => value * scale)\n }))\n}\n\nfunction scaledEffects(effects: readonly Effect[], scale: number): Effect[] {\n return effects.map((effect) => ({\n ...structuredClone(effect),\n offset: { x: effect.offset.x * scale, y: effect.offset.y * scale },\n radius: effect.radius * scale,\n spread: effect.spread * scale,\n ...(effect.progressive\n ? {\n progressive: {\n ...structuredClone(effect.progressive),\n startRadius: effect.progressive.startRadius * scale\n }\n }\n : {})\n }))\n}\n\nfunction scaledFills(fills: readonly Fill[], scale: number): Fill[] {\n return fills.map((fill) => ({\n ...structuredClone(fill),\n scale: fill.scale == null ? undefined : fill.scale * scale,\n spacing: fill.spacing == null ? undefined : fill.spacing * scale,\n patternSpacing: fill.patternSpacing\n ? { x: fill.patternSpacing.x * scale, y: fill.patternSpacing.y * scale }\n : undefined,\n noiseSize: fill.noiseSize\n ? { x: fill.noiseSize.x * scale, y: fill.noiseSize.y * scale }\n : undefined\n }))\n}\n\nfunction scaledStyleRuns(styleRuns: readonly StyleRun[], scale: number): StyleRun[] {\n return styleRuns.map((run) => ({\n ...structuredClone(run),\n style: {\n ...structuredClone(run.style),\n fontSize: run.style.fontSize == null ? undefined : run.style.fontSize * scale,\n letterSpacing: run.style.letterSpacing && scaledTextLength(run.style.letterSpacing, scale),\n lineHeight: run.style.lineHeight && scaledTextLength(run.style.lineHeight, scale),\n textDecorationThickness: scaledOptional(run.style.textDecorationThickness ?? null, scale),\n textUnderlineOffset: scaledOptional(run.style.textUnderlineOffset ?? null, scale)\n }\n }))\n}\n\nfunction scaledParagraphStyles(\n paragraphs: readonly TextParagraphStyle[],\n scale: number\n): TextParagraphStyle[] {\n return paragraphs.map((style) => {\n const scaled = { ...style }\n for (const field of TEXT_PARAGRAPH_SPACING_FIELDS) {\n const value = style[field]\n if (value !== undefined) scaled[field] = value * scale\n }\n return scaled\n })\n}\n\nfunction scaledLayoutGrids(grids: readonly LayoutGrid[], scale: number): LayoutGrid[] {\n return grids.map((grid) => ({\n ...structuredClone(grid),\n offset: grid.offset == null ? undefined : grid.offset * scale,\n sectionSize: grid.sectionSize == null ? undefined : grid.sectionSize * scale,\n gutterSize: grid.gutterSize == null ? undefined : grid.gutterSize * scale\n }))\n}\n\nfunction scaledGridTracks(tracks: readonly GridTrack[], scale: number): GridTrack[] {\n return tracks.map((track) => ({\n ...track,\n value: track.sizing === 'FIXED' ? track.value * scale : track.value\n }))\n}\n\nfunction scaledVectorNetwork(node: SceneNode, scale: number): SceneNode['vectorNetwork'] {\n if (!node.vectorNetwork) return null\n const network = cloneVectorNetwork(node.vectorNetwork)\n for (const vertex of network.vertices) {\n vertex.x *= scale\n vertex.y *= scale\n }\n for (const segment of network.segments) {\n segment.tangentStart.x *= scale\n segment.tangentStart.y *= scale\n segment.tangentEnd.x *= scale\n segment.tangentEnd.y *= scale\n }\n // A region's own paints scale as the layer's do.\n for (const region of network.regions) {\n if (region.fills) region.fills = scaledFills(region.fills, scale)\n }\n return network\n}\n\nexport function scaleNodeChanges(\n node: SceneNode,\n scale: number,\n scalePosition: boolean\n): Partial<SceneNode> {\n return {\n x: scalePosition ? node.x * scale : node.x,\n y: scalePosition ? node.y * scale : node.y,\n width: node.width * scale,\n height: node.height * scale,\n minWidth: scaledOptional(node.minWidth, scale),\n maxWidth: scaledOptional(node.maxWidth, scale),\n minHeight: scaledOptional(node.minHeight, scale),\n maxHeight: scaledOptional(node.maxHeight, scale),\n cornerRadius: node.cornerRadius * scale,\n topLeftRadius: node.topLeftRadius * scale,\n topRightRadius: node.topRightRadius * scale,\n bottomRightRadius: node.bottomRightRadius * scale,\n bottomLeftRadius: node.bottomLeftRadius * scale,\n fontSize: node.fontSize * scale,\n letterSpacing: scaledTextLength(node.letterSpacing, scale),\n lineHeight: node.lineHeight && scaledTextLength(node.lineHeight, scale),\n textDecorationThickness: scaledOptional(node.textDecorationThickness, scale),\n textUnderlineOffset: scaledOptional(node.textUnderlineOffset, scale),\n styleRuns: scaledStyleRuns(node.styleRuns, scale),\n listSpacing: node.listSpacing * scale,\n paragraphSpacing: node.paragraphSpacing * scale,\n paragraphIndent: node.paragraphIndent * scale,\n textParagraphs: scaledParagraphStyles(node.textParagraphs, scale),\n itemSpacing: node.itemSpacing * scale,\n counterAxisSpacing: node.counterAxisSpacing * scale,\n paddingTop: node.paddingTop * scale,\n paddingRight: node.paddingRight * scale,\n paddingBottom: node.paddingBottom * scale,\n paddingLeft: node.paddingLeft * scale,\n gridColumnGap: node.gridColumnGap * scale,\n gridRowGap: node.gridRowGap * scale,\n gridTemplateColumns: scaledGridTracks(node.gridTemplateColumns, scale),\n gridTemplateRows: scaledGridTracks(node.gridTemplateRows, scale),\n strokes: scaledStrokes(node.strokes, scale),\n strokeWeight: node.strokeWeight * scale,\n dashPattern: node.dashPattern.map((value) => value * scale),\n borderTopWeight: node.borderTopWeight * scale,\n borderRightWeight: node.borderRightWeight * scale,\n borderBottomWeight: node.borderBottomWeight * scale,\n borderLeftWeight: node.borderLeftWeight * scale,\n strokeMiterLimit: node.strokeMiterLimit,\n effects: scaledEffects(node.effects, scale),\n fills: scaledFills(node.fills, scale),\n layoutGrids: scaledLayoutGrids(node.layoutGrids, scale),\n vectorNetwork: scaledVectorNetwork(node, scale),\n fillGeometry: scaleGeometryPaths(node.fillGeometry, scale, scale),\n strokeGeometry: scaleGeometryPaths(node.strokeGeometry, scale, scale)\n }\n}\n"],"mappings":";;;;AAcA,SAAS,eAAe,OAAsB,OAA8B;CAC1E,OAAO,SAAS,OAAO,OAAO,QAAQ;AACxC;AAEA,SAAS,cAAc,SAA4B,OAAyB;CAC1E,OAAO,QAAQ,KAAK,YAAY;EAC9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,QAAQ,KAAK;EAC1D,iBAAiB,KAAK,kBAAkB;EACxC,mBAAmB,KAAK,oBAAoB;EAC5C,oBAAoB,KAAK,qBAAqB;EAC9C,kBAAkB,KAAK,mBAAmB;EAC1C,kBAAkB,KAAK;EACvB,SAAS,cAAc,KAAK,SAAS,KAAK;EAC1C,OAAO,YAAY,KAAK,OAAO,KAAK;EACpC,aAAa,kBAAkB,KAAK,aAAa,KAAK;EACtD,eAAe,oBAAoB,MAAM,KAAK;EAC9C,cAAc,mBAAmB,KAAK,cAAc,OAAO,KAAK;EAChE,gBAAgB,mBAAmB,KAAK,gBAAgB,OAAO,KAAK;CACtE;AACF"}
1
+ {"version":3,"file":"node.js","names":[],"sources":["../../src/scaling/node.ts"],"sourcesContent":["import { cloneVectorNetwork, copyDerivedGlyphs, scaleGeometryPaths } from '../copy'\nimport { scaledTextLength } from '../text/lengths'\nimport { TEXT_PARAGRAPH_SPACING_FIELDS } from '../text/paragraphs'\nimport { followsPath } from '../text/path'\nimport {\n type Effect,\n type Fill,\n type GridTrack,\n type LayoutGrid,\n type SceneNode,\n type Stroke,\n type StyleRun,\n type TextParagraphStyle\n} from '../types'\n\nfunction scaledOptional(value: number | null, scale: number): number | null {\n return value == null ? null : value * scale\n}\n\nfunction scaledStrokes(strokes: readonly Stroke[], scale: number): Stroke[] {\n return strokes.map((stroke) => ({\n ...structuredClone(stroke),\n weight: stroke.weight * scale,\n dashPattern: stroke.dashPattern?.map((value) => value * scale)\n }))\n}\n\nfunction scaledEffects(effects: readonly Effect[], scale: number): Effect[] {\n return effects.map((effect) => ({\n ...structuredClone(effect),\n offset: { x: effect.offset.x * scale, y: effect.offset.y * scale },\n radius: effect.radius * scale,\n spread: effect.spread * scale,\n ...(effect.progressive\n ? {\n progressive: {\n ...structuredClone(effect.progressive),\n startRadius: effect.progressive.startRadius * scale\n }\n }\n : {})\n }))\n}\n\nfunction scaledFills(fills: readonly Fill[], scale: number): Fill[] {\n return fills.map((fill) => ({\n ...structuredClone(fill),\n scale: fill.scale == null ? undefined : fill.scale * scale,\n spacing: fill.spacing == null ? undefined : fill.spacing * scale,\n patternSpacing: fill.patternSpacing\n ? { x: fill.patternSpacing.x * scale, y: fill.patternSpacing.y * scale }\n : undefined,\n noiseSize: fill.noiseSize\n ? { x: fill.noiseSize.x * scale, y: fill.noiseSize.y * scale }\n : undefined\n }))\n}\n\nfunction scaledStyleRuns(styleRuns: readonly StyleRun[], scale: number): StyleRun[] {\n return styleRuns.map((run) => ({\n ...structuredClone(run),\n style: {\n ...structuredClone(run.style),\n fontSize: run.style.fontSize == null ? undefined : run.style.fontSize * scale,\n letterSpacing: run.style.letterSpacing && scaledTextLength(run.style.letterSpacing, scale),\n lineHeight: run.style.lineHeight && scaledTextLength(run.style.lineHeight, scale),\n textDecorationThickness: scaledOptional(run.style.textDecorationThickness ?? null, scale),\n textUnderlineOffset: scaledOptional(run.style.textUnderlineOffset ?? null, scale)\n }\n }))\n}\n\nfunction scaledParagraphStyles(\n paragraphs: readonly TextParagraphStyle[],\n scale: number\n): TextParagraphStyle[] {\n return paragraphs.map((style) => {\n const scaled = { ...style }\n for (const field of TEXT_PARAGRAPH_SPACING_FIELDS) {\n const value = style[field]\n if (value !== undefined) scaled[field] = value * scale\n }\n return scaled\n })\n}\n\nfunction scaledLayoutGrids(grids: readonly LayoutGrid[], scale: number): LayoutGrid[] {\n return grids.map((grid) => ({\n ...structuredClone(grid),\n offset: grid.offset == null ? undefined : grid.offset * scale,\n sectionSize: grid.sectionSize == null ? undefined : grid.sectionSize * scale,\n gutterSize: grid.gutterSize == null ? undefined : grid.gutterSize * scale\n }))\n}\n\nfunction scaledGridTracks(tracks: readonly GridTrack[], scale: number): GridTrack[] {\n return tracks.map((track) => ({\n ...track,\n value: track.sizing === 'FIXED' ? track.value * scale : track.value\n }))\n}\n\nfunction scaledVectorNetwork(node: SceneNode, scale: number): SceneNode['vectorNetwork'] {\n if (!node.vectorNetwork) return null\n const network = cloneVectorNetwork(node.vectorNetwork)\n for (const vertex of network.vertices) {\n vertex.x *= scale\n vertex.y *= scale\n }\n for (const segment of network.segments) {\n segment.tangentStart.x *= scale\n segment.tangentStart.y *= scale\n segment.tangentEnd.x *= scale\n segment.tangentEnd.y *= scale\n }\n // A region's own paints scale as the layer's do.\n for (const region of network.regions) {\n if (region.fills) region.fills = scaledFills(region.fills, scale)\n }\n return network\n}\n\nfunction scaledPathText(node: SceneNode, scale: number): Partial<SceneNode> {\n if (!node.textPathData && !followsPath(node)) {\n return {}\n }\n const box = node.textPathBox\n return {\n derivedTextGlyphs:\n copyDerivedGlyphs(node.derivedTextGlyphs)?.map((glyph) => ({\n ...glyph,\n x: glyph.x * scale,\n y: glyph.y * scale,\n fontSize: glyph.fontSize * scale,\n ...(glyph.advance === undefined ? {} : { advance: glyph.advance * scale })\n })) ?? null,\n textPathBox: box && {\n x: box.x * scale,\n y: box.y * scale,\n width: box.width * scale,\n height: box.height * scale\n }\n }\n}\n\nexport function scaleNodeChanges(\n node: SceneNode,\n scale: number,\n scalePosition: boolean\n): Partial<SceneNode> {\n return {\n x: scalePosition ? node.x * scale : node.x,\n y: scalePosition ? node.y * scale : node.y,\n width: node.width * scale,\n height: node.height * scale,\n minWidth: scaledOptional(node.minWidth, scale),\n maxWidth: scaledOptional(node.maxWidth, scale),\n minHeight: scaledOptional(node.minHeight, scale),\n maxHeight: scaledOptional(node.maxHeight, scale),\n cornerRadius: node.cornerRadius * scale,\n topLeftRadius: node.topLeftRadius * scale,\n topRightRadius: node.topRightRadius * scale,\n bottomRightRadius: node.bottomRightRadius * scale,\n bottomLeftRadius: node.bottomLeftRadius * scale,\n fontSize: node.fontSize * scale,\n letterSpacing: scaledTextLength(node.letterSpacing, scale),\n lineHeight: node.lineHeight && scaledTextLength(node.lineHeight, scale),\n textDecorationThickness: scaledOptional(node.textDecorationThickness, scale),\n textUnderlineOffset: scaledOptional(node.textUnderlineOffset, scale),\n styleRuns: scaledStyleRuns(node.styleRuns, scale),\n listSpacing: node.listSpacing * scale,\n paragraphSpacing: node.paragraphSpacing * scale,\n paragraphIndent: node.paragraphIndent * scale,\n textParagraphs: scaledParagraphStyles(node.textParagraphs, scale),\n derivedTextHeight: scaledOptional(node.derivedTextHeight, scale),\n itemSpacing: node.itemSpacing * scale,\n counterAxisSpacing: node.counterAxisSpacing === null ? null : node.counterAxisSpacing * scale,\n paddingTop: node.paddingTop * scale,\n paddingRight: node.paddingRight * scale,\n paddingBottom: node.paddingBottom * scale,\n paddingLeft: node.paddingLeft * scale,\n gridColumnGap: node.gridColumnGap * scale,\n gridRowGap: node.gridRowGap * scale,\n gridTemplateColumns: scaledGridTracks(node.gridTemplateColumns, scale),\n gridTemplateRows: scaledGridTracks(node.gridTemplateRows, scale),\n strokes: scaledStrokes(node.strokes, scale),\n strokeWeight: node.strokeWeight * scale,\n dashPattern: node.dashPattern.map((value) => value * scale),\n borderTopWeight: node.borderTopWeight * scale,\n borderRightWeight: node.borderRightWeight * scale,\n borderBottomWeight: node.borderBottomWeight * scale,\n borderLeftWeight: node.borderLeftWeight * scale,\n strokeMiterLimit: node.strokeMiterLimit,\n effects: scaledEffects(node.effects, scale),\n fills: scaledFills(node.fills, scale),\n layoutGrids: scaledLayoutGrids(node.layoutGrids, scale),\n vectorNetwork: scaledVectorNetwork(node, scale),\n fillGeometry: scaleGeometryPaths(node.fillGeometry, scale, scale),\n strokeGeometry: scaleGeometryPaths(node.strokeGeometry, scale, scale),\n ...scaledPathText(node, scale)\n }\n}\n"],"mappings":";;;;;AAeA,SAAS,eAAe,OAAsB,OAA8B;CAC1E,OAAO,SAAS,OAAO,OAAO,QAAQ;AACxC;AAEA,SAAS,cAAc,SAA4B,OAAyB;CAC1E,OAAO,QAAQ,KAAK,YAAY;EAC9B,GAAG,gBAAgB,MAAM;EACzB,QAAQ,OAAO,SAAS;EACxB,aAAa,OAAO,aAAa,KAAK,UAAU,QAAQ,KAAK;CAC/D,EAAE;AACJ;AAEA,SAAS,cAAc,SAA4B,OAAyB;CAC1E,OAA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;EAAe"}
1
+ {"version":3,"file":"model.d.ts","names":[],"sources":["../../src/shaders/model.ts"],"mappings":";;;;KAaK,aAAa,KAAK;;iBAGP,iBAAiB,MAAM,KAAK,2BAA2B;;;;;;iBASvD,cACd,MAAM,KAAK,0BACX,OAAO,OACN;;iBAMa,eAAe,MAAM;;;;;iBAQrB,iBACd,MAAM,KAAK,0BACX,iBAAiB,QACjB,mBAAkB,gBACjB;;;;;iBAea,kBAAkB,QAAQ;EACxC,OAAO;EACP,QAAQ;;;iBAiBM,gBAAgB,QAAQ;;;;;iBAQxB,qBACd,QAAQ,aACR;EAAQ;EAAe"}
@@ -1,8 +1,8 @@
1
1
  import { TRANSPARENT } from "../constants.js";
2
- import { randomHex } from "../random.js";
3
2
  import { readAllPluginData, withAllPluginData } from "../plugin-data/field.js";
4
3
  import { OPEN_PENCIL_PLUGIN_DATA } from "../plugin-data/fields.js";
5
4
  import { imagePaintHashes } from "../images.js";
5
+ import { randomHex } from "@open-pencil/std/random";
6
6
  import { sha1 } from "@noble/hashes/legacy.js";
7
7
  import { bytesToHex, utf8ToBytes } from "@noble/hashes/utils.js";
8
8
  //#region src/shaders/model.ts
@@ -1 +1 @@
1
- {"version":3,"file":"model.js","names":[],"sources":["../../src/shaders/model.ts"],"sourcesContent":["import { sha1 } from '@noble/hashes/legacy.js'\nimport { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js'\n\nimport { TRANSPARENT } from '../constants'\nimport { imagePaintHashes } from '../images'\nimport { readAllPluginData, withAllPluginData } from '../plugin-data/field'\nimport { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'\nimport { randomHex } from '../random'\nimport type { Fill, SceneNode } from '../types'\nimport type { ShaderPaint, ShaderPreset } from './schema'\n\n/** What shader paints read of a node. */\ntype ShaderNode = Pick<SceneNode, 'fills' | 'strokes' | 'pluginData'>\n\n/** Every shader a node's fills and strokes draw, whether or not a paint still shows it. */\nexport function readShaderPaints(node: Pick<SceneNode, 'pluginData'>): ShaderPaint[] {\n return readAllPluginData(node.pluginData, OPEN_PENCIL_PLUGIN_DATA.shader)\n}\n\n/**\n * The shader a fill or stroke of `node` draws: an image paint whose image is a shader's frame.\n * Null for any other paint, including one whose image was replaced, which then draws as the\n * image it holds.\n */\nexport function shaderOfPaint(\n node: Pick<SceneNode, 'pluginData'>,\n paint: Fill\n): ShaderPaint | null {\n if (paint.type !== 'IMAGE' || !paint.imageHash) return null\n return readShaderPaints(node).find((shader) => shader.image === paint.imageHash) ?? null\n}\n\n/** Whether any paint of `node` draws a shader. */\nexport function hasShaderPaint(node: ShaderNode): boolean {\n return [...node.fills, ...node.strokes].some((paint) => shaderOfPaint(node, paint) !== null)\n}\n\n/**\n * The node's plugin data with `shaders` set by their image, keeping only shaders a fill or\n * stroke of `paints` still shows, so removing or replacing a paint leaves no entry behind.\n */\nexport function withShaderPaints(\n node: Pick<SceneNode, 'pluginData'>,\n paints: readonly Fill[],\n shaders: readonly ShaderPaint[] = []\n): SceneNode['pluginData'] {\n const shown = new Set(imagePaintHashes(paints))\n const byImage = new Map(readShaderPaints(node).map((shader) => [shader.image, shader]))\n for (const shader of shaders) byImage.set(shader.image, shader)\n return withAllPluginData(\n node.pluginData,\n OPEN_PENCIL_PLUGIN_DATA.shader,\n [...byImage.values()].filter((shader) => shown.has(shader.image))\n )\n}\n\n/**\n * A new paint that draws `preset`, and the shader entry it shows. Its image names a frame not yet\n * rendered, unique to this paint, so no other paint's entry claims it until the frame is drawn.\n */\nexport function createShaderPaint(preset: ShaderPreset): {\n paint: Fill\n shader: ShaderPaint\n} {\n const image = randomHex(20)\n return {\n paint: {\n type: 'IMAGE',\n color: TRANSPARENT,\n opacity: 1,\n visible: true,\n imageHash: image,\n imageScaleMode: 'FILL'\n },\n shader: { image, preset: structuredClone(preset) }\n }\n}\n\n/** A short key that tells one preset from another, which a rendered frame records. */\nexport function shaderPresetKey(preset: ShaderPreset): string {\n return bytesToHex(sha1(utf8ToBytes(JSON.stringify(preset))))\n}\n\n/**\n * Whether a shader's frame was rendered for its preset at the size `node` has now, so undoing a\n * preset change after reopening the document does not keep a frame of the newer preset.\n */\nexport function isShaderFrameCurrent(\n shader: ShaderPaint,\n size: { width: number; height: number }\n): boolean {\n const { frame } = shader\n return (\n frame !== undefined &&\n frame.preset === shaderPresetKey(shader.preset) &&\n Math.round(frame.width) === Math.round(size.width) &&\n Math.round(frame.height) === Math.round(size.height)\n )\n}\n"],"mappings":";;;;;;;;;AAeA,SAAgB,iBAAiB,MAAoD;CACnF,OAAO,kBAAkB,KAAK,YAAY,wBAAwB,MAAM;AAC1E;;;;;;AAOA,SAAgB,cACd,MACA,OACoB;CACpB,IAAI,MAAM,SAAS,WAAW,CAAC,MAAM,WAAW,OAAO;CACvD,OAAO,iBAAiB,IAAI,CAAC,CAAC,MAAM,WAAW,OAAO,UAAU,MAAM,SAAS,KAAK;AACtF;;AAGA,SAAgB,eAAe,MAA2B;CACxD,OAAO,CAAC,GAAG,KAAK,OAAO,GAAG,KAAK,OAAO,CAAC,CAAC,MAAM,UAAU,cAAc,MAAM,KAAK,MAAM,IAAI;AAC7F;;;;;AAMA,SAAgB,iBACd,MACA,QACA,UAAkC,CAAC,GACV;CACzB,MAAM,QAAQ,IAAI,IAAI,iBAAiB,MAAM,CAAC;CAC9C,MAAM,UAAU,IAAI,IAAI,iBAAiB,IAAI,CAAC,CAAC,KAAK,WAAW,CAAC,OAAO,OAAO,MAAM,CAAC,CAAC;CACtF,KAAK,MAAM,UAAU,SAAS,QAAQ,IAAI,OAAO,OAAO,MAAM;CAC9D,OAAO,kBACL,KAAK,YACL,wBAAwB,QACxB,CAAC,GAAG,QAAQ,OAAO,CAAC,CAAC,CAAC,QAAQ,WAAW,MAAM,IAAI,OAAO,KAAK,CAAC,CAClE;AACF;;;;;AAMA,SAAgB,kBAAkB,QAGhC;CACA,MAAM,QAAQ,UAAU,EAAE;CAC1B,OAAO;EACL,OAAO;GACL,MAAM;GACN,OAAO;GACP,SAAS;GACT,SAAS;GACT,WAAW;GACX,gBAAgB;EAClB;EACA,QAAQ;GAAE;GAAO,QAAQ,gBAAgB,MAAM;EAAE;CACnD;AACF;;AAGA,SAAgB,gBAAgB,QAA8B;CAC5D,OAAO,WAAW,KAAK,YAAY,KAAK,UAAU,MAAM,CAAC,CAAC,CAAC;AAC7D;;;;;AAMA,SAAgB,qBACd,QACA,MACS;CACT,MAAM,EAAE,UAAU;CAClB,OACE,UAAU,KAAA,KACV,MAAM,WAAW,gBAAgB,OAAO,MAAM,KAC9C,KAAK,MAAM,MAAM,KAAK,MAAM,KAAK,MAAM,KAAK,KAAK,KACjD,KAAK,MAAM,MAAM,MAAM,MAAM,KAAK,MAAM,KAAK,MAAM;AAEvD"}
1
+ {"version":3,"file":"model.js","names":[],"sources":["../../src/shaders/model.ts"],"sourcesContent":["import { sha1 } from '@noble/hashes/legacy.js'\nimport { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js'\n\nimport { randomHex } from '@open-pencil/std/random'\n\nimport { TRANSPARENT } from '../constants'\nimport { imagePaintHashes } from '../images'\nimport { readAllPluginData, withAllPluginData } from '../plugin-data/field'\nimport { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'\nimport type { Fill, SceneNode } from '../types'\nimport type { ShaderPaint, ShaderPreset } from './schema'\n\n/** What shader paints read of a node. */\ntype ShaderNode = Pick<SceneNode, 'fills' | 'strokes' | 'pluginData'>\n\n/** Every shader a node's fills and strokes draw, whether or not a paint still shows it. */\nexport function readShaderPaints(node: Pick<SceneNode, 'pluginData'>): ShaderPaint[] {\n return readAllPluginData(node.pluginData, OPEN_PENCIL_PLUGIN_DATA.shader)\n}\n\n/**\n * The shader a fill or stroke of `node` draws: an image paint whose image is a shader's frame.\n * Null for any other paint, including one whose image was replaced, which then draws as the\n * image it holds.\n */\nexport function shaderOfPaint(\n node: Pick<SceneNode, 'pluginData'>,\n paint: Fill\n): ShaderPaint | null {\n if (paint.type !== 'IMAGE' || !paint.imageHash) return null\n return readShaderPaints(node).find((shader) => shader.image === paint.imageHash) ?? null\n}\n\n/** Whether any paint of `node` draws a shader. */\nexport function hasShaderPaint(node: ShaderNode): boolean {\n return [...node.fills, ...node.strokes].some((paint) => shaderOfPaint(node, paint) !== null)\n}\n\n/**\n * The node's plugin data with `shaders` set by their image, keeping only shaders a fill or\n * stroke of `paints` still shows, so removing or replacing a paint leaves no entry behind.\n */\nexport function withShaderPaints(\n node: Pick<SceneNode, 'pluginData'>,\n paints: readonly Fill[],\n shaders: readonly ShaderPaint[] = []\n): SceneNode['pluginData'] {\n const shown = new Set(imagePaintHashes(paints))\n const byImage = new Map(readShaderPaints(node).map((shader) => [shader.image, shader]))\n for (const shader of shaders) byImage.set(shader.image, shader)\n return withAllPluginData(\n node.pluginData,\n OPEN_PENCIL_PLUGIN_DATA.shader,\n [...byImage.values()].filter((shader) => shown.has(shader.image))\n )\n}\n\n/**\n * A new paint that draws `preset`, and the shader entry it shows. Its image names a frame not yet\n * rendered, unique to this paint, so no other paint's entry claims it until the frame is drawn.\n */\nexport function createShaderPaint(preset: ShaderPreset): {\n paint: Fill\n shader: ShaderPaint\n} {\n const image = randomHex(20)\n return {\n paint: {\n type: 'IMAGE',\n color: TRANSPARENT,\n opacity: 1,\n visible: true,\n imageHash: image,\n imageScaleMode: 'FILL'\n },\n shader: { image, preset: structuredClone(preset) }\n }\n}\n\n/** A short key that tells one preset from another, which a rendered frame records. */\nexport function shaderPresetKey(preset: ShaderPreset): string {\n return bytesToHex(sha1(utf8ToBytes(JSON.stringify(preset))))\n}\n\n/**\n * Whether a shader's frame was rendered for its preset at the size `node` has now, so undoing a\n * preset change after reopening the document does not keep a frame of the newer preset.\n */\nexport function isShaderFrameCurrent(\n shader: ShaderPaint,\n size: { width: number; height: number }\n): boolean {\n const { frame } = shader\n return (\n frame !== undefined &&\n frame.preset === shaderPresetKey(shader.preset) &&\n Math.round(frame.width) === Math.round(size.width) &&\n Math.round(frame.height) === Math.round(size.height)\n )\n}\n"],"mappings":";;;;;;;;;A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,WAAW,gBAAgB,OAAO,MAAM,KAC9C,KAAK,MAAM,MAAM,KAAK,MAAM,KAAK,MAAM,KAAK,KAAK,KACjD,KAAK,MAAM,MAAM,MAAM,MAAM,KAAK,MAAM,KAAK,MAAM;AAEvD"}
@@ -1 +1 @@
1
- {"version":3,"file":"shared-styles.d.ts","names":[],"sources":["../src/shared-styles.ts"],"mappings":";;;cAiBM;;;;;;;;cASO;;iBAGG,mBAAmB,MAAM;;cAiB5B;;cAwBA;;;;;;;;cASA;;;;;iBAUG,eACd,MAAM,KAAK,yBACX,OAAO;iBAMO,kBAAkB,MAAM,0BAA0B,gBAAgB;iBAIlE,uBAAuB,MAAM,kBAAkB;;iBAK/C,kBAAkB,OAAO,aAAa,YAAY;iBAOlD,gBAAgB,OAAO,YAAY,MAAM,kBAAkB;iBA0B3D,uBACd,MAAM,WACN,SAAS,QAAQ,aAChB,QAAQ;;iBAmBK,gBACd,OAAO,YACP,YACA,MAAM,kBACL;;iBASa;;iBAOA,eAAe,OAAO,aAAa;;;;;iBAmCnC,iBACd,MAAM,iBACN,QAAQ,WACR,iBACA,OAAO,mBACN,QAAQ;;iBAYK,uBAAuB,MAAM,kBAAkB,QAAQ"}
1
+ {"version":3,"file":"shared-styles.d.ts","names":[],"sources":["../src/shared-styles.ts"],"mappings":";;;cAmBM;;;;;;;;cASO;;iBAGG,mBAAmB,MAAM;;cAiB5B;;cAwBA;;;;;;;;cASA;;;;;iBAUG,eACd,MAAM,KAAK,yBACX,OAAO;iBAMO,kBAAkB,MAAM,0BAA0B,gBAAgB;iBAIlE,uBAAuB,MAAM,kBAAkB;;iBAK/C,kBAAkB,OAAO,aAAa,YAAY;iBAOlD,gBAAgB,OAAO,YAAY,MAAM,kBAAkB;iBAuB3D,uBACd,MAAM,WACN,SAAS,QAAQ,aAChB,QAAQ;;iBAmBK,gBACd,OAAO,YACP,YACA,MAAM,kBACL;;iBASa;;iBAOA,eAAe,OAAO,aAAa;;;;;iBAmCnC,iBACd,MAAM,iBACN,QAAQ,WACR,iBACA,OAAO,mBACN,QAAQ;;iBAYK,uBAAuB,MAAM,kBAAkB,QAAQ"}
@@ -1,9 +1,10 @@
1
1
  import { BLACK } from "./constants.js";
2
2
  import { newStrokeGeometry } from "./node-defaults.js";
3
3
  import { copyEffects, copyFills, copyLayoutGrids } from "./copy.js";
4
- import { randomHex } from "./random.js";
4
+ import { STROKE_LINE_KEYS } from "./strokes.js";
5
5
  import { styleRunReferences } from "./text/references.js";
6
6
  import { compact, isEqual, omit, pick } from "es-toolkit";
7
+ import { randomHex } from "@open-pencil/std/random";
7
8
  //#region src/shared-styles.ts
8
9
  const STYLE_REF_KEYS = {
9
10
  fill: "fillStyleId",
@@ -99,14 +100,6 @@ function getSharedStyles(graph, kind) {
99
100
  }
100
101
  return styles.sort((left, right) => left.name.localeCompare(right.name));
101
102
  }
102
- /** What a stroke draws along beside its paint: the layer's, not its stroke style's. */
103
- const STROKE_LINE_KEYS = [
104
- "weight",
105
- "align",
106
- "cap",
107
- "join",
108
- "dashPattern"
109
- ];
110
103
  /**
111
104
  * Whether new strokes only change how their lines run, keeping every paint. A stroke style gives
112
105
  * paints only, so such a write keeps it; any other strokes write leaves it, as setting even the
@@ -1 +1 @@
1
- {"version":3,"file":"shared-styles.js","names":[],"sources":["../src/shared-styles.ts"],"sourcesContent":["import { compact, isEqual, omit, pick } from 'es-toolkit'\n\nimport { BLACK } from './constants'\nimport { copyEffects, copyFills, copyLayoutGrids } from './copy'\nimport type { SceneGraph } from './graph'\nimport { newStrokeGeometry } from './node-defaults'\nimport { randomHex } from './random'\nimport { styleRunReferences } from './text/references'\nimport type {\n CharacterStyleOverride,\n SceneNode,\n SharedStyle,\n SharedStyleKind,\n SharedStyleType,\n Stroke\n} from './types'\n\nconst STYLE_REF_KEYS = {\n fill: 'fillStyleId',\n stroke: 'strokeStyleId',\n text: 'textStyleId',\n effect: 'effectStyleId',\n grid: 'gridStyleId'\n} as const satisfies Record<SharedStyleKind, keyof SceneNode>\n\n/** The fields that link a layer to its shared styles. */\nexport const STYLE_LINK_FIELDS = Object.values(STYLE_REF_KEYS)\n\n/** The shared styles a layer uses: its own, its text ranges', and its vector regions'. */\nexport function referencedStyleIds(node: SceneNode): string[] {\n return compact([\n ...STYLE_LINK_FIELDS.map((field) => node[field]),\n ...styleRunReferences(node.styleRuns).styles,\n ...(node.vectorNetwork?.regions ?? []).map((region) => region.fillStyleId)\n ])\n}\n\nconst STYLE_TYPES = {\n fill: 'FILL',\n stroke: 'FILL',\n text: 'TEXT',\n effect: 'EFFECT',\n grid: 'GRID'\n} as const satisfies Record<SharedStyleKind, SharedStyleType>\n\n/** The fields a text style gives what uses it; changing one leaves the style. */\nexport const SHARED_TEXT_STYLE_FIELDS = [\n 'fontFamily',\n 'fontWeight',\n 'italic',\n 'fontStyle',\n 'fontSize',\n 'lineHeight',\n 'letterSpacing',\n 'textDecoration',\n 'textCase',\n 'fontFeatures'\n] as const satisfies readonly (keyof SceneNode)[]\n\n/**\n * The paragraph fields a text style also gives the whole text; a range takes only the character\n * ones. Changing one leaves the style, and a cleared style resets them (live-verified).\n */\nconst TEXT_STYLE_PARAGRAPH_FIELDS = [\n 'paragraphSpacing',\n 'paragraphIndent',\n 'listSpacing'\n] as const satisfies readonly (keyof SceneNode)[]\n\n/** The fields each kind of shared style gives what uses it; changing one leaves the style. */\nexport const SHARED_STYLE_FIELDS = {\n fill: ['fills'],\n stroke: ['strokes'],\n text: [...SHARED_TEXT_STYLE_FIELDS, ...TEXT_STYLE_PARAGRAPH_FIELDS],\n effect: ['effects'],\n grid: ['layoutGrids']\n} as const satisfies Record<SharedStyleKind, readonly (keyof SceneNode)[]>\n\n/** The fields that pick a text's face, which its style name names. */\nexport const FACE_KEYS = [\n 'fontFamily',\n 'fontWeight',\n 'italic'\n] as const satisfies readonly (keyof SceneNode)[]\n\n/**\n * The face name of characters with `style` over the text: their own, none where they set another\n * face without one (as ranges from HTML or `.pen` do), else the text's.\n */\nexport function rangeFontStyle(\n node: Pick<SceneNode, 'fontStyle'>,\n style: CharacterStyleOverride\n): string | null {\n if (style.fontStyle !== undefined) return style.fontStyle\n return FACE_KEYS.some((key) => style[key] !== undefined) ? null : node.fontStyle\n}\n\nexport function sharedStyleRefKey(kind: SharedStyleKind): (typeof STYLE_REF_KEYS)[SharedStyleKind] {\n return STYLE_REF_KEYS[kind]\n}\n\nexport function sharedStyleTypeForKind(kind: SharedStyleKind): SharedStyleType {\n return STYLE_TYPES[kind]\n}\n\n/** The document's shared style layers, by the source ID layers link them by. */\nexport function sharedStyleLayers(graph: SceneGraph): Map<string, SceneNode> {\n const styles = new Map<string, SceneNode>()\n for (const node of graph.getAllNodes())\n if (node.sharedStyleType !== null && node.source.id) styles.set(node.source.id, node)\n return styles\n}\n\nexport function getSharedStyles(graph: SceneGraph, kind: SharedStyleKind): SharedStyle[] {\n const type = sharedStyleTypeForKind(kind)\n const styles: SharedStyle[] = []\n for (const node of graph.getAllNodes()) {\n if (node.sharedStyleType !== type || !node.source.id) continue\n styles.push({ id: node.source.id, nodeId: node.id, name: node.name, type })\n }\n return styles.sort((left, right) => left.name.localeCompare(right.name))\n}\n\n/** What a stroke draws along beside its paint: the layer's, not its stroke style's. */\nconst STROKE_LINE_KEYS = ['weight', 'align', 'cap', 'join', 'dashPattern'] as const\n\n/**\n * Whether new strokes only change how their lines run, keeping every paint. A stroke style gives\n * paints only, so such a write keeps it; any other strokes write leaves it, as setting even the\n * same paints does in Figma (live-verified).\n */\nfunction restylesStrokeLines(current: readonly Stroke[], next: readonly Stroke[]): boolean {\n const paints = (strokes: readonly Stroke[]) =>\n strokes.map((stroke) => omit(stroke, STROKE_LINE_KEYS))\n const lines = (strokes: readonly Stroke[]) =>\n strokes.map((stroke) => STROKE_LINE_KEYS.map((key) => stroke[key]))\n return isEqual(paints(current), paints(next)) && !isEqual(lines(current), lines(next))\n}\n\nexport function styleDetachmentChanges(\n node: SceneNode,\n changes: Partial<SceneNode>\n): Partial<SceneNode> {\n const next = { ...changes }\n for (const [kind, fields] of Object.entries(SHARED_STYLE_FIELDS)) {\n const reference = STYLE_REF_KEYS[kind as SharedStyleKind]\n if (reference in changes || !node[reference]) continue\n if (!fields.some((field) => field in changes)) continue\n if (kind === 'stroke' && changes.strokes && restylesStrokeLines(node.strokes, changes.strokes))\n continue\n next[reference] = null\n }\n // A face named for another family, weight, or slant names none; the new one takes its own name.\n const changesFace = FACE_KEYS.some((key) => key in changes && changes[key] !== node[key])\n if (changesFace && !('fontStyle' in changes) && node.fontStyle) {\n next.fontStyle = null\n }\n return next\n}\n\n/** The node of the shared style with `id`, when the document holds one of `type`. */\nexport function findSharedStyle(\n graph: SceneGraph,\n id: string,\n type: SharedStyleType\n): SceneNode | undefined {\n if (!id) return undefined\n for (const node of graph.getAllNodes()) {\n if (node.sharedStyleType === type && node.source.id === id) return node\n }\n return undefined\n}\n\n/** A new local style's ID, in the shape Figma gives one: `S:` and a 40-digit hex key. */\nexport function newSharedStyleId(): string {\n return `S:${randomHex(20)},`\n}\n\nconst INTERNAL_CANVAS_NAME = 'Internal Only Canvas'\n\n/** The page that holds the document's styles out of view, as a `.fig` file keeps them; made once. */\nexport function internalCanvas(graph: SceneGraph): SceneNode {\n const existing = graph.getPages(true).find((page) => page.internalOnly)\n if (existing) return existing\n return graph.createNode('CANVAS', graph.rootId, {\n name: INTERNAL_CANVAS_NAME,\n width: 0,\n height: 0,\n internalOnly: true\n })\n}\n\n/**\n * A paint style's paints as a layer's strokes: every paint, each keeping the weight, alignment,\n * and line style of the stroke it replaces, or of the layer's first; no paints clear the strokes\n * (Figma desktop 126).\n */\nfunction strokesFromStyle(target: SceneNode, style: SceneNode): SceneNode['strokes'] {\n // Without strokes, the layer keeps the weight and alignment a new stroke takes.\n const fallback: Stroke = target.strokes[0] ?? {\n type: 'SOLID',\n color: BLACK,\n opacity: 1,\n visible: true,\n ...newStrokeGeometry(target)\n }\n return copyFills(style.fills).map((paint, index) => ({\n ...paint,\n ...pick(target.strokes[index] ?? fallback, STROKE_LINE_KEYS)\n }))\n}\n\n/**\n * What a layer takes from a shared style it starts using: the style's fields and the reference\n * that keeps it attached. Without the style's node, the reference alone.\n */\nexport function sharedStylePatch(\n kind: SharedStyleKind,\n target: SceneNode,\n styleId: string,\n style: SceneNode | null\n): Partial<SceneNode> {\n const patch: Partial<SceneNode> = { [STYLE_REF_KEYS[kind]]: styleId }\n if (!style) return patch\n if (kind === 'fill') patch.fills = copyFills(style.fills)\n else if (kind === 'stroke') patch.strokes = strokesFromStyle(target, style)\n else if (kind === 'effect') patch.effects = copyEffects(style.effects)\n else if (kind === 'grid') patch.layoutGrids = copyLayoutGrids(style.layoutGrids)\n else Object.assign(patch, structuredClone(pick(style, SHARED_STYLE_FIELDS.text)))\n return patch\n}\n\n/** A layer leaving a shared style: it keeps the style's values, without the reference. */\nexport function sharedStyleDetachPatch(kind: SharedStyleKind): Partial<SceneNode> {\n return { [STYLE_REF_KEYS[kind]]: null }\n}\n"],"mappings":";;;;;;;AAiBA,MAAM,iBAAiB;CACrB,MAAM;CACN,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,MAAM;AACR;;AAGA,MAAa,oBAAoB,OAAO,OAAO,cAAc;;AAG7D,SAAgB,mBAAmB,MAA2B;CAC5D,OAAO,QAAQ;EACb,GAAG,kBAAkB,KAAK,UAAU,KAAK,MAAM;EAC/C,GAAG,mBAAmB,KAAK,SAAS,CAAC,CAAC;EACtC,IAAI,KAAK,eAAe,WAAW,CAAC,EAAA,CAAG,KAAK,WAAW,OAAO,WAAW;CAC3E,CAAC;AACH;AAEA,MAAM,cAAc;CAClB,MAAM;CACN,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,MAAM;AACR;;AAGA,MAAa,2BAA2B;CACtC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;;AAMA,MAAM,8BAA8B;CAClC;CACA;CACA;AACF;;AAGA,MAAa,sBAAsB;CACjC,MAAM,CAAC,OAAO;CACd,QAAQ,CAAC,SAAS;CAClB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EACT,SAAS;EACT,GAAG,kBAAkB,MAAM;CAC7B;CACA,OAAO,UAAU,MAAM,KAAK,CAAC,CAAC,KAAK,OAAO,WAAW;EACnD,GAAG;EACH,GAAG,KAAK,OAAO,QAAQ,UAAU,UAAU,gBAAgB;CAC7D,EAAE;AACJ;;;;;AAMA,SAAgB,iBACd,MACA,QACA,SACA,OACoB;CACpB,MAAM,QAA4B,GAAG,eAAe,QAAQ,QAAQ;CACpE,IAAI,CAAC,OAAO,OAAO;CACnB,IAAI,SAAS,QAAQ,MAAM,QAAQ,UAAU,MAAM,KAAK;MACnD,IAAI,SAAS,UAAU,MAAM,UAAU,iBAAiB,QAAQ,KAAK;MACrE,IAAI,SAAS,UAAU,MAAM,UAAU,YAAY,MAAM,OAAO;MAChE,IAAI,SAAS,QAAQ,MAAM,cAAc,gBAAgB,MAAM,WAAW;MAC1E,OAAO,OAAO,OAAO,gBAAgB,KAAK,OAAO,oBAAoB,IAAI,CAAC,CAAC;CAChF,OAAO;AACT;;AAGA,SAAgB,uBAAuB,MAA2C;CAChF,OAAO,GAAG,eAAe,QAAQ,KAAK;AACxC"}
1
+ {"version":3,"file":"shared-styles.js","names":[],"sources":["../src/shared-styles.ts"],"sourcesContent":["import { compact, isEqual, omit, pick } from 'es-toolkit'\n\nimport { randomHex } from '@open-pencil/std/random'\n\nimport { BLACK } from './constants'\nimport { copyEffects, copyFills, copyLayoutGrids } from './copy'\nimport type { SceneGraph } from './graph'\nimport { newStrokeGeometry } from './node-defaults'\nimport { STROKE_LINE_KEYS } from './strokes'\nimport { styleRunReferences } from './text/references'\nimport type {\n CharacterStyleOverride,\n SceneNode,\n SharedStyle,\n SharedStyleKind,\n SharedStyleType,\n Stroke\n} from './types'\n\nconst STYLE_REF_KEYS = {\n fill: 'fillStyleId',\n stroke: 'strokeStyleId',\n text: 'textStyleId',\n effect: 'effectStyleId',\n grid: 'gridStyleId'\n} as const satisfies Record<SharedStyleKind, keyof SceneNode>\n\n/** The fields that link a layer to its shared styles. */\nexport const STYLE_LINK_FIELDS = Object.values(STYLE_REF_KEYS)\n\n/** The shared styles a layer uses: its own, its text ranges', and its vector regions'. */\nexport function referencedStyleIds(node: SceneNode): string[] {\n return compact([\n ...STYLE_LINK_FIELDS.map((field) => node[field]),\n ...styleRunReferences(node.styleRuns).styles,\n ...(node.vectorNetwork?.regions ?? []).map((region) => region.fillStyleId)\n ])\n}\n\nconst STYLE_TYPES = {\n fill: 'FILL',\n stroke: 'FILL',\n text: 'TEXT',\n effect: 'EFFECT',\n grid: 'GRID'\n} as const satisfies Record<SharedStyleKind, SharedStyleType>\n\n/** The fields a text style gives what uses it; changing one leaves the style. */\nexport const SHARED_TEXT_STYLE_FIELDS = [\n 'fontFamily',\n 'fontWeight',\n 'italic',\n 'fontStyle',\n 'fontSize',\n 'lineHeight',\n 'letterSpacing',\n 'textDecoration',\n 'textCase',\n 'fontFeatures'\n] as const satisfies readonly (keyof SceneNode)[]\n\n/**\n * The paragraph fields a text style also gives the whole text; a range takes only the character\n * ones. Changing one leaves the style, and a cleared style resets them (live-verified).\n */\nconst TEXT_STYLE_PARAGRAPH_FIELDS = [\n 'paragraphSpacing',\n 'paragraphIndent',\n 'listSpacing'\n] as const satisfies readonly (keyof SceneNode)[]\n\n/** The fields each kind of shared style gives what uses it; changing one leaves the style. */\nexport const SHARED_STYLE_FIELDS = {\n fill: ['fills'],\n stroke: ['strokes'],\n text: [...SHARED_TEXT_STYLE_FIELDS, ...TEXT_STYLE_PARAGRAPH_FIELDS],\n effect: ['effects'],\n grid: ['layoutGrids']\n} as const satisfies Record<SharedStyleKind, readonly (keyof SceneNode)[]>\n\n/** The fields that pick a text's face, which its style name names. */\nexport const FACE_KEYS = [\n 'fontFamily',\n 'fontWeight',\n 'italic'\n] as const satisfies readonly (keyof SceneNode)[]\n\n/**\n * The face name of characters with `style` over the text: their own, none where they set another\n * face without one (as ranges from HTML or `.pen` do), else the text's.\n */\nexport function rangeFontStyle(\n node: Pick<SceneNode, 'fontStyle'>,\n style: CharacterStyleOverride\n): string | null {\n if (style.fontStyle !== undefined) return style.fontStyle\n return FACE_KEYS.some((key) => style[key] !== undefined) ? null : node.fontStyle\n}\n\nexport function sharedStyleRefKey(kind: SharedStyleKind): (typeof STYLE_REF_KEYS)[SharedStyleKind] {\n return STYLE_REF_KEYS[kind]\n}\n\nexport function sharedStyleTypeForKind(kind: SharedStyleKind): SharedStyleType {\n return STYLE_TYPES[kind]\n}\n\n/** The document's shared style layers, by the source ID layers link them by. */\nexport function sharedStyleLayers(graph: SceneGraph): Map<string, SceneNode> {\n const styles = new Map<string, SceneNode>()\n for (const node of graph.getAllNodes())\n if (node.sharedStyleType !== null && node.source.id) styles.set(node.source.id, node)\n return styles\n}\n\nexport function getSharedStyles(graph: SceneGraph, kind: SharedStyleKind): SharedStyle[] {\n const type = sharedStyleTypeForKind(kind)\n const styles: SharedStyle[] = []\n for (const node of graph.getAllNodes()) {\n if (node.sharedStyleType !== type || !node.source.id) continue\n styles.push({ id: node.source.id, nodeId: node.id, name: node.name, type })\n }\n return styles.sort((left, right) => left.name.localeCompare(right.name))\n}\n\n/**\n * Whether new strokes only change how their lines run, keeping every paint. A stroke style gives\n * paints only, so such a write keeps it; any other strokes write leaves it, as setting even the\n * same paints does in Figma (live-verified).\n */\nfunction restylesStrokeLines(current: readonly Stroke[], next: readonly Stroke[]): boolean {\n const paints = (strokes: readonly Stroke[]) =>\n strokes.map((stroke) => omit(stroke, STROKE_LINE_KEYS))\n const lines = (strokes: readonly Stroke[]) =>\n strokes.map((stroke) => STROKE_LINE_KEYS.map((key) => stroke[key]))\n return isEqual(paints(current), paints(next)) && !isEqual(lines(current), lines(next))\n}\n\nexport function styleDetachmentChanges(\n node: SceneNode,\n changes: Partial<SceneNode>\n): Partial<SceneNode> {\n const next = { ...changes }\n for (const [kind, fields] of Object.entries(SHARED_STYLE_FIELDS)) {\n const reference = STYLE_REF_KEYS[kind as SharedStyleKind]\n if (reference in changes || !node[reference]) continue\n if (!fields.some((field) => field in changes)) continue\n if (kind === 'stroke' && changes.strokes && restylesStrokeLines(node.strokes, changes.strokes))\n continue\n next[reference] = null\n }\n // A face named for another family, weight, or slant names none; the new one takes its own name.\n const changesFace = FACE_KEYS.some((key) => key in changes && changes[key] !== node[key])\n if (changesFace && !('fontStyle' in changes) && node.fontStyle) {\n next.fontStyle = null\n }\n return next\n}\n\n/** The node of the shared style with `id`, when the document holds one of `type`. */\nexport function findSharedStyle(\n graph: SceneGraph,\n id: string,\n type: SharedStyleType\n): SceneNode | undefined {\n if (!id) return undefined\n for (const node of graph.getAllNodes()) {\n if (node.sharedStyleType === type && node.source.id === id) return node\n }\n return undefined\n}\n\n/** A new local style's ID, in the shape Figma gives one: `S:` and a 40-digit hex key. */\nexport function newSharedStyleId(): string {\n return `S:${randomHex(20)},`\n}\n\nconst INTERNAL_CANVAS_NAME = 'Internal Only Canvas'\n\n/** The page that holds the document's styles out of view, as a `.fig` file keeps them; made once. */\nexport function internalCanvas(graph: SceneGraph): SceneNode {\n const existing = graph.getPages(true).find((page) => page.internalOnly)\n if (existing) return existing\n return graph.createNode('CANVAS', graph.rootId, {\n name: INTERNAL_CANVAS_NAME,\n width: 0,\n height: 0,\n internalOnly: true\n })\n}\n\n/**\n * A paint style's paints as a layer's strokes: every paint, each keeping the weight, alignment,\n * and line style of the stroke it replaces, or of the layer's first; no paints clear the strokes\n * (Figma desktop 126).\n */\nfunction strokesFromStyle(target: SceneNode, style: SceneNode): SceneNode['strokes'] {\n // Without strokes, the layer keeps the weight and alignment a new stroke takes.\n const fallback: Stroke = target.strokes[0] ?? {\n type: 'SOLID',\n color: BLACK,\n opacity: 1,\n visible: true,\n ...newStrokeGeometry(target)\n }\n return copyFills(style.fills).map((paint, index) => ({\n ...paint,\n ...pick(target.strokes[index] ?? fallback, STROKE_LINE_KEYS)\n }))\n}\n\n/**\n * What a layer takes from a shared style it starts using: the style's fields and the reference\n * that keeps it attached. Without the style's node, the reference alone.\n */\nexport function sharedStylePatch(\n kind: SharedStyleKind,\n target: SceneNode,\n styleId: string,\n style: SceneNode | null\n): Partial<SceneNode> {\n const patch: Partial<SceneNode> = { [STYLE_REF_KEYS[kind]]: styleId }\n if (!style) return patch\n if (kind === 'fill') patch.fills = copyFills(style.fills)\n else if (kind === 'stroke') patch.strokes = strokesFromStyle(target, style)\n else if (kind === 'effect') patch.effects = copyEffects(style.effects)\n else if (kind === 'grid') patch.layoutGrids = copyLayoutGrids(style.layoutGrids)\n else Object.assign(patch, structuredClone(pick(style, SHARED_STYLE_FIELDS.text)))\n return patch\n}\n\n/** A layer leaving a shared style: it keeps the style's values, without the reference. */\nexport function sharedStyleDetachPatch(kind: SharedStyleKind): Partial<SceneNode> {\n return { [STYLE_REF_KEYS[kind]]: null }\n}\n"],"mappings":";;;;;;;;AAmBA,MAAM,iBAAiB;CACrB,MAAM;CACN,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,MAAM;AACR;;AAGA,MAAa,oBAAoB,OAAO,OAAO,cAAc;;AAG7D,SAAgB,mBAAmB,MAA2B;CAC5D,OAAO,QAAQ;EACb,GAAG,kBAAkB,KAAK,UAAU,KAAK,MAAM;EAC/C,GAAG,mBAAmB,KAAK,SAAS,CAAC,CAAC;EACtC,IAAI,KAAK,eAAe,WAAW,CAAC,EAAA,CAAG,KAAK,WAAW,OAAO,WAAW;CAC3E,CAAC;AACH;AAEA,MAAM,cAAc;CAClB,MAAM;CACN,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,MAAM;AACR;;AAGA,MAAa,2BAA2B;CACtC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;;AAMA,MAAM,8BAA8B;CAClC;CACA;CACA;AACF;;AAGA,MAAa,sBAAsB;CACjC,MAAM,CAAC,OAAO;CACd,QAAQ,CAAC,SAAS;CAClB,MAAM,CAAC,GAAG,0BAA0B,GAAG,2BAA2B;CAClE,QAAQ,CAAC,SAAS;CAClB,MAAM,CAAC,aAAa;AACtB;;AAGA,MAAa,YAA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AE;AACJ;;;;;AAMA,SAAgB,iBACd,MACA,QACA,SACA,OACoB;CACpB,MAAM,QAA4B,GAAG,eAAe,QAAQ,QAAQ;CACpE,IAAI,CAAC,OAAO,OAAO;CACnB,IAAI,SAAS,QAAQ,MAAM,QAAQ,UAAU,MAAM,KAAK;MACnD,IAAI,SAAS,UAAU,MAAM,UAAU,iBAAiB,QAAQ,KAAK;MACrE,IAAI,SAAS,UAAU,MAAM,UAAU,YAAY,MAAM,OAAO;MAChE,IAAI,SAAS,QAAQ,MAAM,cAAc,gBAAgB,MAAM,WAAW;MAC1E,OAAO,OAAO,OAAO,gBAAgB,KAAK,OAAO,oBAAoB,IAAI,CAAC,CAAC;CAChF,OAAO;AACT;;AAGA,SAAgB,uBAAuB,MAA2C;CAChF,OAAO,GAAG,eAAe,QAAQ,KAAK;AACxC"}
@@ -0,0 +1,39 @@
1
+ import { SceneNode, Stroke } from "./types.js";
2
+ //#region src/strokes.d.ts
3
+ /** What a stroke draws along beside its paint. */
4
+ declare const STROKE_LINE_KEYS: readonly ["weight", "align", "cap", "join", "dashPattern"];
5
+ type StrokeLineKey = (typeof STROKE_LINE_KEYS)[number];
6
+ /**
7
+ * The layer field that keeps each line value. Figma keeps one of each per layer; a layer here
8
+ * also keeps them on each stroke, which the renderer draws.
9
+ */
10
+ declare const STROKE_LINE_FIELDS: {
11
+ readonly weight: "strokeWeight";
12
+ readonly align: "strokeAlign";
13
+ readonly cap: "strokeCap";
14
+ readonly join: "strokeJoin";
15
+ readonly dashPattern: "dashPattern";
16
+ };
17
+ type StrokeLines = Partial<Pick<Stroke, StrokeLineKey>>;
18
+ /** The value a stroke draws with; a stroke without a dash draws solid. */
19
+ declare function strokeLine(stroke: Stroke, key: StrokeLineKey): Stroke[StrokeLineKey];
20
+ /** `strokes` with each line field `values` names, as a layer field set alone gives every stroke. */
21
+ declare function withStrokeLines(strokes: readonly Stroke[], values: StrokeLines): Stroke[];
22
+ /**
23
+ * What else `changes` sets so a layer's line fields and its strokes agree: new strokes set the
24
+ * layer's fields to the first stroke's, and a layer field set without strokes goes to every
25
+ * stroke. Strokes win when `changes` sets both, so restoring strokes restores what they draw.
26
+ * Only fields whose value changes are returned, so an instance overrides no more than an edit
27
+ * changed. A layer without strokes keeps its fields for the next stroke (`newStrokeGeometry`).
28
+ */
29
+ declare function strokeLineChanges(node: SceneNode, changes: Partial<SceneNode>): Partial<SceneNode>;
30
+ /**
31
+ * Whether the strokes `changes` sets differ from `node`'s by more than the layer fields the same
32
+ * write sets: a paint, a stroke added or removed, or a stroke's line that no layer field records.
33
+ * Figma overrides a weight or dash set on an instance layer alone, so a component's later paint
34
+ * change still reaches the copy (live).
35
+ */
36
+ declare function changesMoreThanStrokeLines(node: SceneNode, changes: Partial<SceneNode>): boolean;
37
+ //#endregion
38
+ export { STROKE_LINE_FIELDS, STROKE_LINE_KEYS, StrokeLines, changesMoreThanStrokeLines, strokeLine, strokeLineChanges, withStrokeLines };
39
+ //# sourceMappingURL=strokes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strokes.d.ts","names":[],"sources":["../src/strokes.ts"],"mappings":";;;cAMa;KAQR,wBAAwB;;;;;cAMhB;;;;;;;KAQD,cAAc,QAAQ,KAAK,QAAQ;;iBAG/B,WAAW,QAAQ,QAAQ,KAAK,gBAAgB,OAAO;;iBAKvD,gBAAgB,kBAAkB,UAAU,QAAQ,cAAc;;;;;;;;iBAkBlE,kBACd,MAAM,WACN,SAAS,QAAQ,aAChB,QAAQ;;;;;;;iBA6CK,2BAA2B,MAAM,WAAW,SAAS,QAAQ"}
@@ -0,0 +1,104 @@
1
+ import { copyStroke } from "./copy.js";
2
+ import { isEqual } from "es-toolkit";
3
+ //#region src/strokes.ts
4
+ /** What a stroke draws along beside its paint. */
5
+ const STROKE_LINE_KEYS = [
6
+ "weight",
7
+ "align",
8
+ "cap",
9
+ "join",
10
+ "dashPattern"
11
+ ];
12
+ /**
13
+ * The layer field that keeps each line value. Figma keeps one of each per layer; a layer here
14
+ * also keeps them on each stroke, which the renderer draws.
15
+ */
16
+ const STROKE_LINE_FIELDS = {
17
+ weight: "strokeWeight",
18
+ align: "strokeAlign",
19
+ cap: "strokeCap",
20
+ join: "strokeJoin",
21
+ dashPattern: "dashPattern"
22
+ };
23
+ /** The value a stroke draws with; a stroke without a dash draws solid. */
24
+ function strokeLine(stroke, key) {
25
+ return key === "dashPattern" ? stroke.dashPattern ?? [] : stroke[key];
26
+ }
27
+ /** `strokes` with each line field `values` names, as a layer field set alone gives every stroke. */
28
+ function withStrokeLines(strokes, values) {
29
+ return strokes.map((stroke) => {
30
+ const next = copyStroke(stroke);
31
+ for (const key of STROKE_LINE_KEYS) {
32
+ const value = values[key];
33
+ if (value !== void 0) Reflect.set(next, key, structuredClone(value));
34
+ }
35
+ return next;
36
+ });
37
+ }
38
+ /**
39
+ * What else `changes` sets so a layer's line fields and its strokes agree: new strokes set the
40
+ * layer's fields to the first stroke's, and a layer field set without strokes goes to every
41
+ * stroke. Strokes win when `changes` sets both, so restoring strokes restores what they draw.
42
+ * Only fields whose value changes are returned, so an instance overrides no more than an edit
43
+ * changed. A layer without strokes keeps its fields for the next stroke (`newStrokeGeometry`).
44
+ */
45
+ function strokeLineChanges(node, changes) {
46
+ if (changes.strokes) {
47
+ const first = changes.strokes.at(0);
48
+ if (!first) return {};
49
+ const derived = {};
50
+ for (const key of STROKE_LINE_KEYS) {
51
+ const field = STROKE_LINE_FIELDS[key];
52
+ const current = field in changes ? changes[field] : node[field];
53
+ const value = strokeLine(first, key) ?? current;
54
+ if (!isEqual(current, value)) Reflect.set(derived, field, structuredClone(value));
55
+ }
56
+ const cap = derived.strokeCap ?? changes.strokeCap ?? node.strokeCap;
57
+ const join = derived.strokeJoin ?? changes.strokeJoin ?? node.strokeJoin;
58
+ if (changes.strokes.some((stroke) => stroke.cap === void 0 || stroke.join === void 0)) derived.strokes = changes.strokes.map((stroke) => ({
59
+ ...copyStroke(stroke),
60
+ cap: stroke.cap ?? cap,
61
+ join: stroke.join ?? join
62
+ }));
63
+ return derived;
64
+ }
65
+ if (node.strokes.length === 0) return {};
66
+ const values = {};
67
+ let set = false;
68
+ for (const key of STROKE_LINE_KEYS) {
69
+ const value = changes[STROKE_LINE_FIELDS[key]];
70
+ if (value === void 0) continue;
71
+ Reflect.set(values, key, value);
72
+ set = true;
73
+ }
74
+ if (!set) return {};
75
+ const strokes = withStrokeLines(node.strokes, values);
76
+ return isEqual(strokes, node.strokes) ? {} : { strokes };
77
+ }
78
+ /**
79
+ * Whether the strokes `changes` sets differ from `node`'s by more than the layer fields the same
80
+ * write sets: a paint, a stroke added or removed, or a stroke's line that no layer field records.
81
+ * Figma overrides a weight or dash set on an instance layer alone, so a component's later paint
82
+ * change still reaches the copy (live).
83
+ */
84
+ function changesMoreThanStrokeLines(node, changes) {
85
+ const next = changes.strokes;
86
+ if (!next) return false;
87
+ if (next.length !== node.strokes.length) return true;
88
+ const lines = {};
89
+ for (const key of STROKE_LINE_KEYS) {
90
+ const value = changes[STROKE_LINE_FIELDS[key]];
91
+ if (value !== void 0) Reflect.set(lines, key, value);
92
+ }
93
+ const drawn = (stroke) => ({
94
+ ...stroke,
95
+ cap: stroke.cap ?? node.strokeCap,
96
+ join: stroke.join ?? node.strokeJoin,
97
+ dashPattern: stroke.dashPattern ?? []
98
+ });
99
+ return !isEqual(withStrokeLines(node.strokes, lines).map(drawn), next.map(drawn));
100
+ }
101
+ //#endregion
102
+ export { STROKE_LINE_FIELDS, STROKE_LINE_KEYS, changesMoreThanStrokeLines, strokeLine, strokeLineChanges, withStrokeLines };
103
+
104
+ //# sourceMappingURL=strokes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strokes.js","names":[],"sources":["../src/strokes.ts"],"sourcesContent":["import { isEqual } from 'es-toolkit'\n\nimport { copyStroke } from './copy'\nimport type { SceneNode, Stroke } from './types'\n\n/** What a stroke draws along beside its paint. */\nexport const STROKE_LINE_KEYS = [\n 'weight',\n 'align',\n 'cap',\n 'join',\n 'dashPattern'\n] as const satisfies readonly (keyof Stroke)[]\n\ntype StrokeLineKey = (typeof STROKE_LINE_KEYS)[number]\n\n/**\n * The layer field that keeps each line value. Figma keeps one of each per layer; a layer here\n * also keeps them on each stroke, which the renderer draws.\n */\nexport const STROKE_LINE_FIELDS = {\n weight: 'strokeWeight',\n align: 'strokeAlign',\n cap: 'strokeCap',\n join: 'strokeJoin',\n dashPattern: 'dashPattern'\n} as const satisfies Record<StrokeLineKey, keyof SceneNode>\n\nexport type StrokeLines = Partial<Pick<Stroke, StrokeLineKey>>\n\n/** The value a stroke draws with; a stroke without a dash draws solid. */\nexport function strokeLine(stroke: Stroke, key: StrokeLineKey): Stroke[StrokeLineKey] {\n return key === 'dashPattern' ? (stroke.dashPattern ?? []) : stroke[key]\n}\n\n/** `strokes` with each line field `values` names, as a layer field set alone gives every stroke. */\nexport function withStrokeLines(strokes: readonly Stroke[], values: StrokeLines): Stroke[] {\n return strokes.map((stroke) => {\n const next = copyStroke(stroke)\n for (const key of STROKE_LINE_KEYS) {\n const value = values[key]\n if (value !== undefined) Reflect.set(next, key, structuredClone(value))\n }\n return next\n })\n}\n\n/**\n * What else `changes` sets so a layer's line fields and its strokes agree: new strokes set the\n * layer's fields to the first stroke's, and a layer field set without strokes goes to every\n * stroke. Strokes win when `changes` sets both, so restoring strokes restores what they draw.\n * Only fields whose value changes are returned, so an instance overrides no more than an edit\n * changed. A layer without strokes keeps its fields for the next stroke (`newStrokeGeometry`).\n */\nexport function strokeLineChanges(\n node: SceneNode,\n changes: Partial<SceneNode>\n): Partial<SceneNode> {\n if (changes.strokes) {\n const first = changes.strokes.at(0)\n if (!first) return {}\n const derived: Partial<SceneNode> = {}\n for (const key of STROKE_LINE_KEYS) {\n const field = STROKE_LINE_FIELDS[key]\n const current = field in changes ? changes[field] : node[field]\n // A stroke without its own cap or join draws with the layer's.\n const value = strokeLine(first, key) ?? current\n if (!isEqual(current, value)) Reflect.set(derived, field, structuredClone(value))\n }\n // Strokes without their own cap or join take the layer's, so every stroke says what it draws.\n const cap = derived.strokeCap ?? changes.strokeCap ?? node.strokeCap\n const join = derived.strokeJoin ?? changes.strokeJoin ?? node.strokeJoin\n if (changes.strokes.some((stroke) => stroke.cap === undefined || stroke.join === undefined)) {\n derived.strokes = changes.strokes.map((stroke) => ({\n ...copyStroke(stroke),\n cap: stroke.cap ?? cap,\n join: stroke.join ?? join\n }))\n }\n return derived\n }\n if (node.strokes.length === 0) return {}\n const values: StrokeLines = {}\n let set = false\n for (const key of STROKE_LINE_KEYS) {\n const value = changes[STROKE_LINE_FIELDS[key]]\n if (value === undefined) continue\n Reflect.set(values, key, value)\n set = true\n }\n // Most writes, such as a move or a layout pass, set no line field.\n if (!set) return {}\n const strokes = withStrokeLines(node.strokes, values)\n return isEqual(strokes, node.strokes) ? {} : { strokes }\n}\n\n/**\n * Whether the strokes `changes` sets differ from `node`'s by more than the layer fields the same\n * write sets: a paint, a stroke added or removed, or a stroke's line that no layer field records.\n * Figma overrides a weight or dash set on an instance layer alone, so a component's later paint\n * change still reaches the copy (live).\n */\nexport function changesMoreThanStrokeLines(node: SceneNode, changes: Partial<SceneNode>): boolean {\n const next = changes.strokes\n if (!next) return false\n if (next.length !== node.strokes.length) return true\n const lines: StrokeLines = {}\n for (const key of STROKE_LINE_KEYS) {\n const value = changes[STROKE_LINE_FIELDS[key]]\n if (value !== undefined) Reflect.set(lines, key, value)\n }\n // Strokes written without a cap or join take the layer's, as `strokeLineChanges` gives them,\n // and a stroke without a dash draws as solid as one with an empty dash.\n const drawn = (stroke: Stroke): Stroke => ({\n ...stroke,\n cap: stroke.cap ?? node.strokeCap,\n join: stroke.join ?? node.strokeJoin,\n dashPattern: stroke.dashPattern ?? []\n })\n return !isEqual(withStrokeLines(node.strokes, lines).map(drawn), next.map(drawn))\n}\n"],"mappings":";;;;AAMA,MAAa,mBAAmB;CAC9B;CACA;CACA;CACA;CACA;AACF;;;;;AAQA,MAAa,qBAAqB;CAChC,QAAQ;CACR,OAAO;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AC;CAC7B,IAAI,MAAM;CACV,KAAK,MAAM,OAAO,kBAAkB;EAClC,MAAM,QAAQ,QAAQ,mBAAmB;EACzC,IAAI,UAAU,KAAA,GAAW;EACzB,QAAQ,IAAI,QAAQ,KAAK,KAAK;EAC9B,MAAM;CACR;CAEA,IAAI,CAAC,KAAK,OAAO,CAAC;CAClB,MAAM,UAAU,gBAAgB,KAAK,SAAS,MAAM;CACpD,OAAO,QAAQ,SAAS,KAAK,OAAO,IAAI,CAAC,IAAI,EAAE,QAAQ;AACzD;;;;;;;AAQA,SAAgB,2BAA2B,MAAiB,SAAsC;CAChG,MAAM,OAAO,QAAQ;CACrB,IAAI,CAAC,MAAM,OAAO;CAClB,IAAI,KAAK,WAAW,KAAK,QAAQ,QAAQ,OAAO;CAChD,MAAM,QAAqB,CAAC;CAC5B,KAAK,MAAM,OAAO,kBAAkB;EAClC,MAAM,QAAQ,QAAQ,mBAAmB;EACzC,IAAI,UAAU,KAAA,GAAW,QAAQ,IAAI,OAAO,KAAK,KAAK;CACxD;CAGA,MAAM,SAAS,YAA4B;EACzC,GAAG;EACH,KAAK,OAAO,OAAO,KAAK;EACxB,MAAM,OAAO,QAAQ,KAAK;EAC1B,aAAa,OAAO,eAAe,CAAC;CACtC;CACA,OAAO,CAAC,QAAQ,gBAAgB,KAAK,SAAS,KAAK,CAAC,CAAC,IAAI,KAAK,GAAG,KAAK,IAAI,KAAK,CAAC;AAClF"}
@@ -0,0 +1,15 @@
1
+ import { SceneNode, TextAutoResize } from "../types.js";
2
+ //#region src/text/auto-resize.d.ts
3
+ /** A text's resizing as Figma's plugin API names it, where `TRUNCATE` is a fixed box that truncates. */
4
+ type FigmaTextAutoResize = TextAutoResize | 'TRUNCATE';
5
+ type TextResizeFields = Pick<SceneNode, 'textAutoResize' | 'textTruncation' | 'maxLines'>;
6
+ /** The resizing Figma's plugin API reads for a text: a fixed box that truncates reads `TRUNCATE`. */
7
+ declare function figmaTextAutoResize(node: Pick<SceneNode, 'textAutoResize' | 'textTruncation'>): FigmaTextAutoResize;
8
+ /**
9
+ * The fields setting a resizing writes, as Figma's setter does: `TRUNCATE` fixes the box and
10
+ * truncates its end with no line limit, and any other mode stops truncating.
11
+ */
12
+ declare function textAutoResizeFields(mode: FigmaTextAutoResize): Partial<TextResizeFields>;
13
+ //#endregion
14
+ export { FigmaTextAutoResize, figmaTextAutoResize, textAutoResizeFields };
15
+ //# sourceMappingURL=auto-resize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-resize.d.ts","names":[],"sources":["../../src/text/auto-resize.ts"],"mappings":";;;KAGY,sBAAsB;KAE7B,mBAAmB,KAAK;;iBAGb,oBACd,MAAM,KAAK,kDACV;;;;;iBAUa,qBAAqB,MAAM,sBAAsB,QAAQ"}
@@ -0,0 +1,23 @@
1
+ //#region src/text/auto-resize.ts
2
+ /** The resizing Figma's plugin API reads for a text: a fixed box that truncates reads `TRUNCATE`. */
3
+ function figmaTextAutoResize(node) {
4
+ return node.textAutoResize === "NONE" && node.textTruncation === "ENDING" ? "TRUNCATE" : node.textAutoResize;
5
+ }
6
+ /**
7
+ * The fields setting a resizing writes, as Figma's setter does: `TRUNCATE` fixes the box and
8
+ * truncates its end with no line limit, and any other mode stops truncating.
9
+ */
10
+ function textAutoResizeFields(mode) {
11
+ return mode === "TRUNCATE" ? {
12
+ textAutoResize: "NONE",
13
+ textTruncation: "ENDING",
14
+ maxLines: null
15
+ } : {
16
+ textAutoResize: mode,
17
+ textTruncation: "DISABLED"
18
+ };
19
+ }
20
+ //#endregion
21
+ export { figmaTextAutoResize, textAutoResizeFields };
22
+
23
+ //# sourceMappingURL=auto-resize.js.map