node.vue 13 KB

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