node.vue 18 KB

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