feat(grid): add configurable shortcut to focus WHERE condition
This commit is contained in:
27 files changed
+325
No files matched your search
@@ -202,6 +202,7 @@ import {
|
||||
isDeleteCurrentRowShortcut,
|
||||
isEditTableStructureShortcut,
|
||||
isFocusSearchShortcut,
|
||||
isFocusWhereShortcut,
|
||||
isGoToColumnShortcut,
|
||||
isGoToFirstPageShortcut,
|
||||
isGoToLastPageShortcut,
|
||||
@@ -1071,6 +1072,7 @@ const transposeScrollRef = ref<HTMLElement | { $el?: HTMLElement }>();
|
||||
const transposeScrollLeft = ref(0);
|
||||
const transposeViewportWidth = ref(0);
|
||||
const { sortColumn: sortCol, sortColumnIndex: sortColIndex, sortDirection: sortDir, sortMode, setSort, clearSort } = useDataGridSort();
|
||||
const queryControlsRef = ref<InstanceType<typeof DataGridQueryControls>>();
|
||||
const searchBarRef = ref<{ focus: (select?: boolean) => void } | null>(null);
|
||||
const replaceOpen = ref(false);
|
||||
const replacementText = ref("");
|
||||
@@ -1889,6 +1891,11 @@ function navigateSuggestion(delta: number) {
|
||||
dataGridSearch.navigateSuggestion(delta);
|
||||
}
|
||||
|
||||
function focusWhere(): boolean {
|
||||
if (!canUseWhereSearch.value) return false;
|
||||
return queryControlsRef.value?.focusWhere() ?? false;
|
||||
}
|
||||
|
||||
function focusSearch(target: Element | null = null): boolean {
|
||||
const tableInfoDrawer = target?.closest<HTMLElement>("[data-table-info-drawer]");
|
||||
if (tableInfoDrawer) {
|
||||
@@ -9464,6 +9471,12 @@ function openCellDetailSearch(): boolean {
|
||||
async function onGridKeydown(event: KeyboardEvent) {
|
||||
if (event.defaultPrevented) return;
|
||||
|
||||
if (isFocusWhereShortcut(event, settingsStore.editorSettings.shortcuts) && focusWhere()) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
return;
|
||||
}
|
||||
|
||||
const targetAllowsNativeClipboard = eventTargetAllowsNativeClipboard(event);
|
||||
if (!targetAllowsNativeClipboard && props.context === "table-data" && canOpenTableStructureEditor.value && isEditTableStructureShortcut(event, settingsStore.editorSettings.shortcuts)) {
|
||||
event.preventDefault();
|
||||
@@ -11924,6 +11937,7 @@ defineExpose({
|
||||
setMultiRowTranspose,
|
||||
toggleMultiRowTranspose,
|
||||
focusSearch,
|
||||
focusWhere,
|
||||
openGoToColumn,
|
||||
visibleColumnCount,
|
||||
displayableColumnCount,
|
||||
@@ -12425,6 +12439,7 @@ useUpdateBlocker(() => (hasPendingChanges.value || hasPendingDataEditorDraft.val
|
||||
</template>
|
||||
<template v-if="canShowWhereSearch">
|
||||
<DataGridQueryControls
|
||||
ref="queryControlsRef"
|
||||
v-model:where-input="whereFilterInput"
|
||||
v-model:order-by-input="orderByInput"
|
||||
v-model:filter-builder-open="filterBuilderOpen"
|
||||
|
||||
@@ -76,6 +76,15 @@ const emit = defineEmits<{
|
||||
|
||||
const { t } = useI18n();
|
||||
const containerRef = ref<HTMLDivElement>();
|
||||
const whereEditorRef = ref<InstanceType<typeof DataGridConditionEditor>>();
|
||||
|
||||
function focusWhere(): boolean {
|
||||
if (!props.canUseWhereSearch || !whereEditorRef.value) return false;
|
||||
whereEditorRef.value.focus();
|
||||
return true;
|
||||
}
|
||||
|
||||
defineExpose({ focusWhere });
|
||||
const filterBuilderRef = ref<InstanceType<typeof DataGridFilterBuilder>>();
|
||||
const pendingFirstEmptyRuleColumnSearch = ref(false);
|
||||
let openingFirstEmptyRuleColumnSearch = false;
|
||||
@@ -262,6 +271,7 @@ onUnmounted(onResizeEnd);
|
||||
<span v-if="filterButtonCount" class="absolute -right-1 -top-1 flex h-3.5 min-w-3.5 items-center justify-center rounded-full bg-primary px-1 text-[9px] leading-none text-primary-foreground">{{ filterButtonCount }}</span>
|
||||
</button>
|
||||
<DataGridConditionEditor
|
||||
ref="whereEditorRef"
|
||||
:model-value="whereInput"
|
||||
kind="where"
|
||||
:columns="conditionColumns"
|
||||
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, defineComponent, h, markRaw, nextTick, type App, type PropType } from "vue";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import i18n from "@/i18n";
|
||||
import type { QueryResult } from "@/types/database";
|
||||
import { TooltipProvider } from "@/components/ui/tooltip";
|
||||
|
||||
vi.mock("@/composables/useDataGridColumnResize", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("@/composables/useDataGridColumnResize")>();
|
||||
const { ref } = await import("vue");
|
||||
return {
|
||||
...actual,
|
||||
useDataGridColumnResize: () => ({
|
||||
initColumnWidths: vi.fn(),
|
||||
onResizeStart: vi.fn(),
|
||||
autoFitColumn: vi.fn(),
|
||||
renderedColumnWidths: ref([120]),
|
||||
totalWidth: ref(120),
|
||||
columnVars: ref({ "--total-w": "120px" }),
|
||||
getIsResizing: () => false,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
import DataGrid from "../DataGrid.vue";
|
||||
import { useSettingsStore } from "@/stores/settingsStore";
|
||||
|
||||
const mountedApps: Array<{ app: App; host: HTMLElement }> = [];
|
||||
|
||||
const RecycleScroller = defineComponent({
|
||||
props: {
|
||||
items: {
|
||||
type: Array as PropType<unknown[]>,
|
||||
default: () => [],
|
||||
},
|
||||
},
|
||||
setup(props, { attrs, slots }) {
|
||||
return () =>
|
||||
h(
|
||||
"div",
|
||||
attrs,
|
||||
props.items.map((item) => slots.default?.({ item })),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
function mountGrid(options: { displayableColumns?: boolean; columns?: string[]; slots?: Record<string, () => ReturnType<typeof h>>; withTableMetadata?: boolean; context?: "table-data" | "results"; shortcut?: string; onExecuteSql?: () => void } = {}) {
|
||||
const pinia = createPinia();
|
||||
setActivePinia(pinia);
|
||||
const settingsStore = useSettingsStore();
|
||||
settingsStore.updateEditorSettings({
|
||||
dataGridRenderMode: "canvas",
|
||||
shortcuts: {
|
||||
...settingsStore.editorSettings.shortcuts,
|
||||
focusWhere: options.shortcut ?? "Mod+Shift+L",
|
||||
},
|
||||
});
|
||||
const result = markRaw<QueryResult>({
|
||||
columns: options.columns ?? ["id"],
|
||||
rows: [Array.from({ length: (options.columns ?? ["id"]).length }, (_, index) => index + 1)],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
hidden_column_indexes: options.displayableColumns === false ? [0] : undefined,
|
||||
});
|
||||
|
||||
const host = document.createElement("div");
|
||||
document.body.append(host);
|
||||
const Root = defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(
|
||||
TooltipProvider,
|
||||
{ delayDuration: 0 },
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
DataGrid,
|
||||
{
|
||||
result,
|
||||
databaseType: "mysql",
|
||||
context: options.context ?? "table-data",
|
||||
onExecuteSql: options.onExecuteSql ?? vi.fn(),
|
||||
...(options.withTableMetadata
|
||||
? {
|
||||
connectionId: "test-connection",
|
||||
database: "test-database",
|
||||
tableMeta: { tableName: "users", schema: "test-database", columns: [], primaryKeys: [] },
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
options.slots,
|
||||
),
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
const app = createApp(Root);
|
||||
app.use(pinia);
|
||||
app.use(i18n);
|
||||
app.component("RecycleScroller", RecycleScroller);
|
||||
app.mount(host);
|
||||
const mounted = { app, host };
|
||||
mountedApps.push(mounted);
|
||||
return { ...mounted, settingsStore };
|
||||
}
|
||||
|
||||
async function settle() {
|
||||
await nextTick();
|
||||
await Promise.resolve();
|
||||
await nextTick();
|
||||
}
|
||||
|
||||
function gridRoot(host: HTMLElement): HTMLElement {
|
||||
const root = host.querySelector<HTMLElement>("[data-grid-root]");
|
||||
if (!root) throw new Error("Data grid root not found");
|
||||
return root;
|
||||
}
|
||||
|
||||
function focusWhereEvent(target: HTMLElement, options: KeyboardEventInit = {}): KeyboardEvent {
|
||||
const event = new KeyboardEvent("keydown", { bubbles: true, cancelable: true, ctrlKey: true, shiftKey: true, key: "L", ...options });
|
||||
target.dispatchEvent(event);
|
||||
return event;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
for (const { app, host } of mountedApps.splice(0)) {
|
||||
app.unmount();
|
||||
host.remove();
|
||||
}
|
||||
});
|
||||
|
||||
describe("DataGrid focus-WHERE shortcut", () => {
|
||||
it.each(["single", "split"] as const)("focuses WHERE without changing or executing its draft in the %s toolbar", async (layout) => {
|
||||
const execute = vi.fn();
|
||||
const { host, settingsStore } = mountGrid({ withTableMetadata: true, onExecuteSql: execute });
|
||||
settingsStore.updateEditorSettings({ dataGridToolbarLayout: layout });
|
||||
await settle();
|
||||
const where = host.querySelector<HTMLTextAreaElement>('textarea[aria-label="WHERE"]')!;
|
||||
expect(where).not.toBeNull();
|
||||
where.value = "id = 42";
|
||||
where.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
await settle();
|
||||
const root = gridRoot(host);
|
||||
root.focus();
|
||||
const bubbled = vi.fn();
|
||||
host.addEventListener("keydown", bubbled);
|
||||
|
||||
const event = focusWhereEvent(root);
|
||||
await settle();
|
||||
|
||||
expect(event.defaultPrevented).toBe(true);
|
||||
expect(bubbled).not.toHaveBeenCalled();
|
||||
expect(document.activeElement).toBe(where);
|
||||
expect(where.value).toBe("id = 42");
|
||||
expect(execute).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("moves focus from ORDER BY to WHERE", async () => {
|
||||
const { host } = mountGrid({ withTableMetadata: true });
|
||||
await settle();
|
||||
const order = host.querySelector<HTMLTextAreaElement>('textarea[aria-label="ORDER BY"]')!;
|
||||
order.focus();
|
||||
expect(focusWhereEvent(order).defaultPrevented).toBe(true);
|
||||
await settle();
|
||||
expect(document.activeElement).toBe(host.querySelector('textarea[aria-label="WHERE"]'));
|
||||
});
|
||||
|
||||
it.each([{ withTableMetadata: false }, { withTableMetadata: true, context: "results" as const }, { withTableMetadata: true, shortcut: "" }])("leaves the key untouched when WHERE is unavailable or the binding is cleared: %j", async (options) => {
|
||||
const { host } = mountGrid(options);
|
||||
await settle();
|
||||
const root = gridRoot(host);
|
||||
root.focus();
|
||||
expect(focusWhereEvent(root).defaultPrevented).toBe(false);
|
||||
await settle();
|
||||
expect(document.activeElement).toBe(root);
|
||||
});
|
||||
|
||||
it.each([{ isComposing: true }, { key: "J" }])("ignores composition and unmatched keys: %j", async (options) => {
|
||||
const { host } = mountGrid({ withTableMetadata: true });
|
||||
await settle();
|
||||
const root = gridRoot(host);
|
||||
root.focus();
|
||||
expect(focusWhereEvent(root, options).defaultPrevented).toBe(false);
|
||||
await settle();
|
||||
expect(document.activeElement).toBe(root);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user