@pairdraw/core 0.16.2 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (257) hide show
  1. package/dist/canvas/outline-stroke.d.ts.map +1 -1
  2. package/dist/canvas/outline-stroke.js.map +1 -1
  3. package/dist/canvas/overlays/prototype.d.ts +2 -5
  4. package/dist/canvas/overlays/prototype.d.ts.map +1 -1
  5. package/dist/canvas/overlays/prototype.js +3 -19
  6. package/dist/canvas/overlays/prototype.js.map +1 -1
  7. package/dist/canvas/renderer/fonts.js +2 -1
  8. package/dist/canvas/renderer/fonts.js.map +1 -1
  9. package/dist/canvas/renderer.d.ts +4 -0
  10. package/dist/canvas/renderer.d.ts.map +1 -1
  11. package/dist/canvas/renderer.js +4 -0
  12. package/dist/canvas/renderer.js.map +1 -1
  13. package/dist/canvas/rulers.js +1 -1
  14. package/dist/canvas/rulers.js.map +1 -1
  15. package/dist/canvas/scene.d.ts.map +1 -1
  16. package/dist/canvas/scene.js +61 -37
  17. package/dist/canvas/scene.js.map +1 -1
  18. package/dist/canvas/text/font-families.d.ts +4 -3
  19. package/dist/canvas/text/font-families.d.ts.map +1 -1
  20. package/dist/canvas/text/font-families.js +8 -7
  21. package/dist/canvas/text/font-families.js.map +1 -1
  22. package/dist/canvas/text/index.d.ts.map +1 -1
  23. package/dist/canvas/text/index.js +1 -1
  24. package/dist/canvas/text/index.js.map +1 -1
  25. package/dist/canvas/text/paragraph/index.js +2 -2
  26. package/dist/canvas/text/paragraph/index.js.map +1 -1
  27. package/dist/canvas/text/placed.js +2 -2
  28. package/dist/canvas/text/placed.js.map +1 -1
  29. package/dist/canvas/text/renderer.d.ts +2 -0
  30. package/dist/canvas/text/renderer.d.ts.map +1 -1
  31. package/dist/clipboard/pairdraw.d.ts.map +1 -1
  32. package/dist/clipboard/pairdraw.js +53 -7
  33. package/dist/clipboard/pairdraw.js.map +1 -1
  34. package/dist/design-jsx/preview/picture.js +6 -1
  35. package/dist/design-jsx/preview/picture.js.map +1 -1
  36. package/dist/design-jsx/renderer.d.ts.map +1 -1
  37. package/dist/editor/alignment.d.ts.map +1 -1
  38. package/dist/editor/alignment.js +35 -27
  39. package/dist/editor/alignment.js.map +1 -1
  40. package/dist/editor/bridges/undo.d.ts +1 -1
  41. package/dist/editor/clipboard/detach.d.ts.map +1 -1
  42. package/dist/editor/clipboard/detach.js +3 -3
  43. package/dist/editor/clipboard/detach.js.map +1 -1
  44. package/dist/editor/clipboard/paste/properties.d.ts.map +1 -1
  45. package/dist/editor/clipboard/paste/properties.js +3 -1
  46. package/dist/editor/clipboard/paste/properties.js.map +1 -1
  47. package/dist/editor/create.d.ts +1 -1
  48. package/dist/editor/structure.d.ts.map +1 -1
  49. package/dist/editor/structure.js +3 -1
  50. package/dist/editor/structure.js.map +1 -1
  51. package/dist/editor/viewport.js +2 -2
  52. package/dist/editor/viewport.js.map +1 -1
  53. package/dist/figma-api/index.d.ts +43 -8
  54. package/dist/figma-api/index.d.ts.map +1 -1
  55. package/dist/figma-api/index.js +48 -6
  56. package/dist/figma-api/index.js.map +1 -1
  57. package/dist/figma-api/serialization.js +10 -0
  58. package/dist/figma-api/serialization.js.map +1 -1
  59. package/dist/figma-api/text/bindings.d.ts.map +1 -1
  60. package/dist/figma-api/text/bindings.js +20 -8
  61. package/dist/figma-api/text/bindings.js.map +1 -1
  62. package/dist/figma-api/text/index.js +1 -1
  63. package/dist/geometry/connectors.js +2 -14
  64. package/dist/geometry/connectors.js.map +1 -1
  65. package/dist/geometry/index.d.ts +2 -2
  66. package/dist/geometry/scene.d.ts +3 -1
  67. package/dist/geometry/scene.d.ts.map +1 -1
  68. package/dist/geometry/scene.js +16 -1
  69. package/dist/geometry/scene.js.map +1 -1
  70. package/dist/geometry/types.d.ts +7 -1
  71. package/dist/geometry/types.d.ts.map +1 -1
  72. package/dist/io/formats/drawio/export.d.ts.map +1 -1
  73. package/dist/io/formats/drawio/export.js +10 -0
  74. package/dist/io/formats/drawio/export.js.map +1 -1
  75. package/dist/io/formats/drawio/index.d.ts.map +1 -1
  76. package/dist/io/formats/drawio/index.js +32 -1
  77. package/dist/io/formats/drawio/index.js.map +1 -1
  78. package/dist/io/formats/drawio/shapes.d.ts +10 -5
  79. package/dist/io/formats/drawio/shapes.d.ts.map +1 -1
  80. package/dist/io/formats/drawio/shapes.js +10 -6
  81. package/dist/io/formats/drawio/shapes.js.map +1 -1
  82. package/dist/io/formats/drawio/warnings.d.ts +11 -0
  83. package/dist/io/formats/drawio/warnings.d.ts.map +1 -0
  84. package/dist/io/formats/drawio/warnings.js +23 -0
  85. package/dist/io/formats/drawio/warnings.js.map +1 -0
  86. package/dist/io/formats/pdf/dom.d.ts +10 -1
  87. package/dist/io/formats/pdf/dom.d.ts.map +1 -1
  88. package/dist/io/formats/pdf/dom.js +31 -1
  89. package/dist/io/formats/pdf/dom.js.map +1 -1
  90. package/dist/io/formats/pdf/export.js +3 -2
  91. package/dist/io/formats/pdf/export.js.map +1 -1
  92. package/dist/io/formats/raster/headless.d.ts +2 -2
  93. package/dist/io/formats/raster/headless.d.ts.map +1 -1
  94. package/dist/io/formats/raster/headless.js +2 -2
  95. package/dist/io/formats/raster/headless.js.map +1 -1
  96. package/dist/io/formats/raster/render.d.ts +1 -1
  97. package/dist/io/formats/raster/render.d.ts.map +1 -1
  98. package/dist/io/formats/raster/render.js +2 -2
  99. package/dist/io/formats/raster/render.js.map +1 -1
  100. package/dist/io/formats.d.ts.map +1 -1
  101. package/dist/io/formats.js +6 -6
  102. package/dist/io/formats.js.map +1 -1
  103. package/dist/io/registry.d.ts +2 -0
  104. package/dist/io/registry.d.ts.map +1 -1
  105. package/dist/io/registry.js +5 -0
  106. package/dist/io/registry.js.map +1 -1
  107. package/dist/io/types.d.ts +2 -0
  108. package/dist/io/types.d.ts.map +1 -1
  109. package/dist/library/snapshot.d.ts.map +1 -1
  110. package/dist/library/snapshot.js +1 -0
  111. package/dist/library/snapshot.js.map +1 -1
  112. package/dist/lint/linter.d.ts.map +1 -1
  113. package/dist/lint/linter.js +11 -2
  114. package/dist/lint/linter.js.map +1 -1
  115. package/dist/rpc/variables-command.d.ts.map +1 -1
  116. package/dist/rpc/variables-command.js +4 -2
  117. package/dist/rpc/variables-command.js.map +1 -1
  118. package/dist/text/character-style.d.ts.map +1 -1
  119. package/dist/text/character-style.js +2 -1
  120. package/dist/text/character-style.js.map +1 -1
  121. package/dist/text/coverage.d.ts.map +1 -1
  122. package/dist/text/coverage.js +6 -3
  123. package/dist/text/coverage.js.map +1 -1
  124. package/dist/text/fallbacks.d.ts +8 -2
  125. package/dist/text/fallbacks.d.ts.map +1 -1
  126. package/dist/text/fallbacks.js +73 -7
  127. package/dist/text/fallbacks.js.map +1 -1
  128. package/dist/text/font/manager.d.ts +14 -6
  129. package/dist/text/font/manager.d.ts.map +1 -1
  130. package/dist/text/font/manager.js +40 -14
  131. package/dist/text/font/manager.js.map +1 -1
  132. package/dist/text/index.d.ts +2 -2
  133. package/dist/text/index.js +3 -3
  134. package/dist/text/outlines.js +1 -1
  135. package/dist/text/outlines.js.map +1 -1
  136. package/dist/text/path/caret.d.ts +2 -4
  137. package/dist/text/path/caret.d.ts.map +1 -1
  138. package/dist/text/path/caret.js.map +1 -1
  139. package/dist/text/prepare.d.ts +5 -1
  140. package/dist/text/prepare.d.ts.map +1 -1
  141. package/dist/text/prepare.js +19 -2
  142. package/dist/text/prepare.js.map +1 -1
  143. package/dist/text/web-font/assets.d.ts.map +1 -1
  144. package/dist/text/web-font/assets.js +13 -21
  145. package/dist/text/web-font/assets.js.map +1 -1
  146. package/dist/text/web-font/resolver.d.ts.map +1 -1
  147. package/dist/text/web-font/resolver.js +4 -1
  148. package/dist/text/web-font/resolver.js.map +1 -1
  149. package/dist/tools/create/components.d.ts +2 -1
  150. package/dist/tools/create/components.d.ts.map +1 -1
  151. package/dist/tools/create/components.js +27 -1
  152. package/dist/tools/create/components.js.map +1 -1
  153. package/dist/tools/create/icons.d.ts.map +1 -1
  154. package/dist/tools/create/icons.js +11 -5
  155. package/dist/tools/create/icons.js.map +1 -1
  156. package/dist/tools/create/vector.d.ts.map +1 -1
  157. package/dist/tools/create/vector.js +9 -6
  158. package/dist/tools/create/vector.js.map +1 -1
  159. package/dist/tools/create.d.ts +2 -2
  160. package/dist/tools/create.js +2 -2
  161. package/dist/tools/modify/effects.d.ts.map +1 -1
  162. package/dist/tools/modify/effects.js +5 -3
  163. package/dist/tools/modify/effects.js.map +1 -1
  164. package/dist/tools/modify/matching.d.ts.map +1 -1
  165. package/dist/tools/modify/matching.js +9 -6
  166. package/dist/tools/modify/matching.js.map +1 -1
  167. package/dist/tools/modify/paint.d.ts.map +1 -1
  168. package/dist/tools/modify/paint.js +7 -5
  169. package/dist/tools/modify/paint.js.map +1 -1
  170. package/dist/tools/modify/text.d.ts.map +1 -1
  171. package/dist/tools/modify/text.js +28 -8
  172. package/dist/tools/modify/text.js.map +1 -1
  173. package/dist/tools/read/libraries.d.ts.map +1 -1
  174. package/dist/tools/read/libraries.js +20 -8
  175. package/dist/tools/read/libraries.js.map +1 -1
  176. package/dist/tools/read/pages.d.ts.map +1 -1
  177. package/dist/tools/read/pages.js.map +1 -1
  178. package/dist/tools/registry/extended.d.ts.map +1 -1
  179. package/dist/tools/registry/extended.js +2 -1
  180. package/dist/tools/registry/extended.js.map +1 -1
  181. package/dist/tools/schema.d.ts +24 -5
  182. package/dist/tools/schema.d.ts.map +1 -1
  183. package/dist/tools/schema.js +80 -6
  184. package/dist/tools/schema.js.map +1 -1
  185. package/dist/tools/structure/batch.d.ts.map +1 -1
  186. package/dist/tools/structure/batch.js +4 -3
  187. package/dist/tools/structure/batch.js.map +1 -1
  188. package/dist/tools/variables/values.d.ts.map +1 -1
  189. package/dist/tools/variables/values.js +4 -2
  190. package/dist/tools/variables/values.js.map +1 -1
  191. package/dist/tools/vector/export.d.ts.map +1 -1
  192. package/dist/tools/vector/export.js +5 -2
  193. package/dist/tools/vector/export.js.map +1 -1
  194. package/package.json +9 -9
  195. package/src/canvas/outline-stroke.ts +2 -6
  196. package/src/canvas/overlays/prototype.ts +5 -27
  197. package/src/canvas/renderer/fonts.ts +2 -1
  198. package/src/canvas/renderer.ts +4 -0
  199. package/src/canvas/rulers.ts +1 -1
  200. package/src/canvas/scene.ts +92 -63
  201. package/src/canvas/text/font-families.ts +8 -7
  202. package/src/canvas/text/index.ts +3 -1
  203. package/src/canvas/text/paragraph/index.ts +2 -2
  204. package/src/canvas/text/placed.ts +2 -2
  205. package/src/canvas/text/renderer.ts +2 -0
  206. package/src/clipboard/pairdraw.ts +51 -7
  207. package/src/design-jsx/preview/picture.ts +8 -2
  208. package/src/editor/alignment.ts +33 -29
  209. package/src/editor/clipboard/detach.ts +9 -3
  210. package/src/editor/clipboard/paste/properties.ts +3 -1
  211. package/src/editor/structure.ts +3 -1
  212. package/src/editor/viewport.ts +2 -2
  213. package/src/figma-api/index.ts +90 -9
  214. package/src/figma-api/serialization.ts +14 -0
  215. package/src/figma-api/text/bindings.ts +26 -8
  216. package/src/geometry/connectors.ts +2 -12
  217. package/src/geometry/index.ts +1 -1
  218. package/src/geometry/scene.ts +19 -0
  219. package/src/geometry/types.ts +8 -0
  220. package/src/io/formats/drawio/export.ts +11 -0
  221. package/src/io/formats/drawio/index.ts +36 -1
  222. package/src/io/formats/drawio/shapes.ts +13 -9
  223. package/src/io/formats/drawio/warnings.ts +27 -0
  224. package/src/io/formats/pdf/dom.ts +29 -0
  225. package/src/io/formats/pdf/export.ts +3 -2
  226. package/src/io/formats/raster/headless.ts +10 -3
  227. package/src/io/formats/raster/render.ts +4 -2
  228. package/src/io/formats.ts +8 -10
  229. package/src/io/registry.ts +12 -0
  230. package/src/io/types.ts +2 -0
  231. package/src/library/snapshot.ts +1 -0
  232. package/src/lint/linter.ts +12 -2
  233. package/src/rpc/variables-command.ts +6 -5
  234. package/src/text/character-style.ts +8 -4
  235. package/src/text/coverage.ts +17 -2
  236. package/src/text/fallbacks.ts +81 -7
  237. package/src/text/font/manager.ts +50 -17
  238. package/src/text/outlines.ts +1 -1
  239. package/src/text/path/caret.ts +3 -4
  240. package/src/text/prepare.ts +34 -2
  241. package/src/text/web-font/assets.ts +11 -15
  242. package/src/text/web-font/resolver.ts +7 -1
  243. package/src/tools/create/components.ts +33 -0
  244. package/src/tools/create/icons.ts +13 -5
  245. package/src/tools/create/vector.ts +16 -6
  246. package/src/tools/create.ts +1 -0
  247. package/src/tools/modify/effects.ts +11 -4
  248. package/src/tools/modify/matching.ts +10 -6
  249. package/src/tools/modify/paint.ts +13 -5
  250. package/src/tools/modify/text.ts +53 -20
  251. package/src/tools/read/libraries.ts +35 -11
  252. package/src/tools/read/pages.ts +1 -0
  253. package/src/tools/registry/extended.ts +2 -0
  254. package/src/tools/schema.ts +109 -5
  255. package/src/tools/structure/batch.ts +10 -5
  256. package/src/tools/variables/values.ts +6 -3
  257. package/src/tools/vector/export.ts +5 -1
@@ -44,6 +44,11 @@ function style(names: string[], values: Record<string, string | undefined>): Dra
44
44
  }
45
45
  }
46
46
 
47
+ /** A layer's name, which draw.io has no field of its own for, so it travels in the style. */
48
+ function nameValues(node: SceneNode): Record<string, string> {
49
+ return { pairdrawName: encodeURIComponent(node.name) }
50
+ }
51
+
47
52
  /** A layer's first visible solid fill, or the first stop of a gradient, as draw.io's colour. */
48
53
  function fillColor(node: SceneNode): string {
49
54
  const fill = node.fills.find((paint) => paint.visible)
@@ -296,6 +301,7 @@ class PageWriter {
296
301
  style: style(['text'], {
297
302
  html: '1',
298
303
  whiteSpace: 'wrap',
304
+ ...nameValues(node),
299
305
  ...placedValues(placement),
300
306
  ...fontValues(node)
301
307
  }),
@@ -349,6 +355,7 @@ class PageWriter {
349
355
  style: style([], {
350
356
  container: '1',
351
357
  html: '1',
358
+ ...nameValues(node),
352
359
  ...paintValues(node),
353
360
  ...placedValues(placement)
354
361
  }),
@@ -361,7 +368,10 @@ class PageWriter {
361
368
  kind: 'vertex',
362
369
  value: '',
363
370
  style: style(node.type === 'ELLIPSE' ? ['ellipse'] : [], {
371
+ ...(node.type === 'RECTANGLE' ? { pairdrawType: 'RECTANGLE' } : {}),
364
372
  rounded: node.cornerRadius > 0 ? '1' : undefined,
373
+ pairdrawCornerRadius: node.cornerRadius > 0 ? String(node.cornerRadius) : undefined,
374
+ ...nameValues(node),
365
375
  ...placedValues(placement),
366
376
  ...paintValues(node)
367
377
  }),
@@ -378,6 +388,7 @@ class PageWriter {
378
388
  const label = shapeWithTextLabel(this.graph, node)
379
389
  const values: Record<string, string | undefined> = {
380
390
  ...drawioShapeValues(node.shapeWithTextType),
391
+ ...nameValues(node),
381
392
  whiteSpace: 'wrap',
382
393
  html: '1',
383
394
  ...paintValues(node),
@@ -33,6 +33,25 @@ function nameOf(text: string, fallback: string): string {
33
33
  return text.split('\n')[0]?.trim() || fallback
34
34
  }
35
35
 
36
+ /**
37
+ * The name Pairdraw wrote into a cell's style. draw.io has no field of its own for a layer whose
38
+ * cell draws no text, and a text cell's label is its content, so the name travels beside them.
39
+ */
40
+ function writtenName(style: DrawIOStyle): string | undefined {
41
+ const value = style.values.pairdrawName
42
+ if (value === undefined) return undefined
43
+ try {
44
+ return decodeURIComponent(value)
45
+ } catch {
46
+ return undefined
47
+ }
48
+ }
49
+
50
+ /** A layer's name: the one Pairdraw wrote, else the first line of its label. */
51
+ function layerName(style: DrawIOStyle, text: string, fallback: string): string {
52
+ return writtenName(style) ?? nameOf(text, fallback)
53
+ }
54
+
36
55
  /** Builds one draw.io page's cells into `pageId`, in draw.io's drawing order. */
37
56
  class PageBuilder {
38
57
  /** The layer each draw.io cell or layer became. */
@@ -113,13 +132,29 @@ class PageBuilder {
113
132
  return graph.createNode('TEXT', parentId, {
114
133
  ...box,
115
134
  ...placementOf(cell.style),
116
- name: nameOf(label.text, 'Text'),
135
+ name: layerName(cell.style, label.text, 'Text'),
117
136
  ...labelFields(cell.style, label.format),
118
137
  text: label.text,
119
138
  styleRuns: label.runs,
120
139
  textAutoResize: 'NONE'
121
140
  })
122
141
  }
142
+ if (kind.type === 'FRAME') {
143
+ return graph.createNode('FRAME', parentId, {
144
+ ...box,
145
+ ...shapePaint(cell.style),
146
+ name: layerName(cell.style, label.text, 'Frame')
147
+ })
148
+ }
149
+ if (kind.type === 'RECTANGLE') {
150
+ const radius = Number.parseFloat(cell.style.values.pairdrawCornerRadius ?? '')
151
+ return graph.createNode('RECTANGLE', parentId, {
152
+ ...box,
153
+ ...shapePaint(cell.style),
154
+ ...(radius > 0 ? { cornerRadius: radius } : {}),
155
+ name: layerName(cell.style, label.text, 'Rectangle')
156
+ })
157
+ }
123
158
  if (kind.type === 'SECTION') {
124
159
  return graph.createNode('SECTION', parentId, {
125
160
  ...box,
@@ -1,12 +1,14 @@
1
1
  import type { DrawIOStyle } from '@pairdraw/drawio'
2
2
  import type { ShapeWithTextType } from '@pairdraw/scene-graph/shape-with-text'
3
3
 
4
- /** What a draw.io vertex becomes: a FigJam shape, text, a section around its cells, or a group. */
4
+ /** What a draw.io vertex becomes: a shape, text, a frame or section around its cells, or a group. */
5
5
  export type VertexKind =
6
6
  /** A FigJam shape, turned clockwise by `rotation` degrees about its centre. */
7
7
  | { type: 'SHAPE_WITH_TEXT'; shape: ShapeWithTextType; rotation: number; stencil: boolean }
8
8
  | { type: 'TEXT' }
9
+ | { type: 'FRAME' }
9
10
  | { type: 'SECTION' }
11
+ | { type: 'RECTANGLE' }
10
12
  | { type: 'GROUP' }
11
13
 
12
14
  /**
@@ -82,18 +84,20 @@ const DIRECTION_TURN = new Map([
82
84
  ])
83
85
 
84
86
  /**
85
- * What a vertex becomes. Text stays text; a swimlane, or a vertex its style makes a container,
86
- * becomes a section holding its cells; a group stays a group. A shape draw.io turns by
87
- * `direction` keeps its turn. Any other shape, a stencil FigJam has no shape for among them,
88
- * becomes a rectangle, named after the stencil so it can be found.
87
+ * What a vertex becomes. Text stays text; a swimlane becomes a section holding its cells, and
88
+ * any other vertex its style makes a container becomes a frame, as Pairdraw writes one; a group
89
+ * stays a group. A style Pairdraw wrote says when it drew a rectangle, which draw.io has no
90
+ * style of its own for. A shape draw.io turns by `direction` keeps its turn. Any other shape, a
91
+ * stencil FigJam has no shape for among them, becomes a shape named after the stencil.
89
92
  */
90
93
  export function vertexKind(style: DrawIOStyle, hasChildren: boolean): VertexKind {
91
94
  const name = shapeName(style)
92
95
  if (name === 'text') return { type: 'TEXT' }
93
- if (name === 'swimlane' || (hasChildren && style.values.container === '1')) {
94
- return { type: 'SECTION' }
95
- }
96
- if (name === 'group' || (hasChildren && !SHAPES.has(name))) return { type: 'GROUP' }
96
+ if (style.values.pairdrawType === 'RECTANGLE') return { type: 'RECTANGLE' }
97
+ if (name === 'swimlane') return { type: 'SECTION' }
98
+ if (name === 'group') return { type: 'GROUP' }
99
+ if (style.values.container === '1') return { type: 'FRAME' }
100
+ if (hasChildren && !SHAPES.has(name)) return { type: 'GROUP' }
97
101
  let shape = SHAPES.get(name) ?? 'SQUARE'
98
102
  const direction = style.values.direction ?? 'east'
99
103
  let rotation = 0
@@ -0,0 +1,27 @@
1
+ import type { SceneGraph } from '@pairdraw/scene-graph'
2
+
3
+ /** `3 variables`, with the noun matching the count. */
4
+ function counted(count: number, one: string, many: string): string {
5
+ return `${count} ${count === 1 ? one : many}`
6
+ }
7
+
8
+ /**
9
+ * What a draw.io file cannot hold. draw.io has no variable model and no style that reads one, so
10
+ * a document that paints with variables arrives in draw.io as plain colours and comes back
11
+ * unbound. The writer names that loss instead of writing a file that looks complete.
12
+ */
13
+ export function drawioDroppedWarnings(graph: SceneGraph): string[] {
14
+ const variables = graph.variables.size
15
+ if (variables === 0) return []
16
+ const names = [...graph.variableCollections.values()].map((collection) => collection.name)
17
+ const dropped = `draw.io has no variables: ${counted(variables, 'variable', 'variables')} in ${counted(names.length, 'collection', 'collections')} (${names.join(', ')}) not written`
18
+ const bound = [...graph.nodes.values()].filter(
19
+ (node) => Object.keys(node.boundVariables).length > 0
20
+ ).length
21
+ if (bound === 0) return [dropped]
22
+ const users =
23
+ bound === 1
24
+ ? '1 layer that uses one lost the binding'
25
+ : `${counted(bound, 'layer', 'layers')} that use them lost the binding`
26
+ return [`${dropped}, and ${users}`]
27
+ }
@@ -1,4 +1,5 @@
1
1
  import type * as HappyDOM from 'happy-dom'
2
+ import type { svg2pdf } from 'svg2pdf.js'
2
3
 
3
4
  import { IS_BROWSER } from '#core/constants'
4
5
 
@@ -99,3 +100,31 @@ async function withHappyDOM<T>(draw: (parse: SVGPageParser) => Promise<T>): Prom
99
100
  await dom.happyDOM.close()
100
101
  }
101
102
  }
103
+
104
+ /**
105
+ * Loads `svg2pdf.js`. Its package publishes a UMD build only, whose factory reads jsPDF from the
106
+ * global `jspdf` and, with no CommonJS `exports`, writes the function to the global `svg2pdf`; a
107
+ * bundler supplies both, a bare Node install supplies neither, and the module then fails on load.
108
+ * The two globals exist only while the import evaluates, and the function the module made keeps
109
+ * the jsPDF it read, so the rest of the export needs nothing from them.
110
+ */
111
+ export async function loadSVG2PDF(jsPDF: unknown): Promise<typeof svg2pdf> {
112
+ const scope = globalThis as { jspdf?: unknown; svg2pdf?: { svg2pdf?: typeof svg2pdf } }
113
+ const previousJSPDF = Object.getOwnPropertyDescriptor(globalThis, 'jspdf')
114
+ const previousSVG2PDF = Object.getOwnPropertyDescriptor(globalThis, 'svg2pdf')
115
+ const set = (name: string, value: unknown): void => {
116
+ Object.defineProperty(globalThis, name, { value, configurable: true, writable: true })
117
+ }
118
+ set('jspdf', { jsPDF })
119
+ try {
120
+ const module = (await import('svg2pdf.js')) as { svg2pdf?: typeof svg2pdf }
121
+ const draw = module.svg2pdf ?? scope.svg2pdf?.svg2pdf
122
+ if (!draw) throw new Error('svg2pdf.js exported no svg2pdf function')
123
+ return draw
124
+ } finally {
125
+ if (previousJSPDF) Object.defineProperty(globalThis, 'jspdf', previousJSPDF)
126
+ else delete scope.jspdf
127
+ if (previousSVG2PDF) Object.defineProperty(globalThis, 'svg2pdf', previousSVG2PDF)
128
+ else delete scope.svg2pdf
129
+ }
130
+ }
@@ -3,7 +3,7 @@ import type { SceneGraph } from '@pairdraw/scene-graph'
3
3
  import { computeContentBounds } from '#core/io/formats/raster'
4
4
  import { renderNodesToSVG, type SVGExportOptions, type SVGTextLayout } from '#core/io/formats/svg'
5
5
 
6
- import { withPDFDOM, type SVGPageParser } from './dom'
6
+ import { loadSVG2PDF, withPDFDOM, type SVGPageParser } from './dom'
7
7
  import {
8
8
  cutFontInstances,
9
9
  embeddedFontAlias,
@@ -97,7 +97,8 @@ async function renderPages(
97
97
  if (prepared.length === 0) return null
98
98
  const first = prepared[0]
99
99
 
100
- const [{ jsPDF }, { svg2pdf }] = await Promise.all([import('jspdf'), import('svg2pdf.js')])
100
+ const { jsPDF } = await import('jspdf')
101
+ const svg2pdf = await loadSVG2PDF(jsPDF)
101
102
 
102
103
  const doc = new jsPDF({
103
104
  orientation: orientationOf(first),
@@ -4,7 +4,12 @@ import type { SceneGraph } from '@pairdraw/scene-graph'
4
4
 
5
5
  import { SkiaRenderer } from '#core/canvas'
6
6
 
7
- import { renderNodesToImage, renderThumbnail, type ExportFormat } from './render'
7
+ import {
8
+ renderNodesToImage,
9
+ renderThumbnail,
10
+ type ExportFormat,
11
+ type RasterExportFormat
12
+ } from './render'
8
13
 
9
14
  let cachedCk: CanvasKit | null = null
10
15
  let cachedRenderer: SkiaRenderer | null = null
@@ -59,10 +64,12 @@ export async function headlessRenderThumbnail(
59
64
  graph: SceneGraph,
60
65
  pageId: string,
61
66
  width: number,
62
- height: number
67
+ height: number,
68
+ format: RasterExportFormat = 'PNG',
69
+ quality = 100
63
70
  ): Promise<Uint8Array | null> {
64
71
  const { ck, renderer } = await getRenderer()
65
72
  renderer.invalidateAllPictures()
66
73
  await renderer.prepareForExport(graph, pageId, graph.getNode(pageId)?.childIds ?? [])
67
- return renderThumbnail(ck, renderer, graph, pageId, width, height)
74
+ return renderThumbnail(ck, renderer, graph, pageId, width, height, format, quality)
68
75
  }
@@ -366,7 +366,9 @@ export function renderThumbnail(
366
366
  graph: SceneGraph,
367
367
  pageId: string,
368
368
  width: number,
369
- height: number
369
+ height: number,
370
+ format: RasterExportFormat = 'PNG',
371
+ quality = 100
370
372
  ): Uint8Array<ArrayBuffer> | null {
371
373
  const frame = thumbnailFrame(graph, pageId, width, height)
372
374
  if (!frame) return null
@@ -374,7 +376,7 @@ export function renderThumbnail(
374
376
  // The page's own background, which the editor's page colour matches only while it is open.
375
377
  const background = getPageColor(graph.getNode(pageId))
376
378
 
377
- return renderToSurface(ck, renderer, graph, pageId, pixelW, pixelH, 'PNG', 100, (canvas) => {
379
+ return renderToSurface(ck, renderer, graph, pageId, pixelW, pixelH, format, quality, (canvas) => {
378
380
  canvas.clear(ck.Color4f(background.r, background.g, background.b, 1))
379
381
  canvas.translate(-bounds.minX * scale, -bounds.minY * scale)
380
382
  canvas.scale(scale, scale)
package/src/io/formats.ts CHANGED
@@ -314,12 +314,14 @@ export const mermaidFormat: IOFormatAdapter<'mermaid'> = {
314
314
  async function writeDrawIO(graph: SceneGraph, target: DrawIOExportTarget): Promise<ExportResult> {
315
315
  const { writeDrawIOFile } = await import('@pairdraw/drawio')
316
316
  const { sceneGraphToDrawIO } = await import('./formats/drawio')
317
+ const { drawioDroppedWarnings } = await import('./formats/drawio/warnings')
317
318
  return {
318
319
  format: 'drawio',
319
320
  mimeType: 'application/vnd.jgraph.mxfile',
320
321
  extension: 'drawio',
321
322
  data: writeDrawIOFile(sceneGraphToDrawIO(graph, target)),
322
- encoding: 'utf8'
323
+ encoding: 'utf8',
324
+ warnings: drawioDroppedWarnings(graph)
323
325
  }
324
326
  }
325
327
 
@@ -458,19 +460,15 @@ export const jsxFormat: IOFormatAdapter<'jsx'> = {
458
460
  category: 'code',
459
461
  extensions: ['jsx'],
460
462
  mimeTypes: ['text/plain', 'text/jsx'],
461
- support: {
462
- exportSelection: true,
463
- exportNode: true
464
- },
463
+ support: EXPORT_EVERY_TARGET,
465
464
  exportOptions: FIXED_SIZE_EXPORT,
466
465
  async exportContent(request): Promise<ExportResult> {
467
466
  const nodeId = ensureSingleNode(request.target)
467
+ // A page or a document exports its top-level layers, as Tailwind JSX does.
468
+ const target = nodeId ? null : resolveExportNodes(request)
468
469
  let data = ''
469
- if (nodeId) {
470
- data = sceneNodeToJSX(nodeId, request.graph)
471
- } else if (request.target.scope === 'selection') {
472
- data = selectionToJSX(request.target.nodeIds, request.graph)
473
- }
470
+ if (nodeId) data = sceneNodeToJSX(nodeId, request.graph)
471
+ else if (target) data = selectionToJSX(target.nodeIds, request.graph)
474
472
  if (!data) throw new Error('Nothing to export')
475
473
  return {
476
474
  format: 'jsx',
@@ -38,6 +38,18 @@ export class IORegistry {
38
38
  })
39
39
  }
40
40
 
41
+ /** The writable format a file name asks for, so a save keeps the document's own format. */
42
+ findWriter(fileName: string): IOFormatAdapter | null {
43
+ const lower = fileName.toLowerCase()
44
+ return (
45
+ this.adapters.find(
46
+ (adapter) =>
47
+ !!adapter.support.writeDocument &&
48
+ adapter.extensions.some((ext) => lower.endsWith(`.${ext}`))
49
+ ) ?? null
50
+ )
51
+ }
52
+
41
53
  findReader(fileName: string, mimeType?: string): IOFormatAdapter | null {
42
54
  return (
43
55
  this.adapters.find((adapter) => {
package/src/io/types.ts CHANGED
@@ -133,6 +133,8 @@ export interface ExportResult {
133
133
  encoding?: IOTextEncoding
134
134
  /** Extra files the main file refers to, such as external images and fonts. */
135
135
  assets?: ExportAsset[]
136
+ /** What the format could not hold, such as the variables draw.io has no model for. */
137
+ warnings?: string[]
136
138
  }
137
139
 
138
140
  export interface HTMLExportOptions {
@@ -19,6 +19,7 @@ const VOLATILE_NODE_FIELDS = new Set([
19
19
  'textPicture',
20
20
  'derivedTextGlyphs',
21
21
  'derivedLayout',
22
+ 'derivedTextHeight',
22
23
  'librarySource'
23
24
  ])
24
25
 
@@ -28,6 +28,15 @@ function effectiveTextStyles(node: SceneNode): { fontSize: number; fontWeight: n
28
28
  return covered < node.text.length || runs.length === 0 ? [base, ...runs] : runs
29
29
  }
30
30
 
31
+ /** A preset's rules, refusing a name no preset has so a typo cannot silently disable linting. */
32
+ function presetRules(id: string) {
33
+ if (!Object.hasOwn(presets, id)) {
34
+ const known = Object.keys(presets).sort().join(', ')
35
+ throw new Error(`Unknown lint preset "${id}". Known presets: ${known}`)
36
+ }
37
+ return presets[id].rules
38
+ }
39
+
31
40
  export class Linter {
32
41
  private rules = new Map<string, Rule>()
33
42
  private ruleConfigs = new Map<string, { severity: Severity; options?: Record<string, unknown> }>()
@@ -46,16 +55,17 @@ export class Linter {
46
55
  string,
47
56
  Severity | { severity: Severity; options?: Record<string, unknown> }
48
57
  > = {}
49
- if (options.preset) baseConfig = { ...presets[options.preset].rules }
58
+ if (options.preset) baseConfig = { ...presetRules(options.preset) }
50
59
  if (options.config?.extends) {
51
60
  const ids = Array.isArray(options.config.extends)
52
61
  ? options.config.extends
53
62
  : [options.config.extends]
54
- for (const id of ids) baseConfig = { ...baseConfig, ...presets[id].rules }
63
+ for (const id of ids) baseConfig = { ...baseConfig, ...presetRules(id) }
55
64
  }
56
65
  if (options.config?.rules) baseConfig = { ...baseConfig, ...options.config.rules }
57
66
  const rulesToLoad = options.rules ?? Object.keys(baseConfig)
58
67
  for (const ruleId of rulesToLoad) {
68
+ if (!Object.hasOwn(allRules, ruleId)) throw new Error(`Unknown lint rule "${ruleId}"`)
59
69
  const rule = allRules[ruleId]
60
70
  const config = baseConfig[ruleId]
61
71
  if (config === 'off') continue
@@ -61,11 +61,7 @@ export const variablesCommand: RPCCommand<VariablesArgs, VariablesResult> = {
61
61
  const typeFilter = args.type?.toUpperCase()
62
62
  const collFilter = args.collection?.toLowerCase()
63
63
 
64
- const result: VariablesResult = {
65
- collections: [],
66
- totalVariables: graph.variables.size,
67
- totalCollections: graph.variableCollections.size
68
- }
64
+ const result: VariablesResult = { collections: [], totalVariables: 0, totalCollections: 0 }
69
65
 
70
66
  for (const coll of graph.variableCollections.values()) {
71
67
  if (collFilter && !coll.name.toLowerCase().includes(collFilter)) continue
@@ -89,6 +85,11 @@ export const variablesCommand: RPCCommand<VariablesArgs, VariablesResult> = {
89
85
  })
90
86
  }
91
87
 
88
+ result.totalVariables = result.collections.reduce(
89
+ (count, collection) => count + collection.variables.length,
90
+ 0
91
+ )
92
+ result.totalCollections = result.collections.length
92
93
  return result
93
94
  }
94
95
  }
@@ -16,6 +16,7 @@ import {
16
16
  SHARED_TEXT_STYLE_FIELDS,
17
17
  styleDetachmentChanges
18
18
  } from '@pairdraw/scene-graph/shared-styles'
19
+ import { withBoundBindingScales } from '@pairdraw/scene-graph/variables'
19
20
 
20
21
  /** A character's style: the text's own, with its run's overrides over it. */
21
22
  export type CharacterStyle = Required<
@@ -140,10 +141,13 @@ function withoutRedundantOverrides(node: SceneNode, runs: StyleRun[]): StyleRun[
140
141
  const base = nodeStyle(node)
141
142
  const result: StyleRun[] = []
142
143
  for (const run of runs) {
143
- const style = omit(
144
- run.style,
145
- CHARACTER_STYLE_KEYS.filter(
146
- (key) => run.style[key] !== undefined && isEqual(run.style[key], base[key])
144
+ // A range keeps a binding's scale only while it binds the number itself.
145
+ const style = withBoundBindingScales(
146
+ omit(
147
+ run.style,
148
+ CHARACTER_STYLE_KEYS.filter(
149
+ (key) => run.style[key] !== undefined && isEqual(run.style[key], base[key])
150
+ )
147
151
  )
148
152
  )
149
153
  if (Object.keys(style).length === 0) continue
@@ -4,10 +4,14 @@ import { DEFAULT_FONT_FAMILY } from '#core/constants'
4
4
  import {
5
5
  ARABIC_CHARACTER,
6
6
  cjkFallbackScriptForLanguage,
7
+ DEVANAGARI_CHARACTER,
7
8
  fontFallbackScriptForCharacter,
8
9
  HAN_CHARACTER,
9
10
  HANGUL_CHARACTER,
11
+ HEBREW_CHARACTER,
10
12
  KANA_CHARACTER,
13
+ LETTER_FALLBACK_SCRIPTS,
14
+ THAI_CHARACTER,
11
15
  TRADITIONAL_HAN_CHARACTER,
12
16
  type FontFallbackScript
13
17
  } from '#core/text/fallbacks'
@@ -20,6 +24,12 @@ function scriptCharRegex(script: FontFallbackScript): RegExp {
20
24
  switch (script) {
21
25
  case 'arabic':
22
26
  return ARABIC_CHARACTER
27
+ case 'hebrew':
28
+ return HEBREW_CHARACTER
29
+ case 'thai':
30
+ return THAI_CHARACTER
31
+ case 'devanagari':
32
+ return DEVANAGARI_CHARACTER
23
33
  case 'cjk-jp':
24
34
  return KANA_CHARACTER
25
35
  case 'cjk-kr':
@@ -32,7 +42,10 @@ function scriptCharRegex(script: FontFallbackScript): RegExp {
32
42
  }
33
43
 
34
44
  export function textHasFallbackScript(text: string): boolean {
35
- return ARABIC_CHARACTER.test(text) || CJK_CHAR_RE.test(text)
45
+ return (
46
+ LETTER_FALLBACK_SCRIPTS.some((script) => scriptCharRegex(script).test(text)) ||
47
+ CJK_CHAR_RE.test(text)
48
+ )
36
49
  }
37
50
 
38
51
  function styleForCharacter(
@@ -87,7 +100,9 @@ export function textFallbackScriptsWithoutCoverage(node: SceneNode): FontFallbac
87
100
 
88
101
  export function textNeededFallbackScripts(node: SceneNode): FontFallbackScript[] {
89
102
  const scripts = new Set<FontFallbackScript>()
90
- if (textNeedsFallbackScript(node, 'arabic')) scripts.add('arabic')
103
+ for (const script of LETTER_FALLBACK_SCRIPTS) {
104
+ if (textNeedsFallbackScript(node, script)) scripts.add(script)
105
+ }
91
106
 
92
107
  let missingIdeograph = false
93
108
  let missingTraditionalIdeograph = false
@@ -5,10 +5,32 @@ import {
5
5
  CJK_GOOGLE_FONTS
6
6
  } from '#core/constants'
7
7
 
8
- export type FontFallbackScript = 'cjk' | 'cjk-sc' | 'cjk-tc' | 'cjk-jp' | 'cjk-kr' | 'arabic'
8
+ export type FontFallbackScript =
9
+ | 'cjk'
10
+ | 'cjk-sc'
11
+ | 'cjk-tc'
12
+ | 'cjk-jp'
13
+ | 'cjk-kr'
14
+ | 'arabic'
15
+ | 'hebrew'
16
+ | 'thai'
17
+ | 'devanagari'
18
+
19
+ /** The fallbacks for scripts written in letters, which the renderer tries before the CJK ones. */
20
+ export const LETTER_FALLBACK_SCRIPTS = [
21
+ 'arabic',
22
+ 'hebrew',
23
+ 'thai',
24
+ 'devanagari'
25
+ ] as const satisfies readonly FontFallbackScript[]
26
+
27
+ export type LetterFallbackScript = (typeof LETTER_FALLBACK_SCRIPTS)[number]
9
28
 
10
29
  /** Characters of the writing systems that need a fallback font, by their Unicode script. */
11
30
  export const ARABIC_CHARACTER = /\p{Script=Arabic}/u
31
+ export const HEBREW_CHARACTER = /\p{Script=Hebrew}/u
32
+ export const THAI_CHARACTER = /\p{Script=Thai}/u
33
+ export const DEVANAGARI_CHARACTER = /\p{Script=Devanagari}/u
12
34
  export const HANGUL_CHARACTER = /\p{Script=Hangul}/u
13
35
  export const KANA_CHARACTER = /[\p{Script=Hiragana}\p{Script=Katakana}]/u
14
36
  export const HAN_CHARACTER = /\p{Script=Han}/u
@@ -23,6 +45,9 @@ export function fontFallbackScriptForCharacter(
23
45
  language?: string | null
24
46
  ): FontFallbackScript | null {
25
47
  if (ARABIC_CHARACTER.test(char)) return 'arabic'
48
+ if (HEBREW_CHARACTER.test(char)) return 'hebrew'
49
+ if (THAI_CHARACTER.test(char)) return 'thai'
50
+ if (DEVANAGARI_CHARACTER.test(char)) return 'devanagari'
26
51
  if (HANGUL_CHARACTER.test(char)) return 'cjk-kr'
27
52
  if (KANA_CHARACTER.test(char)) return 'cjk-jp'
28
53
  const languageScript = cjkFallbackScriptForLanguage(language)
@@ -72,7 +97,7 @@ export function cjkLocalFallbackFamilies(userAgent?: string): string[] {
72
97
  return [...CJK_FALLBACK_FAMILIES_LINUX]
73
98
  }
74
99
 
75
- function platformCJKFamilies(
100
+ function platformFamilies(
76
101
  userAgent: string | undefined,
77
102
  families: Record<'mac' | 'windows' | 'linux', string[]>
78
103
  ): string[] {
@@ -88,25 +113,25 @@ function cjkScriptLocalFallbackFamilies(
88
113
  ): string[] {
89
114
  switch (script) {
90
115
  case 'cjk-tc':
91
- return platformCJKFamilies(userAgent, {
116
+ return platformFamilies(userAgent, {
92
117
  mac: ['PingFang TC', 'Heiti TC', 'PingFang SC', 'Hiragino Sans'],
93
118
  windows: ['Microsoft JhengHei', 'Microsoft YaHei', 'Microsoft YaHei UI', 'SimSun'],
94
119
  linux: ['Noto Sans CJK TC', 'Noto Sans CJK SC', 'Droid Sans Fallback']
95
120
  })
96
121
  case 'cjk-jp':
97
- return platformCJKFamilies(userAgent, {
122
+ return platformFamilies(userAgent, {
98
123
  mac: ['Hiragino Sans', 'PingFang SC', 'PingFang TC'],
99
124
  windows: ['Yu Gothic', 'Microsoft YaHei', 'Microsoft JhengHei'],
100
125
  linux: ['Noto Sans CJK JP', 'Noto Sans CJK SC', 'Droid Sans Fallback']
101
126
  })
102
127
  case 'cjk-kr':
103
- return platformCJKFamilies(userAgent, {
128
+ return platformFamilies(userAgent, {
104
129
  mac: ['Apple SD Gothic Neo', 'PingFang SC', 'Hiragino Sans'],
105
130
  windows: ['Malgun Gothic', 'Microsoft YaHei', 'Yu Gothic'],
106
131
  linux: ['Noto Sans CJK KR', 'Noto Sans CJK SC', 'Droid Sans Fallback']
107
132
  })
108
133
  default:
109
- return platformCJKFamilies(userAgent, {
134
+ return platformFamilies(userAgent, {
110
135
  mac: ['PingFang SC', 'Heiti SC', 'PingFang TC', 'Hiragino Sans'],
111
136
  windows: ['Microsoft YaHei', 'Microsoft YaHei UI', 'SimHei', 'SimSun'],
112
137
  linux: ['Noto Sans CJK SC', 'WenQuanYi Micro Hei', 'Droid Sans Fallback']
@@ -114,6 +139,52 @@ function cjkScriptLocalFallbackFamilies(
114
139
  }
115
140
  }
116
141
 
142
+ /**
143
+ * Installed fonts that draw Hebrew, Thai, and Devanagari on each system, then the Noto family
144
+ * Google Fonts serves for it, so text in these scripts draws without bundling their fonts.
145
+ */
146
+ const LETTER_SCRIPT_FAMILIES: Record<
147
+ Exclude<LetterFallbackScript, 'arabic'>,
148
+ { local: Record<'mac' | 'windows' | 'linux', string[]>; remote: string[] }
149
+ > = {
150
+ hebrew: {
151
+ local: {
152
+ mac: ['Arial Hebrew', 'Lucida Grande'],
153
+ windows: ['Segoe UI', 'Arial', 'David'],
154
+ linux: ['Noto Sans Hebrew', 'DejaVu Sans']
155
+ },
156
+ remote: ['Noto Sans Hebrew']
157
+ },
158
+ thai: {
159
+ local: {
160
+ mac: ['Thonburi', 'Sukhumvit Set', 'Ayuthaya'],
161
+ windows: ['Leelawadee UI', 'Tahoma'],
162
+ linux: ['Noto Sans Thai', 'Loma']
163
+ },
164
+ remote: ['Noto Sans Thai']
165
+ },
166
+ devanagari: {
167
+ local: {
168
+ mac: ['Kohinoor Devanagari', 'Devanagari Sangam MN', 'Devanagari MT'],
169
+ windows: ['Nirmala UI', 'Mangal'],
170
+ linux: ['Noto Sans Devanagari', 'Lohit Devanagari']
171
+ },
172
+ remote: ['Noto Sans Devanagari']
173
+ }
174
+ }
175
+
176
+ function letterScriptEntry(
177
+ script: Exclude<LetterFallbackScript, 'arabic'>,
178
+ userAgent?: string
179
+ ): FontFallbackManifestEntry {
180
+ const families = LETTER_SCRIPT_FAMILIES[script]
181
+ return {
182
+ script,
183
+ localFamilies: [...platformFamilies(userAgent, families.local)],
184
+ remoteFamilies: [...families.remote]
185
+ }
186
+ }
187
+
117
188
  const CJK_SCRIPT_REMOTE_FAMILIES: Record<
118
189
  Extract<FontFallbackScript, 'cjk-sc' | 'cjk-tc' | 'cjk-jp' | 'cjk-kr'>,
119
190
  string[]
@@ -157,7 +228,10 @@ export function fontFallbackManifest(
157
228
  script: 'arabic',
158
229
  localFamilies: [...ARABIC_LOCAL_FALLBACK_FAMILIES],
159
230
  remoteFamilies: [...ARABIC_REMOTE_FALLBACK_FAMILIES]
160
- }
231
+ },
232
+ hebrew: letterScriptEntry('hebrew', userAgent),
233
+ thai: letterScriptEntry('thai', userAgent),
234
+ devanagari: letterScriptEntry('devanagari', userAgent)
161
235
  }
162
236
  }
163
237