add: jump, find, replace, copy, paste for editor

This commit is contained in:
hyb1996
2017-09-28 21:09:16 +08:00
parent 8f3d88c94c
commit e1f95c09f3
23 changed files with 721 additions and 123 deletions

View File

@@ -166,10 +166,13 @@
function findNext(cm, rev, callback) {cm.operation(function() {
var state = getSearchState(cm);
var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
if (!cursor.find(rev)) {
cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
if (!cursor.find(rev)) return;
var query = parseQuery(state.query);
var cursor = getSearchCursor(cm, query, rev ? state.posFrom : state.posTo);
var match = cursor.find(rev);
if (!match) {
cursor = getSearchCursor(cm, query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
match = cursor.find(rev);
if (!match) return;
}
cm.setSelection(cursor.from(), cursor.to());
cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
@@ -202,42 +205,12 @@
});
}
function replace(cm, all) {
if (cm.getOption("readOnly")) return;
var query = cm.getSelection() || getSearchState(cm).lastQuery;
var dialogText = '<span class="CodeMirror-search-label">' + (all ? 'Replace all:' : 'Replace:') + '</span>';
dialog(cm, dialogText + replaceQueryDialog, dialogText, query, function(query) {
if (!query) return;
query = parseQuery(query);
dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) {
text = parseString(text)
if (all) {
replaceAll(cm, query, text)
} else {
clearSearch(cm);
var cursor = getSearchCursor(cm, query, cm.getCursor("from"));
var advance = function() {
var start = cursor.from(), match;
if (!(match = cursor.findNext())) {
cursor = getSearchCursor(cm, query);
if (!(match = cursor.findNext()) ||
(start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
}
cm.setSelection(cursor.from(), cursor.to());
cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
confirmDialog(cm, doReplaceConfirm, "Replace?",
[function() {doReplace(match);}, advance,
function() {replaceAll(cm, query, text)}]);
};
var doReplace = function(match) {
cursor.replace(typeof query == "string" ? text :
text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
advance();
};
advance();
}
});
});
function replaceSelection(cm) {
var text = parseString(cm.state.search.text);
var query = parseQuery(cm.state.search.query);
var match = cm.getRange(cm.state.search.posFrom, cm.state.search.posTo).match(query);
cm.replaceSelection(typeof query == "string" ? text :
text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
}
CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
@@ -247,6 +220,13 @@
CodeMirror.commands.findNext = doSearch;
CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
CodeMirror.commands.clearSearch = clearSearch;
CodeMirror.commands.replace = replace;
CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
CodeMirror.commands.replace = replaceSelection;
CodeMirror.commands.replaceAll = function(cm) {
if (cm.getOption("readOnly")) return;
var query = cm.state.search.query;
var text = cm.state.search.text;
query = parseQuery(query);
text = parseString(text);
replaceAll(cm, query, text);
};
});

View File

@@ -6,7 +6,7 @@
height: 300px;
color: black;
direction: ltr;
font-size: 35px;
font-size: 13px;
}
/* PADDING */

View File

@@ -28,6 +28,7 @@
<script src="codemirror/addon/fold/indent-fold.js"></script>
<script src="codemirror/addon/fold/comment-fold.js"></script>
<!-- 代码补全 -->
<link rel="stylesheet" href="codemirror/addon/hint/show-hint.css">
<script src="codemirror/addon/hint/javascript-hint.js"></script>
@@ -36,6 +37,11 @@
<!-- 代码格式化 -->
<script type="text/javascript" src="beautify/beautify.min.js"></script>
<!-- 查找替换 -->
<script type="text/javascript" src="codemirror/addon/search/search.js"></script>
<script type="text/javascript" src="codemirror/addon/search/searchcursor.js"></script>
<head>
<title>CodeMirrorEditor</title>
</head>
@@ -59,5 +65,6 @@
editor.on("keyup", function(cm, e) {
editor.execCommand("autocomplete")
})
editor.setCursor({line: 0, ch: 0});
</script>
</html>