You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

499 lines
12 KiB

9 months ago
  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. -webkit-touch-callout: none;
  340. }
  341. /* 内部样式 */
  342. ._b,
  343. ._strong {
  344. font-weight: bold;
  345. }
  346. ._code {
  347. font-family: monospace;
  348. }
  349. ._del {
  350. text-decoration: line-through;
  351. }
  352. ._em,
  353. ._i {
  354. font-style: italic;
  355. }
  356. ._h1 {
  357. font-size: 2em;
  358. }
  359. ._h2 {
  360. font-size: 1.5em;
  361. }
  362. ._h3 {
  363. font-size: 1.17em;
  364. }
  365. ._h5 {
  366. font-size: 0.83em;
  367. }
  368. ._h6 {
  369. font-size: 0.67em;
  370. }
  371. ._h1,
  372. ._h2,
  373. ._h3,
  374. ._h4,
  375. ._h5,
  376. ._h6 {
  377. display: block;
  378. font-weight: bold;
  379. }
  380. ._image {
  381. height: 1px;
  382. }
  383. ._ins {
  384. text-decoration: underline;
  385. }
  386. ._li {
  387. display: list-item;
  388. }
  389. ._ol {
  390. list-style-type: decimal;
  391. }
  392. ._ol,
  393. ._ul {
  394. display: block;
  395. padding-left: 40px;
  396. margin: 1em 0;
  397. }
  398. ._q::before {
  399. content: '"';
  400. }
  401. ._q::after {
  402. content: '"';
  403. }
  404. ._sub {
  405. font-size: smaller;
  406. vertical-align: sub;
  407. }
  408. ._sup {
  409. font-size: smaller;
  410. vertical-align: super;
  411. }
  412. ._thead,
  413. ._tbody,
  414. ._tfoot {
  415. display: table-row-group;
  416. }
  417. ._tr {
  418. display: table-row;
  419. }
  420. ._td,
  421. ._th {
  422. display: table-cell;
  423. vertical-align: middle;
  424. }
  425. ._th {
  426. font-weight: bold;
  427. text-align: center;
  428. }
  429. ._ul {
  430. list-style-type: disc;
  431. }
  432. ._ul ._ul {
  433. margin: 0;
  434. list-style-type: circle;
  435. }
  436. ._ul ._ul ._ul {
  437. list-style-type: square;
  438. }
  439. ._abbr,
  440. ._b,
  441. ._code,
  442. ._del,
  443. ._em,
  444. ._i,
  445. ._ins,
  446. ._label,
  447. ._q,
  448. ._span,
  449. ._strong,
  450. ._sub,
  451. ._sup {
  452. display: inline;
  453. }
  454. /* #ifdef APP-PLUS */
  455. ._video {
  456. width: 300px;
  457. height: 225px;
  458. }
  459. /* #endif */
  460. </style>