node.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 !== 'mp-html'; 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. methods: {
  184. // #ifdef MP-WEIXIN
  185. toJSON () { return this },
  186. // #endif
  187. /**
  188. * @description 播放视频事件
  189. * @param {Event} e
  190. */
  191. play (e) {
  192. const i = e.currentTarget.dataset.i
  193. const node = this.childs[i]
  194. this.root.$emit('play', {
  195. source: node.name,
  196. attrs: {
  197. ...node.attrs,
  198. src: node.src[this.ctrl[i] || 0]
  199. }
  200. })
  201. // #ifndef APP-PLUS
  202. if (this.root.pauseVideo) {
  203. let flag = false
  204. const id = e.target.id
  205. for (let i = this.root._videos.length; i--;) {
  206. if (this.root._videos[i].id === id) {
  207. flag = true
  208. } else {
  209. this.root._videos[i].pause() // 自动暂停其他视频
  210. }
  211. }
  212. // 将自己加入列表
  213. if (!flag) {
  214. const ctx = uni.createVideoContext(id
  215. // #ifndef MP-BAIDU
  216. , this
  217. // #endif
  218. )
  219. ctx.id = id
  220. if (this.root.playbackRate) {
  221. ctx.playbackRate(this.root.playbackRate)
  222. }
  223. this.root._videos.push(ctx)
  224. }
  225. }
  226. // #endif
  227. },
  228. /**
  229. * @description 图片点击事件
  230. * @param {Event} e
  231. */
  232. imgTap (e) {
  233. const node = this.childs[e.currentTarget.dataset.i]
  234. if (node.a) {
  235. this.linkTap(node.a)
  236. return
  237. }
  238. if (node.attrs.ignore) return
  239. // #ifdef H5 || APP-PLUS
  240. node.attrs.src = node.attrs.src || node.attrs['data-src']
  241. // #endif
  242. // #ifndef APP-HARMONY
  243. this.root.$emit('imgtap', node.attrs)
  244. // #endif
  245. // #ifdef APP-HARMONY
  246. this.root.$emit('imgtap', {
  247. ...node.attrs
  248. })
  249. // #endif
  250. // 自动预览图片
  251. if (this.root.previewImg) {
  252. uni.previewImage({
  253. // #ifdef MP-WEIXIN
  254. showmenu: this.root.showImgMenu,
  255. // #endif
  256. // #ifdef MP-ALIPAY
  257. enablesavephoto: this.root.showImgMenu,
  258. enableShowPhotoDownload: this.root.showImgMenu,
  259. // #endif
  260. current: parseInt(node.attrs.i),
  261. urls: this.root.imgList
  262. })
  263. }
  264. },
  265. /**
  266. * @description 图片长按
  267. */
  268. imgLongTap (e) {
  269. // #ifdef APP-PLUS
  270. const attrs = this.childs[e.currentTarget.dataset.i].attrs
  271. if (this.opts[3] && !attrs.ignore) {
  272. uni.showActionSheet({
  273. itemList: ['保存图片'],
  274. success: () => {
  275. const save = path => {
  276. uni.saveImageToPhotosAlbum({
  277. filePath: path,
  278. success () {
  279. uni.showToast({
  280. title: '保存成功'
  281. })
  282. }
  283. })
  284. }
  285. if (this.root.imgList[attrs.i].startsWith('http')) {
  286. uni.downloadFile({
  287. url: this.root.imgList[attrs.i],
  288. success: res => save(res.tempFilePath)
  289. })
  290. } else {
  291. save(this.root.imgList[attrs.i])
  292. }
  293. }
  294. })
  295. }
  296. // #endif
  297. },
  298. /**
  299. * @description 图片加载完成事件
  300. * @param {Event} e
  301. */
  302. imgLoad (e) {
  303. const i = e.currentTarget.dataset.i
  304. /* #ifndef H5 || (APP-PLUS && VUE2) */
  305. if (!this.childs[i].w) {
  306. // 设置原宽度
  307. this.$set(this.ctrl, i, e.detail.width)
  308. } else /* #endif */ if ((this.opts[1] && !this.ctrl[i]) || this.ctrl[i] === -1) {
  309. // 加载完毕,取消加载中占位图
  310. this.$set(this.ctrl, i, 1)
  311. }
  312. this.checkReady()
  313. },
  314. /**
  315. * @description 检查是否所有图片加载完毕
  316. */
  317. checkReady () {
  318. if (this.root && !this.root.lazyLoad) {
  319. this.root._unloadimgs -= 1
  320. if (!this.root._unloadimgs) {
  321. setTimeout(() => {
  322. this.root.getRect().then(rect => {
  323. this.root.$emit('ready', rect)
  324. }).catch(() => {
  325. this.root.$emit('ready', {})
  326. })
  327. }, 350)
  328. }
  329. }
  330. },
  331. /**
  332. * @description 链接点击事件
  333. * @param {Event} e
  334. */
  335. linkTap (e) {
  336. const node = e.currentTarget ? this.childs[e.currentTarget.dataset.i] : {}
  337. const attrs = node.attrs || e
  338. const href = attrs.href
  339. this.root.$emit('linktap', Object.assign({
  340. innerText: this.root.getText(node.children || []) // 链接内的文本内容
  341. }, attrs))
  342. if (href) {
  343. if (href[0] === '#') {
  344. // 跳转锚点
  345. this.root.navigateTo(href.substring(1)).catch(() => { })
  346. } else if (href.split('?')[0].includes('://')) {
  347. // 复制外部链接
  348. if (this.root.copyLink) {
  349. // #ifdef H5
  350. window.open(href)
  351. // #endif
  352. // #ifdef MP
  353. uni.setClipboardData({
  354. data: href,
  355. success: () =>
  356. uni.showToast({
  357. title: '链接已复制'
  358. })
  359. })
  360. // #endif
  361. // #ifdef APP-PLUS
  362. plus.runtime.openWeb(href)
  363. // #endif
  364. // #ifdef APP-HARMONY
  365. plus.runtime.openURL(href)
  366. // #endif
  367. }
  368. } else {
  369. // 跳转页面
  370. uni.navigateTo({
  371. url: href,
  372. fail () {
  373. uni.switchTab({
  374. url: href,
  375. fail () { }
  376. })
  377. }
  378. })
  379. }
  380. }
  381. },
  382. /**
  383. * @description 错误事件
  384. * @param {Event} e
  385. */
  386. mediaError (e) {
  387. const i = e.currentTarget.dataset.i
  388. const node = this.childs[i]
  389. // 加载其他源
  390. if (node.name === 'video' || node.name === 'audio') {
  391. let index = (this.ctrl[i] || 0) + 1
  392. if (index > node.src.length) {
  393. index = 0
  394. }
  395. if (index < node.src.length) {
  396. this.$set(this.ctrl, i, index)
  397. return
  398. }
  399. } else if (node.name === 'img') {
  400. // #ifdef H5 && VUE3
  401. if (this.opts[0] && !this.ctrl.load) return
  402. // #endif
  403. // 显示错误占位图
  404. if (this.opts[2]) {
  405. this.$set(this.ctrl, i, -1)
  406. }
  407. this.checkReady()
  408. }
  409. if (this.root) {
  410. this.root.$emit('error', {
  411. source: node.name,
  412. attrs: node.attrs,
  413. // #ifndef H5 && VUE3
  414. errMsg: e.detail.errMsg
  415. // #endif
  416. })
  417. }
  418. }
  419. }
  420. }
  421. </script>
  422. <style>
  423. /* a 标签默认效果 */
  424. ._a {
  425. padding: 1.5px 0 1.5px 0;
  426. color: #366092;
  427. word-break: break-all;
  428. }
  429. /* a 标签点击态效果 */
  430. ._hover {
  431. text-decoration: underline;
  432. opacity: 0.7;
  433. }
  434. /* 图片默认效果 */
  435. ._img {
  436. max-width: 100%;
  437. -webkit-touch-callout: none;
  438. }
  439. /* 内部样式 */
  440. ._block {
  441. display: block;
  442. }
  443. ._b,
  444. ._strong {
  445. font-weight: bold;
  446. }
  447. ._code {
  448. font-family: monospace;
  449. }
  450. ._del {
  451. text-decoration: line-through;
  452. }
  453. ._em,
  454. ._i {
  455. font-style: italic;
  456. }
  457. ._h1 {
  458. font-size: 2em;
  459. }
  460. ._h2 {
  461. font-size: 1.5em;
  462. }
  463. ._h3 {
  464. font-size: 1.17em;
  465. }
  466. ._h5 {
  467. font-size: 0.83em;
  468. }
  469. ._h6 {
  470. font-size: 0.67em;
  471. }
  472. ._h1,
  473. ._h2,
  474. ._h3,
  475. ._h4,
  476. ._h5,
  477. ._h6 {
  478. display: block;
  479. font-weight: bold;
  480. }
  481. ._image {
  482. height: 1px;
  483. }
  484. ._ins {
  485. text-decoration: underline;
  486. }
  487. ._li {
  488. display: list-item;
  489. }
  490. ._ol {
  491. list-style-type: decimal;
  492. }
  493. ._ol,
  494. ._ul {
  495. display: block;
  496. padding-left: 40px;
  497. margin: 1em 0;
  498. }
  499. ._q::before {
  500. content: '"';
  501. }
  502. ._q::after {
  503. content: '"';
  504. }
  505. ._sub {
  506. font-size: smaller;
  507. vertical-align: sub;
  508. }
  509. ._sup {
  510. font-size: smaller;
  511. vertical-align: super;
  512. }
  513. ._thead,
  514. ._tbody,
  515. ._tfoot {
  516. display: table-row-group;
  517. }
  518. ._tr {
  519. display: table-row;
  520. }
  521. ._td,
  522. ._th {
  523. display: table-cell;
  524. vertical-align: middle;
  525. }
  526. ._th {
  527. font-weight: bold;
  528. text-align: center;
  529. }
  530. ._ul {
  531. list-style-type: disc;
  532. }
  533. ._ul ._ul {
  534. margin: 0;
  535. list-style-type: circle;
  536. }
  537. ._ul ._ul ._ul {
  538. list-style-type: square;
  539. }
  540. ._abbr,
  541. ._b,
  542. ._code,
  543. ._del,
  544. ._em,
  545. ._i,
  546. ._ins,
  547. ._label,
  548. ._q,
  549. ._span,
  550. ._strong,
  551. ._sub,
  552. ._sup {
  553. display: inline;
  554. }
  555. /* #ifdef APP-PLUS */
  556. ._video {
  557. width: 300px;
  558. height: 225px;
  559. }
  560. /* #endif */
  561. </style>