replace and replaceAll will return the number of items replaced

This commit is contained in:
okonomiyaki3000 2011-12-21 17:16:37 +09:00
commit f79c2d8ce2

View file

@ -1093,13 +1093,18 @@ var Editor = function(renderer, session) {
if (options) if (options)
this.$search.set(options); this.$search.set(options);
var range = this.$search.find(this.session); var range = this.$search.find(this.session),
replaced = 0;
if (!range) if (!range)
return; return replaced;
this.$tryReplace(range, replacement); if(this.$tryReplace(range, replacement)) {
replaced = 1;
}
if (range !== null) if (range !== null)
this.selection.setSelectionRange(range); this.selection.setSelectionRange(range);
return replaced;
}; };
this.replaceAll = function(replacement, options) { this.replaceAll = function(replacement, options) {
@ -1107,20 +1112,26 @@ var Editor = function(renderer, session) {
this.$search.set(options); this.$search.set(options);
} }
var ranges = this.$search.findAll(this.session); var ranges = this.$search.findAll(this.session),
replaced = 0;
if (!ranges.length) if (!ranges.length)
return; return replaced;
var selection = this.getSelectionRange(); var selection = this.getSelectionRange();
this.clearSelection(); this.clearSelection();
this.selection.moveCursorTo(0, 0); this.selection.moveCursorTo(0, 0);
this.$blockScrolling += 1; this.$blockScrolling += 1;
for (var i = ranges.length - 1; i >= 0; --i) for (var i = ranges.length - 1; i >= 0; --i) {
this.$tryReplace(ranges[i], replacement); if(this.$tryReplace(ranges[i], replacement)) {
replaced++;
}
}
this.selection.setSelectionRange(selection); this.selection.setSelectionRange(selection);
this.$blockScrolling -= 1; this.$blockScrolling -= 1;
return replaced;
}; };
this.$tryReplace = function(range, replacement) { this.$tryReplace = function(range, replacement) {