mirror of
https://github.com/t8y2/dbx.git
synced 2026-11-02 00:29:30 +00:00
48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { ref, type Ref } from "vue";
|
|
import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/safeStorage";
|
|
|
|
export function usePanelResize() {
|
|
const sidebarWidth = ref(Number(safeLocalStorageGet("dbx-sidebar-width")) || 260);
|
|
const aiPanelWidth = ref(Number(safeLocalStorageGet("dbx-ai-panel-width")) || 360);
|
|
const historyWidth = ref(Number(safeLocalStorageGet("dbx-history-width")) || 288);
|
|
const sqlLibraryWidth = ref(Number(safeLocalStorageGet("dbx-sql-library-width")) || 288);
|
|
|
|
function startPanelResize(widthRef: Ref<number>, storageKey: string, direction: "left" | "right") {
|
|
return (e: MouseEvent) => {
|
|
e.preventDefault();
|
|
const startX = e.clientX;
|
|
const startWidth = widthRef.value;
|
|
|
|
const onMouseMove = (ev: MouseEvent) => {
|
|
const delta = ev.clientX - startX;
|
|
widthRef.value = Math.max(180, Math.min(800, startWidth + (direction === "right" ? delta : -delta)));
|
|
};
|
|
|
|
const onMouseUp = () => {
|
|
document.removeEventListener("mousemove", onMouseMove);
|
|
document.removeEventListener("mouseup", onMouseUp);
|
|
safeLocalStorageSet(storageKey, String(widthRef.value));
|
|
};
|
|
|
|
document.addEventListener("mousemove", onMouseMove);
|
|
document.addEventListener("mouseup", onMouseUp);
|
|
};
|
|
}
|
|
|
|
const startSidebarResize = startPanelResize(sidebarWidth, "dbx-sidebar-width", "right");
|
|
const startAiPanelResize = startPanelResize(aiPanelWidth, "dbx-ai-panel-width", "left");
|
|
const startHistoryResize = startPanelResize(historyWidth, "dbx-history-width", "left");
|
|
const startSqlLibraryResize = startPanelResize(sqlLibraryWidth, "dbx-sql-library-width", "left");
|
|
|
|
return {
|
|
sidebarWidth,
|
|
aiPanelWidth,
|
|
historyWidth,
|
|
sqlLibraryWidth,
|
|
startSidebarResize,
|
|
startAiPanelResize,
|
|
startHistoryResize,
|
|
startSqlLibraryResize,
|
|
};
|
|
}
|