node.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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" @click.capture.stop="linkTap">
  21. <!-- #ifdef MP-ALIPAY || MP-TOUTIAO -->
  22. <rich-text :nodes="n.children" style="display:inline" />
  23. <!-- #endif -->
  24. <!-- #ifndef MP-ALIPAY || MP-TOUTIAO -->
  25. <node name="span" :childs="n.children" :opts="opts" />
  26. <!-- #endif -->
  27. </view>
  28. <!-- 视频 -->
  29. <!-- #ifdef APP-PLUS -->
  30. <view v-else-if="n.html" :id="n.attrs.id" :class="n.attrs.class" :style="n.attrs.style" v-html="n.html" />
  31. <!-- #endif -->
  32. <!-- #ifndef APP-PLUS -->
  33. <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" />
  34. <!-- #endif -->
  35. <!-- #ifdef H5 || APP-PLUS -->
  36. <iframe v-else-if="n.name=='iframe'" :style="n.attrs.style" :allowfullscreen="n.attrs.allowfullscreen" :frameborder="n.attrs.frameborder" :src="n.attrs.src" />
  37. <embed v-else-if="n.name=='embed'" :style="n.attrs.style" :src="n.attrs.src" />
  38. <!-- #endif -->
  39. <!-- #ifndef MP-TOUTIAO -->
  40. <!-- 音频 -->
  41. <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" />
  42. <!-- #endif -->
  43. <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">
  44. <node v-if="n.name=='li'" :childs="n.children" :opts="opts" />
  45. <view v-else v-for="(tbody, x) in n.children" v-bind:key="x" :class="'_'+tbody.name+' '+tbody.attrs.class" :style="tbody.attrs.style">
  46. <node v-if="tbody.name=='td'||tbody.name=='th'" :childs="tbody.children" :opts="opts" />
  47. <block v-else v-for="(tr, y) in tbody.children" v-bind:key="y">
  48. <view v-if="tr.name=='td'||tr.name=='th'" :class="'_'+tr.name+' '+tr.attrs.class" :style="tr.attrs.style">
  49. <node :childs="tr.children" :opts="opts" />
  50. </view>
  51. <view v-else :class="'_'+tr.name+' '+tr.attrs.class" :style="tr.attrs.style">
  52. <view v-for="(td, z) in tr.children" v-bind:key="z" :class="'_'+td.name+' '+td.attrs.class" :style="td.attrs.style">
  53. <node :childs="td.children" :opts="opts" />
  54. </view>
  55. </view>
  56. </block>
  57. </view>
  58. </view>
  59. <!-- 富文本 -->
  60. <!-- #ifdef H5 || MP-WEIXIN || MP-QQ || APP-PLUS || MP-360 -->
  61. <rich-text v-else-if="handler.use(n)" :id="n.attrs.id" :style="n.f" :nodes="[n]" />
  62. <!-- #endif -->
  63. <!-- #ifndef H5 || MP-WEIXIN || MP-QQ || APP-PLUS || MP-360 -->
  64. <rich-text v-else-if="!n.c" :id="n.attrs.id" :style="n.f+';display:inline'" :preview="false" :nodes="[n]" />
  65. <!-- #endif -->
  66. <!-- 继续递归 -->
  67. <view v-else-if="n.c==2" :id="n.attrs.id" :class="'_'+n.name+' '+n.attrs.class" :style="n.f+';'+n.attrs.style">
  68. <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" />
  69. </view>
  70. <node v-else :style="n.f" :name="n.name" :attrs="n.attrs" :childs="n.children" :opts="opts" />
  71. </block>
  72. </view>
  73. </template>
  74. <script module="handler" lang="wxs">
  75. // 行内标签列表
  76. var inlineTags = {
  77. abbr: true,
  78. b: true,
  79. big: true,
  80. code: true,
  81. del: true,
  82. em: true,
  83. i: true,
  84. ins: true,
  85. label: true,
  86. q: true,
  87. small: true,
  88. span: true,
  89. strong: true,
  90. sub: true,
  91. sup: true
  92. }
  93. /**
  94. * @description 是否使用 rich-text 显示剩余内容
  95. */
  96. module.exports = {
  97. use: function (item) {
  98. // 微信和 QQ 的 rich-text inline 布局无效
  99. if (inlineTags[item.name] || (item.attrs.style || '').indexOf('display:inline') != -1)
  100. return false
  101. return !item.c
  102. }
  103. }
  104. </script>
  105. <script>
  106. import node from './node'
  107. export default {
  108. name: 'node',
  109. // #ifdef MP-WEIXIN
  110. options: {
  111. virtualHost: true
  112. },
  113. // #endif
  114. data() {
  115. return {
  116. ctrl: {}
  117. }
  118. },
  119. props: {
  120. name: String,
  121. attrs: {
  122. type: Object,
  123. default() {
  124. return {}
  125. }
  126. },
  127. childs: Array,
  128. opts: Array
  129. },
  130. components: {
  131. node
  132. },
  133. mounted() {
  134. for (this.root = this.$parent; this.root.$options.name != 'mp-html'; this.root = this.root.$parent);
  135. // #ifdef H5 || APP-PLUS
  136. if (this.opts[0]) {
  137. for (var i = this.childs.length; i--;)
  138. if (this.childs[i].name == 'img')
  139. break
  140. if (i != -1) {
  141. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  142. top: 500,
  143. bottom: 500
  144. })
  145. this.observer.observe('._img', res => {
  146. if (res.intersectionRatio) {
  147. this.$set(this.ctrl, 'load', 1)
  148. this.observer.disconnect()
  149. }
  150. })
  151. }
  152. }
  153. // #endif
  154. },
  155. beforeDestroy() {
  156. // #ifdef H5 || APP-PLUS
  157. if (this.observer)
  158. this.observer.disconnect()
  159. // #endif
  160. },
  161. methods:{
  162. // #ifdef MP-WEIXIN
  163. toJSON() { },
  164. // #endif
  165. /**
  166. * @description 播放视频事件
  167. * @param {Event} e
  168. */
  169. play(e) {
  170. // #ifndef APP-PLUS
  171. if (this.root.pauseVideo) {
  172. var flag = false, id = e.target.id
  173. for (var i = this.root._videos.length; i--;) {
  174. if (this.root._videos[i].id == id)
  175. flag = true
  176. else
  177. this.root._videos[i].pause() // 自动暂停其他视频
  178. }
  179. // 将自己加入列表
  180. if (!flag) {
  181. var ctx = uni.createVideoContext(id
  182. // #ifndef MP-BAIDU
  183. , this
  184. // #endif
  185. )
  186. ctx.id = id
  187. this.root._videos.push(ctx)
  188. }
  189. }
  190. // #endif
  191. },
  192. /**
  193. * @description 图片点击事件
  194. * @param {Event} e
  195. */
  196. imgTap(e) {
  197. var attrs = this.childs[e.currentTarget.dataset.i].attrs
  198. if (attrs.ignore)
  199. return
  200. attrs.src = attrs['data-src'] || attrs.src
  201. this.root.$emit('imgtap', attrs)
  202. // 自动预览图片
  203. if (this.root.previewImg)
  204. uni.previewImage({
  205. current: parseInt(attrs.i),
  206. urls: this.root.imgList
  207. })
  208. },
  209. /**
  210. * @description 图片长按
  211. */
  212. imgLongTap() {
  213. // #ifdef APP-PLUS
  214. var attrs = this.childs[e.currentTarget.dataset.i].attrs
  215. if (!attrs.ignore)
  216. uni.showActionSheet({
  217. itemList: ['保存图片'],
  218. success: () => {
  219. uni.downloadFile({
  220. url: this.root.imgList[attrs.i],
  221. success: res => {
  222. uni.saveImageToPhotosAlbum({
  223. filePath: res.tempFilePath,
  224. success() {
  225. uni.showToast({
  226. title: '保存成功'
  227. })
  228. }
  229. })
  230. }
  231. })
  232. }
  233. })
  234. // #endif
  235. },
  236. /**
  237. * @description 图片加载完成事件
  238. * @param {Event} e
  239. */
  240. imgLoad(e) {
  241. var i = e.currentTarget.dataset.i
  242. // #ifndef H5 || APP-PLUS
  243. // 设置原宽度
  244. if (!this.childs[i].w)
  245. this.$set(this.ctrl, i, e.detail.width)
  246. else
  247. // #endif
  248. // 加载完毕,取消加载中占位图
  249. if ((this.opts[1] && !this.ctrl[i]) || this.ctrl[i] == -1)
  250. this.$set(this.ctrl, i, 1)
  251. },
  252. /**
  253. * @description 链接点击事件
  254. * @param {Event} e
  255. */
  256. linkTap(e) {
  257. var attrs = this.childs[e.currentTarget.dataset.i].attrs,
  258. href = attrs.href
  259. this.root.$emit('linktap', attrs)
  260. if (href) {
  261. // 跳转锚点
  262. if (href[0] == '#')
  263. this.root.navigateTo(href.substring(1)).catch(() => { })
  264. // 复制外部链接
  265. else if (href.includes('://')) {
  266. if (this.root.copyLink) {
  267. // #ifdef H5
  268. window.open(href)
  269. // #endif
  270. // #ifdef MP
  271. uni.setClipboardData({
  272. data: href,
  273. success: () =>
  274. uni.showToast({
  275. title: '链接已复制'
  276. })
  277. })
  278. // #endif
  279. // #ifdef APP-PLUS
  280. plus.runtime.openWeb(href)
  281. // #endif
  282. }
  283. }
  284. // 跳转页面
  285. else
  286. uni.navigateTo({
  287. url: href,
  288. fail() {
  289. uni.switchTab({
  290. url: href,
  291. fail() { }
  292. })
  293. }
  294. })
  295. }
  296. },
  297. /**
  298. * @description 错误事件
  299. * @param {Event} e
  300. */
  301. mediaError(e) {
  302. var i = e.currentTarget.dataset.i,
  303. node = this.childs[i]
  304. // 加载其他源
  305. if (node.name == 'video' || node.name == 'audio') {
  306. var index = (this.ctrl[i] || 0) + 1
  307. if (index > node.src.length)
  308. index = 0
  309. if (index < node.src.length)
  310. return this.$set(this.ctrl, i, index)
  311. }
  312. // 显示错误占位图
  313. else if (node.name == 'img' && this.opts[2])
  314. this.$set(this.ctrl, i, -1)
  315. if (this.root)
  316. this.root.$emit('error', {
  317. source: node.name,
  318. attrs: node.attrs,
  319. errMsg: e.detail.errMsg
  320. })
  321. }
  322. }
  323. }
  324. </script>
  325. <style>
  326. /* a 标签默认效果 */
  327. ._a {
  328. padding: 1.5px 0 1.5px 0;
  329. color: #366092;
  330. word-break: break-all;
  331. }
  332. /* a 标签点击态效果 */
  333. ._hover {
  334. text-decoration: underline;
  335. opacity: 0.7;
  336. }
  337. /* 图片默认效果 */
  338. ._img {
  339. max-width: 100%;
  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. </style>