@@ -112,39 +112,7 @@ function canAddMore (definition, context) {
112
112
function add ( { definitionId } , context ) {
113
113
const definition = findDefinition ( { definitionId } , context )
114
114
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 )
148
116
149
117
const widget = {
150
118
id : shortid ( ) ,
@@ -187,13 +155,53 @@ function updateCount (definitionId, mod) {
187
155
widgetCount . set ( definitionId , getCount ( definitionId ) + mod )
188
156
}
189
157
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
+
190
199
function hasEnoughSpace ( grid , x , y , width , height ) {
191
200
// Test if enough horizontal available space
192
201
for ( let testX = x ; testX < x + width ; testX ++ ) {
193
202
// 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 ++ ) {
195
204
if ( grid . get ( `${ testX } :${ testY } ` ) ) {
196
- x = testX + 1
197
205
return { result : false , testX }
198
206
}
199
207
}
@@ -234,12 +242,31 @@ function move (input, context) {
234
242
if ( widget . width > definition . maxWidth ) widget . width = definition . maxWidth
235
243
if ( widget . height < definition . minHeight ) widget . height = definition . minHeight
236
244
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
+
238
256
save ( context )
239
257
}
240
258
return widgets
241
259
}
242
260
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
+
243
270
async function openConfig ( { id } , context ) {
244
271
const widget = findById ( { id } , context )
245
272
const definition = findDefinition ( widget , context )
0 commit comments