fix(grid): save value editor and cell detail edits via Ctrl+S

This commit is contained in:
kuangzhiqiang authored and GitHub committed 2026-09-30 01:07:11 +08:00
1 parent 26ab78f2d3
commit b41538ba6b
6 files changed
+130 -5

No files matched your search

@@ -6308,6 +6308,19 @@ watch(valueEditorContainer, async (el) => {
onBlur: () => { onBlur: () => {
if (!detailValueDiffOpen.value && !detailTransformOpen.value) commitValueEditorEdit(); if (!detailValueDiffOpen.value && !detailTransformOpen.value) commitValueEditorEdit();
}, },
// 非 temporal 值编辑器(CodeMirror)里按 Ctrl/Cmd+S:仅靠网格全局快捷键不会把
// 草稿提交成待保存变更,必须在这里认领保存键——先 commitValueEditorEdit 落脏,
// 再触发保存(#10515;temporal 走 @save→onTemporalCellEditorSave)。
// useCellDetailEditor 对每个 keydown 都会回调本钩子,必须先用 isSaveShortcut
// 过滤:否则普通按键也被吞掉(preventDefault),编辑器将完全无法输入。
onSaveShortcut: (event) => {
if (!isSaveShortcut(event, settingsStore.editorSettings.shortcuts)) return false;
commitValueEditorEdit();
void nextTick().then(() => {
void saveGridChangesFromShortcut();
});
return true;
},
editorTheme: editorThemeAccessor, editorTheme: editorThemeAccessor,
appAppearance: editorAppAppearance, appAppearance: editorAppAppearance,
appPalette: editorAppPalette, appPalette: editorAppPalette,
@@ -60,7 +60,17 @@ const emit = defineEmits<{
copySqlCondition: []; copySqlCondition: [];
}>(); }>();
const { geometryPreviewOpen, geometryCanvas, detailsEditorContainer, sideJsonPreviewContainer, openSearch } = useDataGridCellDetail({ detail: toRef(props, "detail"), editValue: detailEditValue, onCancel: () => emit("cancel") }); const { geometryPreviewOpen, geometryCanvas, detailsEditorContainer, sideJsonPreviewContainer, openSearch } = useDataGridCellDetail({
detail: toRef(props, "detail"),
editValue: detailEditValue,
onCancel: () => emit("cancel"),
// 详情编辑器里按 Ctrl/Cmd+S 与 temporal 编辑器的 ctrl+s 路径保持一致:
// 先 emit commit(把草稿落成待保存变更),再 emit save(触发网格保存)。
onSave: () => {
emit("commit");
emit("save");
},
});
void geometryCanvas; void geometryCanvas;
void detailsEditorContainer; void detailsEditorContainer;
void sideJsonPreviewContainer; void sideJsonPreviewContainer;
@@ -211,6 +211,12 @@ function finishCommit() {
closeHandled = true; closeHandled = true;
isCommitting = true; isCommitting = true;
emit("commit"); emit("commit");
// 值编辑器面板用 commitValueEditorEdit 保持同一实例不卸载(以便连续编辑),
// isCommitting 若一直闩锁,第二次 ctrl+s/Enter 会在上面的短路处被吞掉(#10515)。
// 本 tick 内仍防重入;下一个微任务复位,让持续挂载的实例可再次提交。
nextTick(() => {
isCommitting = false;
});
} }
function finishCancel() { function finishCancel() {
@@ -218,6 +224,9 @@ function finishCancel() {
closeHandled = true; closeHandled = true;
isCommitting = true; isCommitting = true;
emit("cancel"); emit("cancel");
nextTick(() => {
isCommitting = false;
});
} }
function onKeydown(event: KeyboardEvent) { function onKeydown(event: KeyboardEvent) {
@@ -2,7 +2,7 @@
import { readFileSync } from "node:fs"; import { readFileSync } from "node:fs";
import { resolve } from "node:path"; import { resolve } from "node:path";
import { createApp, defineComponent, h, ref, type App } from "vue"; import { createApp, defineComponent, h, nextTick, ref, type App } from "vue";
import { createPinia, setActivePinia } from "pinia"; import { createPinia, setActivePinia } from "pinia";
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
@@ -99,4 +99,42 @@ describe("TemporalCellEditor save shortcut", () => {
expect(cellDetailPanelSource).toContain("@save=\"emit('save')\""); expect(cellDetailPanelSource).toContain("@save=\"emit('save')\"");
expect(dataGridSource).toMatch(/onTemporalCellEditorSave\(\)[\s\S]*?saveGridChangesFromShortcut\(\)/); expect(dataGridSource).toMatch(/onTemporalCellEditorSave\(\)[\s\S]*?saveGridChangesFromShortcut\(\)/);
}); });
it("re-arms a persistent instance so a value-editor temporal cell can save repeatedly (#10515)", async () => {
// 值编辑器面板用 commitValueEditorEdit 保持 isEditingDetail=true,同一个
// TemporalCellEditor 实例一直挂着;每次 ctrl+s 都必须重新提交,不能被首次提交
// 后闩锁住的 isCommitting 挡住(修复前第二次提交被短路)。
const { input, onCommit, onSave, modelValue } = mountEditor();
input.value = "2026-03-04 05:06:07";
input.dispatchEvent(new Event("input"));
keydown(input, { key: "s", ctrlKey: true });
await nextTick();
expect(onCommit).toHaveBeenCalledTimes(1);
expect(onSave).toHaveBeenCalledTimes(1);
input.value = "2027-07-08 09:10:11";
input.dispatchEvent(new Event("input"));
keydown(input, { key: "s", ctrlKey: true });
await nextTick();
expect(onCommit).toHaveBeenCalledTimes(2);
expect(onSave).toHaveBeenCalledTimes(2);
expect(modelValue.value).toBe("2027-07-08 09:10:11");
});
it("wires onSaveShortcut so the non-temporal value editor can commit + save via Ctrl+S (#10515)", () => {
// useCellDetailEditor(CodeMirror)只认 onSaveShortcut;DataGrid 没传时 varchar 等
// 非 temporal 值编辑器里 ctrl+s 无人消费(必须点「执行」)。这里做结构校验。
expect(dataGridSource).toContain("onSaveShortcut:");
expect(dataGridSource).toMatch(/useCellDetailEditor\(\{[\s\S]{0,2000}?onSaveShortcut[\s\S]{0,1200}?commitValueEditorEdit\(\)/);
// 回归防护:useCellDetailEditor 对每个 keydown 都会回调 onSaveShortcut,处理器
// 必须先用 isSaveShortcut 过滤,否则普通按键也被吞掉,值编辑器完全无法输入。
expect(dataGridSource).toMatch(/onSaveShortcut: \(event\) => \{[\s\S]{0,200}?isSaveShortcut\(event, settingsStore\.editorSettings\.shortcuts\)[\s\S]{0,600}?commitValueEditorEdit\(\)/);
});
it("wires Ctrl+S in the cell details panel editor to commit + save (#10515)", () => {
// 单元格详情 tab 的 CodeMirror 是另一个编辑器实例;ctrl+s 必须与 temporal 详情
// 编辑器的路径一致——先 emit commit(落成待保存变更)再 emit save(触发保存)。
expect(cellDetailPanelSource).toMatch(/onSave: \(\) => \{[\s\S]{0,160}?emit\("commit"\);[\s\S]{0,80}?emit\("save"\);/);
});
}); });
@@ -12,12 +12,14 @@ const mocks = vi.hoisted(() => ({
openSearch: vi.fn(), openSearch: vi.fn(),
focus: vi.fn(), focus: vi.fn(),
onChange: undefined as undefined | ((value: string) => void), onChange: undefined as undefined | ((value: string) => void),
onSaveShortcut: undefined as undefined | ((event: KeyboardEvent) => boolean),
fontFamily: undefined as undefined | (() => string), fontFamily: undefined as undefined | (() => string),
})); }));
vi.mock("@/composables/useCellDetailEditor", () => ({ vi.mock("@/composables/useCellDetailEditor", () => ({
useCellDetailEditor: (options: { onChange?: (value: string) => void; fontFamily: () => string }) => { useCellDetailEditor: (options: { onChange?: (value: string) => void; onSaveShortcut?: (event: KeyboardEvent) => boolean; fontFamily: () => string }) => {
mocks.onChange = options.onChange; mocks.onChange = options.onChange;
mocks.onSaveShortcut = options.onSaveShortcut;
mocks.fontFamily = options.fontFamily; mocks.fontFamily = options.fontFamily;
return { return {
create: mocks.create, create: mocks.create,
@@ -61,6 +63,7 @@ function detail(): DataGridCellDetail {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
mocks.onChange = undefined; mocks.onChange = undefined;
mocks.onSaveShortcut = undefined;
mocks.fontFamily = undefined; mocks.fontFamily = undefined;
mocks.getValue.mockReturnValue(""); mocks.getValue.mockReturnValue("");
}); });
@@ -153,4 +156,41 @@ describe("useDataGridCellDetail", () => {
expect(mocks.setValue).not.toHaveBeenCalled(); expect(mocks.setValue).not.toHaveBeenCalled();
scope.stop(); scope.stop();
}); });
it("claims the save shortcut in the details editor and commits + saves via onSave (#10515)", async () => {
const scope = effectScope();
const onSave = vi.fn();
const composable = scope.run(() => useDataGridCellDetail({ detail: ref(detail()), editValue: ref(""), onCancel: vi.fn(), onSave }))!;
composable.detailsEditorContainer.value = document.createElement("div");
await nextTick();
expect(mocks.create).toHaveBeenCalledOnce();
expect(mocks.onSaveShortcut).toBeTypeOf("function");
// useCellDetailEditor 会对每个 keydown 回调本钩子;普通按键必须原样放行
// (返回 false),否则详情编辑器完全无法输入(#10515 回归)。
expect(mocks.onSaveShortcut?.(new KeyboardEvent("keydown", { key: "a" }))).toBe(false);
expect(mocks.onSaveShortcut?.(new KeyboardEvent("keydown", { key: "Enter" }))).toBe(false);
expect(onSave).not.toHaveBeenCalled();
expect(mocks.onSaveShortcut?.(new KeyboardEvent("keydown", { key: "s", ctrlKey: true }))).toBe(true);
expect(onSave).toHaveBeenCalledOnce();
composable.detailsEditorContainer.value = undefined;
await nextTick();
scope.stop();
});
it("does not claim any key when onSave is not provided", async () => {
const scope = effectScope();
const composable = scope.run(() => useDataGridCellDetail({ detail: ref(detail()), editValue: ref(""), onCancel: vi.fn() }))!;
composable.detailsEditorContainer.value = document.createElement("div");
await nextTick();
expect(mocks.onSaveShortcut).toBeUndefined();
composable.detailsEditorContainer.value = undefined;
await nextTick();
scope.stop();
});
}); });
@@ -1,11 +1,12 @@
import { nextTick, ref, watch, type Ref } from "vue"; import { nextTick, ref, watch, type Ref } from "vue";
import { useCellDetailEditor, type UseCellDetailEditorReturn } from "@/composables/useCellDetailEditor"; import { useCellDetailEditor, type UseCellDetailEditorReturn } from "@/composables/useCellDetailEditor";
import { isSaveShortcut } from "@/lib/editor/keyboardShortcuts";
import { useTheme } from "@/composables/useTheme"; import { useTheme } from "@/composables/useTheme";
import { useSettingsStore } from "@/stores/settingsStore"; import { useSettingsStore } from "@/stores/settingsStore";
import { renderWktOnCanvas } from "@/lib/dataGrid/geometryPreview"; import { renderWktOnCanvas } from "@/lib/dataGrid/geometryPreview";
import type { DataGridCellDetail } from "@/lib/dataGrid/dataGridDetail"; import type { DataGridCellDetail } from "@/lib/dataGrid/dataGridDetail";
export function useDataGridCellDetail(options: { detail: Ref<DataGridCellDetail>; editValue: Ref<string>; onCancel: () => void }) { export function useDataGridCellDetail(options: { detail: Ref<DataGridCellDetail>; editValue: Ref<string>; onCancel: () => void; onSave?: () => void }) {
const settingsStore = useSettingsStore(); const settingsStore = useSettingsStore();
const { isDark, themePalette } = useTheme(); const { isDark, themePalette } = useTheme();
const geometryPreviewOpen = ref(false); const geometryPreviewOpen = ref(false);
@@ -33,7 +34,21 @@ export function useDataGridCellDetail(options: { detail: Ref<DataGridCellDetail>
watch(detailsEditorContainer, async (element) => { watch(detailsEditorContainer, async (element) => {
if (element && !detailsEditor) { if (element && !detailsEditor) {
const editor = useCellDetailEditor({ onChange: (value) => (options.editValue.value = value), onEscape: options.onCancel, ...editorOptions() }); const editor = useCellDetailEditor({
onChange: (value) => (options.editValue.value = value),
onEscape: options.onCancel,
// 详情面板 CodeMirror 里的 Ctrl/Cmd+S:提交草稿成待保存变更并保存(#10515)。
// useCellDetailEditor 对每个 keydown 都会回调本钩子,必须先用 isSaveShortcut
// 过滤,否则普通按键也会被吞掉(preventDefault)导致无法输入。
onSaveShortcut: options.onSave
? (event) => {
if (!isSaveShortcut(event, settingsStore.editorSettings.shortcuts)) return false;
options.onSave?.();
return true;
}
: undefined,
...editorOptions(),
});
detailsEditor = editor; detailsEditor = editor;
await editor.create(element, options.editValue.value, options.detail.value.type); await editor.create(element, options.editValue.value, options.detail.value.type);
if (detailsEditor === editor && editor.getValue() !== options.editValue.value) { if (detailsEditor === editor && editor.getValue() !== options.editValue.value) {