@m3ui-vue/m3ui-vue 1.2.2 → 1.3.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 (235) hide show
  1. package/dist/API.md +2776 -351
  2. package/dist/MButton-CuNzpoxm.js +172 -0
  3. package/dist/MButton-CuNzpoxm.js.map +1 -0
  4. package/dist/MDialog-HxEL-m58.js +173 -0
  5. package/dist/MDialog-HxEL-m58.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-DnPtv-hY.js +824 -0
  9. package/dist/MListItem-DnPtv-hY.js.map +1 -0
  10. package/dist/MLoadingIndicator-BQMNX6Dx.js +115 -0
  11. package/dist/MLoadingIndicator-BQMNX6Dx.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-LURE1Ah7.js +435 -0
  17. package/dist/MSeekBar-LURE1Ah7.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-_J4IS2_z.js +691 -0
  21. package/dist/MSlider-_J4IS2_z.js.map +1 -0
  22. package/dist/MSpinner-BhkMYKOM.js +65 -0
  23. package/dist/MSpinner-BhkMYKOM.js.map +1 -0
  24. package/dist/MTextField-CkzwNBWS.js +296 -0
  25. package/dist/MTextField-CkzwNBWS.js.map +1 -0
  26. package/dist/MTooltip-B67ZDSGp.js +756 -0
  27. package/dist/MTooltip-B67ZDSGp.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 +148 -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 +160 -0
  55. package/dist/components/MFlow.vue.d.ts +305 -0
  56. package/dist/components/MFlowControls.vue.d.ts +33 -0
  57. package/dist/components/MFlowHandle.vue.d.ts +16 -0
  58. package/dist/components/MFlowMinimap.vue.d.ts +22 -0
  59. package/dist/components/MFlowNode.vue.d.ts +35 -0
  60. package/dist/components/MGantt.vue.d.ts +141 -0
  61. package/dist/components/MGanttControls.vue.d.ts +32 -0
  62. package/dist/components/MIconButton.vue.d.ts +3 -1
  63. package/dist/components/MImageCropper.vue.d.ts +110 -0
  64. package/dist/components/MInfiniteScroll.vue.d.ts +1 -1
  65. package/dist/components/MLightbox.vue.d.ts +2 -2
  66. package/dist/components/MList.vue.d.ts +1 -1
  67. package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
  68. package/dist/components/MMapControl.vue.d.ts +1 -1
  69. package/dist/components/MMapSearch.vue.d.ts +1 -1
  70. package/dist/components/MMaskField.vue.d.ts +12 -1
  71. package/dist/components/MMenu.vue.d.ts +1 -1
  72. package/dist/components/MNavigationDrawer.vue.d.ts +1 -1
  73. package/dist/components/MNumberField.vue.d.ts +11 -2
  74. package/dist/components/MPdfViewer.vue.d.ts +174 -0
  75. package/dist/components/MPullToRefresh.vue.d.ts +1 -1
  76. package/dist/components/MResourceTimeline.vue.d.ts +149 -0
  77. package/dist/components/MScheduler.vue.d.ts +3 -3
  78. package/dist/components/MScheme.vue.d.ts +1 -1
  79. package/dist/components/MSearchField.vue.d.ts +1 -1
  80. package/dist/components/MSeekBar.vue.d.ts +1 -1
  81. package/dist/components/MSimpleCarousel.vue.d.ts +1 -1
  82. package/dist/components/MSkeleton.vue.d.ts +1 -1
  83. package/dist/components/MSlider.vue.d.ts +2 -2
  84. package/dist/components/MSparkline.vue.d.ts +1 -1
  85. package/dist/components/MSplitter.vue.d.ts +1 -1
  86. package/dist/components/MStack.vue.d.ts +2 -2
  87. package/dist/components/MSwipeToDismiss.vue.d.ts +1 -1
  88. package/dist/components/MSwitch.vue.d.ts +1 -1
  89. package/dist/components/MTable.vue.d.ts +1 -1
  90. package/dist/components/MTagInput.vue.d.ts +12 -1
  91. package/dist/components/MTextField.vue.d.ts +13 -2
  92. package/dist/components/MToolbar.vue.d.ts +1 -1
  93. package/dist/components/MTree.vue.d.ts +2 -2
  94. package/dist/components/MVideoPlayer.vue.d.ts +253 -0
  95. package/dist/components/MVirtualTable.vue.d.ts +1 -1
  96. package/dist/components/MWindow.vue.d.ts +3 -3
  97. package/dist/components/_MFlowEdge.vue.d.ts +9 -0
  98. package/dist/components/_MFlowEdgeLabel.vue.d.ts +9 -0
  99. package/dist/components/_MFlowNodeShell.vue.d.ts +9 -0
  100. package/dist/components/_MPdfPage.vue.d.ts +76 -0
  101. package/dist/components/_MTimeBackground.vue.d.ts +14 -0
  102. package/dist/components/_MTimeHeader.vue.d.ts +20 -0
  103. package/dist/composables/_flowPanel.d.ts +2 -0
  104. package/dist/composables/_flowSlot.d.ts +5 -0
  105. package/dist/composables/_ganttTime.d.ts +41 -0
  106. package/dist/composables/_useTimeAxis.d.ts +90 -0
  107. package/dist/composables/drawGeometry.d.ts +119 -0
  108. package/dist/composables/flowFormats.d.ts +79 -0
  109. package/dist/composables/flowLayout.d.ts +45 -0
  110. package/dist/composables/flowPaths.d.ts +53 -0
  111. package/dist/composables/ganttFormats.d.ts +52 -0
  112. package/dist/composables/ganttSchedule.d.ts +99 -0
  113. package/dist/composables/useDrawing.d.ts +44 -0
  114. package/dist/composables/useFlow.d.ts +266 -0
  115. package/dist/composables/useGantt.d.ts +87 -0
  116. package/dist/composables/useLocale.d.ts +163 -0
  117. package/dist/composables/useMediaPlayer.d.ts +101 -0
  118. package/dist/composables/usePdf.d.ts +119 -0
  119. package/dist/composables/useResourceTimeline.d.ts +70 -0
  120. package/dist/draw.d.ts +7 -0
  121. package/dist/draw.js +1700 -0
  122. package/dist/draw.js.map +1 -0
  123. package/dist/flow.d.ts +14 -0
  124. package/dist/flow.js +3542 -0
  125. package/dist/flow.js.map +1 -0
  126. package/dist/flowPaths-CUc6X373.js +161 -0
  127. package/dist/flowPaths-CUc6X373.js.map +1 -0
  128. package/dist/gantt.d.ts +12 -0
  129. package/dist/gantt.js +3575 -0
  130. package/dist/gantt.js.map +1 -0
  131. package/dist/index.d.ts +4 -0
  132. package/dist/locales/index.js +1148 -7
  133. package/dist/locales/index.js.map +1 -1
  134. package/dist/m3ui-vue.css +1 -1
  135. package/dist/m3ui.js +7286 -7654
  136. package/dist/m3ui.js.map +1 -1
  137. package/dist/map.js +113 -108
  138. package/dist/map.js.map +1 -1
  139. package/dist/media.d.ts +5 -0
  140. package/dist/media.js +1311 -0
  141. package/dist/media.js.map +1 -0
  142. package/dist/pdf.d.ts +4 -0
  143. package/dist/pdf.js +1871 -0
  144. package/dist/pdf.js.map +1 -0
  145. package/dist/rich-text-editor.js +28 -26
  146. package/dist/rich-text-editor.js.map +1 -1
  147. package/dist/styles.css +1 -1
  148. package/dist/{useLocale-JYxDDeXC.js → useLocale-BpGxplLV.js} +165 -2
  149. package/dist/useLocale-BpGxplLV.js.map +1 -0
  150. package/dist/{useThemeTokens-y0HZpyhL.js → useThemeTokens-DQQcaWbE.js} +40 -54
  151. package/dist/useThemeTokens-DQQcaWbE.js.map +1 -0
  152. package/package.json +29 -5
  153. package/src/__tests__/MAudioPlayer.test.ts +75 -0
  154. package/src/__tests__/MDrawCanvas.test.ts +110 -0
  155. package/src/__tests__/MFloorPlan.test.ts +97 -0
  156. package/src/__tests__/MFlow.test.ts +575 -0
  157. package/src/__tests__/MGantt.test.ts +130 -0
  158. package/src/__tests__/MImageCropper.test.ts +89 -0
  159. package/src/__tests__/MPdfViewer.test.ts +235 -0
  160. package/src/__tests__/MResourceTimeline.test.ts +102 -0
  161. package/src/__tests__/MVideoPlayer.test.ts +201 -0
  162. package/src/__tests__/drawGeometry.test.ts +58 -0
  163. package/src/__tests__/fieldExpose.test.ts +30 -0
  164. package/src/__tests__/flowFormats.test.ts +185 -0
  165. package/src/__tests__/flowLayout.test.ts +113 -0
  166. package/src/__tests__/ganttFormats.test.ts +76 -0
  167. package/src/__tests__/ganttSchedule.test.ts +136 -0
  168. package/src/__tests__/pdfMock.ts +83 -0
  169. package/src/__tests__/useMediaPlayer.test.ts +182 -0
  170. package/src/__tests__/usePdf.test.ts +102 -0
  171. package/src/components/MAudioPlayer.vue +319 -0
  172. package/src/components/MDrawCanvas.vue +813 -0
  173. package/src/components/MDrawLayers.vue +114 -0
  174. package/src/components/MDrawToolbar.vue +171 -0
  175. package/src/components/MFloorPlan.vue +581 -0
  176. package/src/components/MFlow.vue +2049 -0
  177. package/src/components/MFlowControls.vue +110 -0
  178. package/src/components/MFlowHandle.vue +60 -0
  179. package/src/components/MFlowMinimap.vue +134 -0
  180. package/src/components/MFlowNode.vue +98 -0
  181. package/src/components/MGantt.vue +1609 -0
  182. package/src/components/MGanttControls.vue +77 -0
  183. package/src/components/MIconButton.vue +3 -1
  184. package/src/components/MImageCropper.vue +507 -0
  185. package/src/components/MMaskField.vue +13 -0
  186. package/src/components/MNumberField.vue +13 -0
  187. package/src/components/MPdfViewer.vue +1269 -0
  188. package/src/components/MResourceTimeline.vue +1020 -0
  189. package/src/components/MTagInput.vue +13 -0
  190. package/src/components/MTextField.vue +16 -0
  191. package/src/components/MVideoPlayer.vue +839 -0
  192. package/src/components/_MFlowEdge.vue +100 -0
  193. package/src/components/_MFlowEdgeLabel.vue +56 -0
  194. package/src/components/_MFlowNodeShell.vue +111 -0
  195. package/src/components/_MPdfPage.vue +456 -0
  196. package/src/components/_MTimeBackground.vue +16 -0
  197. package/src/components/_MTimeHeader.vue +31 -0
  198. package/src/composables/_flowPanel.ts +13 -0
  199. package/src/composables/_flowSlot.ts +6 -0
  200. package/src/composables/_ganttTime.ts +200 -0
  201. package/src/composables/_useTimeAxis.ts +224 -0
  202. package/src/composables/drawGeometry.ts +405 -0
  203. package/src/composables/flowFormats.ts +916 -0
  204. package/src/composables/flowLayout.ts +297 -0
  205. package/src/composables/flowPaths.ts +239 -0
  206. package/src/composables/ganttFormats.ts +366 -0
  207. package/src/composables/ganttSchedule.ts +403 -0
  208. package/src/composables/useDrawing.ts +53 -0
  209. package/src/composables/useFlow.ts +250 -0
  210. package/src/composables/useGantt.ts +92 -0
  211. package/src/composables/useLocale.ts +328 -0
  212. package/src/composables/useMediaPlayer.ts +406 -0
  213. package/src/composables/usePdf.ts +456 -0
  214. package/src/composables/useResourceTimeline.ts +76 -0
  215. package/src/draw.ts +25 -0
  216. package/src/flow.ts +26 -0
  217. package/src/gantt.ts +12 -0
  218. package/src/index.ts +4 -0
  219. package/src/locales/de.ts +163 -0
  220. package/src/locales/es.ts +163 -0
  221. package/src/locales/fr.ts +163 -0
  222. package/src/locales/ja.ts +163 -0
  223. package/src/locales/ko.ts +163 -0
  224. package/src/locales/pt.ts +163 -0
  225. package/src/locales/zh.ts +163 -0
  226. package/src/media.ts +5 -0
  227. package/src/pdf.ts +15 -0
  228. package/dist/MMenuItem-D4Gx_l3F.js +0 -929
  229. package/dist/MMenuItem-D4Gx_l3F.js.map +0 -1
  230. package/dist/MSlider-Dx_S3vJ9.js +0 -1506
  231. package/dist/MSlider-Dx_S3vJ9.js.map +0 -1
  232. package/dist/MTextField-Bz8JIsnl.js +0 -633
  233. package/dist/MTextField-Bz8JIsnl.js.map +0 -1
  234. package/dist/useLocale-JYxDDeXC.js.map +0 -1
  235. package/dist/useThemeTokens-y0HZpyhL.js.map +0 -1
@@ -0,0 +1,297 @@
1
+ // Automatic layouts for MFlow, without dependencies: a tidy tree (org charts, mind maps) and a layered layout for
2
+ // directed graphs (workflows, pipelines; the Sugiyama method, simplified). Pure functions over nodes, edges and
3
+ // sizes that return positions, so a layout of your own (dagre, elkjs) can take their place: anything with the
4
+ // `FlowLayout` signature.
5
+ import type { FlowEdge, FlowNode, FlowXY } from './useFlow'
6
+
7
+ export type FlowLayoutDirection = 'TB' | 'BT' | 'LR' | 'RL'
8
+
9
+ export interface FlowLayoutOptions {
10
+ /** TB (top to bottom, the default for a vertical MFlow), BT, LR (default for a horizontal one) or RL */
11
+ direction?: FlowLayoutDirection
12
+ /** Space between neighbors in a level, px (default 40) */
13
+ gap?: number
14
+ /** Space between levels, px (default 80) */
15
+ levelGap?: number
16
+ /** Node sizes; MFlow passes the measured ones */
17
+ sizes?: Map<string, { width: number; height: number }> | Record<string, { width: number; height: number }>
18
+ /** Size of a node missing from `sizes` (default 180 × 60) */
19
+ defaultSize?: { width: number; height: number }
20
+ }
21
+
22
+ /** Top left corners by node id */
23
+ export type FlowLayoutResult = Record<string, FlowXY>
24
+
25
+ /** A layout: MFlow's `layout` prop and `layout()` take one */
26
+ export type FlowLayout = (
27
+ nodes: FlowNode[],
28
+ edges: FlowEdge[],
29
+ options: FlowLayoutOptions & { sizes: Map<string, { width: number; height: number }>; direction: FlowLayoutDirection },
30
+ ) => FlowLayoutResult | Promise<FlowLayoutResult>
31
+
32
+ interface Prepared {
33
+ ids: string[]
34
+ size: (id: string) => { breadth: number; depth: number; width: number; height: number }
35
+ out: Map<string, string[]>
36
+ inc: Map<string, string[]>
37
+ horizontal: boolean
38
+ reversed: boolean
39
+ gap: number
40
+ levelGap: number
41
+ }
42
+
43
+ // Top-level, visible nodes only; edges into a group's children count as edges into the group
44
+ function prepare(nodes: FlowNode[], edges: FlowEdge[], o: FlowLayoutOptions): Prepared {
45
+ const byId = new Map(nodes.map((n) => [n.id, n]))
46
+ const top = (id: string) => {
47
+ let n = byId.get(id)
48
+ for (let i = 0; n?.parentId && i < 100; i++) n = byId.get(n.parentId)
49
+ return n
50
+ }
51
+ const ids = nodes.filter((n) => !n.parentId && !n.hidden).map((n) => n.id)
52
+ const keep = new Set(ids)
53
+ const out = new Map<string, string[]>(ids.map((id) => [id, []]))
54
+ const inc = new Map<string, string[]>(ids.map((id) => [id, []]))
55
+ for (const e of edges) {
56
+ if (e.hidden) continue
57
+ const s = top(e.source)?.id
58
+ const t = top(e.target)?.id
59
+ if (!s || !t || s === t || !keep.has(s) || !keep.has(t)) continue
60
+ if (out.get(s)!.includes(t)) continue
61
+ out.get(s)!.push(t)
62
+ inc.get(t)!.push(s)
63
+ }
64
+ const dir = o.direction ?? 'TB'
65
+ const horizontal = dir === 'LR' || dir === 'RL'
66
+ const sizes = o.sizes instanceof Map ? o.sizes : new Map(Object.entries(o.sizes ?? {}))
67
+ const def = o.defaultSize ?? { width: 180, height: 60 }
68
+ return {
69
+ ids,
70
+ size: (id) => {
71
+ const s = sizes.get(id) ?? (byId.get(id)?.width && byId.get(id)?.height ? { width: byId.get(id)!.width!, height: byId.get(id)!.height! } : def)
72
+ return { ...s, breadth: horizontal ? s.height : s.width, depth: horizontal ? s.width : s.height }
73
+ },
74
+ out,
75
+ inc,
76
+ horizontal,
77
+ reversed: dir === 'BT' || dir === 'RL',
78
+ gap: o.gap ?? 40,
79
+ levelGap: o.levelGap ?? 80,
80
+ }
81
+ }
82
+
83
+ // Breadth/depth coordinates (centers along the level, the level's start) → top left corners
84
+ function place(p: Prepared, along: Map<string, number>, level: Map<string, number>): FlowLayoutResult {
85
+ const levels = Math.max(0, ...level.values()) + 1
86
+ const depthOf = Array<number>(levels).fill(0)
87
+ for (const id of p.ids) depthOf[level.get(id)!] = Math.max(depthOf[level.get(id)!]!, p.size(id).depth)
88
+ const start: number[] = []
89
+ let acc = 0
90
+ for (let l = 0; l < levels; l++) {
91
+ start.push(acc)
92
+ acc += depthOf[l]! + p.levelGap
93
+ }
94
+ const total = acc - p.levelGap
95
+ const res: FlowLayoutResult = {}
96
+ for (const id of p.ids) {
97
+ const s = p.size(id)
98
+ const l = level.get(id)!
99
+ // Centered within its level's band
100
+ let d = start[l]! + (depthOf[l]! - s.depth) / 2
101
+ if (p.reversed) d = total - d - s.depth
102
+ const b = along.get(id)! - s.breadth / 2
103
+ res[id] = p.horizontal ? { x: d, y: b } : { x: b, y: d }
104
+ }
105
+ return res
106
+ }
107
+
108
+ /**
109
+ * A tidy tree: each parent centered over its children, every subtree side by side without overlapping, levels
110
+ * aligned. Roots are the nodes nothing points to (in the nodes' order); a child's place among its siblings
111
+ * follows the edges' order. Extra parents and cycles are ignored (a node goes under the first parent reached).
112
+ */
113
+ export function layoutTree(nodes: FlowNode[], edges: FlowEdge[], options: FlowLayoutOptions = {}): FlowLayoutResult {
114
+ const p = prepare(nodes, edges, options)
115
+ const children = new Map<string, string[]>()
116
+ const seen = new Set<string>()
117
+ const roots = p.ids.filter((id) => !p.inc.get(id)!.length)
118
+ // A graph that's all cycles still needs a root
119
+ if (!roots.length && p.ids.length) roots.push(p.ids[0]!)
120
+ const level = new Map<string, number>()
121
+ const queue: string[] = []
122
+ const visit = (r: string) => {
123
+ if (seen.has(r)) return
124
+ seen.add(r)
125
+ level.set(r, 0)
126
+ queue.push(r)
127
+ while (queue.length) {
128
+ const id = queue.shift()!
129
+ const kids: string[] = []
130
+ for (const c of p.out.get(id)!) {
131
+ if (seen.has(c)) continue
132
+ seen.add(c)
133
+ level.set(c, level.get(id)! + 1)
134
+ kids.push(c)
135
+ queue.push(c)
136
+ }
137
+ children.set(id, kids)
138
+ }
139
+ }
140
+ roots.forEach(visit)
141
+ // Whatever only a cycle reaches becomes a root of its own
142
+ for (const id of p.ids) if (!seen.has(id)) { roots.push(id); visit(id) }
143
+
144
+ const width = new Map<string, number>()
145
+ const measure = (id: string): number => {
146
+ const kids = children.get(id)!
147
+ const kidsW = kids.reduce((s, c) => s + measure(c), 0) + Math.max(0, kids.length - 1) * p.gap
148
+ const w = Math.max(p.size(id).breadth, kidsW)
149
+ width.set(id, w)
150
+ return w
151
+ }
152
+ const along = new Map<string, number>()
153
+ const assign = (id: string, left: number) => {
154
+ const w = width.get(id)!
155
+ along.set(id, left + w / 2)
156
+ const kids = children.get(id)!
157
+ const kidsW = kids.reduce((s, c) => s + width.get(c)!, 0) + Math.max(0, kids.length - 1) * p.gap
158
+ let x = left + (w - kidsW) / 2
159
+ for (const c of kids) {
160
+ assign(c, x)
161
+ x += width.get(c)! + p.gap
162
+ }
163
+ }
164
+ let x = 0
165
+ for (const r of roots) {
166
+ measure(r)
167
+ assign(r, x)
168
+ x += width.get(r)! + p.gap * 2
169
+ }
170
+ return place(p, along, level)
171
+ }
172
+
173
+ /**
174
+ * A layered layout for directed graphs: every edge points one level onwards where it can (longest path from the
175
+ * sources), the order within a level reduces crossings (barycenter sweeps), and nodes line up with what they're
176
+ * connected to without overlapping. Edges that close a cycle are laid out as if reversed.
177
+ */
178
+ export function layoutLayered(nodes: FlowNode[], edges: FlowEdge[], options: FlowLayoutOptions = {}): FlowLayoutResult {
179
+ const p = prepare(nodes, edges, options)
180
+ // Break cycles: drop edges back to a node still on the DFS stack
181
+ const state = new Map<string, 0 | 1 | 2>()
182
+ const fwd = new Map<string, string[]>(p.ids.map((id) => [id, []]))
183
+ const dfs = (id: string) => {
184
+ state.set(id, 1)
185
+ for (const t of p.out.get(id)!) {
186
+ if (state.get(t) === 1) continue
187
+ fwd.get(id)!.push(t)
188
+ if (!state.get(t)) dfs(t)
189
+ }
190
+ state.set(id, 2)
191
+ }
192
+ const sources = p.ids.filter((id) => !p.inc.get(id)!.length)
193
+ for (const id of [...sources, ...p.ids]) if (!state.get(id)) dfs(id)
194
+ const back = new Map<string, string[]>(p.ids.map((id) => [id, []]))
195
+ for (const [s, ts] of fwd) for (const t of ts) back.get(t)!.push(s)
196
+
197
+ // Longest path layering, in topological order
198
+ const level = new Map<string, number>()
199
+ const indeg = new Map(p.ids.map((id) => [id, back.get(id)!.length]))
200
+ const queue = p.ids.filter((id) => !indeg.get(id))
201
+ while (queue.length) {
202
+ const id = queue.shift()!
203
+ if (!level.has(id)) level.set(id, 0)
204
+ for (const t of fwd.get(id)!) {
205
+ level.set(t, Math.max(level.get(t) ?? 0, level.get(id)! + 1))
206
+ indeg.set(t, indeg.get(t)! - 1)
207
+ if (!indeg.get(t)) queue.push(t)
208
+ }
209
+ }
210
+ // Pull sources down next to what they feed (a lone input right before its consumer)
211
+ for (const id of p.ids) {
212
+ if (back.get(id)!.length || !fwd.get(id)!.length) continue
213
+ level.set(id, Math.min(...fwd.get(id)!.map((t) => level.get(t)!)) - 1)
214
+ }
215
+ const levels: string[][] = []
216
+ for (const id of p.ids) (levels[level.get(id)!] ??= []).push(id)
217
+ for (let l = 0; l < levels.length; l++) levels[l] ??= []
218
+
219
+ // An edge across several levels gets a placeholder in each level between, so the ordering and the spacing
220
+ // keep a lane open for it instead of running it underneath the nodes in between
221
+ const dummies = new Set<string>()
222
+ for (const [s, ts] of [...fwd]) {
223
+ for (const t of [...ts]) {
224
+ const ls = level.get(s)!
225
+ const lt = level.get(t)!
226
+ if (lt - ls <= 1) continue
227
+ ts.splice(ts.indexOf(t), 1)
228
+ let prev = s
229
+ for (let l = ls + 1; l < lt; l++) {
230
+ const d = `\u0000${s}>${t}#${l}`
231
+ dummies.add(d)
232
+ level.set(d, l)
233
+ levels[l]!.push(d)
234
+ fwd.set(d, [])
235
+ back.set(d, [prev])
236
+ fwd.get(prev)!.push(d)
237
+ prev = d
238
+ }
239
+ fwd.get(prev)!.push(t)
240
+ back.set(t, back.get(t)!.map((x) => (x === s ? prev : x)))
241
+ }
242
+ }
243
+ const LANE = 8
244
+ const sizeOf = (id: string) => (dummies.has(id) ? { breadth: LANE, depth: 0 } : p.size(id))
245
+
246
+ // Crossing reduction: order each level by the mean position of its neighbors in the previous one
247
+ const pos = new Map<string, number>()
248
+ const index = () => levels.forEach((ids) => ids.forEach((id, i) => pos.set(id, i)))
249
+ index()
250
+ const sweep = (down: boolean) => {
251
+ const range = down ? levels.map((_, i) => i).slice(1) : levels.map((_, i) => i).reverse().slice(1)
252
+ for (const l of range) {
253
+ const nb = down ? back : fwd
254
+ const bary = new Map(levels[l]!.map((id) => {
255
+ const ns = nb.get(id)!
256
+ return [id, ns.length ? ns.reduce((s, n) => s + pos.get(n)!, 0) / ns.length : pos.get(id)!]
257
+ }))
258
+ levels[l]!.sort((a, b) => bary.get(a)! - bary.get(b)!)
259
+ levels[l]!.forEach((id, i) => pos.set(id, i))
260
+ }
261
+ }
262
+ for (let i = 0; i < 4; i++) { sweep(true); sweep(false) }
263
+
264
+ // Coordinates along the level: packed first, then pulled toward their neighbors' centers, keeping the order
265
+ const along = new Map<string, number>()
266
+ for (const ids of levels) {
267
+ let x = 0
268
+ for (const id of ids) {
269
+ const b = sizeOf(id).breadth
270
+ along.set(id, x + b / 2)
271
+ x += b + p.gap
272
+ }
273
+ }
274
+ const relax = (ids: string[], nb: Map<string, string[]>) => {
275
+ const want = ids.map((id) => {
276
+ const ns = nb.get(id)!
277
+ return ns.length ? ns.reduce((s, n) => s + along.get(n)!, 0) / ns.length : along.get(id)!
278
+ })
279
+ // Where each wants to be, never closer than the gap to its neighbor: pushed rightwards in one pass and
280
+ // leftwards in another, then averaged (both keep the spacing, so their mean does too, without drifting)
281
+ const space = (i: number) => sizeOf(ids[i]!).breadth / 2 + p.gap + sizeOf(ids[i + 1]!).breadth / 2
282
+ const right = [...want]
283
+ for (let i = 1; i < ids.length; i++) right[i] = Math.max(right[i]!, right[i - 1]! + space(i - 1))
284
+ const left = [...want]
285
+ for (let i = ids.length - 2; i >= 0; i--) left[i] = Math.min(left[i]!, left[i + 1]! - space(i))
286
+ ids.forEach((id, i) => along.set(id, (right[i]! + left[i]!) / 2))
287
+ }
288
+ for (let it = 0; it < 3; it++) {
289
+ for (let l = 1; l < levels.length; l++) relax(levels[l]!, back)
290
+ for (let l = levels.length - 2; l >= 0; l--) relax(levels[l]!, fwd)
291
+ }
292
+ // Start at 0
293
+ let min = Infinity
294
+ for (const id of p.ids) min = Math.min(min, along.get(id)! - p.size(id).breadth / 2)
295
+ if (Number.isFinite(min)) for (const id of p.ids) along.set(id, along.get(id)! - min)
296
+ return place(p, along, level)
297
+ }
@@ -0,0 +1,239 @@
1
+ // Edge paths for MFlow, also exported for edges of your own (`#edge-<type>` slots). Every function takes the two
2
+ // ends in flow coordinates plus the side of its node each end leaves from, and returns an SVG path, the point
3
+ // where a label sits and the direction the path arrives in (for an arrowhead).
4
+
5
+ /** A side of a node: where a handle sits and the direction an edge leaves it in */
6
+ export type FlowPosition = 'top' | 'right' | 'bottom' | 'left'
7
+
8
+ export interface FlowEdgePathParams {
9
+ sourceX: number
10
+ sourceY: number
11
+ /** Default 'bottom' */
12
+ sourcePosition?: FlowPosition
13
+ targetX: number
14
+ targetY: number
15
+ /** Default 'top' */
16
+ targetPosition?: FlowPosition
17
+ }
18
+
19
+ export interface FlowEdgePath {
20
+ /** The SVG path (`d`) */
21
+ path: string
22
+ /** Where the label goes: the path's middle */
23
+ labelX: number
24
+ labelY: number
25
+ /** The direction the path leaves the source in, in radians (0 = right, π/2 = down) */
26
+ sourceAngle: number
27
+ /** The direction the path arrives at the target in, in radians: the way an arrowhead points */
28
+ targetAngle: number
29
+ }
30
+
31
+ const NORMAL: Record<FlowPosition, [number, number]> = {
32
+ top: [0, -1],
33
+ right: [1, 0],
34
+ bottom: [0, 1],
35
+ left: [-1, 0],
36
+ }
37
+ const angleOf = (x: number, y: number) => Math.atan2(y, x)
38
+ const r = (n: number) => Math.round(n * 100) / 100
39
+
40
+ /** A straight line between the ends */
41
+ export function getStraightPath(p: FlowEdgePathParams): FlowEdgePath {
42
+ const { sourceX: sx, sourceY: sy, targetX: tx, targetY: ty } = p
43
+ const a = angleOf(tx - sx, ty - sy)
44
+ return { path: `M${r(sx)},${r(sy)} L${r(tx)},${r(ty)}`, labelX: (sx + tx) / 2, labelY: (sy + ty) / 2, sourceAngle: a, targetAngle: a }
45
+ }
46
+
47
+ // React Flow's control point distance: half the distance along the side's direction, or a curve that grows with
48
+ // the square root of how far behind the node the other end is
49
+ function controlOffset(distance: number, curvature: number) {
50
+ return distance >= 0 ? 0.5 * distance : curvature * 25 * Math.sqrt(-distance)
51
+ }
52
+ function controlPoint(pos: FlowPosition, x1: number, y1: number, x2: number, y2: number, c: number): [number, number] {
53
+ switch (pos) {
54
+ case 'left': return [x1 - controlOffset(x1 - x2, c), y1]
55
+ case 'right': return [x1 + controlOffset(x2 - x1, c), y1]
56
+ case 'top': return [x1, y1 - controlOffset(y1 - y2, c)]
57
+ default: return [x1, y1 + controlOffset(y2 - y1, c)]
58
+ }
59
+ }
60
+
61
+ /** A cubic curve that leaves and arrives square to the nodes' sides. `curvature` (default 0.25) bends it more when the target is behind the source */
62
+ export function getBezierPath(p: FlowEdgePathParams & { curvature?: number }): FlowEdgePath {
63
+ const { sourceX: sx, sourceY: sy, targetX: tx, targetY: ty } = p
64
+ const sp = p.sourcePosition ?? 'bottom'
65
+ const tp = p.targetPosition ?? 'top'
66
+ const c = p.curvature ?? 0.25
67
+ const [c1x, c1y] = controlPoint(sp, sx, sy, tx, ty, c)
68
+ const [c2x, c2y] = controlPoint(tp, tx, ty, sx, sy, c)
69
+ const labelX = 0.125 * sx + 0.375 * c1x + 0.375 * c2x + 0.125 * tx
70
+ const labelY = 0.125 * sy + 0.375 * c1y + 0.375 * c2y + 0.125 * ty
71
+ return {
72
+ path: `M${r(sx)},${r(sy)} C${r(c1x)},${r(c1y)} ${r(c2x)},${r(c2y)} ${r(tx)},${r(ty)}`,
73
+ labelX,
74
+ labelY,
75
+ sourceAngle: angleOf(...NORMAL[sp]),
76
+ targetAngle: angleOf(-NORMAL[tp][0], -NORMAL[tp][1]),
77
+ }
78
+ }
79
+
80
+ type Pt = [number, number]
81
+
82
+ /** The corners of an orthogonal route between the ends, leaving and arriving square to the sides */
83
+ export function smoothStepPoints(p: FlowEdgePathParams & { offset?: number }): Pt[] {
84
+ const { sourceX: sx, sourceY: sy, targetX: tx, targetY: ty } = p
85
+ const sp = p.sourcePosition ?? 'bottom'
86
+ const tp = p.targetPosition ?? 'top'
87
+ const off = p.offset ?? 20
88
+ const [nsx, nsy] = NORMAL[sp]
89
+ const [ntx, nty] = NORMAL[tp]
90
+ const s1: Pt = [sx + nsx * off, sy + nsy * off]
91
+ const t1: Pt = [tx + ntx * off, ty + nty * off]
92
+ const sH = nsx !== 0
93
+ const tH = ntx !== 0
94
+ let mid: Pt[]
95
+ if (sH && tH) {
96
+ if (nsx === -ntx && (tx - sx) * nsx >= 2 * off) {
97
+ // Facing each other with room: one vertical run halfway
98
+ const mx = (sx + tx) / 2
99
+ mid = [[mx, sy], [mx, ty]]
100
+ } else if (nsx === ntx) {
101
+ // Both sides look the same way: go round past the farther one
102
+ const x = nsx > 0 ? Math.max(s1[0], t1[0]) : Math.min(s1[0], t1[0])
103
+ mid = [[x, sy], [x, ty]]
104
+ } else {
105
+ // Behind: out, across halfway between the ends, and back in
106
+ const my = (sy + ty) / 2
107
+ mid = [s1, [s1[0], my], [t1[0], my], t1]
108
+ }
109
+ } else if (!sH && !tH) {
110
+ if (nsy === -nty && (ty - sy) * nsy >= 2 * off) {
111
+ const my = (sy + ty) / 2
112
+ mid = [[sx, my], [tx, my]]
113
+ } else if (nsy === nty) {
114
+ const y = nsy > 0 ? Math.max(s1[1], t1[1]) : Math.min(s1[1], t1[1])
115
+ mid = [[sx, y], [tx, y]]
116
+ } else {
117
+ const mx = (sx + tx) / 2
118
+ mid = [s1, [mx, s1[1]], [mx, t1[1]], t1]
119
+ }
120
+ } else if (sH) {
121
+ // Leaves sideways, arrives from above or below: one corner when both legs go the right way
122
+ if ((tx - sx) * nsx >= off && (ty - sy) * -nty >= off) mid = [[tx, sy]]
123
+ else mid = [s1, [s1[0], t1[1]], t1]
124
+ } else {
125
+ if ((ty - sy) * nsy >= off && (tx - sx) * -ntx >= off) mid = [[sx, ty]]
126
+ else mid = [s1, [t1[0], s1[1]], t1]
127
+ }
128
+ const pts: Pt[] = [[sx, sy], ...mid, [tx, ty]]
129
+ // Drop repeated and collinear points
130
+ const out: Pt[] = []
131
+ for (const q of pts) {
132
+ const a = out[out.length - 1]
133
+ if (a && Math.abs(a[0] - q[0]) < 0.01 && Math.abs(a[1] - q[1]) < 0.01) continue
134
+ const b = out[out.length - 2]
135
+ if (a && b && ((Math.abs(b[0] - a[0]) < 0.01 && Math.abs(a[0] - q[0]) < 0.01) || (Math.abs(b[1] - a[1]) < 0.01 && Math.abs(a[1] - q[1]) < 0.01))) {
136
+ out[out.length - 1] = q
137
+ continue
138
+ }
139
+ out.push(q)
140
+ }
141
+ return out
142
+ }
143
+
144
+ function midpointAlong(pts: Pt[]): Pt {
145
+ let total = 0
146
+ for (let i = 1; i < pts.length; i++) total += Math.hypot(pts[i]![0] - pts[i - 1]![0], pts[i]![1] - pts[i - 1]![1])
147
+ let left = total / 2
148
+ for (let i = 1; i < pts.length; i++) {
149
+ const [ax, ay] = pts[i - 1]!
150
+ const [bx, by] = pts[i]!
151
+ const len = Math.hypot(bx - ax, by - ay)
152
+ if (left <= len && len > 0) return [ax + ((bx - ax) * left) / len, ay + ((by - ay) * left) / len]
153
+ left -= len
154
+ }
155
+ return pts[0] ?? [0, 0]
156
+ }
157
+
158
+ /** Right-angled steps with rounded corners (`borderRadius`, default 8; 0 gives sharp steps). `offset` (default 20) is how far it leaves a node before turning */
159
+ export function getSmoothStepPath(p: FlowEdgePathParams & { borderRadius?: number; offset?: number }): FlowEdgePath {
160
+ const pts = smoothStepPoints(p)
161
+ const radius = p.borderRadius ?? 8
162
+ let d = `M${r(pts[0]![0])},${r(pts[0]![1])}`
163
+ for (let i = 1; i < pts.length; i++) {
164
+ const [x, y] = pts[i]!
165
+ const next = pts[i + 1]
166
+ if (!next || radius <= 0) {
167
+ d += ` L${r(x)},${r(y)}`
168
+ continue
169
+ }
170
+ const [px, py] = pts[i - 1]!
171
+ const rad = Math.min(radius, Math.hypot(x - px, y - py) / 2, Math.hypot(next[0] - x, next[1] - y) / 2)
172
+ const ux = Math.sign(x - px)
173
+ const uy = Math.sign(y - py)
174
+ const vx = Math.sign(next[0] - x)
175
+ const vy = Math.sign(next[1] - y)
176
+ d += ` L${r(x - ux * rad)},${r(y - uy * rad)} Q${r(x)},${r(y)} ${r(x + vx * rad)},${r(y + vy * rad)}`
177
+ }
178
+ const [labelX, labelY] = midpointAlong(pts)
179
+ const a = pts[0]!
180
+ const b = pts[1] ?? a
181
+ const y = pts[pts.length - 2] ?? a
182
+ const z = pts[pts.length - 1]!
183
+ return { path: d, labelX, labelY, sourceAngle: angleOf(b[0] - a[0], b[1] - a[1]), targetAngle: angleOf(z[0] - y[0], z[1] - y[1]) }
184
+ }
185
+
186
+ /**
187
+ * Points along an edge's path, close enough together to measure how far something is from it (bezier curves are
188
+ * sampled; step paths are their corners)
189
+ */
190
+ export function edgePathPoints(type: string | undefined, p: FlowEdgePathParams): [number, number][] {
191
+ if (type === 'straight') return [[p.sourceX, p.sourceY], [p.targetX, p.targetY]]
192
+ if (type === 'smoothstep' || type === 'step') return smoothStepPoints(p)
193
+ const sp = p.sourcePosition ?? 'bottom'
194
+ const tp = p.targetPosition ?? 'top'
195
+ const [c1x, c1y] = controlPoint(sp, p.sourceX, p.sourceY, p.targetX, p.targetY, 0.25)
196
+ const [c2x, c2y] = controlPoint(tp, p.targetX, p.targetY, p.sourceX, p.sourceY, 0.25)
197
+ const out: [number, number][] = []
198
+ for (let i = 0; i <= 24; i++) {
199
+ const t = i / 24
200
+ const u = 1 - t
201
+ out.push([
202
+ u * u * u * p.sourceX + 3 * u * u * t * c1x + 3 * u * t * t * c2x + t * t * t * p.targetX,
203
+ u * u * u * p.sourceY + 3 * u * u * t * c1y + 3 * u * t * t * c2y + t * t * t * p.targetY,
204
+ ])
205
+ }
206
+ return out
207
+ }
208
+
209
+ /** The distance from a point to a polyline */
210
+ export function distanceToPoints(x: number, y: number, pts: [number, number][]): number {
211
+ let best = Infinity
212
+ for (let i = 1; i < pts.length; i++) {
213
+ const [ax, ay] = pts[i - 1]!
214
+ const [bx, by] = pts[i]!
215
+ const dx = bx - ax
216
+ const dy = by - ay
217
+ const len = dx * dx + dy * dy
218
+ const t = len ? Math.max(0, Math.min(1, ((x - ax) * dx + (y - ay) * dy) / len)) : 0
219
+ best = Math.min(best, Math.hypot(ax + t * dx - x, ay + t * dy - y))
220
+ }
221
+ return best
222
+ }
223
+
224
+ /** `getSmoothStepPath` with sharp corners */
225
+ export function getStepPath(p: FlowEdgePathParams & { offset?: number }): FlowEdgePath {
226
+ return getSmoothStepPath({ ...p, borderRadius: 0 })
227
+ }
228
+
229
+ /** An arrowhead's path at (x, y) pointing along `angle`: open (two strokes) or closed (a filled triangle's outline) */
230
+ export function arrowPath(x: number, y: number, angle: number, size = 10, closed = false): string {
231
+ const spread = 0.45
232
+ const ax = x - size * Math.cos(angle - spread)
233
+ const ay = y - size * Math.sin(angle - spread)
234
+ const bx = x - size * Math.cos(angle + spread)
235
+ const by = y - size * Math.sin(angle + spread)
236
+ return closed
237
+ ? `M${r(x)},${r(y)} L${r(ax)},${r(ay)} L${r(bx)},${r(by)} Z`
238
+ : `M${r(ax)},${r(ay)} L${r(x)},${r(y)} L${r(bx)},${r(by)}`
239
+ }