@scalemule/nextjs 0.1.44 → 0.1.46
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/audio.css +0 -7
- package/dist/audio.d.mts +58 -6
- package/dist/audio.d.ts +58 -6
- package/dist/audio.js +151 -27
- package/dist/audio.mjs +146 -26
- package/package.json +1 -1
package/dist/audio.css
CHANGED
|
@@ -223,10 +223,3 @@
|
|
|
223
223
|
flex-basis: 120px;
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
|
-
::highlight(sm-narration-sentence) {
|
|
227
|
-
background-color: var(--sm-narration-sentence, rgba(36, 99, 70, 0.12));
|
|
228
|
-
}
|
|
229
|
-
::highlight(sm-narration-word) {
|
|
230
|
-
background-color: var(--sm-narration-word, rgba(36, 99, 70, 0.32));
|
|
231
|
-
color: inherit;
|
|
232
|
-
}
|
package/dist/audio.d.mts
CHANGED
|
@@ -60,12 +60,12 @@ declare function AudioPlayer(props: AudioPlayerProps): react_jsx_runtime.JSX.Ele
|
|
|
60
60
|
* (the article's markdown rendered to plain text). The article body a
|
|
61
61
|
* site renders is HTML from that same markdown, so the two word
|
|
62
62
|
* sequences correspond nearly one-to-one; this module aligns them and
|
|
63
|
-
* paints the spoken word and sentence
|
|
64
|
-
*
|
|
63
|
+
* paints the spoken word and sentence as boxes in an overlay layer
|
|
64
|
+
* behind the text — the article's own DOM is never rewritten.
|
|
65
65
|
*
|
|
66
|
-
* Everything degrades to nothing:
|
|
67
|
-
*
|
|
68
|
-
*
|
|
66
|
+
* Everything degrades to nothing: no layout APIs (SSR), missing
|
|
67
|
+
* timings, or a body that no longer matches the narration simply means
|
|
68
|
+
* no highlight.
|
|
69
69
|
*/
|
|
70
70
|
interface NarrationTimings {
|
|
71
71
|
version: number;
|
|
@@ -73,6 +73,58 @@ interface NarrationTimings {
|
|
|
73
73
|
/** [text, start_ms, end_ms, sentence_index] */
|
|
74
74
|
words: [string, number, number, number][];
|
|
75
75
|
}
|
|
76
|
+
declare const WORD_HIGHLIGHT = "sm-narration-word";
|
|
77
|
+
declare const SENTENCE_HIGHLIGHT = "sm-narration-sentence";
|
|
76
78
|
declare function narrationHighlightSupported(): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Owns the live highlight for one narration player. Build once per
|
|
81
|
+
* (timings, article body) pair; drive with `update(currentTimeMs)`.
|
|
82
|
+
*
|
|
83
|
+
* Paints into an overlay layer behind the article text (positioned boxes
|
|
84
|
+
* computed from the words' line rectangles) rather than the CSS Custom
|
|
85
|
+
* Highlight API: WebKit leaves stale ::highlight paint behind while the
|
|
86
|
+
* page scrolls, and per-word highlight ranges render as ragged, gapped
|
|
87
|
+
* boxes. Colors come from --sm-narration-sentence / --sm-narration-word
|
|
88
|
+
* on the article body (or any ancestor).
|
|
89
|
+
*/
|
|
90
|
+
declare class NarrationHighlighter {
|
|
91
|
+
private words;
|
|
92
|
+
private ranges;
|
|
93
|
+
private sentenceWords;
|
|
94
|
+
private currentWord;
|
|
95
|
+
private currentSentence;
|
|
96
|
+
private follow;
|
|
97
|
+
private detachUserScroll;
|
|
98
|
+
private reducedMotion;
|
|
99
|
+
private root;
|
|
100
|
+
private layer;
|
|
101
|
+
private sentenceLayer;
|
|
102
|
+
private wordLayer;
|
|
103
|
+
private restoreRootStyle;
|
|
104
|
+
private resizeObserver;
|
|
105
|
+
private onWindowResize;
|
|
106
|
+
constructor(root: Element, timings: NarrationTimings);
|
|
107
|
+
/** Fraction of narration words found in the article body. A low
|
|
108
|
+
* ratio means the body diverged from the script; callers may prefer
|
|
109
|
+
* to hide the toggle below ~0.5. */
|
|
110
|
+
matchRatio(): number;
|
|
111
|
+
private attachUserScroll;
|
|
112
|
+
/** Re-engage auto-scroll (the reader pressed the toggle or sought). */
|
|
113
|
+
resumeFollowing(): void;
|
|
114
|
+
/** Lazily creates the overlay layer behind the article text. */
|
|
115
|
+
private ensureLayer;
|
|
116
|
+
/** Line boxes for the given word indexes, in layer coordinates. */
|
|
117
|
+
private boxesFor;
|
|
118
|
+
private paint;
|
|
119
|
+
/** Re-lays out the current highlight (after resize / reflow). */
|
|
120
|
+
private repaint;
|
|
121
|
+
update(timeMs: number): void;
|
|
122
|
+
private scrollTo;
|
|
123
|
+
clear(): void;
|
|
124
|
+
destroy(): void;
|
|
125
|
+
}
|
|
126
|
+
/** Parse a fetched timings payload; accepts the raw timings object or
|
|
127
|
+
* common envelopes ({timings}, {data:{timings}}). */
|
|
128
|
+
declare function parseTimingsPayload(body: unknown): NarrationTimings | null;
|
|
77
129
|
|
|
78
|
-
export { AudioPlayer, type AudioPlayerNarration, type AudioPlayerProps, type AudioPlayerSource, type AudioPlayerVariant, type NarrationTimings, narrationHighlightSupported };
|
|
130
|
+
export { AudioPlayer, type AudioPlayerNarration, type AudioPlayerProps, type AudioPlayerSource, type AudioPlayerVariant, NarrationHighlighter, type NarrationTimings, SENTENCE_HIGHLIGHT, WORD_HIGHLIGHT, narrationHighlightSupported, parseTimingsPayload };
|
package/dist/audio.d.ts
CHANGED
|
@@ -60,12 +60,12 @@ declare function AudioPlayer(props: AudioPlayerProps): react_jsx_runtime.JSX.Ele
|
|
|
60
60
|
* (the article's markdown rendered to plain text). The article body a
|
|
61
61
|
* site renders is HTML from that same markdown, so the two word
|
|
62
62
|
* sequences correspond nearly one-to-one; this module aligns them and
|
|
63
|
-
* paints the spoken word and sentence
|
|
64
|
-
*
|
|
63
|
+
* paints the spoken word and sentence as boxes in an overlay layer
|
|
64
|
+
* behind the text — the article's own DOM is never rewritten.
|
|
65
65
|
*
|
|
66
|
-
* Everything degrades to nothing:
|
|
67
|
-
*
|
|
68
|
-
*
|
|
66
|
+
* Everything degrades to nothing: no layout APIs (SSR), missing
|
|
67
|
+
* timings, or a body that no longer matches the narration simply means
|
|
68
|
+
* no highlight.
|
|
69
69
|
*/
|
|
70
70
|
interface NarrationTimings {
|
|
71
71
|
version: number;
|
|
@@ -73,6 +73,58 @@ interface NarrationTimings {
|
|
|
73
73
|
/** [text, start_ms, end_ms, sentence_index] */
|
|
74
74
|
words: [string, number, number, number][];
|
|
75
75
|
}
|
|
76
|
+
declare const WORD_HIGHLIGHT = "sm-narration-word";
|
|
77
|
+
declare const SENTENCE_HIGHLIGHT = "sm-narration-sentence";
|
|
76
78
|
declare function narrationHighlightSupported(): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Owns the live highlight for one narration player. Build once per
|
|
81
|
+
* (timings, article body) pair; drive with `update(currentTimeMs)`.
|
|
82
|
+
*
|
|
83
|
+
* Paints into an overlay layer behind the article text (positioned boxes
|
|
84
|
+
* computed from the words' line rectangles) rather than the CSS Custom
|
|
85
|
+
* Highlight API: WebKit leaves stale ::highlight paint behind while the
|
|
86
|
+
* page scrolls, and per-word highlight ranges render as ragged, gapped
|
|
87
|
+
* boxes. Colors come from --sm-narration-sentence / --sm-narration-word
|
|
88
|
+
* on the article body (or any ancestor).
|
|
89
|
+
*/
|
|
90
|
+
declare class NarrationHighlighter {
|
|
91
|
+
private words;
|
|
92
|
+
private ranges;
|
|
93
|
+
private sentenceWords;
|
|
94
|
+
private currentWord;
|
|
95
|
+
private currentSentence;
|
|
96
|
+
private follow;
|
|
97
|
+
private detachUserScroll;
|
|
98
|
+
private reducedMotion;
|
|
99
|
+
private root;
|
|
100
|
+
private layer;
|
|
101
|
+
private sentenceLayer;
|
|
102
|
+
private wordLayer;
|
|
103
|
+
private restoreRootStyle;
|
|
104
|
+
private resizeObserver;
|
|
105
|
+
private onWindowResize;
|
|
106
|
+
constructor(root: Element, timings: NarrationTimings);
|
|
107
|
+
/** Fraction of narration words found in the article body. A low
|
|
108
|
+
* ratio means the body diverged from the script; callers may prefer
|
|
109
|
+
* to hide the toggle below ~0.5. */
|
|
110
|
+
matchRatio(): number;
|
|
111
|
+
private attachUserScroll;
|
|
112
|
+
/** Re-engage auto-scroll (the reader pressed the toggle or sought). */
|
|
113
|
+
resumeFollowing(): void;
|
|
114
|
+
/** Lazily creates the overlay layer behind the article text. */
|
|
115
|
+
private ensureLayer;
|
|
116
|
+
/** Line boxes for the given word indexes, in layer coordinates. */
|
|
117
|
+
private boxesFor;
|
|
118
|
+
private paint;
|
|
119
|
+
/** Re-lays out the current highlight (after resize / reflow). */
|
|
120
|
+
private repaint;
|
|
121
|
+
update(timeMs: number): void;
|
|
122
|
+
private scrollTo;
|
|
123
|
+
clear(): void;
|
|
124
|
+
destroy(): void;
|
|
125
|
+
}
|
|
126
|
+
/** Parse a fetched timings payload; accepts the raw timings object or
|
|
127
|
+
* common envelopes ({timings}, {data:{timings}}). */
|
|
128
|
+
declare function parseTimingsPayload(body: unknown): NarrationTimings | null;
|
|
77
129
|
|
|
78
|
-
export { AudioPlayer, type AudioPlayerNarration, type AudioPlayerProps, type AudioPlayerSource, type AudioPlayerVariant, type NarrationTimings, narrationHighlightSupported };
|
|
130
|
+
export { AudioPlayer, type AudioPlayerNarration, type AudioPlayerProps, type AudioPlayerSource, type AudioPlayerVariant, NarrationHighlighter, type NarrationTimings, SENTENCE_HIGHLIGHT, WORD_HIGHLIGHT, narrationHighlightSupported, parseTimingsPayload };
|
package/dist/audio.js
CHANGED
|
@@ -23,7 +23,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
23
23
|
var audio_exports = {};
|
|
24
24
|
__export(audio_exports, {
|
|
25
25
|
AudioPlayer: () => AudioPlayer,
|
|
26
|
-
|
|
26
|
+
NarrationHighlighter: () => NarrationHighlighter,
|
|
27
|
+
SENTENCE_HIGHLIGHT: () => SENTENCE_HIGHLIGHT,
|
|
28
|
+
WORD_HIGHLIGHT: () => WORD_HIGHLIGHT,
|
|
29
|
+
narrationHighlightSupported: () => narrationHighlightSupported,
|
|
30
|
+
parseTimingsPayload: () => parseTimingsPayload
|
|
27
31
|
});
|
|
28
32
|
module.exports = __toCommonJS(audio_exports);
|
|
29
33
|
|
|
@@ -95,15 +99,46 @@ function wordIndexAt(words, timeMs) {
|
|
|
95
99
|
}
|
|
96
100
|
return ans;
|
|
97
101
|
}
|
|
102
|
+
function mergeLineBoxes(rects) {
|
|
103
|
+
const lines = [];
|
|
104
|
+
const sorted = rects.filter((r) => r.width > 0 && r.height > 0).sort((a, b) => a.top - b.top || a.left - b.left);
|
|
105
|
+
for (const r of sorted) {
|
|
106
|
+
const center = r.top + r.height / 2;
|
|
107
|
+
const line = lines.find((l) => {
|
|
108
|
+
const lc = l.top + l.height / 2;
|
|
109
|
+
return Math.abs(lc - center) < Math.min(l.height, r.height) / 2;
|
|
110
|
+
});
|
|
111
|
+
if (!line) {
|
|
112
|
+
lines.push({ ...r });
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const right = Math.max(line.left + line.width, r.left + r.width);
|
|
116
|
+
const bottom = Math.max(line.top + line.height, r.top + r.height);
|
|
117
|
+
line.left = Math.min(line.left, r.left);
|
|
118
|
+
line.top = Math.min(line.top, r.top);
|
|
119
|
+
line.width = right - line.left;
|
|
120
|
+
line.height = bottom - line.top;
|
|
121
|
+
}
|
|
122
|
+
return lines;
|
|
123
|
+
}
|
|
98
124
|
function narrationHighlightSupported() {
|
|
99
|
-
return typeof window !== "undefined" && typeof
|
|
125
|
+
return typeof window !== "undefined" && typeof document !== "undefined" && typeof Range !== "undefined" && typeof Range.prototype.getClientRects === "function";
|
|
100
126
|
}
|
|
127
|
+
var PAD_X = 2;
|
|
128
|
+
var PAD_Y = 1;
|
|
101
129
|
var NarrationHighlighter = class {
|
|
102
130
|
constructor(root, timings) {
|
|
103
131
|
this.currentWord = -2;
|
|
104
132
|
this.currentSentence = -2;
|
|
105
133
|
this.follow = true;
|
|
106
134
|
this.detachUserScroll = null;
|
|
135
|
+
this.layer = null;
|
|
136
|
+
this.sentenceLayer = null;
|
|
137
|
+
this.wordLayer = null;
|
|
138
|
+
this.restoreRootStyle = null;
|
|
139
|
+
this.resizeObserver = null;
|
|
140
|
+
this.onWindowResize = null;
|
|
141
|
+
this.root = root;
|
|
107
142
|
this.words = timings.words;
|
|
108
143
|
const domWords = collectDomWords(root);
|
|
109
144
|
const matches = alignWords(
|
|
@@ -123,13 +158,12 @@ var NarrationHighlighter = class {
|
|
|
123
158
|
return null;
|
|
124
159
|
}
|
|
125
160
|
});
|
|
126
|
-
this.
|
|
161
|
+
this.sentenceWords = /* @__PURE__ */ new Map();
|
|
127
162
|
this.words.forEach((w, i) => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
else this.sentenceRanges.set(w[3], [range]);
|
|
163
|
+
if (!this.ranges[i]) return;
|
|
164
|
+
const list = this.sentenceWords.get(w[3]);
|
|
165
|
+
if (list) list.push(i);
|
|
166
|
+
else this.sentenceWords.set(w[3], [i]);
|
|
133
167
|
});
|
|
134
168
|
this.reducedMotion = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
135
169
|
this.attachUserScroll(doc);
|
|
@@ -158,31 +192,109 @@ var NarrationHighlighter = class {
|
|
|
158
192
|
this.follow = true;
|
|
159
193
|
this.currentSentence = -2;
|
|
160
194
|
}
|
|
195
|
+
/** Lazily creates the overlay layer behind the article text. */
|
|
196
|
+
ensureLayer() {
|
|
197
|
+
if (this.layer) return true;
|
|
198
|
+
const root = this.root;
|
|
199
|
+
const doc = root.ownerDocument;
|
|
200
|
+
if (!doc || typeof root.style === "undefined") return false;
|
|
201
|
+
const view = doc.defaultView;
|
|
202
|
+
const computed = view?.getComputedStyle(root);
|
|
203
|
+
const previous = { position: root.style.position, isolation: root.style.isolation };
|
|
204
|
+
if (!computed || computed.position === "static") root.style.position = "relative";
|
|
205
|
+
root.style.isolation = "isolate";
|
|
206
|
+
this.restoreRootStyle = () => {
|
|
207
|
+
root.style.position = previous.position;
|
|
208
|
+
root.style.isolation = previous.isolation;
|
|
209
|
+
};
|
|
210
|
+
const make = () => {
|
|
211
|
+
const el = doc.createElement("div");
|
|
212
|
+
el.style.position = "absolute";
|
|
213
|
+
el.style.inset = "0";
|
|
214
|
+
el.style.pointerEvents = "none";
|
|
215
|
+
return el;
|
|
216
|
+
};
|
|
217
|
+
this.layer = make();
|
|
218
|
+
this.layer.setAttribute("aria-hidden", "true");
|
|
219
|
+
this.layer.setAttribute("data-sm-narration-layer", "");
|
|
220
|
+
this.layer.style.zIndex = "-1";
|
|
221
|
+
this.sentenceLayer = make();
|
|
222
|
+
this.wordLayer = make();
|
|
223
|
+
this.layer.append(this.sentenceLayer, this.wordLayer);
|
|
224
|
+
root.prepend(this.layer);
|
|
225
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
226
|
+
this.resizeObserver = new ResizeObserver(() => this.repaint());
|
|
227
|
+
this.resizeObserver.observe(root);
|
|
228
|
+
}
|
|
229
|
+
if (view) {
|
|
230
|
+
this.onWindowResize = () => this.repaint();
|
|
231
|
+
view.addEventListener("resize", this.onWindowResize, { passive: true });
|
|
232
|
+
}
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
/** Line boxes for the given word indexes, in layer coordinates. */
|
|
236
|
+
boxesFor(wordIndexes) {
|
|
237
|
+
const root = this.root;
|
|
238
|
+
const origin = root.getBoundingClientRect();
|
|
239
|
+
const rects = [];
|
|
240
|
+
for (const i of wordIndexes) {
|
|
241
|
+
const range = this.ranges[i];
|
|
242
|
+
if (!range || typeof range.getClientRects !== "function") continue;
|
|
243
|
+
for (const r of Array.from(range.getClientRects())) {
|
|
244
|
+
rects.push({
|
|
245
|
+
left: r.left - origin.left - root.clientLeft + root.scrollLeft,
|
|
246
|
+
top: r.top - origin.top - root.clientTop + root.scrollTop,
|
|
247
|
+
width: r.width,
|
|
248
|
+
height: r.height
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return mergeLineBoxes(rects);
|
|
253
|
+
}
|
|
254
|
+
paint(target, boxes, kind) {
|
|
255
|
+
if (!target) return;
|
|
256
|
+
const doc = target.ownerDocument;
|
|
257
|
+
const color = kind === "word" ? "var(--sm-narration-word, rgba(36, 99, 70, 0.32))" : "var(--sm-narration-sentence, rgba(36, 99, 70, 0.12))";
|
|
258
|
+
const nodes = boxes.map((b) => {
|
|
259
|
+
const el = doc.createElement("div");
|
|
260
|
+
el.setAttribute("data-sm-narration", kind);
|
|
261
|
+
el.className = kind === "word" ? WORD_HIGHLIGHT : SENTENCE_HIGHLIGHT;
|
|
262
|
+
el.style.position = "absolute";
|
|
263
|
+
el.style.left = `${b.left - PAD_X}px`;
|
|
264
|
+
el.style.top = `${b.top - PAD_Y}px`;
|
|
265
|
+
el.style.width = `${b.width + PAD_X * 2}px`;
|
|
266
|
+
el.style.height = `${b.height + PAD_Y * 2}px`;
|
|
267
|
+
el.style.borderRadius = "4px";
|
|
268
|
+
el.style.background = color;
|
|
269
|
+
return el;
|
|
270
|
+
});
|
|
271
|
+
target.replaceChildren(...nodes);
|
|
272
|
+
}
|
|
273
|
+
/** Re-lays out the current highlight (after resize / reflow). */
|
|
274
|
+
repaint() {
|
|
275
|
+
if (this.currentWord < 0) return;
|
|
276
|
+
const sentence = this.words[this.currentWord]?.[3];
|
|
277
|
+
this.paint(this.sentenceLayer, this.boxesFor(this.sentenceWords.get(sentence) ?? []), "sentence");
|
|
278
|
+
this.paint(this.wordLayer, this.boxesFor([this.currentWord]), "word");
|
|
279
|
+
}
|
|
161
280
|
update(timeMs) {
|
|
162
281
|
if (!narrationHighlightSupported()) return;
|
|
163
282
|
const idx = wordIndexAt(this.words, timeMs);
|
|
164
283
|
if (idx === this.currentWord) return;
|
|
165
284
|
this.currentWord = idx;
|
|
166
|
-
const registry = CSS.highlights;
|
|
167
285
|
if (idx < 0) {
|
|
168
|
-
|
|
169
|
-
registry.delete(SENTENCE_HIGHLIGHT);
|
|
170
|
-
this.currentSentence = -2;
|
|
286
|
+
this.clear();
|
|
171
287
|
return;
|
|
172
288
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
else registry.delete(WORD_HIGHLIGHT);
|
|
289
|
+
if (!this.ensureLayer()) return;
|
|
290
|
+
this.paint(this.wordLayer, this.ranges[idx] ? this.boxesFor([idx]) : [], "word");
|
|
176
291
|
const sentence = this.words[idx][3];
|
|
177
292
|
if (sentence !== this.currentSentence) {
|
|
178
293
|
this.currentSentence = sentence;
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
} else {
|
|
184
|
-
registry.delete(SENTENCE_HIGHLIGHT);
|
|
185
|
-
}
|
|
294
|
+
const members = this.sentenceWords.get(sentence) ?? [];
|
|
295
|
+
this.paint(this.sentenceLayer, this.boxesFor(members), "sentence");
|
|
296
|
+
const first = members.length ? this.ranges[members[0]] : null;
|
|
297
|
+
if (first) this.scrollTo(first);
|
|
186
298
|
}
|
|
187
299
|
}
|
|
188
300
|
scrollTo(range) {
|
|
@@ -198,15 +310,23 @@ var NarrationHighlighter = class {
|
|
|
198
310
|
}
|
|
199
311
|
}
|
|
200
312
|
clear() {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
registry.delete(WORD_HIGHLIGHT);
|
|
204
|
-
registry.delete(SENTENCE_HIGHLIGHT);
|
|
313
|
+
this.sentenceLayer?.replaceChildren();
|
|
314
|
+
this.wordLayer?.replaceChildren();
|
|
205
315
|
this.currentWord = -2;
|
|
206
316
|
this.currentSentence = -2;
|
|
207
317
|
}
|
|
208
318
|
destroy() {
|
|
209
319
|
this.clear();
|
|
320
|
+
this.resizeObserver?.disconnect();
|
|
321
|
+
this.resizeObserver = null;
|
|
322
|
+
if (this.onWindowResize) {
|
|
323
|
+
this.root.ownerDocument?.defaultView?.removeEventListener("resize", this.onWindowResize);
|
|
324
|
+
this.onWindowResize = null;
|
|
325
|
+
}
|
|
326
|
+
this.layer?.remove();
|
|
327
|
+
this.layer = this.sentenceLayer = this.wordLayer = null;
|
|
328
|
+
this.restoreRootStyle?.();
|
|
329
|
+
this.restoreRootStyle = null;
|
|
210
330
|
this.detachUserScroll?.();
|
|
211
331
|
this.detachUserScroll = null;
|
|
212
332
|
}
|
|
@@ -680,5 +800,9 @@ function PlayerSession({
|
|
|
680
800
|
// Annotate the CommonJS export names for ESM import in node:
|
|
681
801
|
0 && (module.exports = {
|
|
682
802
|
AudioPlayer,
|
|
683
|
-
|
|
803
|
+
NarrationHighlighter,
|
|
804
|
+
SENTENCE_HIGHLIGHT,
|
|
805
|
+
WORD_HIGHLIGHT,
|
|
806
|
+
narrationHighlightSupported,
|
|
807
|
+
parseTimingsPayload
|
|
684
808
|
});
|
package/dist/audio.mjs
CHANGED
|
@@ -75,15 +75,46 @@ function wordIndexAt(words, timeMs) {
|
|
|
75
75
|
}
|
|
76
76
|
return ans;
|
|
77
77
|
}
|
|
78
|
+
function mergeLineBoxes(rects) {
|
|
79
|
+
const lines = [];
|
|
80
|
+
const sorted = rects.filter((r) => r.width > 0 && r.height > 0).sort((a, b) => a.top - b.top || a.left - b.left);
|
|
81
|
+
for (const r of sorted) {
|
|
82
|
+
const center = r.top + r.height / 2;
|
|
83
|
+
const line = lines.find((l) => {
|
|
84
|
+
const lc = l.top + l.height / 2;
|
|
85
|
+
return Math.abs(lc - center) < Math.min(l.height, r.height) / 2;
|
|
86
|
+
});
|
|
87
|
+
if (!line) {
|
|
88
|
+
lines.push({ ...r });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const right = Math.max(line.left + line.width, r.left + r.width);
|
|
92
|
+
const bottom = Math.max(line.top + line.height, r.top + r.height);
|
|
93
|
+
line.left = Math.min(line.left, r.left);
|
|
94
|
+
line.top = Math.min(line.top, r.top);
|
|
95
|
+
line.width = right - line.left;
|
|
96
|
+
line.height = bottom - line.top;
|
|
97
|
+
}
|
|
98
|
+
return lines;
|
|
99
|
+
}
|
|
78
100
|
function narrationHighlightSupported() {
|
|
79
|
-
return typeof window !== "undefined" && typeof
|
|
101
|
+
return typeof window !== "undefined" && typeof document !== "undefined" && typeof Range !== "undefined" && typeof Range.prototype.getClientRects === "function";
|
|
80
102
|
}
|
|
103
|
+
var PAD_X = 2;
|
|
104
|
+
var PAD_Y = 1;
|
|
81
105
|
var NarrationHighlighter = class {
|
|
82
106
|
constructor(root, timings) {
|
|
83
107
|
this.currentWord = -2;
|
|
84
108
|
this.currentSentence = -2;
|
|
85
109
|
this.follow = true;
|
|
86
110
|
this.detachUserScroll = null;
|
|
111
|
+
this.layer = null;
|
|
112
|
+
this.sentenceLayer = null;
|
|
113
|
+
this.wordLayer = null;
|
|
114
|
+
this.restoreRootStyle = null;
|
|
115
|
+
this.resizeObserver = null;
|
|
116
|
+
this.onWindowResize = null;
|
|
117
|
+
this.root = root;
|
|
87
118
|
this.words = timings.words;
|
|
88
119
|
const domWords = collectDomWords(root);
|
|
89
120
|
const matches = alignWords(
|
|
@@ -103,13 +134,12 @@ var NarrationHighlighter = class {
|
|
|
103
134
|
return null;
|
|
104
135
|
}
|
|
105
136
|
});
|
|
106
|
-
this.
|
|
137
|
+
this.sentenceWords = /* @__PURE__ */ new Map();
|
|
107
138
|
this.words.forEach((w, i) => {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
else this.sentenceRanges.set(w[3], [range]);
|
|
139
|
+
if (!this.ranges[i]) return;
|
|
140
|
+
const list = this.sentenceWords.get(w[3]);
|
|
141
|
+
if (list) list.push(i);
|
|
142
|
+
else this.sentenceWords.set(w[3], [i]);
|
|
113
143
|
});
|
|
114
144
|
this.reducedMotion = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
115
145
|
this.attachUserScroll(doc);
|
|
@@ -138,31 +168,109 @@ var NarrationHighlighter = class {
|
|
|
138
168
|
this.follow = true;
|
|
139
169
|
this.currentSentence = -2;
|
|
140
170
|
}
|
|
171
|
+
/** Lazily creates the overlay layer behind the article text. */
|
|
172
|
+
ensureLayer() {
|
|
173
|
+
if (this.layer) return true;
|
|
174
|
+
const root = this.root;
|
|
175
|
+
const doc = root.ownerDocument;
|
|
176
|
+
if (!doc || typeof root.style === "undefined") return false;
|
|
177
|
+
const view = doc.defaultView;
|
|
178
|
+
const computed = view?.getComputedStyle(root);
|
|
179
|
+
const previous = { position: root.style.position, isolation: root.style.isolation };
|
|
180
|
+
if (!computed || computed.position === "static") root.style.position = "relative";
|
|
181
|
+
root.style.isolation = "isolate";
|
|
182
|
+
this.restoreRootStyle = () => {
|
|
183
|
+
root.style.position = previous.position;
|
|
184
|
+
root.style.isolation = previous.isolation;
|
|
185
|
+
};
|
|
186
|
+
const make = () => {
|
|
187
|
+
const el = doc.createElement("div");
|
|
188
|
+
el.style.position = "absolute";
|
|
189
|
+
el.style.inset = "0";
|
|
190
|
+
el.style.pointerEvents = "none";
|
|
191
|
+
return el;
|
|
192
|
+
};
|
|
193
|
+
this.layer = make();
|
|
194
|
+
this.layer.setAttribute("aria-hidden", "true");
|
|
195
|
+
this.layer.setAttribute("data-sm-narration-layer", "");
|
|
196
|
+
this.layer.style.zIndex = "-1";
|
|
197
|
+
this.sentenceLayer = make();
|
|
198
|
+
this.wordLayer = make();
|
|
199
|
+
this.layer.append(this.sentenceLayer, this.wordLayer);
|
|
200
|
+
root.prepend(this.layer);
|
|
201
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
202
|
+
this.resizeObserver = new ResizeObserver(() => this.repaint());
|
|
203
|
+
this.resizeObserver.observe(root);
|
|
204
|
+
}
|
|
205
|
+
if (view) {
|
|
206
|
+
this.onWindowResize = () => this.repaint();
|
|
207
|
+
view.addEventListener("resize", this.onWindowResize, { passive: true });
|
|
208
|
+
}
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
/** Line boxes for the given word indexes, in layer coordinates. */
|
|
212
|
+
boxesFor(wordIndexes) {
|
|
213
|
+
const root = this.root;
|
|
214
|
+
const origin = root.getBoundingClientRect();
|
|
215
|
+
const rects = [];
|
|
216
|
+
for (const i of wordIndexes) {
|
|
217
|
+
const range = this.ranges[i];
|
|
218
|
+
if (!range || typeof range.getClientRects !== "function") continue;
|
|
219
|
+
for (const r of Array.from(range.getClientRects())) {
|
|
220
|
+
rects.push({
|
|
221
|
+
left: r.left - origin.left - root.clientLeft + root.scrollLeft,
|
|
222
|
+
top: r.top - origin.top - root.clientTop + root.scrollTop,
|
|
223
|
+
width: r.width,
|
|
224
|
+
height: r.height
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return mergeLineBoxes(rects);
|
|
229
|
+
}
|
|
230
|
+
paint(target, boxes, kind) {
|
|
231
|
+
if (!target) return;
|
|
232
|
+
const doc = target.ownerDocument;
|
|
233
|
+
const color = kind === "word" ? "var(--sm-narration-word, rgba(36, 99, 70, 0.32))" : "var(--sm-narration-sentence, rgba(36, 99, 70, 0.12))";
|
|
234
|
+
const nodes = boxes.map((b) => {
|
|
235
|
+
const el = doc.createElement("div");
|
|
236
|
+
el.setAttribute("data-sm-narration", kind);
|
|
237
|
+
el.className = kind === "word" ? WORD_HIGHLIGHT : SENTENCE_HIGHLIGHT;
|
|
238
|
+
el.style.position = "absolute";
|
|
239
|
+
el.style.left = `${b.left - PAD_X}px`;
|
|
240
|
+
el.style.top = `${b.top - PAD_Y}px`;
|
|
241
|
+
el.style.width = `${b.width + PAD_X * 2}px`;
|
|
242
|
+
el.style.height = `${b.height + PAD_Y * 2}px`;
|
|
243
|
+
el.style.borderRadius = "4px";
|
|
244
|
+
el.style.background = color;
|
|
245
|
+
return el;
|
|
246
|
+
});
|
|
247
|
+
target.replaceChildren(...nodes);
|
|
248
|
+
}
|
|
249
|
+
/** Re-lays out the current highlight (after resize / reflow). */
|
|
250
|
+
repaint() {
|
|
251
|
+
if (this.currentWord < 0) return;
|
|
252
|
+
const sentence = this.words[this.currentWord]?.[3];
|
|
253
|
+
this.paint(this.sentenceLayer, this.boxesFor(this.sentenceWords.get(sentence) ?? []), "sentence");
|
|
254
|
+
this.paint(this.wordLayer, this.boxesFor([this.currentWord]), "word");
|
|
255
|
+
}
|
|
141
256
|
update(timeMs) {
|
|
142
257
|
if (!narrationHighlightSupported()) return;
|
|
143
258
|
const idx = wordIndexAt(this.words, timeMs);
|
|
144
259
|
if (idx === this.currentWord) return;
|
|
145
260
|
this.currentWord = idx;
|
|
146
|
-
const registry = CSS.highlights;
|
|
147
261
|
if (idx < 0) {
|
|
148
|
-
|
|
149
|
-
registry.delete(SENTENCE_HIGHLIGHT);
|
|
150
|
-
this.currentSentence = -2;
|
|
262
|
+
this.clear();
|
|
151
263
|
return;
|
|
152
264
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
else registry.delete(WORD_HIGHLIGHT);
|
|
265
|
+
if (!this.ensureLayer()) return;
|
|
266
|
+
this.paint(this.wordLayer, this.ranges[idx] ? this.boxesFor([idx]) : [], "word");
|
|
156
267
|
const sentence = this.words[idx][3];
|
|
157
268
|
if (sentence !== this.currentSentence) {
|
|
158
269
|
this.currentSentence = sentence;
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
} else {
|
|
164
|
-
registry.delete(SENTENCE_HIGHLIGHT);
|
|
165
|
-
}
|
|
270
|
+
const members = this.sentenceWords.get(sentence) ?? [];
|
|
271
|
+
this.paint(this.sentenceLayer, this.boxesFor(members), "sentence");
|
|
272
|
+
const first = members.length ? this.ranges[members[0]] : null;
|
|
273
|
+
if (first) this.scrollTo(first);
|
|
166
274
|
}
|
|
167
275
|
}
|
|
168
276
|
scrollTo(range) {
|
|
@@ -178,15 +286,23 @@ var NarrationHighlighter = class {
|
|
|
178
286
|
}
|
|
179
287
|
}
|
|
180
288
|
clear() {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
registry.delete(WORD_HIGHLIGHT);
|
|
184
|
-
registry.delete(SENTENCE_HIGHLIGHT);
|
|
289
|
+
this.sentenceLayer?.replaceChildren();
|
|
290
|
+
this.wordLayer?.replaceChildren();
|
|
185
291
|
this.currentWord = -2;
|
|
186
292
|
this.currentSentence = -2;
|
|
187
293
|
}
|
|
188
294
|
destroy() {
|
|
189
295
|
this.clear();
|
|
296
|
+
this.resizeObserver?.disconnect();
|
|
297
|
+
this.resizeObserver = null;
|
|
298
|
+
if (this.onWindowResize) {
|
|
299
|
+
this.root.ownerDocument?.defaultView?.removeEventListener("resize", this.onWindowResize);
|
|
300
|
+
this.onWindowResize = null;
|
|
301
|
+
}
|
|
302
|
+
this.layer?.remove();
|
|
303
|
+
this.layer = this.sentenceLayer = this.wordLayer = null;
|
|
304
|
+
this.restoreRootStyle?.();
|
|
305
|
+
this.restoreRootStyle = null;
|
|
190
306
|
this.detachUserScroll?.();
|
|
191
307
|
this.detachUserScroll = null;
|
|
192
308
|
}
|
|
@@ -659,5 +775,9 @@ function PlayerSession({
|
|
|
659
775
|
}
|
|
660
776
|
export {
|
|
661
777
|
AudioPlayer,
|
|
662
|
-
|
|
778
|
+
NarrationHighlighter,
|
|
779
|
+
SENTENCE_HIGHLIGHT,
|
|
780
|
+
WORD_HIGHLIGHT,
|
|
781
|
+
narrationHighlightSupported,
|
|
782
|
+
parseTimingsPayload
|
|
663
783
|
};
|
package/package.json
CHANGED