@m3ui-vue/m3ui-vue 1.2.2 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/API.md +3010 -376
- package/dist/MButton-Bl_BSUep.js +172 -0
- package/dist/MButton-Bl_BSUep.js.map +1 -0
- package/dist/MDialog-D_Yik8ou.js +173 -0
- package/dist/MDialog-D_Yik8ou.js.map +1 -0
- package/dist/MIconButton-DoNotQ9v.js +282 -0
- package/dist/MIconButton-DoNotQ9v.js.map +1 -0
- package/dist/MListItem-BxifHJdU.js +824 -0
- package/dist/MListItem-BxifHJdU.js.map +1 -0
- package/dist/MLoadingIndicator-BxbFhSX6.js +115 -0
- package/dist/MLoadingIndicator-BxbFhSX6.js.map +1 -0
- package/dist/MMenuItem-Bih0h4-n.js +604 -0
- package/dist/MMenuItem-Bih0h4-n.js.map +1 -0
- package/dist/MProgressBar-BCoruxA-.js +184 -0
- package/dist/MProgressBar-BCoruxA-.js.map +1 -0
- package/dist/MSeekBar-BeN9QcB3.js +435 -0
- package/dist/MSeekBar-BeN9QcB3.js.map +1 -0
- package/dist/MSegmentedButton-DL9JtJFX.js +65 -0
- package/dist/MSegmentedButton-DL9JtJFX.js.map +1 -0
- package/dist/MSlider-BVSbJlGS.js +691 -0
- package/dist/MSlider-BVSbJlGS.js.map +1 -0
- package/dist/MSpinner-C2SUnWk4.js +65 -0
- package/dist/MSpinner-C2SUnWk4.js.map +1 -0
- package/dist/MTextField-CEkWSZ2v.js +296 -0
- package/dist/MTextField-CEkWSZ2v.js.map +1 -0
- package/dist/MTooltip-CodJGEO-.js +756 -0
- package/dist/MTooltip-CodJGEO-.js.map +1 -0
- package/dist/_storageKeys-CYXsUBwK.js +18 -0
- package/dist/_storageKeys-CYXsUBwK.js.map +1 -0
- package/dist/_useEscapeStack-CjndQhqg.js +19 -0
- package/dist/_useEscapeStack-CjndQhqg.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/chart.js +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MAudioPlayer.vue.d.ts +115 -0
- package/dist/components/MBadge.vue.d.ts +1 -1
- package/dist/components/MBottomSheetScaffold.vue.d.ts +1 -1
- package/dist/components/MBox.vue.d.ts +1 -1
- package/dist/components/MButton.vue.d.ts +1 -1
- package/dist/components/MConfirmDialog.vue.d.ts +1 -1
- package/dist/components/MCopyButton.vue.d.ts +2 -2
- package/dist/components/MDataTable.vue.d.ts +2 -2
- package/dist/components/MDialog.vue.d.ts +2 -2
- package/dist/components/MDivider.vue.d.ts +1 -1
- package/dist/components/MDragHandle.vue.d.ts +1 -1
- package/dist/components/MDrawCanvas.vue.d.ts +150 -0
- package/dist/components/MDrawLayers.vue.d.ts +3 -0
- package/dist/components/MDrawToolbar.vue.d.ts +34 -0
- package/dist/components/MExportButton.vue.d.ts +1 -1
- package/dist/components/MFab.vue.d.ts +2 -2
- package/dist/components/MFabMenu.vue.d.ts +2 -2
- package/dist/components/MFixed.vue.d.ts +1 -1
- package/dist/components/MFlex.vue.d.ts +2 -2
- package/dist/components/MFloorPlan.vue.d.ts +248 -0
- package/dist/components/MFloorPlanInspector.vue.d.ts +56 -0
- package/dist/components/MFloorPlanToolbar.vue.d.ts +51 -0
- package/dist/components/MFlow.vue.d.ts +318 -0
- package/dist/components/MFlowControls.vue.d.ts +33 -0
- package/dist/components/MFlowHandle.vue.d.ts +16 -0
- package/dist/components/MFlowMinimap.vue.d.ts +22 -0
- package/dist/components/MFlowNode.vue.d.ts +35 -0
- package/dist/components/MGantt.vue.d.ts +141 -0
- package/dist/components/MGanttControls.vue.d.ts +32 -0
- package/dist/components/MIconButton.vue.d.ts +3 -1
- package/dist/components/MImageCropper.vue.d.ts +110 -0
- package/dist/components/MInfiniteScroll.vue.d.ts +1 -1
- package/dist/components/MLightbox.vue.d.ts +2 -2
- package/dist/components/MList.vue.d.ts +1 -1
- package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
- package/dist/components/MMapControl.vue.d.ts +1 -1
- package/dist/components/MMapSearch.vue.d.ts +1 -1
- package/dist/components/MMaskField.vue.d.ts +12 -1
- package/dist/components/MMenu.vue.d.ts +1 -1
- package/dist/components/MNavigationDrawer.vue.d.ts +1 -1
- package/dist/components/MNumberField.vue.d.ts +11 -2
- package/dist/components/MPdfViewer.vue.d.ts +174 -0
- package/dist/components/MPullToRefresh.vue.d.ts +1 -1
- package/dist/components/MResourceTimeline.vue.d.ts +149 -0
- package/dist/components/MScheduler.vue.d.ts +3 -3
- package/dist/components/MScheme.vue.d.ts +1 -1
- package/dist/components/MSearchField.vue.d.ts +1 -1
- package/dist/components/MSeekBar.vue.d.ts +1 -1
- package/dist/components/MSimpleCarousel.vue.d.ts +1 -1
- package/dist/components/MSkeleton.vue.d.ts +1 -1
- package/dist/components/MSlider.vue.d.ts +2 -2
- package/dist/components/MSparkline.vue.d.ts +1 -1
- package/dist/components/MSplitter.vue.d.ts +1 -1
- package/dist/components/MStack.vue.d.ts +2 -2
- package/dist/components/MSwipeToDismiss.vue.d.ts +1 -1
- package/dist/components/MSwitch.vue.d.ts +1 -1
- package/dist/components/MTable.vue.d.ts +1 -1
- package/dist/components/MTagInput.vue.d.ts +12 -1
- package/dist/components/MTextField.vue.d.ts +13 -2
- package/dist/components/MToolbar.vue.d.ts +1 -1
- package/dist/components/MTree.vue.d.ts +2 -2
- package/dist/components/MVideoPlayer.vue.d.ts +253 -0
- package/dist/components/MVirtualTable.vue.d.ts +1 -1
- package/dist/components/MWindow.vue.d.ts +3 -3
- package/dist/components/_MFlowEdge.vue.d.ts +9 -0
- package/dist/components/_MFlowEdgeLabel.vue.d.ts +9 -0
- package/dist/components/_MFlowNodeShell.vue.d.ts +9 -0
- package/dist/components/_MPdfPage.vue.d.ts +76 -0
- package/dist/components/_MTimeBackground.vue.d.ts +14 -0
- package/dist/components/_MTimeHeader.vue.d.ts +20 -0
- package/dist/composables/_flowPanel.d.ts +2 -0
- package/dist/composables/_flowSlot.d.ts +5 -0
- package/dist/composables/_ganttTime.d.ts +41 -0
- package/dist/composables/_useTimeAxis.d.ts +90 -0
- package/dist/composables/drawGeometry.d.ts +119 -0
- package/dist/composables/flowFormats.d.ts +79 -0
- package/dist/composables/flowLayout.d.ts +45 -0
- package/dist/composables/flowPaths.d.ts +53 -0
- package/dist/composables/ganttFormats.d.ts +52 -0
- package/dist/composables/ganttSchedule.d.ts +99 -0
- package/dist/composables/useDrawing.d.ts +44 -0
- package/dist/composables/useFloorPlan.d.ts +58 -0
- package/dist/composables/useFlow.d.ts +266 -0
- package/dist/composables/useGantt.d.ts +87 -0
- package/dist/composables/useLocale.d.ts +204 -0
- package/dist/composables/useMediaPlayer.d.ts +101 -0
- package/dist/composables/usePdf.d.ts +119 -0
- package/dist/composables/useResourceTimeline.d.ts +70 -0
- package/dist/draw.d.ts +7 -0
- package/dist/draw.js +1702 -0
- package/dist/draw.js.map +1 -0
- package/dist/flow.d.ts +14 -0
- package/dist/flow.js +3547 -0
- package/dist/flow.js.map +1 -0
- package/dist/flowPaths-CUc6X373.js +161 -0
- package/dist/flowPaths-CUc6X373.js.map +1 -0
- package/dist/gantt.d.ts +12 -0
- package/dist/gantt.js +3575 -0
- package/dist/gantt.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/locales/index.js +1435 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +8248 -7637
- package/dist/m3ui.js.map +1 -1
- package/dist/map.js +113 -108
- package/dist/map.js.map +1 -1
- package/dist/media.d.ts +5 -0
- package/dist/media.js +1311 -0
- package/dist/media.js.map +1 -0
- package/dist/pdf.d.ts +4 -0
- package/dist/pdf.js +1871 -0
- package/dist/pdf.js.map +1 -0
- package/dist/rich-text-editor.js +28 -26
- package/dist/rich-text-editor.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/{useLocale-JYxDDeXC.js → useLocale-CaWwi-b6.js} +206 -2
- package/dist/useLocale-CaWwi-b6.js.map +1 -0
- package/dist/{useThemeTokens-y0HZpyhL.js → useThemeTokens-DQQcaWbE.js} +40 -54
- package/dist/useThemeTokens-DQQcaWbE.js.map +1 -0
- package/package.json +29 -5
- package/src/__tests__/MAudioPlayer.test.ts +75 -0
- package/src/__tests__/MDrawCanvas.test.ts +118 -0
- package/src/__tests__/MFloorPlan.test.ts +299 -0
- package/src/__tests__/MFlow.test.ts +590 -0
- package/src/__tests__/MGantt.test.ts +130 -0
- package/src/__tests__/MImageCropper.test.ts +89 -0
- package/src/__tests__/MPdfViewer.test.ts +235 -0
- package/src/__tests__/MResourceTimeline.test.ts +102 -0
- package/src/__tests__/MVideoPlayer.test.ts +201 -0
- package/src/__tests__/drawGeometry.test.ts +58 -0
- package/src/__tests__/fieldExpose.test.ts +30 -0
- package/src/__tests__/flowFormats.test.ts +185 -0
- package/src/__tests__/flowLayout.test.ts +113 -0
- package/src/__tests__/ganttFormats.test.ts +76 -0
- package/src/__tests__/ganttSchedule.test.ts +136 -0
- package/src/__tests__/pdfMock.ts +83 -0
- package/src/__tests__/useMediaPlayer.test.ts +182 -0
- package/src/__tests__/usePdf.test.ts +102 -0
- package/src/components/MAudioPlayer.vue +319 -0
- package/src/components/MDrawCanvas.vue +819 -0
- package/src/components/MDrawLayers.vue +114 -0
- package/src/components/MDrawToolbar.vue +171 -0
- package/src/components/MFloorPlan.vue +1080 -0
- package/src/components/MFloorPlanInspector.vue +169 -0
- package/src/components/MFloorPlanToolbar.vue +128 -0
- package/src/components/MFlow.vue +2060 -0
- package/src/components/MFlowControls.vue +110 -0
- package/src/components/MFlowHandle.vue +60 -0
- package/src/components/MFlowMinimap.vue +134 -0
- package/src/components/MFlowNode.vue +98 -0
- package/src/components/MGantt.vue +1609 -0
- package/src/components/MGanttControls.vue +77 -0
- package/src/components/MIconButton.vue +3 -1
- package/src/components/MImageCropper.vue +507 -0
- package/src/components/MMaskField.vue +13 -0
- package/src/components/MNumberField.vue +13 -0
- package/src/components/MPdfViewer.vue +1270 -0
- package/src/components/MResourceTimeline.vue +1020 -0
- package/src/components/MTagInput.vue +13 -0
- package/src/components/MTextField.vue +16 -0
- package/src/components/MVideoPlayer.vue +839 -0
- package/src/components/_MFlowEdge.vue +100 -0
- package/src/components/_MFlowEdgeLabel.vue +56 -0
- package/src/components/_MFlowNodeShell.vue +111 -0
- package/src/components/_MPdfPage.vue +456 -0
- package/src/components/_MTimeBackground.vue +16 -0
- package/src/components/_MTimeHeader.vue +31 -0
- package/src/composables/_flowPanel.ts +13 -0
- package/src/composables/_flowSlot.ts +6 -0
- package/src/composables/_ganttTime.ts +200 -0
- package/src/composables/_useTimeAxis.ts +224 -0
- package/src/composables/drawGeometry.ts +405 -0
- package/src/composables/flowFormats.ts +916 -0
- package/src/composables/flowLayout.ts +297 -0
- package/src/composables/flowPaths.ts +239 -0
- package/src/composables/ganttFormats.ts +366 -0
- package/src/composables/ganttSchedule.ts +403 -0
- package/src/composables/useDrawing.ts +53 -0
- package/src/composables/useFloorPlan.ts +73 -0
- package/src/composables/useFlow.ts +250 -0
- package/src/composables/useGantt.ts +92 -0
- package/src/composables/useLocale.ts +410 -0
- package/src/composables/useMediaPlayer.ts +406 -0
- package/src/composables/usePdf.ts +456 -0
- package/src/composables/useResourceTimeline.ts +76 -0
- package/src/draw.ts +25 -0
- package/src/flow.ts +26 -0
- package/src/gantt.ts +12 -0
- package/src/index.ts +9 -0
- package/src/locales/de.ts +204 -0
- package/src/locales/es.ts +204 -0
- package/src/locales/fr.ts +204 -0
- package/src/locales/ja.ts +204 -0
- package/src/locales/ko.ts +204 -0
- package/src/locales/pt.ts +204 -0
- package/src/locales/zh.ts +204 -0
- package/src/media.ts +5 -0
- package/src/pdf.ts +15 -0
- package/dist/MMenuItem-D4Gx_l3F.js +0 -929
- package/dist/MMenuItem-D4Gx_l3F.js.map +0 -1
- package/dist/MSlider-Dx_S3vJ9.js +0 -1506
- package/dist/MSlider-Dx_S3vJ9.js.map +0 -1
- package/dist/MTextField-Bz8JIsnl.js +0 -633
- package/dist/MTextField-Bz8JIsnl.js.map +0 -1
- package/dist/useLocale-JYxDDeXC.js.map +0 -1
- package/dist/useThemeTokens-y0HZpyhL.js.map +0 -1
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { computed, getCurrentScope, onScopeDispose, ref, shallowRef, toValue, watch, type MaybeRefOrGetter, type Ref, type WritableComputedRef } from 'vue'
|
|
2
|
+
|
|
3
|
+
/** What the lock screen and the system's media notification show (the Media Session API's metadata) */
|
|
4
|
+
export interface MediaPlayerMetadata {
|
|
5
|
+
title?: string
|
|
6
|
+
artist?: string
|
|
7
|
+
album?: string
|
|
8
|
+
/** Cover images, largest last is fine: `[{ src, sizes: '512x512', type: 'image/png' }]` or plain URLs */
|
|
9
|
+
artwork?: (string | { src: string; sizes?: string; type?: string })[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface MediaPlayerOptions {
|
|
13
|
+
/**
|
|
14
|
+
* The `<audio>` or `<video>` element to drive (a ref or a getter; it may come and go). Without one the player
|
|
15
|
+
* runs its own clock over `duration`: for anything that isn't a media file (a canvas, a shader, generated audio)
|
|
16
|
+
* that should still play, pause, seek and change speed like one.
|
|
17
|
+
*/
|
|
18
|
+
media?: MaybeRefOrGetter<HTMLMediaElement | null | undefined>
|
|
19
|
+
/** The length in seconds, for the clock (without `media`) */
|
|
20
|
+
duration?: MaybeRefOrGetter<number | undefined>
|
|
21
|
+
/** Shown on the lock screen and the system's media notification while this player plays (Media Session API) */
|
|
22
|
+
metadata?: MaybeRefOrGetter<MediaPlayerMetadata | null | undefined>
|
|
23
|
+
/** The previous button (and the lock screen's); without it there is none */
|
|
24
|
+
onPrevious?: () => void
|
|
25
|
+
/** The next button (and the lock screen's); without it there is none */
|
|
26
|
+
onNext?: () => void
|
|
27
|
+
/** Whether there is a previous item right now (the first of a playlist has none). Default: `onPrevious` was given */
|
|
28
|
+
hasPrevious?: MaybeRefOrGetter<boolean>
|
|
29
|
+
/** Whether there is a next item right now. Default: `onNext` was given */
|
|
30
|
+
hasNext?: MaybeRefOrGetter<boolean>
|
|
31
|
+
/** Seconds `skip()` and the lock screen's seek buttons move by default. Default 10 */
|
|
32
|
+
seekStep?: number
|
|
33
|
+
/** The clock starts over at the end instead of stopping (with `media`, set the element's `loop`) */
|
|
34
|
+
loop?: MaybeRefOrGetter<boolean | undefined>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface MediaPlayer {
|
|
38
|
+
/** The media element being driven, or null (clock, or not mounted yet) */
|
|
39
|
+
media: Readonly<Ref<HTMLMediaElement | null>>
|
|
40
|
+
/** Playing or paused; writing it plays or pauses */
|
|
41
|
+
playing: WritableComputedRef<boolean>
|
|
42
|
+
/** Position in seconds; writing it seeks */
|
|
43
|
+
currentTime: WritableComputedRef<number>
|
|
44
|
+
/** Length in seconds: 0 until known, Infinity for a live stream */
|
|
45
|
+
duration: Readonly<Ref<number>>
|
|
46
|
+
/** Loaded time ranges, `[[start, end], …]` (the clock reports all of it) */
|
|
47
|
+
buffered: Readonly<Ref<[number, number][]>>
|
|
48
|
+
/** Playback speed; writing it changes it */
|
|
49
|
+
rate: WritableComputedRef<number>
|
|
50
|
+
/** Volume, 0 to 1; writing it changes it */
|
|
51
|
+
volume: WritableComputedRef<number>
|
|
52
|
+
/** Muted; writing it mutes or unmutes */
|
|
53
|
+
muted: WritableComputedRef<boolean>
|
|
54
|
+
/** Stalled for data (a spinner's moment). Writable for the clock, to simulate buffering: it stops advancing meanwhile */
|
|
55
|
+
waiting: Ref<boolean>
|
|
56
|
+
/** Reached the end; `play()` or `toggle()` start over */
|
|
57
|
+
ended: Readonly<Ref<boolean>>
|
|
58
|
+
/** A live stream (infinite duration): no seeking */
|
|
59
|
+
live: Readonly<Ref<boolean>>
|
|
60
|
+
/** The media element's error, if loading or decoding failed */
|
|
61
|
+
error: Readonly<Ref<MediaError | null>>
|
|
62
|
+
/** The browser refused to start playing (autoplay with sound before any user gesture) */
|
|
63
|
+
blocked: Readonly<Ref<boolean>>
|
|
64
|
+
/** Whether there is a previous / next item (see the options of the same names) */
|
|
65
|
+
hasPrevious: Readonly<Ref<boolean>>
|
|
66
|
+
hasNext: Readonly<Ref<boolean>>
|
|
67
|
+
play: () => Promise<void>
|
|
68
|
+
pause: () => void
|
|
69
|
+
toggle: () => Promise<void>
|
|
70
|
+
/** Seeks to a time in seconds (clamped to the media) */
|
|
71
|
+
seek: (time: number) => void
|
|
72
|
+
/** Seeks by an offset in seconds, `seekStep` by default (negative goes back) */
|
|
73
|
+
skip: (seconds?: number) => void
|
|
74
|
+
/** Pauses and goes back to the start */
|
|
75
|
+
stop: () => void
|
|
76
|
+
previous: () => void
|
|
77
|
+
next: () => void
|
|
78
|
+
/**
|
|
79
|
+
* Drives this element instead of the `media` option (null: the clock; undefined: back to `media`). What
|
|
80
|
+
* `MVideoPlayer`/`MAudioPlayer` call with their own element when given a `player`: rate, volume and mute carry over
|
|
81
|
+
*/
|
|
82
|
+
attach: (el: HTMLMediaElement | null | undefined) => void
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const hasWindow = typeof window !== 'undefined'
|
|
86
|
+
const now = () => (typeof performance !== 'undefined' ? performance.now() : Date.now())
|
|
87
|
+
|
|
88
|
+
// The player that last started playing owns the Media Session (there is one per page)
|
|
89
|
+
let sessionOwner: object | null = null
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The state and controls of something that plays: an `<audio>`/`<video>` element, or a clock of its own for
|
|
93
|
+
* anything else. Everything is reactive, the writable parts (`playing`, `currentTime`, `rate`, `volume`, `muted`)
|
|
94
|
+
* act on the media when written, and the player takes the Media Session while it plays (lock screen and
|
|
95
|
+
* notification controls, with `metadata`). `MVideoPlayer` and `MAudioPlayer` are built on it; pass one to their
|
|
96
|
+
* `player` prop to keep playing across pages, or build a player UI of your own on it.
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* const audio = ref<HTMLAudioElement>()
|
|
100
|
+
* const player = useMediaPlayer({ media: audio, metadata: () => ({ title: track.value.title }) })
|
|
101
|
+
* // <audio ref="audio" :src="track.src" /> … <button @click="player.toggle()">
|
|
102
|
+
*
|
|
103
|
+
* // A shader "video" 90 seconds long: its time is player.currentTime
|
|
104
|
+
* const clock = useMediaPlayer({ duration: 90 })
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export function useMediaPlayer(options: MediaPlayerOptions = {}): MediaPlayer {
|
|
108
|
+
const owner = {}
|
|
109
|
+
const step = options.seekStep ?? 10
|
|
110
|
+
const media = shallowRef<HTMLMediaElement | null>(null)
|
|
111
|
+
const attached = shallowRef<HTMLMediaElement | null | undefined>(undefined)
|
|
112
|
+
|
|
113
|
+
const playingState = ref(false)
|
|
114
|
+
const time = ref(0)
|
|
115
|
+
const durationState = ref(0)
|
|
116
|
+
const buffered = ref<[number, number][]>([])
|
|
117
|
+
const rateState = ref(1)
|
|
118
|
+
const volumeState = ref(1)
|
|
119
|
+
const mutedState = ref(false)
|
|
120
|
+
const waiting = ref(false)
|
|
121
|
+
const ended = ref(false)
|
|
122
|
+
const error = shallowRef<MediaError | null>(null)
|
|
123
|
+
const blocked = ref(false)
|
|
124
|
+
const live = computed(() => durationState.value === Infinity)
|
|
125
|
+
|
|
126
|
+
// ── Clock (no media element) ──────────────────────────────────────────────────────────────────────────
|
|
127
|
+
let raf = 0
|
|
128
|
+
let lastFrame = 0
|
|
129
|
+
const clockDuration = () => {
|
|
130
|
+
const d = toValue(options.duration)
|
|
131
|
+
return d !== undefined && Number.isFinite(d) && d > 0 ? d : 0
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function tick(t: number) {
|
|
135
|
+
raf = 0
|
|
136
|
+
if (media.value || !playingState.value) {
|
|
137
|
+
lastFrame = 0
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
const dt = lastFrame ? Math.min(0.25, (t - lastFrame) / 1000) : 0
|
|
141
|
+
lastFrame = t
|
|
142
|
+
if (!waiting.value) {
|
|
143
|
+
const d = durationState.value
|
|
144
|
+
let next = time.value + dt * rateState.value
|
|
145
|
+
if (d && next >= d) {
|
|
146
|
+
if (toValue(options.loop)) next %= d
|
|
147
|
+
else {
|
|
148
|
+
next = d
|
|
149
|
+
playingState.value = false
|
|
150
|
+
ended.value = true
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
time.value = next
|
|
154
|
+
}
|
|
155
|
+
if (playingState.value) raf = requestAnimationFrame(tick)
|
|
156
|
+
else lastFrame = 0
|
|
157
|
+
}
|
|
158
|
+
function runClock() {
|
|
159
|
+
if (raf || !hasWindow || media.value || !playingState.value) return
|
|
160
|
+
lastFrame = 0
|
|
161
|
+
raf = requestAnimationFrame(tick)
|
|
162
|
+
}
|
|
163
|
+
watch(() => (media.value ? null : clockDuration()), (d) => {
|
|
164
|
+
if (d === null) return
|
|
165
|
+
durationState.value = d
|
|
166
|
+
buffered.value = d ? [[0, d]] : []
|
|
167
|
+
if (d && time.value > d) time.value = d
|
|
168
|
+
}, { immediate: true })
|
|
169
|
+
|
|
170
|
+
// ── Media element ─────────────────────────────────────────────────────────────────────────────────────
|
|
171
|
+
function readRanges(el: HTMLMediaElement) {
|
|
172
|
+
const out: [number, number][] = []
|
|
173
|
+
const r = el.buffered
|
|
174
|
+
for (let i = 0; i < (r?.length ?? 0); i++) out.push([r.start(i), r.end(i)])
|
|
175
|
+
buffered.value = out
|
|
176
|
+
}
|
|
177
|
+
function readAll(el: HTMLMediaElement) {
|
|
178
|
+
playingState.value = !el.paused && !el.ended
|
|
179
|
+
time.value = el.currentTime || 0
|
|
180
|
+
durationState.value = Number.isNaN(el.duration) ? 0 : el.duration
|
|
181
|
+
rateState.value = el.playbackRate || 1
|
|
182
|
+
volumeState.value = el.volume
|
|
183
|
+
mutedState.value = el.muted
|
|
184
|
+
ended.value = el.ended
|
|
185
|
+
error.value = el.error
|
|
186
|
+
readRanges(el)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const handlers: Record<string, (el: HTMLMediaElement) => void> = {
|
|
190
|
+
play: () => { playingState.value = true; ended.value = false; blocked.value = false; claimSession() },
|
|
191
|
+
playing: () => { playingState.value = true; waiting.value = false },
|
|
192
|
+
pause: () => { playingState.value = false },
|
|
193
|
+
waiting: () => { waiting.value = true },
|
|
194
|
+
canplay: () => { waiting.value = false },
|
|
195
|
+
seeked: (el) => { time.value = el.currentTime; waiting.value = false; syncPosition() },
|
|
196
|
+
timeupdate: (el) => { time.value = el.currentTime },
|
|
197
|
+
durationchange: (el) => { durationState.value = Number.isNaN(el.duration) ? 0 : el.duration; syncPosition() },
|
|
198
|
+
loadedmetadata: (el) => readAll(el),
|
|
199
|
+
progress: (el) => readRanges(el),
|
|
200
|
+
ratechange: (el) => { rateState.value = el.playbackRate; syncPosition() },
|
|
201
|
+
volumechange: (el) => { volumeState.value = el.volume; mutedState.value = el.muted },
|
|
202
|
+
ended: () => { ended.value = true; playingState.value = false },
|
|
203
|
+
error: (el) => { error.value = el.error; waiting.value = false; playingState.value = false },
|
|
204
|
+
emptied: (el) => { readAll(el); waiting.value = false },
|
|
205
|
+
}
|
|
206
|
+
const listeners = new Map<string, EventListener>()
|
|
207
|
+
|
|
208
|
+
watch(() => (attached.value !== undefined ? attached.value : toValue(options.media)) ?? null, (el, old) => {
|
|
209
|
+
if (old) for (const [type, fn] of listeners) old.removeEventListener(type, fn)
|
|
210
|
+
listeners.clear()
|
|
211
|
+
media.value = el
|
|
212
|
+
if (!el) {
|
|
213
|
+
ended.value = false
|
|
214
|
+
waiting.value = false
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
cancelAnimationFrame(raf)
|
|
218
|
+
raf = 0
|
|
219
|
+
// A volume, rate or mute set before the element came keeps applying to it
|
|
220
|
+
el.playbackRate = rateState.value
|
|
221
|
+
el.volume = volumeState.value
|
|
222
|
+
if (mutedState.value) el.muted = true
|
|
223
|
+
// So does a position set before it (a start time): a fresh element starts there
|
|
224
|
+
if (time.value > 0 && !el.currentTime) el.currentTime = time.value
|
|
225
|
+
for (const [type, fn] of Object.entries(handlers)) {
|
|
226
|
+
const l = () => fn(el)
|
|
227
|
+
listeners.set(type, l)
|
|
228
|
+
el.addEventListener(type, l)
|
|
229
|
+
}
|
|
230
|
+
readAll(el)
|
|
231
|
+
}, { immediate: true })
|
|
232
|
+
|
|
233
|
+
// ── Actions ───────────────────────────────────────────────────────────────────────────────────────────
|
|
234
|
+
async function play() {
|
|
235
|
+
const el = media.value
|
|
236
|
+
if (ended.value) seek(0)
|
|
237
|
+
ended.value = false
|
|
238
|
+
if (!el) {
|
|
239
|
+
playingState.value = true
|
|
240
|
+
claimSession()
|
|
241
|
+
runClock()
|
|
242
|
+
return
|
|
243
|
+
}
|
|
244
|
+
try {
|
|
245
|
+
await el.play()
|
|
246
|
+
} catch (e) {
|
|
247
|
+
// Autoplay with sound before a user gesture; a newer load() or pause() interrupting play() is no failure
|
|
248
|
+
if ((e as DOMException)?.name === 'NotAllowedError') blocked.value = true
|
|
249
|
+
playingState.value = !el.paused
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
function pause() {
|
|
253
|
+
if (media.value) media.value.pause()
|
|
254
|
+
else playingState.value = false
|
|
255
|
+
}
|
|
256
|
+
const toggle = () => (playingState.value ? (pause(), Promise.resolve()) : play())
|
|
257
|
+
|
|
258
|
+
function seek(t: number) {
|
|
259
|
+
if (live.value) return
|
|
260
|
+
const d = durationState.value
|
|
261
|
+
const target = Math.max(0, d ? Math.min(d, t) : t)
|
|
262
|
+
if (target < d) ended.value = false
|
|
263
|
+
time.value = target
|
|
264
|
+
const el = media.value
|
|
265
|
+
if (el) {
|
|
266
|
+
// fastSeek is cheaper but lands on a keyframe: only worth it mid-scrub, so seek precisely
|
|
267
|
+
el.currentTime = target
|
|
268
|
+
}
|
|
269
|
+
syncPosition()
|
|
270
|
+
}
|
|
271
|
+
const skip = (s: number = step) => seek(time.value + s)
|
|
272
|
+
function stop() {
|
|
273
|
+
pause()
|
|
274
|
+
seek(0)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
watch(playingState, (on) => {
|
|
278
|
+
if (on) runClock()
|
|
279
|
+
if (sessionOwner === owner && hasWindow && 'mediaSession' in navigator) {
|
|
280
|
+
navigator.mediaSession.playbackState = on ? 'playing' : 'paused'
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
// ── Media Session ─────────────────────────────────────────────────────────────────────────────────────
|
|
285
|
+
const session = () => (hasWindow && 'mediaSession' in navigator ? navigator.mediaSession : null)
|
|
286
|
+
|
|
287
|
+
function setMetadata() {
|
|
288
|
+
const s = session()
|
|
289
|
+
const m = toValue(options.metadata)
|
|
290
|
+
if (!s || sessionOwner !== owner || typeof MediaMetadata === 'undefined') return
|
|
291
|
+
s.metadata = m
|
|
292
|
+
? new MediaMetadata({
|
|
293
|
+
title: m.title ?? '',
|
|
294
|
+
artist: m.artist ?? '',
|
|
295
|
+
album: m.album ?? '',
|
|
296
|
+
artwork: (m.artwork ?? []).map((a) => (typeof a === 'string' ? { src: a } : a)),
|
|
297
|
+
})
|
|
298
|
+
: null
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function syncPosition() {
|
|
302
|
+
const s = session()
|
|
303
|
+
const d = durationState.value
|
|
304
|
+
if (!s || sessionOwner !== owner || !s.setPositionState) return
|
|
305
|
+
try {
|
|
306
|
+
if (d && Number.isFinite(d)) s.setPositionState({ duration: d, playbackRate: rateState.value, position: Math.min(d, time.value) })
|
|
307
|
+
else s.setPositionState()
|
|
308
|
+
} catch { /* a rate the browser rejects, say */ }
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function setAction(s: MediaSession, action: MediaSessionAction, fn: MediaSessionActionHandler | null) {
|
|
312
|
+
try { s.setActionHandler(action, fn) } catch { /* an action this browser doesn't know */ }
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function claimSession() {
|
|
316
|
+
const s = session()
|
|
317
|
+
if (!s || options.metadata === undefined) return
|
|
318
|
+
sessionOwner = owner
|
|
319
|
+
setMetadata()
|
|
320
|
+
setAction(s, 'play', () => { play() })
|
|
321
|
+
setAction(s, 'pause', () => pause())
|
|
322
|
+
setAction(s, 'stop', () => stop())
|
|
323
|
+
setAction(s, 'seekbackward', (d) => skip(-(d.seekOffset ?? step)))
|
|
324
|
+
setAction(s, 'seekforward', (d) => skip(d.seekOffset ?? step))
|
|
325
|
+
setAction(s, 'seekto', (d) => { if (d.seekTime !== undefined) seek(d.seekTime) })
|
|
326
|
+
setAction(s, 'previoustrack', options.onPrevious ? () => options.onPrevious!() : null)
|
|
327
|
+
setAction(s, 'nexttrack', options.onNext ? () => options.onNext!() : null)
|
|
328
|
+
s.playbackState = playingState.value ? 'playing' : 'paused'
|
|
329
|
+
syncPosition()
|
|
330
|
+
}
|
|
331
|
+
if (options.metadata !== undefined) watch(() => toValue(options.metadata), setMetadata, { deep: true })
|
|
332
|
+
|
|
333
|
+
function releaseSession() {
|
|
334
|
+
const s = session()
|
|
335
|
+
if (!s || sessionOwner !== owner) return
|
|
336
|
+
sessionOwner = null
|
|
337
|
+
s.metadata = null
|
|
338
|
+
s.playbackState = 'none'
|
|
339
|
+
for (const a of ['play', 'pause', 'stop', 'seekbackward', 'seekforward', 'seekto', 'previoustrack', 'nexttrack'] as MediaSessionAction[]) setAction(s, a, null)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (getCurrentScope()) {
|
|
343
|
+
onScopeDispose(() => {
|
|
344
|
+
cancelAnimationFrame(raf)
|
|
345
|
+
const el = media.value
|
|
346
|
+
if (el) for (const [type, fn] of listeners) el.removeEventListener(type, fn)
|
|
347
|
+
releaseSession()
|
|
348
|
+
})
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// ── Writable state ────────────────────────────────────────────────────────────────────────────────────
|
|
352
|
+
const playing = computed({
|
|
353
|
+
get: () => playingState.value,
|
|
354
|
+
set: (v) => { if (v !== playingState.value) v ? play() : pause() },
|
|
355
|
+
})
|
|
356
|
+
const currentTime = computed({ get: () => time.value, set: (t) => seek(t) })
|
|
357
|
+
const rate = computed({
|
|
358
|
+
get: () => rateState.value,
|
|
359
|
+
set: (r) => {
|
|
360
|
+
rateState.value = r
|
|
361
|
+
if (media.value) media.value.playbackRate = r
|
|
362
|
+
syncPosition()
|
|
363
|
+
},
|
|
364
|
+
})
|
|
365
|
+
const volume = computed({
|
|
366
|
+
get: () => volumeState.value,
|
|
367
|
+
set: (v) => {
|
|
368
|
+
volumeState.value = Math.min(1, Math.max(0, v))
|
|
369
|
+
if (media.value) media.value.volume = volumeState.value
|
|
370
|
+
},
|
|
371
|
+
})
|
|
372
|
+
const muted = computed({
|
|
373
|
+
get: () => mutedState.value,
|
|
374
|
+
set: (m) => {
|
|
375
|
+
mutedState.value = m
|
|
376
|
+
if (media.value) media.value.muted = m
|
|
377
|
+
},
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
return {
|
|
381
|
+
media,
|
|
382
|
+
playing,
|
|
383
|
+
currentTime,
|
|
384
|
+
duration: durationState,
|
|
385
|
+
buffered,
|
|
386
|
+
rate,
|
|
387
|
+
volume,
|
|
388
|
+
muted,
|
|
389
|
+
waiting,
|
|
390
|
+
ended,
|
|
391
|
+
live,
|
|
392
|
+
error,
|
|
393
|
+
blocked,
|
|
394
|
+
hasPrevious: computed(() => !!options.onPrevious && (toValue(options.hasPrevious) ?? true)),
|
|
395
|
+
hasNext: computed(() => !!options.onNext && (toValue(options.hasNext) ?? true)),
|
|
396
|
+
play,
|
|
397
|
+
pause,
|
|
398
|
+
toggle,
|
|
399
|
+
seek,
|
|
400
|
+
skip,
|
|
401
|
+
stop,
|
|
402
|
+
previous: () => options.onPrevious?.(),
|
|
403
|
+
next: () => options.onNext?.(),
|
|
404
|
+
attach: (el) => { attached.value = el },
|
|
405
|
+
}
|
|
406
|
+
}
|