diff options
author | Wu Cheng-Han | 2016-09-18 16:35:24 +0800 |
---|---|---|
committer | Wu Cheng-Han | 2016-09-18 16:35:24 +0800 |
commit | 591134007ca70a542239badc61073f0be515abf4 (patch) | |
tree | cf4beb44e971b8465b249be1d7fae7d0ce4c430a /public/js | |
parent | f1c9874ed077e9880a5362d93bd2c6c054c1ddc1 (diff) |
Update to support shortcuts which can add or delete symbol surround text
Diffstat (limited to 'public/js')
-rw-r--r-- | public/js/index.js | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/public/js/index.js b/public/js/index.js index e192db59..b05c8d2a 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -50,9 +50,93 @@ var defaultExtraKeys = { "Ctrl-C": function (cm) { if (!mac && cm.getOption('keyMap').substr(0, 3) === 'vim') document.execCommand("copy"); else return CodeMirror.Pass; + }, + "Ctrl-*": function (cm) { + wrapTextWith(cm, '*'); + }, + "Shift-Ctrl-8": function (cm) { + wrapTextWith(cm, '*'); + }, + "Ctrl-_": function (cm) { + wrapTextWith(cm, '_'); + }, + "Shift-Ctrl--": function (cm) { + wrapTextWith(cm, '_'); + }, + "Ctrl-~": function (cm) { + wrapTextWith(cm, '~'); + }, + "Shift-Ctrl-`": function (cm) { + wrapTextWith(cm, '~'); + }, + "Ctrl-^": function (cm) { + wrapTextWith(cm, '^'); + }, + "Shift-Ctrl-6": function (cm) { + wrapTextWith(cm, '^'); + }, + "Ctrl-+": function (cm) { + wrapTextWith(cm, '+'); + }, + "Shift-Ctrl-=": function (cm) { + wrapTextWith(cm, '+'); + }, + "Ctrl-=": function (cm) { + wrapTextWith(cm, '='); + }, + "Shift-Ctrl-Backspace": function (cm) { + wrapTextWith(cm, 'Backspace'); } }; +var wrapSymbols = ['*', '_', '~', '^', '+', '=']; + +function wrapTextWith(cm, symbol) { + if (!cm.getSelection()) { + return CodeMirror.Pass; + } else { + var ranges = cm.listSelections(); + for (var i = 0; i < ranges.length; i++) { + var range = ranges[i]; + if (!range.empty()) { + var from = range.from(), to = range.to(); + if (symbol !== 'Backspace') { + cm.replaceRange(symbol, to, to, '+input'); + cm.replaceRange(symbol, from, from, '+input'); + // workaround selection range not correct after add symbol + var _ranges = cm.listSelections(); + var anchorIndex = editor.indexFromPos(_ranges[i].anchor); + var headIndex = editor.indexFromPos(_ranges[i].head); + if (anchorIndex > headIndex) { + _ranges[i].anchor.ch--; + } else { + _ranges[i].head.ch--; + } + cm.setSelections(_ranges); + } else { + var preEndPos = { + line: to.line, + ch: to.ch + 1 + }; + var preText = cm.getRange(to, preEndPos); + var preIndex = wrapSymbols.indexOf(preText); + var postEndPos = { + line: from.line, + ch: from.ch - 1 + }; + var postText = cm.getRange(postEndPos, from); + var postIndex = wrapSymbols.indexOf(postText); + // check if surround symbol are list in array and matched + if (preIndex > -1 && postIndex > -1 && preIndex === postIndex) { + cm.replaceRange("", to, preEndPos, '+delete'); + cm.replaceRange("", postEndPos, from, '+delete'); + } + } + } + } + } +} + var idleTime = 300000; //5 mins var updateViewDebounce = 200; var cursorMenuThrottle = 50; |