缩进快捷键

This commit is contained in:
Liang Ding 2014-09-03 00:05:05 +08:00
parent 8d6d306959
commit 77afacc3b1
2 changed files with 52 additions and 45 deletions

View File

@ -9,11 +9,13 @@
<h1>键盘快捷键</h1> <h1>键盘快捷键</h1>
<ul> <ul>
<li>F11: 编辑器全屏</li>
<li>Ctrl+G: 跳转到行</li> <li>Ctrl+G: 跳转到行</li>
<li>Ctrl+\\: 自动补全</li> <li>Ctrl+\\: 自动补全</li>
<li>Ctrl+D: 删除当前行</li> <li>Ctrl+E: 删除当前行</li>
<li>F11: 编辑器全屏</li>
<li>Shift+Tab: 自动缩进</li> <li>Shift+Tab: 自动缩进</li>
<li>Ctrl+]: 缩进</li>
<li>Ctrl+[: 反缩进</li>
</ul> </ul>
</body> </body>
</html> </html>

View File

@ -66,10 +66,10 @@ var editors = {
var start = cur.ch, end = start; var start = cur.ch, end = start;
while (end < curLine.length && word.test(curLine.charAt(end))) { while (end < curLine.length && word.test(curLine.charAt(end))) {
++end; ++end;
} }
while (start && word.test(curLine.charAt(start - 1))) { while (start && word.test(curLine.charAt(start - 1))) {
--start; --start;
} }
var request = { var request = {
code: editor.getValue(), code: editor.getValue(),
cursorLine: cur.line, cursorLine: cur.line,
@ -112,41 +112,44 @@ var editors = {
cm.showHint({hint: CodeMirror.hint.auto}); cm.showHint({hint: CodeMirror.hint.auto});
}; };
CodeMirror.commands.autocompleteRightPart = function(cm) { CodeMirror.commands.autocompleteRightPart = function(cm) {
setTimeout(function() { setTimeout(function() {
var cur = cm.getCursor(); var cur = cm.getCursor();
var curLine = cm.getLine(cur.line); var curLine = cm.getLine(cur.line);
var curChar = curLine.charAt(cur.ch - 1); var curChar = curLine.charAt(cur.ch - 1);
replacement = ''; replacement = '';
switch (curChar) { switch (curChar) {
case '(': case '(':
replacement = ')'; replacement = ')';
break; break;
case '[': case '[':
replacement = ']'; replacement = ']';
break; break;
case '{': case '{':
replacement = '}'; replacement = '}';
break; break;
default: // " or ' default: // " or '
replacement = curChar; replacement = curChar;
break; break;
} }
cm.replaceRange(replacement, CodeMirror.Pos(cur.line, cur.ch)); cm.replaceRange(replacement, CodeMirror.Pos(cur.line, cur.ch));
cm.setCursor(CodeMirror.Pos(cur.line, cur.ch)); cm.setCursor(CodeMirror.Pos(cur.line, cur.ch));
}, 50); }, 50);
return CodeMirror.Pass; return CodeMirror.Pass;
}; };
CodeMirror.commands.gotoLine = function(cm) { CodeMirror.commands.gotoLine = function(cm) {
var line = prompt("Go To Line: ", "0"); var line = prompt("Go To Line: ", "0");
cm.setCursor(CodeMirror.Pos(line - 1, 0)); cm.setCursor(CodeMirror.Pos(line - 1, 0));
}; };
CodeMirror.commands.doNothing = function(cm) {
};
}, },
newEditor: function(data) { newEditor: function(data) {
$(".ico-fullscreen").show(); $(".ico-fullscreen").show();
@ -167,7 +170,7 @@ var editors = {
var editor = CodeMirror.fromTextArea(document.getElementById("editor" + id), { var editor = CodeMirror.fromTextArea(document.getElementById("editor" + id), {
lineNumbers: true, lineNumbers: true,
styleActiveLine: true, styleActiveLine: true,
theme: 'lesser-dark', theme: 'lesser-dark',
indentUnit: 4, indentUnit: 4,
extraKeys: { extraKeys: {
@ -181,12 +184,14 @@ var editors = {
"F11": function(cm) { "F11": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen")); cm.setOption("fullScreen", !cm.getOption("fullScreen"));
}, },
"'('": "autocompleteRightPart", "'('": "autocompleteRightPart",
"'['": "autocompleteRightPart", "'['": "autocompleteRightPart",
"'{'": "autocompleteRightPart", "'{'": "autocompleteRightPart",
"'\"'": "autocompleteRightPart", "'\"'": "autocompleteRightPart",
"'''": "autocompleteRightPart", "'''": "autocompleteRightPart",
"Ctrl-G": "gotoLine" "Ctrl-G": "gotoLine",
"Ctrl-E": "deleteLine",
"Ctrl-D": "doNothing" // 取消默认的 deleteLine
} }
}); });
editor.setSize('100%', $(".edit-panel").height() - $(".edit-header").height()); editor.setSize('100%', $(".edit-panel").height() - $(".edit-header").height());