|
| 1 | +/** |
| 2 | + * #!zh: 上下左右 对应的 东南西北 |
| 3 | + * #!en: top(north)、bottom(south)、left(west)、right(east) |
| 4 | + */ |
| 5 | +const directionKey = { |
| 6 | + t: 'n', |
| 7 | + b: 's', |
| 8 | + l: 'w', |
| 9 | + r: 'e' |
| 10 | +} |
| 11 | + |
| 12 | +// #!zh: 四个边角、两条中线上的点 |
| 13 | +const points = ['lt', 'rt', 'lb', 'rb', 'l', 'r', 't', 'b'] |
| 14 | + |
| 15 | +export default { |
| 16 | + props: ['element', 'active', 'editingElement', 'handleMousedownProp'], |
| 17 | + methods: { |
| 18 | + /** |
| 19 | + * 通过方位计算样式,主要是 top、left、鼠标样式 |
| 20 | + */ |
| 21 | + getPointStyle (point, isWrapElement = true) { |
| 22 | + // const eleStyle = this.numbericElementStyle |
| 23 | + const pos = this.element.commonStyle |
| 24 | + const top = pos.top - 4 // !#zh 减4是为了让元素能够处于 border 的中间 |
| 25 | + const left = pos.left - 4 |
| 26 | + const height = pos.height |
| 27 | + const width = pos.width |
| 28 | + let hasT = /t/.test(point) |
| 29 | + let hasB = /b/.test(point) |
| 30 | + let hasL = /l/.test(point) |
| 31 | + let hasR = /r/.test(point) |
| 32 | + let newLeft = 0 |
| 33 | + let newTop = 0 |
| 34 | + if (point.length === 2) { |
| 35 | + newLeft = hasL ? 0 : width |
| 36 | + newTop = hasT ? 0 : height |
| 37 | + } else { |
| 38 | + // !#zh 上下点,宽度固定在中间 |
| 39 | + if (hasT || hasB) { |
| 40 | + newLeft = width / 2 |
| 41 | + newTop = hasT ? 0 : height |
| 42 | + } |
| 43 | + // !#zh 左右点,高度固定在中间 |
| 44 | + if (hasL || hasR) { |
| 45 | + newLeft = hasL ? 0 : width |
| 46 | + newTop = height / 2 |
| 47 | + } |
| 48 | + } |
| 49 | + const style = { |
| 50 | + left: `${newLeft + (isWrapElement ? 0 : left)}px`, |
| 51 | + top: `${newTop + (isWrapElement ? 0 : top)}px`, |
| 52 | + cursor: point.split('').reverse().map(m => directionKey[m]).join('') + '-resize' |
| 53 | + } |
| 54 | + return style |
| 55 | + }, |
| 56 | + /** |
| 57 | + * !#zh 主要目的是:阻止冒泡 |
| 58 | + */ |
| 59 | + handleWrapperClick (e) { |
| 60 | + e.stopPropagation() |
| 61 | + e.preventDefault() |
| 62 | + }, |
| 63 | + mousedownForMark (point, downEvent) { |
| 64 | + downEvent.stopPropagation() |
| 65 | + downEvent.preventDefault() // Let's stop this event. |
| 66 | + // let eleStyle = this.numbericElementStyle |
| 67 | + const pos = this.element.commonStyle |
| 68 | + let height = pos.height |
| 69 | + let width = pos.width |
| 70 | + let top = pos.top |
| 71 | + let left = pos.left |
| 72 | + let startX = downEvent.clientX |
| 73 | + let startY = downEvent.clientY |
| 74 | + let move = moveEvent => { |
| 75 | + let currX = moveEvent.clientX |
| 76 | + let currY = moveEvent.clientY |
| 77 | + let disY = currY - startY |
| 78 | + let disX = currX - startX |
| 79 | + let hasT = /t/.test(point) |
| 80 | + let hasB = /b/.test(point) |
| 81 | + let hasL = /l/.test(point) |
| 82 | + let hasR = /r/.test(point) |
| 83 | + let newHeight = +height + (hasT ? -disY : hasB ? disY : 0) |
| 84 | + let newWidth = +width + (hasL ? -disX : hasR ? disX : 0) |
| 85 | + pos.height = newHeight > 0 ? newHeight : 0 |
| 86 | + pos.width = newWidth > 0 ? newWidth : 0 |
| 87 | + pos.left = +left + (hasL ? disX : 0) |
| 88 | + pos.top = +top + (hasT ? disY : 0) |
| 89 | + } |
| 90 | + let up = () => { |
| 91 | + document.removeEventListener('mousemove', move) |
| 92 | + document.removeEventListener('mouseup', up) |
| 93 | + } |
| 94 | + document.addEventListener('mousemove', move) |
| 95 | + document.addEventListener('mouseup', up) |
| 96 | + }, |
| 97 | + /** |
| 98 | + * !#zh 给 当前选中元素 添加鼠标移动相关事件 |
| 99 | + * |
| 100 | + * @param {Object} element |
| 101 | + * @param {mouseEvent} e |
| 102 | + */ |
| 103 | + mousedownForElement (e, element) { |
| 104 | + const pos = element.commonStyle |
| 105 | + let startY = e.clientY |
| 106 | + let startX = e.clientX |
| 107 | + let startTop = pos.top |
| 108 | + let startLeft = pos.left |
| 109 | + |
| 110 | + let move = moveEvent => { |
| 111 | + // !#zh 移动的时候,不需要向后代元素传递事件,只需要单纯的移动就OK |
| 112 | + moveEvent.stopPropagation() |
| 113 | + moveEvent.preventDefault() |
| 114 | + |
| 115 | + let currX = moveEvent.clientX |
| 116 | + let currY = moveEvent.clientY |
| 117 | + pos.top = currY - startY + startTop |
| 118 | + pos.left = currX - startX + startLeft |
| 119 | + } |
| 120 | + |
| 121 | + let up = moveEvent => { |
| 122 | + document.removeEventListener('mousemove', move, true) |
| 123 | + document.removeEventListener('mouseup', up, true) |
| 124 | + } |
| 125 | + document.addEventListener('mousemove', move, true) |
| 126 | + document.addEventListener('mouseup', up, true) |
| 127 | + // TODO add comment |
| 128 | + return true |
| 129 | + }, |
| 130 | + handleMousedown (e) { |
| 131 | + if (this.handleMousedownProp) { |
| 132 | + this.handleMousedownProp() |
| 133 | + this.mousedownForElement(e, this.element) |
| 134 | + } |
| 135 | + } |
| 136 | + }, |
| 137 | + render (h) { |
| 138 | + return ( |
| 139 | + <div |
| 140 | + onClick={this.handleWrapperClick} |
| 141 | + onMousedown={this.handleMousedown} |
| 142 | + style={{ ...this.element.getStyle(), position: 'absolute' }} |
| 143 | + > |
| 144 | + { |
| 145 | + this.active && |
| 146 | + points.map(point => { |
| 147 | + const pointStyle = this.getPointStyle(point) |
| 148 | + return ( |
| 149 | + <div |
| 150 | + key={point} |
| 151 | + data-point={point} |
| 152 | + style={pointStyle} |
| 153 | + class="shape__scale-point" |
| 154 | + onMousedown={this.mousedownForMark.bind(this, point)} |
| 155 | + ></div> |
| 156 | + ) |
| 157 | + }) |
| 158 | + } |
| 159 | + {this.$slots.default} |
| 160 | + </div> |
| 161 | + ) |
| 162 | + } |
| 163 | +} |
0 commit comments