fix(grid): commit and save temporal cell edits on Ctrl+S

This commit is contained in:
zipg authored and GitHub committed 2026-09-28 23:03:51 +08:00
1 parent 625d84bb4a
commit cf615f6d15
4 files changed
+131 -1

No files matched your search

@@ -9375,6 +9375,14 @@ async function saveGridChangesFromShortcut() {
return true; return true;
} }
/// datetime/date/time 单元格编辑器会吃掉自身按键,保存快捷键由它转发成 save 事件后在这里补上保存(#10515)。
async function onTemporalCellEditorSave() {
await nextTick();
if (await saveGridChangesFromShortcut()) {
gridRef.value?.focus({ preventScroll: true });
}
}
async function onCellEditKeydown(event: KeyboardEvent) { async function onCellEditKeydown(event: KeyboardEvent) {
if (isSaveShortcut(event, settingsStore.editorSettings.shortcuts)) { if (isSaveShortcut(event, settingsStore.editorSettings.shortcuts)) {
event.preventDefault(); event.preventDefault();
@@ -12870,6 +12878,7 @@ useUpdateBlocker(() => (hasPendingChanges.value || hasPendingDataEditorDraft.val
cell-layout="transpose" cell-layout="transpose"
@cancel="cancelEdit" @cancel="cancelEdit"
@commit="commitGridEdit" @commit="commitGridEdit"
@save="onTemporalCellEditorSave"
/> />
<EnumCellEditor <EnumCellEditor
v-else-if="isBooleanGridCell(displayItems[cell.recordIndex], cell.valueIndex)" v-else-if="isBooleanGridCell(displayItems[cell.recordIndex], cell.valueIndex)"
@@ -13562,6 +13571,7 @@ useUpdateBlocker(() => (hasPendingChanges.value || hasPendingDataEditorDraft.val
:normalize-value="(value) => normalizeTemporalCellEditorValue(value, canvasEditingCell!.actualColIdx)" :normalize-value="(value) => normalizeTemporalCellEditorValue(value, canvasEditingCell!.actualColIdx)"
@cancel="cancelEdit" @cancel="cancelEdit"
@commit="commitGridEdit" @commit="commitGridEdit"
@save="onTemporalCellEditorSave"
/> />
<EnumCellEditor <EnumCellEditor
v-else-if="isBooleanGridCell(getRowItem(canvasEditingCell.rowId), canvasEditingCell.actualColIdx)" v-else-if="isBooleanGridCell(getRowItem(canvasEditingCell.rowId), canvasEditingCell.actualColIdx)"
@@ -13782,6 +13792,7 @@ useUpdateBlocker(() => (hasPendingChanges.value || hasPendingDataEditorDraft.val
:normalize-value="(value) => normalizeTemporalCellEditorValue(value, col.actualColIdx)" :normalize-value="(value) => normalizeTemporalCellEditorValue(value, col.actualColIdx)"
@cancel="cancelEdit" @cancel="cancelEdit"
@commit="commitGridEdit" @commit="commitGridEdit"
@save="onTemporalCellEditorSave"
/> />
<EnumCellEditor <EnumCellEditor
v-else-if="isBooleanGridCell(item, col.actualColIdx)" v-else-if="isBooleanGridCell(item, col.actualColIdx)"
@@ -14129,6 +14140,7 @@ useUpdateBlocker(() => (hasPendingChanges.value || hasPendingDataEditorDraft.val
@copy-value="copyDetailCurrentValue" @copy-value="copyDetailCurrentValue"
@commit="commitDetailEdit" @commit="commitDetailEdit"
@cancel="cancelDetailEdit" @cancel="cancelDetailEdit"
@save="onTemporalCellEditorSave"
@set-null="setDetailNull" @set-null="setDetailNull"
@copy-column-name="copyDetailColumnName" @copy-column-name="copyDetailColumnName"
@copy-sql-condition="copyDetailSqlCondition" @copy-sql-condition="copyDetailSqlCondition"
@@ -14146,6 +14158,7 @@ useUpdateBlocker(() => (hasPendingChanges.value || hasPendingDataEditorDraft.val
:commit-on-close="false" :commit-on-close="false"
@cancel="cancelValueEditorEdit" @cancel="cancelValueEditorEdit"
@commit="commitValueEditorEdit" @commit="commitValueEditorEdit"
@save="onTemporalCellEditorSave"
/> />
<div v-else ref="valueEditorContainer" data-cell-detail-editor-root class="min-h-0 min-w-0 flex-1 w-full rounded border overflow-auto" /> <div v-else ref="valueEditorContainer" data-cell-detail-editor-root class="min-h-0 min-w-0 flex-1 w-full rounded border overflow-auto" />
</div> </div>
@@ -54,6 +54,7 @@ const emit = defineEmits<{
copyValue: []; copyValue: [];
commit: []; commit: [];
cancel: []; cancel: [];
save: [];
setNull: []; setNull: [];
copyColumnName: []; copyColumnName: [];
copySqlCondition: []; copySqlCondition: [];
@@ -183,7 +184,7 @@ defineExpose({ openSearch });
</div> </div>
<template v-if="editing" <template v-if="editing"
><div class="dbx-data-grid-value-font min-h-0 min-w-0 flex-1" :style="editorStyle"> ><div class="dbx-data-grid-value-font min-h-0 min-w-0 flex-1" :style="editorStyle">
<TemporalCellEditor v-if="temporalEditorConfig" v-model="detailEditValue" :kind="temporalEditorConfig.kind" :fraction-precision="temporalEditorConfig.fractionPrecision" variant="inline" :commit-on-close="false" @cancel="emit('cancel')" @commit="emit('commit')" /> <TemporalCellEditor v-if="temporalEditorConfig" v-model="detailEditValue" :kind="temporalEditorConfig.kind" :fraction-precision="temporalEditorConfig.fractionPrecision" variant="inline" :commit-on-close="false" @cancel="emit('cancel')" @commit="emit('commit')" @save="emit('save')" />
<div v-else ref="detailsEditorContainer" data-cell-detail-editor-root class="min-h-0 h-full w-full rounded border overflow-hidden" /> <div v-else ref="detailsEditorContainer" data-cell-detail-editor-root class="min-h-0 h-full w-full rounded border overflow-hidden" />
</div> </div>
<div v-if="!panelIsBottom" class="flex shrink-0 gap-1 py-0.5"> <div v-if="!panelIsBottom" class="flex shrink-0 gap-1 py-0.5">
@@ -3,9 +3,13 @@ import { computed, nextTick, onMounted, ref, watch } from "vue";
import type { FocusOutsideEvent, PointerDownOutsideEvent } from "reka-ui"; import type { FocusOutsideEvent, PointerDownOutsideEvent } from "reka-ui";
import { CalendarClock, ChevronDown, ChevronUp, CircleSlash } from "@lucide/vue"; import { CalendarClock, ChevronDown, ChevronUp, CircleSlash } from "@lucide/vue";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { isSaveShortcut } from "@/lib/editor/keyboardShortcuts";
import { useSettingsStore } from "@/stores/settingsStore";
import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { formatTemporalInputValue, hostTimezoneOffsetSuffix, parseTemporalInputValue, stepTemporalInputValue, temporalOffsetSuffix, type TemporalCellEditorKind } from "@/lib/dataGrid/dataGridTemporalEditor"; import { formatTemporalInputValue, hostTimezoneOffsetSuffix, parseTemporalInputValue, stepTemporalInputValue, temporalOffsetSuffix, type TemporalCellEditorKind } from "@/lib/dataGrid/dataGridTemporalEditor";
const settingsStore = useSettingsStore();
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
kind: TemporalCellEditorKind; kind: TemporalCellEditorKind;
@@ -28,6 +32,7 @@ const emit = defineEmits<{
"update:modelValue": [value: string]; "update:modelValue": [value: string];
commit: []; commit: [];
cancel: []; cancel: [];
save: [];
}>(); }>();
const open = ref(false); const open = ref(false);
@@ -216,6 +221,15 @@ function finishCancel() {
} }
function onKeydown(event: KeyboardEvent) { function onKeydown(event: KeyboardEvent) {
// 编辑器的 keydown 一律 stop 掉,因此网格上的保存快捷键不会冒泡过去;
// 这里必须自己识别并转发,否则 datetime/date/time 单元格编辑期间按 Ctrl/Cmd+S 会被静默丢弃。
if (isSaveShortcut(event, settingsStore.editorSettings.shortcuts)) {
event.preventDefault();
flushInputValue(event.target);
finishCommit();
emit("save");
return;
}
if (event.key === "Enter") { if (event.key === "Enter") {
event.preventDefault(); event.preventDefault();
flushInputValue(event.target); flushInputValue(event.target);
@@ -0,0 +1,102 @@
// @vitest-environment happy-dom
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { createApp, defineComponent, h, ref, type App } from "vue";
import { createPinia, setActivePinia } from "pinia";
import { afterEach, describe, expect, it, vi } from "vitest";
import TemporalCellEditor from "../TemporalCellEditor.vue";
const dataGridSource = readFileSync(resolve(import.meta.dirname, "../DataGrid.vue"), "utf8");
const cellDetailPanelSource = readFileSync(resolve(import.meta.dirname, "../DataGridCellDetailPanel.vue"), "utf8");
const dataGridTemporalEditors = dataGridSource.match(/<TemporalCellEditor\b[\s\S]*?\/>/g) ?? [];
const mountedApps: Array<{ app: App; host: HTMLElement }> = [];
function mountEditor() {
const pinia = createPinia();
setActivePinia(pinia);
const modelValue = ref("2026-01-02 03:04:05");
const onCommit = vi.fn();
const onCancel = vi.fn();
const onSave = vi.fn();
const host = document.createElement("div");
document.body.append(host);
const Root = defineComponent({
setup() {
return () =>
h(TemporalCellEditor, {
kind: "datetime",
modelValue: modelValue.value,
"onUpdate:modelValue": (value: string) => {
modelValue.value = value;
},
onCommit,
onCancel,
onSave,
});
},
});
const app = createApp(Root);
app.use(pinia);
app.mount(host);
mountedApps.push({ app, host });
const input = host.querySelector("input");
if (!input) throw new Error("Temporal editor input not found");
return { host, input, modelValue, onCommit, onCancel, onSave };
}
function keydown(target: HTMLInputElement, init: KeyboardEventInit): KeyboardEvent {
const event = new KeyboardEvent("keydown", { bubbles: true, cancelable: true, ...init });
target.dispatchEvent(event);
return event;
}
afterEach(() => {
for (const { app, host } of mountedApps.splice(0)) {
app.unmount();
host.remove();
}
});
describe("TemporalCellEditor save shortcut", () => {
it("forwards Ctrl/Cmd+S to the grid instead of dropping it (#10515)", () => {
const { input, onCommit, onCancel, onSave, modelValue } = mountEditor();
input.value = "2026-03-04 05:06:07";
input.dispatchEvent(new Event("input"));
const event = keydown(input, { key: "s", ctrlKey: true });
expect(event.defaultPrevented).toBe(true);
expect(modelValue.value).toBe("2026-03-04 05:06:07");
expect(onCommit).toHaveBeenCalledOnce();
expect(onSave).toHaveBeenCalledOnce();
expect(onCancel).not.toHaveBeenCalled();
});
it("keeps the existing Enter commit and Escape cancel behaviour", () => {
const enterCase = mountEditor();
const enter = keydown(enterCase.input, { key: "Enter" });
expect(enter.defaultPrevented).toBe(true);
expect(enterCase.onCommit).toHaveBeenCalledOnce();
expect(enterCase.onSave).not.toHaveBeenCalled();
const escapeCase = mountEditor();
const escape = keydown(escapeCase.input, { key: "Escape" });
expect(escape.defaultPrevented).toBe(true);
expect(escapeCase.onCancel).toHaveBeenCalledOnce();
expect(escapeCase.onCommit).not.toHaveBeenCalled();
expect(escapeCase.onSave).not.toHaveBeenCalled();
});
it("binds the forwarded save event on every grid temporal editor", () => {
expect(dataGridTemporalEditors.length).toBeGreaterThan(0);
for (const editor of dataGridTemporalEditors) {
expect(editor).toContain('@save="onTemporalCellEditorSave"');
}
expect(cellDetailPanelSource).toContain("@save=\"emit('save')\"");
expect(dataGridSource).toMatch(/onTemporalCellEditorSave\(\)[\s\S]*?saveGridChangesFromShortcut\(\)/);
});
});