node.vue 14 KB

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