Skip to content

Commit

Permalink
Fix/invalid shortcut events (#488)
Browse files Browse the repository at this point in the history
Fix #486 .
  • Loading branch information
igoogolx committed Apr 8, 2024
1 parent 8c8a608 commit 7800f0a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
24 changes: 20 additions & 4 deletions app/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ export interface ShortcutEvent {
preventDefault?: () => void;
stopPropagation?: () => void;
isInput?: boolean;
target?: {
blur?: () => void;
};
}

export const formatShortcut = (event: ShortcutEvent): string[] => {
export const formatShortcut = (
event: Pick<
ShortcutEvent,
"ctrlKey" | "metaKey" | "altKey" | "shiftKey" | "key" | "code"
>
): string[] => {
let shortcutKeys: string[] = [];

if (event.ctrlKey) {
Expand Down Expand Up @@ -56,6 +64,9 @@ export const formatKeycode = (code: string): string => {
};

export const convertKeyboardEvent = (e: KeyboardEvent): ShortcutEvent => {
const isInput =
e.target instanceof HTMLInputElement ||
e.target instanceof HTMLTextAreaElement;
return {
ctrlKey: e.ctrlKey,
metaKey: e.metaKey,
Expand All @@ -69,8 +80,13 @@ export const convertKeyboardEvent = (e: KeyboardEvent): ShortcutEvent => {
stopPropagation: () => {
e.stopPropagation();
},
isInput:
e.target instanceof HTMLInputElement ||
e.target instanceof HTMLTextAreaElement,
isInput,
target: {
blur: () => {
if (isInput && e.target) {
(e.target as HTMLInputElement | HTMLTextAreaElement).blur();
}
},
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { disposable } from "@/base/dispose";
import { debounce } from "@/base/misc";
import { ICommand } from "@/renderer/services/command-service";
import { PaperFilterOptions } from "@/renderer/services/paper-service";
import { ShortcutEvent } from "@/common/utils.ts";
// ================================
// State
Expand Down Expand Up @@ -88,7 +89,7 @@ const isFocused = ref(false);
const isMouseOver = ref(false);
const isSelectingCommand = ref(false);
const onRunCommand = (e: Event) => {
const onRunCommand = () => {
if (isCommandPanelShown.value && selectedCommandIndex.value >= 0) {
const commandComponents = commandText.value.split(" ").filter((x) => x);
const commandStr = commandComponents[0].trim();
Expand Down Expand Up @@ -182,8 +183,8 @@ const onFocus = async (payload: Event) => {
enterDisposeHandler = shortcutService.register(
"Enter",
(e: Event) => {
onRunCommand(e);
() => {
onRunCommand();
},
true,
true,
Expand All @@ -192,9 +193,8 @@ const onFocus = async (payload: Event) => {
escapeDisposeHandler = shortcutService.register(
"Escape",
(e: Event) => {
//@ts-ignore
e.target?.blur();
(e: ShortcutEvent) => {
e.target?.blur?.();
uiState.commandBarText = "";
uiState.commandBarSearchMode = "general";
uiState.querySentenceCommandbar = "";
Expand All @@ -206,7 +206,7 @@ const onFocus = async (payload: Event) => {
backDisposeHandler = shortcutService.register(
"Backspace",
(e: Event) => {
() => {
if (commandText.value === "") {
uiState.commandBarText = "";
uiState.commandBarSearchMode = "general";
Expand Down

0 comments on commit 7800f0a

Please sign in to comment.