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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (390) hide show
  1. package/dist/arrow-caps.d.ts.map +1 -1
  2. package/dist/arrow-caps.js +2 -5
  3. package/dist/arrow-caps.js.map +1 -1
  4. package/dist/asset-keys.d.ts +11 -1
  5. package/dist/asset-keys.d.ts.map +1 -1
  6. package/dist/asset-keys.js +16 -1
  7. package/dist/asset-keys.js.map +1 -1
  8. package/dist/behaviours/index.d.ts +2 -2
  9. package/dist/behaviours/index.js +2 -2
  10. package/dist/behaviours/kinds.d.ts +11 -5
  11. package/dist/behaviours/kinds.d.ts.map +1 -1
  12. package/dist/behaviours/kinds.js +76 -75
  13. package/dist/behaviours/kinds.js.map +1 -1
  14. package/dist/behaviours/layers.d.ts.map +1 -1
  15. package/dist/behaviours/layers.js +3 -2
  16. package/dist/behaviours/layers.js.map +1 -1
  17. package/dist/behaviours/model.d.ts +7 -4
  18. package/dist/behaviours/model.d.ts.map +1 -1
  19. package/dist/behaviours/model.js +16 -9
  20. package/dist/behaviours/model.js.map +1 -1
  21. package/dist/behaviours/schema.d.ts +118 -4
  22. package/dist/behaviours/schema.d.ts.map +1 -1
  23. package/dist/behaviours/schema.js +18 -3
  24. package/dist/behaviours/schema.js.map +1 -1
  25. package/dist/behaviours/spec.d.ts.map +1 -1
  26. package/dist/behaviours/spec.js +3 -2
  27. package/dist/behaviours/spec.js.map +1 -1
  28. package/dist/bindings.js +9 -1
  29. package/dist/bindings.js.map +1 -1
  30. package/dist/checkpoint.d.ts +31 -2
  31. package/dist/checkpoint.d.ts.map +1 -1
  32. package/dist/checkpoint.js +211 -10
  33. package/dist/checkpoint.js.map +1 -1
  34. package/dist/components/index.d.ts +3 -3
  35. package/dist/components/index.js +3 -3
  36. package/dist/components/properties.d.ts +18 -1
  37. package/dist/components/properties.d.ts.map +1 -1
  38. package/dist/components/properties.js +66 -3
  39. package/dist/components/properties.js.map +1 -1
  40. package/dist/components/variants.d.ts +16 -10
  41. package/dist/components/variants.d.ts.map +1 -1
  42. package/dist/components/variants.js +39 -13
  43. package/dist/components/variants.js.map +1 -1
  44. package/dist/connectors/hit.d.ts.map +1 -1
  45. package/dist/connectors/hit.js +1 -7
  46. package/dist/connectors/hit.js.map +1 -1
  47. package/dist/connectors/pieces.d.ts.map +1 -1
  48. package/dist/connectors/pieces.js +2 -5
  49. package/dist/connectors/pieces.js.map +1 -1
  50. package/dist/constants.d.ts +6 -1
  51. package/dist/constants.d.ts.map +1 -1
  52. package/dist/constants.js +17 -1
  53. package/dist/constants.js.map +1 -1
  54. package/dist/constraints.d.ts +25 -0
  55. package/dist/constraints.d.ts.map +1 -0
  56. package/dist/constraints.js +51 -0
  57. package/dist/constraints.js.map +1 -0
  58. package/dist/coordinate.d.ts +64 -1
  59. package/dist/coordinate.d.ts.map +1 -1
  60. package/dist/coordinate.js +217 -1
  61. package/dist/coordinate.js.map +1 -1
  62. package/dist/copy.d.ts.map +1 -1
  63. package/dist/copy.js +17 -16
  64. package/dist/copy.js.map +1 -1
  65. package/dist/css/blend.d.ts +11 -0
  66. package/dist/css/blend.d.ts.map +1 -0
  67. package/dist/css/blend.js +30 -0
  68. package/dist/css/blend.js.map +1 -0
  69. package/dist/css/index.d.ts +4 -2
  70. package/dist/css/index.js +4 -2
  71. package/dist/css/text.d.ts +28 -1
  72. package/dist/css/text.d.ts.map +1 -1
  73. package/dist/css/text.js +84 -17
  74. package/dist/css/text.js.map +1 -1
  75. package/dist/css/transform.d.ts +34 -0
  76. package/dist/css/transform.d.ts.map +1 -0
  77. package/dist/css/transform.js +320 -0
  78. package/dist/css/transform.js.map +1 -0
  79. package/dist/css/values.d.ts.map +1 -1
  80. package/dist/css/values.js +11 -1
  81. package/dist/css/values.js.map +1 -1
  82. package/dist/cubic.d.ts +7 -0
  83. package/dist/cubic.d.ts.map +1 -0
  84. package/dist/cubic.js +17 -0
  85. package/dist/cubic.js.map +1 -0
  86. package/dist/export-format.d.ts +14 -1
  87. package/dist/export-format.d.ts.map +1 -1
  88. package/dist/export-format.js +26 -1
  89. package/dist/export-format.js.map +1 -1
  90. package/dist/fields/text.d.ts +2 -2
  91. package/dist/fields/text.d.ts.map +1 -1
  92. package/dist/fields/text.js +2 -1
  93. package/dist/fields/text.js.map +1 -1
  94. package/dist/figjam/index.d.ts +2 -2
  95. package/dist/figjam/index.js +2 -2
  96. package/dist/figjam/palette.d.ts +13 -2
  97. package/dist/figjam/palette.d.ts.map +1 -1
  98. package/dist/figjam/palette.js +30 -2
  99. package/dist/figjam/palette.js.map +1 -1
  100. package/dist/generated/index.d.ts +2 -2
  101. package/dist/generated/index.js +2 -2
  102. package/dist/generated/shape-with-text.d.ts +7 -2
  103. package/dist/generated/shape-with-text.d.ts.map +1 -1
  104. package/dist/generated/shape-with-text.js +20 -3
  105. package/dist/generated/shape-with-text.js.map +1 -1
  106. package/dist/geometry.d.ts +37 -46
  107. package/dist/geometry.d.ts.map +1 -1
  108. package/dist/geometry.js +80 -137
  109. package/dist/geometry.js.map +1 -1
  110. package/dist/graph.d.ts +49 -12
  111. package/dist/graph.d.ts.map +1 -1
  112. package/dist/graph.js +125 -27
  113. package/dist/graph.js.map +1 -1
  114. package/dist/group-bounds.d.ts +14 -4
  115. package/dist/group-bounds.d.ts.map +1 -1
  116. package/dist/group-bounds.js +76 -11
  117. package/dist/group-bounds.js.map +1 -1
  118. package/dist/guides.d.ts +3 -1
  119. package/dist/guides.d.ts.map +1 -1
  120. package/dist/guides.js +5 -1
  121. package/dist/guides.js.map +1 -1
  122. package/dist/hit-test.d.ts +5 -0
  123. package/dist/hit-test.d.ts.map +1 -1
  124. package/dist/hit-test.js +57 -38
  125. package/dist/hit-test.js.map +1 -1
  126. package/dist/images.d.ts +27 -3
  127. package/dist/images.d.ts.map +1 -1
  128. package/dist/images.js +57 -3
  129. package/dist/images.js.map +1 -1
  130. package/dist/index.d.ts +2 -2
  131. package/dist/instances/fields.d.ts +2 -2
  132. package/dist/instances/fields.d.ts.map +1 -1
  133. package/dist/instances/fields.js +8 -0
  134. package/dist/instances/fields.js.map +1 -1
  135. package/dist/instances/index.d.ts +1 -3
  136. package/dist/instances/index.d.ts.map +1 -1
  137. package/dist/instances/index.js +39 -30
  138. package/dist/instances/index.js.map +1 -1
  139. package/dist/instances/kept-overrides.js +204 -0
  140. package/dist/instances/kept-overrides.js.map +1 -0
  141. package/dist/instances/main-component.d.ts +3 -1
  142. package/dist/instances/main-component.d.ts.map +1 -1
  143. package/dist/instances/main-component.js +10 -2
  144. package/dist/instances/main-component.js.map +1 -1
  145. package/dist/instances/overrides.d.ts +4 -1
  146. package/dist/instances/overrides.d.ts.map +1 -1
  147. package/dist/instances/overrides.js +18 -2
  148. package/dist/instances/overrides.js.map +1 -1
  149. package/dist/instances/placement.js +55 -12
  150. package/dist/instances/placement.js.map +1 -1
  151. package/dist/instances/swap.js +291 -0
  152. package/dist/instances/swap.js.map +1 -0
  153. package/dist/instances/sync.js +112 -39
  154. package/dist/instances/sync.js.map +1 -1
  155. package/dist/node-defaults.d.ts.map +1 -1
  156. package/dist/node-defaults.js +2 -1
  157. package/dist/node-defaults.js.map +1 -1
  158. package/dist/order-keys.d.ts +3 -1
  159. package/dist/order-keys.d.ts.map +1 -1
  160. package/dist/order-keys.js +6 -1
  161. package/dist/order-keys.js.map +1 -1
  162. package/dist/outline/flatten.d.ts +15 -0
  163. package/dist/outline/flatten.d.ts.map +1 -0
  164. package/dist/outline/flatten.js +189 -0
  165. package/dist/outline/flatten.js.map +1 -0
  166. package/dist/outline/heads.js +34 -0
  167. package/dist/outline/heads.js.map +1 -0
  168. package/dist/outline/hit.d.ts +21 -0
  169. package/dist/outline/hit.d.ts.map +1 -0
  170. package/dist/outline/hit.js +276 -0
  171. package/dist/outline/hit.js.map +1 -0
  172. package/dist/outline/index.d.ts +5 -0
  173. package/dist/outline/index.js +5 -0
  174. package/dist/outline/point.d.ts +16 -0
  175. package/dist/outline/point.d.ts.map +1 -0
  176. package/dist/outline/point.js +42 -0
  177. package/dist/outline/point.js.map +1 -0
  178. package/dist/outline/shapes.d.ts +35 -0
  179. package/dist/outline/shapes.d.ts.map +1 -0
  180. package/dist/outline/shapes.js +181 -0
  181. package/dist/outline/shapes.js.map +1 -0
  182. package/dist/plugin-data/fields.d.ts +4 -1
  183. package/dist/plugin-data/fields.d.ts.map +1 -1
  184. package/dist/plugin-data/fields.js +21 -4
  185. package/dist/plugin-data/fields.js.map +1 -1
  186. package/dist/resize.d.ts +22 -11
  187. package/dist/resize.d.ts.map +1 -1
  188. package/dist/resize.js +227 -93
  189. package/dist/resize.js.map +1 -1
  190. package/dist/scaling/index.d.ts +3 -2
  191. package/dist/scaling/index.js +3 -2
  192. package/dist/scaling/node.d.ts.map +1 -1
  193. package/dist/scaling/node.js +5 -1
  194. package/dist/scaling/node.js.map +1 -1
  195. package/dist/scaling/snapshot.d.ts +25 -0
  196. package/dist/scaling/snapshot.d.ts.map +1 -0
  197. package/dist/scaling/snapshot.js +39 -0
  198. package/dist/scaling/snapshot.js.map +1 -0
  199. package/dist/scaling/tree.d.ts +24 -2
  200. package/dist/scaling/tree.d.ts.map +1 -1
  201. package/dist/scaling/tree.js +38 -15
  202. package/dist/scaling/tree.js.map +1 -1
  203. package/dist/shaders/model.d.ts.map +1 -1
  204. package/dist/shaders/model.js +2 -1
  205. package/dist/shaders/model.js.map +1 -1
  206. package/dist/shared-styles.d.ts +16 -1
  207. package/dist/shared-styles.d.ts.map +1 -1
  208. package/dist/shared-styles.js +69 -2
  209. package/dist/shared-styles.js.map +1 -1
  210. package/dist/slots/authoring.d.ts +3 -2
  211. package/dist/slots/authoring.d.ts.map +1 -1
  212. package/dist/slots/authoring.js +5 -3
  213. package/dist/slots/authoring.js.map +1 -1
  214. package/dist/slots/content.d.ts.map +1 -1
  215. package/dist/slots/content.js +6 -1
  216. package/dist/slots/content.js.map +1 -1
  217. package/dist/slots/frames.d.ts +2 -0
  218. package/dist/slots/frames.d.ts.map +1 -1
  219. package/dist/slots/frames.js +7 -2
  220. package/dist/slots/frames.js.map +1 -1
  221. package/dist/text/direction.d.ts +3 -1
  222. package/dist/text/direction.d.ts.map +1 -1
  223. package/dist/text/direction.js +5 -1
  224. package/dist/text/direction.js.map +1 -1
  225. package/dist/text/index.d.ts +2 -2
  226. package/dist/text/index.js +3 -3
  227. package/dist/text/lengths.d.ts +6 -1
  228. package/dist/text/lengths.d.ts.map +1 -1
  229. package/dist/text/lengths.js +10 -1
  230. package/dist/text/lengths.js.map +1 -1
  231. package/dist/text/overflow.d.ts +2 -2
  232. package/dist/text/overflow.js.map +1 -1
  233. package/dist/text/picture.d.ts +1 -2
  234. package/dist/text/picture.d.ts.map +1 -1
  235. package/dist/text/picture.js +1 -4
  236. package/dist/text/picture.js.map +1 -1
  237. package/dist/transfer/apply.d.ts.map +1 -1
  238. package/dist/transfer/apply.js +1 -6
  239. package/dist/transfer/apply.js.map +1 -1
  240. package/dist/transfer/copied.d.ts +35 -3
  241. package/dist/transfer/copied.d.ts.map +1 -1
  242. package/dist/transfer/copied.js +44 -13
  243. package/dist/transfer/copied.js.map +1 -1
  244. package/dist/transfer/dependencies.d.ts +38 -0
  245. package/dist/transfer/dependencies.d.ts.map +1 -0
  246. package/dist/transfer/dependencies.js +100 -0
  247. package/dist/transfer/dependencies.js.map +1 -0
  248. package/dist/transfer/history.js +0 -1
  249. package/dist/transfer/history.js.map +1 -1
  250. package/dist/transfer/index.d.ts +5 -3
  251. package/dist/transfer/index.js +5 -3
  252. package/dist/transfer/nodes.d.ts.map +1 -1
  253. package/dist/transfer/nodes.js +16 -19
  254. package/dist/transfer/nodes.js.map +1 -1
  255. package/dist/transfer/plan.d.ts +0 -1
  256. package/dist/transfer/plan.js +3 -5
  257. package/dist/transfer/plan.js.map +1 -1
  258. package/dist/transfer/remove.js +5 -2
  259. package/dist/transfer/remove.js.map +1 -1
  260. package/dist/transfer/resources.d.ts +21 -0
  261. package/dist/transfer/resources.d.ts.map +1 -0
  262. package/dist/transfer/resources.js +38 -0
  263. package/dist/transfer/resources.js.map +1 -0
  264. package/dist/transfer/variables.d.ts +8 -1
  265. package/dist/transfer/variables.d.ts.map +1 -1
  266. package/dist/transfer/variables.js +24 -1
  267. package/dist/transfer/variables.js.map +1 -1
  268. package/dist/types.d.ts +73 -2
  269. package/dist/types.d.ts.map +1 -1
  270. package/dist/types.js.map +1 -1
  271. package/dist/undo.d.ts +28 -3
  272. package/dist/undo.d.ts.map +1 -1
  273. package/dist/undo.js +24 -7
  274. package/dist/undo.js.map +1 -1
  275. package/dist/variables/bindings.d.ts +37 -11
  276. package/dist/variables/bindings.d.ts.map +1 -1
  277. package/dist/variables/bindings.js +122 -74
  278. package/dist/variables/bindings.js.map +1 -1
  279. package/dist/variables/fields.d.ts +19 -1
  280. package/dist/variables/fields.d.ts.map +1 -1
  281. package/dist/variables/fields.js +36 -1
  282. package/dist/variables/fields.js.map +1 -1
  283. package/dist/variables/formula/check.d.ts +1 -1
  284. package/dist/variables/formula/index.d.ts +2 -2
  285. package/dist/variables/formula/index.js +1 -1
  286. package/dist/variables/formula/index.js.map +1 -1
  287. package/dist/variables/formula/print.d.ts +1 -1
  288. package/dist/variables/formula/signatures.js +1 -1
  289. package/dist/variables/index.d.ts +7 -7
  290. package/dist/variables/index.d.ts.map +1 -1
  291. package/dist/variables/index.js +57 -26
  292. package/dist/variables/index.js.map +1 -1
  293. package/dist/variables/paints.d.ts +57 -0
  294. package/dist/variables/paints.d.ts.map +1 -0
  295. package/dist/variables/paints.js +231 -0
  296. package/dist/variables/paints.js.map +1 -0
  297. package/dist/variables/resolution.d.ts +14 -11
  298. package/dist/variables/resolution.d.ts.map +1 -1
  299. package/dist/variables/resolution.js +36 -17
  300. package/dist/variables/resolution.js.map +1 -1
  301. package/dist/variables/subtree.js +17 -0
  302. package/dist/variables/subtree.js.map +1 -0
  303. package/dist/vector-network.d.ts.map +1 -1
  304. package/dist/vector-network.js +2 -8
  305. package/dist/vector-network.js.map +1 -1
  306. package/package.json +14 -1
  307. package/src/arrow-caps.ts +2 -5
  308. package/src/asset-keys.ts +20 -0
  309. package/src/behaviours/kinds.ts +51 -34
  310. package/src/behaviours/layers.ts +4 -2
  311. package/src/behaviours/model.ts +22 -11
  312. package/src/behaviours/schema.ts +20 -4
  313. package/src/behaviours/spec.ts +8 -2
  314. package/src/bindings.ts +11 -2
  315. package/src/checkpoint.ts +319 -17
  316. package/src/components/properties.ts +101 -2
  317. package/src/components/variants.ts +51 -30
  318. package/src/connectors/hit.ts +1 -12
  319. package/src/connectors/pieces.ts +2 -5
  320. package/src/constants.ts +17 -0
  321. package/src/constraints.ts +60 -0
  322. package/src/coordinate.ts +220 -0
  323. package/src/copy.ts +18 -19
  324. package/src/css/blend.ts +27 -0
  325. package/src/css/index.ts +18 -1
  326. package/src/css/text.ts +85 -17
  327. package/src/css/transform.ts +266 -0
  328. package/src/css/values.ts +8 -0
  329. package/src/cubic.ts +14 -0
  330. package/src/export-format.ts +29 -0
  331. package/src/fields/text.ts +3 -1
  332. package/src/figjam/palette.ts +29 -1
  333. package/src/generated/shape-with-text.ts +28 -2
  334. package/src/geometry.ts +97 -206
  335. package/src/graph.ts +155 -45
  336. package/src/group-bounds.ts +84 -17
  337. package/src/guides.ts +5 -0
  338. package/src/hit-test.ts +93 -64
  339. package/src/images.ts +71 -3
  340. package/src/instances/fields.ts +8 -0
  341. package/src/instances/index.ts +62 -31
  342. package/src/instances/kept-overrides.ts +280 -0
  343. package/src/instances/main-component.ts +12 -0
  344. package/src/instances/overrides.ts +27 -1
  345. package/src/instances/placement.ts +63 -10
  346. package/src/instances/swap.ts +456 -0
  347. package/src/instances/sync.ts +176 -50
  348. package/src/node-defaults.ts +3 -1
  349. package/src/order-keys.ts +6 -0
  350. package/src/outline/flatten.ts +190 -0
  351. package/src/outline/heads.ts +52 -0
  352. package/src/outline/hit.ts +369 -0
  353. package/src/outline/index.ts +4 -0
  354. package/src/outline/point.ts +59 -0
  355. package/src/outline/shapes.ts +207 -0
  356. package/src/plugin-data/fields.ts +21 -2
  357. package/src/resize.ts +267 -119
  358. package/src/scaling/index.ts +9 -1
  359. package/src/scaling/node.ts +9 -1
  360. package/src/scaling/snapshot.ts +51 -0
  361. package/src/scaling/tree.ts +63 -19
  362. package/src/shaders/model.ts +2 -3
  363. package/src/shared-styles.ts +90 -1
  364. package/src/slots/authoring.ts +5 -3
  365. package/src/slots/content.ts +6 -1
  366. package/src/slots/frames.ts +9 -7
  367. package/src/text/direction.ts +5 -0
  368. package/src/text/lengths.ts +10 -0
  369. package/src/text/overflow.ts +2 -2
  370. package/src/text/picture.ts +0 -4
  371. package/src/transfer/apply.ts +1 -13
  372. package/src/transfer/copied.ts +83 -15
  373. package/src/transfer/dependencies.ts +126 -0
  374. package/src/transfer/history.ts +0 -6
  375. package/src/transfer/index.ts +20 -2
  376. package/src/transfer/nodes.ts +17 -31
  377. package/src/transfer/plan.ts +3 -11
  378. package/src/transfer/remove.ts +5 -2
  379. package/src/transfer/resources.ts +54 -0
  380. package/src/transfer/variables.ts +34 -0
  381. package/src/types.ts +83 -1
  382. package/src/undo.ts +48 -10
  383. package/src/variables/bindings.ts +152 -88
  384. package/src/variables/fields.ts +46 -1
  385. package/src/variables/formula/index.ts +3 -1
  386. package/src/variables/index.ts +90 -39
  387. package/src/variables/paints.ts +296 -0
  388. package/src/variables/resolution.ts +39 -28
  389. package/src/variables/subtree.ts +15 -0
  390. package/src/vector-network.ts +4 -2
package/src/constants.ts CHANGED
@@ -1,8 +1,25 @@
1
1
  import type { Color } from './primitives'
2
+ import type { GlassEffectParams } from './types'
2
3
 
3
4
  export const BLACK: Color = { r: 0, g: 0, b: 0, a: 1 }
4
5
  export const WHITE: Color = { r: 1, g: 1, b: 1, a: 1 }
5
6
  export const TRANSPARENT: Color = { r: 0, g: 0, b: 0, a: 0 }
7
+ /** The lens of a glass effect whose values are missing: the least it can be, so it bends nothing. */
8
+ export const UNSET_GLASS: Readonly<GlassEffectParams> = {
9
+ refraction: 0,
10
+ depth: 1,
11
+ lightAngle: 0,
12
+ lightIntensity: 0,
13
+ dispersion: 0
14
+ }
15
+ /** The lens a new glass effect starts with when nothing sets its values. */
16
+ export const DEFAULT_GLASS: Readonly<GlassEffectParams> = {
17
+ refraction: 0.5,
18
+ depth: 20,
19
+ lightAngle: -45,
20
+ lightIntensity: 0.5,
21
+ dispersion: 0
22
+ }
6
23
  export const DEFAULT_FONT_FAMILY = 'Inter'
7
24
  export const DEFAULT_STROKE_MITER_LIMIT = 4
8
25
  export const DEFAULT_STROKE_WEIGHT = 1
@@ -0,0 +1,60 @@
1
+ import type { SceneGraph } from './graph'
2
+ import { FITTED_CONTAINER_TYPES } from './node-defaults'
3
+ import type { ConstraintType, SceneNode } from './types'
4
+
5
+ type ConstraintGraph = Pick<SceneGraph, 'getNode' | 'closest'>
6
+
7
+ /** Containers whose children keep their place by their own constraints when they resize. */
8
+ export const CONSTRAINT_PARENT_TYPES: ReadonlySet<SceneNode['type']> = new Set<SceneNode['type']>([
9
+ 'FRAME',
10
+ 'COMPONENT',
11
+ 'COMPONENT_SET',
12
+ 'INSTANCE'
13
+ ])
14
+
15
+ /**
16
+ * The frame, component, or instance whose resize moves `node` by its constraints: its parent, or,
17
+ * for a layer in groups and booleans, the frame around the outermost of them, as in Figma (live,
18
+ * #1835). Null when nothing places it by constraints: on a page or in a section, or in an auto
19
+ * layout flow.
20
+ */
21
+ export function constraintFrame(graph: ConstraintGraph, node: SceneNode): SceneNode | null {
22
+ let layer = node
23
+ const parent = node.parentId
24
+ ? graph.closest(node.parentId, (ancestor) => {
25
+ if (!FITTED_CONTAINER_TYPES.has(ancestor.type)) return true
26
+ layer = ancestor
27
+ return false
28
+ })
29
+ : undefined
30
+ if (!parent || !CONSTRAINT_PARENT_TYPES.has(parent.type)) return null
31
+ if (parent.layoutMode !== 'NONE' && layer.layoutPositioning !== 'ABSOLUTE') return null
32
+ return parent
33
+ }
34
+
35
+ /** Whether `node` has constraints the Design panel offers and the canvas shows. */
36
+ export function isConstraintEligible(graph: ConstraintGraph, node: SceneNode): boolean {
37
+ return constraintFrame(graph, node) !== null
38
+ }
39
+
40
+ /**
41
+ * The layers whose constraints `node` stands for. A group or boolean has none of its own: it shows
42
+ * and sets those of the layers in it, through nested groups and booleans, as in Figma.
43
+ */
44
+ export function constraintLayers(graph: ConstraintGraph, node: SceneNode): SceneNode[] {
45
+ if (!FITTED_CONTAINER_TYPES.has(node.type)) return [node]
46
+ return node.childIds.flatMap((id) => {
47
+ const child = graph.getNode(id)
48
+ return child ? constraintLayers(graph, child) : []
49
+ })
50
+ }
51
+
52
+ /** The constraint the layers share on one axis, or null when they differ or there are none. */
53
+ export function sharedConstraint(
54
+ layers: readonly SceneNode[],
55
+ key: 'horizontalConstraint' | 'verticalConstraint'
56
+ ): ConstraintType | null {
57
+ const first = layers.at(0)?.[key]
58
+ if (first === undefined) return null
59
+ return layers.every((layer) => layer[key] === first) ? first : null
60
+ }
package/src/coordinate.ts CHANGED
@@ -288,3 +288,223 @@ export function getParentToContainerMatrix(
288
288
  }
289
289
  return matrix
290
290
  }
291
+
292
+ /**
293
+ * A selected layer's place in a multi-selection's canvas bounds, taken when a resize starts so
294
+ * every step re-places the layer from where it began rather than from its previewed state.
295
+ */
296
+ export interface SelectionPlacement {
297
+ node: SceneNode
298
+ center: Vector
299
+ /** Draws the layer from its own (0,0)…(width,height) on the canvas. */
300
+ world: Mat3
301
+ /** Draws the layer's parent on the canvas. */
302
+ parentWorld: Mat3
303
+ /** -1 when the layer is drawn mirrored, so its rotation turns the other way on the canvas. */
304
+ direction: 1 | -1
305
+ /** Maps canvas points into the layer's parent's space, or null when it cannot be inverted. */
306
+ parentInverse: Mat3 | null
307
+ }
308
+
309
+ export function selectionPlacement(
310
+ node: SceneNode,
311
+ graph: Pick<SceneGraph, 'getNode' | 'rootId'>
312
+ ): SelectionPlacement {
313
+ const world = getWorldMatrix(node, graph)
314
+ const [cx, cy] = Matrix.mapPoints(world, [node.width / 2, node.height / 2])
315
+ const parent = node.parentId ? graph.getNode(node.parentId) : undefined
316
+ const parentWorld = getParentWorldMatrix(parent, graph)
317
+ return {
318
+ node: { ...node },
319
+ center: { x: cx, y: cy },
320
+ world,
321
+ parentWorld,
322
+ direction: world[0] * world[4] - world[1] * world[3] < 0 ? -1 : 1,
323
+ parentInverse: Matrix.invert(parentWorld)
324
+ }
325
+ }
326
+
327
+ /** The position, in its parent's space, that puts `node`'s center at a canvas point. */
328
+ function positionCenteredAt(node: SceneNode, parentInverse: Mat3 | null, center: Vector): Vector {
329
+ const [px, py] = parentInverse
330
+ ? Matrix.mapPoints(parentInverse, [center.x, center.y])
331
+ : [center.x, center.y]
332
+ // The local matrix translates last, so the layer's center at the origin gives the offset.
333
+ const [qx, qy] = Matrix.mapPoints(getNodeLocalMatrix(node, { x: 0, y: 0 }), [
334
+ node.width / 2,
335
+ node.height / 2
336
+ ])
337
+ return { x: px - qx, y: py - qy }
338
+ }
339
+
340
+ export interface ResizedBoundsOptions {
341
+ /** The layer is locked to its ratio: it scales evenly instead of stretching. */
342
+ keepRatio?: boolean
343
+ /** Adjusts a locked layer's sizes, such as to whole pixels. */
344
+ round?: (size: number) => number
345
+ /**
346
+ * Lands an upright layer's edges on whole pixels from the box's top-left corner, as Figma places
347
+ * the layers of a selection a handle resizes while Snap to pixel grid is on.
348
+ */
349
+ wholeEdges?: boolean
350
+ }
351
+
352
+ /** Within floating-point noise of each other. */
353
+ const sameEdge = (a: number, b: number) => Math.abs(a - b) < 1e-6
354
+
355
+ /**
356
+ * `from`..`to` on the canvas with both ends a whole number of pixels from the box's `start`. An end
357
+ * on the box's own edge stays there, so a box that snapped to a fractional guide keeps its layers on
358
+ * it; a span of no length, such as a level line's height, stays at none.
359
+ */
360
+ function wholeSpan(
361
+ box: { start: number; end: number },
362
+ from: number,
363
+ to: number
364
+ ): { start: number; size: number } {
365
+ const land = (edge: number) =>
366
+ sameEdge(edge, box.end) ? box.end : box.start + Math.round(edge - box.start)
367
+ const start = land(from)
368
+ if (sameEdge(from, to)) return { start, size: 0 }
369
+ return { start, size: Math.max(1, land(to) - start) }
370
+ }
371
+
372
+ /** Where a layer sits in its parent and how large it is, with its angle and flips. */
373
+ export type LayerPlacement = Pick<
374
+ SceneNode,
375
+ 'x' | 'y' | 'width' | 'height' | 'rotation' | 'flipX' | 'flipY'
376
+ >
377
+
378
+ /**
379
+ * Where a layer lands, in its parent's space, when a multi-selection's canvas box `from` is
380
+ * resized to `to`. Figma scales the layer's whole transform with the box, which skews a rotated
381
+ * layer under an uneven scale. A layer here cannot skew, so it takes what Figma then reports for
382
+ * it, and what a `.fig` file of that result opens as: its top-left corner and x axis where the
383
+ * scale puts them, its width and height the scaled lengths of its own axes, and its y axis square
384
+ * to its x axis. An even scale, or a layer upright in the box, comes out exactly as in Figma.
385
+ */
386
+ export function placeInResizedBounds(
387
+ placement: SelectionPlacement,
388
+ from: Rect,
389
+ to: Rect,
390
+ {
391
+ keepRatio = false,
392
+ round = (size: number) => size,
393
+ wholeEdges = false
394
+ }: ResizedBoundsOptions = {}
395
+ ): LayerPlacement {
396
+ const { node, world, parentWorld } = placement
397
+ const scaleX = from.width > 0 ? to.width / from.width : 1
398
+ const scaleY = from.height > 0 ? to.height / from.height : 1
399
+ const scale = Matrix.multiply(
400
+ Matrix.translated(to.x - from.x * scaleX, to.y - from.y * scaleY),
401
+ Matrix.scaled(scaleX, scaleY)
402
+ )
403
+ const [a, b, tx, c, d, ty] = Matrix.multiply(scale, world)
404
+ // The scaled axes: x along the first column, y along the second.
405
+ const xLength = Math.hypot(a, c)
406
+ const yLength = Math.hypot(b, d)
407
+ // A layer locked to its ratio scales evenly, by the mean of the box's scales, as in Figma.
408
+ const even = Math.sqrt(Math.abs(scaleX * scaleY))
409
+ let width = keepRatio ? round(node.width * even) : node.width * xLength
410
+ let height = keepRatio ? round(node.height * even) : node.height * yLength
411
+ const ux = xLength > 0 ? a / xLength : 1
412
+ const uy = xLength > 0 ? c / xLength : 0
413
+ const mirrored = a * d - b * c < 0 ? -1 : 1
414
+ let [left, top] = [tx, ty]
415
+ const upright = Math.abs(uy) < 1e-9 && ux > 0 && mirrored > 0
416
+ if (wholeEdges && !keepRatio && upright) {
417
+ const across = wholeSpan({ start: to.x, end: to.x + to.width }, tx, tx + width)
418
+ const down = wholeSpan({ start: to.y, end: to.y + to.height }, ty, ty + height)
419
+ ;[left, width, top, height] = [across.start, across.size, down.start, down.size]
420
+ }
421
+ const squared: Mat3 = [ux, -uy * mirrored, left, uy, ux * mirrored, top, 0, 0, 1]
422
+ const sized = { ...node, width, height }
423
+ const { x, y, rotation, flipX, flipY } =
424
+ localTransformFromWorld(sized, squared, parentWorld) ?? node
425
+ return { x, y, width, height, rotation, flipX, flipY }
426
+ }
427
+
428
+ /**
429
+ * Where a layer lands, in its parent's space, when it scales evenly by `scale` about a canvas
430
+ * point, as Figma's Scale tool scales it: its angle and flips stay, and the point stays put.
431
+ */
432
+ export function placeScaledAbout(
433
+ placement: SelectionPlacement,
434
+ anchor: Vector,
435
+ scale: number
436
+ ): LayerPlacement {
437
+ const from = { x: anchor.x, y: anchor.y, width: 1, height: 1 }
438
+ return placeInResizedBounds(placement, from, { ...from, width: scale, height: scale })
439
+ }
440
+
441
+ /** A canvas pointer movement along the layer's own axes, as its handles measure it. */
442
+ export function canvasDeltaInLayer(placement: SelectionPlacement, dx: number, dy: number): Vector {
443
+ const [a, b, , c, d] = placement.world
444
+ const det = a * d - b * c
445
+ if (det === 0) return { x: dx, y: dy }
446
+ return { x: (d * dx - b * dy) / det, y: (a * dy - c * dx) / det }
447
+ }
448
+
449
+ /**
450
+ * Where a layer lands when one of its handles resizes it in its own frame, from `from` to `to`,
451
+ * both its box in its parent's axes as an upright layer would have it. `to` is signed: a handle
452
+ * dragged past the opposite edge gives a negative size and mirrors the layer, as Figma flips it.
453
+ * The edges the handle leaves stay where they are on the canvas, whatever the layer's angle.
454
+ */
455
+ export function placeInResizedFrame(
456
+ placement: SelectionPlacement,
457
+ from: Rect,
458
+ to: Rect
459
+ ): LayerPlacement {
460
+ const { node, world, parentWorld } = placement
461
+ const width = Math.abs(to.width)
462
+ const height = Math.abs(to.height)
463
+ const mirrorX = to.width < 0
464
+ const mirrorY = to.height < 0
465
+ const turned = node.rotation !== 0 || node.flipX || node.flipY
466
+ if (!turned && !mirrorX && !mirrorY) {
467
+ return { x: to.x, y: to.y, width, height, rotation: 0, flipX: false, flipY: false }
468
+ }
469
+ const placed = Matrix.multiply(
470
+ Matrix.multiply(world, Matrix.translated(to.x - from.x, to.y - from.y)),
471
+ Matrix.scaled(mirrorX ? -1 : 1, mirrorY ? -1 : 1)
472
+ )
473
+ const local = localTransformFromWorld({ ...node, width, height }, placed, parentWorld)
474
+ if (!local)
475
+ return { ...to, width, height, rotation: node.rotation, flipX: node.flipX, flipY: node.flipY }
476
+ // Unmirrored, the angle and flips stay exactly as they were; only the position follows.
477
+ return mirrorX || mirrorY
478
+ ? { ...local, width, height }
479
+ : {
480
+ x: local.x,
481
+ y: local.y,
482
+ width,
483
+ height,
484
+ rotation: node.rotation,
485
+ flipX: node.flipX,
486
+ flipY: node.flipY
487
+ }
488
+ }
489
+
490
+ /**
491
+ * Where a layer lands, in its parent's space, when a multi-selection turns `degrees` about a
492
+ * canvas pivot, as Figma rotates a selection about its bounds' center: the layer's center swings
493
+ * around the pivot and the layer itself turns by the same angle on the canvas.
494
+ */
495
+ export function placeRotatedAbout(
496
+ placement: SelectionPlacement,
497
+ pivot: Vector,
498
+ degrees: number
499
+ ): { x: number; y: number; rotation: number } {
500
+ const { node, center, direction, parentInverse } = placement
501
+ const radians = (degrees * Math.PI) / 180
502
+ const cos = Math.cos(radians)
503
+ const sin = Math.sin(radians)
504
+ const dx = center.x - pivot.x
505
+ const dy = center.y - pivot.y
506
+ const target = { x: pivot.x + dx * cos - dy * sin, y: pivot.y + dx * sin + dy * cos }
507
+ const turned = node.rotation + degrees * direction
508
+ const rotation = ((((turned + 180) % 360) + 360) % 360) - 180
509
+ return { ...positionCenteredAt({ ...node, rotation }, parentInverse, target), rotation }
510
+ }
package/src/copy.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  * no shared references between source and copy.
9
9
  */
10
10
 
11
- import { geometryCommandCoordCount } from './geometry'
11
+ import { readGeometryBlob } from './geometry'
12
12
  import { cloneInstanceOverrideState } from './instances/overrides'
13
13
  import { createDefaultSourceMetadata } from './node-defaults'
14
14
  import type {
@@ -50,11 +50,16 @@ export function copyStroke(s: Stroke): Stroke {
50
50
  }
51
51
 
52
52
  export function copyEffect(e: Effect): Effect {
53
- return {
54
- ...e,
55
- color: { ...e.color },
56
- offset: { ...e.offset }
53
+ const copy: Effect = { ...e, color: { ...e.color }, offset: { ...e.offset } }
54
+ if (e.glass) copy.glass = { ...e.glass }
55
+ if (e.progressive) {
56
+ copy.progressive = {
57
+ startRadius: e.progressive.startRadius,
58
+ startOffset: { ...e.progressive.startOffset },
59
+ endOffset: { ...e.progressive.endOffset }
60
+ }
57
61
  }
62
+ return copy
58
63
  }
59
64
 
60
65
  export function copyStyleRun(r: StyleRun): StyleRun {
@@ -159,21 +164,15 @@ export function transformGeometryBlob(
159
164
  ty = 0
160
165
  ): Uint8Array {
161
166
  const out = blob.slice()
162
- const dv = new DataView(out.buffer, out.byteOffset, out.byteLength)
163
- let offset = 0
164
- while (offset < out.length) {
165
- const command = out[offset++]
166
- const coords = geometryCommandCoordCount(command)
167
- if (coords == null) break
168
- for (let i = 0; i < coords; i++) {
169
- if (offset + 8 > out.length) break
170
- const x = dv.getFloat32(offset, true)
171
- const y = dv.getFloat32(offset + 4, true)
172
- dv.setFloat32(offset, m00 * x + m01 * y + tx, true)
173
- dv.setFloat32(offset + 4, m10 * x + m11 * y + ty, true)
174
- offset += 8
167
+ const view = new DataView(out.buffer, out.byteOffset, out.byteLength)
168
+ readGeometryBlob(out, (_, coords, offset) => {
169
+ for (let i = 0; i < coords.length; i += 2) {
170
+ const x = coords[i]
171
+ const y = coords[i + 1]
172
+ view.setFloat32(offset + i * 4, m00 * x + m01 * y + tx, true)
173
+ view.setFloat32(offset + i * 4 + 4, m10 * x + m11 * y + ty, true)
175
174
  }
176
- }
175
+ })
177
176
  return out
178
177
  }
179
178
 
@@ -0,0 +1,27 @@
1
+ import type { BlendMode } from '../types'
2
+
3
+ /**
4
+ * Each blend mode as CSS names it, for `mix-blend-mode` and `background-blend-mode`. Linear
5
+ * burn and linear dodge are the `plus-` modes, which `mix-blend-mode` takes and
6
+ * `background-blend-mode` does not.
7
+ */
8
+ export const CSS_BLEND_MODE: Partial<Record<BlendMode, string>> = {
9
+ NORMAL: 'normal',
10
+ DARKEN: 'darken',
11
+ MULTIPLY: 'multiply',
12
+ COLOR_BURN: 'color-burn',
13
+ LIGHTEN: 'lighten',
14
+ LINEAR_BURN: 'plus-darker',
15
+ SCREEN: 'screen',
16
+ LINEAR_DODGE: 'plus-lighter',
17
+ COLOR_DODGE: 'color-dodge',
18
+ OVERLAY: 'overlay',
19
+ SOFT_LIGHT: 'soft-light',
20
+ HARD_LIGHT: 'hard-light',
21
+ DIFFERENCE: 'difference',
22
+ EXCLUSION: 'exclusion',
23
+ HUE: 'hue',
24
+ SATURATION: 'saturation',
25
+ COLOR: 'color',
26
+ LUMINOSITY: 'luminosity'
27
+ }
package/src/css/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { CSS_BLEND_MODE } from './blend'
1
2
  export { parseCSSGridTracks } from './grid'
2
3
  export {
3
4
  parseCSSAlignItems,
@@ -6,5 +7,21 @@ export {
6
7
  parseCSSJustifyContent
7
8
  } from './layout'
8
9
  export { parseCSSShadows } from './shadows'
9
- export { parseCSSFontFamily, parseCSSLetterSpacing, parseCSSLineHeight } from './text'
10
+ export {
11
+ CSS_GENERIC_FONT_FAMILIES,
12
+ cssFontFamilyName,
13
+ parseCSSFontFamilies,
14
+ parseCSSFontFamily,
15
+ parseCSSLetterSpacing,
16
+ parseCSSLineHeight,
17
+ resolveCSSFontFamily,
18
+ type CSSFontFamily
19
+ } from './text'
20
+ export {
21
+ parseCSSTransform,
22
+ parseCSSTransformOrigin,
23
+ parseCSSTransforms,
24
+ type CSSTransformBox,
25
+ type CSSTransformStyle
26
+ } from './transform'
10
27
  export { parseCSSColor, parseCSSNumber, splitCSSValue } from './values'
package/src/css/text.ts CHANGED
@@ -1,8 +1,7 @@
1
- import valueParser from 'postcss-value-parser'
2
-
1
+ import { DEFAULT_FONT_FAMILY } from '../constants'
3
2
  import { percentLength, pixelLength } from '../text/lengths'
4
3
  import type { TextLength } from '../types'
5
- import { parseCSSNumber } from './values'
4
+ import { dimension, parseCSSNumber } from './values'
6
5
 
7
6
  /** A CSS escape: hex digits and one optional whitespace, an escaped line break, or any one character. */
8
7
  const CSS_ESCAPE = /\\(?:([0-9a-fA-F]{1,6})(?:\r\n|[ \t\n\r\f])?|(?:\r\n|[\n\r\f])|([\s\S]))?/gu
@@ -29,28 +28,97 @@ function unescapedIndexOf(text: string, stop: string, from: number): number {
29
28
  return index
30
29
  }
31
30
 
31
+ /** A family of a CSS `font-family` list: its name, and whether it was a quoted string. */
32
+ export interface CSSFontFamily {
33
+ name: string
34
+ quoted: boolean
35
+ }
36
+
37
+ /** The family at the start of `text` and where the next one starts, or null when none is left. */
38
+ function nextFontFamily(text: string): { family: CSSFontFamily | null; rest: string } | null {
39
+ const trimmed = text.trimStart()
40
+ if (!trimmed) return null
41
+ const quote = trimmed[0]
42
+ if (quote === '"' || quote === "'") {
43
+ const end = unescapedIndexOf(trimmed, quote, 1)
44
+ const name = decodeCSSEscapes(trimmed.slice(1, end))
45
+ const comma = unescapedIndexOf(trimmed, ',', end)
46
+ return { family: name ? { name, quoted: true } : null, rest: trimmed.slice(comma + 1) }
47
+ }
48
+ const comma = unescapedIndexOf(trimmed, ',', 0)
49
+ const name = decodeCSSEscapes(trimmed.slice(0, comma)).trim().replace(/\s+/gu, ' ')
50
+ return { family: name ? { name, quoted: false } : null, rest: trimmed.slice(comma + 1) }
51
+ }
52
+
53
+ /**
54
+ * Every family of a CSS `font-family` list in order, without quotes and escapes, as
55
+ * `parseCSSFontFamily` reads the first.
56
+ */
57
+ export function parseCSSFontFamilies(value: string | undefined): CSSFontFamily[] {
58
+ const families: CSSFontFamily[] = []
59
+ for (let next = nextFontFamily(value ?? ''); next; next = nextFontFamily(next.rest)) {
60
+ if (next.family) families.push(next.family)
61
+ }
62
+ return families
63
+ }
64
+
65
+ function firstFontFamily(value: string | undefined): CSSFontFamily | null {
66
+ return parseCSSFontFamilies(value)[0] ?? null
67
+ }
68
+
69
+ /**
70
+ * The real family a family of a `font-family` list names: a generic one, such as `sans-serif`,
71
+ * mapped to a real one, and any other name as it is. A quoted name is never a generic.
72
+ */
73
+ export function cssFontFamilyName(family: CSSFontFamily): string {
74
+ if (family.quoted) return family.name
75
+ return CSS_GENERIC_FONT_FAMILIES[family.name.toLowerCase()] ?? family.name
76
+ }
77
+
32
78
  /**
33
79
  * The first family of a CSS `font-family` list, without its quotes and escapes. A family is a quoted
34
80
  * string or unquoted words, which join with single spaces; a quote opens a string only at the start
35
81
  * of the family, so a name list such as draw.io's keeps `O'Reilly`. Null for an empty list.
36
82
  */
37
83
  export function parseCSSFontFamily(value: string | undefined): string | null {
38
- const text = value?.trimStart()
39
- if (!text) return null
40
- const quote = text[0]
41
- if (quote === '"' || quote === "'") {
42
- return decodeCSSEscapes(text.slice(1, unescapedIndexOf(text, quote, 1))) || null
43
- }
44
- const words = decodeCSSEscapes(text.slice(0, unescapedIndexOf(text, ',', 0)))
45
- return words.trim().replace(/\s+/gu, ' ') || null
84
+ return firstFontFamily(value)?.name ?? null
46
85
  }
47
86
 
48
- /** A CSS dimension's number and lowercase unit, or null. */
49
- function dimension(value: string): { number: number; unit: string } | null {
50
- const parsed = valueParser.unit(value)
51
- if (!parsed) return null
52
- const number = Number(parsed.number)
53
- return Number.isFinite(number) ? { number, unit: parsed.unit.toLowerCase() } : null
87
+ const SANS_SERIF_FAMILY = DEFAULT_FONT_FAMILY
88
+ /** The families FigJam sets its Bookish and Technical typefaces in, which every font provider serves. */
89
+ const SERIF_FAMILY = 'Merriweather'
90
+ const MONOSPACE_FAMILY = 'Roboto Mono'
91
+
92
+ /**
93
+ * Real families for CSS generic families, which name no font but whatever the browser picks for
94
+ * them, and for `-apple-system` and `BlinkMacSystemFont`, which browsers read as `system-ui`.
95
+ * Keyed by lowercase name; the rest of the generics, such as `cursive`, draw in the sans-serif one.
96
+ */
97
+ export const CSS_GENERIC_FONT_FAMILIES: Readonly<Record<string, string>> = {
98
+ serif: SERIF_FAMILY,
99
+ 'ui-serif': SERIF_FAMILY,
100
+ monospace: MONOSPACE_FAMILY,
101
+ 'ui-monospace': MONOSPACE_FAMILY,
102
+ 'sans-serif': SANS_SERIF_FAMILY,
103
+ 'ui-sans-serif': SANS_SERIF_FAMILY,
104
+ 'ui-rounded': SANS_SERIF_FAMILY,
105
+ 'system-ui': SANS_SERIF_FAMILY,
106
+ '-apple-system': SANS_SERIF_FAMILY,
107
+ blinkmacsystemfont: SANS_SERIF_FAMILY,
108
+ cursive: SANS_SERIF_FAMILY,
109
+ fantasy: SANS_SERIF_FAMILY,
110
+ math: SANS_SERIF_FAMILY,
111
+ fangsong: SANS_SERIF_FAMILY
112
+ }
113
+
114
+ /**
115
+ * The family a CSS `font-family` list sets text in: its first family, with a generic family
116
+ * such as `sans-serif` or `system-ui` mapped to a real one. A quoted name is a family's own name,
117
+ * never a generic. Null for an empty list.
118
+ */
119
+ export function resolveCSSFontFamily(value: string | undefined): string | null {
120
+ const family = firstFontFamily(value)
121
+ return family ? cssFontFamilyName(family) : null
54
122
  }
55
123
 
56
124
  /**