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

@@ -12,12 +12,14 @@ const mocks = vi.hoisted(() => ({
openSearch: vi.fn(),
focus: vi.fn(),
onChange: undefined as undefined | ((value: string) => void),
onSaveShortcut: undefined as undefined | ((event: KeyboardEvent) => boolean),
fontFamily: undefined as undefined | (() => string),
}));
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.onSaveShortcut = options.onSaveShortcut;
mocks.fontFamily = options.fontFamily;
return {
create: mocks.create,
@@ -61,6 +63,7 @@ function detail(): DataGridCellDetail {
beforeEach(() => {
vi.clearAllMocks();
mocks.onChange = undefined;
mocks.onSaveShortcut = undefined;
mocks.fontFamily = undefined;
mocks.getValue.mockReturnValue("");
});
@@ -153,4 +156,41 @@ describe("useDataGridCellDetail", () => {
expect(mocks.setValue).not.toHaveBeenCalled();
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 { useCellDetailEditor, type UseCellDetailEditorReturn } from "@/composables/useCellDetailEditor";
import { isSaveShortcut } from "@/lib/editor/keyboardShortcuts";
import { useTheme } from "@/composables/useTheme";
import { useSettingsStore } from "@/stores/settingsStore";
import { renderWktOnCanvas } from "@/lib/dataGrid/geometryPreview";
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 { isDark, themePalette } = useTheme();
const geometryPreviewOpen = ref(false);
@@ -33,7 +34,21 @@ export function useDataGridCellDetail(options: { detail: Ref<DataGridCellDetail>
watch(detailsEditorContainer, async (element) => {
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;
await editor.create(element, options.editValue.value, options.detail.value.type);
if (detailsEditor === editor && editor.getValue() !== options.editValue.value) {