@zodal/media 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +73 -2
- package/dist/chunk-3JIQJ7J6.js +148 -0
- package/dist/chunk-3JIQJ7J6.js.map +1 -0
- package/dist/chunk-7I3V4HKP.js +132 -0
- package/dist/chunk-7I3V4HKP.js.map +1 -0
- package/dist/chunk-AMK7QKN7.js +50 -0
- package/dist/chunk-AMK7QKN7.js.map +1 -0
- package/dist/chunk-NBKMSPYQ.js +46 -0
- package/dist/chunk-NBKMSPYQ.js.map +1 -0
- package/dist/chunk-P3EWPYFQ.js +42 -0
- package/dist/chunk-P3EWPYFQ.js.map +1 -0
- package/dist/chunk-YBCEHG5I.js +68 -0
- package/dist/chunk-YBCEHG5I.js.map +1 -0
- package/dist/commands.cjs +198 -0
- package/dist/commands.cjs.map +1 -0
- package/dist/commands.d.cts +17 -0
- package/dist/commands.d.ts +17 -0
- package/dist/commands.js +171 -0
- package/dist/commands.js.map +1 -0
- package/dist/cues.cjs +78 -0
- package/dist/cues.cjs.map +1 -0
- package/dist/cues.d.cts +39 -0
- package/dist/cues.d.ts +39 -0
- package/dist/cues.js +15 -0
- package/dist/cues.js.map +1 -0
- package/dist/follow.cjs +162 -0
- package/dist/follow.cjs.map +1 -0
- package/dist/follow.d.cts +83 -0
- package/dist/follow.d.ts +83 -0
- package/dist/follow.js +19 -0
- package/dist/follow.js.map +1 -0
- package/dist/index.cjs +510 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/playback.cjs +72 -0
- package/dist/playback.cjs.map +1 -0
- package/dist/playback.d.cts +47 -0
- package/dist/playback.d.ts +47 -0
- package/dist/playback.js +11 -0
- package/dist/playback.js.map +1 -0
- package/dist/sleep.cjs +176 -0
- package/dist/sleep.cjs.map +1 -0
- package/dist/sleep.d.cts +99 -0
- package/dist/sleep.d.ts +99 -0
- package/dist/sleep.js +15 -0
- package/dist/sleep.js.map +1 -0
- package/dist/timed-text.cjs +155 -0
- package/dist/timed-text.cjs.map +1 -0
- package/dist/timed-text.d.cts +44 -0
- package/dist/timed-text.d.ts +44 -0
- package/dist/timed-text.js +9 -0
- package/dist/timed-text.js.map +1 -0
- package/dist/transport.cjs +167 -0
- package/dist/transport.cjs.map +1 -0
- package/dist/transport.d.cts +74 -0
- package/dist/transport.d.ts +74 -0
- package/dist/transport.js +110 -0
- package/dist/transport.js.map +1 -0
- package/dist/vtt.cjs +66 -0
- package/dist/vtt.cjs.map +1 -0
- package/dist/vtt.d.cts +22 -0
- package/dist/vtt.d.ts +22 -0
- package/dist/vtt.js +7 -0
- package/dist/vtt.js.map +1 -0
- package/package.json +155 -4
package/dist/cues.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timed text, looked up by time: which cue and which word are being heard. Pure, no DOM.
|
|
3
|
+
*
|
|
4
|
+
* A cue is a span of text with a start and an end, in seconds, and optionally the times of its
|
|
5
|
+
* words. A spoken overview's sentences (`@zodal/speech`), a song's lyric lines (arioso) and a
|
|
6
|
+
* film's subtitles (srtplay) are all lists of cues. The shape is structural, so this module needs
|
|
7
|
+
* no schema at runtime; the wire model is lacing's (`AlignedTrackV1`, media_hub ADR-0010).
|
|
8
|
+
*/
|
|
9
|
+
/** The fields of a cue this module reads. `words` are `[start, end, text]`, in seconds. */
|
|
10
|
+
interface CueLike {
|
|
11
|
+
start: number;
|
|
12
|
+
end: number;
|
|
13
|
+
text?: string;
|
|
14
|
+
words?: ReadonlyArray<readonly [number, number, string]>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Tolerance so a seek to a cue's own start lands in that cue and not the one before it, after
|
|
18
|
+
* float rounding (`currentTime` is rarely exactly what was set).
|
|
19
|
+
*/
|
|
20
|
+
declare const CUE_EPSILON_S = 0.01;
|
|
21
|
+
interface CueAtOptions {
|
|
22
|
+
/** See `CUE_EPSILON_S`. */
|
|
23
|
+
epsilonS?: number;
|
|
24
|
+
/**
|
|
25
|
+
* `false` (the default): the last cue that has started stays current through the gap after it,
|
|
26
|
+
* as a transcript or lyrics view wants. `true`: -1 between cues, as subtitles want.
|
|
27
|
+
*/
|
|
28
|
+
gaps?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/** The index of the cue being heard at `time`: the last one that has started, or -1. */
|
|
31
|
+
declare function cueAt(cues: readonly CueLike[], time: number, options?: CueAtOptions): number;
|
|
32
|
+
/** The index of the word being heard within `cue`, or -1 when it has no word times. */
|
|
33
|
+
declare function wordAt(cue: CueLike | undefined, time: number): number;
|
|
34
|
+
/** The cues' total length: the last cue's end, which stands in until the media reports its own. */
|
|
35
|
+
declare function cuesDuration(cues: readonly CueLike[]): number;
|
|
36
|
+
/** `m:ss`, or `h:mm:ss` past the hour. */
|
|
37
|
+
declare function formatTime(seconds: number): string;
|
|
38
|
+
|
|
39
|
+
export { CUE_EPSILON_S, type CueAtOptions, type CueLike, cueAt, cuesDuration, formatTime, wordAt };
|
package/dist/cues.js
ADDED
package/dist/cues.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/follow.cjs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/follow.ts
|
|
21
|
+
var follow_exports = {};
|
|
22
|
+
__export(follow_exports, {
|
|
23
|
+
DEFAULT_FOLLOW_OPTIONS: () => DEFAULT_FOLLOW_OPTIONS,
|
|
24
|
+
DEFAULT_OWN_SCROLL_MS: () => DEFAULT_OWN_SCROLL_MS,
|
|
25
|
+
SCROLL_KEYS: () => SCROLL_KEYS,
|
|
26
|
+
attachFollow: () => attachFollow,
|
|
27
|
+
createFollow: () => createFollow,
|
|
28
|
+
scrollLineIntoContainer: () => scrollLineIntoContainer,
|
|
29
|
+
scrollerOf: () => scrollerOf
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(follow_exports);
|
|
32
|
+
var DEFAULT_FOLLOW_OPTIONS = { following: true, resumeAfterMs: null };
|
|
33
|
+
function createFollow(options = {}) {
|
|
34
|
+
const {
|
|
35
|
+
following = DEFAULT_FOLLOW_OPTIONS.following,
|
|
36
|
+
resumeAfterMs = DEFAULT_FOLLOW_OPTIONS.resumeAfterMs,
|
|
37
|
+
setTimer = (fn, ms) => setTimeout(fn, ms),
|
|
38
|
+
clearTimer = (h) => clearTimeout(h)
|
|
39
|
+
} = options;
|
|
40
|
+
let state = { following };
|
|
41
|
+
let fingerIsDown = false;
|
|
42
|
+
let timer = null;
|
|
43
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
44
|
+
const set = (next) => {
|
|
45
|
+
if (state.following === next) return;
|
|
46
|
+
state = { following: next };
|
|
47
|
+
listeners.forEach((l) => l(state));
|
|
48
|
+
};
|
|
49
|
+
const cancel = () => {
|
|
50
|
+
if (timer !== null) clearTimer(timer);
|
|
51
|
+
timer = null;
|
|
52
|
+
};
|
|
53
|
+
const plan = () => {
|
|
54
|
+
cancel();
|
|
55
|
+
if (resumeAfterMs === null || fingerIsDown) return;
|
|
56
|
+
timer = setTimer(() => {
|
|
57
|
+
timer = null;
|
|
58
|
+
set(true);
|
|
59
|
+
}, resumeAfterMs);
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
get: () => state,
|
|
63
|
+
subscribe(listener) {
|
|
64
|
+
listeners.add(listener);
|
|
65
|
+
return () => listeners.delete(listener);
|
|
66
|
+
},
|
|
67
|
+
handMoved() {
|
|
68
|
+
set(false);
|
|
69
|
+
plan();
|
|
70
|
+
},
|
|
71
|
+
fingerDown() {
|
|
72
|
+
fingerIsDown = true;
|
|
73
|
+
cancel();
|
|
74
|
+
},
|
|
75
|
+
fingerUp() {
|
|
76
|
+
fingerIsDown = false;
|
|
77
|
+
if (!state.following) plan();
|
|
78
|
+
},
|
|
79
|
+
resume() {
|
|
80
|
+
cancel();
|
|
81
|
+
set(true);
|
|
82
|
+
},
|
|
83
|
+
dispose: cancel
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function isPageScroller(el) {
|
|
87
|
+
const doc = el.ownerDocument;
|
|
88
|
+
return el === doc.scrollingElement || el === doc.documentElement;
|
|
89
|
+
}
|
|
90
|
+
var SCROLL_KEYS = ["PageUp", "PageDown", "Home", "End"];
|
|
91
|
+
function scrollLineIntoContainer(container, line, { anchor = 0.5, smooth = true } = {}) {
|
|
92
|
+
const win = container.ownerDocument.defaultView;
|
|
93
|
+
const box = container.getBoundingClientRect();
|
|
94
|
+
const at = line.getBoundingClientRect();
|
|
95
|
+
const isPage = isPageScroller(container);
|
|
96
|
+
const style = win?.getComputedStyle?.(line);
|
|
97
|
+
const marginTop = parseFloat(style?.scrollMarginTop ?? "") || 0;
|
|
98
|
+
const marginBottom = parseFloat(style?.scrollMarginBottom ?? "") || 0;
|
|
99
|
+
const viewTop = (isPage ? 0 : box.top + container.clientTop) + marginTop;
|
|
100
|
+
const fullHeight = isPage ? win?.innerHeight ?? box.height : container.clientHeight;
|
|
101
|
+
const viewHeight = Math.max(0, fullHeight - marginTop - marginBottom);
|
|
102
|
+
const top = container.scrollTop + (at.top - viewTop) - (viewHeight * anchor - at.height / 2);
|
|
103
|
+
const calm = win?.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
|
|
104
|
+
const behavior = smooth && !calm ? "smooth" : "auto";
|
|
105
|
+
if (typeof container.scrollTo === "function") container.scrollTo({ top: Math.max(0, top), behavior });
|
|
106
|
+
else container.scrollTop = Math.max(0, top);
|
|
107
|
+
}
|
|
108
|
+
function scrollerOf(el) {
|
|
109
|
+
const doc = el.ownerDocument;
|
|
110
|
+
const page = doc.scrollingElement ?? doc.documentElement;
|
|
111
|
+
for (let node = el.parentElement; node && node !== doc.body && node !== doc.documentElement; node = node.parentElement) {
|
|
112
|
+
const overflow = doc.defaultView?.getComputedStyle(node).overflowY ?? "";
|
|
113
|
+
if ((overflow === "auto" || overflow === "scroll" || overflow === "overlay") && node.scrollHeight > node.clientHeight) return node;
|
|
114
|
+
}
|
|
115
|
+
return page;
|
|
116
|
+
}
|
|
117
|
+
var DEFAULT_OWN_SCROLL_MS = 900;
|
|
118
|
+
function attachFollow(container, follow, options = {}) {
|
|
119
|
+
const { anchor = 0.5, ownScrollMs = DEFAULT_OWN_SCROLL_MS, scrollKeys = SCROLL_KEYS, now = () => Date.now() } = options;
|
|
120
|
+
const doc = container.ownerDocument;
|
|
121
|
+
const target = isPageScroller(container) ? doc.defaultView ?? container : container;
|
|
122
|
+
let ownUntil = 0;
|
|
123
|
+
const offs = [];
|
|
124
|
+
const on = (type, fn) => {
|
|
125
|
+
target.addEventListener(type, fn, { passive: true });
|
|
126
|
+
offs.push(() => target.removeEventListener(type, fn));
|
|
127
|
+
};
|
|
128
|
+
const byHand = () => {
|
|
129
|
+
if (now() > ownUntil) follow.handMoved();
|
|
130
|
+
};
|
|
131
|
+
on("wheel", byHand);
|
|
132
|
+
on("touchmove", byHand);
|
|
133
|
+
on("touchstart", () => follow.fingerDown());
|
|
134
|
+
on("touchend", () => follow.fingerUp());
|
|
135
|
+
on("touchcancel", () => follow.fingerUp());
|
|
136
|
+
on("keydown", (e) => {
|
|
137
|
+
const t = e.target;
|
|
138
|
+
const editable = !!t && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName));
|
|
139
|
+
if (!e.defaultPrevented && !editable && scrollKeys.includes(e.key)) follow.handMoved();
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
bring(line, force = false) {
|
|
143
|
+
if (!line || !force && !follow.get().following) return;
|
|
144
|
+
ownUntil = now() + ownScrollMs;
|
|
145
|
+
scrollLineIntoContainer(container, line, { anchor });
|
|
146
|
+
},
|
|
147
|
+
detach() {
|
|
148
|
+
offs.forEach((off) => off());
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
153
|
+
0 && (module.exports = {
|
|
154
|
+
DEFAULT_FOLLOW_OPTIONS,
|
|
155
|
+
DEFAULT_OWN_SCROLL_MS,
|
|
156
|
+
SCROLL_KEYS,
|
|
157
|
+
attachFollow,
|
|
158
|
+
createFollow,
|
|
159
|
+
scrollLineIntoContainer,
|
|
160
|
+
scrollerOf
|
|
161
|
+
});
|
|
162
|
+
//# sourceMappingURL=follow.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/follow.ts"],"sourcesContent":["/**\n * Following the voice: a list of timed lines that keeps the current one in view until a person\n * scrolls it, and how it comes back.\n *\n * Two parts. `createFollow` is the headless state (following or not, and when to resume by\n * itself), driven by plain calls and an injectable clock, so it is tested without a DOM.\n * `attachFollow` wires it to one scroll container: it scrolls *that container* with `scrollTo`\n * (never `scrollIntoView`, which also scrolls every ancestor: a sheet, a page around a frame), and\n * hears hand scrolling *on that container only*, not on the window.\n *\n * Both behaviours come from the fleet's two copies: `@zodal/speech` (own-scroll grace, a manual\n * \"Back to now\") and arioso's lyrics view (container scroll at 35 %, a finger down holds, resume\n * by itself 4 s after the hand is still). The differences are options, not forks.\n */\n\nexport interface FollowState {\n following: boolean;\n}\n\nexport interface FollowOptions {\n /** Start following. */\n following?: boolean;\n /** Resume by itself this long after the hand is still; `null` never (a \"Back to now\" only). */\n resumeAfterMs?: number | null;\n /** The timer, for tests. */\n setTimer?: (fn: () => void, ms: number) => unknown;\n clearTimer?: (handle: unknown) => void;\n}\n\nexport interface Follow {\n get(): FollowState;\n subscribe(listener: (state: FollowState) => void): () => void;\n /** A person moved the list: stop following (and, with `resumeAfterMs`, plan the return). */\n handMoved(): void;\n /** A finger is on the list: never resume while it is down. */\n fingerDown(): void;\n fingerUp(): void;\n /** \"Back to now\", or a tap on a line: follow again at once. */\n resume(): void;\n /** Stops a planned resume; call when the view goes away. */\n dispose(): void;\n}\n\nexport const DEFAULT_FOLLOW_OPTIONS = { following: true, resumeAfterMs: null } as const;\n\n/** Creates the headless follow state. */\nexport function createFollow(options: FollowOptions = {}): Follow {\n const {\n following = DEFAULT_FOLLOW_OPTIONS.following,\n resumeAfterMs = DEFAULT_FOLLOW_OPTIONS.resumeAfterMs,\n setTimer = (fn, ms) => setTimeout(fn, ms),\n clearTimer = (h) => clearTimeout(h as ReturnType<typeof setTimeout>),\n } = options;\n let state: FollowState = { following };\n let fingerIsDown = false;\n let timer: unknown = null;\n const listeners = new Set<(state: FollowState) => void>();\n\n const set = (next: boolean) => {\n if (state.following === next) return;\n state = { following: next };\n listeners.forEach((l) => l(state));\n };\n const cancel = () => {\n if (timer !== null) clearTimer(timer);\n timer = null;\n };\n const plan = () => {\n cancel();\n if (resumeAfterMs === null || fingerIsDown) return;\n timer = setTimer(() => {\n timer = null;\n set(true);\n }, resumeAfterMs);\n };\n\n return {\n get: () => state,\n subscribe(listener) {\n listeners.add(listener);\n return () => listeners.delete(listener);\n },\n handMoved() {\n set(false);\n plan();\n },\n fingerDown() {\n fingerIsDown = true;\n cancel();\n },\n fingerUp() {\n fingerIsDown = false;\n if (!state.following) plan();\n },\n resume() {\n cancel();\n set(true);\n },\n dispose: cancel,\n };\n}\n\n/** True when `el` is the page's own scroller (its scroll events reach the window, its view is the window). */\nfunction isPageScroller(el: HTMLElement): boolean {\n const doc = el.ownerDocument;\n return el === doc.scrollingElement || el === doc.documentElement;\n}\n\n/**\n * Keys that mean a person is scrolling the list. Space and the arrows are left out: a player binds\n * them to play and seek, and a key pressed in a text field is never counted.\n */\nexport const SCROLL_KEYS = ['PageUp', 'PageDown', 'Home', 'End'] as const;\n\n/**\n * Scrolls `container` so `line` sits at `anchor` (0 top, 0.5 middle) of its visible height.\n * Only the container moves. `smooth` falls back to instant under reduced motion.\n */\nexport function scrollLineIntoContainer(\n container: HTMLElement,\n line: HTMLElement,\n { anchor = 0.5, smooth = true }: { anchor?: number; smooth?: boolean } = {},\n): void {\n const win = container.ownerDocument.defaultView;\n const box = container.getBoundingClientRect();\n const at = line.getBoundingClientRect();\n // The page's own scroller reports its box as the whole document; its view is the window.\n const isPage = isPageScroller(container);\n // The line's scroll margins (a sticky bar's height) shrink the view it is placed in, as they\n // do for scrollIntoView.\n const style = win?.getComputedStyle?.(line);\n const marginTop = parseFloat(style?.scrollMarginTop ?? '') || 0;\n const marginBottom = parseFloat(style?.scrollMarginBottom ?? '') || 0;\n const viewTop = (isPage ? 0 : box.top + container.clientTop) + marginTop;\n const fullHeight = isPage ? win?.innerHeight ?? box.height : container.clientHeight;\n const viewHeight = Math.max(0, fullHeight - marginTop - marginBottom);\n const top = container.scrollTop + (at.top - viewTop) - (viewHeight * anchor - at.height / 2);\n const calm = win?.matchMedia?.('(prefers-reduced-motion: reduce)').matches;\n const behavior: ScrollBehavior = smooth && !calm ? 'smooth' : 'auto';\n if (typeof container.scrollTo === 'function') container.scrollTo({ top: Math.max(0, top), behavior });\n else container.scrollTop = Math.max(0, top);\n}\n\n/** The nearest ancestor of `el` that scrolls vertically, or the page's scroller. */\nexport function scrollerOf(el: HTMLElement): HTMLElement {\n const doc = el.ownerDocument;\n const page = (doc.scrollingElement ?? doc.documentElement) as HTMLElement;\n for (let node = el.parentElement; node && node !== doc.body && node !== doc.documentElement; node = node.parentElement) {\n const overflow = doc.defaultView?.getComputedStyle(node).overflowY ?? '';\n if ((overflow === 'auto' || overflow === 'scroll' || overflow === 'overlay') && node.scrollHeight > node.clientHeight) return node;\n }\n return page;\n}\n\nexport interface AttachFollowOptions {\n /** Where the current line sits in the container: arioso 0.35, a transcript 0.5. */\n anchor?: number;\n /** How long the list's own smooth scroll is not mistaken for a person's. */\n ownScrollMs?: number;\n /** Keys that mean a person is scrolling; leave out keys the host uses (arrows that seek). */\n scrollKeys?: readonly string[];\n now?: () => number;\n}\n\nexport const DEFAULT_OWN_SCROLL_MS = 900;\n\nexport interface AttachedFollow {\n /** Bring `line` into view, if following. `force` brings it even when not (after a resume). */\n bring(line: HTMLElement | null | undefined, force?: boolean): void;\n detach(): void;\n}\n\n/**\n * Wires `follow` to one scroll `container`: hand scrolling there stops following; `bring` moves\n * only the container. For a page that scrolls as a whole, pass `document.scrollingElement`; the\n * hand listeners then go on the window, since a page's scroll events reach it there.\n */\nexport function attachFollow(container: HTMLElement, follow: Follow, options: AttachFollowOptions = {}): AttachedFollow {\n const { anchor = 0.5, ownScrollMs = DEFAULT_OWN_SCROLL_MS, scrollKeys = SCROLL_KEYS, now = () => Date.now() } = options;\n const doc = container.ownerDocument;\n const target: EventTarget = isPageScroller(container) ? doc.defaultView ?? container : container;\n let ownUntil = 0;\n const offs: Array<() => void> = [];\n const on = (type: string, fn: (e: Event) => void) => {\n target.addEventListener(type, fn, { passive: true });\n offs.push(() => target.removeEventListener(type, fn));\n };\n const byHand = () => {\n if (now() > ownUntil) follow.handMoved();\n };\n on('wheel', byHand);\n on('touchmove', byHand);\n on('touchstart', () => follow.fingerDown());\n on('touchend', () => follow.fingerUp());\n on('touchcancel', () => follow.fingerUp());\n on('keydown', (e) => {\n const t = e.target as HTMLElement | null;\n const editable = !!t && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName));\n if (!e.defaultPrevented && !editable && scrollKeys.includes((e as KeyboardEvent).key)) follow.handMoved();\n });\n return {\n bring(line, force = false) {\n if (!line || (!force && !follow.get().following)) return;\n ownUntil = now() + ownScrollMs;\n scrollLineIntoContainer(container, line, { anchor });\n },\n detach() {\n offs.forEach((off) => off());\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA2CO,IAAM,yBAAyB,EAAE,WAAW,MAAM,eAAe,KAAK;AAGtE,SAAS,aAAa,UAAyB,CAAC,GAAW;AAChE,QAAM;AAAA,IACJ,YAAY,uBAAuB;AAAA,IACnC,gBAAgB,uBAAuB;AAAA,IACvC,WAAW,CAAC,IAAI,OAAO,WAAW,IAAI,EAAE;AAAA,IACxC,aAAa,CAAC,MAAM,aAAa,CAAkC;AAAA,EACrE,IAAI;AACJ,MAAI,QAAqB,EAAE,UAAU;AACrC,MAAI,eAAe;AACnB,MAAI,QAAiB;AACrB,QAAM,YAAY,oBAAI,IAAkC;AAExD,QAAM,MAAM,CAAC,SAAkB;AAC7B,QAAI,MAAM,cAAc,KAAM;AAC9B,YAAQ,EAAE,WAAW,KAAK;AAC1B,cAAU,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC;AAAA,EACnC;AACA,QAAM,SAAS,MAAM;AACnB,QAAI,UAAU,KAAM,YAAW,KAAK;AACpC,YAAQ;AAAA,EACV;AACA,QAAM,OAAO,MAAM;AACjB,WAAO;AACP,QAAI,kBAAkB,QAAQ,aAAc;AAC5C,YAAQ,SAAS,MAAM;AACrB,cAAQ;AACR,UAAI,IAAI;AAAA,IACV,GAAG,aAAa;AAAA,EAClB;AAEA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B,UAAM,IAAI,EAAE;AACZ,UAAM,WAAW,CAAC,CAAC,MAAM,EAAE,qBAAqB,4BAA4B,KAAK,EAAE,OAAO;AAC1F,QAAI,CAAC,EAAE,oBAAoB,CAAC,YAAY,WAAW,SAAU,EAAoB,GAAG,EAAG,QAAO,UAAU;AAAA,EAC1G,CAAC;AACD,SAAO;AAAA,IACL,MAAM,MAAM,QAAQ,OAAO;AACzB,UAAI,CAAC,QAAS,CAAC,SAAS,CAAC,OAAO,IAAI,EAAE,UAAY;AAClD,iBAAW,IAAI,IAAI;AACnB,8BAAwB,WAAW,MAAM,EAAE,OAAO,CAAC;AAAA,IACrD;AAAA,IACA,SAAS;AACP,WAAK,QAAQ,CAAC,QAAQ,IAAI,CAAC;AAAA,IAC7B;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Following the voice: a list of timed lines that keeps the current one in view until a person
|
|
3
|
+
* scrolls it, and how it comes back.
|
|
4
|
+
*
|
|
5
|
+
* Two parts. `createFollow` is the headless state (following or not, and when to resume by
|
|
6
|
+
* itself), driven by plain calls and an injectable clock, so it is tested without a DOM.
|
|
7
|
+
* `attachFollow` wires it to one scroll container: it scrolls *that container* with `scrollTo`
|
|
8
|
+
* (never `scrollIntoView`, which also scrolls every ancestor: a sheet, a page around a frame), and
|
|
9
|
+
* hears hand scrolling *on that container only*, not on the window.
|
|
10
|
+
*
|
|
11
|
+
* Both behaviours come from the fleet's two copies: `@zodal/speech` (own-scroll grace, a manual
|
|
12
|
+
* "Back to now") and arioso's lyrics view (container scroll at 35 %, a finger down holds, resume
|
|
13
|
+
* by itself 4 s after the hand is still). The differences are options, not forks.
|
|
14
|
+
*/
|
|
15
|
+
interface FollowState {
|
|
16
|
+
following: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface FollowOptions {
|
|
19
|
+
/** Start following. */
|
|
20
|
+
following?: boolean;
|
|
21
|
+
/** Resume by itself this long after the hand is still; `null` never (a "Back to now" only). */
|
|
22
|
+
resumeAfterMs?: number | null;
|
|
23
|
+
/** The timer, for tests. */
|
|
24
|
+
setTimer?: (fn: () => void, ms: number) => unknown;
|
|
25
|
+
clearTimer?: (handle: unknown) => void;
|
|
26
|
+
}
|
|
27
|
+
interface Follow {
|
|
28
|
+
get(): FollowState;
|
|
29
|
+
subscribe(listener: (state: FollowState) => void): () => void;
|
|
30
|
+
/** A person moved the list: stop following (and, with `resumeAfterMs`, plan the return). */
|
|
31
|
+
handMoved(): void;
|
|
32
|
+
/** A finger is on the list: never resume while it is down. */
|
|
33
|
+
fingerDown(): void;
|
|
34
|
+
fingerUp(): void;
|
|
35
|
+
/** "Back to now", or a tap on a line: follow again at once. */
|
|
36
|
+
resume(): void;
|
|
37
|
+
/** Stops a planned resume; call when the view goes away. */
|
|
38
|
+
dispose(): void;
|
|
39
|
+
}
|
|
40
|
+
declare const DEFAULT_FOLLOW_OPTIONS: {
|
|
41
|
+
readonly following: true;
|
|
42
|
+
readonly resumeAfterMs: null;
|
|
43
|
+
};
|
|
44
|
+
/** Creates the headless follow state. */
|
|
45
|
+
declare function createFollow(options?: FollowOptions): Follow;
|
|
46
|
+
/**
|
|
47
|
+
* Keys that mean a person is scrolling the list. Space and the arrows are left out: a player binds
|
|
48
|
+
* them to play and seek, and a key pressed in a text field is never counted.
|
|
49
|
+
*/
|
|
50
|
+
declare const SCROLL_KEYS: readonly ["PageUp", "PageDown", "Home", "End"];
|
|
51
|
+
/**
|
|
52
|
+
* Scrolls `container` so `line` sits at `anchor` (0 top, 0.5 middle) of its visible height.
|
|
53
|
+
* Only the container moves. `smooth` falls back to instant under reduced motion.
|
|
54
|
+
*/
|
|
55
|
+
declare function scrollLineIntoContainer(container: HTMLElement, line: HTMLElement, { anchor, smooth }?: {
|
|
56
|
+
anchor?: number;
|
|
57
|
+
smooth?: boolean;
|
|
58
|
+
}): void;
|
|
59
|
+
/** The nearest ancestor of `el` that scrolls vertically, or the page's scroller. */
|
|
60
|
+
declare function scrollerOf(el: HTMLElement): HTMLElement;
|
|
61
|
+
interface AttachFollowOptions {
|
|
62
|
+
/** Where the current line sits in the container: arioso 0.35, a transcript 0.5. */
|
|
63
|
+
anchor?: number;
|
|
64
|
+
/** How long the list's own smooth scroll is not mistaken for a person's. */
|
|
65
|
+
ownScrollMs?: number;
|
|
66
|
+
/** Keys that mean a person is scrolling; leave out keys the host uses (arrows that seek). */
|
|
67
|
+
scrollKeys?: readonly string[];
|
|
68
|
+
now?: () => number;
|
|
69
|
+
}
|
|
70
|
+
declare const DEFAULT_OWN_SCROLL_MS = 900;
|
|
71
|
+
interface AttachedFollow {
|
|
72
|
+
/** Bring `line` into view, if following. `force` brings it even when not (after a resume). */
|
|
73
|
+
bring(line: HTMLElement | null | undefined, force?: boolean): void;
|
|
74
|
+
detach(): void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Wires `follow` to one scroll `container`: hand scrolling there stops following; `bring` moves
|
|
78
|
+
* only the container. For a page that scrolls as a whole, pass `document.scrollingElement`; the
|
|
79
|
+
* hand listeners then go on the window, since a page's scroll events reach it there.
|
|
80
|
+
*/
|
|
81
|
+
declare function attachFollow(container: HTMLElement, follow: Follow, options?: AttachFollowOptions): AttachedFollow;
|
|
82
|
+
|
|
83
|
+
export { type AttachFollowOptions, type AttachedFollow, DEFAULT_FOLLOW_OPTIONS, DEFAULT_OWN_SCROLL_MS, type Follow, type FollowOptions, type FollowState, SCROLL_KEYS, attachFollow, createFollow, scrollLineIntoContainer, scrollerOf };
|
package/dist/follow.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Following the voice: a list of timed lines that keeps the current one in view until a person
|
|
3
|
+
* scrolls it, and how it comes back.
|
|
4
|
+
*
|
|
5
|
+
* Two parts. `createFollow` is the headless state (following or not, and when to resume by
|
|
6
|
+
* itself), driven by plain calls and an injectable clock, so it is tested without a DOM.
|
|
7
|
+
* `attachFollow` wires it to one scroll container: it scrolls *that container* with `scrollTo`
|
|
8
|
+
* (never `scrollIntoView`, which also scrolls every ancestor: a sheet, a page around a frame), and
|
|
9
|
+
* hears hand scrolling *on that container only*, not on the window.
|
|
10
|
+
*
|
|
11
|
+
* Both behaviours come from the fleet's two copies: `@zodal/speech` (own-scroll grace, a manual
|
|
12
|
+
* "Back to now") and arioso's lyrics view (container scroll at 35 %, a finger down holds, resume
|
|
13
|
+
* by itself 4 s after the hand is still). The differences are options, not forks.
|
|
14
|
+
*/
|
|
15
|
+
interface FollowState {
|
|
16
|
+
following: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface FollowOptions {
|
|
19
|
+
/** Start following. */
|
|
20
|
+
following?: boolean;
|
|
21
|
+
/** Resume by itself this long after the hand is still; `null` never (a "Back to now" only). */
|
|
22
|
+
resumeAfterMs?: number | null;
|
|
23
|
+
/** The timer, for tests. */
|
|
24
|
+
setTimer?: (fn: () => void, ms: number) => unknown;
|
|
25
|
+
clearTimer?: (handle: unknown) => void;
|
|
26
|
+
}
|
|
27
|
+
interface Follow {
|
|
28
|
+
get(): FollowState;
|
|
29
|
+
subscribe(listener: (state: FollowState) => void): () => void;
|
|
30
|
+
/** A person moved the list: stop following (and, with `resumeAfterMs`, plan the return). */
|
|
31
|
+
handMoved(): void;
|
|
32
|
+
/** A finger is on the list: never resume while it is down. */
|
|
33
|
+
fingerDown(): void;
|
|
34
|
+
fingerUp(): void;
|
|
35
|
+
/** "Back to now", or a tap on a line: follow again at once. */
|
|
36
|
+
resume(): void;
|
|
37
|
+
/** Stops a planned resume; call when the view goes away. */
|
|
38
|
+
dispose(): void;
|
|
39
|
+
}
|
|
40
|
+
declare const DEFAULT_FOLLOW_OPTIONS: {
|
|
41
|
+
readonly following: true;
|
|
42
|
+
readonly resumeAfterMs: null;
|
|
43
|
+
};
|
|
44
|
+
/** Creates the headless follow state. */
|
|
45
|
+
declare function createFollow(options?: FollowOptions): Follow;
|
|
46
|
+
/**
|
|
47
|
+
* Keys that mean a person is scrolling the list. Space and the arrows are left out: a player binds
|
|
48
|
+
* them to play and seek, and a key pressed in a text field is never counted.
|
|
49
|
+
*/
|
|
50
|
+
declare const SCROLL_KEYS: readonly ["PageUp", "PageDown", "Home", "End"];
|
|
51
|
+
/**
|
|
52
|
+
* Scrolls `container` so `line` sits at `anchor` (0 top, 0.5 middle) of its visible height.
|
|
53
|
+
* Only the container moves. `smooth` falls back to instant under reduced motion.
|
|
54
|
+
*/
|
|
55
|
+
declare function scrollLineIntoContainer(container: HTMLElement, line: HTMLElement, { anchor, smooth }?: {
|
|
56
|
+
anchor?: number;
|
|
57
|
+
smooth?: boolean;
|
|
58
|
+
}): void;
|
|
59
|
+
/** The nearest ancestor of `el` that scrolls vertically, or the page's scroller. */
|
|
60
|
+
declare function scrollerOf(el: HTMLElement): HTMLElement;
|
|
61
|
+
interface AttachFollowOptions {
|
|
62
|
+
/** Where the current line sits in the container: arioso 0.35, a transcript 0.5. */
|
|
63
|
+
anchor?: number;
|
|
64
|
+
/** How long the list's own smooth scroll is not mistaken for a person's. */
|
|
65
|
+
ownScrollMs?: number;
|
|
66
|
+
/** Keys that mean a person is scrolling; leave out keys the host uses (arrows that seek). */
|
|
67
|
+
scrollKeys?: readonly string[];
|
|
68
|
+
now?: () => number;
|
|
69
|
+
}
|
|
70
|
+
declare const DEFAULT_OWN_SCROLL_MS = 900;
|
|
71
|
+
interface AttachedFollow {
|
|
72
|
+
/** Bring `line` into view, if following. `force` brings it even when not (after a resume). */
|
|
73
|
+
bring(line: HTMLElement | null | undefined, force?: boolean): void;
|
|
74
|
+
detach(): void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Wires `follow` to one scroll `container`: hand scrolling there stops following; `bring` moves
|
|
78
|
+
* only the container. For a page that scrolls as a whole, pass `document.scrollingElement`; the
|
|
79
|
+
* hand listeners then go on the window, since a page's scroll events reach it there.
|
|
80
|
+
*/
|
|
81
|
+
declare function attachFollow(container: HTMLElement, follow: Follow, options?: AttachFollowOptions): AttachedFollow;
|
|
82
|
+
|
|
83
|
+
export { type AttachFollowOptions, type AttachedFollow, DEFAULT_FOLLOW_OPTIONS, DEFAULT_OWN_SCROLL_MS, type Follow, type FollowOptions, type FollowState, SCROLL_KEYS, attachFollow, createFollow, scrollLineIntoContainer, scrollerOf };
|
package/dist/follow.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_FOLLOW_OPTIONS,
|
|
3
|
+
DEFAULT_OWN_SCROLL_MS,
|
|
4
|
+
SCROLL_KEYS,
|
|
5
|
+
attachFollow,
|
|
6
|
+
createFollow,
|
|
7
|
+
scrollLineIntoContainer,
|
|
8
|
+
scrollerOf
|
|
9
|
+
} from "./chunk-7I3V4HKP.js";
|
|
10
|
+
export {
|
|
11
|
+
DEFAULT_FOLLOW_OPTIONS,
|
|
12
|
+
DEFAULT_OWN_SCROLL_MS,
|
|
13
|
+
SCROLL_KEYS,
|
|
14
|
+
attachFollow,
|
|
15
|
+
createFollow,
|
|
16
|
+
scrollLineIntoContainer,
|
|
17
|
+
scrollerOf
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=follow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|