index.js
3.24 KB
"use strict";function e(e, t, n){return t in e?Object.defineProperty(e, t, { value:n, enumerable:!0, configurable:!0, writable:!0 }):e[t]=n, e;}/* !
* mp-html v2.4.2
* https://github.com/jin-yufeng/mp-html
*
* Released under the MIT license
* Author: Jin Yufeng
*/
const n=[]; const t=require("./parser");
Component({ data:{ nodes:[] }, properties:{ containerStyle:String, content:{ type:String, value:"", observer(e){this.setContent(e);} }, copyLink:{ type:Boolean, value:!0 }, domain:String, errorImg:String, lazyLoad:Boolean, loadingImg:String, pauseVideo:{ type:Boolean, value:!0 }, previewImg:{ type:Boolean, value:!0 }, scrollTable:Boolean, selectable:null, setTitle:{ type:Boolean, value:!0 }, showImgMenu:{ type:Boolean, value:!0 }, tagStyle:Object, useAnchor:null }, created(){this.plugins=[];for (let e=n.length;e--;) this.plugins.push(new n[e](this));}, detached(){this._hook("onDetached");}, methods:{ in(e, t, n){e&&t&&n&&(this._in={ page:e, selector:t, scrollTop:n });}, navigateTo(t, n){const i=this;return new Promise((o, r) =>{if (!i.data.useAnchor) return void r(new Error("Anchor is disabled"));const a=wx.createSelectorQuery().in(i._in?i._in.page:i).select((i._in?i._in.selector:"._root")+(t?"".concat(">>>", "#").concat(t):"")).boundingClientRect();i._in?a.select(i._in.selector).scrollOffset().select(i._in.selector).boundingClientRect():a.selectViewport().scrollOffset(), a.exec((t) =>{if (!t[0]) return void r(new Error("Label not found"));const a=t[1].scrollTop+t[0].top-(t[2]?t[2].top:0)+(n||Number.parseInt(i.data.useAnchor)||0);i._in?i._in.page.setData(e({}, i._in.scrollTop, a)):wx.pageScrollTo({ scrollTop:a, duration:300 }), o();});});}, getText(e){let t="";return function e(n){for (const o of n){if (o.type==="text")t+=o.text.replaceAll("&", "&");else if (o.name==="br")t+="\n";else {const r=o.name==="p"||o.name==="div"||o.name==="tr"||o.name==="li"||o.name[0]==="h"&&o.name[1]>"0"&&o.name[1]<"7";r&&t&&t[t.length-1]!=="\n"&&(t+="\n"), o.children&&e(o.children), r&&t[t.length-1]!=="\n"?t+="\n":o.name!=="td"&&o.name!=="th"||(t+="\t");}}}(e||this.data.nodes), t;}, getRect(){const e=this;return new Promise((t, n) =>{wx.createSelectorQuery().in(e).select("._root").boundingClientRect().exec((e) =>{return e[0]?t(e[0]):n(new Error("Root label not found"));});});}, pauseMedia(){for (let e=(this._videos||[]).length;e--;) this._videos[e].pause();}, setPlaybackRate(e){this.playbackRate=e;for (let t=(this._videos||[]).length;t--;) this._videos[t].playbackRate(e);}, setContent(e, n){const i=this;this.imgList&&n||(this.imgList=[]), this._videos=[];const o={}; const r=new t(this).parse(e);if (n) for (let a=this.data.nodes.length, s=r.length;s--;)o["nodes[".concat(a+s, "]")]=r[s];else o.nodes=r;if (this.setData(o, () =>{i._hook("onLoad"), i.triggerEvent("load");}), this.data.lazyLoad||this.imgList._unloadimgs<this.imgList.length/2){const c=function e(t){t&&t.height||(t={}), t.height===l?i.triggerEvent("ready", t):(l=t.height, setTimeout(() =>{i.getRect().then(e).catch(e);}, 350));}; let l=0;this.getRect().then(c).catch(c);} else this.imgList._unloadimgs||this.getRect().then((e) =>{i.triggerEvent("ready", e);}).catch(() =>{i.triggerEvent("ready", {});});}, _hook(e){for (let t=n.length;t--;) this.plugins[t][e]&&this.plugins[t][e]();}, _add(e){e.detail.root=this;} } });