Skip to content

Commit 242403f

Browse files
author
Guillaume Chau
committed
refactor(widget): generic Movable mixin
1 parent b5b409a commit 242403f

File tree

2 files changed

+74
-54
lines changed

2 files changed

+74
-54
lines changed

packages/@vue/cli-ui/src/components/Widget.vue

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
<script>
127127
import Vue from 'vue'
128128
import Prompts from '../mixins/Prompts'
129+
import Movable from '../mixins/Movable'
129130
130131
import WIDGET_REMOVE from '../graphql/widgetRemove.gql'
131132
import WIDGET_MOVE from '../graphql/widgetMove.gql'
@@ -163,6 +164,11 @@ export default {
163164
}
164165
})
165166
}
167+
}),
168+
169+
Movable({
170+
field: 'widget',
171+
gridSize: GRID_SIZE
166172
})
167173
],
168174
@@ -180,7 +186,6 @@ export default {
180186
181187
data () {
182188
return {
183-
moveState: null,
184189
resizeState: null,
185190
showConfig: false,
186191
injected: {
@@ -196,7 +201,7 @@ export default {
196201
style () {
197202
if (this.moveState) {
198203
return {
199-
...this.getPositionStyle(this.moveState.x, this.moveState.y),
204+
...this.getPositionStyle(this.moveState.pxX, this.moveState.pxY),
200205
...this.getSizeStyle()
201206
}
202207
}
@@ -214,7 +219,7 @@ export default {
214219
215220
moveGhostStyle () {
216221
return {
217-
...this.getPositionStyle(GRID_SIZE * this.moveState.gridX, GRID_SIZE * this.moveState.gridY),
222+
...this.getPositionStyle(GRID_SIZE * this.moveState.x, GRID_SIZE * this.moveState.y),
218223
...this.getSizeStyle()
219224
}
220225
},
@@ -259,7 +264,6 @@ export default {
259264
},
260265
261266
beforeDestroy () {
262-
this.removeMoveListeners()
263267
this.removeResizeListeners()
264268
},
265269
@@ -299,7 +303,7 @@ export default {
299303
},
300304
301305
openDetails () {
302-
// TODOnv
306+
// TODO
303307
},
304308
305309
remove () {
@@ -321,64 +325,19 @@ export default {
321325
})
322326
},
323327
324-
// Moving
325-
326-
removeMoveListeners () {
327-
window.removeEventListener('mousemove', this.onMoveUpdate)
328-
window.removeEventListener('mouseup', this.onMoveEnd)
329-
},
330-
331-
updateMoveState (e) {
332-
const mouseDeltaX = e.clientX - this.$_initalMousePosition.x
333-
const mouseDeltaY = e.clientY - this.$_initalMousePosition.y
334-
const x = this.$_initialPosition.x + mouseDeltaX / 0.7
335-
const y = this.$_initialPosition.y + mouseDeltaY / 0.7
336-
let gridX = Math.round(x / GRID_SIZE)
337-
let gridY = Math.round(y / GRID_SIZE)
338-
if (gridX < 0) gridX = 0
339-
if (gridY < 0) gridY = 0
340-
this.moveState = {
341-
x,
342-
y,
343-
gridX,
344-
gridY
345-
}
346-
},
347-
348-
onMoveStart (e) {
349-
this.$_initalMousePosition = {
350-
x: e.clientX,
351-
y: e.clientY
352-
}
353-
this.$_initialPosition = {
354-
x: this.widget.x * GRID_SIZE,
355-
y: this.widget.y * GRID_SIZE
356-
}
357-
this.updateMoveState(e)
358-
window.addEventListener('mousemove', this.onMoveUpdate)
359-
window.addEventListener('mouseup', this.onMoveEnd)
360-
},
361-
362-
onMoveUpdate (e) {
363-
this.updateMoveState(e)
364-
},
365-
366-
async onMoveEnd (e) {
367-
this.updateMoveState(e)
368-
this.removeMoveListeners()
328+
async onMoved () {
369329
await this.$apollo.mutate({
370330
mutation: WIDGET_MOVE,
371331
variables: {
372332
input: {
373333
id: this.widget.id,
374-
x: this.moveState.gridX,
375-
y: this.moveState.gridY,
334+
x: this.moveState.x,
335+
y: this.moveState.y,
376336
width: this.widget.width,
377337
height: this.widget.height
378338
}
379339
}
380340
})
381-
this.moveState = null
382341
},
383342
384343
// Resizing
@@ -466,7 +425,6 @@ export default {
466425
}
467426
})
468427
this.resizeState = null
469-
console.log('resize end')
470428
}
471429
}
472430
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
export default function movable ({
2+
gridSize,
3+
field
4+
}) {
5+
// @vue/component
6+
return {
7+
data () {
8+
return {
9+
moveState: null
10+
}
11+
},
12+
13+
beforeDestroy () {
14+
this.removeMoveListeners()
15+
},
16+
17+
methods: {
18+
removeMoveListeners () {
19+
window.removeEventListener('mousemove', this.onMoveUpdate)
20+
window.removeEventListener('mouseup', this.onMoveEnd)
21+
},
22+
23+
updateMoveState (e) {
24+
const mouseDeltaX = e.clientX - this.$_initalMousePosition.x
25+
const mouseDeltaY = e.clientY - this.$_initalMousePosition.y
26+
const pxX = this[field].x * gridSize + mouseDeltaX / 0.7
27+
const pxY = this[field].y * gridSize + mouseDeltaY / 0.7
28+
let x = Math.round(pxX / gridSize)
29+
let y = Math.round(pxY / gridSize)
30+
if (x < 0) x = 0
31+
if (y < 0) y = 0
32+
this.moveState = {
33+
pxX,
34+
pxY,
35+
x,
36+
y
37+
}
38+
},
39+
40+
onMoveStart (e) {
41+
this.$_initalMousePosition = {
42+
x: e.clientX,
43+
y: e.clientY
44+
}
45+
this.updateMoveState(e)
46+
window.addEventListener('mousemove', this.onMoveUpdate)
47+
window.addEventListener('mouseup', this.onMoveEnd)
48+
},
49+
50+
onMoveUpdate (e) {
51+
this.updateMoveState(e)
52+
},
53+
54+
async onMoveEnd (e) {
55+
this.updateMoveState(e)
56+
this.removeMoveListeners()
57+
if (this.onMoved) await this.onMoved()
58+
this.moveState = null
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)