trees.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. global.Parser = {};
  121. import trees from './trees'
  122. export default {
  123. components: {
  124. trees
  125. },
  126. name: 'trees',
  127. data() {
  128. return {
  129. controls: {},
  130. // #ifdef MP-WEIXIN || MP-QQ || APP-PLUS
  131. imgLoad: false,
  132. // #endif
  133. // #ifndef APP-PLUS
  134. loadVideo: true
  135. // #endif
  136. }
  137. },
  138. props: {
  139. nodes: Array,
  140. // #ifdef MP-WEIXIN || MP-QQ || H5 || APP-PLUS
  141. lazyLoad: Boolean,
  142. // #endif
  143. // #ifdef APP-PLUS
  144. loadVideo: Boolean
  145. // #endif
  146. },
  147. mounted() {
  148. // 获取顶层组件
  149. this.top = this.$parent;
  150. while (this.top.$options.name != 'parser') {
  151. if (this.top.top) {
  152. this.top = this.top.top;
  153. break;
  154. }
  155. this.top = this.top.$parent;
  156. }
  157. },
  158. // #ifdef MP-WEIXIN || MP-QQ || APP-PLUS
  159. beforeDestroy() {
  160. if (this.observer)
  161. this.observer.disconnect();
  162. },
  163. // #endif
  164. methods: {
  165. // #ifndef MP-ALIPAY
  166. play(e) {
  167. if (this.top.videoContexts.length > 1 && this.top.autopause)
  168. for (var i = this.top.videoContexts.length; i--;)
  169. if (this.top.videoContexts[i].id != e.currentTarget.dataset.id)
  170. this.top.videoContexts[i].pause();
  171. },
  172. // #endif
  173. imgtap(e) {
  174. var attrs = e.currentTarget.dataset.attrs;
  175. if (!attrs.ignore) {
  176. var preview = true, data = {
  177. id: e.target.id,
  178. src: attrs.src,
  179. ignore: () => preview = false
  180. };
  181. global.Parser.onImgtap && global.Parser.onImgtap(data);
  182. this.top.$emit('imgtap', data);
  183. if (preview) {
  184. var urls = this.top.imgList,
  185. current = urls[attrs.i] ? parseInt(attrs.i) : (urls = [attrs.src], 0);
  186. uni.previewImage({
  187. current,
  188. urls
  189. })
  190. }
  191. }
  192. },
  193. imglongtap(e) {
  194. var attrs = e.item.dataset.attrs;
  195. if (!attrs.ignore)
  196. this.top.$emit('imglongtap', {
  197. id: e.target.id,
  198. src: attrs.src
  199. })
  200. },
  201. linkpress(e) {
  202. var jump = true,
  203. attrs = e.currentTarget.dataset.attrs;
  204. attrs.ignore = () => jump = false;
  205. global.Parser.onLinkpress && global.Parser.onLinkpress(attrs);
  206. this.top.$emit('linkpress', attrs);
  207. if (jump) {
  208. // #ifdef MP
  209. if (attrs['app-id']) {
  210. return uni.navigateToMiniProgram({
  211. appId: attrs['app-id'],
  212. path: attrs.path
  213. })
  214. }
  215. // #endif
  216. if (attrs.href) {
  217. if (attrs.href[0] == '#') {
  218. if (this.top.useAnchor)
  219. this.top.navigateTo({
  220. id: attrs.href.substring(1)
  221. })
  222. } else if (attrs.href.indexOf('http') == 0 || attrs.href.indexOf('//') == 0) {
  223. // #ifdef APP-PLUS
  224. plus.runtime.openWeb(attrs.href);
  225. // #endif
  226. // #ifndef APP-PLUS
  227. uni.setClipboardData({
  228. data: attrs.href,
  229. success: () =>
  230. uni.showToast({
  231. title: '链接已复制'
  232. })
  233. })
  234. // #endif
  235. } else
  236. uni.navigateTo({
  237. url: attrs.href
  238. })
  239. }
  240. }
  241. },
  242. error(e) {
  243. var context, target = e.currentTarget,
  244. source = target.dataset.from;
  245. if (source == 'video' || source == 'audio') {
  246. // 加载其他 source
  247. var index = this.controls[target.id] ? this.controls[target.id].index + 1 : 1;
  248. if (index < target.dataset.source.length)
  249. this.$set(this.controls, target.id + '.index', index);
  250. if (source == 'video') context = uni.createVideoContext(target.id, this);
  251. }
  252. this.top && this.top.$emit('error', {
  253. source,
  254. target,
  255. errMsg: e.detail.errMsg,
  256. errCode: e.detail.errCode,
  257. context
  258. });
  259. },
  260. _loadVideo(e) {
  261. this.$set(this.controls, e.currentTarget.id, {
  262. play: true,
  263. index: 0
  264. })
  265. }
  266. }
  267. }
  268. </script>
  269. <style>
  270. /* 在这里引入自定义样式 */
  271. /* 链接和图片效果 */
  272. ._a {
  273. display: inline;
  274. color: #366092;
  275. word-break: break-all;
  276. padding: 1.5px 0 1.5px 0;
  277. }
  278. ._hover {
  279. opacity: 0.7;
  280. text-decoration: underline;
  281. }
  282. ._img {
  283. display: inline-block;
  284. text-indent: 0;
  285. }
  286. /* #ifdef MP-WEIXIN */
  287. :host {
  288. display: inline;
  289. }
  290. /* #endif */
  291. /* #ifdef MP */
  292. .interlayer {
  293. align-content: inherit;
  294. align-items: inherit;
  295. display: inherit;
  296. flex-direction: inherit;
  297. flex-wrap: inherit;
  298. justify-content: inherit;
  299. width: 100%;
  300. white-space: inherit;
  301. }
  302. /* #endif */
  303. ._b,
  304. ._strong {
  305. font-weight: bold;
  306. }
  307. ._blockquote,
  308. ._div,
  309. ._p,
  310. ._ol,
  311. ._ul,
  312. ._li {
  313. display: block;
  314. }
  315. ._code {
  316. font-family: monospace;
  317. }
  318. ._del {
  319. text-decoration: line-through;
  320. }
  321. ._em,
  322. ._i {
  323. font-style: italic;
  324. }
  325. ._h1 {
  326. font-size: 2em;
  327. }
  328. ._h2 {
  329. font-size: 1.5em;
  330. }
  331. ._h3 {
  332. font-size: 1.17em;
  333. }
  334. ._h5 {
  335. font-size: 0.83em;
  336. }
  337. ._h6 {
  338. font-size: 0.67em;
  339. }
  340. ._h1,
  341. ._h2,
  342. ._h3,
  343. ._h4,
  344. ._h5,
  345. ._h6 {
  346. display: block;
  347. font-weight: bold;
  348. }
  349. ._ins {
  350. text-decoration: underline;
  351. }
  352. ._li {
  353. flex: 1;
  354. width: 0;
  355. }
  356. ._ol-bef {
  357. margin-right: 5px;
  358. text-align: right;
  359. width: 36px;
  360. }
  361. ._ul-bef {
  362. line-height: normal;
  363. margin: 0 12px 0 23px;
  364. }
  365. ._ol-bef,
  366. ._ul_bef {
  367. flex: none;
  368. user-select: none;
  369. }
  370. ._ul-p1 {
  371. display: inline-block;
  372. height: 0.3em;
  373. line-height: 0.3em;
  374. overflow: hidden;
  375. width: 0.3em;
  376. }
  377. ._ul-p2 {
  378. border: 0.05em solid black;
  379. border-radius: 50%;
  380. display: inline-block;
  381. height: 0.23em;
  382. width: 0.23em;
  383. }
  384. ._q::before {
  385. content: '"';
  386. }
  387. ._q::after {
  388. content: '"';
  389. }
  390. ._sub {
  391. font-size: smaller;
  392. vertical-align: sub;
  393. }
  394. ._sup {
  395. font-size: smaller;
  396. vertical-align: super;
  397. }
  398. /* #ifndef MP-WEIXIN */
  399. ._abbr,
  400. ._b,
  401. ._code,
  402. ._del,
  403. ._em,
  404. ._i,
  405. ._ins,
  406. ._label,
  407. ._q,
  408. ._span,
  409. ._strong,
  410. ._sub,
  411. ._sup {
  412. display: inline;
  413. }
  414. /* #endif */
  415. /* #ifdef MP-WEIXIN || MP-QQ || MP-ALIPAY */
  416. .__bdo,
  417. .__bdi,
  418. .__ruby,
  419. .__rt,
  420. ._entity {
  421. display: inline-block;
  422. }
  423. /* #endif */
  424. ._video {
  425. background-color: black;
  426. display: inline-block;
  427. height: 225px;
  428. position: relative;
  429. width: 300px;
  430. }
  431. ._video::after {
  432. border-color: transparent transparent transparent white;
  433. border-style: solid;
  434. border-width: 15px 0 15px 30px;
  435. content: '';
  436. left: 50%;
  437. margin: -15px 0 0 -15px;
  438. position: absolute;
  439. top: 50%;
  440. }
  441. </style>