Commit abf2f854aa26b582e16fdf761952e16b69a4ebcd
1 parent
cce6ac60
refactor(shape)
Showing
3 changed files
with
26 additions
and
13 deletions
front-end/h5/src/components/core/editor/canvas/edit.js
| ... | ... | @@ -81,7 +81,12 @@ export default { |
| 81 | 81 | /** |
| 82 | 82 | * #!zh: 在元素移动过程中,计算和生成辅助线 |
| 83 | 83 | */ |
| 84 | - handleElementMove ({ top, left }) { | |
| 84 | + handleElementMove (pos) { | |
| 85 | + this.setElementPosition(pos) | |
| 86 | + this.calcX(pos.left) | |
| 87 | + this.calcY(pos.top) | |
| 88 | + }, | |
| 89 | + handlePointMove ({ top, left }) { | |
| 85 | 90 | this.calcX(left) |
| 86 | 91 | this.calcY(top) |
| 87 | 92 | }, |
| ... | ... | @@ -146,6 +151,7 @@ export default { |
| 146 | 151 | } |
| 147 | 152 | return ( |
| 148 | 153 | <Shape |
| 154 | + defaultPosition={element.commonStyle} | |
| 149 | 155 | element={element} |
| 150 | 156 | active={this.editingElement === element} |
| 151 | 157 | handleMousedownProp={() => { |
| ... | ... | @@ -154,6 +160,10 @@ export default { |
| 154 | 160 | // 这样,就不用等到鼠标抬起的时候,也就是 plugin 的 onClick 生效的时候,才给选中的元素添加边框等选中效果 |
| 155 | 161 | this.setEditingElement(element) |
| 156 | 162 | }} |
| 163 | + // TODO 矩形四周的点叫什么?暂时叫 Point 吧 | |
| 164 | + handlePointMoveProp={pos => { | |
| 165 | + this.setElementPosition(pos) | |
| 166 | + }} | |
| 157 | 167 | handleElementMoveProp={this.handleElementMove} |
| 158 | 168 | > |
| 159 | 169 | {h(element.name, data)} | ... | ... |
front-end/h5/src/components/core/support/shape.js
| ... | ... | @@ -13,14 +13,18 @@ const directionKey = { |
| 13 | 13 | const points = ['lt', 'rt', 'lb', 'rb', 'l', 'r', 't', 'b'] |
| 14 | 14 | |
| 15 | 15 | export default { |
| 16 | - props: ['element', 'active', 'handleMousedownProp', 'handleElementMoveProp'], | |
| 16 | + props: ['defaultPosition', 'active', 'handleMousedownProp', 'handleElementMoveProp', 'handlePointMoveProp'], | |
| 17 | + computed: { | |
| 18 | + position () { | |
| 19 | + return { ...this.defaultPosition } | |
| 20 | + } | |
| 21 | + }, | |
| 17 | 22 | methods: { |
| 18 | 23 | /** |
| 19 | 24 | * 通过方位计算样式,主要是 top、left、鼠标样式 |
| 20 | 25 | */ |
| 21 | 26 | getPointStyle (point, isWrapElement = true) { |
| 22 | - // const eleStyle = this.numbericElementStyle | |
| 23 | - const pos = this.element.commonStyle | |
| 27 | + const pos = this.position | |
| 24 | 28 | const top = pos.top - 4 // !#zh 减4是为了让元素能够处于 border 的中间 |
| 25 | 29 | const left = pos.left - 4 |
| 26 | 30 | const height = pos.height |
| ... | ... | @@ -63,8 +67,7 @@ export default { |
| 63 | 67 | mousedownForMark (point, downEvent) { |
| 64 | 68 | downEvent.stopPropagation() |
| 65 | 69 | downEvent.preventDefault() // Let's stop this event. |
| 66 | - // let eleStyle = this.numbericElementStyle | |
| 67 | - const pos = this.element.commonStyle | |
| 70 | + const pos = { ...this.position } | |
| 68 | 71 | let height = pos.height |
| 69 | 72 | let width = pos.width |
| 70 | 73 | let top = pos.top |
| ... | ... | @@ -86,6 +89,7 @@ export default { |
| 86 | 89 | pos.width = newWidth > 0 ? newWidth : 0 |
| 87 | 90 | pos.left = +left + (hasL ? disX : 0) |
| 88 | 91 | pos.top = +top + (hasT ? disY : 0) |
| 92 | + this.handlePointMoveProp(pos) | |
| 89 | 93 | } |
| 90 | 94 | let up = () => { |
| 91 | 95 | document.removeEventListener('mousemove', move) |
| ... | ... | @@ -97,11 +101,10 @@ export default { |
| 97 | 101 | /** |
| 98 | 102 | * !#zh 给 当前选中元素 添加鼠标移动相关事件 |
| 99 | 103 | * |
| 100 | - * @param {Object} element | |
| 101 | 104 | * @param {mouseEvent} e |
| 102 | 105 | */ |
| 103 | - mousedownForElement (e, element) { | |
| 104 | - const pos = element.commonStyle | |
| 106 | + mousedownForElement (e) { | |
| 107 | + const pos = { ...this.position } | |
| 105 | 108 | let startY = e.clientY |
| 106 | 109 | let startX = e.clientX |
| 107 | 110 | let startTop = pos.top | ... | ... |
front-end/h5/src/store/modules/element.js
| ... | ... | @@ -14,10 +14,10 @@ const actions = { |
| 14 | 14 | commit('setEditingElement', payload) |
| 15 | 15 | }, |
| 16 | 16 | setElementPosition ({ commit }, payload) { |
| 17 | - commit('setElementPosition', payload) | |
| 17 | + commit('setElementCommonStyle', payload) | |
| 18 | 18 | }, |
| 19 | 19 | setElementShape ({ commit }, payload) { |
| 20 | - commit('setElementShape', payload) | |
| 20 | + commit('setElementCommonStyle', payload) | |
| 21 | 21 | } |
| 22 | 22 | } |
| 23 | 23 | |
| ... | ... | @@ -26,10 +26,10 @@ const mutations = { |
| 26 | 26 | setEditingElement (state, payload) { |
| 27 | 27 | state.editingElement = payload |
| 28 | 28 | }, |
| 29 | - setElementCommonStyle (state, commonStyle) { | |
| 29 | + setElementCommonStyle (state, payload) { | |
| 30 | 30 | state.editingElement.commonStyle = { |
| 31 | 31 | ...state.editingElement.commonStyle, |
| 32 | - ...commonStyle | |
| 32 | + ...payload | |
| 33 | 33 | } |
| 34 | 34 | } |
| 35 | 35 | } | ... | ... |