node.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 -->
  16. <text v-else-if="n.type=='text'" 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. // 微信和 QQ 的 rich-text inline 布局无效
  94. if (inlineTags[item.name] || (item.attrs.style || '').indexOf('display:inline') != -1)
  95. return false
  96. return !item.c
  97. }
  98. }
  99. </script>
  100. <script>
  101. import node from './node'
  102. export default {
  103. name: 'node',
  104. // #ifdef MP-WEIXIN
  105. options: {
  106. virtualHost: true
  107. },
  108. // #endif
  109. data() {
  110. return {
  111. ctrl: {}
  112. }
  113. },
  114. props: {
  115. name: String,
  116. attrs: {
  117. type: Object,
  118. default() {
  119. return {}
  120. }
  121. },
  122. childs: Array,
  123. opts: Array
  124. },
  125. components: {
  126. node
  127. },
  128. mounted() {
  129. for (this.root = this.$parent; this.root.$options.name != 'mp-html'; this.root = this.root.$parent);
  130. // #ifdef H5 || APP-PLUS
  131. if (this.opts[0]) {
  132. for (var i = this.childs.length; i--;)
  133. if (this.childs[i].name == 'img')
  134. break
  135. if (i != -1) {
  136. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  137. top: 500,
  138. bottom: 500
  139. })
  140. this.observer.observe('._img', res => {
  141. if (res.intersectionRatio) {
  142. this.$set(this.ctrl, 'load', 1)
  143. this.observer.disconnect()
  144. }
  145. })
  146. }
  147. }
  148. // #endif
  149. },
  150. beforeDestroy() {
  151. // #ifdef H5 || APP-PLUS
  152. if (this.observer)
  153. this.observer.disconnect()
  154. // #endif
  155. },
  156. methods:{
  157. // #ifdef MP-WEIXIN
  158. toJSON() { },
  159. // #endif
  160. /**
  161. * @description 播放视频事件
  162. * @param {Event} e
  163. */
  164. play(e) {
  165. // #ifndef APP-PLUS
  166. if (this.root.pauseVideo) {
  167. var flag = false, id = e.target.id
  168. for (var i = this.root._videos.length; i--;) {
  169. if (this.root._videos[i].id == id)
  170. flag = true
  171. else
  172. this.root._videos[i].pause() // 自动暂停其他视频
  173. }
  174. // 将自己加入列表
  175. if (!flag) {
  176. var ctx = uni.createVideoContext(id
  177. // #ifndef MP-BAIDU
  178. , this
  179. // #endif
  180. )
  181. ctx.id = id
  182. this.root._videos.push(ctx)
  183. }
  184. }
  185. // #endif
  186. },
  187. /**
  188. * @description 图片点击事件
  189. * @param {Event} e
  190. */
  191. imgTap(e) {
  192. var node = this.childs[e.currentTarget.dataset.i]
  193. if (node.a)
  194. return this.linkTap(node.a)
  195. if (node.attrs.ignore)
  196. return
  197. // #ifdef H5 || APP-PLUS
  198. node.attrs.src = node.attrs.src || node.attrs['data-src']
  199. // #endif
  200. this.root.$emit('imgtap', node.attrs)
  201. // 自动预览图片
  202. if (this.root.previewImg)
  203. uni.previewImage({
  204. current: parseInt(node.attrs.i),
  205. urls: this.root.imgList
  206. })
  207. },
  208. /**
  209. * @description 图片长按
  210. */
  211. imgLongTap(e) {
  212. // #ifdef APP-PLUS
  213. var attrs = this.childs[e.currentTarget.dataset.i].attrs
  214. if (!attrs.ignore)
  215. uni.showActionSheet({
  216. itemList: ['保存图片'],
  217. success: () => {
  218. uni.downloadFile({
  219. url: this.root.imgList[attrs.i],
  220. success: res => {
  221. uni.saveImageToPhotosAlbum({
  222. filePath: res.tempFilePath,
  223. success() {
  224. uni.showToast({
  225. title: '保存成功'
  226. })
  227. }
  228. })
  229. }
  230. })
  231. }
  232. })
  233. // #endif
  234. },
  235. /**
  236. * @description 图片加载完成事件
  237. * @param {Event} e
  238. */
  239. imgLoad(e) {
  240. var i = e.currentTarget.dataset.i
  241. // #ifndef H5 || APP-PLUS
  242. // 设置原宽度
  243. if (!this.childs[i].w)
  244. this.$set(this.ctrl, i, e.detail.width)
  245. else
  246. // #endif
  247. // 加载完毕,取消加载中占位图
  248. if ((this.opts[1] && !this.ctrl[i]) || this.ctrl[i] == -1)
  249. this.$set(this.ctrl, i, 1)
  250. },
  251. /**
  252. * @description 链接点击事件
  253. * @param {Event} e
  254. */
  255. linkTap(e) {
  256. var attrs = e.currentTarget ? this.childs[e.currentTarget.dataset.i].attrs : e,
  257. href = attrs.href
  258. this.root.$emit('linktap', attrs)
  259. if (href) {
  260. // 跳转锚点
  261. if (href[0] == '#')
  262. this.root.navigateTo(href.substring(1)).catch(() => { })
  263. // 复制外部链接
  264. else if (href.includes('://')) {
  265. if (this.root.copyLink) {
  266. // #ifdef H5
  267. window.open(href)
  268. // #endif
  269. // #ifdef MP
  270. uni.setClipboardData({
  271. data: href,
  272. success: () =>
  273. uni.showToast({
  274. title: '链接已复制'
  275. })
  276. })
  277. // #endif
  278. // #ifdef APP-PLUS
  279. plus.runtime.openWeb(href)
  280. // #endif
  281. }
  282. }
  283. // 跳转页面
  284. else
  285. uni.navigateTo({
  286. url: href,
  287. fail() {
  288. uni.switchTab({
  289. url: href,
  290. fail() { }
  291. })
  292. }
  293. })
  294. }
  295. },
  296. /**
  297. * @description 错误事件
  298. * @param {Event} e
  299. */
  300. mediaError(e) {
  301. var i = e.currentTarget.dataset.i,
  302. node = this.childs[i]
  303. // 加载其他源
  304. if (node.name == 'video' || node.name == 'audio') {
  305. var index = (this.ctrl[i] || 0) + 1
  306. if (index > node.src.length)
  307. index = 0
  308. if (index < node.src.length)
  309. return this.$set(this.ctrl, i, index)
  310. }
  311. // 显示错误占位图
  312. else if (node.name == 'img' && this.opts[2])
  313. this.$set(this.ctrl, i, -1)
  314. if (this.root)
  315. this.root.$emit('error', {
  316. source: node.name,
  317. attrs: node.attrs,
  318. errMsg: e.detail.errMsg
  319. })
  320. }
  321. }
  322. }
  323. </script>
  324. <style>
  325. /* a 标签默认效果 */
  326. ._a {
  327. padding: 1.5px 0 1.5px 0;
  328. color: #366092;
  329. word-break: break-all;
  330. }
  331. /* a 标签点击态效果 */
  332. ._hover {
  333. text-decoration: underline;
  334. opacity: 0.7;
  335. }
  336. /* 图片默认效果 */
  337. ._img {
  338. max-width: 100%;
  339. width: 100% !important;
  340. -webkit-touch-callout: none;
  341. }
  342. /* 内部样式 */
  343. ._b,
  344. ._strong {
  345. font-weight: bold;
  346. }
  347. ._code {
  348. font-family: monospace;
  349. }
  350. ._del {
  351. text-decoration: line-through;
  352. }
  353. ._em,
  354. ._i {
  355. font-style: italic;
  356. }
  357. ._h1 {
  358. font-size: 2em;
  359. }
  360. ._h2 {
  361. font-size: 1.5em;
  362. }
  363. ._h3 {
  364. font-size: 1.17em;
  365. }
  366. ._h5 {
  367. font-size: 0.83em;
  368. }
  369. ._h6 {
  370. font-size: 0.67em;
  371. }
  372. ._h1,
  373. ._h2,
  374. ._h3,
  375. ._h4,
  376. ._h5,
  377. ._h6 {
  378. display: block;
  379. font-weight: bold;
  380. }
  381. ._image {
  382. height: 1px;
  383. }
  384. ._ins {
  385. text-decoration: underline;
  386. }
  387. ._li {
  388. display: list-item;
  389. }
  390. ._ol {
  391. list-style-type: decimal;
  392. }
  393. ._ol,
  394. ._ul {
  395. display: block;
  396. padding-left: 40px;
  397. margin: 1em 0;
  398. }
  399. ._q::before {
  400. content: '"';
  401. }
  402. ._q::after {
  403. content: '"';
  404. }
  405. ._sub {
  406. font-size: smaller;
  407. vertical-align: sub;
  408. }
  409. ._sup {
  410. font-size: smaller;
  411. vertical-align: super;
  412. }
  413. ._thead,
  414. ._tbody,
  415. ._tfoot {
  416. display: table-row-group;
  417. }
  418. ._tr {
  419. display: table-row;
  420. }
  421. ._td,
  422. ._th {
  423. display: table-cell;
  424. vertical-align: middle;
  425. }
  426. ._th {
  427. font-weight: bold;
  428. text-align: center;
  429. }
  430. ._ul {
  431. list-style-type: disc;
  432. }
  433. ._ul ._ul {
  434. margin: 0;
  435. list-style-type: circle;
  436. }
  437. ._ul ._ul ._ul {
  438. list-style-type: square;
  439. }
  440. ._abbr,
  441. ._b,
  442. ._code,
  443. ._del,
  444. ._em,
  445. ._i,
  446. ._ins,
  447. ._label,
  448. ._q,
  449. ._span,
  450. ._strong,
  451. ._sub,
  452. ._sup {
  453. display: inline;
  454. }
  455. ._p{
  456. /* margin-bottom: -5px; */
  457. }
  458. .ql-video{
  459. /* margin-bottom: -5px; */
  460. width: 100vw;
  461. }
  462. text{
  463. font-size: 14px;
  464. }
  465. rich-text{
  466. font-size: 14px;
  467. }
  468. .ql-video.ql-align-center {
  469. margin: 0 auto
  470. }
  471. .ql-video.ql-align-right {
  472. margin: 0 0 0 auto
  473. }
  474. .ql-bg-black {
  475. background-color: #000
  476. }
  477. .ql-bg-red {
  478. background-color: #e60000
  479. }
  480. .ql-bg-orange {
  481. background-color: #f90
  482. }
  483. .ql-bg-yellow {
  484. background-color: #ff0
  485. }
  486. .ql-bg-green {
  487. background-color: #008a00
  488. }
  489. .ql-bg-blue {
  490. background-color: #06c
  491. }
  492. .ql-bg-purple {
  493. background-color: #93f
  494. }
  495. .ql-color-white {
  496. color: #fff
  497. }
  498. .ql-color-red {
  499. color: #e60000
  500. }
  501. .ql-color-orange {
  502. color: #f90
  503. }
  504. .ql-color-yellow {
  505. color: #ff0
  506. }
  507. .ql-color-green {
  508. color: #008a00
  509. }
  510. .ql-color-blue {
  511. color: #06c
  512. }
  513. .ql-color-purple {
  514. color: #93f
  515. }
  516. .ql-font-serif {
  517. font-family: Georgia,Times New Roman,serif
  518. }
  519. .ql-font-monospace {
  520. font-family: Monaco,Courier New,monospace
  521. }
  522. .ql-size-small {
  523. font-size: .75em
  524. }
  525. .ql-size-large {
  526. font-size: 1.5em
  527. }
  528. .ql-size-huge {
  529. font-size: 2.5em
  530. }
  531. .ql-direction-rtl {
  532. direction: rtl;
  533. text-align: inherit
  534. }
  535. .ql-align-center {
  536. text-align: center
  537. }
  538. .ql-align-justify {
  539. text-align: justify
  540. }
  541. .ql-align-right {
  542. text-align: right
  543. }
  544. /* #ifdef APP-PLUS */
  545. ._video {
  546. width: 300px;
  547. height: 225px;
  548. }
  549. /* #endif */
  550. </style>