@vocasync/astro 1.0.1 → 2.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/README.md +735 -93
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js.map +1 -1
- package/dist/api/index.d.ts +1 -1
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +1 -1
- package/dist/api/index.js.map +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/components/AudioPlayer.astro +305 -1141
- package/dist/components/index.d.ts +10 -32
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +1 -3
- package/dist/components/index.js.map +1 -1
- package/dist/components/types.d.ts +117 -0
- package/dist/components/types.d.ts.map +1 -0
- package/dist/components/types.js +2 -0
- package/dist/components/types.js.map +1 -0
- package/dist/config/index.d.ts +3 -2
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +2 -1
- package/dist/config/index.js.map +1 -1
- package/dist/config/languages.d.ts +33 -0
- package/dist/config/languages.d.ts.map +1 -0
- package/dist/config/languages.js +167 -0
- package/dist/config/languages.js.map +1 -0
- package/dist/config/schema.d.ts +225 -172
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +45 -24
- package/dist/config/schema.js.map +1 -1
- package/dist/core/content-loader.js +1 -1
- package/dist/core/content-loader.js.map +1 -1
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +2 -2
- package/dist/core/index.js.map +1 -1
- package/dist/core/speech-builder.js.map +1 -1
- package/dist/index.d.ts +6 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -6
- package/dist/index.js.map +1 -1
- package/dist/integration.d.ts +28 -2
- package/dist/integration.d.ts.map +1 -1
- package/dist/integration.js +40 -3
- package/dist/integration.js.map +1 -1
- package/dist/lib/latex-to-speech/index.d.ts.map +1 -1
- package/dist/lib/latex-to-speech/index.js.map +1 -1
- package/dist/player-core/boot.d.ts +9 -0
- package/dist/player-core/boot.d.ts.map +1 -0
- package/dist/player-core/boot.js +59 -0
- package/dist/player-core/boot.js.map +1 -0
- package/dist/player-core/highlighter.d.ts +31 -0
- package/dist/player-core/highlighter.d.ts.map +1 -0
- package/dist/player-core/highlighter.js +103 -0
- package/dist/player-core/highlighter.js.map +1 -0
- package/dist/player-core/index.d.ts +17 -0
- package/dist/player-core/index.d.ts.map +1 -0
- package/dist/player-core/index.js +24 -0
- package/dist/player-core/index.js.map +1 -0
- package/dist/player-core/media-url.d.ts +18 -0
- package/dist/player-core/media-url.d.ts.map +1 -0
- package/dist/player-core/media-url.js +33 -0
- package/dist/player-core/media-url.js.map +1 -0
- package/dist/player-core/player.d.ts +9 -0
- package/dist/player-core/player.d.ts.map +1 -0
- package/dist/player-core/player.js +622 -0
- package/dist/player-core/player.js.map +1 -0
- package/dist/player-core/registry.d.ts +20 -0
- package/dist/player-core/registry.d.ts.map +1 -0
- package/dist/player-core/registry.js +31 -0
- package/dist/player-core/registry.js.map +1 -0
- package/dist/player-core/seek-delegate.d.ts +24 -0
- package/dist/player-core/seek-delegate.d.ts.map +1 -0
- package/dist/player-core/seek-delegate.js +64 -0
- package/dist/player-core/seek-delegate.js.map +1 -0
- package/dist/player-core/storage.d.ts +38 -0
- package/dist/player-core/storage.d.ts.map +1 -0
- package/dist/player-core/storage.js +107 -0
- package/dist/player-core/storage.js.map +1 -0
- package/dist/player-core/strings.d.ts +53 -0
- package/dist/player-core/strings.d.ts.map +1 -0
- package/dist/player-core/strings.js +45 -0
- package/dist/player-core/strings.js.map +1 -0
- package/dist/player-core/timings.d.ts +56 -0
- package/dist/player-core/timings.d.ts.map +1 -0
- package/dist/player-core/timings.js +90 -0
- package/dist/player-core/timings.js.map +1 -0
- package/dist/rehype/audio-words.d.ts +9 -3
- package/dist/rehype/audio-words.d.ts.map +1 -1
- package/dist/rehype/audio-words.js +17 -9
- package/dist/rehype/audio-words.js.map +1 -1
- package/dist/rehype/index.d.ts +2 -2
- package/dist/rehype/index.d.ts.map +1 -1
- package/dist/rehype/index.js.map +1 -1
- package/dist/rehype/rehype-math-speech.d.ts.map +1 -1
- package/dist/rehype/rehype-math-speech.js +2 -0
- package/dist/rehype/rehype-math-speech.js.map +1 -1
- package/dist/styles/content.css +41 -0
- package/dist/styles/layers.css +13 -0
- package/dist/styles/player.css +517 -0
- package/dist/styles/preset-shadcn.css +31 -0
- package/dist/styles/tokens.css +266 -0
- package/dist/styles/vocasync.css +10 -0
- package/dist/sync/index.d.ts +1 -1
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +1 -1
- package/dist/sync/index.js.map +1 -1
- package/dist/sync/orchestrator.d.ts.map +1 -1
- package/dist/sync/orchestrator.js +101 -47
- package/dist/sync/orchestrator.js.map +1 -1
- package/dist/types/index.d.ts +19 -5
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +39 -17
- package/src/api/index.ts +4 -4
- package/src/components/AudioPlayer.astro +305 -1141
- package/src/components/index.ts +19 -34
- package/src/components/types.ts +140 -0
- package/src/config/index.ts +29 -24
- package/src/config/languages.ts +171 -0
- package/src/config/schema.ts +47 -24
- package/src/core/content-loader.ts +1 -1
- package/src/core/index.ts +7 -7
- package/src/env.d.ts +2 -0
- package/src/index.ts +23 -28
- package/src/integration.ts +66 -4
- package/src/player-core/boot.ts +62 -0
- package/src/player-core/highlighter.ts +159 -0
- package/src/player-core/index.ts +45 -0
- package/src/player-core/media-url.ts +35 -0
- package/src/player-core/player.ts +725 -0
- package/src/player-core/registry.ts +40 -0
- package/src/player-core/seek-delegate.ts +92 -0
- package/src/player-core/storage.ts +112 -0
- package/src/player-core/strings.ts +89 -0
- package/src/player-core/timings.ts +115 -0
- package/src/rehype/audio-words.ts +25 -12
- package/src/rehype/index.ts +2 -3
- package/src/rehype/rehype-math-speech.ts +3 -1
- package/src/styles/content.css +41 -0
- package/src/styles/layers.css +13 -0
- package/src/styles/player.css +517 -0
- package/src/styles/preset-shadcn.css +31 -0
- package/src/styles/tokens.css +266 -0
- package/src/styles/vocasync.css +10 -0
- package/src/sync/index.ts +1 -1
- package/src/sync/orchestrator.ts +120 -56
- package/src/types/index.ts +19 -5
- package/dist/styles/variables.css +0 -164
- package/src/styles/variables.css +0 -164
package/src/integration.ts
CHANGED
|
@@ -1,19 +1,62 @@
|
|
|
1
1
|
import type { AstroIntegration } from "astro";
|
|
2
|
-
import { type VocaSyncConfig,
|
|
2
|
+
import { loadVocaSyncConfig, type VocaSyncConfig, validateConfig } from "./config/index.js";
|
|
3
3
|
import { createEmptyAudioMap, loadAudioMap } from "./core/audio-map.js";
|
|
4
4
|
import type { AudioMap } from "./types/index.js";
|
|
5
5
|
|
|
6
6
|
// Cache for the loaded audio map
|
|
7
7
|
let audioMapCache: AudioMap | null = null;
|
|
8
8
|
|
|
9
|
+
export interface VocaSyncThemeOptions {
|
|
10
|
+
/**
|
|
11
|
+
* A selector your site already uses to signal dark mode, e.g. `".dark"` or
|
|
12
|
+
* `'[data-theme="night"]'`. The player's dark tokens are emitted under it as well.
|
|
13
|
+
*
|
|
14
|
+
* Usually unnecessary: `.dark`, `[data-theme="dark"]` and `[data-mode="dark"]` are
|
|
15
|
+
* recognised out of the box, which covers Tailwind, next-themes, daisyUI and
|
|
16
|
+
* Starlight. Reach for this only when your site signals dark mode some other way.
|
|
17
|
+
*/
|
|
18
|
+
darkSelector?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface VocaSyncOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Inject the player stylesheet on every page. Set false to import the parts
|
|
24
|
+
* yourself -- `@vocasync/astro/styles/vocasync.css`, or just `content.css` when you
|
|
25
|
+
* drive the engine headlessly and ship your own chrome.
|
|
26
|
+
*
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
styles?: boolean;
|
|
30
|
+
theme?: VocaSyncThemeOptions;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Dark-mode tokens, emitted again under a host's own dark selector. */
|
|
34
|
+
const DARK_TOKENS = `
|
|
35
|
+
--vocasync-accent: #60a5fa;
|
|
36
|
+
--vocasync-surface: #1e293b;
|
|
37
|
+
--vocasync-text: #f1f5f9;
|
|
38
|
+
--vocasync-highlight: #34d399;
|
|
39
|
+
--vocasync-accent-hover: #3b82f6;
|
|
40
|
+
--vocasync-accent-content: #1e293b;
|
|
41
|
+
--vocasync-surface-raised: #334155;
|
|
42
|
+
--vocasync-text-muted: #94a3b8;
|
|
43
|
+
--vocasync-text-faint: #64748b;
|
|
44
|
+
--vocasync-border: #475569;
|
|
45
|
+
--vocasync-track: #475569;
|
|
46
|
+
`;
|
|
47
|
+
|
|
9
48
|
/**
|
|
10
49
|
* VocaSync Astro Integration.
|
|
11
50
|
*
|
|
12
51
|
* Provides:
|
|
13
52
|
* - Virtual module `virtual:vocasync/audio-map` for accessing audio data
|
|
14
|
-
* -
|
|
53
|
+
* - The player stylesheet, injected on every page unless `styles: false`
|
|
15
54
|
* - Optional build-time sync command
|
|
16
55
|
*
|
|
56
|
+
* It does NOT register the rehype plugins. Add `rehypeMathSpeech` and
|
|
57
|
+
* `rehypeAudioWords` to `markdown.rehypePlugins` yourself; their order relative to
|
|
58
|
+
* your maths renderer matters, so it is left explicit.
|
|
59
|
+
*
|
|
17
60
|
* @example
|
|
18
61
|
* ```typescript
|
|
19
62
|
* // astro.config.mjs
|
|
@@ -29,11 +72,12 @@ let audioMapCache: AudioMap | null = null;
|
|
|
29
72
|
* };
|
|
30
73
|
* ```
|
|
31
74
|
*/
|
|
32
|
-
export default function vocasyncIntegration(): AstroIntegration {
|
|
75
|
+
export default function vocasyncIntegration(options: VocaSyncOptions = {}): AstroIntegration {
|
|
76
|
+
const { styles = true, theme = {} } = options;
|
|
33
77
|
return {
|
|
34
78
|
name: "@vocasync/astro",
|
|
35
79
|
hooks: {
|
|
36
|
-
"astro:config:setup": async ({ updateConfig, logger }) => {
|
|
80
|
+
"astro:config:setup": async ({ updateConfig, injectScript, logger }) => {
|
|
37
81
|
// Load config from vocasync.config.mjs
|
|
38
82
|
let config: VocaSyncConfig;
|
|
39
83
|
try {
|
|
@@ -46,6 +90,15 @@ export default function vocasyncIntegration(): AstroIntegration {
|
|
|
46
90
|
|
|
47
91
|
logger.info("VocaSync integration loaded");
|
|
48
92
|
|
|
93
|
+
// Injected rather than left to the consumer: forgetting the import used to
|
|
94
|
+
// yield an unstyled player AND invisible word highlighting, with no error.
|
|
95
|
+
if (styles) {
|
|
96
|
+
injectScript("page-ssr", 'import "@vocasync/astro/styles/vocasync.css";');
|
|
97
|
+
if (theme.darkSelector) {
|
|
98
|
+
injectScript("page-ssr", 'import "virtual:vocasync/theme.css";');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
49
102
|
// Pre-load audio map for rehype plugin
|
|
50
103
|
try {
|
|
51
104
|
audioMapCache = await loadAudioMap(config.output.audioMapPath);
|
|
@@ -70,6 +123,11 @@ export default function vocasyncIntegration(): AstroIntegration {
|
|
|
70
123
|
if (id === "virtual:vocasync/config") {
|
|
71
124
|
return "\0virtual:vocasync/config";
|
|
72
125
|
}
|
|
126
|
+
// Keep the .css suffix: Vite decides how to handle a module by its
|
|
127
|
+
// extension, and without it this would be treated as JavaScript.
|
|
128
|
+
if (id === "virtual:vocasync/theme.css") {
|
|
129
|
+
return "\0virtual:vocasync/theme.css";
|
|
130
|
+
}
|
|
73
131
|
return null;
|
|
74
132
|
},
|
|
75
133
|
async load(id: string) {
|
|
@@ -81,6 +139,10 @@ export default function vocasyncIntegration(): AstroIntegration {
|
|
|
81
139
|
// Export the validated config
|
|
82
140
|
return `export default ${JSON.stringify(config)};`;
|
|
83
141
|
}
|
|
142
|
+
if (id === "\0virtual:vocasync/theme.css") {
|
|
143
|
+
if (!theme.darkSelector) return "";
|
|
144
|
+
return `@layer vocasync.tokens {\n ${theme.darkSelector} {${DARK_TOKENS} }\n}\n`;
|
|
145
|
+
}
|
|
84
146
|
return null;
|
|
85
147
|
},
|
|
86
148
|
},
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createPlayer } from "./player.js";
|
|
2
|
+
import * as registry from "./registry.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Finds every player on the page and wires it up.
|
|
6
|
+
*
|
|
7
|
+
* The component previously used `define:vars`, which forces an inline script: Astro
|
|
8
|
+
* re-executes those on a view-transition swap, so the player kept working across
|
|
9
|
+
* navigations by accident. A hoisted module script does NOT re-execute -- the browser
|
|
10
|
+
* has already loaded it -- so the lifecycle has to be explicit:
|
|
11
|
+
*
|
|
12
|
+
* - boot on load, for pages without <ClientRouter />
|
|
13
|
+
* - boot again on `astro:page-load`, which fires after every swap
|
|
14
|
+
* - tear down on `astro:before-swap`, or animation loops, IntersectionObservers and
|
|
15
|
+
* click delegates leak onto every subsequent page
|
|
16
|
+
*
|
|
17
|
+
* `data-vocasync-ready` makes booting idempotent, since both paths can fire for the
|
|
18
|
+
* same document.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const READY_ATTR = "data-vocasync-ready";
|
|
22
|
+
const PLAYER_SELECTOR = ".vocasync-player";
|
|
23
|
+
|
|
24
|
+
/** Wire up every player in `doc` that is not already running. */
|
|
25
|
+
export function bootAll(doc: Document = document): void {
|
|
26
|
+
for (const el of Array.from(doc.querySelectorAll(PLAYER_SELECTOR))) {
|
|
27
|
+
const root = el as HTMLElement;
|
|
28
|
+
if (root.hasAttribute(READY_ATTR)) continue;
|
|
29
|
+
try {
|
|
30
|
+
createPlayer(root);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
// One malformed player must not stop the others on the page.
|
|
33
|
+
console.error("[vocasync] Failed to initialise player", error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Destroy every live player. Runs before a view-transition swap. */
|
|
39
|
+
export function teardownAll(): void {
|
|
40
|
+
registry.destroyAll();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let installed = false;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Install the lifecycle once per document. Safe to call repeatedly.
|
|
47
|
+
*/
|
|
48
|
+
export function install(doc: Document = document): void {
|
|
49
|
+
if (installed) return;
|
|
50
|
+
installed = true;
|
|
51
|
+
|
|
52
|
+
if (doc.readyState === "loading") {
|
|
53
|
+
doc.addEventListener("DOMContentLoaded", () => bootAll(doc), { once: true });
|
|
54
|
+
} else {
|
|
55
|
+
bootAll(doc);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Astro's view transitions. These listeners survive swaps because the module is
|
|
59
|
+
// evaluated once; that is precisely why they have to exist.
|
|
60
|
+
doc.addEventListener("astro:page-load", () => bootAll(doc));
|
|
61
|
+
doc.addEventListener("astro:before-swap", teardownAll);
|
|
62
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computeSpanTimings,
|
|
3
|
+
EPS,
|
|
4
|
+
idxForTime,
|
|
5
|
+
type SpanTiming,
|
|
6
|
+
trailRange,
|
|
7
|
+
type WordTiming,
|
|
8
|
+
} from "./timings.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Binds alignment timings to the `.vocasync-word[data-i][data-n]` spans the rehype
|
|
12
|
+
* plugin emitted, and keeps the active word and its trail in sync with a time.
|
|
13
|
+
*
|
|
14
|
+
* Owns no audio element and no animation loop -- the caller drives it by calling
|
|
15
|
+
* `update(time)`. Class names are options rather than literals so the same engine can
|
|
16
|
+
* serve markup that does not use the default prefix.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const PARAGRAPH_SELECTOR = "p, li, blockquote, h1, h2, h3, h4, h5, h6, td, th";
|
|
20
|
+
|
|
21
|
+
export interface HighlighterOptions {
|
|
22
|
+
/** Element containing the word spans. */
|
|
23
|
+
articleRoot: Element;
|
|
24
|
+
/** Alignment timings, indexed by `data-i`. */
|
|
25
|
+
words: readonly WordTiming[];
|
|
26
|
+
/** How many spans behind the active one stay marked. */
|
|
27
|
+
trailLength?: number;
|
|
28
|
+
wordSelector?: string;
|
|
29
|
+
activeClass?: string;
|
|
30
|
+
trailClass?: string;
|
|
31
|
+
/** Scroll the active word's paragraph into view when it changes. */
|
|
32
|
+
autoScroll?: boolean;
|
|
33
|
+
/** Injected so the engine stays testable and window-free. */
|
|
34
|
+
prefersReducedMotion?: () => boolean;
|
|
35
|
+
/** Called when the active word changes, including to none during silence. */
|
|
36
|
+
onWordChange?: (el: HTMLElement | null) => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface Highlighter {
|
|
40
|
+
/** Number of resolved spans; 0 means there is nothing to highlight. */
|
|
41
|
+
readonly size: number;
|
|
42
|
+
update(time: number): void;
|
|
43
|
+
/** Remove every mark this highlighter applied. */
|
|
44
|
+
clear(): void;
|
|
45
|
+
/** Start time of the alignment token at `dataI`, or null. */
|
|
46
|
+
startOfToken(dataI: number): number | null;
|
|
47
|
+
setAutoScroll(enabled: boolean): void;
|
|
48
|
+
destroy(): void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createHighlighter(options: HighlighterOptions): Highlighter {
|
|
52
|
+
const {
|
|
53
|
+
articleRoot,
|
|
54
|
+
words,
|
|
55
|
+
trailLength = 4,
|
|
56
|
+
wordSelector = ".vocasync-word[data-i]",
|
|
57
|
+
activeClass = "is-active",
|
|
58
|
+
trailClass = "is-trail",
|
|
59
|
+
prefersReducedMotion = () => false,
|
|
60
|
+
} = options;
|
|
61
|
+
|
|
62
|
+
let autoScroll = options.autoScroll ?? false;
|
|
63
|
+
|
|
64
|
+
const spans = Array.from(articleRoot.querySelectorAll(wordSelector)) as HTMLElement[];
|
|
65
|
+
const spanTimings: SpanTiming<HTMLElement>[] = computeSpanTimings(
|
|
66
|
+
spans.map((el) => ({
|
|
67
|
+
dataI: Number.parseInt(el.dataset.i ?? "", 10),
|
|
68
|
+
dataN: Number.parseInt(el.dataset.n ?? "", 10),
|
|
69
|
+
ref: el,
|
|
70
|
+
})),
|
|
71
|
+
words
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
let activeEl: HTMLElement | null = null;
|
|
75
|
+
let trailStart = 0;
|
|
76
|
+
let trailEnd = 0;
|
|
77
|
+
let lastScrolledParagraph: Element | null = null;
|
|
78
|
+
|
|
79
|
+
function setActive(el: HTMLElement | null) {
|
|
80
|
+
if (activeEl === el) return;
|
|
81
|
+
if (activeEl) activeEl.classList.remove(activeClass);
|
|
82
|
+
if (el) el.classList.add(activeClass);
|
|
83
|
+
activeEl = el;
|
|
84
|
+
options.onWordChange?.(el);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function clearTrail() {
|
|
88
|
+
for (let i = trailStart; i < trailEnd; i++) {
|
|
89
|
+
spanTimings[i]?.ref.classList.remove(trailClass);
|
|
90
|
+
}
|
|
91
|
+
trailStart = 0;
|
|
92
|
+
trailEnd = 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function scrollActiveIntoView(el: HTMLElement) {
|
|
96
|
+
const paragraph = el.closest(PARAGRAPH_SELECTOR);
|
|
97
|
+
if (!paragraph || paragraph === lastScrolledParagraph) return;
|
|
98
|
+
paragraph.scrollIntoView({
|
|
99
|
+
behavior: prefersReducedMotion() ? "auto" : "smooth",
|
|
100
|
+
block: "center",
|
|
101
|
+
});
|
|
102
|
+
lastScrolledParagraph = paragraph;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
get size() {
|
|
107
|
+
return spanTimings.length;
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
update(time) {
|
|
111
|
+
if (!spanTimings.length) return;
|
|
112
|
+
if (!Number.isFinite(time)) return;
|
|
113
|
+
|
|
114
|
+
// One index drives both the active word and the trail, exactly as the
|
|
115
|
+
// original did: computing them separately lets them disagree by one at a
|
|
116
|
+
// span boundary, which shows up as a word that is both active and trailing.
|
|
117
|
+
const lo = idxForTime(spanTimings, time + EPS);
|
|
118
|
+
const candidate = spanTimings[lo];
|
|
119
|
+
const active =
|
|
120
|
+
candidate && time >= candidate.start - EPS && time < candidate.end + EPS
|
|
121
|
+
? candidate.ref
|
|
122
|
+
: null;
|
|
123
|
+
|
|
124
|
+
setActive(active);
|
|
125
|
+
if (autoScroll && activeEl) scrollActiveIntoView(activeEl);
|
|
126
|
+
|
|
127
|
+
// Differential trail: only touch spans that entered or left the range.
|
|
128
|
+
const next = trailRange(lo, trailLength);
|
|
129
|
+
for (let i = trailStart; i < trailEnd; i++) {
|
|
130
|
+
if (i < next.start || i >= next.end) spanTimings[i]?.ref.classList.remove(trailClass);
|
|
131
|
+
}
|
|
132
|
+
for (let i = next.start; i < next.end; i++) {
|
|
133
|
+
if (i < trailStart || i >= trailEnd) spanTimings[i]?.ref.classList.add(trailClass);
|
|
134
|
+
}
|
|
135
|
+
trailStart = next.start;
|
|
136
|
+
trailEnd = next.end;
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
clear() {
|
|
140
|
+
setActive(null);
|
|
141
|
+
clearTrail();
|
|
142
|
+
lastScrolledParagraph = null;
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
startOfToken(dataI) {
|
|
146
|
+
const timing = words[dataI];
|
|
147
|
+
return timing && typeof timing.start === "number" ? timing.start : null;
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
setAutoScroll(enabled) {
|
|
151
|
+
autoScroll = enabled;
|
|
152
|
+
if (!enabled) lastScrolledParagraph = null;
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
destroy() {
|
|
156
|
+
this.clear();
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The player engine: playback, word highlighting and the view-transition lifecycle,
|
|
3
|
+
* with no markup opinions and no Astro dependency.
|
|
4
|
+
*
|
|
5
|
+
* Public and covered by semver from 2.0.0. `install()` is what the shipped component
|
|
6
|
+
* calls; everything below it is for building something else.
|
|
7
|
+
*
|
|
8
|
+
* Storage and the click delegate are deliberately not exported — they are wiring
|
|
9
|
+
* rather than contract, and freezing them would make the engine harder to improve.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Discovery and lifecycle. `install()` boots every player on the page and keeps them
|
|
13
|
+
// working across Astro view transitions.
|
|
14
|
+
export { bootAll, install, teardownAll } from "./boot.js";
|
|
15
|
+
// Word highlighting against rehype-generated spans.
|
|
16
|
+
export {
|
|
17
|
+
createHighlighter,
|
|
18
|
+
type Highlighter,
|
|
19
|
+
type HighlighterOptions,
|
|
20
|
+
} from "./highlighter.js";
|
|
21
|
+
// The stream URL, with its publishable key applied.
|
|
22
|
+
export { resolveAudioSrc } from "./media-url.js";
|
|
23
|
+
// One player, driven from your own markup.
|
|
24
|
+
export { createPlayer, formatTime, type PlayerInstance } from "./player.js";
|
|
25
|
+
|
|
26
|
+
// Labels, for a custom UI that should read the same as the shipped one.
|
|
27
|
+
export {
|
|
28
|
+
defaultStrings,
|
|
29
|
+
describeTime,
|
|
30
|
+
type PlayerStrings,
|
|
31
|
+
resolveStrings,
|
|
32
|
+
type SerializableStrings,
|
|
33
|
+
} from "./strings.js";
|
|
34
|
+
|
|
35
|
+
// The pure timing maths, for building something quite different.
|
|
36
|
+
export {
|
|
37
|
+
computeSpanTimings,
|
|
38
|
+
EPS,
|
|
39
|
+
idxForTime,
|
|
40
|
+
type SpanItem,
|
|
41
|
+
type SpanTiming,
|
|
42
|
+
spanAtTime,
|
|
43
|
+
trailRange,
|
|
44
|
+
type WordTiming,
|
|
45
|
+
} from "./timings.js";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve the audio stream URL, appending the publishable key when one is present.
|
|
3
|
+
*
|
|
4
|
+
* This is deliberately NOT shared with `api/client.ts`'s `withPublishableKey`, which
|
|
5
|
+
* looks almost identical. That one runs under Node during `vocasync sync`, always
|
|
6
|
+
* against absolute API URLs, and has no `document` to resolve a base from. This one
|
|
7
|
+
* runs in the browser, where an audio map may legitimately carry a site-relative URL.
|
|
8
|
+
* Two runtimes, two correct behaviours -- unifying them would break one of them.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns the URL to assign to `audio.src`, or null when there is nothing playable.
|
|
13
|
+
*
|
|
14
|
+
* Never throws. The previous implementation called `new URL(url)` with no base, which
|
|
15
|
+
* throws on a relative URL; the throw was swallowed by the caller's outer try/catch
|
|
16
|
+
* and surfaced as "Audio not available for this article" with no error anywhere.
|
|
17
|
+
*/
|
|
18
|
+
export function resolveAudioSrc(
|
|
19
|
+
audioUrl: unknown,
|
|
20
|
+
publishableKey?: unknown,
|
|
21
|
+
base?: string
|
|
22
|
+
): string | null {
|
|
23
|
+
if (typeof audioUrl !== "string" || audioUrl.trim() === "") return null;
|
|
24
|
+
if (typeof publishableKey !== "string" || publishableKey === "") return audioUrl;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const url = new URL(audioUrl, base);
|
|
28
|
+
url.searchParams.set("pk", publishableKey);
|
|
29
|
+
return url.toString();
|
|
30
|
+
} catch {
|
|
31
|
+
// An unparseable URL is still worth attempting: the browser may resolve it even
|
|
32
|
+
// where URL() will not, and a 404 is a better failure than no audio at all.
|
|
33
|
+
return audioUrl;
|
|
34
|
+
}
|
|
35
|
+
}
|