vue-book-reader 1.2.2 → 1.2.4

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.
@@ -1,290 +0,0 @@
1
- const NS = {
2
- XML: "http://www.w3.org/XML/1998/namespace",
3
- SSML: "http://www.w3.org/2001/10/synthesis"
4
- };
5
- const blockTags = /* @__PURE__ */ new Set([
6
- "article",
7
- "aside",
8
- "audio",
9
- "blockquote",
10
- "caption",
11
- "details",
12
- "dialog",
13
- "div",
14
- "dl",
15
- "dt",
16
- "dd",
17
- "figure",
18
- "footer",
19
- "form",
20
- "figcaption",
21
- "h1",
22
- "h2",
23
- "h3",
24
- "h4",
25
- "h5",
26
- "h6",
27
- "header",
28
- "hgroup",
29
- "hr",
30
- "li",
31
- "main",
32
- "math",
33
- "nav",
34
- "ol",
35
- "p",
36
- "pre",
37
- "section",
38
- "tr"
39
- ]);
40
- const getLang = (el) => {
41
- var _a;
42
- const x = el.lang || ((_a = el == null ? void 0 : el.getAttributeNS) == null ? void 0 : _a.call(el, NS.XML, "lang"));
43
- return x ? x : el.parentElement ? getLang(el.parentElement) : null;
44
- };
45
- const getAlphabet = (el) => {
46
- var _a;
47
- const x = (_a = el == null ? void 0 : el.getAttributeNS) == null ? void 0 : _a.call(el, NS.XML, "lang");
48
- return x ? x : el.parentElement ? getAlphabet(el.parentElement) : null;
49
- };
50
- const getSegmenter = (lang = "en", granularity = "word") => {
51
- const segmenter = new Intl.Segmenter(lang, { granularity });
52
- const granularityIsWord = granularity === "word";
53
- return function* (strs, makeRange) {
54
- const str = strs.join("");
55
- let name = 0;
56
- let strIndex = -1;
57
- let sum = 0;
58
- for (const { index, segment, isWordLike } of segmenter.segment(str)) {
59
- if (granularityIsWord && !isWordLike) continue;
60
- while (sum <= index) sum += strs[++strIndex].length;
61
- const startIndex = strIndex;
62
- const startOffset = index - (sum - strs[strIndex].length);
63
- const end = index + segment.length - 1;
64
- if (end < str.length) while (sum <= end) sum += strs[++strIndex].length;
65
- const endIndex = strIndex;
66
- const endOffset = end - (sum - strs[strIndex].length) + 1;
67
- yield [
68
- (name++).toString(),
69
- makeRange(startIndex, startOffset, endIndex, endOffset)
70
- ];
71
- }
72
- };
73
- };
74
- const fragmentToSSML = (fragment, inherited) => {
75
- const ssml = document.implementation.createDocument(NS.SSML, "speak");
76
- const { lang } = inherited;
77
- if (lang) ssml.documentElement.setAttributeNS(NS.XML, "lang", lang);
78
- const convert = (node, parent, inheritedAlphabet) => {
79
- if (!node) return;
80
- if (node.nodeType === 3) return ssml.createTextNode(node.textContent);
81
- if (node.nodeType === 4) return ssml.createCDATASection(node.textContent);
82
- if (node.nodeType !== 1) return;
83
- let el;
84
- const nodeName = node.nodeName.toLowerCase();
85
- if (nodeName === "foliate-mark") {
86
- el = ssml.createElementNS(NS.SSML, "mark");
87
- el.setAttribute("name", node.dataset.name);
88
- } else if (nodeName === "br")
89
- el = ssml.createElementNS(NS.SSML, "break");
90
- else if (nodeName === "em" || nodeName === "strong")
91
- el = ssml.createElementNS(NS.SSML, "emphasis");
92
- const lang2 = node.lang || node.getAttributeNS(NS.XML, "lang");
93
- if (lang2) {
94
- if (!el) el = ssml.createElementNS(NS.SSML, "lang");
95
- el.setAttributeNS(NS.XML, "lang", lang2);
96
- }
97
- const alphabet = node.getAttributeNS(NS.SSML, "alphabet") || inheritedAlphabet;
98
- if (!el) {
99
- const ph = node.getAttributeNS(NS.SSML, "ph");
100
- if (ph) {
101
- el = ssml.createElementNS(NS.SSML, "phoneme");
102
- if (alphabet) el.setAttribute("alphabet", alphabet);
103
- el.setAttribute("ph", ph);
104
- }
105
- }
106
- if (!el) el = parent;
107
- let child = node.firstChild;
108
- while (child) {
109
- const childEl = convert(child, el, alphabet);
110
- if (childEl && el !== childEl) el.append(childEl);
111
- child = child.nextSibling;
112
- }
113
- return el;
114
- };
115
- convert(fragment.firstChild, ssml.documentElement, inherited.alphabet);
116
- return ssml;
117
- };
118
- const getFragmentWithMarks = (range, textWalker, granularity) => {
119
- const lang = getLang(range.commonAncestorContainer);
120
- const alphabet = getAlphabet(range.commonAncestorContainer);
121
- const segmenter = getSegmenter(lang, granularity);
122
- const fragment = range.cloneContents();
123
- const entries = [...textWalker(range, segmenter)];
124
- const fragmentEntries = [...textWalker(fragment, segmenter)];
125
- for (const [name, range2] of fragmentEntries) {
126
- const mark = document.createElement("foliate-mark");
127
- mark.dataset.name = name;
128
- range2.insertNode(mark);
129
- }
130
- const ssml = fragmentToSSML(fragment, { lang, alphabet });
131
- return { entries, ssml };
132
- };
133
- const rangeIsEmpty = (range) => !range.toString().trim();
134
- function* getBlocks(doc) {
135
- let last;
136
- const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_ELEMENT);
137
- for (let node = walker.nextNode(); node; node = walker.nextNode()) {
138
- const name = node.tagName.toLowerCase();
139
- if (blockTags.has(name)) {
140
- if (last) {
141
- last.setEndBefore(node);
142
- if (!rangeIsEmpty(last)) yield last;
143
- }
144
- last = doc.createRange();
145
- last.setStart(node, 0);
146
- }
147
- }
148
- if (!last) {
149
- last = doc.createRange();
150
- last.setStart(doc.body.firstChild ?? doc.body, 0);
151
- }
152
- last.setEndAfter(doc.body.lastChild ?? doc.body);
153
- if (!rangeIsEmpty(last)) yield last;
154
- }
155
- class ListIterator {
156
- #arr = [];
157
- #iter;
158
- #index = -1;
159
- #f;
160
- constructor(iter, f = (x) => x) {
161
- this.#iter = iter;
162
- this.#f = f;
163
- }
164
- current() {
165
- if (this.#arr[this.#index]) return this.#f(this.#arr[this.#index]);
166
- }
167
- first() {
168
- const newIndex = 0;
169
- if (this.#arr[newIndex]) {
170
- this.#index = newIndex;
171
- return this.#f(this.#arr[newIndex]);
172
- }
173
- }
174
- prev() {
175
- const newIndex = this.#index - 1;
176
- if (this.#arr[newIndex]) {
177
- this.#index = newIndex;
178
- return this.#f(this.#arr[newIndex]);
179
- }
180
- }
181
- next() {
182
- const newIndex = this.#index + 1;
183
- if (this.#arr[newIndex]) {
184
- this.#index = newIndex;
185
- return this.#f(this.#arr[newIndex]);
186
- }
187
- while (true) {
188
- const { done, value } = this.#iter.next();
189
- if (done) break;
190
- this.#arr.push(value);
191
- if (this.#arr[newIndex]) {
192
- this.#index = newIndex;
193
- return this.#f(this.#arr[newIndex]);
194
- }
195
- }
196
- }
197
- find(f) {
198
- const index = this.#arr.findIndex((x) => f(x));
199
- if (index > -1) {
200
- this.#index = index;
201
- return this.#f(this.#arr[index]);
202
- }
203
- while (true) {
204
- const { done, value } = this.#iter.next();
205
- if (done) break;
206
- this.#arr.push(value);
207
- if (f(value)) {
208
- this.#index = this.#arr.length - 1;
209
- return this.#f(value);
210
- }
211
- }
212
- }
213
- }
214
- class TTS {
215
- #list;
216
- #ranges;
217
- #lastMark;
218
- #serializer = new XMLSerializer();
219
- constructor(doc, textWalker, highlight, granularity) {
220
- this.doc = doc;
221
- this.highlight = highlight;
222
- this.#list = new ListIterator(getBlocks(doc), (range) => {
223
- const { entries, ssml } = getFragmentWithMarks(range, textWalker, granularity);
224
- this.#ranges = new Map(entries);
225
- return [ssml, range];
226
- });
227
- }
228
- #getMarkElement(doc, mark) {
229
- if (!mark) return null;
230
- return doc.querySelector(`mark[name="${CSS.escape(mark)}"`);
231
- }
232
- #speak(doc, getNode) {
233
- var _a, _b;
234
- if (!doc) return;
235
- if (!getNode) return this.#serializer.serializeToString(doc);
236
- const ssml = document.implementation.createDocument(NS.SSML, "speak");
237
- ssml.documentElement.replaceWith(ssml.importNode(doc.documentElement, true));
238
- let node = (_a = getNode(ssml)) == null ? void 0 : _a.previousSibling;
239
- while (node) {
240
- const next = node.previousSibling ?? ((_b = node.parentNode) == null ? void 0 : _b.previousSibling);
241
- node.parentNode.removeChild(node);
242
- node = next;
243
- }
244
- return this.#serializer.serializeToString(ssml);
245
- }
246
- start() {
247
- this.#lastMark = null;
248
- const [doc] = this.#list.first() ?? [];
249
- if (!doc) return this.next();
250
- return this.#speak(doc, (ssml) => this.#getMarkElement(ssml, this.#lastMark));
251
- }
252
- resume() {
253
- const [doc] = this.#list.current() ?? [];
254
- if (!doc) return this.next();
255
- return this.#speak(doc, (ssml) => this.#getMarkElement(ssml, this.#lastMark));
256
- }
257
- prev(paused) {
258
- this.#lastMark = null;
259
- const [doc, range] = this.#list.prev() ?? [];
260
- if (paused && range) this.highlight(range.cloneRange());
261
- return this.#speak(doc);
262
- }
263
- next(paused) {
264
- this.#lastMark = null;
265
- const [doc, range] = this.#list.next() ?? [];
266
- if (paused && range) this.highlight(range.cloneRange());
267
- return this.#speak(doc);
268
- }
269
- from(range) {
270
- this.#lastMark = null;
271
- const [doc] = this.#list.find((range_) => range.compareBoundaryPoints(Range.END_TO_START, range_) <= 0);
272
- let mark;
273
- for (const [name, range_] of this.#ranges.entries())
274
- if (range.compareBoundaryPoints(Range.START_TO_START, range_) <= 0) {
275
- mark = name;
276
- break;
277
- }
278
- return this.#speak(doc, (ssml) => this.#getMarkElement(ssml, mark));
279
- }
280
- setMark(mark) {
281
- const range = this.#ranges.get(mark);
282
- if (range) {
283
- this.#lastMark = mark;
284
- this.highlight(range.cloneRange());
285
- }
286
- }
287
- }
288
- export {
289
- TTS
290
- };
@@ -1,290 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const NS = {
4
- XML: "http://www.w3.org/XML/1998/namespace",
5
- SSML: "http://www.w3.org/2001/10/synthesis"
6
- };
7
- const blockTags = /* @__PURE__ */ new Set([
8
- "article",
9
- "aside",
10
- "audio",
11
- "blockquote",
12
- "caption",
13
- "details",
14
- "dialog",
15
- "div",
16
- "dl",
17
- "dt",
18
- "dd",
19
- "figure",
20
- "footer",
21
- "form",
22
- "figcaption",
23
- "h1",
24
- "h2",
25
- "h3",
26
- "h4",
27
- "h5",
28
- "h6",
29
- "header",
30
- "hgroup",
31
- "hr",
32
- "li",
33
- "main",
34
- "math",
35
- "nav",
36
- "ol",
37
- "p",
38
- "pre",
39
- "section",
40
- "tr"
41
- ]);
42
- const getLang = (el) => {
43
- var _a;
44
- const x = el.lang || ((_a = el == null ? void 0 : el.getAttributeNS) == null ? void 0 : _a.call(el, NS.XML, "lang"));
45
- return x ? x : el.parentElement ? getLang(el.parentElement) : null;
46
- };
47
- const getAlphabet = (el) => {
48
- var _a;
49
- const x = (_a = el == null ? void 0 : el.getAttributeNS) == null ? void 0 : _a.call(el, NS.XML, "lang");
50
- return x ? x : el.parentElement ? getAlphabet(el.parentElement) : null;
51
- };
52
- const getSegmenter = (lang = "en", granularity = "word") => {
53
- const segmenter = new Intl.Segmenter(lang, { granularity });
54
- const granularityIsWord = granularity === "word";
55
- return function* (strs, makeRange) {
56
- const str = strs.join("");
57
- let name = 0;
58
- let strIndex = -1;
59
- let sum = 0;
60
- for (const { index, segment, isWordLike } of segmenter.segment(str)) {
61
- if (granularityIsWord && !isWordLike) continue;
62
- while (sum <= index) sum += strs[++strIndex].length;
63
- const startIndex = strIndex;
64
- const startOffset = index - (sum - strs[strIndex].length);
65
- const end = index + segment.length - 1;
66
- if (end < str.length) while (sum <= end) sum += strs[++strIndex].length;
67
- const endIndex = strIndex;
68
- const endOffset = end - (sum - strs[strIndex].length) + 1;
69
- yield [
70
- (name++).toString(),
71
- makeRange(startIndex, startOffset, endIndex, endOffset)
72
- ];
73
- }
74
- };
75
- };
76
- const fragmentToSSML = (fragment, inherited) => {
77
- const ssml = document.implementation.createDocument(NS.SSML, "speak");
78
- const { lang } = inherited;
79
- if (lang) ssml.documentElement.setAttributeNS(NS.XML, "lang", lang);
80
- const convert = (node, parent, inheritedAlphabet) => {
81
- if (!node) return;
82
- if (node.nodeType === 3) return ssml.createTextNode(node.textContent);
83
- if (node.nodeType === 4) return ssml.createCDATASection(node.textContent);
84
- if (node.nodeType !== 1) return;
85
- let el;
86
- const nodeName = node.nodeName.toLowerCase();
87
- if (nodeName === "foliate-mark") {
88
- el = ssml.createElementNS(NS.SSML, "mark");
89
- el.setAttribute("name", node.dataset.name);
90
- } else if (nodeName === "br")
91
- el = ssml.createElementNS(NS.SSML, "break");
92
- else if (nodeName === "em" || nodeName === "strong")
93
- el = ssml.createElementNS(NS.SSML, "emphasis");
94
- const lang2 = node.lang || node.getAttributeNS(NS.XML, "lang");
95
- if (lang2) {
96
- if (!el) el = ssml.createElementNS(NS.SSML, "lang");
97
- el.setAttributeNS(NS.XML, "lang", lang2);
98
- }
99
- const alphabet = node.getAttributeNS(NS.SSML, "alphabet") || inheritedAlphabet;
100
- if (!el) {
101
- const ph = node.getAttributeNS(NS.SSML, "ph");
102
- if (ph) {
103
- el = ssml.createElementNS(NS.SSML, "phoneme");
104
- if (alphabet) el.setAttribute("alphabet", alphabet);
105
- el.setAttribute("ph", ph);
106
- }
107
- }
108
- if (!el) el = parent;
109
- let child = node.firstChild;
110
- while (child) {
111
- const childEl = convert(child, el, alphabet);
112
- if (childEl && el !== childEl) el.append(childEl);
113
- child = child.nextSibling;
114
- }
115
- return el;
116
- };
117
- convert(fragment.firstChild, ssml.documentElement, inherited.alphabet);
118
- return ssml;
119
- };
120
- const getFragmentWithMarks = (range, textWalker, granularity) => {
121
- const lang = getLang(range.commonAncestorContainer);
122
- const alphabet = getAlphabet(range.commonAncestorContainer);
123
- const segmenter = getSegmenter(lang, granularity);
124
- const fragment = range.cloneContents();
125
- const entries = [...textWalker(range, segmenter)];
126
- const fragmentEntries = [...textWalker(fragment, segmenter)];
127
- for (const [name, range2] of fragmentEntries) {
128
- const mark = document.createElement("foliate-mark");
129
- mark.dataset.name = name;
130
- range2.insertNode(mark);
131
- }
132
- const ssml = fragmentToSSML(fragment, { lang, alphabet });
133
- return { entries, ssml };
134
- };
135
- const rangeIsEmpty = (range) => !range.toString().trim();
136
- function* getBlocks(doc) {
137
- let last;
138
- const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_ELEMENT);
139
- for (let node = walker.nextNode(); node; node = walker.nextNode()) {
140
- const name = node.tagName.toLowerCase();
141
- if (blockTags.has(name)) {
142
- if (last) {
143
- last.setEndBefore(node);
144
- if (!rangeIsEmpty(last)) yield last;
145
- }
146
- last = doc.createRange();
147
- last.setStart(node, 0);
148
- }
149
- }
150
- if (!last) {
151
- last = doc.createRange();
152
- last.setStart(doc.body.firstChild ?? doc.body, 0);
153
- }
154
- last.setEndAfter(doc.body.lastChild ?? doc.body);
155
- if (!rangeIsEmpty(last)) yield last;
156
- }
157
- class ListIterator {
158
- #arr = [];
159
- #iter;
160
- #index = -1;
161
- #f;
162
- constructor(iter, f = (x) => x) {
163
- this.#iter = iter;
164
- this.#f = f;
165
- }
166
- current() {
167
- if (this.#arr[this.#index]) return this.#f(this.#arr[this.#index]);
168
- }
169
- first() {
170
- const newIndex = 0;
171
- if (this.#arr[newIndex]) {
172
- this.#index = newIndex;
173
- return this.#f(this.#arr[newIndex]);
174
- }
175
- }
176
- prev() {
177
- const newIndex = this.#index - 1;
178
- if (this.#arr[newIndex]) {
179
- this.#index = newIndex;
180
- return this.#f(this.#arr[newIndex]);
181
- }
182
- }
183
- next() {
184
- const newIndex = this.#index + 1;
185
- if (this.#arr[newIndex]) {
186
- this.#index = newIndex;
187
- return this.#f(this.#arr[newIndex]);
188
- }
189
- while (true) {
190
- const { done, value } = this.#iter.next();
191
- if (done) break;
192
- this.#arr.push(value);
193
- if (this.#arr[newIndex]) {
194
- this.#index = newIndex;
195
- return this.#f(this.#arr[newIndex]);
196
- }
197
- }
198
- }
199
- find(f) {
200
- const index = this.#arr.findIndex((x) => f(x));
201
- if (index > -1) {
202
- this.#index = index;
203
- return this.#f(this.#arr[index]);
204
- }
205
- while (true) {
206
- const { done, value } = this.#iter.next();
207
- if (done) break;
208
- this.#arr.push(value);
209
- if (f(value)) {
210
- this.#index = this.#arr.length - 1;
211
- return this.#f(value);
212
- }
213
- }
214
- }
215
- }
216
- class TTS {
217
- #list;
218
- #ranges;
219
- #lastMark;
220
- #serializer = new XMLSerializer();
221
- constructor(doc, textWalker, highlight, granularity) {
222
- this.doc = doc;
223
- this.highlight = highlight;
224
- this.#list = new ListIterator(getBlocks(doc), (range) => {
225
- const { entries, ssml } = getFragmentWithMarks(range, textWalker, granularity);
226
- this.#ranges = new Map(entries);
227
- return [ssml, range];
228
- });
229
- }
230
- #getMarkElement(doc, mark) {
231
- if (!mark) return null;
232
- return doc.querySelector(`mark[name="${CSS.escape(mark)}"`);
233
- }
234
- #speak(doc, getNode) {
235
- var _a, _b;
236
- if (!doc) return;
237
- if (!getNode) return this.#serializer.serializeToString(doc);
238
- const ssml = document.implementation.createDocument(NS.SSML, "speak");
239
- ssml.documentElement.replaceWith(ssml.importNode(doc.documentElement, true));
240
- let node = (_a = getNode(ssml)) == null ? void 0 : _a.previousSibling;
241
- while (node) {
242
- const next = node.previousSibling ?? ((_b = node.parentNode) == null ? void 0 : _b.previousSibling);
243
- node.parentNode.removeChild(node);
244
- node = next;
245
- }
246
- return this.#serializer.serializeToString(ssml);
247
- }
248
- start() {
249
- this.#lastMark = null;
250
- const [doc] = this.#list.first() ?? [];
251
- if (!doc) return this.next();
252
- return this.#speak(doc, (ssml) => this.#getMarkElement(ssml, this.#lastMark));
253
- }
254
- resume() {
255
- const [doc] = this.#list.current() ?? [];
256
- if (!doc) return this.next();
257
- return this.#speak(doc, (ssml) => this.#getMarkElement(ssml, this.#lastMark));
258
- }
259
- prev(paused) {
260
- this.#lastMark = null;
261
- const [doc, range] = this.#list.prev() ?? [];
262
- if (paused && range) this.highlight(range.cloneRange());
263
- return this.#speak(doc);
264
- }
265
- next(paused) {
266
- this.#lastMark = null;
267
- const [doc, range] = this.#list.next() ?? [];
268
- if (paused && range) this.highlight(range.cloneRange());
269
- return this.#speak(doc);
270
- }
271
- from(range) {
272
- this.#lastMark = null;
273
- const [doc] = this.#list.find((range_) => range.compareBoundaryPoints(Range.END_TO_START, range_) <= 0);
274
- let mark;
275
- for (const [name, range_] of this.#ranges.entries())
276
- if (range.compareBoundaryPoints(Range.START_TO_START, range_) <= 0) {
277
- mark = name;
278
- break;
279
- }
280
- return this.#speak(doc, (ssml) => this.#getMarkElement(ssml, mark));
281
- }
282
- setMark(mark) {
283
- const range = this.#ranges.get(mark);
284
- if (range) {
285
- this.#lastMark = mark;
286
- this.highlight(range.cloneRange());
287
- }
288
- }
289
- }
290
- exports.TTS = TTS;