node.vue 13 KB

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