node.vue 17 KB

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