trees.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/04/13
  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. // +----------------------------------------------------------------------
  121. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  122. // +----------------------------------------------------------------------
  123. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  124. // +----------------------------------------------------------------------
  125. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  126. // +----------------------------------------------------------------------
  127. // | Author: CRMEB Team <admin@crmeb.com>
  128. // +----------------------------------------------------------------------
  129. global.Parser = {};
  130. import trees from './trees'
  131. export default {
  132. components: {
  133. trees
  134. },
  135. name: 'trees',
  136. data() {
  137. return {
  138. controls: {},
  139. // #ifdef MP-WEIXIN || MP-QQ || APP-PLUS
  140. imgLoad: false,
  141. // #endif
  142. // #ifndef APP-PLUS
  143. loadVideo: true
  144. // #endif
  145. }
  146. },
  147. props: {
  148. nodes: Array,
  149. // #ifdef MP-WEIXIN || MP-QQ || H5 || APP-PLUS
  150. lazyLoad: Boolean,
  151. // #endif
  152. // #ifdef APP-PLUS
  153. loadVideo: Boolean
  154. // #endif
  155. },
  156. mounted() {
  157. // 获取顶层组件
  158. this.top = this.$parent;
  159. while (this.top.$options.name != 'parser') {
  160. if (this.top.top) {
  161. this.top = this.top.top;
  162. break;
  163. }
  164. this.top = this.top.$parent;
  165. }
  166. },
  167. // #ifdef MP-WEIXIN || MP-QQ || APP-PLUS
  168. beforeDestroy() {
  169. if (this.observer)
  170. this.observer.disconnect();
  171. },
  172. // #endif
  173. methods: {
  174. // #ifndef MP-ALIPAY
  175. play(e) {
  176. if (this.top.videoContexts.length > 1 && this.top.autopause)
  177. for (var i = this.top.videoContexts.length; i--;)
  178. if (this.top.videoContexts[i].id != e.currentTarget.dataset.id)
  179. this.top.videoContexts[i].pause();
  180. },
  181. // #endif
  182. imgtap(e) {
  183. var attrs = e.currentTarget.dataset.attrs;
  184. if (!attrs.ignore) {
  185. var preview = true, data = {
  186. id: e.target.id,
  187. src: attrs.src,
  188. ignore: () => preview = false
  189. };
  190. global.Parser.onImgtap && global.Parser.onImgtap(data);
  191. this.top.$emit('imgtap', data);
  192. if (preview) {
  193. var urls = this.top.imgList,
  194. current = urls[attrs.i] ? parseInt(attrs.i) : (urls = [attrs.src], 0);
  195. uni.previewImage({
  196. current,
  197. urls
  198. })
  199. }
  200. }
  201. },
  202. imglongtap(e) {
  203. var attrs = e.item.dataset.attrs;
  204. if (!attrs.ignore)
  205. this.top.$emit('imglongtap', {
  206. id: e.target.id,
  207. src: attrs.src
  208. })
  209. },
  210. linkpress(e) {
  211. var jump = true,
  212. attrs = e.currentTarget.dataset.attrs;
  213. attrs.ignore = () => jump = false;
  214. global.Parser.onLinkpress && global.Parser.onLinkpress(attrs);
  215. this.top.$emit('linkpress', attrs);
  216. if (jump) {
  217. // #ifdef MP
  218. if (attrs['app-id']) {
  219. return uni.navigateToMiniProgram({
  220. appId: attrs['app-id'],
  221. path: attrs.path
  222. })
  223. }
  224. // #endif
  225. if (attrs.href) {
  226. if (attrs.href[0] == '#') {
  227. if (this.top.useAnchor)
  228. this.top.navigateTo({
  229. id: attrs.href.substring(1)
  230. })
  231. } else if (attrs.href.indexOf('http') == 0 || attrs.href.indexOf('//') == 0) {
  232. // #ifdef APP-PLUS
  233. plus.runtime.openWeb(attrs.href);
  234. // #endif
  235. // #ifndef APP-PLUS
  236. uni.setClipboardData({
  237. data: attrs.href,
  238. success: () =>
  239. uni.showToast({
  240. title: '链接已复制'
  241. })
  242. })
  243. // #endif
  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-color: transparent transparent transparent 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. video{
  451. width: 100%;
  452. }
  453. </style>