-
Notifications
You must be signed in to change notification settings - Fork 613
fix: Ctrl+A should select all text in CodeEditor #163
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
Conversation
app/components/CodeEditor.tsx
Outdated
| 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]); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please 👍
There was a problem hiding this comment.
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.
for <CodeEditor />
|
Although this PR is ready but a similar PR has been merged already (#164) so closing this one. |
This PR adds the functionality that if
CodeEditoris rendered on screen thenCmd+A/Ctrl+Awill select all the text insideCodeEditor.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 whenCtrl+Awill work./claim #162