Skip to content

Commit b146347

Browse files
Get the correct new position
When using the arrow keys the item moves one colum left or right. Or its own height up or down.
1 parent a05cc11 commit b146347

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/dd-draggable.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
7777
}
7878
// create var event binding so we can easily remove and still look like TS methods (unlike anonymous functions)
7979
this._mouseDown = this._mouseDown.bind(this);
80-
this._mouseKeyDown = this._mouseKeyDown.bind(this);
80+
this._keyDown = this._keyDown.bind(this);
8181
this._keyMove = this._keyMove.bind(this);
8282
this._keyUp = this._keyUp.bind(this);
8383
this._mouseMove = this._mouseMove.bind(this);
@@ -98,7 +98,7 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
9898
if (this.disabled === false) return;
9999
super.enable();
100100
this.dragEls.forEach(dragEl => {
101-
dragEl.addEventListener('keydown', this._mouseKeyDown)
101+
dragEl.addEventListener('keydown', this._keyDown)
102102
dragEl.addEventListener('mousedown', this._mouseDown);
103103
if (isTouch) {
104104
dragEl.addEventListener('touchstart', touchstart);
@@ -160,26 +160,38 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
160160
}
161161

162162
protected _elNewCoordinates(event: KeyboardEvent, element: HTMLElement) {
163+
const node = this.el.gridstackNode
164+
const cellHeight = node.grid.getCellHeight() * node.h
165+
const cellWidth = node.grid.cellWidth()
166+
const maxColumn = node.grid.opts.column
167+
163168
let coordinates = this._elCoordinates(element)
164169

165170
switch (event.code) {
166171
case 'ArrowRight':
167-
coordinates.clientX = coordinates.clientX + 400
172+
if(typeof(maxColumn) == 'number' && node.x === (maxColumn - 1)) { break }
173+
174+
coordinates.clientX = coordinates.clientX + cellWidth + (cellWidth / 2)
168175
break
169176
case 'ArrowLeft':
170-
coordinates.clientX = coordinates.clientX - 400
177+
if (node.x === 0) { break }
178+
179+
coordinates.clientX = coordinates.clientX - cellWidth - (cellWidth / 2)
171180
break
172181
case 'ArrowUp':
173-
coordinates.clientY = coordinates.clientY - 400
182+
if (node.y === 0) { break }
183+
184+
coordinates.clientY = coordinates.clientY - cellHeight
174185
break
175186
case 'ArrowDown':
176-
coordinates.clientY = coordinates.clientY + 400
187+
coordinates.clientY = coordinates.clientY + cellHeight
177188
break
178189
}
190+
179191
return coordinates
180192
}
181193

182-
protected _mouseKeyDown(e: KeyboardEvent): void {
194+
protected _keyDown(e: KeyboardEvent): void {
183195
if(e.code === 'Space') {
184196
e.preventDefault()
185197

@@ -219,6 +231,8 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
219231
e.code === 'ArrowLeft' ||
220232
e.code === 'ArrowUp' ||
221233
e.code === 'ArrowDown') {
234+
e.preventDefault()
235+
222236
e.target.dispatchEvent(new MouseEvent('mousemove', { ...this._elCoordinates(this.keyboardSelected)}))
223237
e.target.dispatchEvent(new MouseEvent('mousemove', { ...this._elNewCoordinates(e, this.keyboardSelected)}))
224238
e.target.dispatchEvent(new MouseEvent('mouseup'))

0 commit comments

Comments
 (0)