Skip to content

Commit 0a33b8f

Browse files
Extract the setting of the new coordinates to a function
1 parent d549881 commit 0a33b8f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/dd-draggable.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,36 +159,44 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
159159
screenY: screenY }
160160
}
161161

162+
_setCoordinates(element: HTMLElement, x: number, y:number) {
163+
let coordinates = this._elCoordinates(element)
164+
165+
coordinates.clientX += x || 0;
166+
coordinates.clientY += y || 0;
167+
168+
return coordinates
169+
}
170+
162171
protected _elNewCoordinates(event: KeyboardEvent, element: HTMLElement) {
163172
const node = this.el.gridstackNode
164173
const cellHeight = node.grid.getCellHeight() * node.h
165174
const cellWidth = node.grid.cellWidth()
166175
const maxColumn = node.grid.opts.column
167-
168-
let coordinates = this._elCoordinates(element)
176+
let xCoord: number, yCoord: number
169177

170178
switch (event.code) {
171179
case 'ArrowRight':
172180
if(typeof(maxColumn) == 'number' && node.x === (maxColumn - 1)) { break }
173181

174-
coordinates.clientX = coordinates.clientX + cellWidth + (cellWidth / 2)
182+
xCoord = cellWidth
175183
break
176184
case 'ArrowLeft':
177185
if (node.x === 0) { break }
178186

179-
coordinates.clientX = coordinates.clientX - cellWidth - (cellWidth / 2)
187+
xCoord = -cellWidth
180188
break
181189
case 'ArrowUp':
182190
if (node.y === 0) { break }
183191

184-
coordinates.clientY = coordinates.clientY - cellHeight
192+
yCoord = -cellHeight
185193
break
186194
case 'ArrowDown':
187-
coordinates.clientY = coordinates.clientY + cellHeight
195+
yCoord = cellHeight
188196
break
189197
}
190198

191-
return coordinates
199+
return this._setCoordinates(element, xCoord, yCoord);
192200
}
193201

194202
protected _keyDown(e: KeyboardEvent): void {

0 commit comments

Comments
 (0)