react-book-reader 1.0.0 → 1.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.
@@ -0,0 +1,72 @@
1
+ const w = (e) => e.replace(/\s+/g, " "), I = (e, { startIndex: g, startOffset: a, endIndex: l, endOffset: c }) => {
2
+ const r = e[g], o = e[l], h = r === o ? r.slice(a, c) : r.slice(a) + e.slice(r + 1, o).join("") + o.slice(0, c), u = w(r.slice(0, a)).trimStart(), n = w(o.slice(c)).trimEnd(), t = u.length < 50 ? "" : "…", i = n.length < 50 ? "" : "…", s = `${t}${u.slice(-50)}`, m = `${n.slice(0, 50)}${i}`;
3
+ return { pre: s, match: h, post: m };
4
+ }, T = function* (e, g, a = {}) {
5
+ const { locales: l = "en", sensitivity: c } = a, r = c === "variant", o = e.join(""), h = r ? o : o.toLocaleLowerCase(l), u = r ? g : g.toLocaleLowerCase(l), n = u.length;
6
+ let t = -1, i = -1, s = 0;
7
+ do
8
+ if (t = h.indexOf(u, t + 1), t > -1) {
9
+ for (; s <= t; ) s += e[++i].length;
10
+ const m = i, d = t - (s - e[i].length), f = t + n;
11
+ for (; s <= f; ) s += e[++i].length;
12
+ const x = i, y = f - (s - e[i].length), p = { startIndex: m, startOffset: d, endIndex: x, endOffset: y };
13
+ yield { range: p, excerpt: I(e, p) };
14
+ }
15
+ while (t > -1);
16
+ }, E = function* (e, g, a = {}) {
17
+ const { locales: l = "en", granularity: c = "word", sensitivity: r = "base" } = a;
18
+ let o, h;
19
+ try {
20
+ o = new Intl.Segmenter(l, { usage: "search", granularity: c }), h = new Intl.Collator(l, { sensitivity: r });
21
+ } catch (s) {
22
+ console.warn(s), o = new Intl.Segmenter("en", { usage: "search", granularity: c }), h = new Intl.Collator("en", { sensitivity: r });
23
+ }
24
+ const u = Array.from(o.segment(g)).length, n = [];
25
+ let t = 0, i = o.segment(e[t])[Symbol.iterator]();
26
+ e: for (; t < e.length; ) {
27
+ for (; n.length < u; ) {
28
+ const { done: m, value: d } = i.next();
29
+ if (m)
30
+ if (t++, t < e.length) {
31
+ i = o.segment(e[t])[Symbol.iterator]();
32
+ continue;
33
+ } else break e;
34
+ const { index: f, segment: x } = d;
35
+ if (/[^\p{Format}]/u.test(x)) {
36
+ if (/\s/u.test(x)) {
37
+ /\s/u.test(n[n.length - 1]?.segment) || n.push({ strIndex: t, index: f, segment: " " });
38
+ continue;
39
+ }
40
+ d.strIndex = t, n.push(d);
41
+ }
42
+ }
43
+ const s = n.map((m) => m.segment).join("");
44
+ if (h.compare(g, s) === 0) {
45
+ const m = t, d = n[n.length - 1], f = d.index + d.segment.length, x = n[0].strIndex, y = n[0].index, p = { startIndex: x, startOffset: y, endIndex: m, endOffset: f };
46
+ yield { range: p, excerpt: I(e, p) };
47
+ }
48
+ n.shift();
49
+ }
50
+ }, L = (e, g, a) => {
51
+ const { granularity: l = "grapheme", sensitivity: c = "base" } = a;
52
+ return !Intl?.Segmenter || l === "grapheme" && (c === "variant" || c === "accent") ? T(e, g, a) : E(e, g, a);
53
+ }, C = (e, g) => {
54
+ const { defaultLocale: a, matchCase: l, matchDiacritics: c, matchWholeWords: r, acceptNode: o } = g;
55
+ return function* (h, u) {
56
+ const n = e(h, function* (t, i) {
57
+ for (const s of L(t, u, {
58
+ locales: h.body.lang || h.documentElement.lang || a || "en",
59
+ granularity: r ? "word" : "grapheme",
60
+ sensitivity: c && l ? "variant" : c && !l ? "accent" : !c && l ? "case" : "base"
61
+ })) {
62
+ const { startIndex: m, startOffset: d, endIndex: f, endOffset: x } = s.range;
63
+ s.range = i(m, d, f, x), yield s;
64
+ }
65
+ }, o);
66
+ for (const t of n) yield t;
67
+ };
68
+ };
69
+ export {
70
+ L as search,
71
+ C as searchMatcher
72
+ };
@@ -0,0 +1,212 @@
1
+ const a = {
2
+ XML: "http://www.w3.org/XML/1998/namespace",
3
+ SSML: "http://www.w3.org/2001/10/synthesis"
4
+ }, M = /* @__PURE__ */ new Set([
5
+ "article",
6
+ "aside",
7
+ "audio",
8
+ "blockquote",
9
+ "caption",
10
+ "details",
11
+ "dialog",
12
+ "div",
13
+ "dl",
14
+ "dt",
15
+ "dd",
16
+ "figure",
17
+ "footer",
18
+ "form",
19
+ "figcaption",
20
+ "h1",
21
+ "h2",
22
+ "h3",
23
+ "h4",
24
+ "h5",
25
+ "h6",
26
+ "header",
27
+ "hgroup",
28
+ "hr",
29
+ "li",
30
+ "main",
31
+ "math",
32
+ "nav",
33
+ "ol",
34
+ "p",
35
+ "pre",
36
+ "section",
37
+ "tr"
38
+ ]), p = (s) => {
39
+ const t = s.lang || s?.getAttributeNS?.(a.XML, "lang");
40
+ return t || (s.parentElement ? p(s.parentElement) : null);
41
+ }, b = (s) => {
42
+ const t = s?.getAttributeNS?.(a.XML, "lang");
43
+ return t || (s.parentElement ? b(s.parentElement) : null);
44
+ }, N = (s = "en", t = "word") => {
45
+ const e = new Intl.Segmenter(s, { granularity: t }), n = t === "word";
46
+ return function* (i, o) {
47
+ const l = i.join("");
48
+ let g = 0, r = -1, h = 0;
49
+ for (const { index: u, segment: f, isWordLike: c } of e.segment(l)) {
50
+ if (n && !c) continue;
51
+ for (; h <= u; ) h += i[++r].length;
52
+ const m = r, w = u - (h - i[r].length), d = u + f.length - 1;
53
+ if (d < l.length) for (; h <= d; ) h += i[++r].length;
54
+ const x = r, L = d - (h - i[r].length) + 1;
55
+ yield [
56
+ (g++).toString(),
57
+ o(m, w, x, L)
58
+ ];
59
+ }
60
+ };
61
+ }, E = (s, t) => {
62
+ const e = document.implementation.createDocument(a.SSML, "speak"), { lang: n } = t;
63
+ n && e.documentElement.setAttributeNS(a.XML, "lang", n);
64
+ const i = (o, l, g) => {
65
+ if (!o) return;
66
+ if (o.nodeType === 3) return e.createTextNode(o.textContent);
67
+ if (o.nodeType === 4) return e.createCDATASection(o.textContent);
68
+ if (o.nodeType !== 1) return;
69
+ let r;
70
+ const h = o.nodeName.toLowerCase();
71
+ h === "foliate-mark" ? (r = e.createElementNS(a.SSML, "mark"), r.setAttribute("name", o.dataset.name)) : h === "br" ? r = e.createElementNS(a.SSML, "break") : (h === "em" || h === "strong") && (r = e.createElementNS(a.SSML, "emphasis"));
72
+ const u = o.lang || o.getAttributeNS(a.XML, "lang");
73
+ u && (r || (r = e.createElementNS(a.SSML, "lang")), r.setAttributeNS(a.XML, "lang", u));
74
+ const f = o.getAttributeNS(a.SSML, "alphabet") || g;
75
+ if (!r) {
76
+ const m = o.getAttributeNS(a.SSML, "ph");
77
+ m && (r = e.createElementNS(a.SSML, "phoneme"), f && r.setAttribute("alphabet", f), r.setAttribute("ph", m));
78
+ }
79
+ r || (r = l);
80
+ let c = o.firstChild;
81
+ for (; c; ) {
82
+ const m = i(c, r, f);
83
+ m && r !== m && r.append(m), c = c.nextSibling;
84
+ }
85
+ return r;
86
+ };
87
+ return i(s.firstChild, e.documentElement, t.alphabet), e;
88
+ }, T = (s, t, e) => {
89
+ const n = p(s.commonAncestorContainer), i = b(s.commonAncestorContainer), o = N(n, e), l = s.cloneContents(), g = [...t(s, o)], r = [...t(l, o)];
90
+ for (const [u, f] of r) {
91
+ const c = document.createElement("foliate-mark");
92
+ c.dataset.name = u, f.insertNode(c);
93
+ }
94
+ const h = E(l, { lang: n, alphabet: i });
95
+ return { entries: g, ssml: h };
96
+ }, S = (s) => !s.toString().trim();
97
+ function* k(s) {
98
+ let t;
99
+ const e = s.createTreeWalker(s.body, NodeFilter.SHOW_ELEMENT);
100
+ for (let n = e.nextNode(); n; n = e.nextNode()) {
101
+ const i = n.tagName.toLowerCase();
102
+ M.has(i) && (t && (t.setEndBefore(n), S(t) || (yield t)), t = s.createRange(), t.setStart(n, 0));
103
+ }
104
+ t || (t = s.createRange(), t.setStart(s.body.firstChild ?? s.body, 0)), t.setEndAfter(s.body.lastChild ?? s.body), S(t) || (yield t);
105
+ }
106
+ class A {
107
+ #t = [];
108
+ #i;
109
+ #e = -1;
110
+ #n;
111
+ constructor(t, e = (n) => n) {
112
+ this.#i = t, this.#n = e;
113
+ }
114
+ current() {
115
+ if (this.#t[this.#e]) return this.#n(this.#t[this.#e]);
116
+ }
117
+ first() {
118
+ if (this.#t[0])
119
+ return this.#e = 0, this.#n(this.#t[0]);
120
+ }
121
+ prev() {
122
+ const t = this.#e - 1;
123
+ if (this.#t[t])
124
+ return this.#e = t, this.#n(this.#t[t]);
125
+ }
126
+ next() {
127
+ const t = this.#e + 1;
128
+ if (this.#t[t])
129
+ return this.#e = t, this.#n(this.#t[t]);
130
+ for (; ; ) {
131
+ const { done: e, value: n } = this.#i.next();
132
+ if (e) break;
133
+ if (this.#t.push(n), this.#t[t])
134
+ return this.#e = t, this.#n(this.#t[t]);
135
+ }
136
+ }
137
+ find(t) {
138
+ const e = this.#t.findIndex((n) => t(n));
139
+ if (e > -1)
140
+ return this.#e = e, this.#n(this.#t[e]);
141
+ for (; ; ) {
142
+ const { done: n, value: i } = this.#i.next();
143
+ if (n) break;
144
+ if (this.#t.push(i), t(i))
145
+ return this.#e = this.#t.length - 1, this.#n(i);
146
+ }
147
+ }
148
+ }
149
+ class y {
150
+ #t;
151
+ #i;
152
+ #e;
153
+ #n = new XMLSerializer();
154
+ constructor(t, e, n, i) {
155
+ this.doc = t, this.highlight = n, this.#t = new A(k(t), (o) => {
156
+ const { entries: l, ssml: g } = T(o, e, i);
157
+ return this.#i = new Map(l), [g, o];
158
+ });
159
+ }
160
+ #r(t, e) {
161
+ return e ? t.querySelector(`mark[name="${CSS.escape(e)}"`) : null;
162
+ }
163
+ #s(t, e) {
164
+ if (!t) return;
165
+ if (!e) return this.#n.serializeToString(t);
166
+ const n = document.implementation.createDocument(a.SSML, "speak");
167
+ n.documentElement.replaceWith(n.importNode(t.documentElement, !0));
168
+ let i = e(n)?.previousSibling;
169
+ for (; i; ) {
170
+ const o = i.previousSibling ?? i.parentNode?.previousSibling;
171
+ i.parentNode.removeChild(i), i = o;
172
+ }
173
+ return this.#n.serializeToString(n);
174
+ }
175
+ start() {
176
+ this.#e = null;
177
+ const [t] = this.#t.first() ?? [];
178
+ return t ? this.#s(t, (e) => this.#r(e, this.#e)) : this.next();
179
+ }
180
+ resume() {
181
+ const [t] = this.#t.current() ?? [];
182
+ return t ? this.#s(t, (e) => this.#r(e, this.#e)) : this.next();
183
+ }
184
+ prev(t) {
185
+ this.#e = null;
186
+ const [e, n] = this.#t.prev() ?? [];
187
+ return t && n && this.highlight(n.cloneRange()), this.#s(e);
188
+ }
189
+ next(t) {
190
+ this.#e = null;
191
+ const [e, n] = this.#t.next() ?? [];
192
+ return t && n && this.highlight(n.cloneRange()), this.#s(e);
193
+ }
194
+ from(t) {
195
+ this.#e = null;
196
+ const [e] = this.#t.find((i) => t.compareBoundaryPoints(Range.END_TO_START, i) <= 0);
197
+ let n;
198
+ for (const [i, o] of this.#i.entries())
199
+ if (t.compareBoundaryPoints(Range.START_TO_START, o) <= 0) {
200
+ n = i;
201
+ break;
202
+ }
203
+ return this.#s(e, (i) => this.#r(i, n));
204
+ }
205
+ setMark(t) {
206
+ const e = this.#i.get(t);
207
+ e && (this.#e = t, this.highlight(e.cloneRange()));
208
+ }
209
+ }
210
+ export {
211
+ y as TTS
212
+ };