@joewinke/jatui 0.34.2 → 0.35.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.
- package/CLAUDE.md +1 -0
- package/dist/components/ListenControl.svelte +1064 -0
- package/dist/components/ListenControl.svelte.d.ts +87 -0
- package/dist/components/table/DataTable.svelte.d.ts +2 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.js +6 -0
- package/dist/listen/keys.d.ts +36 -0
- package/dist/listen/keys.js +68 -0
- package/dist/listen/mediaSession.d.ts +90 -0
- package/dist/listen/mediaSession.js +108 -0
- package/dist/listen/prefs.d.ts +49 -0
- package/dist/listen/prefs.js +97 -0
- package/dist/listen/segments.d.ts +101 -0
- package/dist/listen/segments.js +224 -0
- package/dist/listen/types.d.ts +81 -0
- package/dist/listen/types.js +7 -0
- package/package.json +1 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
export type { ListenSegment, ListenChapter, ListenVoice, ListenSegmentPage, ListenSegmentSource, ListenStatus, ListenApi } from '../listen/types';
|
|
2
|
+
/**
|
|
3
|
+
* ListenControl — "listen to this page" as one compact control.
|
|
4
|
+
*
|
|
5
|
+
* A small pill that expands into a playback bar: play/pause, −15s/+15s and
|
|
6
|
+
* previous/next sentence, speed (0.75–2×), a voice picker, a document-wide
|
|
7
|
+
* progress bar with chapter/section ticks, time, close. Built for hosts
|
|
8
|
+
* that synthesize audio per sentence (TTS): the host supplies an async
|
|
9
|
+
* SEGMENT SOURCE — sentences/paragraphs with ids → audio URLs — and the
|
|
10
|
+
* player starts on the first chunk while the rest synthesizes, hatching
|
|
11
|
+
* the not-yet-real tail of the progress bar. Every segment boundary fires
|
|
12
|
+
* `onSegmentChange(id)` so the host can highlight + scroll-follow.
|
|
13
|
+
*
|
|
14
|
+
* ── One timeline across per-sentence files ──────────────────────────────
|
|
15
|
+
* A "listen" player is a playlist, not one media element. Each segment has
|
|
16
|
+
* its own URL, so the engine walks segments sequentially through ONE
|
|
17
|
+
* hidden `<audio>` element and reports ONE document-wide time: the
|
|
18
|
+
* elapsed clock, the progress fill, the ticks, a ±15s skip that crosses a
|
|
19
|
+
* boundary and a seek from the slider all map through `buildTimeline`'s
|
|
20
|
+
* offsets (listen/segments.ts) — the same one-axis discipline as the
|
|
21
|
+
* VideoPlayer band. Durations the host knows (`durationSec`, TTS usually
|
|
22
|
+
* reports them) lay the timeline out immediately; unknown ones are
|
|
23
|
+
* measured on load and ESTIMATED until then, self-correcting as they land.
|
|
24
|
+
*
|
|
25
|
+
* The transport row borrows VideoPlayer's `kind="audio"` / `transport=
|
|
26
|
+
* "band"` shape (round primary play, ghost transport buttons, tabular
|
|
27
|
+
* clock) and reuses its pure time formatting (`video/time.ts`); the
|
|
28
|
+
* per-segment playlist engine is what doesn't fit inside VideoPlayer and
|
|
29
|
+
* lives here instead.
|
|
30
|
+
*
|
|
31
|
+
* ── Media Session ───────────────────────────────────────────────────────
|
|
32
|
+
* Lock-screen/headset/OS controls are wired while open: metadata (the
|
|
33
|
+
* `title` prop + current voice), play/pause, previous/next track (the
|
|
34
|
+
* sentence skip), seekbackward/seekforward/seekto, and ONE document-wide
|
|
35
|
+
* position state. Feature-detected end to end — nothing runs where the
|
|
36
|
+
* API is missing (listen/mediaSession.ts).
|
|
37
|
+
*
|
|
38
|
+
* ── Scope honesty ───────────────────────────────────────────────────────
|
|
39
|
+
* The keyboard map (Space/K, ←/→ sentences, Shift+←/→ ±15s, [ / ] speed,
|
|
40
|
+
* Escape) fires only while focus is inside the player — a library
|
|
41
|
+
* component must not hijack the host page's keys. Hosts wanting global
|
|
42
|
+
* keys bind `api` and wire their own handler with the same actions.
|
|
43
|
+
*
|
|
44
|
+
* Theming is token-only (DaisyUI semantic vars); motion is reduced-motion
|
|
45
|
+
* aware; everything is labelled; on narrow screens the expanded bar
|
|
46
|
+
* becomes a fixed sticky mini-bar at the bottom.
|
|
47
|
+
*/
|
|
48
|
+
import { type Snippet } from 'svelte';
|
|
49
|
+
import type { ListenApi, ListenChapter, ListenSegment, ListenSegmentSource, ListenVoice } from '../listen/types';
|
|
50
|
+
interface Props {
|
|
51
|
+
/**
|
|
52
|
+
* Host-supplied async segment source (sentences/paragraphs with ids →
|
|
53
|
+
* audio). Called per voice; pages arrive as chunks synthesize and are
|
|
54
|
+
* merged by id, so a later page can fill a segment's `url` in late.
|
|
55
|
+
* Pass a STABLE reference — a fresh inline arrow re-runs the source.
|
|
56
|
+
*/
|
|
57
|
+
source: ListenSegmentSource;
|
|
58
|
+
/** The voices the host offers. Empty ⇒ no picker (one implicit voice, ''). */
|
|
59
|
+
voices?: ListenVoice[];
|
|
60
|
+
/** The picked voice (bindable). Default: the first voice, persisted to storage. */
|
|
61
|
+
voiceId?: string;
|
|
62
|
+
/** Sections for the progress ticks + the caption's chapter label. */
|
|
63
|
+
chapters?: ListenChapter[];
|
|
64
|
+
/** Document title — lock-screen metadata + the bar's aria-label. */
|
|
65
|
+
title?: string;
|
|
66
|
+
/** The pill's label. Default `Listen`. */
|
|
67
|
+
triggerLabel?: string;
|
|
68
|
+
/** Expanded state (bindable). */
|
|
69
|
+
open?: boolean;
|
|
70
|
+
/** Load the source on mount instead of waiting for the first open (TTS cost is the host's call). */
|
|
71
|
+
preload?: boolean;
|
|
72
|
+
/** Estimate for a segment whose duration isn't known yet. Default 3s. */
|
|
73
|
+
estimateSec?: number;
|
|
74
|
+
/** localStorage key prefix for speed/voice. Default `jatui.listen` (→ `jatui.listen:prefs`). */
|
|
75
|
+
storageKey?: string;
|
|
76
|
+
/** Segment-boundary hook — host-side highlight + scroll-follow. */
|
|
77
|
+
onSegmentChange?: (id: string | null, segment: ListenSegment | null) => void;
|
|
78
|
+
onOpenChange?: (open: boolean) => void;
|
|
79
|
+
/** Instance API for hosts wiring their own global keys / external controls. */
|
|
80
|
+
api?: ListenApi;
|
|
81
|
+
class?: string;
|
|
82
|
+
/** Custom trigger content (replaces the headphones+label default). */
|
|
83
|
+
children?: Snippet;
|
|
84
|
+
}
|
|
85
|
+
declare const ListenControl: import("svelte").Component<Props, {}, "open" | "voiceId" | "api">;
|
|
86
|
+
type ListenControl = ReturnType<typeof ListenControl>;
|
|
87
|
+
export default ListenControl;
|
|
@@ -2,7 +2,7 @@ import type { DataTableProps } from './types.js';
|
|
|
2
2
|
declare function $$render<TData>(): {
|
|
3
3
|
props: DataTableProps<TData>;
|
|
4
4
|
exports: {};
|
|
5
|
-
bindings: "
|
|
5
|
+
bindings: "api" | "collapsed" | "sorting";
|
|
6
6
|
slots: {};
|
|
7
7
|
events: {};
|
|
8
8
|
};
|
|
@@ -10,7 +10,7 @@ declare class __sveltets_Render<TData> {
|
|
|
10
10
|
props(): ReturnType<typeof $$render<TData>>['props'];
|
|
11
11
|
events(): ReturnType<typeof $$render<TData>>['events'];
|
|
12
12
|
slots(): ReturnType<typeof $$render<TData>>['slots'];
|
|
13
|
-
bindings(): "
|
|
13
|
+
bindings(): "api" | "collapsed" | "sorting";
|
|
14
14
|
exports(): {};
|
|
15
15
|
}
|
|
16
16
|
interface $$IsomorphicComponent {
|
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,10 @@ export type { ChapterBlock, ChapterStateVisual } from './components/replay/Chapt
|
|
|
18
18
|
export { default as TranscriptPlayer } from './components/TranscriptPlayer.svelte';
|
|
19
19
|
export type { TranscriptSegment, TranscriptBookmark, TranscriptChapter, TranscriptSpeakerTalkTime, } from './components/TranscriptPlayer.svelte';
|
|
20
20
|
export { default as VideoPlayer } from './components/VideoPlayer.svelte';
|
|
21
|
+
export { default as ListenControl } from './components/ListenControl.svelte';
|
|
22
|
+
export type { ListenSegment, ListenChapter, ListenVoice, ListenSegmentPage, ListenSegmentSource, ListenStatus, ListenApi } from './listen/types';
|
|
23
|
+
export { LISTEN_KEYS } from './listen/keys';
|
|
24
|
+
export { LISTEN_SPEEDS, SPEED_FLOOR, SPEED_CEIL, clampSpeed, stepSpeed, formatListenSpeed } from './listen/prefs';
|
|
21
25
|
export type { VideoChapter, VideoSpeakerTurn, VideoTranscriptSegment, VideoBookmark, VideoWaveform, VideoSpeakerTalkTime, VideoLumaTrack, VideoMarker, BandState, BandAxis, NightMode, NightWindow, AudioState, AudioStatus, AudioBrokenReason, VideoAdSpan, RateReport, WindDownState, WindDownPhase, PresenceSource, VideoResumeStore, FlipSource, } from './components/VideoPlayer.svelte';
|
|
22
26
|
export { speakerRateMap, toRateTurns, toRateSegments } from './video/rate';
|
|
23
27
|
export { idleDetectionSupported, idleDetectionPermission, requestIdleDetection, localResumeStore, flipOpen, flipClose, clearPoster, type IdlePermission } from './video/presence';
|
package/dist/index.js
CHANGED
|
@@ -33,6 +33,12 @@ export { default as TranscriptPlayer } from './components/TranscriptPlayer.svelt
|
|
|
33
33
|
// optional so a bare mp4 still gets the full playback UX. TranscriptPlayer
|
|
34
34
|
// stays exported unchanged — meetings already depends on it.
|
|
35
35
|
export { default as VideoPlayer } from './components/VideoPlayer.svelte';
|
|
36
|
+
// ListenControl (jatui-eev2n) — "listen to this page" playback over the host's
|
|
37
|
+
// per-sentence audio source. The component re-exports its types too; these
|
|
38
|
+
// are the same names, so either import site works.
|
|
39
|
+
export { default as ListenControl } from './components/ListenControl.svelte';
|
|
40
|
+
export { LISTEN_KEYS } from './listen/keys';
|
|
41
|
+
export { LISTEN_SPEEDS, SPEED_FLOOR, SPEED_CEIL, clampSpeed, stepSpeed, formatListenSpeed } from './listen/prefs';
|
|
36
42
|
// Rate composer (mos-5lzns.32): the same name → rate table the player applies
|
|
37
43
|
// under `speakerPace` (manual beats measured), for a host drawing its own
|
|
38
44
|
// per-speaker speed UI.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ListenControl keyboard map (jatui-eev2n) — the same single-source discipline
|
|
3
|
+
* as `video/keys.ts`: a key that exists is in the map, a key in the map
|
|
4
|
+
* exists. The component's keydown handler and any host help surface both read
|
|
5
|
+
* LISTEN_KEYS.
|
|
6
|
+
*
|
|
7
|
+
* Map parity: Space/K play-pause, ←/→ prev/next SENTENCE (a reading player's
|
|
8
|
+
* primary navigation — the segments ARE the sentences), Shift+←/→ the ±15s
|
|
9
|
+
* skip, [ / ] speed down/up, Escape close. Deliberately NOT bound globally:
|
|
10
|
+
* the map fires only while focus is inside the player (a library component
|
|
11
|
+
* must not hijack the host page's keys — hosts wanting global keys wire the
|
|
12
|
+
* exported `ListenApi` themselves).
|
|
13
|
+
*/
|
|
14
|
+
export interface ListenKeyRow {
|
|
15
|
+
/** Keycap label — `/`-separated alternates, as shown in a help table. */
|
|
16
|
+
keys: string;
|
|
17
|
+
description: string;
|
|
18
|
+
}
|
|
19
|
+
export declare const LISTEN_KEYS: ListenKeyRow[];
|
|
20
|
+
export type ListenKeyAction = 'toggle' | 'prev' | 'next' | 'back' | 'forward' | 'slower' | 'faster' | 'close';
|
|
21
|
+
/**
|
|
22
|
+
* One keydown → one action (or null). Pure: takes the two KeyboardEvent
|
|
23
|
+
* fields the map needs, so a synthetic event in node tests resolves the same
|
|
24
|
+
* way a real one does.
|
|
25
|
+
*/
|
|
26
|
+
export declare function resolveListenKey(event: {
|
|
27
|
+
key: string;
|
|
28
|
+
shiftKey?: boolean;
|
|
29
|
+
}): ListenKeyAction | null;
|
|
30
|
+
/**
|
|
31
|
+
* Targets whose keys already mean something native — a focused button gets
|
|
32
|
+
* Space as its activation key, the progress `<input type=range>` owns the
|
|
33
|
+
* arrows, a `<select>` owns typeahead. The component skips the map for these
|
|
34
|
+
* (Escape excepted — closing the player outranks every widget).
|
|
35
|
+
*/
|
|
36
|
+
export declare function isNativeControlTarget(target: EventTarget | null): boolean;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ListenControl keyboard map (jatui-eev2n) — the same single-source discipline
|
|
3
|
+
* as `video/keys.ts`: a key that exists is in the map, a key in the map
|
|
4
|
+
* exists. The component's keydown handler and any host help surface both read
|
|
5
|
+
* LISTEN_KEYS.
|
|
6
|
+
*
|
|
7
|
+
* Map parity: Space/K play-pause, ←/→ prev/next SENTENCE (a reading player's
|
|
8
|
+
* primary navigation — the segments ARE the sentences), Shift+←/→ the ±15s
|
|
9
|
+
* skip, [ / ] speed down/up, Escape close. Deliberately NOT bound globally:
|
|
10
|
+
* the map fires only while focus is inside the player (a library component
|
|
11
|
+
* must not hijack the host page's keys — hosts wanting global keys wire the
|
|
12
|
+
* exported `ListenApi` themselves).
|
|
13
|
+
*/
|
|
14
|
+
import { isTypingTarget } from '../utils/keyboardShortcutRegistry';
|
|
15
|
+
export const LISTEN_KEYS = [
|
|
16
|
+
{ keys: 'Space / K', description: 'Play / pause' },
|
|
17
|
+
{ keys: '← / →', description: 'Previous / next sentence' },
|
|
18
|
+
{ keys: 'Shift + ← / →', description: 'Skip ∓15s' },
|
|
19
|
+
{ keys: '[ / ]', description: 'Speed down / up (0.75–2×)' },
|
|
20
|
+
{ keys: 'Escape', description: 'Close the player' }
|
|
21
|
+
];
|
|
22
|
+
/**
|
|
23
|
+
* One keydown → one action (or null). Pure: takes the two KeyboardEvent
|
|
24
|
+
* fields the map needs, so a synthetic event in node tests resolves the same
|
|
25
|
+
* way a real one does.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveListenKey(event) {
|
|
28
|
+
const key = typeof event?.key === 'string' ? event.key : '';
|
|
29
|
+
const shift = event?.shiftKey === true;
|
|
30
|
+
switch (key) {
|
|
31
|
+
case ' ':
|
|
32
|
+
case 'Spacebar': // legacy IE/Edge spelling
|
|
33
|
+
return shift ? null : 'toggle';
|
|
34
|
+
case 'k':
|
|
35
|
+
case 'K':
|
|
36
|
+
return 'toggle';
|
|
37
|
+
case 'ArrowLeft':
|
|
38
|
+
return shift ? 'back' : 'prev';
|
|
39
|
+
case 'ArrowRight':
|
|
40
|
+
return shift ? 'forward' : 'next';
|
|
41
|
+
case '[':
|
|
42
|
+
return 'slower';
|
|
43
|
+
case ']':
|
|
44
|
+
return 'faster';
|
|
45
|
+
case 'Escape':
|
|
46
|
+
return 'close';
|
|
47
|
+
default:
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Targets whose keys already mean something native — a focused button gets
|
|
53
|
+
* Space as its activation key, the progress `<input type=range>` owns the
|
|
54
|
+
* arrows, a `<select>` owns typeahead. The component skips the map for these
|
|
55
|
+
* (Escape excepted — closing the player outranks every widget).
|
|
56
|
+
*/
|
|
57
|
+
export function isNativeControlTarget(target) {
|
|
58
|
+
if (isTypingTarget(target))
|
|
59
|
+
return true;
|
|
60
|
+
// Duck-typed (not `instanceof`) so node tests without DOM globals work.
|
|
61
|
+
// `<input type=range>` (the progress bar) is not a typing target but still
|
|
62
|
+
// owns its own arrow keys natively — as does any other input.
|
|
63
|
+
const el = target;
|
|
64
|
+
if (typeof el?.tagName !== 'string')
|
|
65
|
+
return false;
|
|
66
|
+
const tag = el.tagName.toUpperCase();
|
|
67
|
+
return tag === 'BUTTON' || tag === 'INPUT';
|
|
68
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ListenControl Media Session glue (jatui-eev2n) — lock-screen / headset /
|
|
3
|
+
* OS-media controls for a page whose audio is a SEQUENCE of per-sentence
|
|
4
|
+
* files. The browser only ever sees one session; this module keeps it fed:
|
|
5
|
+
*
|
|
6
|
+
* · metadata — the document title + the current voice (artist);
|
|
7
|
+
* · action handlers — play/pause, previous/next track (sentence),
|
|
8
|
+
* seekbackward/seekforward (the ±15s skip), seekto;
|
|
9
|
+
* · position state — ONE document-wide position, throttled by the component
|
|
10
|
+
* (`navigator.mediaSession.setPositionState` throws on an invalid
|
|
11
|
+
* duration/rate combination, so every write is guarded).
|
|
12
|
+
*
|
|
13
|
+
* Feature-detected end to end: everywhere the API is missing (every non-secure
|
|
14
|
+
* context, older Safari, jsdom, node) these are silent no-ops. Pure enough to
|
|
15
|
+
* drive from a fake session in node tests — the DOM `MediaSession` type is
|
|
16
|
+
* deliberately mirrored structurally, not imported.
|
|
17
|
+
*/
|
|
18
|
+
/** The slice of `navigator.mediaSession` ListenControl uses. */
|
|
19
|
+
export interface MediaSessionLike {
|
|
20
|
+
metadata: {
|
|
21
|
+
title?: string;
|
|
22
|
+
artist?: string;
|
|
23
|
+
album?: string;
|
|
24
|
+
artwork?: {
|
|
25
|
+
src: string;
|
|
26
|
+
sizes?: string;
|
|
27
|
+
type?: string;
|
|
28
|
+
}[];
|
|
29
|
+
} | null;
|
|
30
|
+
setActionHandler(action: string, handler: ((details: {
|
|
31
|
+
seekOffset?: number;
|
|
32
|
+
seekTime?: number;
|
|
33
|
+
fastSeek?: boolean;
|
|
34
|
+
}) => void) | null): void;
|
|
35
|
+
setPositionState(state: {
|
|
36
|
+
duration: number;
|
|
37
|
+
playbackRate?: number;
|
|
38
|
+
position?: number;
|
|
39
|
+
}): void;
|
|
40
|
+
}
|
|
41
|
+
/** Navigator-like lookup — injectable so node tests can pass a fake. */
|
|
42
|
+
export type NavigatorLike = {
|
|
43
|
+
mediaSession?: MediaSessionLike;
|
|
44
|
+
} | null | undefined;
|
|
45
|
+
/** The session, or null where the Media Session API doesn't exist. */
|
|
46
|
+
export declare function getMediaSession(nav?: NavigatorLike): MediaSessionLike | null;
|
|
47
|
+
export interface ListenMediaMeta {
|
|
48
|
+
title: string;
|
|
49
|
+
artist?: string;
|
|
50
|
+
album?: string;
|
|
51
|
+
/** Optional cover art URL(s) — host-supplied. */
|
|
52
|
+
artwork?: string[];
|
|
53
|
+
}
|
|
54
|
+
/** Best-effort metadata write. No-op without a session; never throws. */
|
|
55
|
+
export declare function setListenMetadata(session: MediaSessionLike | null, meta: ListenMediaMeta): void;
|
|
56
|
+
/**
|
|
57
|
+
* Wire the transport actions. Each handler is registered on its own try/catch:
|
|
58
|
+
* browsers reject actions they don't implement individually, and one refusal
|
|
59
|
+
* must not cost the rest. A null handler value clears that action.
|
|
60
|
+
*/
|
|
61
|
+
export declare function setListenActions(session: MediaSessionLike | null, handlers: {
|
|
62
|
+
play?: (() => void) | null;
|
|
63
|
+
pause?: (() => void) | null;
|
|
64
|
+
previoustrack?: (() => void) | null;
|
|
65
|
+
nexttrack?: (() => void) | null;
|
|
66
|
+
seekbackward?: ((details: {
|
|
67
|
+
seekOffset?: number;
|
|
68
|
+
}) => void) | null;
|
|
69
|
+
seekforward?: ((details: {
|
|
70
|
+
seekOffset?: number;
|
|
71
|
+
}) => void) | null;
|
|
72
|
+
seekto?: ((details: {
|
|
73
|
+
seekTime?: number;
|
|
74
|
+
fastSeek?: boolean;
|
|
75
|
+
}) => void) | null;
|
|
76
|
+
stop?: (() => void) | null;
|
|
77
|
+
}): void;
|
|
78
|
+
/** Hand the lock-screen back: null metadata, null handlers. */
|
|
79
|
+
export declare function clearListenSession(session: MediaSessionLike | null): void;
|
|
80
|
+
/**
|
|
81
|
+
* One document-wide position write. Guards the two ways this throws: no
|
|
82
|
+
* session, and an invalid state (negative/NaN duration, position outside it,
|
|
83
|
+
* a non-positive rate) — `setPositionState` rejects those with an exception,
|
|
84
|
+
* which must never surface from a timeupdate.
|
|
85
|
+
*/
|
|
86
|
+
export declare function updateListenPosition(session: MediaSessionLike | null, state: {
|
|
87
|
+
duration: number;
|
|
88
|
+
position: number;
|
|
89
|
+
playbackRate: number;
|
|
90
|
+
}): void;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ListenControl Media Session glue (jatui-eev2n) — lock-screen / headset /
|
|
3
|
+
* OS-media controls for a page whose audio is a SEQUENCE of per-sentence
|
|
4
|
+
* files. The browser only ever sees one session; this module keeps it fed:
|
|
5
|
+
*
|
|
6
|
+
* · metadata — the document title + the current voice (artist);
|
|
7
|
+
* · action handlers — play/pause, previous/next track (sentence),
|
|
8
|
+
* seekbackward/seekforward (the ±15s skip), seekto;
|
|
9
|
+
* · position state — ONE document-wide position, throttled by the component
|
|
10
|
+
* (`navigator.mediaSession.setPositionState` throws on an invalid
|
|
11
|
+
* duration/rate combination, so every write is guarded).
|
|
12
|
+
*
|
|
13
|
+
* Feature-detected end to end: everywhere the API is missing (every non-secure
|
|
14
|
+
* context, older Safari, jsdom, node) these are silent no-ops. Pure enough to
|
|
15
|
+
* drive from a fake session in node tests — the DOM `MediaSession` type is
|
|
16
|
+
* deliberately mirrored structurally, not imported.
|
|
17
|
+
*/
|
|
18
|
+
/** The session, or null where the Media Session API doesn't exist. */
|
|
19
|
+
export function getMediaSession(nav = typeof navigator === 'undefined' ? null : navigator) {
|
|
20
|
+
const session = nav?.mediaSession;
|
|
21
|
+
if (!session || typeof session.setActionHandler !== 'function')
|
|
22
|
+
return null;
|
|
23
|
+
return session;
|
|
24
|
+
}
|
|
25
|
+
/** Best-effort metadata write. No-op without a session; never throws. */
|
|
26
|
+
export function setListenMetadata(session, meta) {
|
|
27
|
+
if (!session)
|
|
28
|
+
return;
|
|
29
|
+
try {
|
|
30
|
+
const plain = {
|
|
31
|
+
title: meta.title,
|
|
32
|
+
artist: meta.artist ?? '',
|
|
33
|
+
album: meta.album ?? '',
|
|
34
|
+
...(meta.artwork && meta.artwork.length
|
|
35
|
+
? { artwork: meta.artwork.map((src) => ({ src, sizes: '512x512' })) }
|
|
36
|
+
: {})
|
|
37
|
+
};
|
|
38
|
+
// `MediaMetadata` is browser-global; node/fake sessions get the plain shape.
|
|
39
|
+
const Ctor = typeof MediaMetadata === 'undefined' ? null : MediaMetadata;
|
|
40
|
+
session.metadata = Ctor ? new Ctor(plain) : plain;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
/* hostile metadata — keep playing without lock-screen info */
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Wire the transport actions. Each handler is registered on its own try/catch:
|
|
48
|
+
* browsers reject actions they don't implement individually, and one refusal
|
|
49
|
+
* must not cost the rest. A null handler value clears that action.
|
|
50
|
+
*/
|
|
51
|
+
export function setListenActions(session, handlers) {
|
|
52
|
+
if (!session)
|
|
53
|
+
return;
|
|
54
|
+
for (const [action, handler] of Object.entries(handlers)) {
|
|
55
|
+
if (handler === undefined)
|
|
56
|
+
continue;
|
|
57
|
+
try {
|
|
58
|
+
session.setActionHandler(action, handler);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
/* this browser doesn't implement that action */
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Hand the lock-screen back: null metadata, null handlers. */
|
|
66
|
+
export function clearListenSession(session) {
|
|
67
|
+
if (!session)
|
|
68
|
+
return;
|
|
69
|
+
try {
|
|
70
|
+
session.metadata = null;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
/* ignore */
|
|
74
|
+
}
|
|
75
|
+
setListenActions(session, {
|
|
76
|
+
play: null,
|
|
77
|
+
pause: null,
|
|
78
|
+
previoustrack: null,
|
|
79
|
+
nexttrack: null,
|
|
80
|
+
seekbackward: null,
|
|
81
|
+
seekforward: null,
|
|
82
|
+
seekto: null,
|
|
83
|
+
stop: null
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* One document-wide position write. Guards the two ways this throws: no
|
|
88
|
+
* session, and an invalid state (negative/NaN duration, position outside it,
|
|
89
|
+
* a non-positive rate) — `setPositionState` rejects those with an exception,
|
|
90
|
+
* which must never surface from a timeupdate.
|
|
91
|
+
*/
|
|
92
|
+
export function updateListenPosition(session, state) {
|
|
93
|
+
if (!session)
|
|
94
|
+
return;
|
|
95
|
+
const { duration, position, playbackRate } = state;
|
|
96
|
+
if (!Number.isFinite(duration) || duration <= 0)
|
|
97
|
+
return;
|
|
98
|
+
if (!Number.isFinite(position) || position < 0 || position > duration)
|
|
99
|
+
return;
|
|
100
|
+
if (!Number.isFinite(playbackRate) || playbackRate <= 0)
|
|
101
|
+
return;
|
|
102
|
+
try {
|
|
103
|
+
session.setPositionState({ duration, position, playbackRate });
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
/* raced with a source change — the next timeupdate rewrites it */
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ListenControl preferences — speed and voice persistence (jatui-eev2n).
|
|
3
|
+
*
|
|
4
|
+
* The same contract as `video/settings/store.ts` and `columnStorage`: jatui
|
|
5
|
+
* persists to THIS browser's localStorage by default, a host keeping per-user
|
|
6
|
+
* state passes its own store. Every access is guarded — private mode, a full
|
|
7
|
+
* quota, or SSR degrade to "nothing stored", never to a thrown error.
|
|
8
|
+
*/
|
|
9
|
+
/** The listening speeds people actually pick — click cycles, [ / ] step. */
|
|
10
|
+
export declare const LISTEN_SPEEDS: number[];
|
|
11
|
+
/** The task's range: 0.75–2×. */
|
|
12
|
+
export declare const SPEED_FLOOR = 0.75;
|
|
13
|
+
export declare const SPEED_CEIL = 2;
|
|
14
|
+
/**
|
|
15
|
+
* Clamp to the 0.75–2× range. Garbage (NaN, Infinity, hostile types) degrades
|
|
16
|
+
* to 1× — never a thrown error.
|
|
17
|
+
*/
|
|
18
|
+
export declare function clampSpeed(rate: unknown): number;
|
|
19
|
+
/**
|
|
20
|
+
* One step along the speed staircase in `direction`. A rate OFF the staircase
|
|
21
|
+
* (1.1 from a host's own menu, say) steps to its nearest staircase neighbour
|
|
22
|
+
* in the pressed direction — never snapping back to 1×. Past either end the
|
|
23
|
+
* rate holds.
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Plain multiplier label for listening speeds — `0.75×`, `1×`, `1.25×`,
|
|
27
|
+
* `1.5×`, `1.75×`, `2×`. (VideoPlayer's `formatSpeedLabel` writes sub-1×
|
|
28
|
+
* speeds as a delta — mpv style — which reads wrong on a reading player.)
|
|
29
|
+
*/
|
|
30
|
+
export declare function formatListenSpeed(rate: number): string;
|
|
31
|
+
export declare function stepSpeed(current: number, direction: 1 | -1): number;
|
|
32
|
+
export interface ListenPrefs {
|
|
33
|
+
speed: number;
|
|
34
|
+
voiceId: string | null;
|
|
35
|
+
}
|
|
36
|
+
/** What persistence needs. Null/undefined ⇒ no storage (SSR, private mode). */
|
|
37
|
+
export type ListenPrefStore = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'> | null | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Read prefs. Missing/corrupt storage or hostile JSON → null (use the
|
|
40
|
+
* defaults); a hostile `speed` degrades to 1× rather than poisoning the
|
|
41
|
+
* player, and `voiceId` must be a non-empty string to survive.
|
|
42
|
+
*/
|
|
43
|
+
export declare function loadListenPrefs(store: ListenPrefStore, key: string): ListenPrefs | null;
|
|
44
|
+
/** Write prefs. Best-effort: a throwing/full store is swallowed. */
|
|
45
|
+
export declare function saveListenPrefs(store: ListenPrefStore, key: string, prefs: ListenPrefs): void;
|
|
46
|
+
/** The default store: this browser's localStorage, or null on SSR/private mode. */
|
|
47
|
+
export declare function localListenStore(): ListenPrefStore;
|
|
48
|
+
/** Default key prefix — hosts pass `storageKey` to namespace per-page/feature. */
|
|
49
|
+
export declare const LISTEN_PREFS_KEY = "jatui.listen:prefs";
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ListenControl preferences — speed and voice persistence (jatui-eev2n).
|
|
3
|
+
*
|
|
4
|
+
* The same contract as `video/settings/store.ts` and `columnStorage`: jatui
|
|
5
|
+
* persists to THIS browser's localStorage by default, a host keeping per-user
|
|
6
|
+
* state passes its own store. Every access is guarded — private mode, a full
|
|
7
|
+
* quota, or SSR degrade to "nothing stored", never to a thrown error.
|
|
8
|
+
*/
|
|
9
|
+
/** The listening speeds people actually pick — click cycles, [ / ] step. */
|
|
10
|
+
export const LISTEN_SPEEDS = [0.75, 1, 1.25, 1.5, 1.75, 2];
|
|
11
|
+
/** The task's range: 0.75–2×. */
|
|
12
|
+
export const SPEED_FLOOR = 0.75;
|
|
13
|
+
export const SPEED_CEIL = 2;
|
|
14
|
+
/**
|
|
15
|
+
* Clamp to the 0.75–2× range. Garbage (NaN, Infinity, hostile types) degrades
|
|
16
|
+
* to 1× — never a thrown error.
|
|
17
|
+
*/
|
|
18
|
+
export function clampSpeed(rate) {
|
|
19
|
+
const n = typeof rate === 'number' ? rate : Number(rate);
|
|
20
|
+
if (!Number.isFinite(n) || n <= 0)
|
|
21
|
+
return 1;
|
|
22
|
+
return Math.min(SPEED_CEIL, Math.max(SPEED_FLOOR, n));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* One step along the speed staircase in `direction`. A rate OFF the staircase
|
|
26
|
+
* (1.1 from a host's own menu, say) steps to its nearest staircase neighbour
|
|
27
|
+
* in the pressed direction — never snapping back to 1×. Past either end the
|
|
28
|
+
* rate holds.
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* Plain multiplier label for listening speeds — `0.75×`, `1×`, `1.25×`,
|
|
32
|
+
* `1.5×`, `1.75×`, `2×`. (VideoPlayer's `formatSpeedLabel` writes sub-1×
|
|
33
|
+
* speeds as a delta — mpv style — which reads wrong on a reading player.)
|
|
34
|
+
*/
|
|
35
|
+
export function formatListenSpeed(rate) {
|
|
36
|
+
return `${clampSpeed(rate)}×`;
|
|
37
|
+
}
|
|
38
|
+
export function stepSpeed(current, direction) {
|
|
39
|
+
const cur = clampSpeed(current);
|
|
40
|
+
const i = LISTEN_SPEEDS.indexOf(cur);
|
|
41
|
+
if (i !== -1)
|
|
42
|
+
return LISTEN_SPEEDS[Math.min(LISTEN_SPEEDS.length - 1, Math.max(0, i + direction))];
|
|
43
|
+
if (direction > 0)
|
|
44
|
+
return LISTEN_SPEEDS.find((s) => s > cur) ?? cur;
|
|
45
|
+
for (let j = LISTEN_SPEEDS.length - 1; j >= 0; j--)
|
|
46
|
+
if (LISTEN_SPEEDS[j] < cur)
|
|
47
|
+
return LISTEN_SPEEDS[j];
|
|
48
|
+
return cur;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Read prefs. Missing/corrupt storage or hostile JSON → null (use the
|
|
52
|
+
* defaults); a hostile `speed` degrades to 1× rather than poisoning the
|
|
53
|
+
* player, and `voiceId` must be a non-empty string to survive.
|
|
54
|
+
*/
|
|
55
|
+
export function loadListenPrefs(store, key) {
|
|
56
|
+
if (!store || typeof key !== 'string' || key === '')
|
|
57
|
+
return null;
|
|
58
|
+
try {
|
|
59
|
+
const raw = store.getItem(key);
|
|
60
|
+
if (raw == null || raw === '')
|
|
61
|
+
return null;
|
|
62
|
+
const parsed = JSON.parse(raw);
|
|
63
|
+
if (parsed == null || typeof parsed !== 'object')
|
|
64
|
+
return null;
|
|
65
|
+
const obj = parsed;
|
|
66
|
+
const speed = typeof obj.speed === 'number' && Number.isFinite(obj.speed) ? clampSpeed(obj.speed) : NaN;
|
|
67
|
+
const voiceId = typeof obj.voiceId === 'string' && obj.voiceId !== '' ? obj.voiceId : null;
|
|
68
|
+
if (!Number.isFinite(speed) && voiceId === null)
|
|
69
|
+
return null;
|
|
70
|
+
return { speed: Number.isFinite(speed) ? speed : 1, voiceId };
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Write prefs. Best-effort: a throwing/full store is swallowed. */
|
|
77
|
+
export function saveListenPrefs(store, key, prefs) {
|
|
78
|
+
if (!store || typeof key !== 'string' || key === '')
|
|
79
|
+
return;
|
|
80
|
+
try {
|
|
81
|
+
store.setItem(key, JSON.stringify({ speed: clampSpeed(prefs.speed), voiceId: prefs.voiceId ?? null }));
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
/* private mode, quota — the session keeps its in-memory values */
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** The default store: this browser's localStorage, or null on SSR/private mode. */
|
|
88
|
+
export function localListenStore() {
|
|
89
|
+
try {
|
|
90
|
+
return typeof localStorage === 'undefined' ? null : localStorage;
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Default key prefix — hosts pass `storageKey` to namespace per-page/feature. */
|
|
97
|
+
export const LISTEN_PREFS_KEY = 'jatui.listen:prefs';
|