node.vue 16 KB

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