react-book-reader 1.0.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,263 @@
1
+ const N = (a) => a ? a.replace(/[\t\n\f\r ]+/g, " ").replace(/^[\t\n\f\r ]+/, "").replace(/[\t\n\f\r ]+$/, "") : "", d = (a) => N(a == null ? void 0 : a.textContent), w = {
2
+ XLINK: "http://www.w3.org/1999/xlink",
3
+ EPUB: "http://www.idpf.org/2007/ops"
4
+ }, S = {
5
+ XML: "application/xml",
6
+ XHTML: "application/xhtml+xml"
7
+ }, m = {
8
+ strong: ["strong", "self"],
9
+ emphasis: ["em", "self"],
10
+ style: ["span", "self"],
11
+ a: "anchor",
12
+ strikethrough: ["s", "self"],
13
+ sub: ["sub", "self"],
14
+ sup: ["sup", "self"],
15
+ code: ["code", "self"],
16
+ image: "image"
17
+ }, D = {
18
+ tr: ["tr", ["align"]],
19
+ th: ["th", ["colspan", "rowspan", "align", "valign"]],
20
+ td: ["td", ["colspan", "rowspan", "align", "valign"]]
21
+ }, E = {
22
+ epigraph: ["blockquote"],
23
+ subtitle: ["h2", m],
24
+ "text-author": ["p", m],
25
+ date: ["p", m],
26
+ stanza: "stanza"
27
+ }, T = {
28
+ title: ["header", {
29
+ p: ["h1", m],
30
+ "empty-line": ["br"]
31
+ }],
32
+ epigraph: ["blockquote", "self"],
33
+ image: "image",
34
+ annotation: ["aside"],
35
+ section: ["section", "self"],
36
+ p: ["p", m],
37
+ poem: ["blockquote", E],
38
+ subtitle: ["h2", m],
39
+ cite: ["blockquote", "self"],
40
+ "empty-line": ["br"],
41
+ table: ["table", D],
42
+ "text-author": ["p", m]
43
+ };
44
+ E.epigraph.push(T);
45
+ const $ = {
46
+ image: "image",
47
+ title: ["section", {
48
+ p: ["h1", m],
49
+ "empty-line": ["br"]
50
+ }],
51
+ epigraph: ["section", T],
52
+ section: ["section", T]
53
+ }, k = (a) => {
54
+ const t = a.getAttributeNS(w.XLINK, "href"), [, o] = t.split("#"), s = a.getRootNode().getElementById(o);
55
+ return s ? `data:${s.getAttribute("content-type")};base64,${s.textContent}` : t;
56
+ };
57
+ class X {
58
+ constructor(t) {
59
+ this.fb2 = t, this.doc = document.implementation.createDocument(w.XHTML, "html");
60
+ }
61
+ image(t) {
62
+ const o = this.doc.createElement("img");
63
+ return o.alt = t.getAttribute("alt"), o.title = t.getAttribute("title"), o.setAttribute("src", k(t)), o;
64
+ }
65
+ anchor(t) {
66
+ const o = this.convert(t, { a: ["a", m] });
67
+ return o.setAttribute("href", t.getAttributeNS(w.XLINK, "href")), t.getAttribute("type") === "note" && o.setAttributeNS(w.EPUB, "epub:type", "noteref"), o;
68
+ }
69
+ stanza(t) {
70
+ const o = this.convert(t, {
71
+ stanza: ["p", {
72
+ title: ["header", {
73
+ p: ["strong", m],
74
+ "empty-line": ["br"]
75
+ }],
76
+ subtitle: ["p", m]
77
+ }]
78
+ });
79
+ for (const s of t.children)
80
+ s.nodeName === "v" && (o.append(this.doc.createTextNode(s.textContent)), o.append(this.doc.createElement("br")));
81
+ return o;
82
+ }
83
+ convert(t, o) {
84
+ if (t.nodeType === 3)
85
+ return this.doc.createTextNode(t.textContent);
86
+ if (t.nodeType === 4)
87
+ return this.doc.createCDATASection(t.textContent);
88
+ if (t.nodeType === 8)
89
+ return this.doc.createComment(t.textContent);
90
+ const s = o == null ? void 0 : o[t.nodeName];
91
+ if (!s)
92
+ return null;
93
+ if (typeof s == "string")
94
+ return this[s](t);
95
+ const [p, c] = s, u = this.doc.createElement(p);
96
+ if (t.id && (u.id = t.id), u.classList.add(t.nodeName), Array.isArray(c))
97
+ for (const g of c)
98
+ u.setAttribute(g, t.getAttribute(g));
99
+ const y = c === "self" ? o : Array.isArray(c) ? null : c;
100
+ let b = t.firstChild;
101
+ for (; b; ) {
102
+ const g = this.convert(b, y);
103
+ g && u.append(g), b = b.nextSibling;
104
+ }
105
+ return u;
106
+ }
107
+ }
108
+ const z = async (a) => {
109
+ var u;
110
+ const t = await a.arrayBuffer(), o = new TextDecoder("utf-8").decode(t), s = new DOMParser(), p = s.parseFromString(o, S.XML), c = p.xmlEncoding || ((u = o.match(/^<\?xml\s+version\s*=\s*["']1.\d+"\s+encoding\s*=\s*["']([A-Za-z0-9._-]*)["']/)) == null ? void 0 : u[1]);
111
+ if (c && c.toLowerCase() !== "utf-8") {
112
+ const y = new TextDecoder(c).decode(t);
113
+ return s.parseFromString(y, S.XML);
114
+ }
115
+ return p;
116
+ }, O = URL.createObjectURL(new Blob([`
117
+ @namespace epub "http://www.idpf.org/2007/ops";
118
+ body > img, section > img {
119
+ display: block;
120
+ margin: auto;
121
+ }
122
+ .title h1 {
123
+ text-align: center;
124
+ }
125
+ body > section > .title, body.notesBodyType > .title {
126
+ margin: 3em 0;
127
+ }
128
+ body.notesBodyType > section .title h1 {
129
+ text-align: start;
130
+ }
131
+ body.notesBodyType > section .title {
132
+ margin: 1em 0;
133
+ }
134
+ p {
135
+ text-indent: 1em;
136
+ margin: 0;
137
+ }
138
+ :not(p) + p, p:first-child {
139
+ text-indent: 0;
140
+ }
141
+ .poem p {
142
+ text-indent: 0;
143
+ margin: 1em 0;
144
+ }
145
+ .text-author, .date {
146
+ text-align: end;
147
+ }
148
+ .text-author:before {
149
+ content: "—";
150
+ }
151
+ table {
152
+ border-collapse: collapse;
153
+ }
154
+ td, th {
155
+ padding: .25em;
156
+ }
157
+ a[epub|type~="noteref"] {
158
+ font-size: .75em;
159
+ vertical-align: super;
160
+ }
161
+ body:not(.notesBodyType) > .title, body:not(.notesBodyType) > .epigraph {
162
+ margin: 3em 0;
163
+ }
164
+ `], { type: "text/css" })), I = (a) => `<?xml version="1.0" encoding="utf-8"?>
165
+ <html xmlns="http://www.w3.org/1999/xhtml">
166
+ <head><link href="${O}" rel="stylesheet" type="text/css"/></head>
167
+ <body>${a}</body>
168
+ </html>`, v = "data-foliate-id", H = async (a) => {
169
+ const t = {}, o = await z(a), s = new X(o), p = (e) => o.querySelector(e), c = (e) => [...o.querySelectorAll(e)], u = (e) => {
170
+ const n = d(e.querySelector("nickname"));
171
+ if (n)
172
+ return n;
173
+ const r = d(e.querySelector("first-name")), i = d(e.querySelector("middle-name")), l = d(e.querySelector("last-name")), f = [r, i, l].filter((h) => h).join(" "), x = l ? [l, [r, i].filter((h) => h).join(" ")].join(", ") : null;
174
+ return { name: f, sortAs: x };
175
+ }, y = (e) => (e == null ? void 0 : e.getAttribute("value")) ?? d(e), b = p("title-info annotation");
176
+ if (t.metadata = {
177
+ title: d(p("title-info book-title")),
178
+ identifier: d(p("document-info id")),
179
+ language: d(p("title-info lang")),
180
+ author: c("title-info author").map(u),
181
+ translator: c("title-info translator").map(u),
182
+ producer: c("document-info author").map(u).concat(c("document-info program-used").map(d)),
183
+ publisher: d(p("publish-info publisher")),
184
+ published: y(p("title-info date")),
185
+ modified: y(p("document-info date")),
186
+ description: b ? s.convert(
187
+ b,
188
+ { annotation: ["div", T] }
189
+ ).innerHTML : null,
190
+ subject: c("title-info genre").map(d)
191
+ }, p("coverpage image")) {
192
+ const e = k(p("coverpage image"));
193
+ t.getCover = () => fetch(e).then((n) => n.blob());
194
+ } else
195
+ t.getCover = () => null;
196
+ const g = Array.from(o.querySelectorAll("body"), (e) => {
197
+ const n = s.convert(e, { body: ["body", $] });
198
+ return [Array.from(n.children, (r) => {
199
+ const i = [r, ...r.querySelectorAll("[id]")].map((l) => l.id);
200
+ return { el: r, ids: i };
201
+ }), n];
202
+ }), C = [], q = g[0][0].map(({ el: e, ids: n }) => {
203
+ const r = Array.from(
204
+ e.querySelectorAll(":scope > section > .title"),
205
+ (i, l) => (i.setAttribute(v, l), { title: d(i), index: l })
206
+ );
207
+ return { ids: n, titles: r, el: e };
208
+ }).concat(g.slice(1).map(([e, n]) => {
209
+ const r = e.map((i) => i.ids).flat();
210
+ return n.classList.add("notesBodyType"), { ids: r, el: n, linear: "no" };
211
+ })).map(({ ids: e, titles: n, el: r, linear: i }) => {
212
+ var M;
213
+ const l = I(r.outerHTML), f = new Blob([l], { type: S.XHTML }), x = URL.createObjectURL(f);
214
+ C.push(x);
215
+ const h = N(
216
+ ((M = r.querySelector(".title, .subtitle, p")) == null ? void 0 : M.textContent) ?? (r.classList.contains("title") ? r.textContent : "")
217
+ );
218
+ return {
219
+ ids: e,
220
+ title: h,
221
+ titles: n,
222
+ load: () => x,
223
+ createDocument: () => new DOMParser().parseFromString(l, S.XHTML),
224
+ // doo't count image data as it'd skew the size too much
225
+ size: f.size - Array.from(
226
+ r.querySelectorAll("[src]"),
227
+ (L) => {
228
+ var A;
229
+ return ((A = L.getAttribute("src")) == null ? void 0 : A.length) ?? 0;
230
+ }
231
+ ).reduce((L, A) => L + A, 0),
232
+ linear: i
233
+ };
234
+ }), B = /* @__PURE__ */ new Map();
235
+ return t.sections = q.map((e, n) => {
236
+ const { ids: r, load: i, createDocument: l, size: f, linear: x } = e;
237
+ for (const h of r)
238
+ h && B.set(h, n);
239
+ return { id: n, load: i, createDocument: l, size: f, linear: x };
240
+ }), t.toc = q.map(({ title: e, titles: n }, r) => {
241
+ const i = r.toString();
242
+ return {
243
+ label: e,
244
+ href: i,
245
+ subitems: n != null && n.length ? n.map(({ title: l, index: f }) => ({
246
+ label: l,
247
+ href: `${i}#${f}`
248
+ })) : null
249
+ };
250
+ }).filter((e) => e), t.resolveHref = (e) => {
251
+ const [n, r] = e.split("#");
252
+ return n ? { index: Number(n), anchor: (i) => i.querySelector(`[${v}="${r}"]`) } : { index: B.get(r), anchor: (i) => i.getElementById(r) };
253
+ }, t.splitTOCHref = (e) => {
254
+ var n;
255
+ return ((n = e == null ? void 0 : e.split("#")) == null ? void 0 : n.map((r) => Number(r))) ?? [];
256
+ }, t.getTOCFragment = (e, n) => e.querySelector(`[${v}="${n}"]`), t.destroy = () => {
257
+ for (const e of C)
258
+ URL.revokeObjectURL(e);
259
+ }, t;
260
+ };
261
+ export {
262
+ H as makeFB2
263
+ };
@@ -0,0 +1,162 @@
1
+ var c = Uint8Array, S = Uint16Array, Y = Uint32Array, r1 = new c([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0, 0]), n1 = new c([0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 0, 0]), i1 = new c([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]), e1 = function(n, e) {
2
+ for (var r = new S(31), v = 0; v < 31; ++v)
3
+ r[v] = e += 1 << n[v - 1];
4
+ var t = new Y(r[30]);
5
+ for (v = 1; v < 30; ++v)
6
+ for (var u = r[v]; u < r[v + 1]; ++u)
7
+ t[u] = u - r[v] << 5 | v;
8
+ return [r, t];
9
+ }, a1 = e1(r1, 2), t1 = a1[0], w1 = a1[1];
10
+ t1[28] = 258, w1[258] = 28;
11
+ for (var b1 = e1(n1, 0)[0], Z = new S(32768), f = 0; f < 32768; ++f) {
12
+ var x = (43690 & f) >>> 1 | (21845 & f) << 1;
13
+ x = (61680 & (x = (52428 & x) >>> 2 | (13107 & x) << 2)) >>> 4 | (3855 & x) << 4, Z[f] = ((65280 & x) >>> 8 | (255 & x) << 8) >>> 1;
14
+ }
15
+ var L = function(n, e, r) {
16
+ for (var v = n.length, t = 0, u = new S(e); t < v; ++t)
17
+ n[t] && ++u[n[t] - 1];
18
+ var s, b = new S(e);
19
+ for (t = 0; t < e; ++t)
20
+ b[t] = b[t - 1] + u[t - 1] << 1;
21
+ if (r) {
22
+ s = new S(1 << e);
23
+ var a = 15 - e;
24
+ for (t = 0; t < v; ++t)
25
+ if (n[t])
26
+ for (var o = t << 4 | n[t], l = e - n[t], E = b[n[t] - 1]++ << l, m = E | (1 << l) - 1; E <= m; ++E)
27
+ s[Z[E] >>> a] = o;
28
+ } else
29
+ for (s = new S(v), t = 0; t < v; ++t)
30
+ n[t] && (s[t] = Z[b[n[t] - 1]++] >>> 15 - n[t]);
31
+ return s;
32
+ }, M = new c(288);
33
+ for (f = 0; f < 144; ++f)
34
+ M[f] = 8;
35
+ for (f = 144; f < 256; ++f)
36
+ M[f] = 9;
37
+ for (f = 256; f < 280; ++f)
38
+ M[f] = 7;
39
+ for (f = 280; f < 288; ++f)
40
+ M[f] = 8;
41
+ var v1 = new c(32);
42
+ for (f = 0; f < 32; ++f)
43
+ v1[f] = 5;
44
+ var l1 = L(M, 9, 1), h1 = L(v1, 5, 1), G = function(n) {
45
+ for (var e = n[0], r = 1; r < n.length; ++r)
46
+ n[r] > e && (e = n[r]);
47
+ return e;
48
+ }, w = function(n, e, r) {
49
+ var v = e / 8 | 0;
50
+ return (n[v] | n[v + 1] << 8) >> (7 & e) & r;
51
+ }, J = function(n, e) {
52
+ var r = e / 8 | 0;
53
+ return (n[r] | n[r + 1] << 8 | n[r + 2] << 16) >> (7 & e);
54
+ }, s1 = ["unexpected EOF", "invalid block type", "invalid length/literal", "invalid distance", "stream finished", "no stream handler", , "no callback", "invalid UTF-8 data", "extra field too long", "date not in range 1980-2099", "filename too long", "stream finishing", "invalid zip data"], h = function(n, e, r) {
55
+ var v = new Error(e || s1[n]);
56
+ if (v.code = n, Error.captureStackTrace && Error.captureStackTrace(v, h), !r)
57
+ throw v;
58
+ return v;
59
+ }, E1 = function(n, e, r) {
60
+ var v = n.length;
61
+ if (!v || r && r.f && !r.l)
62
+ return e || new c(0);
63
+ var t = !e || r, u = !r || r.i;
64
+ r || (r = {}), e || (e = new c(3 * v));
65
+ var s = function(y) {
66
+ var T = e.length;
67
+ if (y > T) {
68
+ var k = new c(Math.max(2 * T, y));
69
+ k.set(e), e = k;
70
+ }
71
+ }, b = r.f || 0, a = r.p || 0, o = r.b || 0, l = r.l, E = r.d, m = r.m, _ = r.n, j = 8 * v;
72
+ do {
73
+ if (!l) {
74
+ b = w(n, a, 1);
75
+ var q = w(n, a + 1, 3);
76
+ if (a += 3, !q) {
77
+ var B = n[(p = 4 + ((a + 7) / 8 | 0)) - 4] | n[p - 3] << 8, D = p + B;
78
+ if (D > v) {
79
+ u && h(0);
80
+ break;
81
+ }
82
+ t && s(o + B), e.set(n.subarray(p, D), o), r.b = o += B, r.p = a = 8 * D, r.f = b;
83
+ continue;
84
+ }
85
+ if (q == 1)
86
+ l = l1, E = h1, m = 9, _ = 5;
87
+ else if (q == 2) {
88
+ var F = w(n, a, 31) + 257, P = w(n, a + 10, 15) + 4, W = F + w(n, a + 5, 31) + 1;
89
+ a += 14;
90
+ for (var d = new c(W), O = new c(19), i = 0; i < P; ++i)
91
+ O[i1[i]] = w(n, a + 3 * i, 7);
92
+ a += 3 * P;
93
+ var I = G(O), f1 = (1 << I) - 1, o1 = L(O, I, 1);
94
+ for (i = 0; i < W; ) {
95
+ var p, H = o1[w(n, a, f1)];
96
+ if (a += 15 & H, (p = H >>> 4) < 16)
97
+ d[i++] = p;
98
+ else {
99
+ var z = 0, N = 0;
100
+ for (p == 16 ? (N = 3 + w(n, a, 3), a += 2, z = d[i - 1]) : p == 17 ? (N = 3 + w(n, a, 7), a += 3) : p == 18 && (N = 11 + w(n, a, 127), a += 7); N--; )
101
+ d[i++] = z;
102
+ }
103
+ }
104
+ var $ = d.subarray(0, F), g = d.subarray(F);
105
+ m = G($), _ = G(g), l = L($, m, 1), E = L(g, _, 1);
106
+ } else
107
+ h(1);
108
+ if (a > j) {
109
+ u && h(0);
110
+ break;
111
+ }
112
+ }
113
+ t && s(o + 131072);
114
+ for (var u1 = (1 << m) - 1, c1 = (1 << _) - 1, Q = a; ; Q = a) {
115
+ var U = (z = l[J(n, a) & u1]) >>> 4;
116
+ if ((a += 15 & z) > j) {
117
+ u && h(0);
118
+ break;
119
+ }
120
+ if (z || h(2), U < 256)
121
+ e[o++] = U;
122
+ else {
123
+ if (U == 256) {
124
+ Q = a, l = null;
125
+ break;
126
+ }
127
+ var X = U - 254;
128
+ if (U > 264) {
129
+ var A = r1[i = U - 257];
130
+ X = w(n, a, (1 << A) - 1) + t1[i], a += A;
131
+ }
132
+ var R = E[J(n, a) & c1], V = R >>> 4;
133
+ if (R || h(3), a += 15 & R, g = b1[V], V > 3 && (A = n1[V], g += J(n, a) & (1 << A) - 1, a += A), a > j) {
134
+ u && h(0);
135
+ break;
136
+ }
137
+ t && s(o + 131072);
138
+ for (var K = o + X; o < K; o += 4)
139
+ e[o] = e[o - g], e[o + 1] = e[o + 1 - g], e[o + 2] = e[o + 2 - g], e[o + 3] = e[o + 3 - g];
140
+ o = K;
141
+ }
142
+ }
143
+ r.l = l, r.p = Q, r.b = o, r.f = b, l && (b = 1, r.m = m, r.d = E, r.n = _);
144
+ } while (!b);
145
+ return o == e.length ? e : function(y, T, k) {
146
+ (T == null || T < 0) && (T = 0), (k == null || k > y.length) && (k = y.length);
147
+ var C = new (y.BYTES_PER_ELEMENT == 2 ? S : y.BYTES_PER_ELEMENT == 4 ? Y : c)(k - T);
148
+ return C.set(y.subarray(T, k)), C;
149
+ }(e, 0, o);
150
+ }, p1 = new c(0);
151
+ function y1(n, e) {
152
+ return E1((((15 & (r = n)[0]) != 8 || r[0] >>> 4 > 7 || (r[0] << 8 | r[1]) % 31) && h(6, "invalid zlib data"), 32 & r[1] && h(6, "invalid zlib data: preset dictionaries not supported"), n.subarray(2, -4)), e);
153
+ var r;
154
+ }
155
+ var g1 = typeof TextDecoder < "u" && new TextDecoder();
156
+ try {
157
+ g1.decode(p1, { stream: !0 });
158
+ } catch {
159
+ }
160
+ export {
161
+ y1 as unzlibSync
162
+ };
@@ -0,0 +1,223 @@
1
+ var K = Object.defineProperty;
2
+ var Q = (r, h, t) => h in r ? K(r, h, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[h] = t;
3
+ var D = (r, h, t) => (Q(r, typeof h != "symbol" ? h + "" : h, t), t), W = (r, h, t) => {
4
+ if (!h.has(r))
5
+ throw TypeError("Cannot " + t);
6
+ };
7
+ var n = (r, h, t) => (W(r, h, "read from private field"), t ? t.call(r) : h.get(r)), f = (r, h, t) => {
8
+ if (h.has(r))
9
+ throw TypeError("Cannot add the same private member more than once");
10
+ h instanceof WeakSet ? h.add(r) : h.set(r, t);
11
+ }, p = (r, h, t, e) => (W(r, h, "write to private field"), e ? e.call(r, t) : h.set(r, t), t);
12
+ var c = (r, h, t) => (W(r, h, "access private method"), t);
13
+ const I = (r) => {
14
+ var h, t;
15
+ return (t = (h = r == null ? void 0 : r.split(/[,;\s]/)) == null ? void 0 : h.filter((e) => e)) == null ? void 0 : t.map((e) => e.split("=").map((i) => i.trim()));
16
+ }, U = (r, h) => {
17
+ var i, s;
18
+ if (r.documentElement.localName === "svg") {
19
+ const [, , a, o] = ((i = r.documentElement.getAttribute("viewBox")) == null ? void 0 : i.split(/\s/)) ?? [];
20
+ return { width: a, height: o };
21
+ }
22
+ const t = I((s = r.querySelector('meta[name="viewport"]')) == null ? void 0 : s.getAttribute("content"));
23
+ if (t)
24
+ return Object.fromEntries(t);
25
+ if (typeof h == "string")
26
+ return I(h);
27
+ if (h)
28
+ return h;
29
+ const e = r.querySelector("img");
30
+ return e ? { width: e.naturalWidth, height: e.naturalHeight } : (console.warn(new Error("Missing viewport properties")), { width: 1e3, height: 2e3 });
31
+ };
32
+ var k, j, x, S, L, w, y, b, v, A, H, E, C, M, N, R, P, V, G, T, z;
33
+ class X extends HTMLElement {
34
+ constructor() {
35
+ super();
36
+ f(this, A);
37
+ f(this, E);
38
+ f(this, M);
39
+ f(this, R);
40
+ f(this, V);
41
+ f(this, T);
42
+ f(this, k, this.attachShadow({ mode: "closed" }));
43
+ f(this, j, new ResizeObserver(() => c(this, E, C).call(this)));
44
+ f(this, x, void 0);
45
+ f(this, S, -1);
46
+ D(this, "defaultViewport");
47
+ D(this, "spread");
48
+ f(this, L, !1);
49
+ f(this, w, void 0);
50
+ f(this, y, void 0);
51
+ f(this, b, void 0);
52
+ f(this, v, void 0);
53
+ const t = new CSSStyleSheet();
54
+ n(this, k).adoptedStyleSheets = [t], t.replaceSync(`:host {
55
+ width: 100%;
56
+ height: 100%;
57
+ display: flex;
58
+ justify-content: center;
59
+ align-items: center;
60
+ }`), n(this, j).observe(this);
61
+ }
62
+ open(t) {
63
+ this.book = t;
64
+ const { rendition: e } = t;
65
+ this.spread = e == null ? void 0 : e.spread, this.defaultViewport = e == null ? void 0 : e.viewport;
66
+ const i = t.dir === "rtl", s = !i;
67
+ this.rtl = i, (e == null ? void 0 : e.spread) === "none" ? p(this, x, t.sections.map((a) => ({ center: a }))) : p(this, x, t.sections.reduce((a, o) => {
68
+ const l = a[a.length - 1], { linear: m, pageSpread: g } = o;
69
+ if (m === "no")
70
+ return a;
71
+ const u = () => {
72
+ const d = {};
73
+ return a.push(d), d;
74
+ };
75
+ if (g === "center") {
76
+ const d = l.left || l.right ? u() : l;
77
+ d.center = o;
78
+ } else if (g === "left") {
79
+ const d = l.center || l.left || s ? u() : l;
80
+ d.left = o;
81
+ } else if (g === "right") {
82
+ const d = l.center || l.right || i ? u() : l;
83
+ d.right = o;
84
+ } else
85
+ s ? l.center || l.right ? u().left = o : l.left ? l.right = o : l.left = o : l.center || l.left ? u().right = o : l.right ? l.left = o : l.right = o;
86
+ return a;
87
+ }, [{}]));
88
+ }
89
+ get index() {
90
+ const t = n(this, x)[n(this, S)], e = (t == null ? void 0 : t.center) ?? (this.side === "left" ? t.left ?? t.right : t.right ?? t.left);
91
+ return this.book.sections.indexOf(e);
92
+ }
93
+ getSpreadOf(t) {
94
+ const e = n(this, x);
95
+ for (let i = 0; i < e.length; i++) {
96
+ const { left: s, right: a, center: o } = e[i];
97
+ if (s === t)
98
+ return { index: i, side: "left" };
99
+ if (a === t)
100
+ return { index: i, side: "right" };
101
+ if (o === t)
102
+ return { index: i, side: "center" };
103
+ }
104
+ }
105
+ async goToSpread(t, e, i) {
106
+ var a, o, l, m, g, u;
107
+ if (t < 0 || t > n(this, x).length - 1)
108
+ return;
109
+ if (t === n(this, S)) {
110
+ c(this, E, C).call(this, e);
111
+ return;
112
+ }
113
+ p(this, S, t);
114
+ const s = n(this, x)[t];
115
+ if (s.center) {
116
+ const d = this.book.sections.indexOf(s.center), O = await ((o = (a = s.center) == null ? void 0 : a.load) == null ? void 0 : o.call(a));
117
+ await c(this, M, N).call(this, { center: { index: d, src: O } });
118
+ } else {
119
+ const d = this.book.sections.indexOf(s.left), O = this.book.sections.indexOf(s.right), $ = await ((m = (l = s.left) == null ? void 0 : l.load) == null ? void 0 : m.call(l)), B = await ((u = (g = s.right) == null ? void 0 : g.load) == null ? void 0 : u.call(g)), F = { index: d, src: $ }, q = { index: O, src: B };
120
+ await c(this, M, N).call(this, { left: F, right: q, side: e });
121
+ }
122
+ c(this, T, z).call(this, i);
123
+ }
124
+ async select(t) {
125
+ await this.goTo(t);
126
+ }
127
+ async goTo(t) {
128
+ const { book: e } = this, i = await t, s = e.sections[i.index];
129
+ if (!s)
130
+ return;
131
+ const { index: a, side: o } = this.getSpreadOf(s);
132
+ await this.goToSpread(a, o);
133
+ }
134
+ async next() {
135
+ if (this.rtl ? c(this, R, P).call(this) : c(this, V, G).call(this))
136
+ c(this, T, z).call(this, "page");
137
+ else
138
+ return this.goToSpread(n(this, S) + 1, this.rtl ? "right" : "left", "page");
139
+ }
140
+ async prev() {
141
+ if (this.rtl ? c(this, V, G).call(this) : c(this, R, P).call(this))
142
+ c(this, T, z).call(this, "page");
143
+ else
144
+ return this.goToSpread(n(this, S) - 1, this.rtl ? "left" : "right", "page");
145
+ }
146
+ getContents() {
147
+ return Array.from(n(this, k).querySelectorAll("iframe"), (t) => ({
148
+ doc: t.contentDocument
149
+ // TODO: index, overlayer
150
+ }));
151
+ }
152
+ destroy() {
153
+ n(this, j).unobserve(this);
154
+ }
155
+ }
156
+ k = new WeakMap(), j = new WeakMap(), x = new WeakMap(), S = new WeakMap(), L = new WeakMap(), w = new WeakMap(), y = new WeakMap(), b = new WeakMap(), v = new WeakMap(), A = new WeakSet(), H = async function({ index: t, src: e }) {
157
+ const i = document.createElement("div"), s = document.createElement("iframe");
158
+ return i.append(s), Object.assign(s.style, {
159
+ border: "0",
160
+ display: "none",
161
+ overflow: "hidden"
162
+ }), s.setAttribute("sandbox", "allow-same-origin allow-scripts"), s.setAttribute("scrolling", "no"), s.setAttribute("part", "filter"), n(this, k).append(i), e ? new Promise((a) => {
163
+ const o = () => {
164
+ s.removeEventListener("load", o);
165
+ const l = s.contentDocument;
166
+ this.dispatchEvent(new CustomEvent("load", { detail: { doc: l, index: t } }));
167
+ const { width: m, height: g } = U(l, this.defaultViewport);
168
+ a({
169
+ element: i,
170
+ iframe: s,
171
+ width: parseFloat(m),
172
+ height: parseFloat(g)
173
+ });
174
+ };
175
+ s.addEventListener("load", o), s.src = e;
176
+ }) : { blank: !0, element: i, iframe: s };
177
+ }, E = new WeakSet(), C = function(t = n(this, v)) {
178
+ if (!t)
179
+ return;
180
+ const e = n(this, w) ?? {}, i = n(this, b) ?? n(this, y), s = t === "left" ? e : i, { width: a, height: o } = this.getBoundingClientRect(), l = this.spread !== "both" && this.spread !== "portrait" && o > a;
181
+ p(this, L, l);
182
+ const m = e.width ?? i.width, g = e.height ?? i.height, u = l || n(this, b) ? Math.min(
183
+ a / (s.width ?? m),
184
+ o / (s.height ?? g)
185
+ ) : Math.min(
186
+ a / ((e.width ?? m) + (i.width ?? m)),
187
+ o / Math.max(
188
+ e.height ?? g,
189
+ i.height ?? g
190
+ )
191
+ ), d = (O) => {
192
+ const { element: $, iframe: B, width: F, height: q, blank: J } = O;
193
+ Object.assign(B.style, {
194
+ width: `${F}px`,
195
+ height: `${q}px`,
196
+ transform: `scale(${u})`,
197
+ transformOrigin: "top left",
198
+ display: J ? "none" : "block"
199
+ }), Object.assign($.style, {
200
+ width: `${(F ?? m) * u}px`,
201
+ height: `${(q ?? g) * u}px`,
202
+ overflow: "hidden",
203
+ display: "block"
204
+ }), l && O !== s && ($.style.display = "none");
205
+ };
206
+ n(this, b) ? d(n(this, b)) : (d(e), d(i));
207
+ }, M = new WeakSet(), N = async function({ left: t, right: e, center: i, side: s }) {
208
+ n(this, k).replaceChildren(), p(this, w, null), p(this, y, null), p(this, b, null), i ? (p(this, b, await c(this, A, H).call(this, i)), p(this, v, "center"), c(this, E, C).call(this)) : (p(this, w, await c(this, A, H).call(this, t)), p(this, y, await c(this, A, H).call(this, e)), p(this, v, n(this, w).blank ? "right" : n(this, y).blank ? "left" : s), c(this, E, C).call(this));
209
+ }, R = new WeakSet(), P = function() {
210
+ var t, e, i, s;
211
+ if (!(n(this, b) || (t = n(this, w)) != null && t.blank) && n(this, L) && ((s = (i = (e = n(this, w)) == null ? void 0 : e.element) == null ? void 0 : i.style) == null ? void 0 : s.display) === "none")
212
+ return n(this, y).element.style.display = "none", n(this, w).element.style.display = "block", p(this, v, "left"), !0;
213
+ }, V = new WeakSet(), G = function() {
214
+ var t, e, i, s;
215
+ if (!(n(this, b) || (t = n(this, y)) != null && t.blank) && n(this, L) && ((s = (i = (e = n(this, y)) == null ? void 0 : e.element) == null ? void 0 : i.style) == null ? void 0 : s.display) === "none")
216
+ return n(this, w).element.style.display = "none", n(this, y).element.style.display = "block", p(this, v, "right"), !0;
217
+ }, T = new WeakSet(), z = function(t) {
218
+ this.dispatchEvent(new CustomEvent("relocate", { detail: { reason: t, range: null, index: this.index, fraction: 0, size: 1 } }));
219
+ };
220
+ customElements.define("foliate-fxl", X);
221
+ export {
222
+ X as FixedLayout
223
+ };