Skip to content

Commit

Permalink
feat: support delete all callback command
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Aug 16, 2024
1 parent 6414373 commit 4f70eb1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
27 changes: 19 additions & 8 deletions apps/client/utils/keyboardShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,31 @@ export function registerShortcut(key: string, command: Shortcut["command"]) {

export function cancelShortcut(key: string, command: Shortcut["command"]): void;
export function cancelShortcut(shortcut: Shortcut): void;
export function cancelShortcut(key: string): void;
export function cancelShortcut(keyOrShortcut: string | Shortcut, command?: Shortcut["command"]) {
function normalizeShortcut() {
let normalShortcut: Shortcut;
if (typeof keyOrShortcut === "string" && command) {
normalShortcut = createShortcut(keyOrShortcut, command);
} else {
normalShortcut = keyOrShortcut as Shortcut;
function normalizeShortcut(): Shortcut | Partial<Shortcut> {
if (typeof keyOrShortcut === "object") {
return keyOrShortcut;
}

return normalShortcut;
return command ? createShortcut(keyOrShortcut, command) : parseKey(keyOrShortcut);
}

let normalShortcut = normalizeShortcut();

if (typeof keyOrShortcut === "string" && !command) {
// 如果只传了 key,删除所有匹配的快捷键
for (let i = shortcuts.length - 1; i >= 0; i--) {
if (
shortcuts[i].key === normalShortcut!.key &&
shortcuts[i].ctrlKey === normalShortcut!.ctrlKey &&
shortcuts[i].metaKey === normalShortcut!.metaKey
) {
shortcuts.splice(i, 1);
}
}
return;
}

const index = shortcuts.findIndex(({ key, command, ctrlKey, metaKey }) => {
// 精准匹配对应快捷键对象
return (
Expand Down
52 changes: 52 additions & 0 deletions apps/client/utils/tests/keyboardShortcuts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,58 @@ describe("keyboardShortcuts", () => {

expect(command).not.toBeCalled();
});

it("should remove all commands when only key is provided", () => {
let commandA = vi.fn();
let commandB = vi.fn();
registerShortcut("enter", commandA);
registerShortcut("enter", commandB);

// 只传递 key,删除所有匹配的快捷键
cancelShortcut("enter");

// 触发
fireEvent.keyDown({
key: "enter",
});

expect(commandA).not.toBeCalled();
expect(commandB).not.toBeCalled();
});

it("should remove all ctrl when only key is provided", () => {
let commandA = vi.fn();
let commandB = vi.fn();
registerShortcut("ctrl+A", commandA);
registerShortcut("ctrl+a", commandB);

cancelShortcut("ctrl+a");

fireEvent.keyDown({
ctrlKey: true,
key: "a",
});

expect(commandA).not.toBeCalled();
expect(commandB).not.toBeCalled();
});

it("should remove all commands when only key is provided", () => {
let commandA = vi.fn();
let commandB = vi.fn();
registerShortcut("command+A", commandA);
registerShortcut("command+a", commandB);

cancelShortcut("command+a");

fireEvent.keyDown({
metaKey: true,
key: "a",
});

expect(commandA).not.toBeCalled();
expect(commandB).not.toBeCalled();
});
});

describe("utils function", () => {
Expand Down

0 comments on commit 4f70eb1

Please sign in to comment.