创建codemirror对象:
var myCodeMirror = CodeMirror(document.body, {
value: "function myScript(){return 100;}\n",
mode: "javascript"
});
命令使用方法示例:
//以下命令的作用是将滚动条置于文本区最下方
codemirror.execCommand("goDocEnd");
如果与angularjs的ui-codemirror模块集成,需要将光标或滚动条置于文本最下方时,则需要在ui-codemirror.js文件中增加命令:
function configNgModelLink(codemirror, ngModel, scope) {
if (!ngModel) { return; }
// CodeMirror expects a string, so make sure it gets one.
// This does not change the model.
ngModel.$formatters.push(function(value) {
if (angular.isUndefined(value) || value === null) {
return '';
} else if (angular.isObject(value) || angular.isArray(value)) {
throw new Error('ui-codemirror cannot use an object or an array as a model');
}
return value;
});
// Override the ngModelController $render method, which is what gets called when the model is updated.
// This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else.
ngModel.$render = function() {
//Code mirror expects a string so make sure it gets one
//Although the formatter have already done this, it can be possible that another formatter returns undefined (for example the required directive)
var safeViewValue = ngModel.$viewValue || '';
codemirror.setValue(safeViewValue);
codemirror.execCommand("goDocEnd"); //将光标和滚动条设置到文本区最下方
};
// Keep the ngModel in sync with changes from CodeMirror
codemirror.on('change', function(instance) {
var newValue = instance.getValue();
if (newValue !== ngModel.$viewValue) {
scope.$evalAsync(function() {
ngModel.$setViewValue(newValue);
codemirror.execCommand("goDocEnd");//将光标和滚动条设置到文本区最下方
});
}
});
}
其他命令如下,加粗字体命令的均可直接使用:
selectAll
Ctrl-A (PC), Cmd-A (Mac)
Select the whole content of the editor.
singleSelection
Esc
When multiple selections are present, this deselects all but the primary selection.
killLine
Ctrl-K (Mac)
Emacs-style line killing. Deletes the part of the line after the cursor. If that consists only of whitespace, the newline at the end of the line is also deleted.
deleteLine
Ctrl-D (PC), Cmd-D (Mac)
Deletes the whole line under the cursor, including newline at the end.
delLineLeft
Delete the part of the line before the cursor.
delWrappedLineLeft
Cmd-Backspace (Mac)
Delete the part of the line from the left side of the visual line the cursor is on to the cursor.
delWrappedLineRight
Cmd-Delete (Mac)
Delete the part of the line from the cursor to the right side of the visual line the cursor is on.
undo
Ctrl-Z (PC), Cmd-Z (Mac)
Undo the last change.
redo
Ctrl-Y (PC), Shift-Cmd-Z (Mac), Cmd-Y (Mac)
Redo the last undone change.
undoSelection
Ctrl-U (PC), Cmd-U (Mac)
Undo the last change to the selection, or if there are no selection-only changes at the top of the history, undo the last change.
redoSelection
Alt-U (PC), Shift-Cmd-U (Mac)
Redo the last change to the selection, or the last text change if no selection changes remain.
goDocStart
Ctrl-Home (PC), Cmd-Up (Mac), Cmd-Home (Mac)
Move the cursor to the start of the document.
goDocEnd
Ctrl-End (PC), Cmd-End (Mac), Cmd-Down (Mac)
Move the cursor to the end of the document.
goLineStart
Alt-Left (PC), Ctrl-A (Mac)
Move the cursor to the start of the line.
goLineStartSmart
Home
Move to the start of the text on the line, or if we are already there, to the actual start of the line (including whitespace).
goLineEnd
Alt-Right (PC), Ctrl-E (Mac)
Move the cursor to the end of the line.
goLineRight
Cmd-Right (Mac)
Move the cursor to the right side of the visual line it is on.
goLineLeft
Cmd-Left (Mac)
Move the cursor to the left side of the visual line it is on. If this line is wrapped, that may not be the start of the line.
goLineLeftSmart
Move the cursor to the left side of the visual line it is on. If that takes it to the start of the line, behave like goLineStartSmart
.
goLineUp
Up, Ctrl-P (Mac)
Move the cursor up one line.
goLineDown
Down, Ctrl-N (Mac)
Move down one line.
goPageUp
PageUp, Shift-Ctrl-V (Mac)
Move the cursor up one screen, and scroll up by the same distance.
goPageDown
PageDown, Ctrl-V (Mac)
Move the cursor down one screen, and scroll down by the same distance.
goCharLeft
Left, Ctrl-B (Mac)
Move the cursor one character left, going to the previous line when hitting the start of line.
goCharRight
Right, Ctrl-F (Mac)
Move the cursor one character right, going to the next line when hitting the end of line.
goColumnLeft
Move the cursor one character left, but don't cross line boundaries.
goColumnRight
Move the cursor one character right, don't cross line boundaries.
goWordLeft
Alt-B (Mac)
Move the cursor to the start of the previous word.
goWordRight
Alt-F (Mac)
Move the cursor to the end of the next word.
goGroupLeft
Ctrl-Left (PC), Alt-Left (Mac)
Move to the left of the group before the cursor. A group is a stretch of word characters, a stretch of punctuation characters, a newline, or a stretch of more than one whitespace character.
goGroupRight
Ctrl-Right (PC), Alt-Right (Mac)
Move to the right of the group after the cursor (see above).
delCharBefore
Shift-Backspace, Ctrl-H (Mac)
Delete the character before the cursor.
delCharAfter
Delete, Ctrl-D (Mac)
Delete the character after the cursor.
delWordBefore
Alt-Backspace (Mac)
Delete up to the start of the word before the cursor.
delWordAfter
Alt-D (Mac)
Delete up to the end of the word after the cursor.
delGroupBefore
Ctrl-Backspace (PC), Alt-Backspace (Mac)
Delete to the left of the group before the cursor.
delGroupAfter
Ctrl-Delete (PC), Ctrl-Alt-Backspace (Mac), Alt-Delete (Mac)
Delete to the start of the group after the cursor.
indentAuto
Shift-Tab
Auto-indent the current line or selection.
indentMore
Ctrl-] (PC), Cmd-] (Mac)
Indent the current line or selection by one indent unit.
indentLess
Ctrl-[ (PC), Cmd-[ (Mac)
Dedent the current line or selection by one indent unit.
insertTab
Insert a tab character at the cursor.
insertSoftTab
Insert the amount of spaces that match the width a tab at the cursor position would have.
defaultTab
Tab
If something is selected, indent it by one indent unit. If nothing is selected, insert a tab character.
transposeChars
Ctrl-T (Mac)
Swap the characters before and after the cursor.
newlineAndIndent
Enter
Insert a newline and auto-indent the new line.
toggleOverwrite
Insert
Flip the overwrite flag.
save
Ctrl-S (PC), Cmd-S (Mac)
Not defined by the core library, only referred to in key maps. Intended to provide an easy way for user code to define a save command.
find
Ctrl-F (PC), Cmd-F (Mac)
findNext
Ctrl-G (PC), Cmd-G (Mac)
findPrev
Shift-Ctrl-G (PC), Shift-Cmd-G (Mac)
replace
Shift-Ctrl-F (PC), Cmd-Alt-F (Mac)
replaceAll
Shift-Ctrl-R (PC), Shift-Cmd-Alt-F (Mac)
来源:oschina
链接:https://my.oschina.net/u/2391658/blog/789632