jyf-parser.vue 20 KB

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