@scalemule/nextjs 0.1.64 → 0.1.66
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.d.mts +3 -1
- package/dist/audio.d.ts +3 -1
- package/dist/audio.js +49 -16
- package/dist/audio.mjs +49 -16
- package/package.json +1 -1
package/dist/audio.d.mts
CHANGED
|
@@ -99,6 +99,8 @@ declare class NarrationHighlighter {
|
|
|
99
99
|
private currentWord;
|
|
100
100
|
private currentSentence;
|
|
101
101
|
private follow;
|
|
102
|
+
private userScrolledAt;
|
|
103
|
+
private lastScrollAt;
|
|
102
104
|
private detachUserScroll;
|
|
103
105
|
private reducedMotion;
|
|
104
106
|
private root;
|
|
@@ -114,7 +116,7 @@ declare class NarrationHighlighter {
|
|
|
114
116
|
* to hide the toggle below ~0.5. */
|
|
115
117
|
matchRatio(): number;
|
|
116
118
|
private attachUserScroll;
|
|
117
|
-
/** Re-engage auto-scroll (the reader pressed the toggle or sought). */
|
|
119
|
+
/** Re-engage auto-scroll immediately (the reader pressed the toggle or sought). */
|
|
118
120
|
resumeFollowing(): void;
|
|
119
121
|
/** Lazily creates the overlay layer behind the article text. */
|
|
120
122
|
private ensureLayer;
|
package/dist/audio.d.ts
CHANGED
|
@@ -99,6 +99,8 @@ declare class NarrationHighlighter {
|
|
|
99
99
|
private currentWord;
|
|
100
100
|
private currentSentence;
|
|
101
101
|
private follow;
|
|
102
|
+
private userScrolledAt;
|
|
103
|
+
private lastScrollAt;
|
|
102
104
|
private detachUserScroll;
|
|
103
105
|
private reducedMotion;
|
|
104
106
|
private root;
|
|
@@ -114,7 +116,7 @@ declare class NarrationHighlighter {
|
|
|
114
116
|
* to hide the toggle below ~0.5. */
|
|
115
117
|
matchRatio(): number;
|
|
116
118
|
private attachUserScroll;
|
|
117
|
-
/** Re-engage auto-scroll (the reader pressed the toggle or sought). */
|
|
119
|
+
/** Re-engage auto-scroll immediately (the reader pressed the toggle or sought). */
|
|
118
120
|
resumeFollowing(): void;
|
|
119
121
|
/** Lazily creates the overlay layer behind the article text. */
|
|
120
122
|
private ensureLayer;
|
package/dist/audio.js
CHANGED
|
@@ -74,22 +74,43 @@ function collectDomWords(root) {
|
|
|
74
74
|
}
|
|
75
75
|
function alignWords(timingNorms, domNorms, window2 = 6) {
|
|
76
76
|
const out = new Array(timingNorms.length).fill(-1);
|
|
77
|
+
const resyncWindow = Math.max(window2 * 4, 24);
|
|
77
78
|
let j = 0;
|
|
79
|
+
let inSync = true;
|
|
78
80
|
for (let i = 0; i < timingNorms.length; i++) {
|
|
79
81
|
const target = timingNorms[i];
|
|
80
82
|
if (!target) continue;
|
|
83
|
+
const nextTarget = i + 1 < timingNorms.length ? timingNorms[i + 1] : "";
|
|
81
84
|
let found = -1;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
if (inSync && j < domNorms.length && domNorms[j] === target) {
|
|
86
|
+
found = j;
|
|
87
|
+
} else {
|
|
88
|
+
if (nextTarget) {
|
|
89
|
+
const limit = Math.min(domNorms.length - 1, j + resyncWindow);
|
|
90
|
+
for (let k = j; k < limit; k++) {
|
|
91
|
+
if (domNorms[k] === target && domNorms[k + 1] === nextTarget) {
|
|
92
|
+
found = k;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (found === -1 && (inSync || !nextTarget)) {
|
|
98
|
+
const limit = Math.min(domNorms.length, j + window2);
|
|
99
|
+
for (let k = j; k < limit; k++) {
|
|
100
|
+
if (domNorms[k] === target) {
|
|
101
|
+
found = k;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
86
105
|
}
|
|
87
106
|
}
|
|
88
107
|
if (found === -1) {
|
|
108
|
+
inSync = false;
|
|
89
109
|
continue;
|
|
90
110
|
}
|
|
91
111
|
out[i] = found;
|
|
92
112
|
j = found + 1;
|
|
113
|
+
inSync = true;
|
|
93
114
|
}
|
|
94
115
|
return out;
|
|
95
116
|
}
|
|
@@ -135,11 +156,15 @@ function narrationHighlightSupported() {
|
|
|
135
156
|
}
|
|
136
157
|
var PAD_X = 2;
|
|
137
158
|
var PAD_Y = 1;
|
|
159
|
+
var AUTO_SCROLL_THROTTLE_MS = 180;
|
|
160
|
+
var USER_SCROLL_COOLDOWN_MS = 1200;
|
|
138
161
|
var NarrationHighlighter = class {
|
|
139
162
|
constructor(root, timings) {
|
|
140
163
|
this.currentWord = -2;
|
|
141
164
|
this.currentSentence = -2;
|
|
142
165
|
this.follow = true;
|
|
166
|
+
this.userScrolledAt = 0;
|
|
167
|
+
this.lastScrollAt = 0;
|
|
143
168
|
this.detachUserScroll = null;
|
|
144
169
|
this.layer = null;
|
|
145
170
|
this.sentenceLayer = null;
|
|
@@ -185,20 +210,23 @@ var NarrationHighlighter = class {
|
|
|
185
210
|
return this.ranges.filter(Boolean).length / this.ranges.length;
|
|
186
211
|
}
|
|
187
212
|
attachUserScroll(doc) {
|
|
188
|
-
const
|
|
189
|
-
this.
|
|
213
|
+
const onUserScroll = () => {
|
|
214
|
+
this.userScrolledAt = Date.now();
|
|
190
215
|
};
|
|
191
216
|
const opts = { passive: true };
|
|
192
|
-
doc.addEventListener("wheel",
|
|
193
|
-
doc.addEventListener("touchmove",
|
|
217
|
+
doc.addEventListener("wheel", onUserScroll, opts);
|
|
218
|
+
doc.addEventListener("touchmove", onUserScroll, opts);
|
|
194
219
|
this.detachUserScroll = () => {
|
|
195
|
-
doc.removeEventListener("wheel",
|
|
196
|
-
doc.removeEventListener("touchmove",
|
|
220
|
+
doc.removeEventListener("wheel", onUserScroll, opts);
|
|
221
|
+
doc.removeEventListener("touchmove", onUserScroll, opts);
|
|
197
222
|
};
|
|
198
223
|
}
|
|
199
|
-
/** Re-engage auto-scroll (the reader pressed the toggle or sought). */
|
|
224
|
+
/** Re-engage auto-scroll immediately (the reader pressed the toggle or sought). */
|
|
200
225
|
resumeFollowing() {
|
|
201
226
|
this.follow = true;
|
|
227
|
+
this.userScrolledAt = 0;
|
|
228
|
+
this.lastScrollAt = 0;
|
|
229
|
+
this.currentWord = -2;
|
|
202
230
|
this.currentSentence = -2;
|
|
203
231
|
}
|
|
204
232
|
/** Lazily creates the overlay layer behind the article text. */
|
|
@@ -298,21 +326,26 @@ var NarrationHighlighter = class {
|
|
|
298
326
|
if (!this.ensureLayer()) return;
|
|
299
327
|
this.paint(this.wordLayer, this.ranges[idx] ? this.boxesFor([idx]) : [], "word");
|
|
300
328
|
const sentence = this.words[idx][3];
|
|
329
|
+
const members = this.sentenceWords.get(sentence) ?? [];
|
|
301
330
|
if (sentence !== this.currentSentence) {
|
|
302
331
|
this.currentSentence = sentence;
|
|
303
|
-
const members = this.sentenceWords.get(sentence) ?? [];
|
|
304
332
|
this.paint(this.sentenceLayer, this.boxesFor(members), "sentence");
|
|
305
|
-
const first = members.length ? this.ranges[members[0]] : null;
|
|
306
|
-
if (first) this.scrollTo(first);
|
|
307
333
|
}
|
|
334
|
+
const activeRange = this.ranges[idx] ?? (members.length ? this.ranges[members[0]] : null);
|
|
335
|
+
if (activeRange) this.scrollTo(activeRange);
|
|
308
336
|
}
|
|
309
337
|
scrollTo(range) {
|
|
310
338
|
if (!this.follow) return;
|
|
339
|
+
const now = Date.now();
|
|
340
|
+
if (now - this.userScrolledAt < USER_SCROLL_COOLDOWN_MS) return;
|
|
341
|
+
if (now - this.lastScrollAt < AUTO_SCROLL_THROTTLE_MS) return;
|
|
311
342
|
try {
|
|
312
343
|
const rect = range.getBoundingClientRect();
|
|
313
344
|
const viewport = window.innerHeight || 0;
|
|
314
|
-
if (
|
|
315
|
-
|
|
345
|
+
if (viewport <= 0) return;
|
|
346
|
+
if (rect.top < viewport * 0.2 || rect.bottom > viewport * 0.75) {
|
|
347
|
+
this.lastScrollAt = now;
|
|
348
|
+
const target = Math.max(0, window.scrollY + rect.top - viewport * 0.35);
|
|
316
349
|
window.scrollTo({ top: target, behavior: this.reducedMotion ? "auto" : "smooth" });
|
|
317
350
|
}
|
|
318
351
|
} catch {
|
package/dist/audio.mjs
CHANGED
|
@@ -44,22 +44,43 @@ function collectDomWords(root) {
|
|
|
44
44
|
}
|
|
45
45
|
function alignWords(timingNorms, domNorms, window2 = 6) {
|
|
46
46
|
const out = new Array(timingNorms.length).fill(-1);
|
|
47
|
+
const resyncWindow = Math.max(window2 * 4, 24);
|
|
47
48
|
let j = 0;
|
|
49
|
+
let inSync = true;
|
|
48
50
|
for (let i = 0; i < timingNorms.length; i++) {
|
|
49
51
|
const target = timingNorms[i];
|
|
50
52
|
if (!target) continue;
|
|
53
|
+
const nextTarget = i + 1 < timingNorms.length ? timingNorms[i + 1] : "";
|
|
51
54
|
let found = -1;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
if (inSync && j < domNorms.length && domNorms[j] === target) {
|
|
56
|
+
found = j;
|
|
57
|
+
} else {
|
|
58
|
+
if (nextTarget) {
|
|
59
|
+
const limit = Math.min(domNorms.length - 1, j + resyncWindow);
|
|
60
|
+
for (let k = j; k < limit; k++) {
|
|
61
|
+
if (domNorms[k] === target && domNorms[k + 1] === nextTarget) {
|
|
62
|
+
found = k;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (found === -1 && (inSync || !nextTarget)) {
|
|
68
|
+
const limit = Math.min(domNorms.length, j + window2);
|
|
69
|
+
for (let k = j; k < limit; k++) {
|
|
70
|
+
if (domNorms[k] === target) {
|
|
71
|
+
found = k;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
56
75
|
}
|
|
57
76
|
}
|
|
58
77
|
if (found === -1) {
|
|
78
|
+
inSync = false;
|
|
59
79
|
continue;
|
|
60
80
|
}
|
|
61
81
|
out[i] = found;
|
|
62
82
|
j = found + 1;
|
|
83
|
+
inSync = true;
|
|
63
84
|
}
|
|
64
85
|
return out;
|
|
65
86
|
}
|
|
@@ -105,11 +126,15 @@ function narrationHighlightSupported() {
|
|
|
105
126
|
}
|
|
106
127
|
var PAD_X = 2;
|
|
107
128
|
var PAD_Y = 1;
|
|
129
|
+
var AUTO_SCROLL_THROTTLE_MS = 180;
|
|
130
|
+
var USER_SCROLL_COOLDOWN_MS = 1200;
|
|
108
131
|
var NarrationHighlighter = class {
|
|
109
132
|
constructor(root, timings) {
|
|
110
133
|
this.currentWord = -2;
|
|
111
134
|
this.currentSentence = -2;
|
|
112
135
|
this.follow = true;
|
|
136
|
+
this.userScrolledAt = 0;
|
|
137
|
+
this.lastScrollAt = 0;
|
|
113
138
|
this.detachUserScroll = null;
|
|
114
139
|
this.layer = null;
|
|
115
140
|
this.sentenceLayer = null;
|
|
@@ -155,20 +180,23 @@ var NarrationHighlighter = class {
|
|
|
155
180
|
return this.ranges.filter(Boolean).length / this.ranges.length;
|
|
156
181
|
}
|
|
157
182
|
attachUserScroll(doc) {
|
|
158
|
-
const
|
|
159
|
-
this.
|
|
183
|
+
const onUserScroll = () => {
|
|
184
|
+
this.userScrolledAt = Date.now();
|
|
160
185
|
};
|
|
161
186
|
const opts = { passive: true };
|
|
162
|
-
doc.addEventListener("wheel",
|
|
163
|
-
doc.addEventListener("touchmove",
|
|
187
|
+
doc.addEventListener("wheel", onUserScroll, opts);
|
|
188
|
+
doc.addEventListener("touchmove", onUserScroll, opts);
|
|
164
189
|
this.detachUserScroll = () => {
|
|
165
|
-
doc.removeEventListener("wheel",
|
|
166
|
-
doc.removeEventListener("touchmove",
|
|
190
|
+
doc.removeEventListener("wheel", onUserScroll, opts);
|
|
191
|
+
doc.removeEventListener("touchmove", onUserScroll, opts);
|
|
167
192
|
};
|
|
168
193
|
}
|
|
169
|
-
/** Re-engage auto-scroll (the reader pressed the toggle or sought). */
|
|
194
|
+
/** Re-engage auto-scroll immediately (the reader pressed the toggle or sought). */
|
|
170
195
|
resumeFollowing() {
|
|
171
196
|
this.follow = true;
|
|
197
|
+
this.userScrolledAt = 0;
|
|
198
|
+
this.lastScrollAt = 0;
|
|
199
|
+
this.currentWord = -2;
|
|
172
200
|
this.currentSentence = -2;
|
|
173
201
|
}
|
|
174
202
|
/** Lazily creates the overlay layer behind the article text. */
|
|
@@ -268,21 +296,26 @@ var NarrationHighlighter = class {
|
|
|
268
296
|
if (!this.ensureLayer()) return;
|
|
269
297
|
this.paint(this.wordLayer, this.ranges[idx] ? this.boxesFor([idx]) : [], "word");
|
|
270
298
|
const sentence = this.words[idx][3];
|
|
299
|
+
const members = this.sentenceWords.get(sentence) ?? [];
|
|
271
300
|
if (sentence !== this.currentSentence) {
|
|
272
301
|
this.currentSentence = sentence;
|
|
273
|
-
const members = this.sentenceWords.get(sentence) ?? [];
|
|
274
302
|
this.paint(this.sentenceLayer, this.boxesFor(members), "sentence");
|
|
275
|
-
const first = members.length ? this.ranges[members[0]] : null;
|
|
276
|
-
if (first) this.scrollTo(first);
|
|
277
303
|
}
|
|
304
|
+
const activeRange = this.ranges[idx] ?? (members.length ? this.ranges[members[0]] : null);
|
|
305
|
+
if (activeRange) this.scrollTo(activeRange);
|
|
278
306
|
}
|
|
279
307
|
scrollTo(range) {
|
|
280
308
|
if (!this.follow) return;
|
|
309
|
+
const now = Date.now();
|
|
310
|
+
if (now - this.userScrolledAt < USER_SCROLL_COOLDOWN_MS) return;
|
|
311
|
+
if (now - this.lastScrollAt < AUTO_SCROLL_THROTTLE_MS) return;
|
|
281
312
|
try {
|
|
282
313
|
const rect = range.getBoundingClientRect();
|
|
283
314
|
const viewport = window.innerHeight || 0;
|
|
284
|
-
if (
|
|
285
|
-
|
|
315
|
+
if (viewport <= 0) return;
|
|
316
|
+
if (rect.top < viewport * 0.2 || rect.bottom > viewport * 0.75) {
|
|
317
|
+
this.lastScrollAt = now;
|
|
318
|
+
const target = Math.max(0, window.scrollY + rect.top - viewport * 0.35);
|
|
286
319
|
window.scrollTo({ top: target, behavior: this.reducedMotion ? "auto" : "smooth" });
|
|
287
320
|
}
|
|
288
321
|
} catch {
|
package/package.json
CHANGED