node.vue 16 KB

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