1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix drag-n-drop logic

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2021-05-03 06:36:46 +03:00
parent a3958b6a64
commit ef7d786755
2 changed files with 25 additions and 5 deletions

View File

@ -158,17 +158,37 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
hotbar.items[index] = null;
}
swapItems(from: number, to: number): void {
findClosestEmptyIndex(from: number, direction = 1) {
let index = from;
while(this.getActive().items[index] != null) {
index += direction;
}
return index;
}
restackItems(from: number, to: number): void {
const { items } = this.getActive();
const source = items[from];
const moveDown = from < to;
if (from < 0 || to < 0 || from >= items.length || to >= items.length || isNaN(from) || isNaN(to)) {
throw new Error("Invalid 'from' or 'to' arguments");
}
if (from != to) {
const source = items.splice(from, 1);
if (from == to) {
return;
}
items.splice(to, 0, source[0]);
items.splice(from, 1, null);
if (items[to] == null) {
items.splice(to, 1, source);
} else {
// Move cells up or down to closes empty cell
items.splice(this.findClosestEmptyIndex(to, moveDown ? -1 : 1), 1);
items.splice(to, 0, source);
}
}

View File

@ -45,7 +45,7 @@ export class HotbarMenu extends React.Component<Props> {
const from = parseInt(source.droppableId);
const to = parseInt(destination.droppableId);
HotbarStore.getInstance().swapItems(from, to);
HotbarStore.getInstance().restackItems(from, to);
}
renderGrid() {