node.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. <template>
  2. <view @tap="nodeTap" :id="attrs.id" :class="'_block _'+name+' '+attrs.class" :style="(ctrl.root?'border:1px solid black;padding:5px;display:block;':'')+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 && VUE2) -->
  9. <img v-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl['e'+i]?'border:1px dashed black;padding:3px;':'')+(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||('n'+i)" :class="'_img '+n.attrs.class" :style="(ctrl['e'+i]?'border:1px dashed black;padding:3px;':'')+(ctrl[i]===-1?'display:none;':'')+'width:'+(ctrl[i]||1)+'px;height:'+(ctrl['h'+i]||1)+'px;'+n.attrs.style" :src="n.attrs.src" :mode="!n.h?'widthFix':(!n.w?'heightFix':'')" :lazy-load="opts[0]" :webp="n.webp" :show-menu-by-longpress="!opts[5]&&opts[3]&&!n.attrs.ignore" :image-menu-prevent="opts[5]||!opts[3]||n.attrs.ignore" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap" @longpress="imgLongTap" />
  13. <!-- #endif -->
  14. <!-- #ifdef APP-PLUS && VUE3 -->
  15. <image v-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl['e'+i]?'border:1px dashed black;padding:3px;':'')+(ctrl[i]===-1?'display:none;':'')+'width:'+(ctrl[i]||1)+'px;'+n.attrs.style" :src="n.attrs.src||(ctrl.load?n.attrs['data-src']:'')" :mode="!n.h?'widthFix':(!n.w?'heightFix':'')" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap" @longpress="imgLongTap" />
  16. <!-- #endif -->
  17. <!-- 文本 -->
  18. <text v-else-if="n.type==='text'&&!ctrl['e'+i]" :data-i="i" :user-select="opts[4]" :decode="!opts[5]" @tap="editStart">{{n.text}}
  19. <text v-if="!n.text" style="color:gray">{{opts[6]||'请输入'}}</text>
  20. </text>
  21. <text v-else-if="n.type==='text'&&ctrl['e'+i]===1" :data-i="i" style="border:1px dashed black;min-width:50px;width:auto;padding:5px;display:block" @tap.stop="editStart">{{n.text}}
  22. <text v-if="!n.text" style="color:gray">{{opts[6]||'请输入'}}</text>
  23. </text>
  24. <textarea v-else-if="n.type==='text'" style="border:1px dashed black;min-width:50px;width:auto;padding:5px" auto-height maxlength="-1" :focus="ctrl['e'+i]===3" :value="n.text" :data-i="i" @input="editInput" @blur="editEnd" />
  25. <text v-else-if="n.name==='br'">\n</text>
  26. <!-- 链接 -->
  27. <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">
  28. <node name="span" :childs="n.children" :opts="[opts[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+'.'+i+'.children']" style="display:inherit" />
  29. </view>
  30. <!-- 视频 -->
  31. <!-- #ifdef APP-PLUS -->
  32. <view v-else-if="n.html" :data-i="i" @tap="mediaTap" :id="n.attrs.id" :class="'_video '+n.attrs.class" :style="n.attrs.style" v-html="n.html" @vplay.stop="play" />
  33. <!-- #endif -->
  34. <!-- #ifndef APP-PLUS -->
  35. <video :show-center-play-btn="!opts[5]" @tap="mediaTap" 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" :object-fit="n.attrs['object-fit']" :poster="n.attrs.poster" :src="n.src[ctrl[i]||0]" :data-i="i" @play="play" @error="mediaError" />
  36. <!-- #endif -->
  37. <!-- #ifdef H5 || APP-PLUS -->
  38. <iframe v-else-if="n.name==='iframe'" :style="n.attrs.style" :allowfullscreen="n.attrs.allowfullscreen" :frameborder="n.attrs.frameborder" :src="n.attrs.src" />
  39. <embed v-else-if="n.name==='embed'" :style="n.attrs.style" :src="n.attrs.src" />
  40. <!-- #endif -->
  41. <!-- #ifndef MP-TOUTIAO || ((H5 || APP-PLUS) && VUE3) -->
  42. <!-- 音频 -->
  43. <audio @tap="mediaTap" 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" />
  44. <!-- #endif -->
  45. <view v-else-if="(n.name==='table'&&(n.c||opts[5]))||n.name==='li'" :id="n.attrs.id" :class="'_'+n.name+' '+n.attrs.class" :style="n.attrs.style">
  46. <node v-if="n.name==='li'" :childs="n.children" :opts="[opts[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+'.'+i+'.children']" />
  47. <view v-else v-for="(tbody, x) in n.children" v-bind:key="x" :class="'_'+tbody.name+' '+tbody.attrs.class" :style="tbody.attrs.style">
  48. <node v-if="tbody.name==='td'||tbody.name==='th'" :childs="tbody.children" :opts="[opts[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+'.'+i+'.children.'+x+'.children']" />
  49. <block v-else v-for="(tr, y) in tbody.children" v-bind:key="y">
  50. <view v-if="tr.name==='td'||tr.name==='th'" :class="'_'+tr.name+' '+tr.attrs.class" :style="tr.attrs.style">
  51. <node :childs="tr.children" :opts="[opts[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+'.'+i+'.children.'+x+'.children.'+y+'.children']" />
  52. </view>
  53. <view v-else :class="'_'+tr.name+' '+tr.attrs.class" :style="tr.attrs.style">
  54. <view v-for="(td, z) in tr.children" v-bind:key="z" :class="'_'+td.name+' '+td.attrs.class" :style="td.attrs.style">
  55. <node :childs="td.children" :opts="[opts[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+'.'+i+'.children.'+x+'.children.'+y+'.children.'+z+'.children']" />
  56. </view>
  57. </view>
  58. </block>
  59. </view>
  60. </view>
  61. <!-- 富文本 -->
  62. <!-- #ifdef H5 || ((MP-WEIXIN || MP-QQ || APP-PLUS || MP-360) && VUE2) -->
  63. <rich-text v-else-if="!opts[5]&&!n.c&&!handler.isInline(n.name, n.attrs.style)" :id="n.attrs.id" :style="n.f" :user-select="opts[4]" :nodes="[n]" />
  64. <!-- #endif -->
  65. <!-- #ifndef H5 || ((MP-WEIXIN || MP-QQ || APP-PLUS || MP-360) && VUE2) -->
  66. <rich-text v-else-if="!opts[5]&&!n.c" :id="n.attrs.id" :style="n.f+';display:inline'" :preview="false" :selectable="opts[4]" :user-select="opts[4]" :nodes="[n]" />
  67. <!-- #endif -->
  68. <!-- 继续递归 -->
  69. <view v-else-if="n.c===2" :id="n.attrs.id" :class="'_block _'+n.name+' '+n.attrs.class" :style="n.f+';'+n.attrs.style">
  70. <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[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+'.'+i+'.children.'+j+'.children']" />
  71. </view>
  72. <node v-else :style="n.f" :name="n.name" :attrs="n.attrs" :childs="n.children" :opts="[opts[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+'.'+i+'.children']" />
  73. </block>
  74. </view>
  75. </template>
  76. <script module="handler" lang="wxs">
  77. // 行内标签列表
  78. var inlineTags = {
  79. abbr: true,
  80. b: true,
  81. big: true,
  82. code: true,
  83. del: true,
  84. em: true,
  85. i: true,
  86. ins: true,
  87. label: true,
  88. q: true,
  89. small: true,
  90. span: true,
  91. strong: true,
  92. sub: true,
  93. sup: true
  94. }
  95. /**
  96. * @description 判断是否为行内标签
  97. */
  98. module.exports = {
  99. isInline: function (tagName, style) {
  100. return inlineTags[tagName] || (style || '').indexOf('display:inline') !== -1
  101. }
  102. }
  103. </script>
  104. <script>
  105. function getTop(e) {
  106. let top
  107. // #ifdef H5 || APP-PLUS
  108. top = e.touches[0].pageY
  109. // #endif
  110. // #ifdef MP-ALIPAY
  111. top = e.detail.pageY
  112. // #endif
  113. // #ifndef H5 || MP-ALIPAY || APP-PLUS
  114. top = e.detail.y
  115. // #endif
  116. if (top - e.currentTarget.offsetTop < 150)
  117. top = e.currentTarget.offsetTop
  118. if (top < 30)
  119. top += 70
  120. return top - 30
  121. }
  122. import node from './node'
  123. export default {
  124. name: 'node',
  125. options: {
  126. // #ifdef MP-WEIXIN
  127. virtualHost: true,
  128. // #endif
  129. // #ifdef MP-TOUTIAO
  130. addGlobalClass: false
  131. // #endif
  132. },
  133. data () {
  134. return {
  135. ctrl: {}
  136. }
  137. },
  138. props: {
  139. name: String,
  140. attrs: {
  141. type: Object,
  142. default () {
  143. return {}
  144. }
  145. },
  146. childs: Array,
  147. opts: Array
  148. },
  149. components: {
  150. // #ifndef H5 && VUE3
  151. node
  152. // #endif
  153. },
  154. mounted () {
  155. this.$nextTick(() => {
  156. for (this.root = this.$parent; this.root.$options.name !== 'mp-html'; this.root = this.root.$parent);
  157. })
  158. // #ifdef H5 || APP-PLUS
  159. if (this.opts[0]) {
  160. let i
  161. for (i = this.childs.length; i--;) {
  162. if (this.childs[i].name === 'img') break
  163. }
  164. if (i !== -1) {
  165. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  166. top: 500,
  167. bottom: 500
  168. })
  169. this.observer.observe('._img', res => {
  170. if (res.intersectionRatio) {
  171. this.$set(this.ctrl, 'load', 1)
  172. this.observer.disconnect()
  173. }
  174. })
  175. }
  176. }
  177. // #endif
  178. },
  179. beforeDestroy () {
  180. if (this.root._edit === this) {
  181. this.root._edit = undefined
  182. }
  183. // #ifdef H5 || APP-PLUS
  184. if (this.observer) {
  185. this.observer.disconnect()
  186. }
  187. // #endif
  188. },
  189. methods:{editStart (e) {
  190. if (this.opts[5]) {
  191. const i = e.currentTarget.dataset.i
  192. if (!this.ctrl['e' + i]) {
  193. // 显示虚线框
  194. this.$set(this.ctrl, 'e' + i, 1)
  195. setTimeout(() => {
  196. this.root._mask.push(() => this.$set(this.ctrl, 'e' + i, 0))
  197. }, 50)
  198. this.root._edit = this
  199. this.i = i
  200. this.cursor = this.childs[i].text.length
  201. } else {
  202. this.root._mask.pop()
  203. this.root._maskTap()
  204. // 将 text 转为 textarea
  205. this.$set(this.ctrl, 'e' + i, 2)
  206. // 延时对焦,避免高度错误
  207. setTimeout(() => {
  208. this.$set(this.ctrl, 'e' + i, 3)
  209. }, 50)
  210. }
  211. }
  212. },editInput (e) {
  213. const i = e.target.dataset.i
  214. // 替换连续空格
  215. const value = e.detail.value.replace(/ {2,}/, $ => {
  216. let res = '\xa0'
  217. for (let i = 1; i < $.length; i++) {
  218. res += '\xa0'
  219. }
  220. return res
  221. })
  222. this.root._editVal(`${this.opts[7]}.${i}.text`, this.childs[i].text, value) // 记录编辑历史
  223. this.cursor = e.detail.cursor
  224. },editEnd (e) {
  225. const i = e.target.dataset.i
  226. this.$set(this.ctrl, 'e' + i, 0)
  227. // 更新到视图
  228. this.root._setData(`${this.opts[7]}.${i}.text`, e.detail.value.replace(/ {2}/g, '\xa0 '))
  229. if (e.detail.cursor !== undefined) {
  230. this.cursor = e.detail.cursor
  231. }
  232. },insert (node) {
  233. setTimeout(() => {
  234. const childs = this.childs.slice(0)
  235. if (!childs[this.i]) {
  236. childs.push(node)
  237. } else if (childs[this.i].text) {
  238. // 在文本中插入
  239. const text = childs[this.i].text
  240. const list = []
  241. if (this.cursor) {
  242. list.push({
  243. type: 'text',
  244. text: text.substring(0, this.cursor)
  245. })
  246. }
  247. list.push(node)
  248. if (this.cursor < text.length) {
  249. list.push({
  250. type: 'text',
  251. text: text.substring(this.cursor)
  252. })
  253. }
  254. childs.splice(this.i, 1, ...list)
  255. } else {
  256. childs.splice(parseInt(this.i) + 1, 0, node)
  257. }
  258. this.root._editVal(this.opts[7], this.childs, childs, true)
  259. this.i = parseInt(this.i) + 1
  260. }, 200)
  261. },remove (i) {
  262. const arr = this.childs.slice(0)
  263. const delEle = arr.splice(i, 1)[0]
  264. if (delEle.name === 'img' || delEle.name === 'video' || delEle.name === 'audio') {
  265. let src = delEle.attrs.src
  266. if (delEle.src) {
  267. src = delEle.src.length === 1 ? delEle.src[0] : delEle.src
  268. }
  269. this.root.$emit('remove', {
  270. type: delEle.name,
  271. src
  272. })
  273. }
  274. this.root._edit = undefined
  275. this.root._maskTap()
  276. this.root._editVal(this.opts[7], this.childs, arr, true)
  277. },nodeTap (e) {
  278. if (this.opts[5]) {
  279. if (this.root._lock) return
  280. this.root._lock = true
  281. setTimeout(() => {
  282. this.root._lock = false
  283. }, 50)
  284. if (this.ctrl['e' + this.i] === 3) return
  285. this.root._maskTap()
  286. this.root._edit = this
  287. let start = this.opts[7].lastIndexOf('children.')
  288. if (start !== -1) {
  289. start += 9
  290. } else {
  291. start = 6
  292. }
  293. const i = parseInt(this.opts[7].substring(start, this.opts[7].lastIndexOf('.children')))
  294. let parent = this.$parent
  295. while (parent && parent.$options.name !== 'node') {
  296. parent = parent.$parent
  297. }
  298. if (!parent || this.opts[7].length - parent.opts[7].length > 15) return
  299. // 显示实线框
  300. this.$set(this.ctrl, 'root', 1)
  301. this.root._mask.push(() => this.$set(this.ctrl, 'root', 0))
  302. if (this.childs.length === 1 && this.childs[0].type === 'text' && !this.ctrl.e0) {
  303. this.$set(this.ctrl, 'e0', 1)
  304. this.root._mask.push(() => this.$set(this.ctrl, 'e0', 0))
  305. this.i = 0
  306. this.cursor = this.childs[0].text.length
  307. }
  308. const items = this.root._getItem(parent.childs[i], i !== 0, i !== parent.childs.length - 1)
  309. this.root._tooltip({
  310. top: getTop(e),
  311. items,
  312. success: tapIndex => {
  313. if (items[tapIndex] === '大小') {
  314. // 改变字体大小
  315. const style = parent.childs[i].attrs.style || ''
  316. let value = style.match(/;font-size:([0-9]+)px/)
  317. if (value) {
  318. value = parseInt(value[1])
  319. } else {
  320. value = 16
  321. }
  322. this.root._slider({
  323. min: 10,
  324. max: 30,
  325. value,
  326. top: getTop(e),
  327. changing: val => {
  328. if (Math.abs(val - value) > 2) {
  329. // 字号变换超过 2 时更新到视图
  330. parent.changeStyle('font-size', i, val + 'px', value + 'px')
  331. value = e.detail.value
  332. }
  333. },
  334. change: val => {
  335. if (val !== value) {
  336. parent.changeStyle('font-size', i, val + 'px', value + 'px')
  337. }
  338. this.root._editVal(`${parent.opts[7]}.${i}.attrs.style`, style, parent.childs[i].attrs.style)
  339. }
  340. })
  341. } else if (items[tapIndex] === '上移' || items[tapIndex] === '下移') {
  342. const arr = parent.childs.slice(0)
  343. const item = arr[i]
  344. if (items[tapIndex] === '上移') {
  345. arr[i] = arr[i - 1]
  346. arr[i - 1] = item
  347. } else {
  348. arr[i] = arr[i + 1]
  349. arr[i + 1] = item
  350. }
  351. this.root._editVal(parent.opts[7], parent.childs, arr, true)
  352. } else if (items[tapIndex] === '删除') {
  353. parent.remove(i)
  354. } else {
  355. const style = parent.childs[i].attrs.style || ''
  356. let newStyle = ''
  357. const item = items[tapIndex]
  358. let name
  359. let value
  360. if (item === '斜体') {
  361. name = 'font-style'
  362. value = 'italic'
  363. } else if (item === '粗体') {
  364. name = 'font-weight'
  365. value = 'bold'
  366. } else if (item === '下划线') {
  367. name = 'text-decoration'
  368. value = 'underline'
  369. } else if (item === '居中') {
  370. name = 'text-align'
  371. value = 'center'
  372. } else if (item === '缩进') {
  373. name = 'text-indent'
  374. value = '2em'
  375. }
  376. if (style.includes(name + ':')) {
  377. // 已有则取消
  378. newStyle = style.replace(new RegExp(name + ':[^;]+'), '')
  379. } else {
  380. // 没有则添加
  381. newStyle = style + ';' + name + ':' + value
  382. }
  383. this.root._editVal(`${parent.opts[7]}.${i}.attrs.style`, style, newStyle, true)
  384. }
  385. }
  386. })
  387. }
  388. },mediaTap (e) {
  389. if (this.opts[5]) {
  390. const i = e.target.dataset.i
  391. const node = this.childs[i]
  392. const items = this.root._getItem(node)
  393. this.root._edit = this
  394. this.i = i
  395. this.root._tooltip({
  396. top: e.target.offsetTop - 30,
  397. items,
  398. success: tapIndex => {
  399. switch (items[tapIndex]) {
  400. case '封面':
  401. // 设置封面
  402. this.root.getSrc('img', node.attrs.poster || '').then(url => {
  403. this.root._editVal(`${this.opts[7]}.${i}.attrs.poster`, node.attrs.poster, url instanceof Array ? url[0] : url, true)
  404. }).catch(() => { })
  405. break
  406. case '删除':
  407. this.remove(i)
  408. break
  409. case '循环':
  410. case '不循环':
  411. // 切换循环播放
  412. this.root._setData(`${this.opts[7]}.${i}.attrs.loop`, !node.attrs.loop)
  413. uni.showToast({
  414. title: '成功'
  415. })
  416. break
  417. case '自动播放':
  418. case '不自动播放':
  419. // 切换自动播放播放
  420. this.root._setData(`${this.opts[7]}.${i}.attrs.autoplay`, !node.attrs.autoplay)
  421. uni.showToast({
  422. title: '成功'
  423. })
  424. break
  425. }
  426. }
  427. })
  428. // 避免上层出现点击态
  429. this.root._lock = true
  430. setTimeout(() => {
  431. this.root._lock = false
  432. }, 50)
  433. }
  434. },changeStyle (name, i, value, oldVal) {
  435. let style = this.childs[i].attrs.style || ''
  436. if (style.includes(';' + name + ':' + oldVal)) {
  437. // style 中已经有
  438. style = style.replace(';' + name + ':' + oldVal, ';' + name + ':' + value)
  439. } else {
  440. // 没有则新增
  441. style += ';' + name + ':' + value
  442. }
  443. this.root._setData(`${this.opts[7]}.${i}.attrs.style`, style)
  444. },
  445. // #ifdef MP-WEIXIN
  446. toJSON () { },
  447. // #endif
  448. /**
  449. * @description 播放视频事件
  450. * @param {Event} e
  451. */
  452. play (e) {
  453. this.root.$emit('play')
  454. // #ifndef APP-PLUS
  455. if (this.root.pauseVideo) {
  456. let flag = false
  457. const id = e.target.id
  458. for (let i = this.root._videos.length; i--;) {
  459. if (this.root._videos[i].id === id) {
  460. flag = true
  461. } else {
  462. this.root._videos[i].pause() // 自动暂停其他视频
  463. }
  464. }
  465. // 将自己加入列表
  466. if (!flag) {
  467. const ctx = uni.createVideoContext(id
  468. // #ifndef MP-BAIDU
  469. , this
  470. // #endif
  471. )
  472. ctx.id = id
  473. this.root._videos.push(ctx)
  474. }
  475. }
  476. // #endif
  477. },
  478. /**
  479. * @description 图片点击事件
  480. * @param {Event} e
  481. */
  482. imgTap (e) {
  483. if (!this.opts[5]) {
  484. const node = this.childs[e.currentTarget.dataset.i]
  485. if (node.a) {
  486. this.linkTap(node.a)
  487. return
  488. }
  489. if (node.attrs.ignore) return
  490. // #ifdef H5 || APP-PLUS
  491. node.attrs.src = node.attrs.src || node.attrs['data-src']
  492. // #endif
  493. this.root.$emit('imgtap', node.attrs)
  494. // 自动预览图片
  495. if (this.root.previewImg) {
  496. uni.previewImage({
  497. // #ifdef MP-WEIXIN
  498. showmenu: this.root.showImgMenu,
  499. // #endif
  500. // #ifdef MP-ALIPAY
  501. enablesavephoto: this.root.showImgMenu,
  502. enableShowPhotoDownload: this.root.showImgMenu,
  503. // #endif
  504. current: parseInt(node.attrs.i),
  505. urls: this.root.imgList
  506. })
  507. }
  508. } else {
  509. const i = e.currentTarget.dataset.i
  510. const node = this.childs[i]
  511. const items = this.root._getItem(node)
  512. this.root._edit = this
  513. this.i = i
  514. this.root._maskTap()
  515. this.$set(this.ctrl, 'e' + i, 1)
  516. this.root._mask.push(() => this.$set(this.ctrl, 'e' + i, 0))
  517. this.root._tooltip({
  518. top: getTop(e),
  519. items,
  520. success: tapIndex => {
  521. if (items[tapIndex] === '换图') {
  522. // 换图
  523. this.root.getSrc('img', node.attrs.src || '').then(url => {
  524. this.root._editVal(this.opts[7] + '.' + i + '.attrs.src', node.attrs.src, url instanceof Array ? url[0] : url, true)
  525. }).catch(() => { })
  526. } else if (items[tapIndex] === '宽度') {
  527. // 更改宽度
  528. const style = node.attrs.style || ''
  529. let value = style.match(/max-width:([0-9]+)%/)
  530. if (value) {
  531. value = parseInt(value[1])
  532. } else {
  533. value = 100
  534. }
  535. this.root._slider({
  536. min: 0,
  537. max: 100,
  538. value,
  539. top: getTop(e),
  540. changing: val => {
  541. // 变化超过 5% 更新时视图
  542. if (Math.abs(val - value) > 5) {
  543. this.changeStyle('max-width', i, val + '%', value + '%')
  544. value = val
  545. }
  546. },
  547. change: val => {
  548. if (val !== value) {
  549. this.changeStyle('max-width', i, val + '%', value + '%')
  550. value = val
  551. }
  552. this.root._editVal(this.opts[7] + '.' + i + '.attrs.style', style, this.childs[i].attrs.style)
  553. }
  554. })
  555. } else if (items[tapIndex] === '超链接') {
  556. // 将图片设置为链接
  557. this.root.getSrc('link', node.a ? node.a.href : '').then(url => {
  558. // 如果有 a 标签则替换 href
  559. if (node.a) {
  560. this.root._editVal(this.opts[7] + '.' + i + '.a.href', node.a.href, url, true)
  561. } else {
  562. const link = {
  563. name: 'a',
  564. attrs: {
  565. href: url
  566. },
  567. children: [node]
  568. }
  569. node.a = link.attrs
  570. this.root._editVal(this.opts[7] + '.' + i, node, link, true)
  571. }
  572. wx.showToast({
  573. title: '成功'
  574. })
  575. }).catch(() => { })
  576. } else if (items[tapIndex] === '预览图') {
  577. // 设置预览图链接
  578. this.root.getSrc('img', node.attrs['original-src'] || '').then(url => {
  579. this.root._editVal(this.opts[7] + '.' + i + '.attrs.original-src', node.attrs['original-src'], url instanceof Array ? url[0] : url, true)
  580. uni.showToast({
  581. title: '成功'
  582. })
  583. }).catch(() => { })
  584. } else if (items[tapIndex] === '删除') {
  585. this.remove(i)
  586. } else {
  587. // 禁用 / 启用预览
  588. this.root._setData(this.opts[7] + '.' + i + '.attrs.ignore', !node.attrs.ignore)
  589. uni.showToast({
  590. title: '成功'
  591. })
  592. }
  593. }
  594. })
  595. this.root._lock = true
  596. setTimeout(() => {
  597. this.root._lock = false
  598. }, 50)
  599. }
  600. },
  601. /**
  602. * @description 图片长按
  603. */
  604. imgLongTap (e) {
  605. // #ifdef APP-PLUS
  606. const attrs = this.childs[e.currentTarget.dataset.i].attrs
  607. if (this.opts[3] && !attrs.ignore) {
  608. uni.showActionSheet({
  609. itemList: ['保存图片'],
  610. success: () => {
  611. const save = path => {
  612. uni.saveImageToPhotosAlbum({
  613. filePath: path,
  614. success () {
  615. uni.showToast({
  616. title: '保存成功'
  617. })
  618. }
  619. })
  620. }
  621. if (this.root.imgList[attrs.i].startsWith('http')) {
  622. uni.downloadFile({
  623. url: this.root.imgList[attrs.i],
  624. success: res => save(res.tempFilePath)
  625. })
  626. } else {
  627. save(this.root.imgList[attrs.i])
  628. }
  629. }
  630. })
  631. }
  632. // #endif
  633. },
  634. /**
  635. * @description 图片加载完成事件
  636. * @param {Event} e
  637. */
  638. imgLoad(e) {
  639. // #ifdef MP-WEIXIN || MP-QQ
  640. if (this.opts[5])
  641. this.$nextTick(() => {
  642. const id = this.childs[i].attrs.id || ('n' + i)
  643. uni.createSelectorQuery().in(this).select('#' + id).boundingClientRect().exec(res => {
  644. this.$set(this.ctrl, 'h'+i, res[0].height)
  645. })
  646. })
  647. // #endif
  648. const i = e.currentTarget.dataset.i
  649. /* #ifndef H5 || (APP-PLUS && VUE2) */
  650. if (!this.childs[i].w) {
  651. this.$set(this.ctrl, i, e.detail.width)
  652. if (this.opts[5]) {
  653. const path = this.opts[7] + '.' + i + '.attrs.'
  654. if (e.detail.width < 150)
  655. this.root._setData(path + 'ignore', 'T')
  656. this.root._setData(path + 'width', e.detail.width.toString())
  657. }
  658. } else /* #endif */ if ((this.opts[1] && !this.ctrl[i]) || this.ctrl[i] === -1) {
  659. // 加载完毕,取消加载中占位图
  660. this.$set(this.ctrl, i, 1)
  661. }
  662. },
  663. /**
  664. * @description 链接点击事件
  665. * @param {Event} e
  666. */
  667. linkTap (e) {
  668. if (!this.opts[5]) {
  669. const node = e.currentTarget ? this.childs[e.currentTarget.dataset.i] : {}
  670. const attrs = node.attrs || e
  671. const href = attrs.href
  672. this.root.$emit('linktap', Object.assign({
  673. innerText: this.root.getText(node.children || []) // 链接内的文本内容
  674. }, attrs))
  675. if (href) {
  676. if (href[0] === '#') {
  677. // 跳转锚点
  678. this.root.navigateTo(href.substring(1)).catch(() => { })
  679. } else if (href.split('?')[0].includes('://')) {
  680. // 复制外部链接
  681. if (this.root.copyLink) {
  682. // #ifdef H5
  683. window.open(href)
  684. // #endif
  685. // #ifdef MP
  686. uni.setClipboardData({
  687. data: href,
  688. success: () =>
  689. uni.showToast({
  690. title: '链接已复制'
  691. })
  692. })
  693. // #endif
  694. // #ifdef APP-PLUS
  695. plus.runtime.openWeb(href)
  696. // #endif
  697. }
  698. } else {
  699. // 跳转页面
  700. uni.navigateTo({
  701. url: href,
  702. fail () {
  703. uni.switchTab({
  704. url: href,
  705. fail () { }
  706. })
  707. }
  708. })
  709. }
  710. }
  711. } else {
  712. const i = e.currentTarget.dataset.i
  713. const node = this.childs[i]
  714. const items = this.root._getItem(node)
  715. this.root._tooltip({
  716. top: getTop(e),
  717. items,
  718. success: tapIndex => {
  719. if (items[tapIndex] === '更换链接') {
  720. this.root.getSrc('link', node.attrs.href).then(url => {
  721. this.root._editVal(this.opts[7] + '.' + i + '.attrs.href', node.attrs.href, url, true)
  722. uni.showToast({
  723. title: '成功'
  724. })
  725. }).catch(() => { })
  726. } else {
  727. this.remove(i)
  728. }
  729. }
  730. })
  731. }
  732. },
  733. /**
  734. * @description 错误事件
  735. * @param {Event} e
  736. */
  737. mediaError (e) {
  738. const i = e.currentTarget.dataset.i
  739. const node = this.childs[i]
  740. // 加载其他源
  741. if (node.name === 'video' || node.name === 'audio') {
  742. let index = (this.ctrl[i] || 0) + 1
  743. if (index > node.src.length) {
  744. index = 0
  745. }
  746. if (index < node.src.length) {
  747. this.$set(this.ctrl, i, index)
  748. return
  749. }
  750. } else if (node.name === 'img') {
  751. // #ifdef H5 && VUE3
  752. if (this.opts[0] && !this.ctrl.load) return
  753. // #endif
  754. // 显示错误占位图
  755. if (this.opts[2]) {
  756. this.$set(this.ctrl, i, -1)
  757. }
  758. }
  759. if (this.root) {
  760. this.root.$emit('error', {
  761. source: node.name,
  762. attrs: node.attrs,
  763. // #ifndef H5 && VUE3
  764. errMsg: e.detail.errMsg
  765. // #endif
  766. })
  767. }
  768. }
  769. }
  770. }
  771. </script>
  772. <style>/* #ifndef H5 || MP-ALIPAY || APP-PLUS */
  773. /deep/ ._address,
  774. /deep/ ._article,
  775. /deep/ ._aside,
  776. /deep/ ._body,
  777. /deep/ ._caption,
  778. /deep/ ._center,
  779. /deep/ ._cite,
  780. /deep/ ._footer,
  781. /deep/ ._header,
  782. /deep/ ._html,
  783. /deep/ ._nav,
  784. /deep/ ._pre,
  785. /deep/ ._section {
  786. display: block;
  787. }
  788. /* #endif */
  789. /deep/ ._video {
  790. width: 300px;
  791. height: 225px;
  792. display: inline-block;
  793. background-color: black;
  794. }
  795. /* a 标签默认效果 */
  796. ._a {
  797. padding: 1.5px 0 1.5px 0;
  798. color: #366092;
  799. word-break: break-all;
  800. }
  801. /* a 标签点击态效果 */
  802. ._hover {
  803. text-decoration: underline;
  804. opacity: 0.7;
  805. }
  806. /* 图片默认效果 */
  807. ._img {
  808. max-width: 100%;
  809. -webkit-touch-callout: none;
  810. }
  811. /* 内部样式 */
  812. ._block {
  813. display: block;
  814. }
  815. ._b,
  816. ._strong {
  817. font-weight: bold;
  818. }
  819. ._code {
  820. font-family: monospace;
  821. }
  822. ._del {
  823. text-decoration: line-through;
  824. }
  825. ._em,
  826. ._i {
  827. font-style: italic;
  828. }
  829. ._h1 {
  830. font-size: 2em;
  831. }
  832. ._h2 {
  833. font-size: 1.5em;
  834. }
  835. ._h3 {
  836. font-size: 1.17em;
  837. }
  838. ._h5 {
  839. font-size: 0.83em;
  840. }
  841. ._h6 {
  842. font-size: 0.67em;
  843. }
  844. ._h1,
  845. ._h2,
  846. ._h3,
  847. ._h4,
  848. ._h5,
  849. ._h6 {
  850. display: block;
  851. font-weight: bold;
  852. }
  853. ._image {
  854. height: 1px;
  855. }
  856. ._ins {
  857. text-decoration: underline;
  858. }
  859. ._li {
  860. display: list-item;
  861. }
  862. ._ol {
  863. list-style-type: decimal;
  864. }
  865. ._ol,
  866. ._ul {
  867. display: block;
  868. padding-left: 40px;
  869. margin: 1em 0;
  870. }
  871. ._q::before {
  872. content: '"';
  873. }
  874. ._q::after {
  875. content: '"';
  876. }
  877. ._sub {
  878. font-size: smaller;
  879. vertical-align: sub;
  880. }
  881. ._sup {
  882. font-size: smaller;
  883. vertical-align: super;
  884. }
  885. ._thead,
  886. ._tbody,
  887. ._tfoot {
  888. display: table-row-group;
  889. }
  890. ._tr {
  891. display: table-row;
  892. }
  893. ._td,
  894. ._th {
  895. display: table-cell;
  896. vertical-align: middle;
  897. }
  898. ._th {
  899. font-weight: bold;
  900. text-align: center;
  901. }
  902. ._ul {
  903. list-style-type: disc;
  904. }
  905. ._ul ._ul {
  906. margin: 0;
  907. list-style-type: circle;
  908. }
  909. ._ul ._ul ._ul {
  910. list-style-type: square;
  911. }
  912. ._abbr,
  913. ._b,
  914. ._code,
  915. ._del,
  916. ._em,
  917. ._i,
  918. ._ins,
  919. ._label,
  920. ._q,
  921. ._span,
  922. ._strong,
  923. ._sub,
  924. ._sup {
  925. display: inline;
  926. }
  927. /* #ifdef APP-PLUS */
  928. ._video {
  929. width: 300px;
  930. height: 225px;
  931. }
  932. /* #endif */
  933. </style>