jyf-parser.vue 21 KB

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