fix(tab): avoid accidental drag on slight tab movement
This commit is contained in:
5 files changed
+132
-18
No files matched your search
@@ -47,7 +47,7 @@ const queryStore = useQueryStore();
|
|||||||
const settingsStore = useSettingsStore();
|
const settingsStore = useSettingsStore();
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const tabDrag = useTabDrag((draggedId, targetId, position) => {
|
const tabDrag = useTabDrag((draggedId, targetId, position) => {
|
||||||
queryStore.reorderTab(draggedId, targetId, position);
|
return queryStore.reorderTab(draggedId, targetId, position);
|
||||||
});
|
});
|
||||||
const editingTabId = ref<string | null>(null);
|
const editingTabId = ref<string | null>(null);
|
||||||
const editingTitle = ref("");
|
const editingTitle = ref("");
|
||||||
@@ -538,7 +538,7 @@ function tabMenuIcon(tab: QueryTab) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleTabClick(tab: QueryTab) {
|
function handleTabClick(tab: QueryTab) {
|
||||||
if (tabDrag.state.wasDragged) return;
|
if (tabDrag.state.suppressClick) return;
|
||||||
activateTab(tab.id);
|
activateTab(tab.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
// @vitest-environment happy-dom
|
||||||
|
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { TAB_DRAG_HORIZONTAL_THRESHOLD, useTabDrag } from "@/composables/useTabDrag";
|
||||||
|
|
||||||
|
function createDragHarness(onDrop = vi.fn(() => true)) {
|
||||||
|
const drag = useTabDrag(onDrop);
|
||||||
|
const source = document.createElement("div");
|
||||||
|
source.innerHTML = '<span class="truncate">Source</span>';
|
||||||
|
source.addEventListener("mousedown", (event) => drag.startDrag(event, "source"));
|
||||||
|
document.body.appendChild(source);
|
||||||
|
return { drag, source, onDrop };
|
||||||
|
}
|
||||||
|
|
||||||
|
function beginDrag(source: HTMLElement, x = 100, y = 20) {
|
||||||
|
source.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, button: 0, clientX: x, clientY: y }));
|
||||||
|
}
|
||||||
|
|
||||||
|
function movePointer(x: number, y = 20) {
|
||||||
|
document.dispatchEvent(new MouseEvent("mousemove", { bubbles: true, clientX: x, clientY: y }));
|
||||||
|
}
|
||||||
|
|
||||||
|
function releasePointer() {
|
||||||
|
document.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
|
||||||
|
}
|
||||||
|
|
||||||
|
function enterDropTarget(drag: ReturnType<typeof useTabDrag>, tabId = "target", x = 10) {
|
||||||
|
const target = document.createElement("div");
|
||||||
|
target.getBoundingClientRect = () => ({ x: 0, y: 0, left: 0, top: 0, right: 100, bottom: 28, width: 100, height: 28, toJSON: () => ({}) });
|
||||||
|
target.addEventListener("mousemove", (event) => drag.updateTarget(event, tabId));
|
||||||
|
document.body.appendChild(target);
|
||||||
|
target.dispatchEvent(new MouseEvent("mousemove", { bubbles: true, clientX: x, clientY: 20 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
releasePointer();
|
||||||
|
document.body.innerHTML = "";
|
||||||
|
document.body.style.cursor = "";
|
||||||
|
document.body.style.userSelect = "";
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("useTabDrag", () => {
|
||||||
|
it("ignores sub-threshold horizontal movement and vertical pointer jitter", () => {
|
||||||
|
const { drag, source, onDrop } = createDragHarness();
|
||||||
|
beginDrag(source);
|
||||||
|
|
||||||
|
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD - 1, 120);
|
||||||
|
|
||||||
|
expect(drag.state.active).toBe(false);
|
||||||
|
expect(drag.state.suppressClick).toBe(false);
|
||||||
|
expect(onDrop).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not suppress the click when a drag gesture has no valid drop target", () => {
|
||||||
|
const { drag, source, onDrop } = createDragHarness();
|
||||||
|
beginDrag(source);
|
||||||
|
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
|
||||||
|
expect(drag.state.active).toBe(true);
|
||||||
|
|
||||||
|
releasePointer();
|
||||||
|
|
||||||
|
expect(onDrop).not.toHaveBeenCalled();
|
||||||
|
expect(drag.state.suppressClick).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not suppress the click when the drop callback reports no reorder", () => {
|
||||||
|
const onDrop = vi.fn(() => false);
|
||||||
|
const { drag, source } = createDragHarness(onDrop);
|
||||||
|
beginDrag(source);
|
||||||
|
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
|
||||||
|
enterDropTarget(drag);
|
||||||
|
|
||||||
|
releasePointer();
|
||||||
|
|
||||||
|
expect(onDrop).toHaveBeenCalledWith("source", "target", "before");
|
||||||
|
expect(drag.state.suppressClick).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("suppresses the click only after a completed reorder", () => {
|
||||||
|
const { drag, source, onDrop } = createDragHarness();
|
||||||
|
beginDrag(source);
|
||||||
|
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
|
||||||
|
enterDropTarget(drag, "target", 90);
|
||||||
|
|
||||||
|
releasePointer();
|
||||||
|
|
||||||
|
expect(onDrop).toHaveBeenCalledWith("source", "target", "after");
|
||||||
|
expect(drag.state.suppressClick).toBe(true);
|
||||||
|
|
||||||
|
beginDrag(source);
|
||||||
|
expect(drag.state.suppressClick).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -7,19 +7,19 @@ interface TabDragState {
|
|||||||
draggedId: string | null;
|
draggedId: string | null;
|
||||||
targetId: string | null;
|
targetId: string | null;
|
||||||
dropPosition: TabDropPosition | null;
|
dropPosition: TabDropPosition | null;
|
||||||
wasDragged: boolean;
|
suppressClick: boolean;
|
||||||
startX: number;
|
startX: number;
|
||||||
startY: number;
|
startY: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DRAG_THRESHOLD = 5;
|
export const TAB_DRAG_HORIZONTAL_THRESHOLD = 12;
|
||||||
|
|
||||||
const state = reactive<TabDragState>({
|
const state = reactive<TabDragState>({
|
||||||
active: false,
|
active: false,
|
||||||
draggedId: null,
|
draggedId: null,
|
||||||
targetId: null,
|
targetId: null,
|
||||||
dropPosition: null,
|
dropPosition: null,
|
||||||
wasDragged: false,
|
suppressClick: false,
|
||||||
startX: 0,
|
startX: 0,
|
||||||
startY: 0,
|
startY: 0,
|
||||||
});
|
});
|
||||||
@@ -30,7 +30,7 @@ let pending: {
|
|||||||
y: number;
|
y: number;
|
||||||
sourceEl: HTMLElement | null;
|
sourceEl: HTMLElement | null;
|
||||||
} | null = null;
|
} | null = null;
|
||||||
let onDropCallback: ((draggedId: string, targetId: string, position: TabDropPosition) => void) | null = null;
|
let onDropCallback: ((draggedId: string, targetId: string, position: TabDropPosition) => boolean) | null = null;
|
||||||
let ghostEl: HTMLElement | null = null;
|
let ghostEl: HTMLElement | null = null;
|
||||||
|
|
||||||
function createGhost(sourceEl: HTMLElement, x: number, y: number) {
|
function createGhost(sourceEl: HTMLElement, x: number, y: number) {
|
||||||
@@ -79,10 +79,8 @@ function onMouseMove(event: MouseEvent) {
|
|||||||
|
|
||||||
if (pending && !state.active) {
|
if (pending && !state.active) {
|
||||||
const dx = event.clientX - pending.x;
|
const dx = event.clientX - pending.x;
|
||||||
const dy = event.clientY - pending.y;
|
if (Math.abs(dx) < TAB_DRAG_HORIZONTAL_THRESHOLD) return;
|
||||||
if (Math.abs(dx) < DRAG_THRESHOLD && Math.abs(dy) < DRAG_THRESHOLD) return;
|
|
||||||
state.active = true;
|
state.active = true;
|
||||||
state.wasDragged = true;
|
|
||||||
state.draggedId = pending.id;
|
state.draggedId = pending.id;
|
||||||
state.startX = pending.x;
|
state.startX = pending.x;
|
||||||
state.startY = pending.y;
|
state.startY = pending.y;
|
||||||
@@ -100,8 +98,9 @@ function onMouseMove(event: MouseEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onMouseUp() {
|
function onMouseUp() {
|
||||||
|
state.suppressClick = false;
|
||||||
if (state.active && state.draggedId && state.targetId && state.dropPosition && onDropCallback) {
|
if (state.active && state.draggedId && state.targetId && state.dropPosition && onDropCallback) {
|
||||||
onDropCallback(state.draggedId, state.targetId, state.dropPosition);
|
state.suppressClick = onDropCallback(state.draggedId, state.targetId, state.dropPosition);
|
||||||
}
|
}
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
@@ -128,7 +127,7 @@ function ensureListeners() {
|
|||||||
listenersAttached = true;
|
listenersAttached = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useTabDrag(onDrop: (draggedId: string, targetId: string, position: TabDropPosition) => void) {
|
export function useTabDrag(onDrop: (draggedId: string, targetId: string, position: TabDropPosition) => boolean) {
|
||||||
ensureListeners();
|
ensureListeners();
|
||||||
onDropCallback = onDrop;
|
onDropCallback = onDrop;
|
||||||
|
|
||||||
@@ -136,7 +135,7 @@ export function useTabDrag(onDrop: (draggedId: string, targetId: string, positio
|
|||||||
if (event.button !== 0) return;
|
if (event.button !== 0) return;
|
||||||
const target = event.target as HTMLElement;
|
const target = event.target as HTMLElement;
|
||||||
if (target.closest("button, input, [data-tab-title-input]")) return;
|
if (target.closest("button, input, [data-tab-title-input]")) return;
|
||||||
state.wasDragged = false;
|
state.suppressClick = false;
|
||||||
const el = (event.currentTarget as HTMLElement) || null;
|
const el = (event.currentTarget as HTMLElement) || null;
|
||||||
pending = { id: tabId, x: event.clientX, y: event.clientY, sourceEl: el };
|
pending = { id: tabId, x: event.clientX, y: event.clientY, sourceEl: el };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2702,11 +2702,16 @@ export const useQueryStore = defineStore("query", () => {
|
|||||||
function reorderTab(id: string, targetId: string, position: "before" | "after") {
|
function reorderTab(id: string, targetId: string, position: "before" | "after") {
|
||||||
const fromIdx = tabs.value.findIndex((t) => t.id === id);
|
const fromIdx = tabs.value.findIndex((t) => t.id === id);
|
||||||
const toIdx = tabs.value.findIndex((t) => t.id === targetId);
|
const toIdx = tabs.value.findIndex((t) => t.id === targetId);
|
||||||
if (fromIdx < 0 || toIdx < 0 || fromIdx === toIdx) return;
|
if (fromIdx < 0 || toIdx < 0 || fromIdx === toIdx) return false;
|
||||||
const [tab] = tabs.value.splice(fromIdx, 1);
|
|
||||||
const newToIdx = tabs.value.findIndex((t) => t.id === targetId);
|
const reordered = [...tabs.value];
|
||||||
tabs.value.splice(newToIdx + (position === "after" ? 1 : 0), 0, tab);
|
const [tab] = reordered.splice(fromIdx, 1);
|
||||||
tabs.value = orderPinnedFirst(tabs.value, (item) => !!item.pinned);
|
const newToIdx = reordered.findIndex((t) => t.id === targetId);
|
||||||
|
reordered.splice(newToIdx + (position === "after" ? 1 : 0), 0, tab);
|
||||||
|
const nextTabs = orderPinnedFirst(reordered, (item) => !!item.pinned);
|
||||||
|
if (nextTabs.every((item, index) => item.id === tabs.value[index]?.id)) return false;
|
||||||
|
tabs.value = nextTabs;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function persistSavedSqlExecutionTarget(tab: QueryTab, options: UpdateExecutionTargetOptions) {
|
function persistSavedSqlExecutionTarget(tab: QueryTab, options: UpdateExecutionTargetOptions) {
|
||||||
|
|||||||
+18
-1
@@ -7302,9 +7302,26 @@ test("reorderTab with after position places tab correctly", () => {
|
|||||||
const tabC = store.createTab("conn-1", "db", "C", "query");
|
const tabC = store.createTab("conn-1", "db", "C", "query");
|
||||||
|
|
||||||
// Drag A after C
|
// Drag A after C
|
||||||
store.reorderTab(tabA, tabC, "after");
|
assert.equal(store.reorderTab(tabA, tabC, "after"), true);
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
store.tabs.map((t) => t.id),
|
store.tabs.map((t) => t.id),
|
||||||
[tabB, tabC, tabA],
|
[tabB, tabC, tabA],
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("reorderTab reports adjacent no-op drops without replacing tab order", () => {
|
||||||
|
setActivePinia(createPinia());
|
||||||
|
const store = useQueryStore();
|
||||||
|
|
||||||
|
const tabA = store.createTab("conn-1", "db", "A", "query");
|
||||||
|
const tabB = store.createTab("conn-1", "db", "B", "query");
|
||||||
|
const tabC = store.createTab("conn-1", "db", "C", "query");
|
||||||
|
const originalTabs = [...store.tabs];
|
||||||
|
|
||||||
|
assert.equal(store.reorderTab(tabA, tabB, "before"), false);
|
||||||
|
assert.deepEqual(store.tabs, originalTabs);
|
||||||
|
assert.equal(store.reorderTab(tabB, tabA, "after"), false);
|
||||||
|
assert.deepEqual(store.tabs, originalTabs);
|
||||||
|
assert.equal(store.reorderTab(tabC, "missing", "before"), false);
|
||||||
|
assert.deepEqual(store.tabs, originalTabs);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user