Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] keyBindings events missing #1711

Closed
2 tasks done
sytolk opened this issue Mar 5, 2025 · 2 comments
Closed
2 tasks done

[Feature] keyBindings events missing #1711

sytolk opened this issue Mar 5, 2025 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@sytolk
Copy link

sytolk commented Mar 5, 2025

Initial checklist

  • I agree to follow the code of conduct
  • I searched issues and discussions and couldn’t find anything (or linked relevant results below)

Problem

There is no way to handle keydown event like Ctrl+S on focus is in editor

Solution

can we add listener for keydown event?

crepe.on((listener) => {
      listener.onKeydown: (event, ctx) => {
        if (event.ctrlKey && event.key === 's') {
          event.preventDefault();
          // handle Ctrl+S keys 
        }
      }
})

Alternatives

Trying this without success - it works only if focus is not in editor it appears that keydown event is handled in milkdown editor?

 const onKeydown = useCallback(
      (e: KeyboardEvent) => {
        if (e.ctrlKey && e.key === 's') {
          saveDescription();
          return
        }
      }, [])

useEffect(() => {
    window.addEventListener('keydown', onKeydown);
    
    return () => {
      window.removeEventListener('keydown', onKeydown);
    };
}, [onKeydown]);
@sytolk sytolk added the enhancement New feature or request label Mar 5, 2025
@Saul-Mirone
Copy link
Member

@Saul-Mirone Saul-Mirone closed this as not planned Won't fix, can't repro, duplicate, stale Mar 6, 2025
@sytolk
Copy link
Author

sytolk commented Mar 6, 2025

$useKeymap works. Thanks!

This is my implementation for Crepe editor:

import { commandsCtx } from '@milkdown/kit/core';
import { $useKeymap, $command  } from "@milkdown/kit/utils";

       const saveCommand = $command('saveCommand', (ctx) => (options) => {
          return (state, dispatch) => {
            saveDescription();
            return true;
          }
        });

        const saveKeyMap = $useKeymap("saveKeymap", {
          saveDescription: {
            shortcuts: "Ctrl-s",
            command: (ctx) => {
              const commands = ctx.get(commandsCtx)
              return () => commands.call(saveCommand.key)
            },
          },
        });

        crepe.editor.use([saveCommand,saveKeyMap].flat());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants