node.vue 15 KB

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