diff --git a/package.json b/package.json index c9d3b45..b822f2c 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "@codemirror/language": "^6.1.0", "@codemirror/search": "^6.2.0", "@codemirror/state": "^6.0.1", - "@codemirror/view": "^6.0.3" + "@codemirror/view": "^6.3.0" }, "devDependencies": { "codemirror": "6.0.0", diff --git a/src/index.ts b/src/index.ts index f404262..6e5490e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ import { EditorView, showPanel, Panel, + keymap, } from "@codemirror/view"; import { setSearchQuery } from "@codemirror/search"; @@ -182,54 +183,61 @@ const vimPlugin = ViewPlugin.fromClass( decorations = Decoration.none; }, { - eventHandlers: { - keydown: function (e: KeyboardEvent, view: EditorView) { - const key = CodeMirror.vimKey(e); - const cm = this.cm; - if (!key) return; + decorations: (v) => v.decorations, - // clear search highlight - let vim = cm.state.vim; - if ( - key == "" && - !vim.insertMode && - !vim.visualMode && - this.query /* && !cm.inMultiSelectMode*/ - ) { - const searchState = vim.searchState_ - if (searchState) { - cm.removeOverlay(searchState.getOverlay()) - searchState.setOverlay(null); - } - } + provide: plugin => { + return keymap.of([ + { + any: function(view, e) { + const pluginInstance = view.plugin(plugin) + if (!pluginInstance) return false; - cm.state.vim.status = (cm.state.vim.status || "") + key; - let result = Vim.multiSelectHandleKey(cm, key, "user"); + const key = CodeMirror.vimKey(e); + const cm = pluginInstance.cm; + if (!key) return false; - // insert mode - if (!result && cm.state.vim.insertMode && cm.state.overwrite) { - if (e.key && e.key.length == 1 && !/\n/.test(e.key)) { - result = true; - cm.overWriteSelection(e.key); - } else if (e.key == "Backspace") { - result = true; - CodeMirror.commands.cursorCharLeft(cm); - } - } - if (result) { - CodeMirror.signal(this.cm, 'vim-keypress', key); - e.preventDefault(); - e.stopPropagation(); - this.blockCursor.scheduleRedraw(); - } + // clear search highlight + let vim = cm.state.vim; + if ( + key == "" && + !vim.insertMode && + !vim.visualMode && + pluginInstance.query /* && !cm.inMultiSelectMode*/ + ) { + const searchState = vim.searchState_ + if (searchState) { + cm.removeOverlay(searchState.getOverlay()) + searchState.setOverlay(null); + } + } - this.updateStatus(); + cm.state.vim.status = (cm.state.vim.status || "") + key; + let result = Vim.multiSelectHandleKey(cm, key, "user"); - return !!result; - }, - }, + // insert mode + if (!result && cm.state.vim.insertMode && cm.state.overwrite) { + if (e.key && e.key.length == 1 && !/\n/.test(e.key)) { + result = true; + cm.overWriteSelection(e.key); + } else if (e.key == "Backspace") { + result = true; + CodeMirror.commands.cursorCharLeft(cm); + } + } + if (result) { + CodeMirror.signal(cm, 'vim-keypress', key); + e.preventDefault(); + e.stopPropagation(); + pluginInstance.blockCursor.scheduleRedraw(); + } - decorations: (v) => v.decorations, + pluginInstance.updateStatus(); + + return !!result; + } + } + ]) + } } ); diff --git a/yarn.lock b/yarn.lock index 02d324a..16b3c0f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1055,9 +1055,9 @@ safe-buffer@~5.1.0, safe-buffer@~5.1.1: integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g== selenium-webdriver@^4.0.0-beta.3: - version "4.4.0" - resolved "https://registry.yarnpkg.com/selenium-webdriver/-/selenium-webdriver-4.4.0.tgz#3f280504f6c0ac64a24b176304213b5a49ec2553" - integrity sha512-Du+/xfpvNi9zHAeYgXhOWN9yH0hph+cuX+hHDBr7d+SbtQVcfNJwBzLsbdHrB1Wh7MHXFuIkSG88A9TRRQUx3g== + version "4.5.0" + resolved "https://registry.yarnpkg.com/selenium-webdriver/-/selenium-webdriver-4.5.0.tgz#7e20d0fc038177970dad81159950c12f7411ac0d" + integrity sha512-9mSFii+lRwcnT2KUAB1kqvx6+mMiiQHH60Y0VUtr3kxxi3oZ3CV3B8e2nuJ7T4SPb+Q6VA0swswe7rYpez07Bg== dependencies: jszip "^3.10.0" tmp "^0.2.1"