Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions markdoc/editor/hotkeys/hotkeys.markdoc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect } from "react";
import { useCallback, useEffect, useRef } from "react";
import { useHotkeys } from "react-hotkeys-hook";

export interface Hotkey {
Expand Down Expand Up @@ -50,6 +50,9 @@ export const hotkeys: Record<string, Hotkey> = {
export const useMarkdownHotkeys = (
textareaRef: React.RefObject<HTMLTextAreaElement>,
) => {
const currentTextareaRef = useRef<HTMLTextAreaElement | null>(null);
const handlerRef = useRef<(e: KeyboardEvent) => void>();

// Create a single callback for all hotkeys
const handleHotkey = useCallback(
(hotkey: Hotkey) => (e: KeyboardEvent) => {
Expand Down Expand Up @@ -150,12 +153,8 @@ export const useMarkdownHotkeys = (
[textareaRef],
);

// Use useEffect to bind event listeners directly to the textarea
useEffect(() => {
const textarea = textareaRef.current;
if (!textarea) return;

const keydownHandler = (e: KeyboardEvent) => {
handlerRef.current = (e: KeyboardEvent) => {
// Check if it's a meta/ctrl key combination
if (!e.metaKey && !e.ctrlKey) return;

Expand All @@ -170,11 +169,31 @@ export const useMarkdownHotkeys = (
handleHotkey(matchingHotkey)(e);
}
};
}, [handleHotkey]);

textarea.addEventListener('keydown', keydownHandler);
useEffect(() => {
const textarea = textareaRef.current;

if (textarea === currentTextareaRef.current) return;

// Clean up previous event listener
if (currentTextareaRef.current && handlerRef.current) {
currentTextareaRef.current.removeEventListener('keydown', handlerRef.current);
}

// Set up new event listener if textarea exists
if (textarea && handlerRef.current) {
textarea.addEventListener('keydown', handlerRef.current);
currentTextareaRef.current = textarea;
} else {
currentTextareaRef.current = null;
}

return () => {
textarea.removeEventListener('keydown', keydownHandler);
if (currentTextareaRef.current && handlerRef.current) {
currentTextareaRef.current.removeEventListener('keydown', handlerRef.current);
currentTextareaRef.current = null;
}
};
}, [textareaRef, handleHotkey]);
});
};
Loading