build.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /* global getTop */
  2. module.exports = {
  3. style: `/* #ifndef H5 || MP-ALIPAY || APP-PLUS */
  4. ._address,
  5. ._article,
  6. ._aside,
  7. ._body,
  8. ._caption,
  9. ._center,
  10. ._cite,
  11. ._footer,
  12. ._header,
  13. ._html,
  14. ._nav,
  15. ._pre,
  16. ._section {
  17. display: block;
  18. }
  19. /* #endif */
  20. ._video {
  21. width: 300px;
  22. height: 225px;
  23. display: inline-block;
  24. background-color: black;
  25. }`,
  26. methods: {
  27. /**
  28. * @description 开始编辑文本
  29. * @param {Event} e
  30. */
  31. editStart (e) {
  32. if (this.opts[5]) {
  33. const i = e.currentTarget.dataset.i
  34. if (!this.ctrl['e' + i]) {
  35. // 显示虚线框
  36. this.$set(this.ctrl, 'e' + i, 1)
  37. setTimeout(() => {
  38. this.root._mask.push(() => this.$set(this.ctrl, 'e' + i, 0))
  39. }, 50)
  40. this.root._edit = this
  41. this.i = i
  42. this.cursor = this.childs[i].text.length
  43. } else {
  44. this.root._mask.pop()
  45. this.root._maskTap()
  46. // 将 text 转为 textarea
  47. this.$set(this.ctrl, 'e' + i, 2)
  48. // 延时对焦,避免高度错误
  49. setTimeout(() => {
  50. this.$set(this.ctrl, 'e' + i, 3)
  51. }, 50)
  52. }
  53. }
  54. },
  55. /**
  56. * @description 输入文本
  57. * @param {Event} e
  58. */
  59. editInput (e) {
  60. const i = e.target.dataset.i
  61. // 替换连续空格
  62. const value = e.detail.value.replace(/ {2,}/, $ => {
  63. let res = '\xa0'
  64. for (let i = 1; i < $.length; i++) {
  65. res += '\xa0'
  66. }
  67. return res
  68. })
  69. this.root._editVal(`${this.opts[7]}.${i}.text`, this.childs[i].text, value) // 记录编辑历史
  70. this.cursor = e.detail.cursor
  71. },
  72. /**
  73. * @description 完成编辑文本
  74. * @param {Event} e
  75. */
  76. editEnd (e) {
  77. const i = e.target.dataset.i
  78. this.$set(this.ctrl, 'e' + i, 0)
  79. // 更新到视图
  80. this.root._setData(`${this.opts[7]}.${i}.text`, e.detail.value.replace(/ {2}/g, '\xa0 '))
  81. if (e.detail.cursor !== undefined) {
  82. this.cursor = e.detail.cursor
  83. }
  84. },
  85. /**
  86. * @description 插入一个标签
  87. * @param {Object} node 要插入的标签
  88. */
  89. insert (node) {
  90. setTimeout(() => {
  91. const childs = this.childs.slice(0)
  92. if (!childs[this.i]) {
  93. childs.push(node)
  94. } else if (childs[this.i].text) {
  95. // 在文本中插入
  96. const text = childs[this.i].text
  97. if (node.type === 'text') {
  98. if (this.cursor) {
  99. childs[this.i].text = text.substring(0, this.cursor) + node.text + text.substring(this.cursor)
  100. } else {
  101. childs[this.i].text += node.text
  102. }
  103. } else {
  104. const list = []
  105. if (this.cursor) {
  106. list.push({
  107. type: 'text',
  108. text: text.substring(0, this.cursor)
  109. })
  110. }
  111. list.push(node)
  112. if (this.cursor < text.length) {
  113. list.push({
  114. type: 'text',
  115. text: text.substring(this.cursor)
  116. })
  117. }
  118. childs.splice(this.i, 1, ...list)
  119. }
  120. } else {
  121. childs.splice(parseInt(this.i) + 1, 0, node)
  122. }
  123. this.root._editVal(this.opts[7], this.childs, childs, true)
  124. this.i = parseInt(this.i) + 1
  125. }, 200)
  126. },
  127. /**
  128. * @description 移除第 i 个标签
  129. * @param {Number} i
  130. */
  131. remove (i) {
  132. const arr = this.childs.slice(0)
  133. const delEle = arr.splice(i, 1)[0]
  134. if (delEle.name === 'img' || delEle.name === 'video' || delEle.name === 'audio') {
  135. let src = delEle.attrs.src
  136. if (delEle.src) {
  137. src = delEle.src.length === 1 ? delEle.src[0] : delEle.src
  138. }
  139. this.root.$emit('remove', {
  140. type: delEle.name,
  141. src
  142. })
  143. }
  144. this.root._edit = undefined
  145. this.root._maskTap()
  146. this.root._editVal(this.opts[7], this.childs, arr, true)
  147. },
  148. /**
  149. * @description 标签被点击
  150. * @param {Event} e
  151. */
  152. nodeTap (e) {
  153. if (this.opts[5]) {
  154. if (this.root._lock) return
  155. this.root._lock = true
  156. setTimeout(() => {
  157. this.root._lock = false
  158. }, 50)
  159. if (this.ctrl['e' + this.i] === 3) return
  160. this.root._maskTap()
  161. this.root._edit = this
  162. let start = this.opts[7].lastIndexOf('children.')
  163. if (start !== -1) {
  164. start += 9
  165. } else {
  166. start = 6
  167. }
  168. const i = parseInt(this.opts[7].substring(start, this.opts[7].lastIndexOf('.children')))
  169. let parent = this.$parent
  170. while (parent && parent.$options.name !== 'node') {
  171. parent = parent.$parent
  172. }
  173. if (!parent || this.opts[7].length - parent.opts[7].length > 15) return
  174. // 显示实线框
  175. this.$set(this.ctrl, 'root', 1)
  176. this.root._mask.push(() => this.$set(this.ctrl, 'root', 0))
  177. if (this.childs.length === 1 && this.childs[0].type === 'text' && !this.ctrl.e0) {
  178. this.$set(this.ctrl, 'e0', 1)
  179. this.root._mask.push(() => this.$set(this.ctrl, 'e0', 0))
  180. this.i = 0
  181. this.cursor = this.childs[0].text.length
  182. }
  183. const items = this.root._getItem(parent.childs[i], i !== 0, i !== parent.childs.length - 1)
  184. this.root._tooltip({
  185. top: getTop(e),
  186. items,
  187. success: tapIndex => {
  188. if (items[tapIndex] === '大小') {
  189. // 改变字体大小
  190. const style = parent.childs[i].attrs.style || ''
  191. let value = style.match(/;font-size:([0-9]+)px/)
  192. if (value) {
  193. value = parseInt(value[1])
  194. } else {
  195. value = 16
  196. }
  197. this.root._slider({
  198. min: 10,
  199. max: 30,
  200. value,
  201. top: getTop(e),
  202. changing: val => {
  203. if (Math.abs(val - value) > 2) {
  204. // 字号变换超过 2 时更新到视图
  205. parent.changeStyle('font-size', i, val + 'px', value + 'px')
  206. value = e.detail.value
  207. }
  208. },
  209. change: val => {
  210. if (val !== value) {
  211. parent.changeStyle('font-size', i, val + 'px', value + 'px')
  212. }
  213. this.root._editVal(`${parent.opts[7]}.${i}.attrs.style`, style, parent.childs[i].attrs.style)
  214. }
  215. })
  216. } else if (items[tapIndex] === '上移' || items[tapIndex] === '下移') {
  217. const arr = parent.childs.slice(0)
  218. const item = arr[i]
  219. if (items[tapIndex] === '上移') {
  220. arr[i] = arr[i - 1]
  221. arr[i - 1] = item
  222. } else {
  223. arr[i] = arr[i + 1]
  224. arr[i + 1] = item
  225. }
  226. this.root._editVal(parent.opts[7], parent.childs, arr, true)
  227. } else if (items[tapIndex] === '删除') {
  228. parent.remove(i)
  229. } else {
  230. const style = parent.childs[i].attrs.style || ''
  231. let newStyle = ''
  232. const item = items[tapIndex]
  233. let name
  234. let value
  235. if (item === '斜体') {
  236. name = 'font-style'
  237. value = 'italic'
  238. } else if (item === '粗体') {
  239. name = 'font-weight'
  240. value = 'bold'
  241. } else if (item === '下划线') {
  242. name = 'text-decoration'
  243. value = 'underline'
  244. } else if (item === '居中') {
  245. name = 'text-align'
  246. value = 'center'
  247. } else if (item === '缩进') {
  248. name = 'text-indent'
  249. value = '2em'
  250. }
  251. if (style.includes(name + ':')) {
  252. // 已有则取消
  253. newStyle = style.replace(new RegExp(name + ':[^;]+'), '')
  254. } else {
  255. // 没有则添加
  256. newStyle = style + ';' + name + ':' + value
  257. }
  258. this.root._editVal(`${parent.opts[7]}.${i}.attrs.style`, style, newStyle, true)
  259. }
  260. }
  261. })
  262. }
  263. },
  264. /**
  265. * @description 音视频被点击
  266. * @param {Event} e
  267. */
  268. mediaTap (e) {
  269. if (this.opts[5]) {
  270. const i = e.target.dataset.i
  271. const node = this.childs[i]
  272. const items = this.root._getItem(node)
  273. this.root._edit = this
  274. this.i = i
  275. this.root._tooltip({
  276. top: e.target.offsetTop - 30,
  277. items,
  278. success: tapIndex => {
  279. switch (items[tapIndex]) {
  280. case '封面':
  281. // 设置封面
  282. this.root.getSrc('img', node.attrs.poster || '').then(url => {
  283. this.root._editVal(`${this.opts[7]}.${i}.attrs.poster`, node.attrs.poster, url instanceof Array ? url[0] : url, true)
  284. }).catch(() => { })
  285. break
  286. case '删除':
  287. this.remove(i)
  288. break
  289. case '循环':
  290. case '不循环':
  291. // 切换循环播放
  292. this.root._setData(`${this.opts[7]}.${i}.attrs.loop`, !node.attrs.loop)
  293. uni.showToast({
  294. title: '成功'
  295. })
  296. break
  297. case '自动播放':
  298. case '不自动播放':
  299. // 切换自动播放播放
  300. this.root._setData(`${this.opts[7]}.${i}.attrs.autoplay`, !node.attrs.autoplay)
  301. uni.showToast({
  302. title: '成功'
  303. })
  304. break
  305. }
  306. }
  307. })
  308. // 避免上层出现点击态
  309. this.root._lock = true
  310. setTimeout(() => {
  311. this.root._lock = false
  312. }, 50)
  313. }
  314. },
  315. /**
  316. * 改变样式
  317. * @param {String} name 属性名
  318. * @param {Number} i 第几个标签
  319. * @param {String} value 新值
  320. * @param {String} oldVal 旧值
  321. */
  322. changeStyle (name, i, value, oldVal) {
  323. let style = this.childs[i].attrs.style || ''
  324. if (style.includes(';' + name + ':' + oldVal)) {
  325. // style 中已经有
  326. style = style.replace(';' + name + ':' + oldVal, ';' + name + ':' + value)
  327. } else {
  328. // 没有则新增
  329. style += ';' + name + ':' + value
  330. }
  331. this.root._setData(`${this.opts[7]}.${i}.attrs.style`, style)
  332. }
  333. },
  334. handler (file) {
  335. if (file.isBuffer()) {
  336. let content = file.contents.toString()
  337. if (file.path.includes('mp-html.vue')) {
  338. // 传递 editable 属性和路径
  339. content = content.replace(/opts\s*=\s*"\[([^\]]+)\]"/, 'opts="[$1,editable,placeholder,\'nodes\']"')
  340. .replace(/<view(.*?):style\s*=\s*"containerStyle"/, '<view$1:style="(editable?\'min-height:200px;\':\'\')+containerStyle" @tap="_containTap"')
  341. // 工具弹窗
  342. .replace(/<\/view>\s*<\/template>/, ` <view v-if="tooltip" class="_tooltip_contain" :style="'top:'+tooltip.top+'px'">
  343. <view class="_tooltip">
  344. <view v-for="(item, index) in tooltip.items" v-bind:key="index" class="_tooltip_item" :data-i="index" @tap="_tooltipTap">{{item}}</view>
  345. </view>
  346. </view>
  347. <view v-if="slider" class="_slider" :style="'top:'+slider.top+'px'">
  348. <slider :value="slider.value" :min="slider.min" :max="slider.max" handle-size="14" block-size="14" show-value activeColor="white" style="padding:3px" @changing="_sliderChanging" @change="_sliderChange" />
  349. </view>
  350. </view>
  351. </template>`)
  352. // 添加 data
  353. .replace(/data\s*\(\)\s*{\s*return\s*{/, `data() {
  354. return {
  355. tooltip: null,
  356. slider: null,`)
  357. // 添加 editable 属性
  358. .replace(/props\s*:\s*{/, `props: {
  359. editable: Boolean,
  360. placeholder: String,`)
  361. // 添加 watch
  362. .replace(/watch\s*:\s*{/, `watch: {
  363. editable(val) {
  364. this.setContent(val ? this.content : this.getContent())
  365. if (!val)
  366. this._maskTap()
  367. },`)
  368. .replace(/if\s*\(this.content/, 'if ((this.content || this.editable)')
  369. // 处理各类弹窗的事件
  370. .replace(/methods\s*:\s*{/, `methods: {
  371. _containTap() {
  372. if (!this._lock && !this.slider) {
  373. this._edit = undefined
  374. this._maskTap()
  375. }
  376. },
  377. _tooltipTap(e) {
  378. this._tooltipcb(e.currentTarget.dataset.i)
  379. this.$set(this, 'tooltip', null)
  380. },
  381. _sliderChanging(e) {
  382. this._slideringcb(e.detail.value)
  383. },
  384. _sliderChange(e) {
  385. this._slidercb(e.detail.value)
  386. },`)
  387. // 工具弹窗的样式
  388. .replace('</style>', `
  389. /* 提示条 */
  390. ._tooltip_contain {
  391. position: absolute;
  392. right: 20px;
  393. left: 20px;
  394. text-align: center;
  395. }
  396. ._tooltip {
  397. box-sizing: border-box;
  398. display: inline-block;
  399. width: auto;
  400. max-width: 100%;
  401. height: 30px;
  402. padding: 0 3px;
  403. overflow: scroll;
  404. font-size: 14px;
  405. line-height: 30px;
  406. white-space: nowrap;
  407. }
  408. ._tooltip_item {
  409. display: inline-block;
  410. width: auto;
  411. padding: 0 2vw;
  412. line-height: 30px;
  413. background-color: black;
  414. color: white;
  415. }
  416. /* 图片宽度滚动条 */
  417. ._slider {
  418. position: absolute;
  419. left: 20px;
  420. width: 220px;
  421. }
  422. ._tooltip,
  423. ._slider {
  424. background-color: black;
  425. border-radius: 3px;
  426. opacity: 0.75;
  427. }
  428. </style>`)
  429. } else if (file.path.includes('parser.js')) {
  430. // 不做 expose 处理
  431. content = content.replace(/parser.prototype.expose\s*=\s*function\s*\(\)\s*{/, `parser.prototype.expose = function () {
  432. if (this.options.editable) return`)
  433. .replace(/popNode\s*=\s*function\s*\(\)\s*{/, 'popNode = function () {\n const editable = this.options.editable')
  434. // 不转换标签名
  435. .replace(/if\s*\(config.blockTags\[node.name\]\)\s*{[\s\S]+?}/, `if (config.blockTags[node.name]) {
  436. if (!editable) {
  437. node.name = 'div'
  438. }
  439. }`)
  440. // 转换表格和列表
  441. .replace(/else\s*if\s*\(node.c\)/, 'else if (!editable && node.c )')
  442. .replace(/node.c(\)|\s*&&|\s*\n)/g, '(node.c || editable)$1')
  443. .replace(/while\s*\(map\[row\s*\+\s*'.'\s*\+\s*col\]\)\s*{[\s\S]+?}/, `while (map[row + '.' + col]) {
  444. col++
  445. }
  446. if (editable) {
  447. td.r = row
  448. }`)
  449. .replace(/let\s+str\s*=\s*'<video style="width:100%;height:100%"'/, `let str = '<video style="width:100%;height:100%"'
  450. if (editable) {
  451. attrs.controls = ''
  452. }`)
  453. } else if (file.path.includes('node.vue')) {
  454. content =
  455. // 传递 opts
  456. content.replace(/:childs\s*=\s*"tbody.children"\s*:opts="opts"/, ':childs="tbody.children" :opts="[opts[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+\'.\'+i+\'.children.\'+x+\'.children\']"')
  457. .replace(/:childs\s*=\s*"n2.children"\s*:opts="opts"/, ':childs="n2.children" :opts="[opts[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+\'.\'+i+\'.children.\'+j+\'.children\']"')
  458. .replace(/:childs\s*=\s*"tr.children"\s*:opts="opts"/, ':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\']"')
  459. .replace(/:childs\s*=\s*"td.children"\s*:opts="opts"/, ':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\']"')
  460. .replace(/opts\s*=\s*"opts"/g, 'opts="[opts[0],opts[1],opts[2],opts[3],opts[4],opts[5],opts[6],opts[7]+\'.\'+i+\'.children\']"')
  461. // 不使用 rich-text
  462. .replace(/!n.c/g, '!opts[5]&&!n.c').replace('&&n.c', '&&(n.c||opts[5])')
  463. // 修改普通标签
  464. .replace(/<view\s+:id(.+?)style="/, '<view @tap="nodeTap" :id$1style="(ctrl.root?\'border:1px solid black;padding:5px;display:block;\':\'\')+')
  465. // 修改文本块
  466. .replace(/<!--\s*文本\s*-->[\s\S]+?<!--\s*链接\s*-->/,
  467. `<!-- 文本 -->
  468. <text v-else-if="n.type==='text'&&!ctrl['e'+i]" :data-i="i" :user-select="opts[4]" :decode="!opts[5]" @tap="editStart">{{n.text}}
  469. <text v-if="!n.text" style="color:gray">{{opts[6]||'请输入'}}</text>
  470. </text>
  471. <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}}
  472. <text v-if="!n.text" style="color:gray">{{opts[6]||'请输入'}}</text>
  473. </text>
  474. <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" />
  475. <text v-else-if="n.name==='br'">\\n</text>
  476. <!-- 链接 -->`)
  477. // 修改图片
  478. .replace(/<image(.+?)id="n.attrs.id/, '<image$1id="n.attrs.id||(\'n\'+i)')
  479. .replace('height:1px', "height:'+(ctrl['h'+i]||1)+'px")
  480. .replace(/:style\s*=\s*"\(ctrl\[i\]/g, ':style="(ctrl[\'e\'+i]?\'border:1px dashed black;padding:3px;\':\'\')+(ctrl[i]')
  481. .replace(/show-menu-by-longpress\s*=\s*"(\S+?)"\s*:image-menu-prevent\s*=\s*"(\S+?)"/, 'show-menu-by-longpress="!opts[5]&&$1" :image-menu-prevent="opts[5]||$2"')
  482. // 修改音视频
  483. .replace('v-else-if="n.html"', 'v-else-if="n.html" :data-i="i" @tap="mediaTap"')
  484. .replace('<video', '<video :show-center-play-btn="!opts[5]" @tap="mediaTap"')
  485. .replace('audio ', 'audio @tap="mediaTap" ')
  486. .replace('<script>',
  487. `<script>
  488. function getTop(e) {
  489. let top
  490. // #ifdef H5 || APP-PLUS
  491. top = e.touches[0].pageY
  492. // #endif
  493. // #ifdef MP-ALIPAY
  494. top = e.detail.pageY
  495. // #endif
  496. // #ifndef H5 || MP-ALIPAY || APP-PLUS
  497. top = e.detail.y
  498. // #endif
  499. if (top - e.currentTarget.offsetTop < 150 || top < 600) {
  500. top = e.currentTarget.offsetTop
  501. }
  502. if (top < 30) {
  503. top += 70
  504. }
  505. return top - 30
  506. }`)
  507. // 周期处理
  508. .replace(/beforeDestroy\s*\(\)\s*{/, `beforeDestroy () {
  509. if (this.root._edit === this) {
  510. this.root._edit = undefined
  511. }`)
  512. // 记录图片宽度
  513. .replace(/imgLoad\s*\(e\)\s*{/, `imgLoad(e) {
  514. // #ifdef MP-WEIXIN || MP-QQ
  515. if (this.opts[5])
  516. this.$nextTick(() => {
  517. const id = this.childs[i].attrs.id || ('n' + i)
  518. uni.createSelectorQuery().in(this).select('#' + id).boundingClientRect().exec(res => {
  519. this.$set(this.ctrl, 'h'+i, res[0].height)
  520. })
  521. })
  522. // #endif`)
  523. .replace(/if\s*\(!this.childs\[i\].w\)\s*{[\s\S]+?}/,
  524. `if (!this.childs[i].w) {
  525. this.$set(this.ctrl, i, e.detail.width)
  526. if (this.opts[5]) {
  527. const path = this.opts[7] + '.' + i + '.attrs.'
  528. if (e.detail.width < 150)
  529. this.root._setData(path + 'ignore', 'T')
  530. this.root._setData(path + 'width', e.detail.width.toString())
  531. }
  532. }`)
  533. // 处理图片长按
  534. .replace(/imgLongTap\s*\(\)\s*{/, `imgLongTap() {
  535. if (this.opts[5]) return`)
  536. // 处理图片点击
  537. .replace(/imgTap\s*\(e\)\s*{([\s\S]+?)},\s*\/\*/,
  538. `imgTap (e) {
  539. if (!this.opts[5]) {$1} else {
  540. const i = e.currentTarget.dataset.i
  541. const node = this.childs[i]
  542. const items = this.root._getItem(node)
  543. this.root._edit = this
  544. this.i = i
  545. this.root._maskTap()
  546. this.$set(this.ctrl, 'e' + i, 1)
  547. this.root._mask.push(() => this.$set(this.ctrl, 'e' + i, 0))
  548. this.root._tooltip({
  549. top: getTop(e),
  550. items,
  551. success: tapIndex => {
  552. if (items[tapIndex] === '换图') {
  553. // 换图
  554. this.root.getSrc('img', node.attrs.src || '').then(url => {
  555. this.root._editVal(this.opts[7] + '.' + i + '.attrs.src', node.attrs.src, url instanceof Array ? url[0] : url, true)
  556. }).catch(() => { })
  557. } else if (items[tapIndex] === '宽度') {
  558. // 更改宽度
  559. const style = node.attrs.style || ''
  560. let value = style.match(/max-width:([0-9]+)%/)
  561. if (value) {
  562. value = parseInt(value[1])
  563. } else {
  564. value = 100
  565. }
  566. this.root._slider({
  567. min: 0,
  568. max: 100,
  569. value,
  570. top: getTop(e),
  571. changing: val => {
  572. // 变化超过 5% 更新时视图
  573. if (Math.abs(val - value) > 5) {
  574. this.changeStyle('max-width', i, val + '%', value + '%')
  575. value = val
  576. }
  577. },
  578. change: val => {
  579. if (val !== value) {
  580. this.changeStyle('max-width', i, val + '%', value + '%')
  581. value = val
  582. }
  583. this.root._editVal(this.opts[7] + '.' + i + '.attrs.style', style, this.childs[i].attrs.style)
  584. }
  585. })
  586. } else if (items[tapIndex] === '超链接') {
  587. // 将图片设置为链接
  588. this.root.getSrc('link', node.a ? node.a.href : '').then(url => {
  589. // 如果有 a 标签则替换 href
  590. if (node.a) {
  591. this.root._editVal(this.opts[7] + '.' + i + '.a.href', node.a.href, url, true)
  592. } else {
  593. const link = {
  594. name: 'a',
  595. attrs: {
  596. href: url
  597. },
  598. children: [node]
  599. }
  600. node.a = link.attrs
  601. this.root._editVal(this.opts[7] + '.' + i, node, link, true)
  602. }
  603. wx.showToast({
  604. title: '成功'
  605. })
  606. }).catch(() => { })
  607. } else if (items[tapIndex] === '预览图') {
  608. // 设置预览图链接
  609. this.root.getSrc('img', node.attrs['original-src'] || '').then(url => {
  610. this.root._editVal(this.opts[7] + '.' + i + '.attrs.original-src', node.attrs['original-src'], url instanceof Array ? url[0] : url, true)
  611. uni.showToast({
  612. title: '成功'
  613. })
  614. }).catch(() => { })
  615. } else if (items[tapIndex] === '删除') {
  616. this.remove(i)
  617. } else {
  618. // 禁用 / 启用预览
  619. this.root._setData(this.opts[7] + '.' + i + '.attrs.ignore', !node.attrs.ignore)
  620. uni.showToast({
  621. title: '成功'
  622. })
  623. }
  624. }
  625. })
  626. this.root._lock = true
  627. setTimeout(() => {
  628. this.root._lock = false
  629. }, 50)
  630. }
  631. },
  632. /*`)
  633. // 处理链接点击
  634. .replace(/linkTap\s*\(e\)\s*{([\s\S]+?)},\s*\/\*/,
  635. `linkTap (e) {
  636. if (!this.opts[5]) {$1} else {
  637. const i = e.currentTarget.dataset.i
  638. const node = this.childs[i]
  639. const items = this.root._getItem(node)
  640. this.root._tooltip({
  641. top: getTop(e),
  642. items,
  643. success: tapIndex => {
  644. if (items[tapIndex] === '更换链接') {
  645. this.root.getSrc('link', node.attrs.href).then(url => {
  646. this.root._editVal(this.opts[7] + '.' + i + '.attrs.href', node.attrs.href, url, true)
  647. uni.showToast({
  648. title: '成功'
  649. })
  650. }).catch(() => { })
  651. } else {
  652. this.remove(i)
  653. }
  654. }
  655. })
  656. }
  657. },
  658. /*`)
  659. }
  660. file.contents = Buffer.from(content)
  661. }
  662. }
  663. }