node.vue 13 KB

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