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.

153 lines (152 loc) 6.14 kB
import { defer as y, sprint as F } from "./utils/core.mjs"; import z from "./epubcfi.mjs"; import N from "./utils/hook.mjs"; import { replaceBase as I } from "./utils/replacements.mjs"; import O from "./utils/request.mjs"; import { DOMParser as R } from "../../@xmldom/xmldom/lib/index.mjs"; class H { constructor(e, t) { this.idref = e.idref, this.linear = e.linear === "yes", this.properties = e.properties, this.index = e.index, this.href = e.href, this.url = e.url, this.canonical = e.canonical, this.next = e.next, this.prev = e.prev, this.cfiBase = e.cfiBase, t ? this.hooks = t : (this.hooks = {}, this.hooks.serialize = new N(this), this.hooks.content = new N(this)), this.document = void 0, this.contents = void 0, this.output = void 0; } /** * Load the section from its url * @param {method} [_request] a request method to use for loading * @return {document} a promise with the xml document */ load(e) { var t = e || this.request || O, o = new y(), n = o.promise; return this.contents ? o.resolve(this.contents) : t(this.url).then((function(i) { return this.document = i, this.contents = i.documentElement, this.hooks.content.trigger(this.document, this); }).bind(this)).then((function() { o.resolve(this.contents); }).bind(this)).catch(function(i) { o.reject(i); }), n; } /** * Adds a base tag for resolving urls in the section * @private */ base() { return I(this.document, this); } /** * Render the contents of a section * @param {method} [_request] a request method to use for loading * @return {string} output a serialized XML Document */ render(e) { var t = new y(), o = t.promise; return this.output, this.load(e).then((function(n) { var i = typeof navigator < "u" && navigator.userAgent || "", r = i.indexOf("Trident") >= 0, h; typeof XMLSerializer > "u" || r ? h = R : h = XMLSerializer; var c = new h(); return this.output = c.serializeToString(n), this.output; }).bind(this)).then((function() { return this.hooks.serialize.trigger(this.output, this); }).bind(this)).then((function() { t.resolve(this.output); }).bind(this)).catch(function(n) { t.reject(n); }), o; } /** * Find a string in a section * @param {string} _query The query string to find * @return {object[]} A list of matches, with form {cfi, excerpt} */ find(e) { var t = this, o = [], n = e.toLowerCase(), i = function(r) { for (var h = r.textContent.toLowerCase(), c = t.document.createRange(), g, s, a = -1, d, v = 150; s != -1; ) s = h.indexOf(n, a + 1), s != -1 && (c = t.document.createRange(), c.setStart(r, s), c.setEnd(r, s + n.length), g = t.cfiFromRange(c), r.textContent.length < v ? d = r.textContent : (d = r.textContent.substring(s - v / 2, s + v / 2), d = "..." + d + "..."), o.push({ cfi: g, excerpt: d })), a = s; }; return F(t.document, function(r) { i(r); }), o; } /** * Search a string in multiple sequential Element of the section. If the document.createTreeWalker api is missed(eg: IE8), use `find` as a fallback. * @param {string} _query The query string to search * @param {int} maxSeqEle The maximum number of Element that are combined for search, default value is 5. * @return {object[]} A list of matches, with form {cfi, excerpt} */ search(e, t = 5) { if (typeof document.createTreeWalker > "u") return this.find(e); let o = []; const n = 150, i = this, r = e.toLowerCase(), h = function(a) { const u = a.reduce((w, f) => w + f.textContent, "").toLowerCase().indexOf(r); if (u != -1) { const f = u + r.length; let l = 0, k = 0; if (u < a[0].length) { let b; for (; l < a.length - 1 && (k += a[l].length, !(f <= k)); ) l += 1; let E = a[0], B = a[l], x = i.document.createRange(); x.setStart(E, u); let S = a.slice(0, l).reduce((m, C) => m + C.textContent.length, 0); x.setEnd(B, S > f ? f : f - S), b = i.cfiFromRange(x); let p = a.slice(0, l + 1).reduce((m, C) => m + C.textContent, ""); p.length > n && (p = p.substring(u - n / 2, u + n / 2), p = "..." + p + "..."), o.push({ cfi: b, excerpt: p }); } } }, c = document.createTreeWalker(i.document, NodeFilter.SHOW_TEXT, null, !1); let g, s = []; for (; g = c.nextNode(); ) s.push(g), s.length == t && (h(s.slice(0, t)), s = s.slice(1, t)); return s.length > 0 && h(s), o; } /** * Reconciles the current chapters layout properties with * the global layout properties. * @param {object} globalLayout The global layout settings object, chapter properties string * @return {object} layoutProperties Object with layout properties */ reconcileLayoutSettings(e) { var t = { layout: e.layout, spread: e.spread, orientation: e.orientation }; return this.properties.forEach(function(o) { var n = o.replace("rendition:", ""), i = n.indexOf("-"), r, h; i != -1 && (r = n.slice(0, i), h = n.slice(i + 1), t[r] = h); }), t; } /** * Get a CFI from a Range in the Section * @param {range} _range * @return {string} cfi an EpubCFI string */ cfiFromRange(e) { return new z(e, this.cfiBase).toString(); } /** * Get a CFI from an Element in the Section * @param {element} el * @return {string} cfi an EpubCFI string */ cfiFromElement(e) { return new z(e, this.cfiBase).toString(); } /** * Unload the section document */ unload() { this.document = void 0, this.contents = void 0, this.output = void 0; } destroy() { this.unload(), this.hooks.serialize.clear(), this.hooks.content.clear(), this.hooks = void 0, this.idref = void 0, this.linear = void 0, this.properties = void 0, this.index = void 0, this.href = void 0, this.url = void 0, this.next = void 0, this.prev = void 0, this.cfiBase = void 0; } } export { H as default }; //# sourceMappingURL=section.mjs.map