123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- <template>
- <view id="_root" :class="(selectable ? '_select ' : '') + '_root'" :style="containerStyle">
- <slot v-if="!nodes[0]" />
-
- <node v-else :childs="nodes" :opts="[lazyLoad, loadingImg, errorImg, showImgMenu, selectable]" name="span" />
-
-
- <web-view ref="web" src="/static/app-plus/mp-html/local.html" :style="'margin-top:-2px;height:' + height + 'px'" @onPostMessage="_onMessage" />
-
- </view>
- </template>
- <script>
- import node from './node/node'
- import Parser from './parser'
- const plugins = []
- const dom = weex.requireModule('dom')
- export default {
- name: 'u-parse',
- data() {
- return {
- nodes: [],
-
- height: 3
-
- }
- },
- props: {
- containerStyle: {
- type: String,
- default: ''
- },
- content: {
- type: String,
- default: ''
- },
- copyLink: {
- type: [Boolean, String],
- default: true
- },
- domain: String,
- errorImg: {
- type: String,
- default: ''
- },
- lazyLoad: {
- type: [Boolean, String],
- default: false
- },
- loadingImg: {
- type: String,
- default: ''
- },
- pauseVideo: {
- type: [Boolean, String],
- default: true
- },
- previewImg: {
- type: [Boolean, String],
- default: true
- },
- scrollTable: [Boolean, String],
- selectable: [Boolean, String],
- setTitle: {
- type: [Boolean, String],
- default: true
- },
- showImgMenu: {
- type: [Boolean, String],
- default: true
- },
- tagStyle: Object,
- useAnchor: [Boolean, Number]
- },
-
- emits: ['load', 'ready', 'imgTap', 'linkTap', 'play', 'error'],
-
-
- components: {
- node
- },
-
- 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)
- }
- },
- beforeUnmount() {
- this._hook('onDetached')
- },
- methods: {
-
- in(page, selector, scrollTop) {
-
- if (page && selector && scrollTop) {
- this._in = {
- page,
- selector,
- scrollTop
- }
- }
-
- },
-
- navigateTo(id, offset) {
- return new Promise((resolve, reject) => {
- if (!this.useAnchor) {
- reject(Error('Anchor is disabled'))
- return
- }
- offset = offset || parseInt(this.useAnchor) || 0
-
- 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}})')
- }
-
-
- let deep = ' '
-
- deep = '>>>'
-
- const selector = uni.createSelectorQuery()
-
- .in(this._in ? this._in.page : this)
-
- .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()
- } else {
-
- selector.selectViewport().scrollOffset()
- }
- selector.exec(res => {
- if (!res[0]) {
- reject(Error('Label not found'))
- return
- }
- const scrollTop = res[1].scrollTop + res[0].top - (res[2] ? res[2].top : 0) + offset
- if (this._in) {
-
- this._in.page[this._in.scrollTop] = scrollTop
- } else {
-
- uni.pageScrollTo({
- scrollTop,
- duration: 300
- })
- }
- resolve()
- })
-
- })
- },
-
- getText(nodes) {
- 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'
- }
- }
- }
- })(nodes || this.nodes)
- return text
- },
-
- getRect() {
- return new Promise((resolve, reject) => {
- uni.createSelectorQuery()
-
- .in(this)
-
- .select('#_root').boundingClientRect().exec(res => res[0] ? resolve(res[0]) : reject(Error('Root label not found')))
- })
- },
-
- pauseMedia() {
- for (let i = (this._videos || []).length; i--;) {
- this._videos[i].pause()
- }
-
- const command = 'for(var e=document.getElementsByTagName("video"),i=e.length;i--;)e[i].pause()'
-
- let page = this.$parent
- while (!page.$scope) page = page.$parent
- page.$scope.$getAppWebview().evalJS(command)
-
-
- this.$refs.web.evalJs(command)
-
-
- },
-
- setPlaybackRate(rate) {
- this.playbackRate = rate
- for (let i = (this._videos || []).length; i--;) {
- this._videos[i].playbackRate(rate)
- }
-
- const command = 'for(var e=document.getElementsByTagName("video"),i=e.length;i--;)e[i].playbackRate=' + rate
-
- let page = this.$parent
- while (!page.$scope) page = page.$parent
- page.$scope.$getAppWebview().evalJS(command)
-
-
- this.$refs.web.evalJs(command)
-
-
- },
-
- setContent(content, append) {
- if (!append || !this.imgList) {
- this.imgList = []
- }
- const nodes = new Parser(this).parse(content)
-
- if (this._ready) {
- this._set(nodes, append)
- }
-
- this.$set(this, 'nodes', append ? (this.nodes || []).concat(nodes) : nodes)
- // #ifndef APP-PLUS-NVUE
- this._videos = []
- this.$nextTick(() => {
- this._hook('onLoad')
- this.$emit('load')
- })
- if (this.lazyLoad || this.imgList._unloadimgs < this.imgList.length / 2) {
-
- let height = 0
- const callback = rect => {
- if (!rect || !rect.height) rect = {}
-
- if (rect.height === height) {
- this.$emit('ready', rect)
- } else {
- height = rect.height
- setTimeout(() => {
- this.getRect().then(callback).catch(callback)
- }, 350)
- }
- }
- this.getRect().then(callback).catch(callback)
- } else {
-
- if (!this.imgList._unloadimgs) {
- this.getRect().then(rect => {
- this.$emit('ready', rect)
- }).catch(() => {
- this.$emit('ready', {})
- })
- }
- }
-
- },
-
- _hook(name) {
- for (let i = plugins.length; i--;) {
- if (this.plugins[i][name]) {
- this.plugins[i][name]()
- }
- }
- },
-
-
- _set(nodes, append) {
- this.$refs.web.evalJs('setContent(' + JSON.stringify(nodes).replace(/%22/g, '') + ',' + JSON.stringify([this.containerStyle.replace(/(?:margin|padding)[^;]+/g, ''), this.errorImg, this.loadingImg, this.pauseVideo, this.scrollTable, this.selectable]) + ',' + append + ')')
- },
-
- _onMessage(e) {
- const message = e.detail.data[0]
- switch (message.action) {
-
- case 'onJSBridgeReady':
- this._ready = true
- if (this.nodes) {
- this._set(this.nodes)
- }
- break
-
- case 'onLoad':
- this.height = message.height
- this._hook('onLoad')
- this.$emit('load')
- break
-
- case 'onReady':
- this.getRect().then(res => {
- this.$emit('ready', res)
- }).catch(() => {
- this.$emit('ready', {})
- })
- 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() {
- uni.switchTab({
- url: href
- })
- }
- })
- }
- }
- break
- }
- case 'onPlay':
- this.$emit('play')
- 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(Error('Label not found'))
- }
- break
-
- case 'onClick':
- this.$emit('tap')
- this.$emit('click')
- break
-
- case 'onError':
- this.$emit('error', {
- source: message.source,
- attrs: message.attrs
- })
- }
- }
-
- }
- }
- </script>
- <style>
- ._root {
- padding: 1px 0;
- overflow-x: auto;
- overflow-y: hidden;
- -webkit-overflow-scrolling: touch;
- }
- ._select {
- user-select: text;
- }
- </style>
|