@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,136 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { addWorkingDays, criticalPath, rollupSummaries, scheduleTasks, workingDays, type GanttTask } from '../composables/ganttSchedule'
3
+ import { snapTime, unitsFor, parseDate, formatDate } from '../composables/_ganttTime'
4
+
5
+ const T = (id: string, start: string, end: string, extra: Partial<GanttTask> = {}): GanttTask => ({ id, name: id, start, end, ...extra })
6
+ const byId = (ts: GanttTask[]) => Object.fromEntries(ts.map((t) => [t.id, t]))
7
+
8
+ describe('gantt time', () => {
9
+ it('parses and formats local dates and datetimes', () => {
10
+ expect(formatDate(parseDate('2026-03-05'), true)).toBe('2026-03-05')
11
+ expect(formatDate(parseDate('2026-03-05T09:30'), false)).toBe('2026-03-05T09:30')
12
+ expect(formatDate(snapTime(parseDate('2026-03-05T09:38'), 15), false)).toBe('2026-03-05T09:45')
13
+ expect(formatDate(snapTime(parseDate('2026-03-05T13:00'), 1440), true)).toBe('2026-03-06')
14
+ })
15
+ it('picks header units that fit their labels', () => {
16
+ expect(unitsFor(1152)).toEqual({ bottom: 'hour', top: 'day', step: 1 })
17
+ expect(unitsFor(240)).toEqual({ bottom: 'hour', top: 'day', step: 6 })
18
+ expect(unitsFor(40)).toMatchObject({ bottom: 'day', top: 'month' })
19
+ expect(unitsFor(20)).toMatchObject({ bottom: 'week', top: 'month' })
20
+ expect(unitsFor(5)).toMatchObject({ bottom: 'month', top: 'year' })
21
+ expect(unitsFor(1.2).bottom).toBe('quarter')
22
+ expect(unitsFor(0.3).bottom).toBe('year')
23
+ })
24
+ })
25
+
26
+ describe('working days', () => {
27
+ it('counts and adds them around weekends and holidays', () => {
28
+ // 2026-03-06 is a Friday
29
+ expect(workingDays('2026-03-06', '2026-03-09')).toBe(2)
30
+ expect(addWorkingDays('2026-03-06', 1)).toBe('2026-03-09')
31
+ expect(addWorkingDays('2026-03-07', 0)).toBe('2026-03-09')
32
+ expect(addWorkingDays('2026-03-06', 1, { holidays: ['2026-03-09'] })).toBe('2026-03-10')
33
+ expect(addWorkingDays('2026-03-09', -1)).toBe('2026-03-06')
34
+ })
35
+ })
36
+
37
+ describe('rollupSummaries', () => {
38
+ it('gives summaries the span of their tasks and weighted progress, nested', () => {
39
+ const tasks = [
40
+ T('p', '2026-01-01', '2026-01-01'),
41
+ T('s', '2026-01-01', '2026-01-01', { parentId: 'p' }),
42
+ T('a', '2026-01-05', '2026-01-06', { parentId: 's', progress: 100 }),
43
+ T('b', '2026-01-07', '2026-01-12', { parentId: 's', progress: 0 }),
44
+ T('c', '2026-01-20', '2026-01-21', { parentId: 'p', progress: 50 }),
45
+ ]
46
+ const r = byId(rollupSummaries(tasks))
47
+ expect(r.s).toMatchObject({ start: '2026-01-05', end: '2026-01-12', progress: 25 })
48
+ expect(r.p).toMatchObject({ start: '2026-01-05', end: '2026-01-21' })
49
+ // Unchanged → same array
50
+ const once = rollupSummaries(tasks)
51
+ expect(rollupSummaries(once)).toBe(once)
52
+ })
53
+ })
54
+
55
+ describe('scheduleTasks', () => {
56
+ it('pushes successors past their predecessors, for every dependency type and lag', () => {
57
+ const tasks = [
58
+ T('a', '2026-01-05', '2026-01-09'),
59
+ T('fs', '2026-01-06', '2026-01-07', { dependencies: ['a'] }),
60
+ T('ss', '2026-01-01', '2026-01-02', { dependencies: [{ id: 'a', type: 'SS', lag: 2 }] }),
61
+ T('ff', '2026-01-01', '2026-01-03', { dependencies: [{ id: 'a', type: 'FF' }] }),
62
+ T('late', '2026-02-01', '2026-02-02', { dependencies: ['a'] }),
63
+ T('chain', '2026-01-01', '2026-01-01', { dependencies: ['fs'] }),
64
+ ]
65
+ const r = byId(scheduleTasks(tasks))
66
+ expect(r.fs).toMatchObject({ start: '2026-01-10', end: '2026-01-11' })
67
+ expect(r.ss).toMatchObject({ start: '2026-01-07', end: '2026-01-08' })
68
+ expect(r.ff).toMatchObject({ start: '2026-01-07', end: '2026-01-09' })
69
+ // Push mode leaves a task that's already late enough alone; asap pulls it back
70
+ expect(r.late!.start).toBe('2026-02-01')
71
+ expect(byId(scheduleTasks(tasks, { mode: 'asap' })).late!.start).toBe('2026-01-10')
72
+ expect(r.chain!.start).toBe('2026-01-12')
73
+ expect(scheduleTasks([tasks[0]!])).toEqual([tasks[0]])
74
+ })
75
+ it('counts working days with a calendar; a link onto a summary moves each of its tasks', () => {
76
+ const tasks = [
77
+ // Friday
78
+ T('a', '2026-03-06', '2026-03-06'),
79
+ T('b', '2026-03-02', '2026-03-03', { dependencies: ['a'] }),
80
+ T('phase', '2026-01-01', '2026-01-01', { dependencies: ['a'] }),
81
+ T('p1', '2026-03-01', '2026-03-02', { parentId: 'phase' }),
82
+ ]
83
+ const r = byId(scheduleTasks(tasks, { calendar: {} }))
84
+ // Two working days from Monday
85
+ expect(r.b).toMatchObject({ start: '2026-03-09', end: '2026-03-10' })
86
+ expect(r.p1!.start).toBe('2026-03-09')
87
+ expect(r.phase).toMatchObject({ start: '2026-03-09', end: r.p1!.end })
88
+ })
89
+ it('survives cycles', () => {
90
+ const tasks = [T('a', '2026-01-01', '2026-01-02', { dependencies: ['b'] }), T('b', '2026-01-01', '2026-01-02', { dependencies: ['a'] })]
91
+ expect(() => scheduleTasks(tasks)).not.toThrow()
92
+ })
93
+ })
94
+
95
+ describe('criticalPath', () => {
96
+ it('finds the chain that sets the end, with slack for the rest', () => {
97
+ const tasks = [
98
+ T('a', '2026-01-01', '2026-01-02'),
99
+ T('b', '2026-01-03', '2026-01-10', { dependencies: ['a'] }),
100
+ T('c', '2026-01-03', '2026-01-04', { dependencies: ['a'] }),
101
+ T('d', '2026-01-11', '2026-01-11', { dependencies: ['b', 'c'] }),
102
+ ]
103
+ const cp = criticalPath(tasks)
104
+ expect(cp.tasks).toEqual(['a', 'b', 'd'])
105
+ expect(cp.dependencies.sort()).toEqual(['a>b', 'b>d'])
106
+ expect(cp.slack.c).toBe(6)
107
+ expect(cp.finish).toBe('2026-01-11')
108
+ })
109
+ })
110
+
111
+ describe('criticalPath with a calendar', () => {
112
+ it("doesn't count a weekend between two tasks as slack", () => {
113
+ // Friday, then Monday
114
+ const tasks = [T('a', '2026-03-06', '2026-03-06'), T('b', '2026-03-09', '2026-03-10', { dependencies: ['a'] })]
115
+ expect(criticalPath(tasks).tasks).toEqual(['b'])
116
+ const cp = criticalPath(tasks, { calendar: {} })
117
+ expect(cp.tasks).toEqual(['a', 'b'])
118
+ expect(cp.dependencies).toEqual(['a>b'])
119
+ })
120
+ })
121
+
122
+ describe('criticalPath across weekends', () => {
123
+ it('finds a chain whose links all cross weekends, the way Tempo plans it', () => {
124
+ // Mon–Fri, then Mon–Fri, then a Monday milestone; a side task with real slack
125
+ const tasks = [
126
+ T('a', '2026-03-02', '2026-03-06'),
127
+ T('b', '2026-03-09', '2026-03-13', { dependencies: ['a'] }),
128
+ T('side', '2026-03-09', '2026-03-10', { dependencies: ['a'] }),
129
+ { id: 'm', name: 'm', start: '2026-03-16', type: 'milestone' as const, dependencies: ['b', 'side'] },
130
+ ]
131
+ const cp = criticalPath(tasks, { calendar: {} })
132
+ expect(cp.tasks).toEqual(['a', 'b', 'm'])
133
+ expect(cp.slack.side).toBe(3)
134
+ expect(cp.dependencies.sort()).toEqual(['a>b', 'b>m'])
135
+ })
136
+ })
@@ -0,0 +1,83 @@
1
+ // A fake pdfjs-dist for tests (happy-dom can't run pdf.js): pages with sizes, text and links, an outline, metadata
2
+ // and an optional password, with the calls the library makes.
3
+ import { vi } from 'vitest'
4
+
5
+ export interface FakePage { width: number; height: number; text: { str: string; hasEOL?: boolean }[]; links?: { rect: number[]; url?: string; dest?: unknown }[] }
6
+
7
+ export const fakeDoc = {
8
+ pages: [] as FakePage[],
9
+ password: '' as string,
10
+ outline: [] as unknown[],
11
+ title: 'Fake document',
12
+ }
13
+
14
+ class Viewport {
15
+ constructor(public width: number, public height: number, public scale: number, public rotation: number) {}
16
+ convertToViewportPoint(x: number, y: number) { return [x * this.scale, this.height - y * this.scale] }
17
+ convertToPdfPoint(x: number, y: number) { return [x / this.scale, (this.height - y) / this.scale] }
18
+ get rawDims() { return { pageWidth: this.width / this.scale, pageHeight: this.height / this.scale } }
19
+ }
20
+
21
+ function page(i: number) {
22
+ const p = fakeDoc.pages[i]!
23
+ return {
24
+ rotate: 0,
25
+ getViewport: ({ scale = 1, rotation = 0 }: { scale?: number; rotation?: number } = {}) => {
26
+ const swap = rotation % 180 !== 0
27
+ return new Viewport((swap ? p.height : p.width) * scale, (swap ? p.width : p.height) * scale, scale, rotation)
28
+ },
29
+ render: () => ({ promise: Promise.resolve(), cancel: () => {} }),
30
+ getTextContent: async () => ({ items: p.text.map((t) => ({ str: t.str, hasEOL: !!t.hasEOL, transform: [1, 0, 0, 1, 0, 0] })) }),
31
+ getAnnotations: async () => (p.links ?? []).map((l) => ({ subtype: 'Link', ...l })),
32
+ cleanup: () => {},
33
+ }
34
+ }
35
+
36
+ export const getDocument = vi.fn((_params: Record<string, unknown>) => {
37
+ const task: Record<string, unknown> = { destroy: vi.fn() }
38
+ task.promise = (async () => {
39
+ await Promise.resolve()
40
+ if (fakeDoc.password && _params.password !== fakeDoc.password) {
41
+ // Ask until the right one comes
42
+ let tried = _params.password as string | undefined
43
+ while (tried !== fakeDoc.password) {
44
+ tried = await new Promise<string>((resolve) => (task.onPassword as (cb: (p: string) => void, reason: number) => void)(resolve, tried === undefined ? 1 : 2))
45
+ }
46
+ }
47
+ return {
48
+ numPages: fakeDoc.pages.length,
49
+ getPage: async (n: number) => page(n - 1),
50
+ getOutline: async () => fakeDoc.outline,
51
+ getMetadata: async () => ({ info: { Title: fakeDoc.title }, metadata: null }),
52
+ getDestination: async (name: string) => (name === 'ch2' ? [{ num: 1 }, { name: 'XYZ' }, 0, 700, null] : null),
53
+ getPageIndex: async (ref: { num: number }) => ref.num,
54
+ getData: async () => new Uint8Array([37, 80, 68, 70]),
55
+ destroy: vi.fn(),
56
+ }
57
+ })()
58
+ return task
59
+ })
60
+
61
+ export class TextLayer {
62
+ textDivs: HTMLElement[] = []
63
+ textContentItemsStr: string[] = []
64
+ constructor(private o: { textContentSource: { items: { str: string }[] }; container: HTMLElement }) {}
65
+ async render() {
66
+ for (const item of this.o.textContentSource.items) {
67
+ const span = document.createElement('span')
68
+ span.textContent = item.str
69
+ this.o.container.appendChild(span)
70
+ this.textDivs.push(span)
71
+ this.textContentItemsStr.push(item.str)
72
+ }
73
+ }
74
+ cancel() {}
75
+ }
76
+
77
+ export class TouchManager {
78
+ constructor(_o: unknown) {}
79
+ }
80
+
81
+ export const GlobalWorkerOptions = { workerSrc: 'fake-worker.js' }
82
+ export const PasswordResponses = { NEED_PASSWORD: 1, INCORRECT_PASSWORD: 2 }
83
+ export const PDFDateString = { toDateObject: () => null }
@@ -0,0 +1,182 @@
1
+ import { describe, it, expect, afterEach, vi } from 'vitest'
2
+ import { effectScope, nextTick, ref } from 'vue'
3
+ import { useMediaPlayer, type MediaPlayerOptions } from '../composables/useMediaPlayer'
4
+
5
+ afterEach(() => {
6
+ vi.useRealTimers()
7
+ vi.restoreAllMocks()
8
+ Reflect.deleteProperty(navigator, 'mediaSession')
9
+ })
10
+
11
+ function setup(options: MediaPlayerOptions) {
12
+ const scope = effectScope()
13
+ const player = scope.run(() => useMediaPlayer(options))!
14
+ return { player, stop: () => scope.stop() }
15
+ }
16
+
17
+ // A media element whose play() resolves (or rejects) and fires the events a browser would
18
+ function fakeMedia(opts: { block?: boolean } = {}) {
19
+ const el = document.createElement('video')
20
+ let paused = true
21
+ Object.defineProperty(el, 'paused', { get: () => paused })
22
+ Object.defineProperty(el, 'duration', { value: 120, configurable: true })
23
+ el.play = vi.fn(async () => {
24
+ if (opts.block) throw new DOMException('blocked', 'NotAllowedError')
25
+ paused = false
26
+ el.dispatchEvent(new Event('play'))
27
+ el.dispatchEvent(new Event('playing'))
28
+ })
29
+ el.pause = vi.fn(() => {
30
+ paused = true
31
+ el.dispatchEvent(new Event('pause'))
32
+ })
33
+ return el
34
+ }
35
+
36
+ describe('useMediaPlayer — clock', () => {
37
+ it('advances while playing, at the rate, and stops at the end', async () => {
38
+ vi.useFakeTimers()
39
+ const { player } = setup({ duration: 10 })
40
+ expect(player.duration.value).toBe(10)
41
+ await player.play()
42
+ vi.advanceTimersByTime(1000)
43
+ expect(player.currentTime.value).toBeGreaterThan(0.8)
44
+ expect(player.currentTime.value).toBeLessThan(1.2)
45
+
46
+ player.rate.value = 2
47
+ const before = player.currentTime.value
48
+ vi.advanceTimersByTime(1000)
49
+ expect(player.currentTime.value - before).toBeGreaterThan(1.7)
50
+
51
+ vi.advanceTimersByTime(10000)
52
+ expect(player.currentTime.value).toBe(10)
53
+ expect(player.playing.value).toBe(false)
54
+ expect(player.ended.value).toBe(true)
55
+
56
+ // Playing again starts over
57
+ await player.play()
58
+ expect(player.ended.value).toBe(false)
59
+ expect(player.currentTime.value).toBeLessThan(1)
60
+ })
61
+
62
+ it("doesn't advance while waiting, and loops with loop", async () => {
63
+ vi.useFakeTimers()
64
+ const { player } = setup({ duration: 4, loop: true })
65
+ await player.play()
66
+ player.waiting.value = true
67
+ vi.advanceTimersByTime(1000)
68
+ expect(player.currentTime.value).toBe(0)
69
+ player.waiting.value = false
70
+ vi.advanceTimersByTime(5000)
71
+ expect(player.playing.value).toBe(true)
72
+ expect(player.currentTime.value).toBeLessThan(4)
73
+ })
74
+
75
+ it('seek and skip clamp to the media', () => {
76
+ const { player } = setup({ duration: 30 })
77
+ player.skip()
78
+ expect(player.currentTime.value).toBe(10)
79
+ player.skip(-25)
80
+ expect(player.currentTime.value).toBe(0)
81
+ player.currentTime.value = 99
82
+ expect(player.currentTime.value).toBe(30)
83
+ })
84
+ })
85
+
86
+ describe('useMediaPlayer — media element', () => {
87
+ it('mirrors the element and acts on it', async () => {
88
+ const el = fakeMedia()
89
+ const media = ref<HTMLMediaElement | null>(el)
90
+ const { player } = setup({ media })
91
+ expect(player.duration.value).toBe(120)
92
+
93
+ await player.play()
94
+ expect(el.play).toHaveBeenCalled()
95
+ expect(player.playing.value).toBe(true)
96
+
97
+ player.currentTime.value = 30
98
+ expect(el.currentTime).toBe(30)
99
+ player.rate.value = 1.5
100
+ expect(el.playbackRate).toBe(1.5)
101
+ player.muted.value = true
102
+ expect(el.muted).toBe(true)
103
+
104
+ el.dispatchEvent(new Event('waiting'))
105
+ expect(player.waiting.value).toBe(true)
106
+ el.dispatchEvent(new Event('playing'))
107
+ expect(player.waiting.value).toBe(false)
108
+
109
+ player.playing.value = false
110
+ expect(el.pause).toHaveBeenCalled()
111
+ expect(player.playing.value).toBe(false)
112
+ })
113
+
114
+ it('a position set before the element exists is where it starts', async () => {
115
+ const media = ref<HTMLMediaElement | null>(null)
116
+ const { player } = setup({ media })
117
+ player.seek(105)
118
+ const el = fakeMedia()
119
+ media.value = el
120
+ await nextTick()
121
+ expect(el.currentTime).toBe(105)
122
+ expect(player.currentTime.value).toBe(105)
123
+ })
124
+
125
+ it('reports a blocked autoplay', async () => {
126
+ const { player } = setup({ media: fakeMedia({ block: true }) })
127
+ await player.play()
128
+ expect(player.blocked.value).toBe(true)
129
+ expect(player.playing.value).toBe(false)
130
+ })
131
+
132
+ it('carries rate and volume over to an attached element', async () => {
133
+ const { player } = setup({ duration: 60 })
134
+ player.rate.value = 2
135
+ player.volume.value = 0.3
136
+ const el = fakeMedia()
137
+ player.attach(el)
138
+ await nextTick()
139
+ expect(player.media.value).toBe(el)
140
+ expect(el.playbackRate).toBe(2)
141
+ expect(el.volume).toBeCloseTo(0.3)
142
+ expect(player.duration.value).toBe(120)
143
+ player.attach(undefined)
144
+ await nextTick()
145
+ expect(player.media.value).toBe(null)
146
+ expect(player.duration.value).toBe(60)
147
+ })
148
+ })
149
+
150
+ describe('useMediaPlayer — Media Session', () => {
151
+ it('takes the session while playing: metadata, handlers and position', async () => {
152
+ const handlers: Record<string, ((d: MediaSessionActionDetails) => void) | null> = {}
153
+ const session = {
154
+ metadata: null as unknown,
155
+ playbackState: 'none',
156
+ setActionHandler: (a: string, fn: ((d: MediaSessionActionDetails) => void) | null) => { handlers[a] = fn },
157
+ setPositionState: vi.fn(),
158
+ }
159
+ Object.defineProperty(navigator, 'mediaSession', { value: session, configurable: true })
160
+ vi.stubGlobal('MediaMetadata', class { constructor(public init: MediaMetadataInit) {} })
161
+ const onNext = vi.fn()
162
+ const { player, stop } = setup({ duration: 100, metadata: { title: 'Song', artist: 'Band', artwork: ['/a.png'] }, onNext })
163
+
164
+ await player.play()
165
+ expect((session.metadata as { init: MediaMetadataInit }).init).toMatchObject({ title: 'Song', artist: 'Band', artwork: [{ src: '/a.png' }] })
166
+ expect(session.playbackState).toBe('playing')
167
+ expect(session.setPositionState).toHaveBeenCalledWith({ duration: 100, playbackRate: 1, position: 0 })
168
+
169
+ handlers.seekforward!({ action: 'seekforward' })
170
+ expect(player.currentTime.value).toBe(10)
171
+ handlers.seekto!({ action: 'seekto', seekTime: 42 })
172
+ expect(player.currentTime.value).toBe(42)
173
+ handlers.nexttrack!({ action: 'nexttrack' })
174
+ expect(onNext).toHaveBeenCalled()
175
+ expect(handlers.previoustrack).toBeNull()
176
+
177
+ stop()
178
+ expect(session.metadata).toBeNull()
179
+ expect(handlers.play).toBeNull()
180
+ vi.unstubAllGlobals()
181
+ })
182
+ })
@@ -0,0 +1,102 @@
1
+ import { describe, it, expect, beforeEach, vi } from 'vitest'
2
+ import { effectScope, nextTick, ref } from 'vue'
3
+ import { flushPromises } from '@vue/test-utils'
4
+
5
+ vi.mock('pdfjs-dist', () => import('./pdfMock'))
6
+ const { fakeDoc } = await import('./pdfMock')
7
+ const { usePdf } = await import('../composables/usePdf')
8
+
9
+ beforeEach(() => {
10
+ fakeDoc.pages = [
11
+ { width: 612, height: 792, text: [{ str: 'Pocket gardens grow ' }, { str: 'basil and', hasEOL: true }, { str: 'tomatoes.' }] },
12
+ { width: 612, height: 792, text: [{ str: 'Café crème and BASIL pesto' }] },
13
+ { width: 842, height: 595, text: [{ str: 'A wide page' }] },
14
+ ]
15
+ fakeDoc.password = ''
16
+ fakeDoc.outline = [
17
+ { title: 'One', dest: [{ num: 0 }, { name: 'XYZ' }, 0, 700, null], url: null, items: [{ title: 'One.a', dest: 'ch2', url: null, items: [] }] },
18
+ { title: 'Site', dest: null, url: 'https://m3ui.dev', items: [] },
19
+ ]
20
+ })
21
+
22
+ function setup(src: Parameters<typeof usePdf>[0], options?: Parameters<typeof usePdf>[1]) {
23
+ const scope = effectScope()
24
+ const pdf = scope.run(() => usePdf(src, options))!
25
+ return { pdf, stop: () => scope.stop() }
26
+ }
27
+
28
+ async function settle() {
29
+ for (let i = 0; i < 6; i++) await flushPromises()
30
+ }
31
+
32
+ describe('usePdf', () => {
33
+ it('loads pages, their own sizes, the outline (resolved to pages) and metadata', async () => {
34
+ const { pdf } = setup('/doc.pdf')
35
+ expect(pdf.loading.value).toBe(true)
36
+ await settle()
37
+ expect(pdf.loading.value).toBe(false)
38
+ expect(pdf.numPages.value).toBe(3)
39
+ expect(pdf.pageSizes.value.map((s) => s.width)).toEqual([612, 612, 842])
40
+ expect(pdf.metadata.value.title).toBe('Fake document')
41
+ expect(pdf.outline.value[0]).toMatchObject({ title: 'One', page: 1, top: 700 })
42
+ expect(pdf.outline.value[0]!.children[0]).toMatchObject({ title: 'One.a', page: 2, top: 700 })
43
+ expect(pdf.outline.value[1]).toMatchObject({ title: 'Site', url: 'https://m3ui.dev', page: undefined })
44
+ })
45
+
46
+ it('searches every page, case and accent insensitive, across line breaks, with context', async () => {
47
+ const { pdf } = setup('/doc.pdf')
48
+ await settle()
49
+ const basil = await pdf.search('basil')
50
+ expect(basil.map((m) => m.page)).toEqual([1, 2])
51
+ expect(basil[0]!.context).toContain('grow basil and tomatoes')
52
+ // The line break between items reads as a space
53
+ expect((await pdf.search('and tomatoes')).length).toBe(1)
54
+ expect((await pdf.search('cafe creme')).length).toBe(1)
55
+ expect((await pdf.search('BASIL', { caseSensitive: true })).map((m) => m.page)).toEqual([2])
56
+ // Offsets point into the page's text
57
+ const t = await pdf.pageText(2)
58
+ const m = (await pdf.search('pesto'))[0]!
59
+ expect(t.text.slice(m.start, m.end)).toBe('pesto')
60
+ })
61
+
62
+ it('asks for a password, and again when it is wrong', async () => {
63
+ fakeDoc.password = 'm3ui'
64
+ const { pdf } = setup('/locked.pdf')
65
+ await settle()
66
+ expect(pdf.needsPassword.value).toBe(true)
67
+ expect(pdf.wrongPassword.value).toBe(false)
68
+ pdf.submitPassword('nope')
69
+ await settle()
70
+ expect(pdf.needsPassword.value).toBe(true)
71
+ expect(pdf.wrongPassword.value).toBe(true)
72
+ pdf.submitPassword('m3ui')
73
+ await settle()
74
+ expect(pdf.needsPassword.value).toBe(false)
75
+ expect(pdf.numPages.value).toBe(3)
76
+ })
77
+
78
+ it('a new source replaces the document; null clears it', async () => {
79
+ const src = ref<string | null>('/a.pdf')
80
+ const { pdf } = setup(src)
81
+ await settle()
82
+ expect(pdf.numPages.value).toBe(3)
83
+ fakeDoc.pages = fakeDoc.pages.slice(0, 1)
84
+ src.value = '/b.pdf'
85
+ await nextTick()
86
+ expect(pdf.doc.value).toBe(null)
87
+ await settle()
88
+ expect(pdf.numPages.value).toBe(1)
89
+ src.value = null
90
+ await settle()
91
+ expect(pdf.numPages.value).toBe(0)
92
+ expect(pdf.loading.value).toBe(false)
93
+ })
94
+
95
+ it('resolves named and explicit destinations', async () => {
96
+ const { pdf } = setup('/doc.pdf')
97
+ await settle()
98
+ expect(await pdf.resolveDest('ch2')).toEqual({ page: 2, top: 700 })
99
+ expect(await pdf.resolveDest([{ num: 2 }, { name: 'Fit' }])).toEqual({ page: 3, top: undefined })
100
+ expect(await pdf.resolveDest('missing')).toBe(null)
101
+ })
102
+ })