generated from jackyzha0/quartz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: Custom keybinding with react-codemirror | ||
date: 2024-07-17 | ||
--- | ||
|
||
I'm currently using uiwjs/react-codemirror for a project, and needed to add a custom binding to cmd+enter | ||
|
||
The documentation didn't quite show what I needed, but with the following you can bind custom keys | ||
|
||
```js | ||
`import React from 'react'; | ||
import CodeMirror from '@uiw/react-codemirror'; | ||
import { keymap } from '@codemirror/view'; | ||
import { defaultKeymap } from '@codemirror/commands'; | ||
const MyCodeEditor = () => { | ||
const handleCmdEnter = React.useCallback((view) => { | ||
console.log('Cmd+Enter pressed!'); | ||
return true; | ||
}, []); | ||
const customKeymap = keymap.of([ | ||
{ | ||
key: 'Mod-Enter', | ||
run: handleCmdEnter, | ||
}, | ||
]); | ||
return ( | ||
<CodeMirror | ||
value="code and stuff" | ||
extensions={[customKeymap]} | ||
/> | ||
); | ||
}; | ||
export default MyCodeEditor;` | ||
``` | ||
|
||
Note that you may need to ensure your keymap extension is listed _before_ other extensions, otherwise it may be handled too soon.--- |