Skip to content

Conversation

@il3ven
Copy link

@il3ven il3ven commented May 25, 2023

This PR adds the functionality that if CodeEditor is rendered on screen then Cmd+A/Ctrl+A will select all the text inside CodeEditor.

For this solution I have added a event listener on window. Here a global event listener is the last option. I was not able to find a CodeMirror solution. Even if there is a CodeMirror solution we will need to enable cursor so the user knows when Ctrl+A will work.

/claim #162

Comment on lines 99 to 112
useEffect(() => {
const listener = (event: WindowEventMap["keydown"]) => {
if ((event.ctrlKey || event.metaKey) && event.key === "a") {
event.preventDefault();
view?.dispatch({ selection: { anchor: 0, head: state?.doc.length } });
}
};

window.addEventListener("keydown", listener);

return () => {
window.removeEventListener("keydown", listener);
};
}, [view, state]);
Copy link

@afzalsayed96 afzalsayed96 May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend tinykeys for shortcuts management for this kind of project.

import tinykeys from 'tinykeys'

function useHotKey(params: Parameters<typeof tinykeys>[1]) {
  useEffect(() => {
    const unsubscribe = tinykeys(window, params)
    return () => {
      unsubscribe()
    }
  }, [ params ])
}
useHotKey({
    "$mod+A": () => {
        event.preventDefault();
        view?.dispatch({ selection: { anchor: 0, head: state?.doc.length } });
    }
  })

Mac: $mod = Meta (⌘)
Windows/Linux: $mod = Control

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually already use the react-hotkeys-hook package in a bunch of different places, e.g.: https://github.com/triggerdotdev/jsonhero-web/blob/main/app/components/SearchBar.tsx#L18

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I change this to react-hotkeys-hook?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed e65b5ce adding react-hotkeys-hook.

@il3ven
Copy link
Author

il3ven commented Jul 19, 2023

Although this PR is ready but a similar PR has been merged already (#164) so closing this one.

@il3ven il3ven closed this Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants