Skip to content

Commit 9d8a4f2

Browse files
author
Guillaume Chau
committed
refactor(widget): OnGrid mixin
1 parent 0325be1 commit 9d8a4f2

File tree

2 files changed

+65
-48
lines changed

2 files changed

+65
-48
lines changed

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

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
>
1212
<div
1313
class="shell"
14-
:style="style"
14+
:style="mainStyle"
1515
>
1616
<div class="wrapper">
1717
<div class="content-wrapper">
@@ -126,6 +126,7 @@
126126
<script>
127127
import Vue from 'vue'
128128
import Prompts from '../mixins/Prompts'
129+
import OnGrid from '../mixins/OnGrid'
129130
import Movable from '../mixins/Movable'
130131
import Resizable from '../mixins/Resizable'
131132
@@ -168,6 +169,11 @@ export default {
168169
}
169170
}),
170171
172+
OnGrid({
173+
field: 'widget',
174+
gridSize: GRID_SIZE
175+
}),
176+
171177
Movable({
172178
field: 'widget',
173179
gridSize: GRID_SIZE,
@@ -206,39 +212,6 @@ export default {
206212
},
207213
208214
computed: {
209-
style () {
210-
if (this.moveState) {
211-
return {
212-
...this.getPositionStyle(this.moveState.pxX, this.moveState.pxY),
213-
...this.getSizeStyle()
214-
}
215-
}
216-
if (this.resizeState) {
217-
return {
218-
...this.getPositionStyle(this.resizeState.pxX, this.resizeState.pxY),
219-
...this.getSizeStyle(this.resizeState.pxWidth, this.resizeState.pxHeight)
220-
}
221-
}
222-
return {
223-
...this.getPositionStyle(GRID_SIZE * this.widget.x, GRID_SIZE * this.widget.y),
224-
...this.getSizeStyle()
225-
}
226-
},
227-
228-
moveGhostStyle () {
229-
return {
230-
...this.getPositionStyle(GRID_SIZE * this.moveState.x, GRID_SIZE * this.moveState.y),
231-
...this.getSizeStyle()
232-
}
233-
},
234-
235-
resizeGhostStyle () {
236-
return {
237-
...this.getPositionStyle(GRID_SIZE * this.resizeState.x, GRID_SIZE * this.resizeState.y),
238-
...this.getSizeStyle(GRID_SIZE * this.resizeState.width, GRID_SIZE * this.resizeState.height)
239-
}
240-
},
241-
242215
isSelected () {
243216
return this.widget.id === state.selectedWidgetId
244217
}
@@ -259,20 +232,6 @@ export default {
259232
},
260233
261234
methods: {
262-
getPositionStyle (x, y) {
263-
return {
264-
left: `${x}px`,
265-
top: `${y}px`
266-
}
267-
},
268-
269-
getSizeStyle (width, height) {
270-
return {
271-
width: `${width || GRID_SIZE * this.widget.width}px`,
272-
height: `${height || GRID_SIZE * this.widget.height}px`
273-
}
274-
},
275-
276235
async openConfig () {
277236
await this.$apollo.mutate({
278237
mutation: WIDGET_CONFIG_OPEN,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export default function ({
2+
gridSize,
3+
field
4+
}) {
5+
// @vue/component
6+
return {
7+
computed: {
8+
mainStyle () {
9+
if (this.moveState) {
10+
return {
11+
...this.getPositionStyle(this.moveState.pxX, this.moveState.pxY),
12+
...this.getSizeStyle()
13+
}
14+
}
15+
if (this.resizeState) {
16+
return {
17+
...this.getPositionStyle(this.resizeState.pxX, this.resizeState.pxY),
18+
...this.getSizeStyle(this.resizeState.pxWidth, this.resizeState.pxHeight)
19+
}
20+
}
21+
return {
22+
...this.getPositionStyle(gridSize * this[field].x, gridSize * this[field].y),
23+
...this.getSizeStyle()
24+
}
25+
},
26+
27+
moveGhostStyle () {
28+
return {
29+
...this.getPositionStyle(gridSize * this.moveState.x, gridSize * this.moveState.y),
30+
...this.getSizeStyle()
31+
}
32+
},
33+
34+
resizeGhostStyle () {
35+
return {
36+
...this.getPositionStyle(gridSize * this.resizeState.x, gridSize * this.resizeState.y),
37+
...this.getSizeStyle(gridSize * this.resizeState.width, gridSize * this.resizeState.height)
38+
}
39+
}
40+
},
41+
42+
methods: {
43+
getPositionStyle (x, y) {
44+
return {
45+
left: `${x}px`,
46+
top: `${y}px`
47+
}
48+
},
49+
50+
getSizeStyle (width, height) {
51+
return {
52+
width: `${width || gridSize * this[field].width}px`,
53+
height: `${height || gridSize * this[field].height}px`
54+
}
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)