diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f221cca..bcd847c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,9 +17,9 @@ jobs: - uses: actions/setup-node@v2 with: - node-version: 18 + node-version: '16.4' - - run: yarn install - - run: cd ./dev/cm5 && yarn install - - run: yarn run build - - run: yarn run testAll + - run: npm install + - run: cd ./dev/cm5 && npm install + - run: npm run build + - run: npm run testAll diff --git a/dev/cm5/index.js b/dev/cm5/index.js index 2d1dd39..9b114ee 100644 --- a/dev/cm5/index.js +++ b/dev/cm5/index.js @@ -1,128 +1,6 @@ import { initVim as initVimInternal } from "../../src/vim.js"; -export function initVim(CodeMirror) { - var Vim = CodeMirror.Vim = initVimInternal(CodeMirror); - var Pos = CodeMirror.Pos; - - function transformCursor(cm, range) { - var vim = cm.state.vim; - if (!vim || vim.insertMode) return range.head; - var head = vim.sel.head; - if (!head) return range.head; - - if (vim.visualBlock) { - if (range.head.line != head.line) { - return; - } - } - if (range.from() == range.anchor && !range.empty()) { - if (range.head.line == head.line && range.head.ch != head.ch) - return new Pos(range.head.line, range.head.ch - 1); - } - - return range.head; - } - - CodeMirror.keyMap['vim-insert'] = { - // TODO: override navigation keys so that Esc will cancel automatic - // indentation from o, O, i_ - fallthrough: ['default'], - attach: attachVimMap, - detach: detachVimMap, - call: cmKey - }; - - CodeMirror.keyMap['vim-replace'] = { - 'Backspace': 'goCharLeft', - fallthrough: ['vim-insert'], - attach: attachVimMap, - detach: detachVimMap - }; - - - CodeMirror.keyMap.vim = { - attach: attachVimMap, - detach: detachVimMap, - call: cmKey - }; - - // Deprecated, simply setting the keymap works again. - CodeMirror.defineOption('vimMode', false, function(cm, val, prev) { - if (val && cm.getOption("keyMap") != "vim") - cm.setOption("keyMap", "vim"); - else if (!val && prev != CodeMirror.Init && /^vim/.test(cm.getOption("keyMap"))) - cm.setOption("keyMap", "default"); - }); - - function cmKey(key, cm) { - if (!cm) { return undefined; } - if (this[key]) { return this[key]; } - var vimKey = cmKeyToVimKey(key); - if (!vimKey) { - return false; - } - var cmd = Vim.findKey(cm, vimKey); - if (typeof cmd == 'function') { - CodeMirror.signal(cm, 'vim-keypress', vimKey); - } - return cmd; - } - - var modifiers = {Shift:'S',Ctrl:'C',Alt:'A',Cmd:'D',Mod:'A',CapsLock:''}; - var specialKeys = {Enter:'CR',Backspace:'BS',Delete:'Del',Insert:'Ins'}; - - function cmKeyToVimKey(key) { - if (key.charAt(0) == '\'') { - // Keypress character binding of format "'a'" - return key.charAt(1); - } - var pieces = key.split(/-(?!$)/); - var lastPiece = pieces[pieces.length - 1]; - if (pieces.length == 1 && pieces[0].length == 1) { - // No-modifier bindings use literal character bindings above. Skip. - return false; - } else if (pieces.length == 2 && pieces[0] == 'Shift' && lastPiece.length == 1) { - // Ignore Shift+char bindings as they should be handled by literal character. - return false; - } - var hasCharacter = false; - for (var i = 0; i < pieces.length; i++) { - var piece = pieces[i]; - if (piece in modifiers) { pieces[i] = modifiers[piece]; } - else { hasCharacter = true; } - if (piece in specialKeys) { pieces[i] = specialKeys[piece]; } - } - if (!hasCharacter) { - // Vim does not support modifier only keys. - return false; - } - // TODO: Current bindings expect the character to be lower case, but - // it looks like vim key notation uses upper case. - if (/^[A-Z]$/.test(lastPiece)) - pieces[pieces.length - 1] = lastPiece.toLowerCase(); - - return '<' + pieces.join('-') + '>'; - } - - function detachVimMap(cm, next) { - if (this == CodeMirror.keyMap.vim) { - cm.options.$customCursor = null; - CodeMirror.rmClass(cm.getWrapperElement(), "cm-fat-cursor"); - } - - if (!next || next.attach != attachVimMap) - Vim.leaveVimMode(cm); - } - function attachVimMap(cm, prev) { - if (this == CodeMirror.keyMap.vim) { - if (cm.curOp) cm.curOp.selectionChanged = true; - cm.options.$customCursor = transformCursor; - CodeMirror.addClass(cm.getWrapperElement(), "cm-fat-cursor"); - } - - if (!prev || prev.attach != attachVimMap) - Vim.enterVimMode(cm); - } - - return CodeMirror.Vim; +export function initVim(CodeMirror5) { + CodeMirror5.Vim = initVimInternal(CodeMirror5); + return CodeMirror5.Vim; } diff --git a/dev/cm5/package.json b/dev/cm5/package.json index e4ef59a..e5acbf5 100644 --- a/dev/cm5/package.json +++ b/dev/cm5/package.json @@ -9,8 +9,8 @@ "scripts": { "test": "cm-runtests .", "build": "node bin/build.js", - "buildAndTest": "yarn run build && yarn test", - "publish": "yarn run build && npm publish" + "buildAndTest": "npm run build && npm test", + "publish": "npm run build && npm publish" }, "keywords": [ "editor", diff --git a/dev/index.html b/dev/index.html index 9dd5e00..713ba70 100644 --- a/dev/index.html +++ b/dev/index.html @@ -18,7 +18,8 @@

- tests |  +
+ tests
cm5 demo diff --git a/dev/index.ts b/dev/index.ts index 419d660..991f128 100644 --- a/dev/index.ts +++ b/dev/index.ts @@ -1,6 +1,6 @@ import { basicSetup, EditorView } from 'codemirror' import { highlightActiveLine, keymap, Decoration, DecorationSet, - ViewPlugin, ViewUpdate, WidgetType, drawSelection } from '@codemirror/view'; + ViewPlugin, ViewUpdate, WidgetType } from '@codemirror/view'; import { javascript } from '@codemirror/lang-javascript'; import { xml } from '@codemirror/lang-xml'; import { Vim, vim } from "../src/index" @@ -97,7 +97,6 @@ const testWidgetPlugin = ViewPlugin.fromClass(class { } }) -if (!localStorage.status) localStorage.status = "true"; var options = { wrap: addOption("wrap"), html: addOption("html"), @@ -111,7 +110,6 @@ var options = { split: addOption("split", "", function() { }), - readOnly: addOption("readOnly") }; @@ -119,10 +117,8 @@ var options = { Vim.defineOption('wrap', false, 'boolean', null, function(val, cm) { if (val == undefined) return options.wrap; var checkbox = document.getElementById("wrap"); - if (checkbox) { - checkbox.checked = val; - checkbox.onclick(); - } + checkbox.checked = val; + checkbox.onclick(); }); var focusEditorButton = document.createElement("button"); @@ -188,15 +184,12 @@ function updateView() { if (!options.split && view2) deleteSplit(); if (!view) view = createView(); - addLogListeners(); selectTab(options.html ? "html": "js") var extensions = [ - EditorState.readOnly.of(options.readOnly), enableVim && vim({status: options.status}), options.wrap && EditorView.lineWrapping, - drawSelection({cursorBlinkRate: window.blinkRate}) ].filter((x)=>!!x) as Extension[]; view.dispatch({ @@ -207,7 +200,6 @@ function updateView() { function selectTab(tab: string) { if (view) view.setState(tabs[tab]) if (view2) view2.setState(tabs[tab]) - addLogListeners(); } Vim.defineEx("tabnext", "tabn", () => { @@ -277,27 +269,6 @@ function createView() { return view; } -function addLogListeners() { - var cm = (view as any)?.cm; - if (cm) { - cm.off("inputEvent", logHandler); - cm.on("inputEvent", logHandler); - } -} -var i = 0; -function logHandler(e: any) { - var message = [i++, e.type.padEnd(10), e.key, e.code].join(" "); - var entry = logContainer.childNodes.length < 1000 - ? document.createElement("div") - : logContainer.lastChild! as HTMLElement; - entry.textContent = message - logContainer.insertBefore(entry, logContainer.firstChild); -} -var logContainer = document.createElement("pre"); -document.body.appendChild(logContainer); -logContainer.className = ".logContainer" - - updateView() diff --git a/dev/web-demo.html b/dev/web-demo.html index 2ec99fd..0b749f6 100644 --- a/dev/web-demo.html +++ b/dev/web-demo.html @@ -4,7 +4,7 @@