guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame^] | 1 | <template> |
| 2 | <view> |
| 3 | <slot v-if="!nodes.length" /> |
| 4 | <!--#ifdef APP-PLUS-NVUE--> |
| 5 | <web-view id="_top" ref="web" :style="'margin-top:-2px;height:'+height+'px'" @onPostMessage="_message" /> |
| 6 | <!--#endif--> |
| 7 | <!--#ifndef APP-PLUS-NVUE--> |
| 8 | <view id="_top" :style="showAm+(selectable?';user-select:text;-webkit-user-select:text':'')"> |
| 9 | <!--#ifdef H5 || MP-360--> |
| 10 | <div :id="'rtf'+uid"></div> |
| 11 | <!--#endif--> |
| 12 | <!--#ifndef H5 || MP-360--> |
| 13 | <trees :nodes="nodes" :lazyLoad="lazyLoad" :loading="loadingImg" /> |
| 14 | <!--#endif--> |
| 15 | </view> |
| 16 | <!--#endif--> |
| 17 | </view> |
| 18 | </template> |
| 19 | |
| 20 | <script> |
| 21 | var search; |
| 22 | // #ifndef H5 || APP-PLUS-NVUE || MP-360 |
| 23 | import trees from './libs/trees'; |
| 24 | var cache = {}, |
| 25 | // #ifdef MP-WEIXIN || MP-TOUTIAO |
| 26 | fs = uni.getFileSystemManager ? uni.getFileSystemManager() : null, |
| 27 | // #endif |
| 28 | Parser = require('./libs/MpHtmlParser.js'); |
| 29 | var dom; |
| 30 | // 计算 cache 的 key |
| 31 | function hash(str) { |
| 32 | for (var i = str.length, val = 5381; i--;) |
| 33 | val += (val << 5) + str.charCodeAt(i); |
| 34 | return val; |
| 35 | } |
| 36 | // #endif |
| 37 | // #ifdef H5 || APP-PLUS-NVUE || MP-360 |
| 38 | var { |
| 39 | windowWidth, |
| 40 | platform |
| 41 | } = uni.getSystemInfoSync(), |
| 42 | cfg = require('./libs/config.js'); |
| 43 | // #endif |
| 44 | // #ifdef APP-PLUS-NVUE |
| 45 | var weexDom = weex.requireModule('dom'); |
| 46 | // #endif |
| 47 | /** |
| 48 | * Parser 富文本组件 |
| 49 | * @tutorial https://github.com/jin-yufeng/Parser |
| 50 | * @property {String} html 富文本数据 |
| 51 | * @property {Boolean} autopause 是否在播放一个视频时自动暂停其他视频 |
| 52 | * @property {Boolean} autoscroll 是否自动给所有表格添加一个滚动层 |
| 53 | * @property {Boolean} autosetTitle 是否自动将 title 标签中的内容设置到页面标题 |
| 54 | * @property {Number} compress 压缩等级 |
| 55 | * @property {String} domain 图片、视频等链接的主域名 |
| 56 | * @property {Boolean} lazyLoad 是否开启图片懒加载 |
| 57 | * @property {String} loadingImg 图片加载完成前的占位图 |
| 58 | * @property {Boolean} selectable 是否开启长按复制 |
| 59 | * @property {Object} tagStyle 标签的默认样式 |
| 60 | * @property {Boolean} showWithAnimation 是否使用渐显动画 |
| 61 | * @property {Boolean} useAnchor 是否使用锚点 |
| 62 | * @property {Boolean} useCache 是否缓存解析结果 |
| 63 | * @event {Function} parse 解析完成事件 |
| 64 | * @event {Function} load dom 加载完成事件 |
| 65 | * @event {Function} ready 所有图片加载完毕事件 |
| 66 | * @event {Function} error 错误事件 |
| 67 | * @event {Function} imgtap 图片点击事件 |
| 68 | * @event {Function} linkpress 链接点击事件 |
| 69 | * @author JinYufeng |
| 70 | * @version 20201029 |
| 71 | * @listens MIT |
| 72 | */ |
| 73 | export default { |
| 74 | name: 'parser', |
| 75 | data() { |
| 76 | return { |
| 77 | // #ifdef H5 || MP-360 |
| 78 | uid: this._uid, |
| 79 | // #endif |
| 80 | // #ifdef APP-PLUS-NVUE |
| 81 | height: 1, |
| 82 | // #endif |
| 83 | // #ifndef APP-PLUS-NVUE |
| 84 | showAm: '', |
| 85 | // #endif |
| 86 | nodes: [] |
| 87 | } |
| 88 | }, |
| 89 | // #ifndef H5 || APP-PLUS-NVUE || MP-360 |
| 90 | components: { |
| 91 | trees |
| 92 | }, |
| 93 | // #endif |
| 94 | props: { |
| 95 | html: String, |
| 96 | autopause: { |
| 97 | type: Boolean, |
| 98 | default: true |
| 99 | }, |
| 100 | autoscroll: Boolean, |
| 101 | autosetTitle: { |
| 102 | type: Boolean, |
| 103 | default: true |
| 104 | }, |
| 105 | // #ifndef H5 || APP-PLUS-NVUE || MP-360 |
| 106 | compress: Number, |
| 107 | loadingImg: String, |
| 108 | useCache: Boolean, |
| 109 | // #endif |
| 110 | domain: String, |
| 111 | lazyLoad: Boolean, |
| 112 | selectable: Boolean, |
| 113 | tagStyle: Object, |
| 114 | showWithAnimation: Boolean, |
| 115 | useAnchor: Boolean |
| 116 | }, |
| 117 | watch: { |
| 118 | html(html) { |
| 119 | this.setContent(html); |
| 120 | } |
| 121 | }, |
| 122 | created() { |
| 123 | // 图片数组 |
| 124 | this.imgList = []; |
| 125 | this.imgList.each = function(f) { |
| 126 | for (var i = 0, len = this.length; i < len; i++) |
| 127 | this.setItem(i, f(this[i], i, this)); |
| 128 | } |
| 129 | this.imgList.setItem = function(i, src) { |
| 130 | if (i == void 0 || !src) return; |
| 131 | // #ifndef MP-ALIPAY || APP-PLUS |
| 132 | // 去重 |
| 133 | if (src.indexOf('http') == 0 && this.includes(src)) { |
| 134 | var newSrc = src.split('://')[0]; |
| 135 | for (var j = newSrc.length, c; c = src[j]; j++) { |
| 136 | if (c == '/' && src[j - 1] != '/' && src[j + 1] != '/') break; |
| 137 | newSrc += Math.random() > 0.5 ? c.toUpperCase() : c; |
| 138 | } |
| 139 | newSrc += src.substr(j); |
| 140 | return this[i] = newSrc; |
| 141 | } |
| 142 | // #endif |
| 143 | this[i] = src; |
| 144 | // 暂存 data src |
| 145 | if (src.includes('data:image')) { |
| 146 | var filePath, info = src.match(/data:image\/(\S+?);(\S+?),(.+)/); |
| 147 | if (!info) return; |
| 148 | // #ifdef MP-WEIXIN || MP-TOUTIAO |
| 149 | filePath = `${wx.env.USER_DATA_PATH}/${Date.now()}.${info[1]}`; |
| 150 | fs && fs.writeFile({ |
| 151 | filePath, |
| 152 | data: info[3], |
| 153 | encoding: info[2], |
| 154 | success: () => this[i] = filePath |
| 155 | }) |
| 156 | // #endif |
| 157 | // #ifdef APP-PLUS |
| 158 | filePath = `_doc/parser_tmp/${Date.now()}.${info[1]}`; |
| 159 | var bitmap = new plus.nativeObj.Bitmap(); |
| 160 | bitmap.loadBase64Data(src, () => { |
| 161 | bitmap.save(filePath, {}, () => { |
| 162 | bitmap.clear() |
| 163 | this[i] = filePath; |
| 164 | }) |
| 165 | }) |
| 166 | // #endif |
| 167 | } |
| 168 | } |
| 169 | }, |
| 170 | mounted() { |
| 171 | // #ifdef H5 || MP-360 |
| 172 | this.document = document.getElementById('rtf' + this._uid); |
| 173 | // #endif |
| 174 | // #ifndef H5 || APP-PLUS-NVUE || MP-360 |
| 175 | if (dom) this.document = new dom(this); |
| 176 | // #endif |
| 177 | if (search) this.search = args => search(this, args); |
| 178 | // #ifdef APP-PLUS-NVUE |
| 179 | this.document = this.$refs.web; |
| 180 | setTimeout(() => { |
| 181 | // #endif |
| 182 | if (this.html) this.setContent(this.html); |
| 183 | // #ifdef APP-PLUS-NVUE |
| 184 | }, 30) |
| 185 | // #endif |
| 186 | }, |
| 187 | beforeDestroy() { |
| 188 | // #ifdef H5 || MP-360 |
| 189 | if (this._observer) this._observer.disconnect(); |
| 190 | // #endif |
| 191 | this.imgList.each(src => { |
| 192 | // #ifdef APP-PLUS |
| 193 | if (src && src.includes('_doc')) { |
| 194 | plus.io.resolveLocalFileSystemURL(src, entry => { |
| 195 | entry.remove(); |
| 196 | }); |
| 197 | } |
| 198 | // #endif |
| 199 | // #ifdef MP-WEIXIN || MP-TOUTIAO |
| 200 | if (src && src.includes(uni.env.USER_DATA_PATH)) |
| 201 | fs && fs.unlink({ |
| 202 | filePath: src |
| 203 | }) |
| 204 | // #endif |
| 205 | }) |
| 206 | clearInterval(this._timer); |
| 207 | }, |
| 208 | methods: { |
| 209 | // 设置富文本内容 |
| 210 | setContent(html, append) { |
| 211 | // #ifdef APP-PLUS-NVUE |
| 212 | if (!html) |
| 213 | return this.height = 1; |
| 214 | if (append) |
| 215 | this.$refs.web.evalJs("var b=document.createElement('div');b.innerHTML='" + html.replace(/'/g, "\\'") + |
| 216 | "';document.getElementById('parser').appendChild(b)"); |
| 217 | else { |
| 218 | html = |
| 219 | '<meta charset="utf-8" /><meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"><style>html,body{width:100%;height:100%;overflow:hidden}body{margin:0}</style><base href="' + |
| 220 | this.domain + '"><div id="parser"' + (this.selectable ? '>' : ' style="user-select:none">') + this._handleHtml(html).replace(/\n/g, '\\n') + |
| 221 | '</div><script>"use strict";function e(e){if(window.__dcloud_weex_postMessage||window.__dcloud_weex_){var t={data:[e]};window.__dcloud_weex_postMessage?window.__dcloud_weex_postMessage(t):window.__dcloud_weex_.postMessage(JSON.stringify(t))}}document.body.onclick=function(){e({action:"click"})},' + |
| 222 | (this.showWithAnimation ? 'document.body.style.animation="_show .5s",' : '') + |
| 223 | 'setTimeout(function(){e({action:"load",text:document.body.innerText,height:document.getElementById("parser").scrollHeight})},50);\x3c/script>'; |
| 224 | if (platform == 'android') html = html.replace(/%/g, '%25'); |
| 225 | this.$refs.web.evalJs("document.write('" + html.replace(/'/g, "\\'") + "');document.close()"); |
| 226 | } |
| 227 | this.$refs.web.evalJs( |
| 228 | 'var t=document.getElementsByTagName("title");t.length&&e({action:"getTitle",title:t[0].innerText});for(var o,n=document.getElementsByTagName("style"),r=1;o=n[r++];)o.innerHTML=o.innerHTML.replace(/body/g,"#parser");for(var a,c=document.getElementsByTagName("img"),s=[],i=0==c.length,d=0,l=0,g=0;a=c[l];l++)parseInt(a.style.width||a.getAttribute("width"))>' + |
| 229 | windowWidth + '&&(a.style.height="auto"),a.onload=function(){++d==c.length&&(i=!0)},a.onerror=function(){++d==c.length&&(i=!0),' + (cfg.errorImg ? 'this.src="' + cfg.errorImg + '",' : '') + |
| 230 | 'e({action:"error",source:"img",target:this})},a.hasAttribute("ignore")||"A"==a.parentElement.nodeName||(a.i=g++,s.push(a.getAttribute("original-src")||a.src||a.getAttribute("data-src")),a.onclick=function(t){t.stopPropagation(),e({action:"preview",img:{i:this.i,src:this.src}})});e({action:"getImgList",imgList:s});for(var u,m=document.getElementsByTagName("a"),f=0;u=m[f];f++)u.onclick=function(m){m.stopPropagation();var t,o=this.getAttribute("href");if("#"==o[0]){var n=document.getElementById(o.substr(1));n&&(t=n.offsetTop)}return e({action:"linkpress",href:o,offset:t}),!1};for(var h,y=document.getElementsByTagName("video"),v=0;h=y[v];v++)h.style.maxWidth="100%",h.onerror=function(){e({action:"error",source:"video",target:this})}' + |
| 231 | (this.autopause ? ',h.onplay=function(){for(var e,t=0;e=y[t];t++)e!=this&&e.pause()}' : '') + |
| 232 | ';for(var _,p=document.getElementsByTagName("audio"),w=0;_=p[w];w++)_.onerror=function(){e({action:"error",source:"audio",target:this})};' + |
| 233 | (this.autoscroll ? 'for(var T,E=document.getElementsByTagName("table"),B=0;T=E[B];B++){var N=document.createElement("div");N.style.overflow="scroll",T.parentNode.replaceChild(N,T),N.appendChild(T)}' : '') + |
| 234 | 'var x=document.getElementById("parser");clearInterval(window.timer),window.timer=setInterval(function(){i&&clearInterval(window.timer),e({action:"ready",ready:i,height:x.scrollHeight})},350)' |
| 235 | ) |
| 236 | this.nodes = [1]; |
| 237 | // #endif |
| 238 | // #ifdef H5 || MP-360 |
| 239 | if (!html) { |
| 240 | if (this.rtf && !append) this.rtf.parentNode.removeChild(this.rtf); |
| 241 | return; |
| 242 | } |
| 243 | var div = document.createElement('div'); |
| 244 | if (!append) { |
| 245 | if (this.rtf) this.rtf.parentNode.removeChild(this.rtf); |
| 246 | this.rtf = div; |
| 247 | } else { |
| 248 | if (!this.rtf) this.rtf = div; |
| 249 | else this.rtf.appendChild(div); |
| 250 | } |
| 251 | div.innerHTML = this._handleHtml(html, append); |
| 252 | for (var styles = this.rtf.getElementsByTagName('style'), i = 0, style; style = styles[i++];) { |
| 253 | style.innerHTML = style.innerHTML.replace(/body/g, '#rtf' + this._uid); |
| 254 | style.setAttribute('scoped', 'true'); |
| 255 | } |
| 256 | // 懒加载 |
| 257 | if (!this._observer && this.lazyLoad && IntersectionObserver) { |
| 258 | this._observer = new IntersectionObserver(changes => { |
| 259 | for (let item, i = 0; item = changes[i++];) { |
| 260 | if (item.isIntersecting) { |
| 261 | item.target.src = item.target.getAttribute('data-src'); |
| 262 | item.target.removeAttribute('data-src'); |
| 263 | this._observer.unobserve(item.target); |
| 264 | } |
| 265 | } |
| 266 | }, { |
| 267 | rootMargin: '500px 0px 500px 0px' |
| 268 | }) |
| 269 | } |
| 270 | var _ts = this; |
| 271 | // 获取标题 |
| 272 | var title = this.rtf.getElementsByTagName('title'); |
| 273 | if (title.length && this.autosetTitle) |
| 274 | uni.setNavigationBarTitle({ |
| 275 | title: title[0].innerText |
| 276 | }) |
| 277 | // 填充 domain |
| 278 | var fill = target => { |
| 279 | var src = target.getAttribute('src'); |
| 280 | if (this.domain && src) { |
| 281 | if (src[0] == '/') { |
| 282 | if (src[1] == '/') |
| 283 | target.src = (this.domain.includes('://') ? this.domain.split('://')[0] : '') + ':' + src; |
| 284 | else target.src = this.domain + src; |
| 285 | } else if (!src.includes('://') && src.indexOf('data:') != 0) target.src = this.domain + '/' + src; |
| 286 | } |
| 287 | } |
| 288 | // 图片处理 |
| 289 | this.imgList.length = 0; |
| 290 | var imgs = this.rtf.getElementsByTagName('img'); |
| 291 | for (let i = 0, j = 0, img; img = imgs[i]; i++) { |
| 292 | if (parseInt(img.style.width || img.getAttribute('width')) > windowWidth) |
| 293 | img.style.height = 'auto'; |
| 294 | fill(img); |
| 295 | if (!img.hasAttribute('ignore') && img.parentElement.nodeName != 'A') { |
| 296 | img.i = j++; |
| 297 | _ts.imgList.push(img.getAttribute('original-src') || img.src || img.getAttribute('data-src')); |
| 298 | img.onclick = function(e) { |
| 299 | e.stopPropagation(); |
| 300 | var preview = true; |
| 301 | this.ignore = () => preview = false; |
| 302 | _ts.$emit('imgtap', this); |
| 303 | if (preview) { |
| 304 | uni.previewImage({ |
| 305 | current: this.i, |
| 306 | urls: _ts.imgList |
| 307 | }); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | img.onerror = function() { |
| 312 | if (cfg.errorImg) |
| 313 | _ts.imgList[this.i] = this.src = cfg.errorImg; |
| 314 | _ts.$emit('error', { |
| 315 | source: 'img', |
| 316 | target: this |
| 317 | }); |
| 318 | } |
| 319 | if (_ts.lazyLoad && this._observer && img.src && img.i != 0) { |
| 320 | img.setAttribute('data-src', img.src); |
| 321 | img.removeAttribute('src'); |
| 322 | this._observer.observe(img); |
| 323 | } |
| 324 | } |
| 325 | // 链接处理 |
| 326 | var links = this.rtf.getElementsByTagName('a'); |
| 327 | for (var link of links) { |
| 328 | link.onclick = function(e) { |
| 329 | e.stopPropagation(); |
| 330 | var jump = true, |
| 331 | href = this.getAttribute('href'); |
| 332 | _ts.$emit('linkpress', { |
| 333 | href, |
| 334 | ignore: () => jump = false |
| 335 | }); |
| 336 | if (jump && href) { |
| 337 | if (href[0] == '#') { |
| 338 | if (_ts.useAnchor) { |
| 339 | _ts.navigateTo({ |
| 340 | id: href.substr(1) |
| 341 | }) |
| 342 | } |
| 343 | } else if (href.indexOf('http') == 0 || href.indexOf('//') == 0) |
| 344 | return true; |
| 345 | else |
| 346 | uni.navigateTo({ |
| 347 | url: href |
| 348 | }) |
| 349 | } |
| 350 | return false; |
| 351 | } |
| 352 | } |
| 353 | // 视频处理 |
| 354 | var videos = this.rtf.getElementsByTagName('video'); |
| 355 | _ts.videoContexts = videos; |
| 356 | for (let video, i = 0; video = videos[i++];) { |
| 357 | fill(video); |
| 358 | video.style.maxWidth = '100%'; |
| 359 | video.onerror = function() { |
| 360 | _ts.$emit('error', { |
| 361 | source: 'video', |
| 362 | target: this |
| 363 | }); |
| 364 | } |
| 365 | video.onplay = function() { |
| 366 | if (_ts.autopause) |
| 367 | for (let item, i = 0; item = _ts.videoContexts[i++];) |
| 368 | if (item != this) item.pause(); |
| 369 | } |
| 370 | } |
| 371 | // 音频处理 |
| 372 | var audios = this.rtf.getElementsByTagName('audio'); |
| 373 | for (var audio of audios) { |
| 374 | fill(audio); |
| 375 | audio.onerror = function() { |
| 376 | _ts.$emit('error', { |
| 377 | source: 'audio', |
| 378 | target: this |
| 379 | }); |
| 380 | } |
| 381 | } |
| 382 | // 表格处理 |
| 383 | if (this.autoscroll) { |
| 384 | var tables = this.rtf.getElementsByTagName('table'); |
| 385 | for (var table of tables) { |
| 386 | let div = document.createElement('div'); |
| 387 | div.style.overflow = 'scroll'; |
| 388 | table.parentNode.replaceChild(div, table); |
| 389 | div.appendChild(table); |
| 390 | } |
| 391 | } |
| 392 | if (!append) this.document.appendChild(this.rtf); |
| 393 | this.$nextTick(() => { |
| 394 | this.nodes = [1]; |
| 395 | this.$emit('load'); |
| 396 | }); |
| 397 | setTimeout(() => this.showAm = '', 500); |
| 398 | // #endif |
| 399 | // #ifndef APP-PLUS-NVUE |
| 400 | // #ifndef H5 || MP-360 |
| 401 | var nodes; |
| 402 | if (!html) return this.nodes = []; |
| 403 | var parser = new Parser(html, this); |
| 404 | // 缓存读取 |
| 405 | if (this.useCache) { |
| 406 | var hashVal = hash(html); |
| 407 | if (cache[hashVal]) |
| 408 | nodes = cache[hashVal]; |
| 409 | else { |
| 410 | nodes = parser.parse(); |
| 411 | cache[hashVal] = nodes; |
| 412 | } |
| 413 | } else nodes = parser.parse(); |
| 414 | this.$emit('parse', nodes); |
| 415 | if (append) this.nodes = this.nodes.concat(nodes); |
| 416 | else this.nodes = nodes; |
| 417 | if (nodes.length && nodes.title && this.autosetTitle) |
| 418 | uni.setNavigationBarTitle({ |
| 419 | title: nodes.title |
| 420 | }) |
| 421 | if (this.imgList) this.imgList.length = 0; |
| 422 | this.videoContexts = []; |
| 423 | this.$nextTick(() => { |
| 424 | (function f(cs) { |
| 425 | for (var i = cs.length; i--;) { |
| 426 | if (cs[i].top) { |
| 427 | cs[i].controls = []; |
| 428 | cs[i].init(); |
| 429 | f(cs[i].$children); |
| 430 | } |
| 431 | } |
| 432 | })(this.$children) |
| 433 | this.$emit('load'); |
| 434 | }) |
| 435 | // #endif |
| 436 | var height; |
| 437 | clearInterval(this._timer); |
| 438 | this._timer = setInterval(() => { |
| 439 | // #ifdef H5 || MP-360 |
| 440 | this.rect = this.rtf.getBoundingClientRect(); |
| 441 | // #endif |
| 442 | // #ifndef H5 || MP-360 |
| 443 | uni.createSelectorQuery().in(this) |
| 444 | .select('#_top').boundingClientRect().exec(res => { |
| 445 | if (!res) return; |
| 446 | this.rect = res[0]; |
| 447 | // #endif |
| 448 | if (this.rect.height == height) { |
| 449 | this.$emit('ready', this.rect) |
| 450 | clearInterval(this._timer); |
| 451 | } |
| 452 | height = this.rect.height; |
| 453 | // #ifndef H5 || MP-360 |
| 454 | }); |
| 455 | // #endif |
| 456 | }, 350); |
| 457 | if (this.showWithAnimation && !append) this.showAm = 'animation:_show .5s'; |
| 458 | // #endif |
| 459 | }, |
| 460 | // 获取文本内容 |
| 461 | getText(ns = this.nodes) { |
| 462 | var txt = ''; |
| 463 | // #ifdef APP-PLUS-NVUE |
| 464 | txt = this._text; |
| 465 | // #endif |
| 466 | // #ifdef H5 || MP-360 |
| 467 | txt = this.rtf.innerText; |
| 468 | // #endif |
| 469 | // #ifndef H5 || APP-PLUS-NVUE || MP-360 |
| 470 | for (var i = 0, n; n = ns[i++];) { |
| 471 | if (n.type == 'text') txt += n.text.replace(/ /g, '\u00A0').replace(/</g, '<').replace(/>/g, '>') |
| 472 | .replace(/&/g, '&'); |
| 473 | else if (n.type == 'br') txt += '\n'; |
| 474 | else { |
| 475 | // 块级标签前后加换行 |
| 476 | var block = n.name == 'p' || n.name == 'div' || n.name == 'tr' || n.name == 'li' || (n.name[0] == 'h' && n.name[1] > |
| 477 | '0' && n.name[1] < '7'); |
| 478 | if (block && txt && txt[txt.length - 1] != '\n') txt += '\n'; |
| 479 | if (n.children) txt += this.getText(n.children); |
| 480 | if (block && txt[txt.length - 1] != '\n') txt += '\n'; |
| 481 | else if (n.name == 'td' || n.name == 'th') txt += '\t'; |
| 482 | } |
| 483 | } |
| 484 | // #endif |
| 485 | return txt; |
| 486 | }, |
| 487 | // 锚点跳转 |
| 488 | in (obj) { |
| 489 | if (obj.page && obj.selector && obj.scrollTop) this._in = obj; |
| 490 | }, |
| 491 | navigateTo(obj) { |
| 492 | if (!this.useAnchor) return obj.fail && obj.fail('Anchor is disabled'); |
| 493 | // #ifdef APP-PLUS-NVUE |
| 494 | if (!obj.id) |
| 495 | weexDom.scrollToElement(this.$refs.web); |
| 496 | else |
| 497 | this.$refs.web.evalJs('var pos=document.getElementById("' + obj.id + |
| 498 | '");if(pos)post({action:"linkpress",href:"#",offset:pos.offsetTop+' + (obj.offset || 0) + '})'); |
| 499 | obj.success && obj.success(); |
| 500 | // #endif |
| 501 | // #ifndef APP-PLUS-NVUE |
| 502 | var d = ' '; |
| 503 | // #ifdef MP-WEIXIN || MP-QQ || MP-TOUTIAO |
| 504 | d = '>>>'; |
| 505 | // #endif |
| 506 | var selector = uni.createSelectorQuery().in(this._in ? this._in.page : this).select((this._in ? this._in.selector : |
| 507 | '#_top') + (obj.id ? `${d}#${obj.id},${this._in?this._in.selector:'#_top'}${d}.${obj.id}` : '')).boundingClientRect(); |
| 508 | if (this._in) selector.select(this._in.selector).scrollOffset().select(this._in.selector).boundingClientRect(); |
| 509 | else selector.selectViewport().scrollOffset(); |
| 510 | selector.exec(res => { |
| 511 | if (!res[0]) return obj.fail && obj.fail('Label not found') |
| 512 | var scrollTop = res[1].scrollTop + res[0].top - (res[2] ? res[2].top : 0) + (obj.offset || 0); |
| 513 | if (this._in) this._in.page[this._in.scrollTop] = scrollTop; |
| 514 | else uni.pageScrollTo({ |
| 515 | scrollTop, |
| 516 | duration: 300 |
| 517 | }) |
| 518 | obj.success && obj.success(); |
| 519 | }) |
| 520 | // #endif |
| 521 | }, |
| 522 | // 获取视频对象 |
| 523 | getVideoContext(id) { |
| 524 | // #ifndef APP-PLUS-NVUE |
| 525 | if (!id) return this.videoContexts; |
| 526 | else |
| 527 | for (var i = this.videoContexts.length; i--;) |
| 528 | if (this.videoContexts[i].id == id) return this.videoContexts[i]; |
| 529 | // #endif |
| 530 | }, |
| 531 | // #ifdef H5 || APP-PLUS-NVUE || MP-360 |
| 532 | _handleHtml(html, append) { |
| 533 | if (!append) { |
| 534 | // 处理 tag-style 和 userAgentStyles |
| 535 | var style = '<style>@keyframes _show{0%{opacity:0}100%{opacity:1}}img{max-width:100%}'; |
| 536 | for (var item in cfg.userAgentStyles) |
| 537 | style += `${item}{${cfg.userAgentStyles[item]}}`; |
| 538 | for (item in this.tagStyle) |
| 539 | style += `${item}{${this.tagStyle[item]}}`; |
| 540 | style += '</style>'; |
| 541 | html = style + html; |
| 542 | } |
| 543 | // 处理 rpx |
| 544 | if (html.includes('rpx')) |
| 545 | html = html.replace(/[0-9.]+\s*rpx/g, $ => (parseFloat($) * windowWidth / 750) + 'px'); |
| 546 | return html; |
| 547 | }, |
| 548 | // #endif |
| 549 | // #ifdef APP-PLUS-NVUE |
| 550 | _message(e) { |
| 551 | // 接收 web-view 消息 |
| 552 | var d = e.detail.data[0]; |
| 553 | switch (d.action) { |
| 554 | case 'load': |
| 555 | this.$emit('load'); |
| 556 | this.height = d.height; |
| 557 | this._text = d.text; |
| 558 | break; |
| 559 | case 'getTitle': |
| 560 | if (this.autosetTitle) |
| 561 | uni.setNavigationBarTitle({ |
| 562 | title: d.title |
| 563 | }) |
| 564 | break; |
| 565 | case 'getImgList': |
| 566 | this.imgList.length = 0; |
| 567 | for (var i = d.imgList.length; i--;) |
| 568 | this.imgList.setItem(i, d.imgList[i]); |
| 569 | break; |
| 570 | case 'preview': |
| 571 | var preview = true; |
| 572 | d.img.ignore = () => preview = false; |
| 573 | this.$emit('imgtap', d.img); |
| 574 | if (preview) |
| 575 | uni.previewImage({ |
| 576 | current: d.img.i, |
| 577 | urls: this.imgList |
| 578 | }) |
| 579 | break; |
| 580 | case 'linkpress': |
| 581 | var jump = true, |
| 582 | href = d.href; |
| 583 | this.$emit('linkpress', { |
| 584 | href, |
| 585 | ignore: () => jump = false |
| 586 | }) |
| 587 | if (jump && href) { |
| 588 | if (href[0] == '#') { |
| 589 | if (this.useAnchor) |
| 590 | weexDom.scrollToElement(this.$refs.web, { |
| 591 | offset: d.offset |
| 592 | }) |
| 593 | } else if (href.includes('://')) |
| 594 | plus.runtime.openWeb(href); |
| 595 | else |
| 596 | uni.navigateTo({ |
| 597 | url: href |
| 598 | }) |
| 599 | } |
| 600 | break; |
| 601 | case 'error': |
| 602 | if (d.source == 'img' && cfg.errorImg) |
| 603 | this.imgList.setItem(d.target.i, cfg.errorImg); |
| 604 | this.$emit('error', { |
| 605 | source: d.source, |
| 606 | target: d.target |
| 607 | }) |
| 608 | break; |
| 609 | case 'ready': |
| 610 | this.height = d.height; |
| 611 | if (d.ready) uni.createSelectorQuery().in(this).select('#_top').boundingClientRect().exec(res => { |
| 612 | this.rect = res[0]; |
| 613 | this.$emit('ready', res[0]); |
| 614 | }) |
| 615 | break; |
| 616 | case 'click': |
| 617 | this.$emit('click'); |
| 618 | this.$emit('tap'); |
| 619 | } |
| 620 | }, |
| 621 | // #endif |
| 622 | } |
| 623 | } |
| 624 | </script> |
| 625 | |
| 626 | <style> |
| 627 | @keyframes _show { |
| 628 | 0% { |
| 629 | opacity: 0; |
| 630 | } |
| 631 | |
| 632 | 100% { |
| 633 | opacity: 1; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | /* #ifdef MP-WEIXIN */ |
| 638 | :host { |
| 639 | display: block; |
| 640 | overflow: auto; |
| 641 | -webkit-overflow-scrolling: touch; |
| 642 | } |
| 643 | |
| 644 | /* #endif */ |
| 645 | </style> |