UNPKG

vue-files-preview-inno

Version:

A tool for previewing files such as doc, excel, pdf, image, markdown, txt, audio, and video and so on.

231 lines (230 loc) 7.74 kB
import d from "./epubcfi.mjs"; import { nodeBounds as v } from "./utils/core.mjs"; class T { constructor(e, n, i, a = !1) { this.layout = e, this.horizontal = i === "horizontal", this.direction = n || "ltr", this._dev = a; } /** * Find CFI pairs for entire section at once */ section(e) { var n = this.findRanges(e), i = this.rangeListToCfiList(e.section.cfiBase, n); return i; } /** * Find CFI pairs for a page * @param {Contents} contents Contents from view * @param {string} cfiBase string of the base for a cfi * @param {number} start position to start at * @param {number} end position to end at */ page(e, n, i, a) { var s = e && e.document ? e.document.body : !1, t; if (s) { if (t = this.rangePairToCfiPair(n, { start: this.findStart(s, i, a), end: this.findEnd(s, i, a) }), this._dev === !0) { let l = e.document, r = new d(t.start).toRange(l), o = new d(t.end).toRange(l), h = l.defaultView.getSelection(), u = l.createRange(); h.removeAllRanges(), u.setStart(r.startContainer, r.startOffset), u.setEnd(o.endContainer, o.endOffset), h.addRange(u); } return t; } } /** * Walk a node, preforming a function on each node it finds * @private * @param {Node} root Node to walkToNode * @param {function} func walk function * @return {*} returns the result of the walk function */ walk(e, n) { if (!(e && e.nodeType === Node.TEXT_NODE)) { var i = { acceptNode: function(r) { return r.data.trim().length > 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT; } }, a = i.acceptNode; a.acceptNode = i.acceptNode; for (var s = document.createTreeWalker(e, NodeFilter.SHOW_TEXT, a, !1), t, l; (t = s.nextNode()) && (l = n(t), !l); ) ; return l; } } findRanges(e) { for (var n = [], i = e.contents.scrollWidth(), a = Math.ceil(i / this.layout.spreadWidth), s = a * this.layout.divisor, t = this.layout.columnWidth, l = this.layout.gap, r, o, h = 0; h < s.pages; h++) r = (t + l) * h, o = t * (h + 1) + l * h, n.push({ start: this.findStart(e.document.body, r, o), end: this.findEnd(e.document.body, r, o) }); return n; } /** * Find Start Range * @private * @param {Node} root root node * @param {number} start position to start at * @param {number} end position to end at * @return {Range} */ findStart(e, n, i) { for (var a = [e], s, t, l = e; a.length; ) if (s = a.shift(), t = this.walk(s, (r) => { var o, h, u, g, f; if (f = v(r), this.horizontal && this.direction === "ltr") { if (o = this.horizontal ? f.left : f.top, h = this.horizontal ? f.right : f.bottom, o >= n && o <= i) return r; if (h > n) return r; l = r, a.push(r); } else if (this.horizontal && this.direction === "rtl") { if (o = f.left, h = f.right, h <= i && h >= n) return r; if (o < i) return r; l = r, a.push(r); } else { if (u = f.top, g = f.bottom, u >= n && u <= i) return r; if (g > n) return r; l = r, a.push(r); } }), t) return this.findTextStartRange(t, n, i); return this.findTextStartRange(l, n, i); } /** * Find End Range * @private * @param {Node} root root node * @param {number} start position to start at * @param {number} end position to end at * @return {Range} */ findEnd(e, n, i) { for (var a = [e], s, t = e, l; a.length; ) if (s = a.shift(), l = this.walk(s, (r) => { var o, h, u, g, f; if (f = v(r), this.horizontal && this.direction === "ltr") { if (o = Math.round(f.left), h = Math.round(f.right), o > i && t) return t; if (h > i) return r; t = r, a.push(r); } else if (this.horizontal && this.direction === "rtl") { if (o = Math.round(this.horizontal ? f.left : f.top), h = Math.round(this.horizontal ? f.right : f.bottom), h < n && t) return t; if (o < n) return r; t = r, a.push(r); } else { if (u = Math.round(f.top), g = Math.round(f.bottom), u > i && t) return t; if (g > i) return r; t = r, a.push(r); } }), l) return this.findTextEndRange(l, n, i); return this.findTextEndRange(t, n, i); } /** * Find Text Start Range * @private * @param {Node} root root node * @param {number} start position to start at * @param {number} end position to end at * @return {Range} */ findTextStartRange(e, n, i) { for (var a = this.splitTextNodeIntoRanges(e), s, t, l, r, o, h = 0; h < a.length; h++) if (s = a[h], t = s.getBoundingClientRect(), this.horizontal && this.direction === "ltr") { if (l = t.left, l >= n) return s; } else if (this.horizontal && this.direction === "rtl") { if (o = t.right, o <= i) return s; } else if (r = t.top, r >= n) return s; return a[0]; } /** * Find Text End Range * @private * @param {Node} root root node * @param {number} start position to start at * @param {number} end position to end at * @return {Range} */ findTextEndRange(e, n, i) { for (var a = this.splitTextNodeIntoRanges(e), s, t, l, r, o, h, u, g = 0; g < a.length; g++) { if (t = a[g], l = t.getBoundingClientRect(), this.horizontal && this.direction === "ltr") { if (r = l.left, o = l.right, r > i && s) return s; if (o > i) return t; } else if (this.horizontal && this.direction === "rtl") { if (r = l.left, o = l.right, o < n && s) return s; if (r < n) return t; } else { if (h = l.top, u = l.bottom, h > i && s) return s; if (u > i) return t; } s = t; } return a[a.length - 1]; } /** * Split up a text node into ranges for each word * @private * @param {Node} root root node * @param {string} [_splitter] what to split on * @return {Range[]} */ splitTextNodeIntoRanges(e, n) { var i = [], a = e.textContent || "", s = a.trim(), t, l = e.ownerDocument, r = n || " ", o = s.indexOf(r); if (o === -1 || e.nodeType != Node.TEXT_NODE) return t = l.createRange(), t.selectNodeContents(e), [t]; for (t = l.createRange(), t.setStart(e, 0), t.setEnd(e, o), i.push(t), t = !1; o != -1; ) o = s.indexOf(r, o + 1), o > 0 && (t && (t.setEnd(e, o), i.push(t)), t = l.createRange(), t.setStart(e, o + 1)); return t && (t.setEnd(e, s.length), i.push(t)), i; } /** * Turn a pair of ranges into a pair of CFIs * @private * @param {string} cfiBase base string for an EpubCFI * @param {object} rangePair { start: Range, end: Range } * @return {object} { start: "epubcfi(...)", end: "epubcfi(...)" } */ rangePairToCfiPair(e, n) { var i = n.start, a = n.end; i.collapse(!0), a.collapse(!1); let s = new d(i, e).toString(), t = new d(a, e).toString(); return { start: s, end: t }; } rangeListToCfiList(e, n) { for (var i = [], a, s = 0; s < n.length; s++) a = this.rangePairToCfiPair(e, n[s]), i.push(a); return i; } /** * Set the axis for mapping * @param {string} axis horizontal | vertical * @return {boolean} is it horizontal? */ axis(e) { return e && (this.horizontal = e === "horizontal"), this.horizontal; } } export { T as default }; //# sourceMappingURL=mapping.mjs.map