added ability to exit using ESC

This commit is contained in:
fuwaa 2022-08-23 09:49:14 +08:00
parent ed9c586156
commit f9ca7dc5a1
No known key found for this signature in database
GPG key ID: 7CE0BF0D3175E2DC
5 changed files with 28 additions and 19 deletions

3
lib/commands/core.js Normal file
View file

@ -0,0 +1,3 @@
"use babel";
export default [];

View file

@ -12,18 +12,6 @@ export default Option = (props) => {
// hide modal after executing // hide modal after executing
} }
function checkfocus() {
if (document.activeElement.id === "cpInput") {
return true;
} else {
return false;
}
}
const isHighlighted = {
backgroundColor: idx === 0 ? "var(--highlight-background)" : "",
};
return ( return (
<a className="option flex-row" onClick={execute} href="#"> <a className="option flex-row" onClick={execute} href="#">
<p className="nomargin"> <p className="nomargin">

View file

@ -5,12 +5,10 @@ import Option from "./command.js";
import Commands from "../commands/application.js"; import Commands from "../commands/application.js";
import { logger, useModal } from "inkdrop"; import { logger, useModal } from "inkdrop";
import useArrowKeyNavigation from "../navigation/hook.js"; import useArrowKeyNavigation from "../navigation/hook.js";
import { ipcRenderer } from "electron";
const CommandPalette = (props) => { const CommandPalette = (props) => {
const modal = useModal(); const modal = useModal();
const { Dialog } = inkdrop.components.classes; const { Dialog } = inkdrop.components.classes;
const parentRef = useArrowKeyNavigation({ selectors: "a,input" });
const inputRef = useRef(null); const inputRef = useRef(null);
const [searchQuery, setSearchQuery] = React.useState(""); const [searchQuery, setSearchQuery] = React.useState("");
@ -23,6 +21,7 @@ const CommandPalette = (props) => {
const sub = inkdrop.commands.add(document.body, { const sub = inkdrop.commands.add(document.body, {
"commandpalette:toggle": toggle, "commandpalette:toggle": toggle,
}); });
console.log("useeffect triggered");
return () => sub.dispose(); return () => sub.dispose();
}, [toggle]); }, [toggle]);
@ -58,10 +57,15 @@ const CommandPalette = (props) => {
{...modal.state} {...modal.state}
onBackdropClick={modal.close} onBackdropClick={modal.close}
className="commandpalette flex-col" className="commandpalette flex-col"
ref={parentRef}
> >
<Dialog.Content className="commandpalette"> <Dialog.Content className="commandpalette">
<div className="commandpalettewrapper flex-col" ref={parentRef}> <div
className="commandpalettewrapper flex-col"
ref={useArrowKeyNavigation({
selectors: "a,input",
modal: modal.close,
})}
>
<div className="flex-col contents"> <div className="flex-col contents">
<div className="ui small input"> <div className="ui small input">
<input <input

View file

@ -7,6 +7,7 @@ function handleEnter({ event, currentIndex, availableElements }) {
} else { } else {
clickElement = availableElements[currentIndex]; clickElement = availableElements[currentIndex];
} }
if (clickElement === undefined) return;
clickElement.click(); clickElement.click();
event.preventDefault(); event.preventDefault();
} }
@ -40,12 +41,13 @@ function handleArrowKey({ event, currentIndex, availableElements }) {
export default function handleEvents({ export default function handleEvents({
event, event,
parentNode, parentNode,
modal,
selectors = "a,button,input", selectors = "a,button,input",
}) { }) {
if (!parentNode) return; if (!parentNode) return;
const key = event.key; const key = event.key;
if (!["ArrowUp", "ArrowDown", "Enter"].includes(key)) { if (!["ArrowUp", "ArrowDown", "Enter", "Escape"].includes(key)) {
return; return;
} }
@ -68,5 +70,12 @@ export default function handleEvents({
if (key === "Enter") { if (key === "Enter") {
handleEnter({ event, currentIndex, availableElements }); handleEnter({ event, currentIndex, availableElements });
} }
if (key === "Escape") {
modal();
event.preventDefault();
return;
}
handleArrowKey({ event, currentIndex, availableElements }); handleArrowKey({ event, currentIndex, availableElements });
} }

View file

@ -9,12 +9,17 @@ import { useRef, useEffect } from "react";
* @returns a useRef, which can be applied to a component * @returns a useRef, which can be applied to a component
*/ */
export default function useArrowKeyNavigation(props) { export default function useArrowKeyNavigation(props) {
const { selectors } = props || {}; const { selectors, modal } = props || {};
const parentNode = useRef(); const parentNode = useRef();
useEffect(() => { useEffect(() => {
const eventHandler = (event) => { const eventHandler = (event) => {
handleEvents({ event, parentNode: parentNode.current, selectors }); handleEvents({
event,
parentNode: parentNode.current,
selectors,
modal: modal,
});
}; };
document.addEventListener("keydown", eventHandler); document.addEventListener("keydown", eventHandler);
return () => document.removeEventListener("keydown", eventHandler); return () => document.removeEventListener("keydown", eventHandler);