feat(grid): improve WHERE condition completion
This commit is contained in:
10 files changed
+576
-56
No files matched your search
@@ -216,7 +216,7 @@ import { supportsTableStructureEditing } from "@/lib/database/databaseCapabiliti
|
||||
import { rememberDataGridConditionHistory } from "@/lib/dataGrid/dataGridConditionHistory";
|
||||
import { restoreDataGridLocalColumnFilters, serializeDataGridLocalColumnFilters } from "@/lib/dataGrid/dataGridLocalColumnFilterState";
|
||||
import { effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
|
||||
import { dataGridConditionColumnOptions } from "@/lib/dataGrid/dataGridConditionCompletion";
|
||||
import { dataGridConditionColumnOptions, dataGridConditionIdentifierQuote } from "@/lib/dataGrid/dataGridConditionCompletion";
|
||||
import { isMacOS } from "@/lib/backend/platform";
|
||||
import { appendDebugLog, isDebugLoggingEnabled } from "@/lib/backend/debugLog";
|
||||
import { formatShortcut } from "@/lib/editor/shortcutRegistry";
|
||||
@@ -615,6 +615,7 @@ const { searchText, deferredSearchText: deferredClientSearchText, overlayVisible
|
||||
const orderByInput = ref(props.initialOrderByInput ?? "");
|
||||
const whereFilterInput = ref(props.initialWhereInput ?? "");
|
||||
const conditionColumns = computed(() => dataGridConditionColumnOptions(props.tableMeta?.columns ?? props.result.columns, resolvedDatabaseType.value));
|
||||
const conditionIdentifierQuote = computed(() => dataGridConditionIdentifierQuote(resolvedDatabaseType.value, connectionStore.connectionIdentifierQuote?.(props.connectionId)));
|
||||
const conditionHistoryScope = computed(() => ({
|
||||
connectionId: props.connectionId,
|
||||
database: props.database,
|
||||
@@ -7774,6 +7775,7 @@ const gridContextMenuItems = computed<ContextMenuItem[]>(() => {
|
||||
v-model:filter-builder-open="filterBuilderOpen"
|
||||
:columns="props.tableMeta?.columns.map((column) => column.name) ?? props.result.columns"
|
||||
:condition-columns="conditionColumns"
|
||||
:identifier-quote="conditionIdentifierQuote"
|
||||
:history-scope="conditionHistoryScope"
|
||||
:can-use-where-search="canUseWhereSearch"
|
||||
:compact="compactDataGridToolbar"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onMounted, onUnmounted, ref, useId, watch, type CSSProperties } from "vue";
|
||||
import { ChevronDown, X } from "@lucide/vue";
|
||||
import { useDataGridConditionEditor, type DataGridConditionColumnOption, type DataGridConditionSuggestionProvider } from "@/composables/useDataGridConditionEditor";
|
||||
import { completeDataGridConditionQuote, useDataGridConditionEditor, type DataGridConditionColumnOption, type DataGridConditionSuggestionProvider } from "@/composables/useDataGridConditionEditor";
|
||||
import { getDataGridConditionSuggestionPosition, getDataGridConditionSuggestionPreferredWidth } from "@/lib/dataGrid/dataGridConditionSuggestionPosition";
|
||||
import type { DataGridConditionHistoryKind, DataGridConditionHistoryScope } from "@/lib/dataGrid/dataGridConditionHistory";
|
||||
|
||||
@@ -14,6 +14,7 @@ const props = withDefaults(
|
||||
ariaLabel?: string;
|
||||
historyEmptyText?: string;
|
||||
historyNoMatchesText?: string;
|
||||
identifierQuote?: string;
|
||||
suggestionProvider?: DataGridConditionSuggestionProvider;
|
||||
suggestionDebounceMs?: number;
|
||||
disabled?: boolean;
|
||||
@@ -39,6 +40,8 @@ const emit = defineEmits<{
|
||||
}>();
|
||||
|
||||
const inputRef = ref<HTMLTextAreaElement>();
|
||||
const selectionStart = ref(modelValue.value.length);
|
||||
const selectionEnd = ref(modelValue.value.length);
|
||||
const suggestionListId = `${useId()}-${props.kind}-condition-suggestions`;
|
||||
const overlayRef = ref<HTMLTextAreaElement>();
|
||||
const controlRef = ref<HTMLDivElement>();
|
||||
@@ -56,6 +59,9 @@ let expandAfterComposition = false;
|
||||
const editor = useDataGridConditionEditor({
|
||||
kind: props.kind,
|
||||
value: modelValue,
|
||||
selectionStart,
|
||||
selectionEnd,
|
||||
identifierQuote: () => props.identifierQuote,
|
||||
columns: () => props.columns,
|
||||
historyScope: () => props.historyScope,
|
||||
suggestionProvider: props.suggestionProvider,
|
||||
@@ -166,9 +172,9 @@ function resizeEditor(forceExpand = false) {
|
||||
void nextTick(() => {
|
||||
const overlay = overlayRef.value;
|
||||
if (!overlay || composing.value) return;
|
||||
const start = input.selectionStart;
|
||||
syncSelection(input);
|
||||
overlay.focus();
|
||||
overlay.setSelectionRange(start, start);
|
||||
overlay.setSelectionRange(selectionStart.value, selectionEnd.value);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -188,7 +194,31 @@ function onCompositionEnd() {
|
||||
function focus(select = false) {
|
||||
const target = activeEditor.value ?? inputRef.value;
|
||||
target?.focus();
|
||||
if (select) target?.select();
|
||||
if (select) {
|
||||
target?.select();
|
||||
if (target) syncSelection(target);
|
||||
} else {
|
||||
target?.setSelectionRange(selectionStart.value, selectionEnd.value);
|
||||
}
|
||||
resizeEditor(true);
|
||||
}
|
||||
|
||||
function syncSelection(target: HTMLTextAreaElement) {
|
||||
selectionStart.value = target.selectionStart;
|
||||
selectionEnd.value = target.selectionEnd;
|
||||
}
|
||||
|
||||
function onFocus(event: FocusEvent) {
|
||||
syncSelection(event.currentTarget as HTMLTextAreaElement);
|
||||
resizeEditor(true);
|
||||
}
|
||||
|
||||
function onSelectionChange(event: Event) {
|
||||
syncSelection(event.currentTarget as HTMLTextAreaElement);
|
||||
}
|
||||
|
||||
function onClick(event: MouseEvent) {
|
||||
onSelectionChange(event);
|
||||
resizeEditor(true);
|
||||
}
|
||||
|
||||
@@ -201,7 +231,8 @@ function scheduleCollapse() {
|
||||
}, 0);
|
||||
}
|
||||
|
||||
function onInput() {
|
||||
function onInput(event: Event) {
|
||||
syncSelection(event.currentTarget as HTMLTextAreaElement);
|
||||
resizeEditor(true);
|
||||
updateSuggestionPosition();
|
||||
}
|
||||
@@ -221,11 +252,29 @@ async function clearCondition() {
|
||||
}
|
||||
|
||||
function onKeydown(event: KeyboardEvent) {
|
||||
if (completeQuote(event)) return;
|
||||
const action = editor.handleKeydown(event);
|
||||
if (action === "apply") void applyCondition();
|
||||
if (action === "accept") void nextTick(() => focus());
|
||||
}
|
||||
|
||||
function completeQuote(event: KeyboardEvent) {
|
||||
if (props.kind !== "where" || (event.key !== "'" && event.key !== '"') || event.isComposing || event.keyCode === 229 || event.metaKey || event.ctrlKey || event.altKey) return false;
|
||||
const target = event.currentTarget as HTMLTextAreaElement;
|
||||
const completion = completeDataGridConditionQuote(modelValue.value, target.selectionStart, target.selectionEnd, event.key);
|
||||
event.preventDefault();
|
||||
modelValue.value = completion.value;
|
||||
selectionStart.value = completion.selectionStart;
|
||||
selectionEnd.value = completion.selectionEnd;
|
||||
editor.dismiss();
|
||||
void nextTick(() => {
|
||||
const active = activeEditor.value;
|
||||
active?.focus();
|
||||
active?.setSelectionRange(completion.selectionStart, completion.selectionEnd);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
function openHistory() {
|
||||
editor.openHistory();
|
||||
updateSuggestionPosition();
|
||||
@@ -340,9 +389,11 @@ defineExpose({ focus, dismiss: editor.dismiss, rememberHistory: editor.rememberH
|
||||
class="data-grid-topbar-condition-input absolute inset-x-0 top-0 h-6 min-w-0 resize-none bg-transparent outline-none"
|
||||
:class="[props.kind === 'where' ? 'data-grid-topbar-condition-input--where' : 'data-grid-topbar-condition-input--order', { 'data-grid-topbar-condition-input--compact': props.compact }]"
|
||||
style="height: 24px"
|
||||
@focus="resizeEditor(true)"
|
||||
@focus="onFocus"
|
||||
@blur="scheduleCollapse"
|
||||
@click="resizeEditor(true)"
|
||||
@click="onClick"
|
||||
@select="onSelectionChange"
|
||||
@keyup="onSelectionChange"
|
||||
@compositionstart="onCompositionStart"
|
||||
@compositionend="onCompositionEnd"
|
||||
@input="onInput"
|
||||
@@ -373,6 +424,10 @@ defineExpose({ focus, dismiss: editor.dismiss, rememberHistory: editor.rememberH
|
||||
class="data-grid-topbar-condition-input data-grid-topbar-condition-input--expanded absolute resize-none outline-none"
|
||||
:class="[props.kind === 'where' ? 'data-grid-topbar-condition-input--where' : 'data-grid-topbar-condition-input--order', { 'data-grid-topbar-condition-input--compact': props.compact }]"
|
||||
@blur="scheduleCollapse"
|
||||
@focus="onFocus"
|
||||
@click="onSelectionChange"
|
||||
@select="onSelectionChange"
|
||||
@keyup="onSelectionChange"
|
||||
@compositionstart="onCompositionStart"
|
||||
@compositionend="onCompositionEnd"
|
||||
@input="onInput"
|
||||
|
||||
@@ -24,6 +24,7 @@ const props = defineProps<{
|
||||
orderByInput: string;
|
||||
columns: readonly string[];
|
||||
conditionColumns: readonly DataGridConditionColumnOption[];
|
||||
identifierQuote?: string;
|
||||
historyScope: DataGridConditionHistoryScope;
|
||||
canUseWhereSearch: boolean;
|
||||
compact: boolean;
|
||||
@@ -179,6 +180,7 @@ onUnmounted(onResizeEnd);
|
||||
:model-value="whereInput"
|
||||
kind="where"
|
||||
:columns="conditionColumns"
|
||||
:identifier-quote="identifierQuote"
|
||||
:history-scope="historyScope"
|
||||
placeholder="WHERE"
|
||||
:history-empty-text="t('grid.conditionHistoryEmpty')"
|
||||
@@ -204,6 +206,7 @@ onUnmounted(onResizeEnd);
|
||||
:model-value="orderByInput"
|
||||
kind="orderBy"
|
||||
:columns="conditionColumns"
|
||||
:identifier-quote="identifierQuote"
|
||||
:history-scope="historyScope"
|
||||
placeholder="ORDER BY"
|
||||
:history-empty-text="t('grid.conditionHistoryEmpty')"
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, defineComponent, h, nextTick, ref, type App } from "vue";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import DataGridConditionEditor from "@/components/grid/DataGridConditionEditor.vue";
|
||||
import type { DataGridConditionHistoryKind } from "@/lib/dataGrid/dataGridConditionHistory";
|
||||
|
||||
const mountedApps: Array<{ app: App; host: HTMLElement }> = [];
|
||||
|
||||
function mountEditor(kind: DataGridConditionHistoryKind, initialValue: string, options: { columns?: string[]; identifierQuote?: string } = {}) {
|
||||
const value = ref(initialValue);
|
||||
const host = document.createElement("div");
|
||||
document.body.appendChild(host);
|
||||
const app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(DataGridConditionEditor, {
|
||||
kind,
|
||||
modelValue: value.value,
|
||||
"onUpdate:modelValue": (nextValue: string) => (value.value = nextValue),
|
||||
historyScope: {},
|
||||
columns: options.columns,
|
||||
identifierQuote: options.identifierQuote,
|
||||
});
|
||||
},
|
||||
}),
|
||||
);
|
||||
app.mount(host);
|
||||
mountedApps.push({ app, host });
|
||||
return { value, input: host.querySelector("textarea") as HTMLTextAreaElement };
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const { app, host } of mountedApps.splice(0)) {
|
||||
app.unmount();
|
||||
host.remove();
|
||||
}
|
||||
});
|
||||
|
||||
describe("DataGridConditionEditor quote completion", () => {
|
||||
it("inserts paired quotes in WHERE and places the caret between them", async () => {
|
||||
const { value, input } = mountEditor("where", "id = ");
|
||||
input.focus();
|
||||
input.setSelectionRange(5, 5);
|
||||
|
||||
const event = new KeyboardEvent("keydown", { key: "'", bubbles: true, cancelable: true });
|
||||
input.dispatchEvent(event);
|
||||
await nextTick();
|
||||
|
||||
expect(event.defaultPrevented).toBe(true);
|
||||
expect(value.value).toBe("id = ''");
|
||||
expect(input.selectionStart).toBe(6);
|
||||
expect(input.selectionEnd).toBe(6);
|
||||
});
|
||||
|
||||
it("wraps selected WHERE text and skips an existing closing quote", async () => {
|
||||
const { value, input } = mountEditor("where", "name");
|
||||
input.focus();
|
||||
input.setSelectionRange(0, 4);
|
||||
input.dispatchEvent(new KeyboardEvent("keydown", { key: '"', bubbles: true, cancelable: true }));
|
||||
await nextTick();
|
||||
|
||||
expect(value.value).toBe('"name"');
|
||||
expect(input.selectionStart).toBe(1);
|
||||
expect(input.selectionEnd).toBe(5);
|
||||
|
||||
input.setSelectionRange(5, 5);
|
||||
input.dispatchEvent(new KeyboardEvent("keydown", { key: '"', bubbles: true, cancelable: true }));
|
||||
await nextTick();
|
||||
expect(value.value).toBe('"name"');
|
||||
expect(input.selectionStart).toBe(6);
|
||||
});
|
||||
|
||||
it("does not intercept quotes in ORDER BY", () => {
|
||||
const { value, input } = mountEditor("orderBy", "name");
|
||||
input.focus();
|
||||
input.setSelectionRange(4, 4);
|
||||
|
||||
const event = new KeyboardEvent("keydown", { key: '"', bubbles: true, cancelable: true });
|
||||
input.dispatchEvent(event);
|
||||
|
||||
expect(event.defaultPrevented).toBe(false);
|
||||
expect(value.value).toBe("name");
|
||||
});
|
||||
|
||||
it("passes the textarea caret range through when accepting a suggestion", async () => {
|
||||
const { value, input } = mountEditor("where", "status = cus AND enabled = 1", { columns: ["customer_id"] });
|
||||
input.focus();
|
||||
input.setSelectionRange(12, 12);
|
||||
input.dispatchEvent(new Event("select", { bubbles: true }));
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(document.querySelector('[role="option"]')?.textContent).toContain("customer_id"));
|
||||
|
||||
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Tab", bubbles: true, cancelable: true }));
|
||||
await nextTick();
|
||||
|
||||
expect(value.value).toBe("status = customer_id AND enabled = 1");
|
||||
expect(input.selectionStart).toBe(20);
|
||||
expect(input.selectionEnd).toBe(20);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user