1
0

build.js 26 KB

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