fix(memo-editor): scope Cmd+Enter save to the active editor (#5745)
Co-authored-by: memoclaw <265580040+memoclaw@users.noreply.github.com>
This commit is contained in:
parent
e0334cf0a8
commit
05810e7882
1 changed files with 18 additions and 5 deletions
|
|
@ -5,16 +5,29 @@ interface UseKeyboardOptions {
|
||||||
onSave: () => void;
|
onSave: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useKeyboard = (_editorRef: React.RefObject<EditorRefActions | null>, options: UseKeyboardOptions) => {
|
export const useKeyboard = (editorRef: React.RefObject<EditorRefActions | null>, options: UseKeyboardOptions) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleKeyDown = (event: KeyboardEvent) => {
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
|
if (!(event.metaKey || event.ctrlKey) || event.key !== "Enter") {
|
||||||
event.preventDefault();
|
return;
|
||||||
options.onSave();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const editor = editorRef.current?.getEditor();
|
||||||
|
if (!editor) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const activeElement = document.activeElement;
|
||||||
|
const target = event.target;
|
||||||
|
if (activeElement !== editor && target !== editor) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
options.onSave();
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener("keydown", handleKeyDown);
|
window.addEventListener("keydown", handleKeyDown);
|
||||||
return () => window.removeEventListener("keydown", handleKeyDown);
|
return () => window.removeEventListener("keydown", handleKeyDown);
|
||||||
}, [options]);
|
}, [editorRef, options]);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue