Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

631 строка
17 KiB

  1. <template>
  2. <view :id="attrs.id" :class="'_block _'+name+' '+attrs.class" :style="attrs.style">
  3. <block v-for="(n, i) in childs" v-bind:key="i">
  4. <!-- 图片 -->
  5. <!-- 占位图 -->
  6. <image v-if="n.name==='img'&&!n.t&&((opts[1]&&!ctrl[i])||ctrl[i]<0)" class="_img" :style="n.attrs.style" :src="ctrl[i]<0?opts[2]:opts[1]" mode="widthFix" />
  7. <!-- 显示图片 -->
  8. <!-- #ifdef H5 || (APP-PLUS && VUE2) -->
  9. <img v-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl[i]===-1?'display:none;':'')+n.attrs.style" :src="n.attrs.src||(ctrl.load?n.attrs['data-src']:'')" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap" @longpress="imgLongTap" />
  10. <!-- #endif -->
  11. <!-- #ifndef H5 || (APP-PLUS && VUE2) -->
  12. <!-- 表格中的图片,使用 rich-text 防止大小不正确 -->
  13. <rich-text v-if="n.name==='img'&&n.t" :style="'display:'+n.t" :nodes="[{attrs:{style:n.attrs.style||'',src:n.attrs.src},name:'img'}]" :data-i="i" @tap.stop="imgTap" />
  14. <!-- #endif -->
  15. <!-- #ifdef APP-HARMONY -->
  16. <image v-else-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl[i]===-1?'display:none;':'')+'width:'+ctrl[i]+'px;'+n.attrs.style" :src="n.attrs.src||(ctrl.load?n.attrs['data-src']:'')" :mode="!n.h?'widthFix':(!n.w?'heightFix':(n.m||'scaleToFill'))" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap" @longpress="imgLongTap" />
  17. <!-- #endif -->
  18. <!-- #ifndef H5 || APP-PLUS || MP-KUAISHOU -->
  19. <image v-else-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl[i]===-1?'display:none;':'')+'width:'+(ctrl[i]||1)+'px;height:1px;'+n.attrs.style" :src="n.attrs.src" :mode="!n.h?'widthFix':(!n.w?'heightFix':(n.m||'scaleToFill'))" :lazy-load="opts[0]" :webp="n.webp" :show-menu-by-longpress="opts[3]&&!n.attrs.ignore" :image-menu-prevent="!opts[3]||n.attrs.ignore" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap" @longpress="imgLongTap" />
  20. <!-- #endif -->
  21. <!-- #ifdef MP-KUAISHOU -->
  22. <image v-else-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl[i]===-1?'display:none;':'')+n.attrs.style" :src="n.attrs.src" :lazy-load="opts[0]" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap"></image>
  23. <!-- #endif -->
  24. <!-- #ifdef APP-PLUS && VUE3 -->
  25. <image v-else-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl[i]===-1?'display:none;':'')+'width:'+(ctrl[i]||1)+'px;'+n.attrs.style" :src="n.attrs.src||(ctrl.load?n.attrs['data-src']:'')" :mode="!n.h?'widthFix':(!n.w?'heightFix':(n.m||''))" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap" @longpress="imgLongTap" />
  26. <!-- #endif -->
  27. <!-- 文本 -->
  28. <!-- #ifdef MP-WEIXIN -->
  29. <text v-else-if="n.text" :user-select="opts[4]=='force'&&isiOS" decode>{{n.text}}</text>
  30. <!-- #endif -->
  31. <!-- #ifndef MP-WEIXIN || MP-BAIDU || MP-ALIPAY || MP-TOUTIAO -->
  32. <text v-else-if="n.text" decode>{{n.text}}</text>
  33. <!-- #endif -->
  34. <text v-else-if="n.name==='br'">{{'\n'}}</text>
  35. <!-- 链接 -->
  36. <view v-else-if="n.name==='a'" :id="n.attrs.id" :class="(n.attrs.href?'_a ':'')+n.attrs.class" hover-class="_hover" :style="'display:inline;'+n.attrs.style" :data-i="i" @tap.stop="linkTap">
  37. <node name="span" :childs="n.children" :opts="opts" style="display:inherit" />
  38. </view>
  39. <!-- 视频 -->
  40. <!-- #ifdef APP-PLUS -->
  41. <view v-else-if="n.html" :id="n.attrs.id" :class="'_video '+n.attrs.class" :style="n.attrs.style" v-html="n.html" :data-i="i" @vplay.stop="play" />
  42. <!-- #endif -->
  43. <!-- #ifndef APP-PLUS -->
  44. <video v-else-if="n.name==='video'" :id="n.attrs.id" :class="n.attrs.class" :style="n.attrs.style" :autoplay="n.attrs.autoplay" :controls="n.attrs.controls" :loop="n.attrs.loop" :muted="n.attrs.muted" :object-fit="n.attrs['object-fit']" :poster="n.attrs.poster" :src="n.src[ctrl[i]||0]" :data-i="i" @play="play" @error="mediaError" />
  45. <!-- #endif -->
  46. <!-- #ifdef H5 || APP-PLUS -->
  47. <iframe v-else-if="n.name==='iframe'" :style="n.attrs.style" :allowfullscreen="n.attrs.allowfullscreen" :frameborder="n.attrs.frameborder" :src="n.attrs.src" />
  48. <embed v-else-if="n.name==='embed'" :style="n.attrs.style" :src="n.attrs.src" />
  49. <!-- #endif -->
  50. <!-- #ifndef MP-TOUTIAO || ((H5 || APP-PLUS) && VUE3) -->
  51. <!-- 音频 -->
  52. <audio v-else-if="n.name==='audio'" :id="n.attrs.id" :class="n.attrs.class" :style="n.attrs.style" :author="n.attrs.author" :controls="n.attrs.controls" :loop="n.attrs.loop" :name="n.attrs.name" :poster="n.attrs.poster" :src="n.src[ctrl[i]||0]" :data-i="i" @play="play" @error="mediaError" />
  53. <!-- #endif -->
  54. <view v-else-if="(n.name==='table'&&n.c)||n.name==='li'" :id="n.attrs.id" :class="'_'+n.name+' '+n.attrs.class" :style="n.attrs.style">
  55. <node v-if="n.name==='li'" :childs="n.children" :opts="opts" />
  56. <view v-else v-for="(tbody, x) in n.children" v-bind:key="x" :class="'_'+tbody.name+' '+tbody.attrs.class" :style="tbody.attrs.style">
  57. <node v-if="tbody.name==='td'||tbody.name==='th'" :childs="tbody.children" :opts="opts" />
  58. <block v-else v-for="(tr, y) in tbody.children" v-bind:key="y">
  59. <view v-if="tr.name==='td'||tr.name==='th'" :class="'_'+tr.name+' '+tr.attrs.class" :style="tr.attrs.style">
  60. <node :childs="tr.children" :opts="opts" />
  61. </view>
  62. <view v-else :class="'_'+tr.name+' '+tr.attrs.class" :style="tr.attrs.style">
  63. <view v-for="(td, z) in tr.children" v-bind:key="z" :class="'_'+td.name+' '+td.attrs.class" :style="td.attrs.style">
  64. <node :childs="td.children" :opts="opts" />
  65. </view>
  66. </view>
  67. </block>
  68. </view>
  69. </view>
  70. <!-- insert -->
  71. <!-- 富文本 -->
  72. <!-- #ifdef H5 || ((MP-WEIXIN || MP-QQ || APP-PLUS || MP-360) && VUE2) -->
  73. <rich-text v-else-if="!n.c&&!handler.isInline(n.name, n.attrs.style)" :id="n.attrs.id" :style="n.f" :user-select="opts[4]" :nodes="[n]" />
  74. <!-- #endif -->
  75. <!-- #ifndef H5 || ((MP-WEIXIN || MP-QQ || APP-PLUS || MP-360) && VUE2) -->
  76. <rich-text v-else-if="!n.c" :id="n.attrs.id" :style="'display:inline;'+n.f" :preview="false" :selectable="opts[4]" :user-select="opts[4]" :nodes="[n]" />
  77. <!-- #endif -->
  78. <!-- 继续递归 -->
  79. <view v-else-if="n.c===2" :id="n.attrs.id" :class="'_block _'+n.name+' '+n.attrs.class" :style="n.f+';'+n.attrs.style">
  80. <node v-for="(n2, j) in n.children" v-bind:key="j" :style="n2.f" :name="n2.name" :attrs="n2.attrs" :childs="n2.children" :opts="opts" />
  81. </view>
  82. <node v-else :style="n.f" :name="n.name" :attrs="n.attrs" :childs="n.children" :opts="opts" />
  83. </block>
  84. </view>
  85. </template>
  86. <script module="handler" lang="wxs">
  87. // 行内标签列表
  88. var inlineTags = {
  89. abbr: true,
  90. b: true,
  91. big: true,
  92. code: true,
  93. del: true,
  94. em: true,
  95. i: true,
  96. ins: true,
  97. label: true,
  98. q: true,
  99. small: true,
  100. span: true,
  101. strong: true,
  102. sub: true,
  103. sup: true
  104. }
  105. /**
  106. * @description 判断是否为行内标签
  107. */
  108. module.exports = {
  109. isInline: function (tagName, style) {
  110. return inlineTags[tagName] || (style || '').indexOf('display:inline') !== -1
  111. }
  112. }
  113. </script>
  114. <script>
  115. import node from './node'
  116. export default {
  117. name: 'node',
  118. options: {
  119. // #ifdef MP-WEIXIN
  120. virtualHost: true,
  121. // #endif
  122. // #ifdef MP-TOUTIAO
  123. addGlobalClass: false
  124. // #endif
  125. },
  126. data () {
  127. return {
  128. ctrl: {},
  129. // #ifdef MP-WEIXIN
  130. isiOS: uni.getSystemInfoSync().system.includes('iOS')
  131. // #endif
  132. }
  133. },
  134. props: {
  135. name: String,
  136. attrs: {
  137. type: Object,
  138. default () {
  139. return {}
  140. }
  141. },
  142. childs: Array,
  143. opts: Array
  144. },
  145. components: {
  146. // #ifndef ((H5 || APP-PLUS) && VUE3) || APP-HARMONY
  147. node
  148. // #endif
  149. },
  150. mounted () {
  151. this.$nextTick(() => {
  152. // 修复可能导致死循环的问题
  153. for (this.root = this?.$parent; this.root && this.root?.$options.name !== 'up-parse'; this.root = this.root?.$parent);
  154. })
  155. // #ifdef H5 || APP-PLUS
  156. if (this.opts[0]) {
  157. let i
  158. for (i = this.childs.length; i--;) {
  159. if (this.childs[i].name === 'img') break
  160. }
  161. if (i !== -1) {
  162. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  163. top: 500,
  164. bottom: 500
  165. })
  166. this.observer.observe('._img', res => {
  167. if (res.intersectionRatio) {
  168. this.$set(this.ctrl, 'load', 1)
  169. this.observer.disconnect()
  170. }
  171. })
  172. }
  173. }
  174. // #endif
  175. },
  176. beforeDestroy () {
  177. // #ifdef H5 || APP-PLUS
  178. if (this.observer) {
  179. this.observer.disconnect()
  180. }
  181. // #endif
  182. },
  183. emits: ['linktap', 'imgtap', 'play', 'ready', 'error'],
  184. methods: {
  185. // #ifdef MP-WEIXIN
  186. toJSON () { return this },
  187. // #endif
  188. /**
  189. * @description 规范化链接地址
  190. * @param {any} href
  191. * @return {String}
  192. */
  193. normalizeHref (href) {
  194. return typeof href === 'string' ? href.trim() : ''
  195. },
  196. /**
  197. * @description 判断是否为外部链接
  198. * @param {String} href
  199. * @return {Boolean}
  200. */
  201. isExternalLink (href) {
  202. return href.split('?')[0].includes('://')
  203. },
  204. /**
  205. * @description 打开外部链接
  206. * @param {String} href
  207. */
  208. openExternalLink (href) {
  209. // #ifdef H5
  210. window.open(href)
  211. // #endif
  212. // #ifdef MP
  213. uni.setClipboardData({
  214. data: href,
  215. success: () =>
  216. uni.showToast({
  217. title: '链接已复制'
  218. })
  219. })
  220. // #endif
  221. // #ifdef APP-PLUS
  222. try {
  223. plus.runtime.openWeb(href)
  224. } catch (e) { }
  225. // #endif
  226. // #ifdef APP-HARMONY
  227. try {
  228. plus.runtime.openURL(href)
  229. } catch (e) { }
  230. // #endif
  231. },
  232. /**
  233. * @description 播放视频事件
  234. * @param {Event} e
  235. */
  236. play (e) {
  237. const i = e.currentTarget.dataset.i
  238. const node = this.childs[i]
  239. this.root.$emit('play', {
  240. source: node.name,
  241. attrs: {
  242. ...node.attrs,
  243. src: node.src[this.ctrl[i] || 0]
  244. }
  245. })
  246. // #ifndef APP-PLUS
  247. if (this.root.pauseVideo) {
  248. let flag = false
  249. const id = e.target.id
  250. for (let i = this.root._videos.length; i--;) {
  251. if (this.root._videos[i].id === id) {
  252. flag = true
  253. } else {
  254. this.root._videos[i].pause() // 自动暂停其他视频
  255. }
  256. }
  257. // 将自己加入列表
  258. if (!flag) {
  259. const ctx = uni.createVideoContext(id
  260. // #ifndef MP-BAIDU
  261. , this
  262. // #endif
  263. )
  264. ctx.id = id
  265. if (this.root.playbackRate) {
  266. ctx.playbackRate(this.root.playbackRate)
  267. }
  268. this.root._videos.push(ctx)
  269. }
  270. }
  271. // #endif
  272. },
  273. /**
  274. * @description 图片点击事件
  275. * @param {Event} e
  276. */
  277. imgTap (e) {
  278. const node = this.childs[e.currentTarget.dataset.i]
  279. if (node.a) {
  280. this.linkTap(node.a)
  281. return
  282. }
  283. if (node.attrs.ignore) return
  284. // #ifdef H5 || APP-PLUS
  285. node.attrs.src = node.attrs.src || node.attrs['data-src']
  286. // #endif
  287. // #ifndef APP-HARMONY
  288. this.root.$emit('imgtap', node.attrs)
  289. // #endif
  290. // #ifdef APP-HARMONY
  291. this.root.$emit('imgtap', {
  292. ...node.attrs
  293. })
  294. // #endif
  295. // 自动预览图片
  296. if (this.root.previewImg) {
  297. uni.previewImage({
  298. // #ifdef MP-WEIXIN
  299. showmenu: this.root.showImgMenu,
  300. // #endif
  301. // #ifdef MP-ALIPAY
  302. enablesavephoto: this.root.showImgMenu,
  303. enableShowPhotoDownload: this.root.showImgMenu,
  304. // #endif
  305. current: parseInt(node.attrs.i),
  306. urls: this.root.imgList
  307. })
  308. }
  309. },
  310. /**
  311. * @description 图片长按
  312. */
  313. imgLongTap (e) {
  314. // #ifdef APP-PLUS
  315. const attrs = this.childs[e.currentTarget.dataset.i].attrs
  316. if (this.opts[3] && !attrs.ignore) {
  317. uni.showActionSheet({
  318. itemList: ['保存图片'],
  319. success: () => {
  320. const save = path => {
  321. uni.saveImageToPhotosAlbum({
  322. filePath: path,
  323. success () {
  324. uni.showToast({
  325. title: '保存成功'
  326. })
  327. }
  328. })
  329. }
  330. if (this.root.imgList[attrs.i].startsWith('http')) {
  331. uni.downloadFile({
  332. url: this.root.imgList[attrs.i],
  333. success: res => save(res.tempFilePath)
  334. })
  335. } else {
  336. save(this.root.imgList[attrs.i])
  337. }
  338. }
  339. })
  340. }
  341. // #endif
  342. },
  343. /**
  344. * @description 图片加载完成事件
  345. * @param {Event} e
  346. */
  347. imgLoad (e) {
  348. const i = e.currentTarget.dataset.i
  349. /* #ifndef H5 || (APP-PLUS && VUE2) */
  350. if (!this.childs[i].w) {
  351. // 设置原宽度
  352. this.$set(this.ctrl, i, e.detail.width)
  353. } else /* #endif */ if ((this.opts[1] && !this.ctrl[i]) || this.ctrl[i] === -1) {
  354. // 加载完毕,取消加载中占位图
  355. this.$set(this.ctrl, i, 1)
  356. }
  357. this.checkReady()
  358. },
  359. /**
  360. * @description 检查是否所有图片加载完毕
  361. */
  362. checkReady () {
  363. if (this.root && !this.root.lazyLoad) {
  364. this.root._unloadimgs -= 1
  365. if (!this.root._unloadimgs) {
  366. setTimeout(() => {
  367. this.root.getRect().then(rect => {
  368. this.root.$emit('ready', rect)
  369. }).catch(() => {
  370. this.root.$emit('ready', {})
  371. })
  372. }, 350)
  373. }
  374. }
  375. },
  376. /**
  377. * @description 链接点击事件
  378. * @param {Event} e
  379. */
  380. linkTap (e) {
  381. const node = e.currentTarget ? this.childs[e.currentTarget.dataset.i] : {}
  382. const attrs = node.attrs || e
  383. const href = this.normalizeHref(attrs.href)
  384. this.root.$emit('linktap', Object.assign({
  385. innerText: this.root.getText(node.children || []) // 链接内的文本内容
  386. }, attrs))
  387. if (href) {
  388. if (href[0] === '#') {
  389. // 跳转锚点
  390. this.root.navigateTo(href.substring(1)).catch(() => { })
  391. } else if (this.isExternalLink(href)) {
  392. // 复制外部链接
  393. if (this.root.copyLink) {
  394. this.openExternalLink(href)
  395. }
  396. } else {
  397. // 跳转页面
  398. uni.navigateTo({
  399. url: href,
  400. fail () {
  401. uni.switchTab({
  402. url: href,
  403. fail () { }
  404. })
  405. }
  406. })
  407. }
  408. }
  409. },
  410. /**
  411. * @description 错误事件
  412. * @param {Event} e
  413. */
  414. mediaError (e) {
  415. const i = e.currentTarget.dataset.i
  416. const node = this.childs[i]
  417. // 加载其他源
  418. if (node.name === 'video' || node.name === 'audio') {
  419. let index = (this.ctrl[i] || 0) + 1
  420. if (index > node.src.length) {
  421. index = 0
  422. }
  423. if (index < node.src.length) {
  424. this.$set(this.ctrl, i, index)
  425. return
  426. }
  427. } else if (node.name === 'img') {
  428. // #ifdef H5 && VUE3
  429. if (this.opts[0] && !this.ctrl.load) return
  430. // #endif
  431. // 显示错误占位图
  432. if (this.opts[2]) {
  433. this.$set(this.ctrl, i, -1)
  434. }
  435. this.checkReady()
  436. }
  437. if (this.root) {
  438. this.root.$emit('error', {
  439. source: node.name,
  440. attrs: node.attrs,
  441. // #ifndef H5 && VUE3
  442. errMsg: e.detail.errMsg
  443. // #endif
  444. })
  445. }
  446. }
  447. }
  448. }
  449. </script>
  450. <style>
  451. /* a 标签默认效果 */
  452. ._a {
  453. padding: 1.5px 0 1.5px 0;
  454. color: #366092;
  455. word-break: break-all;
  456. }
  457. /* a 标签点击态效果 */
  458. ._hover {
  459. text-decoration: underline;
  460. opacity: 0.7;
  461. }
  462. /* 图片默认效果 */
  463. ._img {
  464. max-width: 100%;
  465. -webkit-touch-callout: none;
  466. }
  467. /* 内部样式 */
  468. ._block {
  469. display: block;
  470. }
  471. ._b,
  472. ._strong {
  473. font-weight: bold;
  474. }
  475. ._code {
  476. font-family: monospace;
  477. }
  478. ._del {
  479. text-decoration: line-through;
  480. }
  481. ._em,
  482. ._i {
  483. font-style: italic;
  484. }
  485. ._h1 {
  486. font-size: 2em;
  487. }
  488. ._h2 {
  489. font-size: 1.5em;
  490. }
  491. ._h3 {
  492. font-size: 1.17em;
  493. }
  494. ._h5 {
  495. font-size: 0.83em;
  496. }
  497. ._h6 {
  498. font-size: 0.67em;
  499. }
  500. ._h1,
  501. ._h2,
  502. ._h3,
  503. ._h4,
  504. ._h5,
  505. ._h6 {
  506. display: block;
  507. font-weight: bold;
  508. }
  509. ._image {
  510. height: 1px;
  511. }
  512. ._ins {
  513. text-decoration: underline;
  514. }
  515. ._li {
  516. display: list-item;
  517. }
  518. ._ol {
  519. list-style-type: decimal;
  520. }
  521. ._ol,
  522. ._ul {
  523. display: block;
  524. padding-left: 40px;
  525. margin: 1em 0;
  526. }
  527. ._q::before {
  528. content: '"';
  529. }
  530. ._q::after {
  531. content: '"';
  532. }
  533. ._sub {
  534. font-size: smaller;
  535. vertical-align: sub;
  536. }
  537. ._sup {
  538. font-size: smaller;
  539. vertical-align: super;
  540. }
  541. ._thead,
  542. ._tbody,
  543. ._tfoot {
  544. display: table-row-group;
  545. }
  546. ._tr {
  547. display: table-row;
  548. }
  549. ._td,
  550. ._th {
  551. display: table-cell;
  552. vertical-align: middle;
  553. }
  554. ._th {
  555. font-weight: bold;
  556. text-align: center;
  557. }
  558. ._ul {
  559. list-style-type: disc;
  560. }
  561. ._ul ._ul {
  562. margin: 0;
  563. list-style-type: circle;
  564. }
  565. ._ul ._ul ._ul {
  566. list-style-type: square;
  567. }
  568. ._abbr,
  569. ._b,
  570. ._code,
  571. ._del,
  572. ._em,
  573. ._i,
  574. ._ins,
  575. ._label,
  576. ._q,
  577. ._span,
  578. ._strong,
  579. ._sub,
  580. ._sup {
  581. display: inline;
  582. }
  583. /* #ifdef APP-PLUS */
  584. ._video {
  585. width: 300px;
  586. height: 225px;
  587. }
  588. /* #endif */
  589. </style>