@m3ui-vue/m3ui-vue 1.2.2 → 1.3.1

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 (241) hide show
  1. package/dist/API.md +3010 -376
  2. package/dist/MButton-Bl_BSUep.js +172 -0
  3. package/dist/MButton-Bl_BSUep.js.map +1 -0
  4. package/dist/MDialog-D_Yik8ou.js +173 -0
  5. package/dist/MDialog-D_Yik8ou.js.map +1 -0
  6. package/dist/MIconButton-DoNotQ9v.js +282 -0
  7. package/dist/MIconButton-DoNotQ9v.js.map +1 -0
  8. package/dist/MListItem-BxifHJdU.js +824 -0
  9. package/dist/MListItem-BxifHJdU.js.map +1 -0
  10. package/dist/MLoadingIndicator-BxbFhSX6.js +115 -0
  11. package/dist/MLoadingIndicator-BxbFhSX6.js.map +1 -0
  12. package/dist/MMenuItem-Bih0h4-n.js +604 -0
  13. package/dist/MMenuItem-Bih0h4-n.js.map +1 -0
  14. package/dist/MProgressBar-BCoruxA-.js +184 -0
  15. package/dist/MProgressBar-BCoruxA-.js.map +1 -0
  16. package/dist/MSeekBar-BeN9QcB3.js +435 -0
  17. package/dist/MSeekBar-BeN9QcB3.js.map +1 -0
  18. package/dist/MSegmentedButton-DL9JtJFX.js +65 -0
  19. package/dist/MSegmentedButton-DL9JtJFX.js.map +1 -0
  20. package/dist/MSlider-BVSbJlGS.js +691 -0
  21. package/dist/MSlider-BVSbJlGS.js.map +1 -0
  22. package/dist/MSpinner-C2SUnWk4.js +65 -0
  23. package/dist/MSpinner-C2SUnWk4.js.map +1 -0
  24. package/dist/MTextField-CEkWSZ2v.js +296 -0
  25. package/dist/MTextField-CEkWSZ2v.js.map +1 -0
  26. package/dist/MTooltip-CodJGEO-.js +756 -0
  27. package/dist/MTooltip-CodJGEO-.js.map +1 -0
  28. package/dist/_storageKeys-CYXsUBwK.js +18 -0
  29. package/dist/_storageKeys-CYXsUBwK.js.map +1 -0
  30. package/dist/_useEscapeStack-CjndQhqg.js +19 -0
  31. package/dist/_useEscapeStack-CjndQhqg.js.map +1 -0
  32. package/dist/api.json +1 -1
  33. package/dist/chart.js +1 -1
  34. package/dist/code-editor.js +1 -1
  35. package/dist/components/MAudioPlayer.vue.d.ts +115 -0
  36. package/dist/components/MBadge.vue.d.ts +1 -1
  37. package/dist/components/MBottomSheetScaffold.vue.d.ts +1 -1
  38. package/dist/components/MBox.vue.d.ts +1 -1
  39. package/dist/components/MButton.vue.d.ts +1 -1
  40. package/dist/components/MConfirmDialog.vue.d.ts +1 -1
  41. package/dist/components/MCopyButton.vue.d.ts +2 -2
  42. package/dist/components/MDataTable.vue.d.ts +2 -2
  43. package/dist/components/MDialog.vue.d.ts +2 -2
  44. package/dist/components/MDivider.vue.d.ts +1 -1
  45. package/dist/components/MDragHandle.vue.d.ts +1 -1
  46. package/dist/components/MDrawCanvas.vue.d.ts +150 -0
  47. package/dist/components/MDrawLayers.vue.d.ts +3 -0
  48. package/dist/components/MDrawToolbar.vue.d.ts +34 -0
  49. package/dist/components/MExportButton.vue.d.ts +1 -1
  50. package/dist/components/MFab.vue.d.ts +2 -2
  51. package/dist/components/MFabMenu.vue.d.ts +2 -2
  52. package/dist/components/MFixed.vue.d.ts +1 -1
  53. package/dist/components/MFlex.vue.d.ts +2 -2
  54. package/dist/components/MFloorPlan.vue.d.ts +248 -0
  55. package/dist/components/MFloorPlanInspector.vue.d.ts +56 -0
  56. package/dist/components/MFloorPlanToolbar.vue.d.ts +51 -0
  57. package/dist/components/MFlow.vue.d.ts +318 -0
  58. package/dist/components/MFlowControls.vue.d.ts +33 -0
  59. package/dist/components/MFlowHandle.vue.d.ts +16 -0
  60. package/dist/components/MFlowMinimap.vue.d.ts +22 -0
  61. package/dist/components/MFlowNode.vue.d.ts +35 -0
  62. package/dist/components/MGantt.vue.d.ts +141 -0
  63. package/dist/components/MGanttControls.vue.d.ts +32 -0
  64. package/dist/components/MIconButton.vue.d.ts +3 -1
  65. package/dist/components/MImageCropper.vue.d.ts +110 -0
  66. package/dist/components/MInfiniteScroll.vue.d.ts +1 -1
  67. package/dist/components/MLightbox.vue.d.ts +2 -2
  68. package/dist/components/MList.vue.d.ts +1 -1
  69. package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
  70. package/dist/components/MMapControl.vue.d.ts +1 -1
  71. package/dist/components/MMapSearch.vue.d.ts +1 -1
  72. package/dist/components/MMaskField.vue.d.ts +12 -1
  73. package/dist/components/MMenu.vue.d.ts +1 -1
  74. package/dist/components/MNavigationDrawer.vue.d.ts +1 -1
  75. package/dist/components/MNumberField.vue.d.ts +11 -2
  76. package/dist/components/MPdfViewer.vue.d.ts +174 -0
  77. package/dist/components/MPullToRefresh.vue.d.ts +1 -1
  78. package/dist/components/MResourceTimeline.vue.d.ts +149 -0
  79. package/dist/components/MScheduler.vue.d.ts +3 -3
  80. package/dist/components/MScheme.vue.d.ts +1 -1
  81. package/dist/components/MSearchField.vue.d.ts +1 -1
  82. package/dist/components/MSeekBar.vue.d.ts +1 -1
  83. package/dist/components/MSimpleCarousel.vue.d.ts +1 -1
  84. package/dist/components/MSkeleton.vue.d.ts +1 -1
  85. package/dist/components/MSlider.vue.d.ts +2 -2
  86. package/dist/components/MSparkline.vue.d.ts +1 -1
  87. package/dist/components/MSplitter.vue.d.ts +1 -1
  88. package/dist/components/MStack.vue.d.ts +2 -2
  89. package/dist/components/MSwipeToDismiss.vue.d.ts +1 -1
  90. package/dist/components/MSwitch.vue.d.ts +1 -1
  91. package/dist/components/MTable.vue.d.ts +1 -1
  92. package/dist/components/MTagInput.vue.d.ts +12 -1
  93. package/dist/components/MTextField.vue.d.ts +13 -2
  94. package/dist/components/MToolbar.vue.d.ts +1 -1
  95. package/dist/components/MTree.vue.d.ts +2 -2
  96. package/dist/components/MVideoPlayer.vue.d.ts +253 -0
  97. package/dist/components/MVirtualTable.vue.d.ts +1 -1
  98. package/dist/components/MWindow.vue.d.ts +3 -3
  99. package/dist/components/_MFlowEdge.vue.d.ts +9 -0
  100. package/dist/components/_MFlowEdgeLabel.vue.d.ts +9 -0
  101. package/dist/components/_MFlowNodeShell.vue.d.ts +9 -0
  102. package/dist/components/_MPdfPage.vue.d.ts +76 -0
  103. package/dist/components/_MTimeBackground.vue.d.ts +14 -0
  104. package/dist/components/_MTimeHeader.vue.d.ts +20 -0
  105. package/dist/composables/_flowPanel.d.ts +2 -0
  106. package/dist/composables/_flowSlot.d.ts +5 -0
  107. package/dist/composables/_ganttTime.d.ts +41 -0
  108. package/dist/composables/_useTimeAxis.d.ts +90 -0
  109. package/dist/composables/drawGeometry.d.ts +119 -0
  110. package/dist/composables/flowFormats.d.ts +79 -0
  111. package/dist/composables/flowLayout.d.ts +45 -0
  112. package/dist/composables/flowPaths.d.ts +53 -0
  113. package/dist/composables/ganttFormats.d.ts +52 -0
  114. package/dist/composables/ganttSchedule.d.ts +99 -0
  115. package/dist/composables/useDrawing.d.ts +44 -0
  116. package/dist/composables/useFloorPlan.d.ts +58 -0
  117. package/dist/composables/useFlow.d.ts +266 -0
  118. package/dist/composables/useGantt.d.ts +87 -0
  119. package/dist/composables/useLocale.d.ts +204 -0
  120. package/dist/composables/useMediaPlayer.d.ts +101 -0
  121. package/dist/composables/usePdf.d.ts +119 -0
  122. package/dist/composables/useResourceTimeline.d.ts +70 -0
  123. package/dist/draw.d.ts +7 -0
  124. package/dist/draw.js +1702 -0
  125. package/dist/draw.js.map +1 -0
  126. package/dist/flow.d.ts +14 -0
  127. package/dist/flow.js +3547 -0
  128. package/dist/flow.js.map +1 -0
  129. package/dist/flowPaths-CUc6X373.js +161 -0
  130. package/dist/flowPaths-CUc6X373.js.map +1 -0
  131. package/dist/gantt.d.ts +12 -0
  132. package/dist/gantt.js +3575 -0
  133. package/dist/gantt.js.map +1 -0
  134. package/dist/index.d.ts +9 -0
  135. package/dist/locales/index.js +1435 -7
  136. package/dist/locales/index.js.map +1 -1
  137. package/dist/m3ui-vue.css +1 -1
  138. package/dist/m3ui.js +8248 -7637
  139. package/dist/m3ui.js.map +1 -1
  140. package/dist/map.js +113 -108
  141. package/dist/map.js.map +1 -1
  142. package/dist/media.d.ts +5 -0
  143. package/dist/media.js +1311 -0
  144. package/dist/media.js.map +1 -0
  145. package/dist/pdf.d.ts +4 -0
  146. package/dist/pdf.js +1871 -0
  147. package/dist/pdf.js.map +1 -0
  148. package/dist/rich-text-editor.js +28 -26
  149. package/dist/rich-text-editor.js.map +1 -1
  150. package/dist/styles.css +1 -1
  151. package/dist/{useLocale-JYxDDeXC.js → useLocale-CaWwi-b6.js} +206 -2
  152. package/dist/useLocale-CaWwi-b6.js.map +1 -0
  153. package/dist/{useThemeTokens-y0HZpyhL.js → useThemeTokens-DQQcaWbE.js} +40 -54
  154. package/dist/useThemeTokens-DQQcaWbE.js.map +1 -0
  155. package/package.json +29 -5
  156. package/src/__tests__/MAudioPlayer.test.ts +75 -0
  157. package/src/__tests__/MDrawCanvas.test.ts +118 -0
  158. package/src/__tests__/MFloorPlan.test.ts +299 -0
  159. package/src/__tests__/MFlow.test.ts +590 -0
  160. package/src/__tests__/MGantt.test.ts +130 -0
  161. package/src/__tests__/MImageCropper.test.ts +89 -0
  162. package/src/__tests__/MPdfViewer.test.ts +235 -0
  163. package/src/__tests__/MResourceTimeline.test.ts +102 -0
  164. package/src/__tests__/MVideoPlayer.test.ts +201 -0
  165. package/src/__tests__/drawGeometry.test.ts +58 -0
  166. package/src/__tests__/fieldExpose.test.ts +30 -0
  167. package/src/__tests__/flowFormats.test.ts +185 -0
  168. package/src/__tests__/flowLayout.test.ts +113 -0
  169. package/src/__tests__/ganttFormats.test.ts +76 -0
  170. package/src/__tests__/ganttSchedule.test.ts +136 -0
  171. package/src/__tests__/pdfMock.ts +83 -0
  172. package/src/__tests__/useMediaPlayer.test.ts +182 -0
  173. package/src/__tests__/usePdf.test.ts +102 -0
  174. package/src/components/MAudioPlayer.vue +319 -0
  175. package/src/components/MDrawCanvas.vue +819 -0
  176. package/src/components/MDrawLayers.vue +114 -0
  177. package/src/components/MDrawToolbar.vue +171 -0
  178. package/src/components/MFloorPlan.vue +1080 -0
  179. package/src/components/MFloorPlanInspector.vue +169 -0
  180. package/src/components/MFloorPlanToolbar.vue +128 -0
  181. package/src/components/MFlow.vue +2060 -0
  182. package/src/components/MFlowControls.vue +110 -0
  183. package/src/components/MFlowHandle.vue +60 -0
  184. package/src/components/MFlowMinimap.vue +134 -0
  185. package/src/components/MFlowNode.vue +98 -0
  186. package/src/components/MGantt.vue +1609 -0
  187. package/src/components/MGanttControls.vue +77 -0
  188. package/src/components/MIconButton.vue +3 -1
  189. package/src/components/MImageCropper.vue +507 -0
  190. package/src/components/MMaskField.vue +13 -0
  191. package/src/components/MNumberField.vue +13 -0
  192. package/src/components/MPdfViewer.vue +1270 -0
  193. package/src/components/MResourceTimeline.vue +1020 -0
  194. package/src/components/MTagInput.vue +13 -0
  195. package/src/components/MTextField.vue +16 -0
  196. package/src/components/MVideoPlayer.vue +839 -0
  197. package/src/components/_MFlowEdge.vue +100 -0
  198. package/src/components/_MFlowEdgeLabel.vue +56 -0
  199. package/src/components/_MFlowNodeShell.vue +111 -0
  200. package/src/components/_MPdfPage.vue +456 -0
  201. package/src/components/_MTimeBackground.vue +16 -0
  202. package/src/components/_MTimeHeader.vue +31 -0
  203. package/src/composables/_flowPanel.ts +13 -0
  204. package/src/composables/_flowSlot.ts +6 -0
  205. package/src/composables/_ganttTime.ts +200 -0
  206. package/src/composables/_useTimeAxis.ts +224 -0
  207. package/src/composables/drawGeometry.ts +405 -0
  208. package/src/composables/flowFormats.ts +916 -0
  209. package/src/composables/flowLayout.ts +297 -0
  210. package/src/composables/flowPaths.ts +239 -0
  211. package/src/composables/ganttFormats.ts +366 -0
  212. package/src/composables/ganttSchedule.ts +403 -0
  213. package/src/composables/useDrawing.ts +53 -0
  214. package/src/composables/useFloorPlan.ts +73 -0
  215. package/src/composables/useFlow.ts +250 -0
  216. package/src/composables/useGantt.ts +92 -0
  217. package/src/composables/useLocale.ts +410 -0
  218. package/src/composables/useMediaPlayer.ts +406 -0
  219. package/src/composables/usePdf.ts +456 -0
  220. package/src/composables/useResourceTimeline.ts +76 -0
  221. package/src/draw.ts +25 -0
  222. package/src/flow.ts +26 -0
  223. package/src/gantt.ts +12 -0
  224. package/src/index.ts +9 -0
  225. package/src/locales/de.ts +204 -0
  226. package/src/locales/es.ts +204 -0
  227. package/src/locales/fr.ts +204 -0
  228. package/src/locales/ja.ts +204 -0
  229. package/src/locales/ko.ts +204 -0
  230. package/src/locales/pt.ts +204 -0
  231. package/src/locales/zh.ts +204 -0
  232. package/src/media.ts +5 -0
  233. package/src/pdf.ts +15 -0
  234. package/dist/MMenuItem-D4Gx_l3F.js +0 -929
  235. package/dist/MMenuItem-D4Gx_l3F.js.map +0 -1
  236. package/dist/MSlider-Dx_S3vJ9.js +0 -1506
  237. package/dist/MSlider-Dx_S3vJ9.js.map +0 -1
  238. package/dist/MTextField-Bz8JIsnl.js +0 -633
  239. package/dist/MTextField-Bz8JIsnl.js.map +0 -1
  240. package/dist/useLocale-JYxDDeXC.js.map +0 -1
  241. package/dist/useThemeTokens-y0HZpyhL.js.map +0 -1
@@ -0,0 +1,405 @@
1
+ // The Drawing family's data and geometry: strokes with pressure turned into filled outlines, shapes, text, hit tests,
2
+ // the stroke-cutting eraser, moving and scaling, and SVG export. Pure functions, no Vue, so they work on the server
3
+ // and in tests, and an app can render or process a drawing without the canvas.
4
+
5
+ /** A point of a stroke: x, y, and the pressure (0–1) */
6
+ export type DrawPoint = [x: number, y: number, pressure: number]
7
+ export type DrawPen = 'pen' | 'brush' | 'marker' | 'highlighter'
8
+ export type DrawShapeKind = 'line' | 'arrow' | 'rect' | 'ellipse'
9
+
10
+ interface DrawItemBase {
11
+ id: string
12
+ /** The layer it's on (`undefined`: the first one) */
13
+ layer?: string
14
+ /** The page it's on, in `mode="pages"` (its coordinates are then relative to that page) */
15
+ page?: number
16
+ color: string
17
+ }
18
+ export interface DrawStroke extends DrawItemBase {
19
+ type: 'stroke'
20
+ pen: DrawPen
21
+ /** Width at full pressure, in drawing units */
22
+ size: number
23
+ points: DrawPoint[]
24
+ }
25
+ export interface DrawShape extends DrawItemBase {
26
+ type: 'shape'
27
+ shape: DrawShapeKind
28
+ from: [number, number]
29
+ to: [number, number]
30
+ size: number
31
+ /** Fill of a rect or an ellipse (`null`/absent: none) */
32
+ fill?: string | null
33
+ }
34
+ export interface DrawText extends DrawItemBase {
35
+ type: 'text'
36
+ /** Top-left corner */
37
+ at: [number, number]
38
+ text: string
39
+ /** Font size */
40
+ size: number
41
+ }
42
+ export type DrawItem = DrawStroke | DrawShape | DrawText
43
+
44
+ export interface DrawLayer {
45
+ id: string
46
+ name: string
47
+ visible?: boolean
48
+ locked?: boolean
49
+ }
50
+ export interface DrawBox { x: number; y: number; width: number; height: number }
51
+
52
+ let seq = 0
53
+ export const drawId = () => `d${Date.now().toString(36)}${(seq++).toString(36)}`
54
+
55
+ /** How each pen behaves: how much pressure thins it, how much its ends taper, how opaque it is */
56
+ export const PENS: Record<DrawPen, { thinning: number; taper: number; opacity: number }> = {
57
+ pen: { thinning: 0.6, taper: 0.4, opacity: 1 },
58
+ brush: { thinning: 0.85, taper: 1, opacity: 1 },
59
+ marker: { thinning: 0, taper: 0, opacity: 1 },
60
+ highlighter: { thinning: 0, taper: 0, opacity: 0.35 },
61
+ }
62
+
63
+ const r2 = (n: number) => Math.round(n * 100) / 100
64
+
65
+ /**
66
+ * Smooths raw input points: each one moves part of the way toward the next sample (`smoothing` 0 keeps them as they
67
+ * are, 1 follows lazily), and points closer than a fraction of the size are merged. Pressure is kept.
68
+ */
69
+ export function smoothPoints(points: DrawPoint[], smoothing = 0.5, size = 4): DrawPoint[] {
70
+ if (points.length < 2) return points.slice()
71
+ const t = 1 - Math.min(0.9, Math.max(0, smoothing) * 0.85)
72
+ const min = Math.max(0.5, size * 0.1)
73
+ const out: DrawPoint[] = [points[0]!]
74
+ let [x, y] = points[0]!
75
+ for (let i = 1; i < points.length; i++) {
76
+ const p = points[i]!
77
+ x += (p[0] - x) * t
78
+ y += (p[1] - y) * t
79
+ const last = out[out.length - 1]!
80
+ if (Math.hypot(x - last[0], y - last[1]) >= min || i === points.length - 1) out.push([x, y, p[2]])
81
+ }
82
+ // Ends where the pointer did
83
+ const end = points[points.length - 1]!
84
+ out[out.length - 1] = [end[0], end[1], end[2]]
85
+ return out
86
+ }
87
+
88
+ /** Pressure made up from speed, for a mouse or a finger: slow is heavy, fast is light */
89
+ export function simulatePressure(points: DrawPoint[], size: number): DrawPoint[] {
90
+ let p = 0.5
91
+ return points.map((pt, i) => {
92
+ if (!i) return [pt[0], pt[1], 0.5]
93
+ const prev = points[i - 1]!
94
+ const speed = Math.min(1, Math.hypot(pt[0] - prev[0], pt[1] - prev[1]) / (size * 2))
95
+ p += (1 - speed - p) * 0.3
96
+ return [pt[0], pt[1], Math.max(0.2, Math.min(1, p))]
97
+ })
98
+ }
99
+
100
+ /** The radius along a stroke: pressure thins it, and the ends taper over a distance tied to its size */
101
+ function radii(points: DrawPoint[], size: number, pen: DrawPen) {
102
+ const { thinning, taper } = PENS[pen]
103
+ const dist: number[] = [0]
104
+ for (let i = 1; i < points.length; i++) dist.push(dist[i - 1]! + Math.hypot(points[i]![0] - points[i - 1]![0], points[i]![1] - points[i - 1]![1]))
105
+ const total = dist[dist.length - 1]!
106
+ const tl = Math.min(total / 3, size * 3 * taper)
107
+ return points.map((p, i) => {
108
+ let r = (size / 2) * (1 - thinning * (1 - p[2]))
109
+ if (tl > 0) {
110
+ const ease = (v: number) => 1 - (1 - v) * (1 - v)
111
+ r *= Math.max(0.15, Math.min(1, ease(Math.min(1, dist[i]! / tl)), ease(Math.min(1, (total - dist[i]!) / tl))))
112
+ }
113
+ return Math.max(0.25, r)
114
+ })
115
+ }
116
+
117
+ /** A stroke as the SVG path of its filled outline (round caps, its width following pressure) */
118
+ export function strokePath(s: Pick<DrawStroke, 'points' | 'size' | 'pen'>): string {
119
+ const pts = s.points
120
+ if (!pts.length) return ''
121
+ const rs = radii(pts, s.size, s.pen)
122
+ if (pts.length === 1 || pts.every((p) => Math.hypot(p[0] - pts[0]![0], p[1] - pts[0]![1]) < 0.5)) {
123
+ const [x, y] = pts[0]!
124
+ const r = r2(Math.max(...rs))
125
+ return `M${r2(x - r)} ${r2(y)}a${r} ${r} 0 1 0 ${r * 2} 0a${r} ${r} 0 1 0 ${-r * 2} 0Z`
126
+ }
127
+ const left: [number, number][] = []
128
+ const right: [number, number][] = []
129
+ for (let i = 0; i < pts.length; i++) {
130
+ const a = pts[Math.max(0, i - 1)]!
131
+ const b = pts[Math.min(pts.length - 1, i + 1)]!
132
+ let dx = b[0] - a[0]
133
+ let dy = b[1] - a[1]
134
+ const len = Math.hypot(dx, dy) || 1
135
+ dx /= len
136
+ dy /= len
137
+ const r = rs[i]!
138
+ left.push([pts[i]![0] - dy * r, pts[i]![1] + dx * r])
139
+ right.push([pts[i]![0] + dy * r, pts[i]![1] - dx * r])
140
+ }
141
+ // Each side as quadratic curves through the midpoints, joined by round caps
142
+ const side = (ps: [number, number][]) => {
143
+ let d = ''
144
+ for (let i = 1; i < ps.length - 1; i++) {
145
+ const [x, y] = ps[i]!
146
+ const [nx, ny] = ps[i + 1]!
147
+ d += `Q${r2(x)} ${r2(y)} ${r2((x + nx) / 2)} ${r2((y + ny) / 2)}`
148
+ }
149
+ const last = ps[ps.length - 1]!
150
+ return `${d}L${r2(last[0])} ${r2(last[1])}`
151
+ }
152
+ const rEnd = r2(rs[rs.length - 1]!)
153
+ const rStart = r2(rs[0]!)
154
+ const back = right.slice().reverse()
155
+ return `M${r2(left[0]![0])} ${r2(left[0]![1])}${side(left)}A${rEnd} ${rEnd} 0 0 1 ${r2(back[0]![0])} ${r2(back[0]![1])}${side(back)}A${rStart} ${rStart} 0 0 1 ${r2(left[0]![0])} ${r2(left[0]![1])}Z`
156
+ }
157
+
158
+ /** A shape's outline as an SVG path (stroked), plus an arrow's head (filled) */
159
+ export function shapePath(s: Pick<DrawShape, 'shape' | 'from' | 'to' | 'size'>): { d: string; head?: string } {
160
+ const [x1, y1] = s.from
161
+ const [x2, y2] = s.to
162
+ switch (s.shape) {
163
+ case 'rect': {
164
+ const x = Math.min(x1, x2)
165
+ const y = Math.min(y1, y2)
166
+ return { d: `M${r2(x)} ${r2(y)}h${r2(Math.abs(x2 - x1))}v${r2(Math.abs(y2 - y1))}h${r2(-Math.abs(x2 - x1))}Z` }
167
+ }
168
+ case 'ellipse': {
169
+ const rx = Math.abs(x2 - x1) / 2
170
+ const ry = Math.abs(y2 - y1) / 2
171
+ const cx = (x1 + x2) / 2
172
+ const cy = (y1 + y2) / 2
173
+ return { d: `M${r2(cx - rx)} ${r2(cy)}a${r2(rx)} ${r2(ry)} 0 1 0 ${r2(rx * 2)} 0a${r2(rx)} ${r2(ry)} 0 1 0 ${r2(-rx * 2)} 0Z` }
174
+ }
175
+ case 'arrow': {
176
+ const len = Math.hypot(x2 - x1, y2 - y1) || 1
177
+ const ux = (x2 - x1) / len
178
+ const uy = (y2 - y1) / len
179
+ const h = Math.min(len * 0.6, Math.max(10, s.size * 4))
180
+ const w = h * 0.55
181
+ const bx = x2 - ux * h
182
+ const by = y2 - uy * h
183
+ return {
184
+ d: `M${r2(x1)} ${r2(y1)}L${r2(bx + ux * 0.5)} ${r2(by + uy * 0.5)}`,
185
+ head: `M${r2(x2)} ${r2(y2)}L${r2(bx - uy * w)} ${r2(by + ux * w)}L${r2(bx + uy * w)} ${r2(by - ux * w)}Z`,
186
+ }
187
+ }
188
+ default:
189
+ return { d: `M${r2(x1)} ${r2(y1)}L${r2(x2)} ${r2(y2)}` }
190
+ }
191
+ }
192
+
193
+ /** Holding Shift: lines snap to 45°, rects and ellipses become squares and circles */
194
+ export function constrain(kind: DrawShapeKind, from: [number, number], to: [number, number]): [number, number] {
195
+ const dx = to[0] - from[0]
196
+ const dy = to[1] - from[1]
197
+ if (kind === 'rect' || kind === 'ellipse') {
198
+ const m = Math.max(Math.abs(dx), Math.abs(dy))
199
+ return [from[0] + Math.sign(dx || 1) * m, from[1] + Math.sign(dy || 1) * m]
200
+ }
201
+ const a = Math.round(Math.atan2(dy, dx) / (Math.PI / 4)) * (Math.PI / 4)
202
+ const len = Math.hypot(dx, dy)
203
+ return [from[0] + Math.cos(a) * len, from[1] + Math.sin(a) * len]
204
+ }
205
+
206
+ export const LINE_HEIGHT = 1.25
207
+ /** A text's box, estimated from its characters (good enough to hit and select it) */
208
+ export function textBox(t: Pick<DrawText, 'at' | 'text' | 'size'>): DrawBox {
209
+ const lines = t.text.split('\n')
210
+ return { x: t.at[0], y: t.at[1], width: Math.max(t.size, ...lines.map((l) => l.length * t.size * 0.56)), height: lines.length * t.size * LINE_HEIGHT }
211
+ }
212
+
213
+ /** An item's bounds */
214
+ export function itemBox(item: DrawItem): DrawBox {
215
+ if (item.type === 'text') return textBox(item)
216
+ const pts: [number, number][] = item.type === 'stroke' ? item.points.map((p) => [p[0], p[1]]) : [item.from, item.to]
217
+ const pad = item.size / 2
218
+ let x1 = Infinity
219
+ let y1 = Infinity
220
+ let x2 = -Infinity
221
+ let y2 = -Infinity
222
+ for (const [x, y] of pts) {
223
+ x1 = Math.min(x1, x)
224
+ y1 = Math.min(y1, y)
225
+ x2 = Math.max(x2, x)
226
+ y2 = Math.max(y2, y)
227
+ }
228
+ return { x: x1 - pad, y: y1 - pad, width: x2 - x1 + pad * 2, height: y2 - y1 + pad * 2 }
229
+ }
230
+ export function unionBox(boxes: DrawBox[]): DrawBox | null {
231
+ if (!boxes.length) return null
232
+ const x1 = Math.min(...boxes.map((b) => b.x))
233
+ const y1 = Math.min(...boxes.map((b) => b.y))
234
+ const x2 = Math.max(...boxes.map((b) => b.x + b.width))
235
+ const y2 = Math.max(...boxes.map((b) => b.y + b.height))
236
+ return { x: x1, y: y1, width: x2 - x1, height: y2 - y1 }
237
+ }
238
+
239
+ function segDist(px: number, py: number, ax: number, ay: number, bx: number, by: number) {
240
+ const dx = bx - ax
241
+ const dy = by - ay
242
+ const len = dx * dx + dy * dy
243
+ const t = len ? Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / len)) : 0
244
+ return Math.hypot(ax + t * dx - px, ay + t * dy - py)
245
+ }
246
+ /** Points along a shape's outline, for hit tests and lasso */
247
+ function shapePoints(s: DrawShape): [number, number][] {
248
+ const [x1, y1] = s.from
249
+ const [x2, y2] = s.to
250
+ if (s.shape === 'rect') return [[x1, y1], [x2, y1], [x2, y2], [x1, y2], [x1, y1]]
251
+ if (s.shape === 'ellipse') {
252
+ const cx = (x1 + x2) / 2
253
+ const cy = (y1 + y2) / 2
254
+ return Array.from({ length: 33 }, (_, i) => [cx + (Math.abs(x2 - x1) / 2) * Math.cos((i / 32) * Math.PI * 2), cy + (Math.abs(y2 - y1) / 2) * Math.sin((i / 32) * Math.PI * 2)])
255
+ }
256
+ return [s.from, s.to]
257
+ }
258
+
259
+ /** Whether a point (with a tolerance) touches an item */
260
+ export function hitItem(item: DrawItem, x: number, y: number, tolerance = 4): boolean {
261
+ if (item.type === 'text') {
262
+ const b = textBox(item)
263
+ return x >= b.x - tolerance && x <= b.x + b.width + tolerance && y >= b.y - tolerance && y <= b.y + b.height + tolerance
264
+ }
265
+ if (item.type === 'shape' && item.fill && (item.shape === 'rect' || item.shape === 'ellipse')) {
266
+ const b = itemBox(item)
267
+ if (x >= b.x && x <= b.x + b.width && y >= b.y && y <= b.y + b.height) {
268
+ if (item.shape === 'rect') return true
269
+ const rx = b.width / 2
270
+ const ry = b.height / 2
271
+ if (((x - b.x - rx) / rx) ** 2 + ((y - b.y - ry) / ry) ** 2 <= 1) return true
272
+ }
273
+ }
274
+ const pts: [number, number][] = item.type === 'stroke' ? item.points.map((p) => [p[0], p[1]]) : shapePoints(item)
275
+ const reach = item.size / 2 + tolerance
276
+ if (pts.length === 1) return Math.hypot(pts[0]![0] - x, pts[0]![1] - y) <= reach
277
+ for (let i = 1; i < pts.length; i++) if (segDist(x, y, pts[i - 1]![0], pts[i - 1]![1], pts[i]![0], pts[i]![1]) <= reach) return true
278
+ return false
279
+ }
280
+
281
+ /**
282
+ * The stroke-cutting eraser: the parts of a stroke within `r` of (x, y) go, what's left splits into separate
283
+ * strokes. Returns the item unchanged when it isn't touched, `[]` when it's all gone. Shapes and text go whole.
284
+ */
285
+ export function eraseAt(item: DrawItem, x: number, y: number, r: number): DrawItem[] {
286
+ if (item.type !== 'stroke') return hitItem(item, x, y, r) ? [] : [item]
287
+ const reach = r + item.size / 2
288
+ const pts = item.points
289
+ if (pts.length === 1) return Math.hypot(pts[0]![0] - x, pts[0]![1] - y) <= reach ? [] : [item]
290
+ let touched = false
291
+ const runs: DrawPoint[][] = []
292
+ let run: DrawPoint[] = []
293
+ pts.forEach((p, i) => {
294
+ const prev = pts[i - 1]
295
+ const gone = Math.hypot(p[0] - x, p[1] - y) <= reach
296
+ const crossed = !!prev && run.length > 0 && segDist(x, y, prev[0], prev[1], p[0], p[1]) <= reach
297
+ if (gone || crossed) {
298
+ touched = true
299
+ if (run.length > 1) runs.push(run)
300
+ run = gone ? [] : [p]
301
+ } else run.push(p)
302
+ })
303
+ if (run.length > 1) runs.push(run)
304
+ if (!touched) return [item]
305
+ return runs.map((points, i) => ({ ...item, id: i ? drawId() : item.id, points }))
306
+ }
307
+
308
+ function inPolygon(x: number, y: number, poly: [number, number][]) {
309
+ let inside = false
310
+ for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
311
+ const [xi, yi] = poly[i]!
312
+ const [xj, yj] = poly[j]!
313
+ if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) inside = !inside
314
+ }
315
+ return inside
316
+ }
317
+ /** Whether a lasso (a closed polygon) takes an item: most of its points inside */
318
+ export function inLasso(item: DrawItem, poly: [number, number][]): boolean {
319
+ if (poly.length < 3) return false
320
+ let pts: [number, number][]
321
+ if (item.type === 'stroke') pts = item.points.map((p) => [p[0], p[1]])
322
+ else if (item.type === 'shape') pts = shapePoints(item)
323
+ else {
324
+ const b = textBox(item)
325
+ pts = [[b.x, b.y], [b.x + b.width, b.y], [b.x, b.y + b.height], [b.x + b.width, b.y + b.height]]
326
+ }
327
+ const n = pts.filter(([x, y]) => inPolygon(x, y, poly)).length
328
+ return n >= Math.max(1, pts.length * 0.5)
329
+ }
330
+
331
+ /** An item moved by (dx, dy) and scaled by (sx, sy) around (ox, oy) */
332
+ export function transformItem(item: DrawItem, t: { dx?: number; dy?: number; sx?: number; sy?: number; ox?: number; oy?: number }): DrawItem {
333
+ const { dx = 0, dy = 0, sx = 1, sy = 1, ox = 0, oy = 0 } = t
334
+ const X = (x: number) => ox + (x - ox) * sx + dx
335
+ const Y = (y: number) => oy + (y - oy) * sy + dy
336
+ const k = Math.sqrt(Math.abs(sx * sy))
337
+ if (item.type === 'stroke') return { ...item, size: item.size * k, points: item.points.map(([x, y, p]) => [X(x), Y(y), p]) }
338
+ if (item.type === 'shape') return { ...item, size: item.size * k, from: [X(item.from[0]), Y(item.from[1])], to: [X(item.to[0]), Y(item.to[1])] }
339
+ return { ...item, size: item.size * k, at: [X(item.at[0]), Y(item.at[1])] }
340
+ }
341
+
342
+ const esc = (s: string) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
343
+ /** One item as SVG markup */
344
+ export function itemSvg(item: DrawItem, fontFamily = 'sans-serif'): string {
345
+ if (item.type === 'stroke') {
346
+ const o = PENS[item.pen].opacity
347
+ return `<path d="${strokePath(item)}" fill="${esc(item.color)}"${o < 1 ? ` fill-opacity="${o}"` : ''}/>`
348
+ }
349
+ if (item.type === 'shape') {
350
+ const { d, head } = shapePath(item)
351
+ const fill = item.fill && (item.shape === 'rect' || item.shape === 'ellipse') ? esc(item.fill) : 'none'
352
+ return `<path d="${d}" fill="${fill}" stroke="${esc(item.color)}" stroke-width="${item.size}" stroke-linecap="round" stroke-linejoin="round"/>${head ? `<path d="${head}" fill="${esc(item.color)}"/>` : ''}`
353
+ }
354
+ const lines = item.text.split('\n')
355
+ return `<text x="${item.at[0]}" y="${item.at[1]}" font-size="${item.size}" font-family="${esc(fontFamily)}" fill="${esc(item.color)}">${lines
356
+ .map((l, i) => `<tspan x="${item.at[0]}" dy="${i ? item.size * LINE_HEIGHT : item.size * 0.95}">${esc(l) || ' '}</tspan>`)
357
+ .join('')}</text>`
358
+ }
359
+
360
+ export interface DrawSvgOptions {
361
+ /** The area to export (default: the items' bounds plus `padding`) */
362
+ box?: DrawBox
363
+ padding?: number
364
+ /** A background fill (default: none, transparent) */
365
+ background?: string
366
+ /** Only the items of this page */
367
+ page?: number
368
+ /** Leave out these layers (hidden ones, say) */
369
+ hiddenLayers?: string[]
370
+ /** The layers' order, bottom first (items of other layers go first) */
371
+ layers?: string[]
372
+ fontFamily?: string
373
+ }
374
+ /** Items in drawing order: by layer, then as they come */
375
+ export function sortByLayer(items: DrawItem[], layers?: string[]) {
376
+ if (!layers?.length) return items
377
+ const rank = (i: DrawItem) => (i.layer === undefined ? 0 : Math.max(0, layers.indexOf(i.layer)))
378
+ return items.map((it, i) => [it, i] as const).sort((a, b) => rank(a[0]) - rank(b[0]) || a[1] - b[1]).map(([it]) => it)
379
+ }
380
+ /** A drawing (or one page of it) as a standalone SVG document */
381
+ export function toDrawSvg(items: DrawItem[], opts: DrawSvgOptions = {}): string {
382
+ const shown = sortByLayer(items, opts.layers).filter((i) => (opts.page === undefined || (i.page ?? 0) === opts.page) && !opts.hiddenLayers?.includes(i.layer ?? ''))
383
+ const pad = opts.padding ?? 16
384
+ const b = opts.box ?? unionBox(shown.map(itemBox)) ?? { x: 0, y: 0, width: 1, height: 1 }
385
+ const box = opts.box ? b : { x: b.x - pad, y: b.y - pad, width: b.width + pad * 2, height: b.height + pad * 2 }
386
+ const bg = opts.background ? `<rect x="${box.x}" y="${box.y}" width="${box.width}" height="${box.height}" fill="${esc(opts.background)}"/>` : ''
387
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${r2(box.x)} ${r2(box.y)} ${r2(box.width)} ${r2(box.height)}" width="${Math.ceil(box.width)}" height="${Math.ceil(box.height)}">${bg}${shown.map((i) => itemSvg(i, opts.fontFamily)).join('')}</svg>`
388
+ }
389
+
390
+ /** An SVG document rendered to a PNG blob (in a browser), `scale` times its size */
391
+ export async function svgToPng(svg: string, scale = 2): Promise<Blob> {
392
+ const url = URL.createObjectURL(new Blob([svg], { type: 'image/svg+xml' }))
393
+ try {
394
+ const img = new Image()
395
+ img.src = url
396
+ await img.decode()
397
+ const canvas = document.createElement('canvas')
398
+ canvas.width = Math.ceil(img.width * scale)
399
+ canvas.height = Math.ceil(img.height * scale)
400
+ canvas.getContext('2d')!.drawImage(img, 0, 0, canvas.width, canvas.height)
401
+ return await new Promise((resolve, reject) => canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('PNG export failed'))), 'image/png'))
402
+ } finally {
403
+ URL.revokeObjectURL(url)
404
+ }
405
+ }