Skip to content

Commit f9c05b4

Browse files
authored
Apply Vim keybindings for Yorkie undo/redo (#569)
1 parent d26044c commit f9c05b4

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

frontend/src/components/editor/Editor.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function Editor(props: EditorProps) {
4040
const workspaceStore = useSelector(selectWorkspace);
4141
const { mutateAsync: createUploadUrl } = useCreateUploadUrlMutation();
4242
const { mutateAsync: uploadFile } = useUploadFileMutation();
43-
const { applyFormat, setKeymapConfig } = useFormatUtils();
43+
const { applyFormat, setKeymapConfig, setupVimKeybindings } = useFormatUtils();
4444
const { toolBarState, setToolBarState, updateFormatBar } = useToolBar();
4545

4646
const {
@@ -78,6 +78,11 @@ function Editor(props: EditorProps) {
7878
return `${import.meta.env.VITE_API_ADDR}/files/${uploadUrlData.fileKey}`;
7979
};
8080

81+
// Setup vim keybindings when vim mode is activated
82+
if (configStore.codeKey === CodeKeyType.VIM) {
83+
setupVimKeybindings();
84+
}
85+
8186
const state = EditorState.create({
8287
doc: editorStore.doc.getRoot().content?.toString() ?? "",
8388
extensions: [
@@ -122,6 +127,7 @@ function Editor(props: EditorProps) {
122127
applyFormat,
123128
updateFormatBar,
124129
setKeymapConfig,
130+
setupVimKeybindings,
125131
]);
126132

127133
return (

frontend/src/hooks/useFormatUtils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { indentWithTab } from "@codemirror/commands";
55
import { Dispatch, SetStateAction } from "react";
66
import { useSelector } from "react-redux";
77
import { selectEditor } from "../store/editorSlice";
8+
import { Vim } from "@replit/codemirror-vim";
89

910
export interface ToolBarState {
1011
show: boolean;
@@ -125,6 +126,25 @@ export const useFormatUtils = () => {
125126
return true;
126127
}, [doc]);
127128

129+
// Setup Vim keybindings to use Yorkie undo/redo
130+
const setupVimKeybindings = useCallback(() => {
131+
// Map 'u' key in vim mode to Yorkie undo
132+
Vim.defineAction("yorkieUndo", () => {
133+
handleYorkieUndo();
134+
});
135+
136+
// Map redo command in vim mode to Yorkie redo
137+
Vim.defineAction("yorkieRedo", () => {
138+
handleYorkieRedo();
139+
});
140+
141+
// Map 'u' key to Yorkie undo action
142+
Vim.mapCommand("u", "action", "yorkieUndo", {}, { context: "normal" });
143+
144+
// Map 'Ctrl-r' key to Yorkie redo action
145+
Vim.mapCommand("<C-r>", "action", "yorkieRedo", {}, { context: "normal" });
146+
}, [handleYorkieUndo, handleYorkieRedo]);
147+
128148
const setKeymapConfig = useCallback(
129149
() => [
130150
indentWithTab,
@@ -186,5 +206,6 @@ export const useFormatUtils = () => {
186206
checkAndAddFormat,
187207
handleYorkieUndo,
188208
handleYorkieRedo,
209+
setupVimKeybindings,
189210
};
190211
};

0 commit comments

Comments
 (0)