fix(sidebar): keep group rename input focused
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, watch } from "vue";
|
||||
import { ref, computed, nextTick, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { translateBackendError } from "@/i18n/backend-errors";
|
||||
import {
|
||||
@@ -103,6 +103,7 @@ import {
|
||||
} from "@/lib/createDatabaseSql";
|
||||
import { buildRenameObjectSql, supportsObjectRename, type RenameableObjectType } from "@/lib/objectRenameSql";
|
||||
import { hexToRgba } from "@/lib/color";
|
||||
import { focusSidebarRenameInput, shouldPreventRenameCloseAutoFocus } from "@/lib/sidebarRenameFocus";
|
||||
import DangerConfirmDialog from "@/components/editor/DangerConfirmDialog.vue";
|
||||
import { isTauriRuntime } from "@/lib/tauriRuntime";
|
||||
import DatabaseIcon from "@/components/icons/DatabaseIcon.vue";
|
||||
@@ -1455,11 +1456,23 @@ function openVisibleDatabasesDialog() {
|
||||
// --- Connection Group Management ---
|
||||
const isRenamingGroup = ref(false);
|
||||
const renameInput = ref("");
|
||||
const renameInputRef = ref<HTMLInputElement>();
|
||||
const preventRenameCloseAutoFocus = ref(false);
|
||||
|
||||
function startRenameGroup() {
|
||||
renameInput.value = props.node.label;
|
||||
isRenamingGroup.value = true;
|
||||
preventRenameCloseAutoFocus.value = true;
|
||||
emit("rename-started");
|
||||
nextTick(() => {
|
||||
focusSidebarRenameInput(() => (isRenamingGroup.value ? renameInputRef.value : undefined));
|
||||
});
|
||||
}
|
||||
|
||||
function onContextMenuCloseAutoFocus(event: Event) {
|
||||
if (shouldPreventRenameCloseAutoFocus(preventRenameCloseAutoFocus)) {
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
@@ -1687,13 +1700,13 @@ const isDragging = computed(() => dragState.active && dragState.draggedId === pr
|
||||
/>
|
||||
<input
|
||||
v-if="isRenamingGroup"
|
||||
ref="renameInputRef"
|
||||
v-model="renameInput"
|
||||
class="min-w-0 flex-1 truncate bg-transparent border border-primary/50 rounded px-1 text-xs outline-none"
|
||||
class="min-w-0 flex-1 truncate bg-transparent border border-primary/50 rounded px-1 outline-none"
|
||||
@blur="finishRenameGroup"
|
||||
@keydown.enter.prevent="finishRenameGroup"
|
||||
@keydown.escape.prevent="isRenamingGroup = false"
|
||||
@click.stop
|
||||
@vue:mounted="($event: any) => $event.el.focus()"
|
||||
/>
|
||||
<Tooltip v-else :disabled="!isTruncated">
|
||||
<TooltipTrigger as-child>
|
||||
@@ -1742,7 +1755,7 @@ const isDragging = computed(() => dragState.active && dragState.draggedId === pr
|
||||
</div>
|
||||
</ContextMenuTrigger>
|
||||
|
||||
<ContextMenuContent class="w-auto min-w-36">
|
||||
<ContextMenuContent class="w-auto min-w-36" @close-auto-focus="onContextMenuCloseAutoFocus">
|
||||
<ContextMenuItem v-if="canPin" @click="togglePin">
|
||||
<Pin class="w-4 h-4" :class="{ 'fill-current': isPinned }" />
|
||||
{{ isPinned ? t("contextMenu.unpin") : t("contextMenu.pin") }}
|
||||
@@ -1816,7 +1829,7 @@ const isDragging = computed(() => dragState.active && dragState.draggedId === pr
|
||||
<Plus class="w-4 h-4" /> {{ t("toolbar.newConnection") }}
|
||||
</ContextMenuItem>
|
||||
<ContextMenuSeparator />
|
||||
<ContextMenuItem @click="startRenameGroup">
|
||||
<ContextMenuItem @select="startRenameGroup">
|
||||
<Pencil class="w-4 h-4" /> {{ t("connectionGroup.renameGroup") }}
|
||||
</ContextMenuItem>
|
||||
<ContextMenuSeparator />
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
type RenameInput = Pick<HTMLInputElement, "focus" | "select">;
|
||||
type FrameScheduler = (callback: () => void) => number;
|
||||
type PendingRenameFocusRestore = { value: boolean };
|
||||
|
||||
const scheduleAnimationFrame: FrameScheduler = (callback) => {
|
||||
if (typeof requestAnimationFrame === "function") {
|
||||
return requestAnimationFrame(callback);
|
||||
}
|
||||
return setTimeout(callback, 0) as unknown as number;
|
||||
};
|
||||
|
||||
export function focusSidebarRenameInput(
|
||||
getInput: () => RenameInput | undefined,
|
||||
schedule: FrameScheduler = scheduleAnimationFrame,
|
||||
) {
|
||||
schedule(() => {
|
||||
const input = getInput();
|
||||
input?.focus();
|
||||
input?.select();
|
||||
});
|
||||
}
|
||||
|
||||
export function shouldPreventRenameCloseAutoFocus(pending: PendingRenameFocusRestore): boolean {
|
||||
if (!pending.value) return false;
|
||||
pending.value = false;
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
focusSidebarRenameInput,
|
||||
shouldPreventRenameCloseAutoFocus,
|
||||
} from "../../apps/desktop/src/lib/sidebarRenameFocus.ts";
|
||||
|
||||
test("focusSidebarRenameInput waits for menu focus restoration before focusing", () => {
|
||||
const calls: string[] = [];
|
||||
let scheduled: (() => void) | undefined;
|
||||
|
||||
focusSidebarRenameInput(
|
||||
() => ({
|
||||
focus: () => calls.push("focus"),
|
||||
select: () => calls.push("select"),
|
||||
}),
|
||||
(callback) => {
|
||||
scheduled = callback;
|
||||
return 1;
|
||||
},
|
||||
);
|
||||
|
||||
assert.deepEqual(calls, []);
|
||||
|
||||
scheduled?.();
|
||||
|
||||
assert.deepEqual(calls, ["focus", "select"]);
|
||||
});
|
||||
|
||||
test("focusSidebarRenameInput skips focusing when the input disappeared", () => {
|
||||
let scheduled: (() => void) | undefined;
|
||||
|
||||
focusSidebarRenameInput(
|
||||
() => undefined,
|
||||
(callback) => {
|
||||
scheduled = callback;
|
||||
return 1;
|
||||
},
|
||||
);
|
||||
|
||||
assert.doesNotThrow(() => scheduled?.());
|
||||
});
|
||||
|
||||
test("shouldPreventRenameCloseAutoFocus consumes exactly one pending rename focus restore", () => {
|
||||
const pending = { value: true };
|
||||
|
||||
assert.equal(shouldPreventRenameCloseAutoFocus(pending), true);
|
||||
assert.equal(pending.value, false);
|
||||
assert.equal(shouldPreventRenameCloseAutoFocus(pending), false);
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
const treeItemSource = readFileSync(
|
||||
new URL("../../apps/desktop/src/components/sidebar/TreeItem.vue", import.meta.url),
|
||||
"utf8",
|
||||
);
|
||||
|
||||
test("connection group rename input keeps the normal sidebar text size", () => {
|
||||
const inputMatch = treeItemSource.match(/<input\s+[\s\S]*?ref="renameInputRef"[\s\S]*?class="([^"]+)"/);
|
||||
|
||||
assert.ok(inputMatch, "expected connection group rename input to have a class attribute");
|
||||
assert.ok(!inputMatch[1].split(/\s+/).includes("text-xs"), "rename input should not be smaller than the row label");
|
||||
});
|
||||
Reference in New Issue
Block a user