VSCode shortcuts in CodeMirror

Using VSCode shortcuts in CodeMirror (like HackMD, CodiMd etc ...)

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @icon         https://hackmd.io/favicon.png
// @name         VSCode shortcuts in CodeMirror
// @namespace    http://github.com/cliffxzx
// @version      0.1
// @description  Using VSCode shortcuts in CodeMirror (like HackMD, CodiMd etc ...)
// @author       Cliff Chen
// @match        *://*/*
// @grant        none
// ==/UserScript==

'use strict';
(function() {
  if (typeof window.CodeMirror === 'function') {
    var Pos = window.CodeMirror.Pos;

    const duplicateLines = (cm, down = false) => {
        cm.operation(function() {
            debugger;
            let selectionRange = cm.listSelections();
            var rangeCount = selectionRange.length;
            for (var i = 0; i < rangeCount; i++) {
                var range = cm.listSelections()[i];
                let text = '';
                let start = Math.min(range.head.line, range.anchor.line);
                let end = Math.max(range.head.line, range.anchor.line);

                for(let j = start; j <= end; ++j) {
                    text += cm.getLine(j) + "\n";
                }

                if (down) {
                    const rangeLineCount = end - start + 1;
                    cm.replaceRange(text, Pos(end + 1, 0));
                    selectionRange[i].head.line += rangeLineCount;
                    selectionRange[i].anchor.line += rangeLineCount;
                } else {
                    cm.replaceRange(text, Pos(start, 0));
                }

            }
            cm.setSelections(selectionRange);
            cm.scrollIntoView();
        });
    }

    window.editor.addKeyMap({
        "Alt-Up": "swapLineUp",
        "Alt-Down": "swapLineDown",
        "Shift-Cmd-Alt-Up": "addCursorToPrevLine",
        "Shift-Cmd-Alt-Down": "addCursorToNextLine",
        "Cmd-K Cmd-0": "foldAll",
        "Shift-Alt-Up": (cm) => duplicateLines(cm),
        "Shift-Alt-Down": (cm) => duplicateLines(cm, true)
    });
  }
})();