@scalemule/nextjs 0.1.45 → 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 +27 -6
- package/dist/audio.d.ts +27 -6
- package/dist/audio.js +141 -25
- package/dist/audio.mjs +141 -25
- 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;
|
|
@@ -79,16 +79,30 @@ declare function narrationHighlightSupported(): boolean;
|
|
|
79
79
|
/**
|
|
80
80
|
* Owns the live highlight for one narration player. Build once per
|
|
81
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).
|
|
82
89
|
*/
|
|
83
90
|
declare class NarrationHighlighter {
|
|
84
91
|
private words;
|
|
85
92
|
private ranges;
|
|
86
|
-
private
|
|
93
|
+
private sentenceWords;
|
|
87
94
|
private currentWord;
|
|
88
95
|
private currentSentence;
|
|
89
96
|
private follow;
|
|
90
97
|
private detachUserScroll;
|
|
91
98
|
private reducedMotion;
|
|
99
|
+
private root;
|
|
100
|
+
private layer;
|
|
101
|
+
private sentenceLayer;
|
|
102
|
+
private wordLayer;
|
|
103
|
+
private restoreRootStyle;
|
|
104
|
+
private resizeObserver;
|
|
105
|
+
private onWindowResize;
|
|
92
106
|
constructor(root: Element, timings: NarrationTimings);
|
|
93
107
|
/** Fraction of narration words found in the article body. A low
|
|
94
108
|
* ratio means the body diverged from the script; callers may prefer
|
|
@@ -97,6 +111,13 @@ declare class NarrationHighlighter {
|
|
|
97
111
|
private attachUserScroll;
|
|
98
112
|
/** Re-engage auto-scroll (the reader pressed the toggle or sought). */
|
|
99
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;
|
|
100
121
|
update(timeMs: number): void;
|
|
101
122
|
private scrollTo;
|
|
102
123
|
clear(): void;
|
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;
|
|
@@ -79,16 +79,30 @@ declare function narrationHighlightSupported(): boolean;
|
|
|
79
79
|
/**
|
|
80
80
|
* Owns the live highlight for one narration player. Build once per
|
|
81
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).
|
|
82
89
|
*/
|
|
83
90
|
declare class NarrationHighlighter {
|
|
84
91
|
private words;
|
|
85
92
|
private ranges;
|
|
86
|
-
private
|
|
93
|
+
private sentenceWords;
|
|
87
94
|
private currentWord;
|
|
88
95
|
private currentSentence;
|
|
89
96
|
private follow;
|
|
90
97
|
private detachUserScroll;
|
|
91
98
|
private reducedMotion;
|
|
99
|
+
private root;
|
|
100
|
+
private layer;
|
|
101
|
+
private sentenceLayer;
|
|
102
|
+
private wordLayer;
|
|
103
|
+
private restoreRootStyle;
|
|
104
|
+
private resizeObserver;
|
|
105
|
+
private onWindowResize;
|
|
92
106
|
constructor(root: Element, timings: NarrationTimings);
|
|
93
107
|
/** Fraction of narration words found in the article body. A low
|
|
94
108
|
* ratio means the body diverged from the script; callers may prefer
|
|
@@ -97,6 +111,13 @@ declare class NarrationHighlighter {
|
|
|
97
111
|
private attachUserScroll;
|
|
98
112
|
/** Re-engage auto-scroll (the reader pressed the toggle or sought). */
|
|
99
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;
|
|
100
121
|
update(timeMs: number): void;
|
|
101
122
|
private scrollTo;
|
|
102
123
|
clear(): void;
|
package/dist/audio.js
CHANGED
|
@@ -99,15 +99,46 @@ function wordIndexAt(words, timeMs) {
|
|
|
99
99
|
}
|
|
100
100
|
return ans;
|
|
101
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
|
+
}
|
|
102
124
|
function narrationHighlightSupported() {
|
|
103
|
-
return typeof window !== "undefined" && typeof
|
|
125
|
+
return typeof window !== "undefined" && typeof document !== "undefined" && typeof Range !== "undefined" && typeof Range.prototype.getClientRects === "function";
|
|
104
126
|
}
|
|
127
|
+
var PAD_X = 2;
|
|
128
|
+
var PAD_Y = 1;
|
|
105
129
|
var NarrationHighlighter = class {
|
|
106
130
|
constructor(root, timings) {
|
|
107
131
|
this.currentWord = -2;
|
|
108
132
|
this.currentSentence = -2;
|
|
109
133
|
this.follow = true;
|
|
110
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;
|
|
111
142
|
this.words = timings.words;
|
|
112
143
|
const domWords = collectDomWords(root);
|
|
113
144
|
const matches = alignWords(
|
|
@@ -127,13 +158,12 @@ var NarrationHighlighter = class {
|
|
|
127
158
|
return null;
|
|
128
159
|
}
|
|
129
160
|
});
|
|
130
|
-
this.
|
|
161
|
+
this.sentenceWords = /* @__PURE__ */ new Map();
|
|
131
162
|
this.words.forEach((w, i) => {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
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]);
|
|
137
167
|
});
|
|
138
168
|
this.reducedMotion = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
139
169
|
this.attachUserScroll(doc);
|
|
@@ -162,31 +192,109 @@ var NarrationHighlighter = class {
|
|
|
162
192
|
this.follow = true;
|
|
163
193
|
this.currentSentence = -2;
|
|
164
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
|
+
}
|
|
165
280
|
update(timeMs) {
|
|
166
281
|
if (!narrationHighlightSupported()) return;
|
|
167
282
|
const idx = wordIndexAt(this.words, timeMs);
|
|
168
283
|
if (idx === this.currentWord) return;
|
|
169
284
|
this.currentWord = idx;
|
|
170
|
-
const registry = CSS.highlights;
|
|
171
285
|
if (idx < 0) {
|
|
172
|
-
|
|
173
|
-
registry.delete(SENTENCE_HIGHLIGHT);
|
|
174
|
-
this.currentSentence = -2;
|
|
286
|
+
this.clear();
|
|
175
287
|
return;
|
|
176
288
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
else registry.delete(WORD_HIGHLIGHT);
|
|
289
|
+
if (!this.ensureLayer()) return;
|
|
290
|
+
this.paint(this.wordLayer, this.ranges[idx] ? this.boxesFor([idx]) : [], "word");
|
|
180
291
|
const sentence = this.words[idx][3];
|
|
181
292
|
if (sentence !== this.currentSentence) {
|
|
182
293
|
this.currentSentence = sentence;
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
} else {
|
|
188
|
-
registry.delete(SENTENCE_HIGHLIGHT);
|
|
189
|
-
}
|
|
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);
|
|
190
298
|
}
|
|
191
299
|
}
|
|
192
300
|
scrollTo(range) {
|
|
@@ -202,15 +310,23 @@ var NarrationHighlighter = class {
|
|
|
202
310
|
}
|
|
203
311
|
}
|
|
204
312
|
clear() {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
registry.delete(WORD_HIGHLIGHT);
|
|
208
|
-
registry.delete(SENTENCE_HIGHLIGHT);
|
|
313
|
+
this.sentenceLayer?.replaceChildren();
|
|
314
|
+
this.wordLayer?.replaceChildren();
|
|
209
315
|
this.currentWord = -2;
|
|
210
316
|
this.currentSentence = -2;
|
|
211
317
|
}
|
|
212
318
|
destroy() {
|
|
213
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;
|
|
214
330
|
this.detachUserScroll?.();
|
|
215
331
|
this.detachUserScroll = null;
|
|
216
332
|
}
|
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
|
}
|
package/package.json
CHANGED