@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,403 @@
1
+ // Scheduling for MGantt, as pure functions over tasks: summaries rolled up from their children, dependencies pushed
2
+ // forward (or tasks pulled as soon as possible), the critical path with each task's slack, and working days.
3
+ // Tasks are treated as immutable: what changes comes back as new objects, the rest as they were.
4
+ import { DAY, addDays, daysBetween, formatDate, isDateOnly, isWorkingDay, parseDate, startOfDay } from './_ganttTime'
5
+
6
+ /** A predecessor of a task: finish-to-start (default), start-to-start, finish-to-finish or start-to-finish */
7
+ export interface GanttDependency {
8
+ /** The predecessor's id */
9
+ id: string
10
+ type?: 'FS' | 'SS' | 'FF' | 'SF'
11
+ /** Days between them (negative overlaps them); working days with a calendar */
12
+ lag?: number
13
+ }
14
+
15
+ /** A task. MGantt emits new arrays with new objects for whatever changed (`v-model:tasks`) */
16
+ export interface GanttTask<D = any> {
17
+ id: string
18
+ name: string
19
+ /** `YYYY-MM-DD` (whole days) or a datetime (`YYYY-MM-DDTHH:mm`, local, or ISO with a zone) */
20
+ start: string
21
+ /** A date is the last day (inclusive); a datetime is when it ends. A milestone needs none */
22
+ end?: string
23
+ /** 0 to 100 */
24
+ progress?: number
25
+ /** `summary` is implied for a task with children (its dates and progress roll up from them) */
26
+ type?: 'task' | 'milestone' | 'summary'
27
+ /** The summary task it belongs to */
28
+ parentId?: string
29
+ /** Its predecessors: ids (finish-to-start) or `GanttDependency` */
30
+ dependencies?: (string | GanttDependency)[]
31
+ /** An M3 role (default `primary`; `summary` defaults to `neutral`) */
32
+ color?: 'primary' | 'secondary' | 'tertiary' | 'error' | 'success' | 'neutral'
33
+ /** The plan it's compared with (MGantt's `baselines`) */
34
+ baselineStart?: string
35
+ baselineEnd?: string
36
+ /** `false` keeps it from being moved, resized, linked, deleted or selected */
37
+ draggable?: boolean
38
+ resizable?: boolean
39
+ linkable?: boolean
40
+ deletable?: boolean
41
+ selectable?: boolean
42
+ class?: string
43
+ data?: D
44
+ }
45
+
46
+ /** Which days are worked: weekdays (0 Sunday … 6 Saturday, default Monday to Friday) and holidays (`YYYY-MM-DD`) */
47
+ export interface GanttCalendar {
48
+ workingDays?: number[]
49
+ holidays?: string[]
50
+ }
51
+
52
+ export interface GanttScheduleOptions {
53
+ /** `'push'` (default) moves tasks only as late as their dependencies need; `'asap'` also pulls them back to the earliest they can start */
54
+ mode?: 'push' | 'asap'
55
+ /** Count durations and lags in working days (date-only tasks), starting on working days */
56
+ calendar?: GanttCalendar
57
+ }
58
+
59
+ export interface GanttCriticalPath {
60
+ /** Tasks with no slack (and summaries holding one) */
61
+ tasks: string[]
62
+ /** Dependencies on the path, as `'<predecessor>><successor>'` */
63
+ dependencies: string[]
64
+ /** Each task's total slack in days: how late it can finish without delaying the end */
65
+ slack: Record<string, number>
66
+ /** When the last task ends */
67
+ finish: string
68
+ }
69
+
70
+ export function normalizeDependency(d: string | GanttDependency): Required<GanttDependency> {
71
+ return typeof d === 'string' ? { id: d, type: 'FS', lag: 0 } : { id: d.id, type: d.type ?? 'FS', lag: d.lag ?? 0 }
72
+ }
73
+
74
+ /** Ids of tasks that have children */
75
+ export function summaryIds(tasks: readonly GanttTask[]): Set<string> {
76
+ const ids = new Set(tasks.map((t) => t.id))
77
+ const out = new Set<string>()
78
+ for (const t of tasks) if (t.parentId && ids.has(t.parentId)) out.add(t.parentId)
79
+ for (const t of tasks) if (t.type === 'summary') out.add(t.id)
80
+ return out
81
+ }
82
+
83
+ export function isMilestone(t: GanttTask): boolean {
84
+ return t.type === 'milestone' || (!t.end && t.type !== 'summary')
85
+ }
86
+
87
+ /** A task's span in ms, end exclusive: a date-only end runs to the next midnight; a milestone has none */
88
+ export function taskSpan(t: Pick<GanttTask, 'start' | 'end' | 'type'>): { start: number; end: number; dateOnly: boolean } {
89
+ const start = parseDate(t.start)
90
+ const dateOnly = isDateOnly(t.start)
91
+ if (t.type === 'milestone' || !t.end) return { start, end: start, dateOnly }
92
+ const e = parseDate(t.end)
93
+ const end = isDateOnly(t.end) ? addDays(startOfDay(e), 1) : e
94
+ return { start, end: Math.max(start, end), dateOnly }
95
+ }
96
+
97
+ /** The task's `start`/`end` strings for a span (end exclusive), in the format it came in */
98
+ export function spanDates(t: Pick<GanttTask, 'start' | 'end' | 'type'>, start: number, end: number): { start: string; end?: string } {
99
+ const dateOnly = isDateOnly(t.start)
100
+ if (t.type === 'milestone' || !t.end) return { start: formatDate(dateOnly ? startOfDay(start) : start, dateOnly) }
101
+ if (dateOnly) {
102
+ const s = startOfDay(start)
103
+ const last = Math.max(s, startOfDay(end - 1))
104
+ return { start: formatDate(s, true), end: formatDate(last, true) }
105
+ }
106
+ return { start: formatDate(start, false), end: formatDate(Math.max(start, end), false) }
107
+ }
108
+
109
+ /** Duration in whole days (date-only tasks) or fractions of a day */
110
+ export function taskDays(t: GanttTask): number {
111
+ const s = taskSpan(t)
112
+ return s.dateOnly ? daysBetween(s.start, s.end) : (s.end - s.start) / DAY
113
+ }
114
+
115
+ /**
116
+ * Summaries take the span of their children (deepest first) and their progress, weighted by duration. Returns the
117
+ * same array when nothing changes.
118
+ */
119
+ export function rollupSummaries<T extends GanttTask>(tasks: readonly T[]): T[] {
120
+ const sums = summaryIds(tasks)
121
+ if (!sums.size) return tasks as T[]
122
+ const kids = new Map<string, T[]>()
123
+ for (const t of tasks) if (t.parentId && sums.has(t.parentId)) (kids.get(t.parentId) ?? kids.set(t.parentId, []).get(t.parentId)!).push(t)
124
+ const out = new Map<string, T>()
125
+ const visiting = new Set<string>()
126
+ const resolve = (t: T): T => {
127
+ if (!sums.has(t.id) || !kids.has(t.id)) return out.get(t.id) ?? t
128
+ if (out.has(t.id)) return out.get(t.id)!
129
+ if (visiting.has(t.id)) return t
130
+ visiting.add(t.id)
131
+ let start = Infinity
132
+ let end = -Infinity
133
+ let dateOnly = true
134
+ let work = 0
135
+ let done = 0
136
+ for (const k of kids.get(t.id)!) {
137
+ const r = resolve(k)
138
+ const s = taskSpan(r)
139
+ if (!Number.isFinite(s.start)) continue
140
+ start = Math.min(start, s.start)
141
+ end = Math.max(end, s.end)
142
+ if (!s.dateOnly) dateOnly = false
143
+ const w = Math.max(s.end - s.start, isMilestone(r) ? 0 : 1)
144
+ work += w
145
+ done += w * (r.progress ?? 0)
146
+ }
147
+ visiting.delete(t.id)
148
+ if (!Number.isFinite(start)) return t
149
+ const dates = dateOnly
150
+ ? { start: formatDate(start, true), end: formatDate(Math.max(start, startOfDay(end - 1)), true) }
151
+ : { start: formatDate(start, false), end: formatDate(end, false) }
152
+ const progress = work ? Math.round(done / work) : (t.progress ?? 0)
153
+ const next = dates.start === t.start && dates.end === t.end && progress === (t.progress ?? 0) ? t : { ...t, start: dates.start, end: dates.end, progress }
154
+ out.set(t.id, next)
155
+ return next
156
+ }
157
+ let changed = false
158
+ const result = tasks.map((t) => {
159
+ const r = resolve(t)
160
+ if (r !== t) changed = true
161
+ return r
162
+ })
163
+ return changed ? result : (tasks as T[])
164
+ }
165
+
166
+ // ── Working days ──────────────────────────────────────────────────────────────────────────────────────────
167
+
168
+ function calendarOf(c?: GanttCalendar) {
169
+ return { days: c?.workingDays ?? [1, 2, 3, 4, 5], holidays: new Set(c?.holidays ?? []) }
170
+ }
171
+
172
+ /** Working days from `start` to `end`, both included (`YYYY-MM-DD`) */
173
+ export function workingDays(start: string, end: string, calendar?: GanttCalendar): number {
174
+ const c = calendarOf(calendar)
175
+ let n = 0
176
+ const last = startOfDay(parseDate(end))
177
+ for (let d = startOfDay(parseDate(start)); d <= last; d = addDays(d, 1)) if (isWorkingDay(d, c.days, c.holidays)) n++
178
+ return n
179
+ }
180
+
181
+ /** The date `n` working days after `date` (before it when negative); 0 moves a day off to the next working day */
182
+ export function addWorkingDays(date: string, n: number, calendar?: GanttCalendar): string {
183
+ const c = calendarOf(calendar)
184
+ if (!c.days.length) return date
185
+ let d = startOfDay(parseDate(date))
186
+ while (!isWorkingDay(d, c.days, c.holidays)) d = addDays(d, 1)
187
+ const step = n < 0 ? -1 : 1
188
+ for (let left = Math.abs(n); left > 0; ) {
189
+ d = addDays(d, step)
190
+ if (isWorkingDay(d, c.days, c.holidays)) left--
191
+ }
192
+ return formatDate(d, true)
193
+ }
194
+
195
+ // ── Scheduling ────────────────────────────────────────────────────────────────────────────────────────────
196
+
197
+ interface Link { pred: string; succ: string; type: 'FS' | 'SS' | 'FF' | 'SF'; lag: number; key: string }
198
+
199
+ // Dependencies between plain tasks: one onto a summary applies to each of its tasks (as MS Project does), one
200
+ // from a summary is resolved against its span once its tasks are placed
201
+ function expandLinks(tasks: readonly GanttTask[]) {
202
+ const byId = new Map(tasks.map((t) => [t.id, t]))
203
+ const sums = summaryIds(tasks)
204
+ const kids = new Map<string, string[]>()
205
+ for (const t of tasks) if (t.parentId && byId.has(t.parentId)) (kids.get(t.parentId) ?? kids.set(t.parentId, []).get(t.parentId)!).push(t.id)
206
+ const leaves = (id: string, depth = 0): string[] => (sums.has(id) && kids.has(id) && depth < 100 ? kids.get(id)!.flatMap((k) => leaves(k, depth + 1)) : [id])
207
+ const links: Link[] = []
208
+ for (const t of tasks) {
209
+ for (const raw of t.dependencies ?? []) {
210
+ const d = normalizeDependency(raw)
211
+ if (!byId.has(d.id) || d.id === t.id) continue
212
+ for (const succ of leaves(t.id)) links.push({ pred: d.id, succ, type: d.type, lag: d.lag, key: `${d.id}>${t.id}` })
213
+ }
214
+ }
215
+ return { byId, sums, kids, leaves, links }
216
+ }
217
+
218
+ // Order: a task after everything it waits for; a summary used as a predecessor after its tasks. Cycles are cut
219
+ function order(ids: string[], deps: Map<string, string[]>): string[] {
220
+ const out: string[] = []
221
+ const state = new Map<string, 1 | 2>()
222
+ const visit = (id: string) => {
223
+ if (state.get(id)) return
224
+ state.set(id, 1)
225
+ for (const p of deps.get(id) ?? []) if (state.get(p) !== 1) visit(p)
226
+ state.set(id, 2)
227
+ out.push(id)
228
+ }
229
+ for (const id of ids) visit(id)
230
+ return out
231
+ }
232
+
233
+ /**
234
+ * Moves tasks so every dependency holds: by default only later (`'push'`, what dragging a predecessor does), or as
235
+ * soon as possible (`'asap'`), keeping each task's duration. Summaries follow their tasks. Returns the same array
236
+ * when nothing moves.
237
+ */
238
+ export function scheduleTasks<T extends GanttTask>(tasks: readonly T[], options: GanttScheduleOptions = {}): T[] {
239
+ const { sums, kids, leaves, links } = expandLinks(tasks)
240
+ const cal = options.calendar ? calendarOf(options.calendar) : null
241
+ const spans = new Map<string, { start: number; end: number; dateOnly: boolean }>()
242
+ for (const t of tasks) if (!sums.has(t.id)) spans.set(t.id, taskSpan(t))
243
+ const deps = new Map<string, string[]>()
244
+ for (const l of links) (deps.get(l.succ) ?? deps.set(l.succ, []).get(l.succ)!).push(l.pred)
245
+ for (const s of sums) deps.set(s, [...(deps.get(s) ?? []), ...leaves(s)])
246
+ const bySucc = new Map<string, Link[]>()
247
+ for (const l of links) (bySucc.get(l.succ) ?? bySucc.set(l.succ, []).get(l.succ)!).push(l)
248
+
249
+ const span = (id: string) => {
250
+ if (!sums.has(id)) return spans.get(id)
251
+ let start = Infinity
252
+ let end = -Infinity
253
+ for (const l of leaves(id)) {
254
+ const s = spans.get(l)
255
+ if (!s) continue
256
+ start = Math.min(start, s.start)
257
+ end = Math.max(end, s.end)
258
+ }
259
+ return Number.isFinite(start) ? { start, end, dateOnly: true } : undefined
260
+ }
261
+ // Shift by days, on working days with a calendar
262
+ const shift = (ms: number, days: number, dateOnly: boolean) => {
263
+ if (!cal || !dateOnly) return ms + days * DAY
264
+ return parseDate(addWorkingDays(formatDate(ms, true), days, options.calendar))
265
+ }
266
+ const workLength = (s: { start: number; end: number }) => {
267
+ let n = 0
268
+ for (let d = s.start; d < s.end; d = addDays(d, 1)) if (isWorkingDay(d, cal!.days, cal!.holidays)) n++
269
+ return n
270
+ }
271
+
272
+ let moved = false
273
+ for (const id of order([...spans.keys(), ...sums], deps)) {
274
+ if (sums.has(id)) continue
275
+ const own = spans.get(id)!
276
+ const incoming = bySucc.get(id)
277
+ if (!incoming?.length || !Number.isFinite(own.start)) continue
278
+ const dur = own.end - own.start
279
+ let earliest = -Infinity
280
+ for (const l of incoming) {
281
+ const p = span(l.pred)
282
+ if (!p) continue
283
+ const pd = p.dateOnly && own.dateOnly
284
+ let need: number
285
+ if (l.type === 'FS') need = shift(p.end, l.lag, pd)
286
+ else if (l.type === 'SS') need = shift(p.start, l.lag, pd)
287
+ else if (l.type === 'FF') need = shift(p.end, l.lag, pd) - dur
288
+ else need = shift(p.start, l.lag, pd) - dur
289
+ earliest = Math.max(earliest, need)
290
+ }
291
+ if (!Number.isFinite(earliest)) continue
292
+ let start = options.mode === 'asap' ? earliest : Math.max(own.start, earliest)
293
+ if (own.dateOnly) start = startOfDay(start + 12 * 3_600_000)
294
+ let end = start + dur
295
+ if (cal && own.dateOnly) {
296
+ // Starts on a working day and lasts as many working days as before
297
+ const days = workLength(own)
298
+ start = parseDate(addWorkingDays(formatDate(start, true), 0, options.calendar))
299
+ end = days ? addDays(parseDate(addWorkingDays(formatDate(start, true), Math.max(0, days - 1), options.calendar)), 1) : start
300
+ } else if (own.dateOnly) end = addDays(start, daysBetween(own.start, own.end))
301
+ if (start !== own.start || end !== own.end) {
302
+ spans.set(id, { start, end, dateOnly: own.dateOnly })
303
+ moved = true
304
+ }
305
+ }
306
+ if (!moved) return rollupSummaries(tasks)
307
+ const next = tasks.map((t) => {
308
+ if (sums.has(t.id) && kids.has(t.id)) return t
309
+ const s = spans.get(t.id)
310
+ const before = taskSpan(t)
311
+ if (!s || (s.start === before.start && s.end === before.end)) return t
312
+ return { ...t, ...spanDates(t, s.start, s.end) }
313
+ })
314
+ return rollupSummaries(next)
315
+ }
316
+
317
+ /**
318
+ * The critical path over the tasks' current dates: tasks whose delay would delay the last one's end (no slack),
319
+ * through their dependencies. Slack is in calendar days, or working days with a calendar (so a weekend between
320
+ * two tasks isn't slack).
321
+ */
322
+ export function criticalPath(tasks: readonly GanttTask[], options: { calendar?: GanttCalendar } = {}): GanttCriticalPath {
323
+ const { sums, kids, leaves, links } = expandLinks(tasks)
324
+ const real = new Map<string, { start: number; end: number }>()
325
+ for (const t of tasks) if (!sums.has(t.id) || !kids.has(t.id)) real.set(t.id, taskSpan(t))
326
+ const ids = [...real.keys()].filter((id) => Number.isFinite(real.get(id)!.start))
327
+ if (!ids.length) return { tasks: [], dependencies: [], slack: {}, finish: '' }
328
+
329
+ // Everything below runs in days: calendar days, or with a calendar working days (a day off has no length), so
330
+ // the weekend between two tasks is neither slack nor duration
331
+ const cal = options.calendar ? calendarOf(options.calendar) : null
332
+ let toDays = (ms: number) => ms / DAY
333
+ if (cal) {
334
+ const first = startOfDay(Math.min(...ids.map((id) => real.get(id)!.start)))
335
+ const index = new Map<number, number>()
336
+ toDays = (ms: number) => {
337
+ const day = startOfDay(ms)
338
+ let n = index.get(day)
339
+ if (n === undefined) {
340
+ n = 0
341
+ const step = day >= first ? 1 : -1
342
+ for (let d = first; d !== day; d = addDays(d, step)) if (isWorkingDay(step > 0 ? d : addDays(d, -1), cal.days, cal.holidays)) n += step
343
+ index.set(day, n)
344
+ }
345
+ return n + (isWorkingDay(day, cal.days, cal.holidays) ? (ms - day) / DAY : 0)
346
+ }
347
+ }
348
+ const spans = new Map(ids.map((id) => [id, { start: toDays(real.get(id)!.start), end: toDays(real.get(id)!.end) }]))
349
+ // Links from a summary come from each of its tasks
350
+ const flat: Link[] = links.flatMap((l) => (sums.has(l.pred) && kids.has(l.pred) ? leaves(l.pred).map((p) => ({ ...l, pred: p })) : [l]))
351
+ .filter((l) => spans.has(l.pred) && spans.has(l.succ))
352
+ const finish = Math.max(...ids.map((id) => spans.get(id)!.end))
353
+ const out = new Map<string, Link[]>()
354
+ const deps = new Map<string, string[]>()
355
+ for (const l of flat) {
356
+ ;(out.get(l.pred) ?? out.set(l.pred, []).get(l.pred)!).push(l)
357
+ ;(deps.get(l.succ) ?? deps.set(l.succ, []).get(l.succ)!).push(l.pred)
358
+ }
359
+ const late = new Map<string, number>()
360
+ // Backward pass: successors first
361
+ for (const id of order(ids, deps).reverse()) {
362
+ const s = spans.get(id)!
363
+ const dur = s.end - s.start
364
+ let lf = finish
365
+ for (const l of out.get(id) ?? []) {
366
+ const ls = late.get(l.succ)
367
+ if (ls === undefined) continue
368
+ const succ = spans.get(l.succ)!
369
+ const lfSucc = ls + (succ.end - succ.start)
370
+ if (l.type === 'FS') lf = Math.min(lf, ls - l.lag)
371
+ else if (l.type === 'SS') lf = Math.min(lf, ls - l.lag + dur)
372
+ else if (l.type === 'FF') lf = Math.min(lf, lfSucc - l.lag)
373
+ else lf = Math.min(lf, lfSucc - l.lag + dur)
374
+ }
375
+ late.set(id, lf - dur)
376
+ }
377
+ const slack: Record<string, number> = {}
378
+ const critical = new Set<string>()
379
+ for (const id of ids) {
380
+ const days = late.get(id)! - spans.get(id)!.start
381
+ slack[id] = Math.round(days * 1000) / 1000
382
+ if (days < 1e-6) critical.add(id)
383
+ }
384
+ // A dependency is on the path when both ends are and it's what holds the successor back
385
+ const tight = new Set<string>()
386
+ for (const l of flat) {
387
+ if (!critical.has(l.pred) || !critical.has(l.succ)) continue
388
+ const p = spans.get(l.pred)!
389
+ const s = spans.get(l.succ)!
390
+ const from = (l.type === 'FS' || l.type === 'FF' ? p.end : p.start) + l.lag
391
+ const to = l.type === 'FS' || l.type === 'SS' ? s.start : s.end
392
+ if (Math.abs(to - from) < 1e-6) tight.add(l.key)
393
+ }
394
+ for (const s of sums) if (kids.has(s) && leaves(s).some((l) => critical.has(l))) critical.add(s)
395
+ const end = Math.max(...ids.map((id) => real.get(id)!.end))
396
+ const last = tasks.find((t) => real.get(t.id)?.end === end)
397
+ return {
398
+ tasks: tasks.filter((t) => critical.has(t.id)).map((t) => t.id),
399
+ dependencies: [...tight],
400
+ slack,
401
+ finish: formatDate(isDateOnly(last?.start) ? addDays(end, -1) : end, isDateOnly(last?.start)),
402
+ }
403
+ }
@@ -0,0 +1,53 @@
1
+ import { inject, type ComputedRef, type InjectionKey, type Ref } from 'vue'
2
+ import type { DrawItem, DrawLayer, DrawPen, DrawShapeKind } from './drawGeometry'
3
+
4
+ /** What a pointer does on an MDrawCanvas */
5
+ export type DrawTool = DrawPen | 'eraser' | 'stroke-eraser' | DrawShapeKind | 'text' | 'select' | 'pan'
6
+
7
+ export interface DrawExportOptions {
8
+ /** In `mode="pages"`: the page to export (default 0) */
9
+ page?: number
10
+ /** PNG: pixels per drawing unit (default 2) */
11
+ scale?: number
12
+ /** Paint the paper color behind the drawing (default true) */
13
+ background?: boolean
14
+ }
15
+
16
+ /** What an MDrawCanvas shares with MDrawToolbar, MDrawLayers and anything built with useDrawing() inside it */
17
+ export interface DrawContext {
18
+ tool: Ref<DrawTool>
19
+ color: Ref<string>
20
+ size: Ref<number>
21
+ /** New rects and ellipses are filled with the color */
22
+ fill: Ref<boolean>
23
+ items: ComputedRef<DrawItem[]>
24
+ selection: Ref<string[]>
25
+ layers: ComputedRef<DrawLayer[]>
26
+ setLayers(layers: DrawLayer[]): void
27
+ /** The layer new items go on */
28
+ layer: Ref<string | undefined>
29
+ mode: ComputedRef<'infinite' | 'pages'>
30
+ pages: Ref<number>
31
+ addPage(): void
32
+ zoom: ComputedRef<number>
33
+ zoomIn(): void
34
+ zoomOut(): void
35
+ fitView(): void
36
+ canUndo: ComputedRef<boolean>
37
+ canRedo: ComputedRef<boolean>
38
+ undo(): void
39
+ redo(): void
40
+ clear(): void
41
+ deleteSelection(): void
42
+ exportSvg(opts?: DrawExportOptions): string
43
+ exportPng(opts?: DrawExportOptions): Promise<Blob>
44
+ }
45
+
46
+ export const DRAW_KEY: InjectionKey<DrawContext> = Symbol('m3-draw')
47
+
48
+ /** The surrounding MDrawCanvas' state and actions, for a toolbar or panel of your own inside it */
49
+ export function useDrawing(): DrawContext {
50
+ const ctx = inject(DRAW_KEY, null)
51
+ if (!ctx) throw new Error('useDrawing() must be called inside <MDrawCanvas>')
52
+ return ctx
53
+ }