¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view id="_root" :class="(selectable?'_select ':'')+'_root'"> |
| | | <slot v-if="!nodes[0]" /> |
| | | <!-- #ifndef APP-PLUS-NVUE --> |
| | | <node v-else :childs="nodes" :opts="[lazyLoad,loadingImg,errorImg,showImgMenu]" /> |
| | | <!-- #endif --> |
| | | <!-- #ifdef APP-PLUS-NVUE --> |
| | | <web-view ref="web" src="/static/app-plus/mp-html/local.html" :style="'margin-top:-2px;height:' + height + 'px'" @onPostMessage="_onMessage" /> |
| | | <!-- #endif --> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | import props from './props.js'; |
| | | /** |
| | | * mp-html v2.0.4 |
| | | * @description 坿æ¬ç»ä»¶ |
| | | * @tutorial https://github.com/jin-yufeng/mp-html |
| | | * @property {String} bgColor èæ¯é¢è²ï¼åªéç¨ä¸APP-PLUS-NVUE |
| | | * @property {String} content ç¨äºæ¸²æç坿æ¬å符串ï¼é»è®¤ true ï¼ |
| | | * @property {Boolean} copyLink æ¯å¦å
许å¤é¨é¾æ¥è¢«ç¹å»æ¶èªå¨å¤å¶ |
| | | * @property {String} domain 主ååï¼ç¨äºæ¼æ¥é¾æ¥ |
| | | * @property {String} errorImg å¾çåºéæ¶çå ä½å¾é¾æ¥ |
| | | * @property {Boolean} lazyLoad æ¯å¦å¼å¯å¾çæå è½½ï¼é»è®¤ true ï¼ |
| | | * @property {string} loadingImg å¾çå è½½è¿ç¨ä¸çå ä½å¾é¾æ¥ |
| | | * @property {Boolean} pauseVideo æ¯å¦å¨ææ¾ä¸ä¸ªè§é¢æ¶èªå¨æåå
¶å®è§é¢ï¼é»è®¤ true ï¼ |
| | | * @property {Boolean} previewImg æ¯å¦å
许å¾ç被ç¹å»æ¶èªå¨é¢è§ï¼é»è®¤ true ï¼ |
| | | * @property {Boolean} scrollTable æ¯å¦ç»æ¯ä¸ªè¡¨æ ¼æ·»å ä¸ä¸ªæ»å¨å±ä½¿å
¶è½åç¬æ¨ªåæ»å¨ |
| | | * @property {Boolean} selectable æ¯å¦å¼å¯é¿æå¤å¶ |
| | | * @property {Boolean} setTitle æ¯å¦å° title æ ç¾çå
容设置å°é¡µé¢æ é¢ï¼é»è®¤ true ï¼ |
| | | * @property {Boolean} showImgMenu æ¯å¦å
许å¾çè¢«é¿ææ¶æ¾ç¤ºèåï¼é»è®¤ true ï¼ |
| | | * @property {Object} tagStyle æ ç¾çé»è®¤æ ·å¼ |
| | | * @property {Boolean | Number} useAnchor æ¯å¦ä½¿ç¨éç¹é¾æ¥ |
| | | * |
| | | * @event {Function} load dom ç»æå è½½å®æ¯æ¶è§¦å |
| | | * @event {Function} ready ææå¾çå è½½å®æ¯æ¶è§¦å |
| | | * @event {Function} imgTap å¾ç被ç¹å»æ¶è§¦å |
| | | * @event {Function} linkTap 龿¥è¢«ç¹å»æ¶è§¦å |
| | | * @event {Function} error åªä½å è½½åºéæ¶è§¦å |
| | | */ |
| | | const plugins=[] |
| | | const parser = require('./parser') |
| | | // #ifndef APP-PLUS-NVUE |
| | | import node from './node/node' |
| | | // #endif |
| | | // #ifdef APP-PLUS-NVUE |
| | | const dom = weex.requireModule('dom') |
| | | // #endif |
| | | export default { |
| | | name: 'mp-html', |
| | | data() { |
| | | return { |
| | | nodes: [], |
| | | // #ifdef APP-PLUS-NVUE |
| | | height: 0 |
| | | // #endif |
| | | } |
| | | }, |
| | | mixins:[props], |
| | | // #ifndef APP-PLUS-NVUE |
| | | components: { |
| | | node |
| | | }, |
| | | // #endif |
| | | watch: { |
| | | content(content) { |
| | | this.setContent(content) |
| | | } |
| | | }, |
| | | created() { |
| | | this.plugins = [] |
| | | for (let i = plugins.length; i--;) |
| | | this.plugins.push(new plugins[i](this)) |
| | | }, |
| | | mounted() { |
| | | if (this.content && !this.nodes.length) |
| | | this.setContent(this.content) |
| | | }, |
| | | beforeDestroy() { |
| | | this._hook('onDetached') |
| | | clearInterval(this._timer) |
| | | }, |
| | | methods: { |
| | | /** |
| | | * @description å°éç¹è·³è½¬çèå´éå®å¨ä¸ä¸ª scroll-view å
|
| | | * @param {Object} page scroll-view æå¨é¡µé¢çç¤ºä¾ |
| | | * @param {String} selector scroll-view çéæ©å¨ |
| | | * @param {String} scrollTop scroll-view scroll-top 屿§ç»å®çåéå |
| | | */ |
| | | in(page, selector, scrollTop) { |
| | | // #ifndef APP-PLUS-NVUE |
| | | if (page && selector && scrollTop) |
| | | this._in = { |
| | | page, |
| | | selector, |
| | | scrollTop |
| | | } |
| | | // #endif |
| | | }, |
| | | |
| | | /** |
| | | * @description éç¹è·³è½¬ |
| | | * @param {String} id è¦è·³è½¬çéç¹ id |
| | | * @param {Number} offset 跳转ä½ç½®çåç§»é |
| | | * @returns {Promise} |
| | | */ |
| | | navigateTo(id, offset) { |
| | | return new Promise((resolve, reject) => { |
| | | if (!this.useAnchor) |
| | | return reject('Anchor is disabled') |
| | | offset = offset || parseInt(this.useAnchor) || 0 |
| | | // #ifdef APP-PLUS-NVUE |
| | | if (!id) { |
| | | dom.scrollToElement(this.$refs.web, { |
| | | offset |
| | | }) |
| | | resolve() |
| | | } else { |
| | | this._navigateTo = { |
| | | resolve, |
| | | reject, |
| | | offset |
| | | } |
| | | this.$refs.web.evalJs('uni.postMessage({data:{action:"getOffset",offset:(document.getElementById(' + id + ')||{}).offsetTop}})') |
| | | } |
| | | // #endif |
| | | // #ifndef APP-PLUS-NVUE |
| | | let deep = ' ' |
| | | // #ifdef MP-WEIXIN || MP-QQ || MP-TOUTIAO |
| | | deep = '>>>' |
| | | // #endif |
| | | const selector = uni.createSelectorQuery() |
| | | // #ifndef MP-ALIPAY |
| | | .in(this._in ? this._in.page : this) |
| | | // #endif |
| | | .select((this._in ? this._in.selector : '._root') + (id ? `${deep}#${id}` : '')).boundingClientRect() |
| | | if (this._in) |
| | | selector.select(this._in.selector).scrollOffset() |
| | | .select(this._in.selector).boundingClientRect() // è·å scroll-view çä½ç½®åæ»å¨è·ç¦» |
| | | else |
| | | selector.selectViewport().scrollOffset() // è·åçªå£çæ»å¨è·ç¦» |
| | | selector.exec(res => { |
| | | if (!res[0]) |
| | | return reject('Label not found') |
| | | const scrollTop = res[1].scrollTop + res[0].top - (res[2] ? res[2].top : 0) + offset |
| | | if (this._in) |
| | | // scroll-view 跳转 |
| | | this._in.page[this._in.scrollTop] = scrollTop |
| | | else |
| | | // 页é¢è·³è½¬ |
| | | uni.pageScrollTo({ |
| | | scrollTop, |
| | | duration: 300 |
| | | }) |
| | | resolve() |
| | | }) |
| | | // #endif |
| | | }) |
| | | }, |
| | | |
| | | /** |
| | | * @description è·åææ¬å
容 |
| | | * @return {String} |
| | | */ |
| | | getText() { |
| | | let text = ''; |
| | | (function traversal(nodes) { |
| | | for (let i = 0; i < nodes.length; i++) { |
| | | const node = nodes[i] |
| | | if (node.type == 'text') |
| | | text += node.text.replace(/&/g, '&') |
| | | else if (node.name == 'br') |
| | | text += '\n' |
| | | else { |
| | | // å级æ ç¾ååå æ¢è¡ |
| | | const isBlock = node.name == 'p' || node.name == 'div' || node.name == 'tr' || node.name == 'li' || (node.name[0] == 'h' && node.name[1] > '0' && node.name[1] < '7') |
| | | if (isBlock && text && text[text.length - 1] != '\n') |
| | | text += '\n' |
| | | // éå½è·ååèç¹çææ¬ |
| | | if (node.children) |
| | | traversal(node.children) |
| | | if (isBlock && text[text.length - 1] != '\n') |
| | | text += '\n' |
| | | else if (node.name == 'td' || node.name == 'th') |
| | | text += '\t' |
| | | } |
| | | } |
| | | })(this.nodes) |
| | | return text |
| | | }, |
| | | |
| | | /** |
| | | * @description è·åå
容大å°åä½ç½® |
| | | * @return {Promise} |
| | | */ |
| | | getRect() { |
| | | return new Promise((resolve, reject) => { |
| | | uni.createSelectorQuery() |
| | | // #ifndef MP-ALIPAY |
| | | .in(this) |
| | | // #endif |
| | | .select('#_root').boundingClientRect().exec(res => res[0] ? resolve(res[0]) : reject('Root label not found')) |
| | | }) |
| | | }, |
| | | |
| | | /** |
| | | * @description 设置å
容 |
| | | * @param {String} content html å
容 |
| | | * @param {Boolean} append æ¯å¦å¨å°¾é¨è¿½å |
| | | */ |
| | | setContent(content, append) { |
| | | if (!append || !this.imgList) |
| | | this.imgList = [] |
| | | const nodes = new parser(this).parse(content) |
| | | // #ifdef APP-PLUS-NVUE |
| | | if (this._ready) |
| | | this._set(nodes, append) |
| | | // #endif |
| | | this.$set(this, 'nodes', append ? (this.nodes || []).concat(nodes) : nodes) |
| | | |
| | | // #ifndef APP-PLUS-NVUE |
| | | this._videos = [] |
| | | this.$nextTick(() => { |
| | | this._hook('onLoad') |
| | | this.$emit('load') |
| | | }) |
| | | |
| | | // çå¾
å¾çå è½½å®æ¯ |
| | | let height |
| | | clearInterval(this._timer) |
| | | this._timer = setInterval(() => { |
| | | this.getRect().then(rect => { |
| | | // 350ms æ»é«åº¦æ åå就触å ready äºä»¶ |
| | | if (rect.height == height) { |
| | | this.$emit('ready', rect) |
| | | clearInterval(this._timer) |
| | | } |
| | | height = rect.height |
| | | }).catch(() => { }) |
| | | }, 350) |
| | | // #endif |
| | | }, |
| | | |
| | | /** |
| | | * @description è°ç¨æä»¶é©å彿° |
| | | */ |
| | | _hook(name) { |
| | | for (let i = plugins.length; i--;) |
| | | if (this.plugins[i][name]) |
| | | this.plugins[i][name]() |
| | | }, |
| | | |
| | | // #ifdef APP-PLUS-NVUE |
| | | /** |
| | | * @description 设置å
容 |
| | | */ |
| | | _set(nodes, append) { |
| | | this.$refs.web.evalJs('setContent(' + JSON.stringify(nodes) + ',' + JSON.stringify([this.bgColor, this.errorImg, this.loadingImg, this.pauseVideo, this.scrollTable, this.selectable]) + ',' + append + ')') |
| | | }, |
| | | |
| | | /** |
| | | * @description æ¥æ¶å° web-view æ¶æ¯ |
| | | */ |
| | | _onMessage(e) { |
| | | const message = e.detail.data[0] |
| | | switch (message.action) { |
| | | // web-view åå§å宿¯ |
| | | case 'onJSBridgeReady': |
| | | this._ready = true |
| | | if (this.nodes) |
| | | this._set(this.nodes) |
| | | break |
| | | // å
容 dom å è½½å®æ¯ |
| | | case 'onLoad': |
| | | this.height = message.height |
| | | this._hook('onLoad') |
| | | this.$emit('load') |
| | | break |
| | | // ææå¾çå è½½å®æ¯ |
| | | case 'onReady': |
| | | this.getRect().then(res => { |
| | | this.$emit('ready', res) |
| | | }).catch(() => { }) |
| | | break |
| | | // æ»é«åº¦åçåå |
| | | case 'onHeightChange': |
| | | this.height = message.height |
| | | break |
| | | // å¾çç¹å» |
| | | case 'onImgTap': |
| | | this.$emit('imgTap', message.attrs) |
| | | if (this.previewImg) |
| | | uni.previewImage({ |
| | | current: parseInt(message.attrs.i), |
| | | urls: this.imgList |
| | | }) |
| | | break |
| | | // 龿¥ç¹å» |
| | | case 'onLinkTap': |
| | | const href = message.attrs.href |
| | | this.$emit('linkTap', message.attrs) |
| | | if (href) { |
| | | // éç¹è·³è½¬ |
| | | if (href[0] == '#') { |
| | | if (this.useAnchor) |
| | | dom.scrollToElement(this.$refs.web, { |
| | | offset: message.offset |
| | | }) |
| | | } |
| | | // æå¼å¤é¾ |
| | | else if (href.includes('://')) { |
| | | if (this.copyLink) |
| | | plus.runtime.openWeb(href) |
| | | } |
| | | else |
| | | uni.navigateTo({ |
| | | url: href, |
| | | fail() { |
| | | wx.switchTab({ |
| | | url: href |
| | | }) |
| | | } |
| | | }) |
| | | } |
| | | break |
| | | // è·åå°éç¹çåç§»é |
| | | case 'getOffset': |
| | | if (typeof message.offset == 'number') { |
| | | dom.scrollToElement(this.$refs.web, { |
| | | offset: message.offset + this._navigateTo.offset |
| | | }) |
| | | this._navigateTo.resolve() |
| | | } else |
| | | this._navigateTo.reject('Label not found') |
| | | break |
| | | // ç¹å» |
| | | case 'onClick': |
| | | this.$emit('tap') |
| | | break |
| | | // åºé |
| | | case 'onError': |
| | | this.$emit('error', { |
| | | source: message.source, |
| | | attrs: message.attrs |
| | | }) |
| | | } |
| | | } |
| | | // #endif |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style> |
| | | /* #ifndef APP-PLUS-NVUE */ |
| | | /* æ ¹èç¹æ ·å¼ */ |
| | | ._root { |
| | | overflow: auto; |
| | | -webkit-overflow-scrolling: touch; |
| | | } |
| | | |
| | | /* é¿æå¤å¶ */ |
| | | ._select { |
| | | user-select: text; |
| | | } |
| | | /* #endif */ |
| | | </style> |