Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: move tab navigation keyboard shortcuts to `key-map.ts`

Abdullah Attaade60e4272f3
diff --git a/apps/web/src/common/key-map.ts b/apps/web/src/common/key-map.ts
index 40f46da26..fc7a0191f 100644
--- a/apps/web/src/common/key-map.ts
+++ b/apps/web/src/common/key-map.ts
@@ -18,11 +18,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import hotkeys from "hotkeys-js";
-import { GlobalKeyboard } from "../utils/keyboard";
import { useEditorStore } from "../stores/editor-store";
import { useStore as useSearchStore } from "../stores/search-store";
import { useEditorManager } from "../components/editor/manager";
+function isInEditor(e: KeyboardEvent) {
+ return (
+ e.target instanceof HTMLElement && !!e.target?.closest(".editor-container")
+ );
+}
+
const KEYMAP = [
// {
// keys: ["command+n", "ctrl+n", "command+alt+n", "ctrl+alt+n"],
@@ -54,15 +59,34 @@ const KEYMAP = [
// });
// },
// },
+ {
+ keys: [
+ "command+option+right",
+ "ctrl+alt+right",
+ "command+option+shift+right",
+ "ctrl+alt+shift+right"
+ ],
+ description: "Go to next tab",
+ global: false,
+ action: () => useEditorStore.getState().focusNextTab()
+ },
+ {
+ keys: [
+ "command+option+left",
+ "ctrl+alt+left",
+ "command+option+shift+left",
+ "ctrl+alt+shift+left"
+ ],
+ description: "Go to next tab",
+ global: false,
+ action: () => useEditorStore.getState().focusPreviousTab()
+ },
{
keys: ["command+f", "ctrl+f"],
description: "Search all notes",
global: false,
action: (e: KeyboardEvent) => {
- const isInEditor =
- e.target instanceof HTMLElement &&
- !!e.target?.closest(".editor-container");
- if (isInEditor) {
+ if (isInEditor(e)) {
const activeSession = useEditorStore.getState().getActiveSession();
if (activeSession?.type === "readonly") {
e.preventDefault();
@@ -149,14 +173,9 @@ export function registerKeyMap() {
};
KEYMAP.forEach((key) => {
- hotkeys(
- key.keys.join(","),
- {
- element: key.global
- ? (GlobalKeyboard as unknown as HTMLElement)
- : document.body
- },
- key.action
- );
+ hotkeys(key.keys.join(","), (e) => {
+ e.preventDefault();
+ key.action?.(e);
+ });
});
}
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index 17a3ab311..e589d91df 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -125,31 +125,6 @@ export default function TabsView() {
const isTOCVisible = useEditorStore((store) => store.isTOCVisible);
const [dropRef, overlayRef] = useDragOverlay();
- useEffect(() => {
- const onKeyDown = (event: KeyboardEvent) => {
- if (
- (event.ctrlKey || event.metaKey) &&
- event.altKey &&
- event.key === "ArrowRight"
- ) {
- event.preventDefault();
- useEditorStore.getState().focusNextTab();
- }
- if (
- (event.ctrlKey || event.metaKey) &&
- event.altKey &&
- event.key === "ArrowLeft"
- ) {
- event.preventDefault();
- useEditorStore.getState().focusPreviousTab();
- }
- };
- document.body.addEventListener("keydown", onKeyDown);
- return () => {
- document.body.removeEventListener("keydown", onKeyDown);
- };
- }, []);
-
return (
<>
<Flex