Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sql-editor): auto-re-focus to active SQL Editor when needed (#4653)
Liu Jid46b09d2d8e8
diff --git a/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue b/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vueindex 31d3dd8094..4b0360683d 100644--- a/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue+++ b/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue@@ -20,6 +20,9 @@<CompactSQLEditorv-model:sql="query.sql"class="min-h-[2rem]"+ :class="[+ isEditableQueryItem(query) ? 'active-editor' : 'read-only-editor',+ ]":readonly="!isEditableQueryItem(query)"@execute="handleExecute"@history="handleHistory"@@ -80,6 +83,7 @@ import {import { useExecuteSQL } from "@/composables/useExecuteSQL";import { useCancelableTimeout } from "@/composables/useCancelableTimeout";import { useHistory } from "./useHistory";+import { useAttractFocus } from "./useAttractFocus";const QUERY_TIMEOUT_MS = 5000;const MAX_QUERY_ITEM_COUNT = 20;@@ -192,4 +196,9 @@ watch(queryListHeight, () => {}});});++useAttractFocus({+ excluded: [{ tag: "textarea", selector: ".active-editor" }],+ targetSelector: ".active-editor textarea",+});</script>diff --git a/frontend/src/views/sql-editor/TerminalPanel/useAttractFocus.ts b/frontend/src/views/sql-editor/TerminalPanel/useAttractFocus.tsnew file mode 100644index 0000000000..ce4f732249--- /dev/null+++ b/frontend/src/views/sql-editor/TerminalPanel/useAttractFocus.ts@@ -0,0 +1,62 @@+import { MaybeRef } from "@/types";+import { isDescendantOf } from "@/utils";+import { useEventListener } from "@vueuse/core";+import { unref } from "vue";++const FOCUS_ATTRACTIVE_KEYS: (RegExp | string)[] = [+ /^[A-Za-z0-9\s.`~!@#$%^&*()-=[\]\\;',./_+{}|:"<>?]$/,+ "ArrowUp",+ "ArrowDown",+ "ArrowLeft",+ "ArrowRight",+ "Backspace",+ "Delete",+ "Home",+ "End",+];++const shouldAttractFocus = (e: KeyboardEvent) => {+ if (e.metaKey || e.ctrlKey) {+ // A downgrade for safety since key strokes with cmd or ctrl might+ // probably be a system-wide keyboard shortcut.+ return false;+ }+ return FOCUS_ATTRACTIVE_KEYS.some((rule) => {+ if (typeof rule === "string") {+ return e.key === rule;+ }+ return e.key.match(rule);+ });+};++export type AttractFocusOptions = {+ excluded?: { tag: string; selector: string }[];+ targetSelector: string;+};++export const useAttractFocus = (options: MaybeRef<AttractFocusOptions>) => {+ useEventListener("keydown", (e: KeyboardEvent) => {+ const sourceElement = e.target as Element;+ const tag = sourceElement.tagName.toLowerCase();+ if (tag === "input") {+ // Don't rob the focus in other text boxes+ return;+ }+ const { excluded = [], targetSelector } = unref(options);+ for (let i = 0; i < excluded.length; i++) {+ const rule = excluded[i];+ if (tag === rule.tag && isDescendantOf(sourceElement, rule.selector)) {+ return;+ }+ }+ if (!shouldAttractFocus(e)) {+ return;+ }++ const target = document.querySelector(+ targetSelector+ ) as HTMLTextAreaElement | null;+ // monaco-editor will consume the focus event with key stroke11+ target?.focus();+ });+};