trees.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <!--
  2. trees 递归显示组件
  3. github:https://github.com/jin-yufeng/Parser
  4. docs:https://jin-yufeng.github.io/Parser
  5. 插件市场:https://ext.dcloud.net.cn/plugin?id=805
  6. author:JinYufeng
  7. update:2020/03/23
  8. -->
  9. <template>
  10. <view class="interlayer">
  11. <block v-for="(n, index) in nodes" v-bind:key="index">
  12. <!--图片-->
  13. <!--#ifdef MP-WEIXIN || MP-QQ || MP-ALIPAY || APP-PLUS-->
  14. <rich-text v-if="n.name=='img'" :id="n.attrs.id" class="_img" :style="''+handler.getStyle(n.attrs.style)" :nodes="handler.getNode(n,!lazyLoad||imgLoad)"
  15. :data-attrs="n.attrs" @tap="imgtap" @longpress="imglongtap" />
  16. <!--#endif-->
  17. <!--#ifdef MP-BAIDU || MP-TOUTIAO-->
  18. <rich-text v-if="n.name=='img'" :id="n.attrs.id" class="_img" :style="n.attrs.contain" :nodes='[n]' :data-attrs="n.attrs"
  19. @tap="imgtap" @longpress="imglongtap" />
  20. <!--#endif-->
  21. <!--文本-->
  22. <!--#ifdef MP-WEIXIN || MP-QQ || APP-PLUS-->
  23. <rich-text v-else-if="n.decode" class="_entity" :nodes="[n]"></rich-text>
  24. <!--#endif-->
  25. <text v-else-if="n.type=='text'" decode>{{n.text}}</text>
  26. <text v-else-if="n.name=='br'">\n</text>
  27. <!--视频-->
  28. <view v-else-if="n.name=='video'">
  29. <view v-if="(!loadVideo||n.lazyLoad)&&!(controls[n.attrs.id]&&controls[n.attrs.id].play)" :id="n.attrs.id" :class="'_video '+(n.attrs.class||'')"
  30. :style="n.attrs.style" @tap="_loadVideo" />
  31. <video v-else :id="n.attrs.id" :class="n.attrs.class" :style="n.attrs.style" :autoplay="n.attrs.autoplay||(controls[n.attrs.id]&&controls[n.attrs.id].play)"
  32. :controls="n.attrs.controls" :loop="n.attrs.loop" :muted="n.attrs.muted" :poster="n.attrs.poster" :src="n.attrs.source[(controls[n.attrs.id]&&controls[n.attrs.id].index)||0]"
  33. :unit-id="n.attrs['unit-id']" :data-id="n.attrs.id" data-from="video" data-source="source" @error="error" @play="play" />
  34. </view>
  35. <!--音频-->
  36. <audio v-else-if="n.name=='audio'" :class="n.attrs.class" :style="n.attrs.style" :author="n.attrs.author" :autoplay="n.attrs.autoplay"
  37. :controls="n.attrs.controls" :loop="n.attrs.loop" :name="n.attrs.name" :poster="n.attrs.poster" :src="n.attrs.source[(controls[n.attrs.id]&&controls[n.attrs.id].index)||0]"
  38. :data-id="n.attrs.id" data-from="audio" data-source="source" @error="error" @play="play" />
  39. <!--链接-->
  40. <view v-else-if="n.name=='a'" :class="'_a '+(n.attrs.class||'')" hover-class="_hover" :style="n.attrs.style"
  41. :data-attrs="n.attrs" @tap="linkpress">
  42. <trees class="_span" :nodes="n.children" />
  43. </view>
  44. <!--广告(按需打开注释)-->
  45. <!--#ifdef MP-WEIXIN || MP-QQ || MP-TOUTIAO-->
  46. <!--<ad v-else-if="n.name=='ad'" :class="n.attrs.class" :style="n.attrs.style" :unit-id="n.attrs['unit-id']"
  47. data-from="ad" @error="error" />-->
  48. <!--#endif-->
  49. <!--#ifdef MP-BAIDU-->
  50. <!--<ad v-else-if="n.name=='ad'" :class="n.attrs.class" :style="n.attrs.style" :appid="n.attrs.appid"
  51. :apid="n.attrs.apid" :type="n.attrs.type" data-from="ad" @error="error" />-->
  52. <!--#endif-->
  53. <!--#ifdef APP-PLUS-->
  54. <!--<ad v-else-if="n.name=='ad'" :class="n.attrs.class" :style="n.attrs.style" :adpid="n.attrs.adpid"
  55. data-from="ad" @error="error" />-->
  56. <!--#endif-->
  57. <!--列表-->
  58. <view v-else-if="n.name=='li'" :id="n.attrs.id" :class="n.attrs.class" :style="(n.attrs.style||'')+';display:flex'">
  59. <view v-if="n.type=='ol'" class="_ol-bef">{{n.num}}</view>
  60. <view v-else class="_ul-bef">
  61. <view v-if="n.floor%3==0" class="_ul-p1">█</view>
  62. <view v-else-if="n.floor%3==2" class="_ul-p2" />
  63. <view v-else class="_ul-p1" style="border-radius:50%">█</view>
  64. </view>
  65. <!--#ifdef MP-ALIPAY-->
  66. <view class="_li">
  67. <trees :nodes="n.children" />
  68. </view>
  69. <!--#endif-->
  70. <!--#ifndef MP-ALIPAY-->
  71. <trees class="_li" :nodes="n.children" :lazyLoad="lazyLoad" :loadVideo="loadVideo" />
  72. <!--#endif-->
  73. </view>
  74. <!--表格-->
  75. <view v-else-if="n.name=='table'&&n.c" :id="n.attrs.id" :class="n.attrs.class" :style="(n.attrs.style||'')+';display:table'">
  76. <view v-for="(tbody, i) in n.children" v-bind:key="i" :class="tbody.attrs.class" :style="(tbody.attrs.style||'')+(tbody.name[0]=='t'?';display:table-'+(tbody.name=='tr'?'row':'row-group'):'')">
  77. <view v-for="(tr, j) in tbody.children" v-bind:key="j" :class="tr.attrs.class" :style="(tr.attrs.style||'')+(tr.name[0]=='t'?';display:table-'+(tr.name=='tr'?'row':'cell'):'')">
  78. <trees v-if="tr.name=='td'" :nodes="tr.children" :lazyLoad="lazyLoad" :loadVideo="loadVideo" />
  79. <block v-else>
  80. <!--#ifdef MP-ALIPAY-->
  81. <view v-for="(td, k) in tr.children" v-bind:key="k" :class="td.attrs.class" :style="(td.attrs.style||'')+(td.name[0]=='t'?';display:table-'+(td.name=='tr'?'row':'cell'):'')">
  82. <trees :nodes="td.children" />
  83. </view>
  84. <!--#endif-->
  85. <!--#ifndef MP-ALIPAY-->
  86. <trees v-for="(td, k) in tr.children" v-bind:key="k" :class="td.attrs.class" :style="(td.attrs.style||'')+(td.name[0]=='t'?';display:table-'+(td.name=='tr'?'row':'cell'):'')"
  87. :nodes="td.children" :lazyLoad="lazyLoad" :loadVideo="loadVideo" />
  88. <!--#endif-->
  89. </block>
  90. </view>
  91. </view>
  92. </view>
  93. <!--#ifdef APP-PLUS-->
  94. <iframe v-else-if="n.name=='iframe'" :style="n.attrs.style" :allowfullscreen="n.attrs.allowfullscreen" :frameborder="n.attrs.frameborder"
  95. :width="n.attrs.width" :height="n.attrs.height" :src="n.attrs.src" />
  96. <embed v-else-if="n.name=='embed'" :style="n.attrs.style" :width="n.attrs.width" :height="n.attrs.height" :src="n.attrs.src" />
  97. <!--#endif-->
  98. <!--富文本-->
  99. <!--#ifdef MP-WEIXIN || MP-QQ || MP-ALIPAY || APP-PLUS-->
  100. <rich-text v-else-if="handler.useRichText(n)" :id="n.attrs.id" :class="'_p __'+n.name" :nodes="[n]" />
  101. <!--#endif-->
  102. <!--#ifdef MP-BAIDU || MP-TOUTIAO-->
  103. <rich-text v-else-if="!(n.c||n.continue)" :id="n.attrs.id" :class="_p" :style="n.attrs.contain" :nodes="[n]" />
  104. <!--#endif-->
  105. <!--#ifdef MP-ALIPAY-->
  106. <view v-else :id="n.attrs.id" :class="'_'+n.name+' '+(n.attrs.class||'')" :style="n.attrs.style">
  107. <trees :nodes="n.children" />
  108. </view>
  109. <!--#endif-->
  110. <!--#ifndef MP-ALIPAY-->
  111. <trees v-else :class="(n.attrs.id||'')+' _'+n.name+' '+(n.attrs.class||'')" :style="n.attrs.style" :nodes="n.children"
  112. :lazyLoad="lazyLoad" :loadVideo="loadVideo" />
  113. <!--#endif-->
  114. </block>
  115. </view>
  116. </template>
  117. <script module="handler" lang="wxs" src="./handler.wxs"></script>
  118. <script module="handler" lang="sjs" src="./handler.sjs"></script>
  119. <script>
  120. import trees from './trees'
  121. export default {
  122. components: {
  123. trees
  124. },
  125. name: 'trees',
  126. data() {
  127. return {
  128. controls: {},
  129. // #ifdef MP-WEIXIN || MP-QQ || APP-PLUS
  130. imgLoad: false,
  131. // #endif
  132. // #ifndef APP-PLUS
  133. loadVideo: true
  134. // #endif
  135. }
  136. },
  137. props: {
  138. nodes: Array,
  139. // #ifdef MP-WEIXIN || MP-QQ || H5 || APP-PLUS
  140. lazyLoad: Boolean,
  141. // #endif
  142. // #ifdef APP-PLUS
  143. loadVideo: Boolean
  144. // #endif
  145. },
  146. mounted() {
  147. // 获取顶层组件
  148. this.top = this.$parent;
  149. while (this.top.$options.name != 'parser') {
  150. if (this.top.top) {
  151. this.top = this.top.top;
  152. break;
  153. }
  154. this.top = this.top.$parent;
  155. }
  156. },
  157. // #ifdef MP-WEIXIN || MP-QQ || APP-PLUS
  158. beforeDestroy() {
  159. if (this.observer)
  160. this.observer.disconnect();
  161. },
  162. // #endif
  163. methods: {
  164. // #ifndef MP-ALIPAY
  165. play(e) {
  166. if (this.top.videoContexts.length > 1 && this.top.autopause)
  167. for (var i = this.top.videoContexts.length; i--;)
  168. if (this.top.videoContexts[i].id != e.currentTarget.dataset.id)
  169. this.top.videoContexts[i].pause();
  170. },
  171. // #endif
  172. imgtap(e) {
  173. var attrs = e.currentTarget.dataset.attrs;
  174. if (!attrs.ignore) {
  175. var preview = true;
  176. this.top.$emit('imgtap', {
  177. id: e.target.id,
  178. src: attrs.src,
  179. ignore: () => preview = false
  180. })
  181. if (preview) {
  182. var urls = this.top.imgList,
  183. current = urls[attrs.i] ? parseInt(attrs.i) : (urls = [attrs.src], 0);
  184. uni.previewImage({
  185. current,
  186. urls
  187. })
  188. }
  189. }
  190. },
  191. imglongtap(e) {
  192. var attrs = e.item.dataset.attrs;
  193. if (!attrs.ignore)
  194. this.top.$emit('imglongtap', {
  195. id: e.target.id,
  196. src: attrs.src
  197. })
  198. },
  199. linkpress(e) {
  200. var jump = true,
  201. attrs = e.currentTarget.dataset.attrs;
  202. attrs.ignore = () => jump = false;
  203. this.top.$emit('linkpress', attrs);
  204. if (jump) {
  205. // #ifdef MP
  206. if (attrs['app-id']) {
  207. return uni.navigateToMiniProgram({
  208. appId: attrs['app-id'],
  209. path: attrs.path
  210. })
  211. }
  212. // #endif
  213. if (attrs.href) {
  214. if (attrs.href[0] == '#') {
  215. if (this.top.useAnchor)
  216. this.top.navigateTo({
  217. id: attrs.href.substring(1)
  218. })
  219. } else if (attrs.href.indexOf('http') == 0 || attrs.href.indexOf('//') == 0) {
  220. // #ifdef APP-PLUS
  221. if (attrs.href.includes('.doc') || attrs.href.includes('.xls') || attrs.href.includes('.ppt') || attrs.href.includes(
  222. '.pdf')) {
  223. uni.showLoading({
  224. title: '文件下载中'
  225. })
  226. uni.downloadFile({
  227. url: attrs.href,
  228. success(res) {
  229. wx.openDocument({
  230. filePath: res.tempFilePath
  231. })
  232. },
  233. complete: uni.hideLoading
  234. })
  235. } else
  236. // #endif
  237. uni.setClipboardData({
  238. data: attrs.href,
  239. success: () =>
  240. uni.showToast({
  241. title: '链接已复制'
  242. })
  243. });
  244. } else
  245. uni.navigateTo({
  246. url: attrs.href
  247. })
  248. }
  249. }
  250. },
  251. error(e) {
  252. var context, target = e.currentTarget,
  253. source = target.dataset.from;
  254. if (source == 'video' || source == 'audio') {
  255. // 加载其他 source
  256. var index = this.controls[target.id] ? this.controls[target.id].index + 1 : 1;
  257. if (index < target.dataset.source.length)
  258. this.$set(this.controls, target.id + '.index', index);
  259. if (source == 'video') context = uni.createVideoContext(target.id, this);
  260. }
  261. this.top && this.top.$emit('error', {
  262. source,
  263. target,
  264. errMsg: e.detail.errMsg,
  265. errCode: e.detail.errCode,
  266. context
  267. });
  268. },
  269. _loadVideo(e) {
  270. this.$set(this.controls, e.currentTarget.id, {
  271. play: true,
  272. index: 0
  273. })
  274. }
  275. }
  276. }
  277. </script>
  278. <style>
  279. /* 在这里引入自定义样式 */
  280. /* 链接和图片效果 */
  281. ._a {
  282. display: inline;
  283. color: #366092;
  284. word-break: break-all;
  285. padding: 1.5px 0 1.5px 0;
  286. }
  287. ._hover {
  288. opacity: 0.7;
  289. text-decoration: underline;
  290. }
  291. ._img {
  292. display: inline-block;
  293. text-indent: 0;
  294. }
  295. /* #ifdef MP-WEIXIN */
  296. :host {
  297. display: inline;
  298. }
  299. /* #endif */
  300. /* #ifdef MP */
  301. .interlayer {
  302. align-content: inherit;
  303. align-items: inherit;
  304. display: inherit;
  305. flex-direction: inherit;
  306. flex-wrap: inherit;
  307. justify-content: inherit;
  308. width: 100%;
  309. white-space: inherit;
  310. }
  311. /* #endif */
  312. ._b,
  313. ._strong {
  314. font-weight: bold;
  315. }
  316. ._blockquote,
  317. ._div,
  318. ._p,
  319. ._ol,
  320. ._ul,
  321. ._li {
  322. display: block;
  323. }
  324. ._code {
  325. font-family: monospace;
  326. }
  327. ._del {
  328. text-decoration: line-through;
  329. }
  330. ._em,
  331. ._i {
  332. font-style: italic;
  333. }
  334. ._h1 {
  335. font-size: 2em;
  336. }
  337. ._h2 {
  338. font-size: 1.5em;
  339. }
  340. ._h3 {
  341. font-size: 1.17em;
  342. }
  343. ._h5 {
  344. font-size: 0.83em;
  345. }
  346. ._h6 {
  347. font-size: 0.67em;
  348. }
  349. ._h1,
  350. ._h2,
  351. ._h3,
  352. ._h4,
  353. ._h5,
  354. ._h6 {
  355. display: block;
  356. font-weight: bold;
  357. }
  358. ._ins {
  359. text-decoration: underline;
  360. }
  361. ._li {
  362. flex: 1;
  363. width: 0;
  364. }
  365. ._ol-bef {
  366. margin-right: 5px;
  367. text-align: right;
  368. width: 36px;
  369. }
  370. ._ul-bef {
  371. line-height: normal;
  372. margin: 0 12px 0 23px;
  373. }
  374. ._ol-bef,
  375. ._ul_bef {
  376. flex: none;
  377. user-select: none;
  378. }
  379. ._ul-p1 {
  380. display: inline-block;
  381. height: 0.3em;
  382. line-height: 0.3em;
  383. overflow: hidden;
  384. width: 0.3em;
  385. }
  386. ._ul-p2 {
  387. border: 0.05em solid black;
  388. border-radius: 50%;
  389. display: inline-block;
  390. height: 0.23em;
  391. width: 0.23em;
  392. }
  393. ._q::before {
  394. content: '"';
  395. }
  396. ._q::after {
  397. content: '"';
  398. }
  399. ._sub {
  400. font-size: smaller;
  401. vertical-align: sub;
  402. }
  403. ._sup {
  404. font-size: smaller;
  405. vertical-align: super;
  406. }
  407. /* #ifndef MP-WEIXIN */
  408. ._abbr,
  409. ._b,
  410. ._code,
  411. ._del,
  412. ._em,
  413. ._i,
  414. ._ins,
  415. ._label,
  416. ._q,
  417. ._span,
  418. ._strong,
  419. ._sub,
  420. ._sup {
  421. display: inline;
  422. }
  423. /* #endif */
  424. /* #ifdef MP-WEIXIN || MP-QQ || MP-ALIPAY */
  425. .__bdo,
  426. .__bdi,
  427. .__ruby,
  428. .__rt,
  429. ._entity {
  430. display: inline-block;
  431. }
  432. /* #endif */
  433. ._video {
  434. background-color: black;
  435. display: inline-block;
  436. height: 225px;
  437. position: relative;
  438. width: 300px;
  439. }
  440. ._video::after {
  441. border-left-color: white;
  442. border-style: solid;
  443. border-width: 15px 0 15px 30px;
  444. content: '';
  445. left: 50%;
  446. margin: -15px 0 0 -15px;
  447. position: absolute;
  448. top: 50%;
  449. }
  450. </style>