Skip to content

Commit ef40b05

Browse files
committed
feat(widget): overlap detection
1 parent a13ad26 commit ef40b05

File tree

1 file changed

+63
-36
lines changed
  • packages/@vue/cli-ui/apollo-server/connectors

1 file changed

+63
-36
lines changed

packages/@vue/cli-ui/apollo-server/connectors/widgets.js

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -112,39 +112,7 @@ function canAddMore (definition, context) {
112112
function add ({ definitionId }, context) {
113113
const definition = findDefinition({ definitionId }, context)
114114

115-
// Find next available space
116-
const width = definition.defaultWidth || definition.minWidth
117-
const height = definition.defaultHeight || definition.minHeight
118-
// Mark occupied positions on the grid
119-
const grid = new Map()
120-
for (const widget of widgets) {
121-
for (let x = widget.x; x < widget.x + widget.width; x++) {
122-
for (let y = widget.y; y < widget.y + widget.height; y++) {
123-
grid.set(`${x}:${y}`, true)
124-
}
125-
}
126-
}
127-
// Go through the possible positions
128-
let x = 0
129-
let y = 0
130-
while (true) {
131-
// Virtual "line brak"
132-
if (x !== 0 && x + width >= 7) {
133-
x = 0
134-
y++
135-
}
136-
if (grid.get(`${x}:${y}`)) {
137-
x++
138-
continue
139-
}
140-
const { result, testX } = hasEnoughSpace(grid, x, y, width, height)
141-
if (!result) {
142-
x = testX + 1
143-
continue
144-
}
145-
// Found! :)
146-
break
147-
}
115+
const { x, y, width, height } = findValidPosition(definition)
148116

149117
const widget = {
150118
id: shortid(),
@@ -187,13 +155,53 @@ function updateCount (definitionId, mod) {
187155
widgetCount.set(definitionId, getCount(definitionId) + mod)
188156
}
189157

158+
function findValidPosition (definition, currentWidget = null) {
159+
// Find next available space
160+
const width = (currentWidget && currentWidget.width) || definition.defaultWidth || definition.minWidth
161+
const height = (currentWidget && currentWidget.height) || definition.defaultHeight || definition.minHeight
162+
// Mark occupied positions on the grid
163+
const grid = new Map()
164+
for (const widget of widgets) {
165+
if (widget !== currentWidget) {
166+
for (let x = widget.x; x < widget.x + widget.width; x++) {
167+
for (let y = widget.y; y < widget.y + widget.height; y++) {
168+
grid.set(`${x}:${y}`, true)
169+
}
170+
}
171+
}
172+
}
173+
// Go through the possible positions
174+
let x = 0
175+
let y = 0
176+
while (true) {
177+
// Virtual "line brak"
178+
if (x !== 0 && x + width >= 7) {
179+
x = 0
180+
y++
181+
}
182+
const { result, testX } = hasEnoughSpace(grid, x, y, width, height)
183+
if (!result) {
184+
x = testX + 1
185+
continue
186+
}
187+
// Found! :)
188+
break
189+
}
190+
191+
return {
192+
x,
193+
y,
194+
width,
195+
height
196+
}
197+
}
198+
190199
function hasEnoughSpace (grid, x, y, width, height) {
191200
// Test if enough horizontal available space
192201
for (let testX = x; testX < x + width; testX++) {
193202
// Test if enough vertical available space
194-
for (let testY = y + 1; testY < y + height; testY++) {
203+
for (let testY = y; testY < y + height; testY++) {
195204
if (grid.get(`${testX}:${testY}`)) {
196-
x = testX + 1
197205
return { result: false, testX }
198206
}
199207
}
@@ -234,12 +242,31 @@ function move (input, context) {
234242
if (widget.width > definition.maxWidth) widget.width = definition.maxWidth
235243
if (widget.height < definition.minHeight) widget.height = definition.minHeight
236244
if (widget.height > definition.maxHeight) widget.height = definition.maxHeight
237-
// TODO push overlapping widgets
245+
246+
for (const otherWidget of widgets) {
247+
if (otherWidget !== widget) {
248+
if (areOverlapping(otherWidget, widget)) {
249+
console.log(otherWidget, widget)
250+
const otherDefinition = findDefinition(otherWidget, context)
251+
Object.assign(otherWidget, findValidPosition(otherDefinition, otherWidget))
252+
}
253+
}
254+
}
255+
238256
save(context)
239257
}
240258
return widgets
241259
}
242260

261+
function areOverlapping (widgetA, widgetB) {
262+
return (
263+
widgetA.x + widgetA.width - 1 >= widgetB.x &&
264+
widgetA.x <= widgetB.x + widgetB.width - 1 &&
265+
widgetA.y + widgetA.height - 1 >= widgetB.y &&
266+
widgetA.y <= widgetB.y + widgetB.height - 1
267+
)
268+
}
269+
243270
async function openConfig ({ id }, context) {
244271
const widget = findById({ id }, context)
245272
const definition = findDefinition(widget, context)

0 commit comments

Comments
 (0)