Skip to content

Commit a9367a4

Browse files
authored
feat: add vim mode via query param (sveltejs#813)
closes sveltejs#570
1 parent 0b7a0cb commit a9367a4

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

packages/editor/src/lib/Workspace.svelte.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,17 @@ function file_type(file: Item) {
5252
}
5353

5454
const tab_behaviour = new Compartment();
55+
const vim_mode = new Compartment();
5556

5657
const default_extensions = [
5758
basicSetup,
5859
EditorState.tabSize.of(2),
5960
tab_behaviour.of(keymap.of([{ key: 'Tab', run: acceptCompletion }])),
6061
indentUnit.of('\t'),
61-
theme
62+
theme,
63+
vim_mode.of([])
6264
];
6365

64-
// TODO add vim mode via a compartment (https://codemirror.net/examples/config/)
65-
// let installed_vim = false;
66-
// let should_install_vim = localStorage.getItem('vim') === 'true';
67-
68-
// const q = new URLSearchParams(location.search);
69-
// if (q.has('vim')) {
70-
// should_install_vim = q.get('vim') === 'true';
71-
// localStorage.setItem('vim', should_install_vim.toString());
72-
// }
73-
74-
// if (!installed_vim && should_install_vim) {
75-
// installed_vim = true;
76-
// const { vim } = await import('@replit/codemirror-vim');
77-
// extensions.push(vim());
78-
// }
79-
8066
export interface ExposedCompilerOptions {
8167
generate: 'client' | 'server';
8268
dev: boolean;
@@ -243,11 +229,27 @@ export class Workspace {
243229
this.modified = {};
244230
}
245231

246-
link(view: EditorView) {
232+
async link(view: EditorView) {
247233
if (this.#view) throw new Error('view is already linked');
248234
this.#view = view;
249235

250236
view.setState(this.#get_state(untrack(() => this.#current)));
237+
238+
let should_install_vim = localStorage.getItem('vim') === 'true';
239+
240+
const q = new URLSearchParams(location.search);
241+
if (q.has('vim')) {
242+
should_install_vim = q.get('vim') === 'true';
243+
localStorage.setItem('vim', should_install_vim.toString());
244+
}
245+
246+
if (should_install_vim) {
247+
const { vim } = await import('@replit/codemirror-vim');
248+
249+
this.#view?.dispatch({
250+
effects: vim_mode.reconfigure(vim())
251+
});
252+
}
251253
}
252254

253255
move(from: Item, to: Item) {

0 commit comments

Comments
 (0)