node.vue 17 KB

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