-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchreplace.js
More file actions
120 lines (102 loc) · 4.47 KB
/
searchreplace.js
File metadata and controls
120 lines (102 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
if (!RedactorPlugins) var RedactorPlugins = {};
(function ($) {
RedactorPlugins.searchreplace = function () {
var selectedText,
selectedHtml,
current,
range;
return {
init: function () {
var button = this.button.add('search', 'Search And Replace');
this.button.setAwesome('search', 'fa-search');
this.button.addCallback(button, this.searchreplace.show);
$.extend(this.opts.shortcuts, {
'ctrl+f': { func: 'searchreplace.show', params: [] }
});
},
show: function () {
this.modal.addTemplate('searchreplace', getTemplate());
this.modal.load('searchreplace', 'Search And Replace', 500);
this.modal.setTitle('Search And Replace');
this.modal.createCancelButton();
var button = this.modal.createActionButton('Run');
button.on('click', this.searchreplace.run);
selectedText = this.selection.getText();
selectedHtml = this.selection.getHtml();
this.selection.get();
range = this.range;
this.selection.save();
this.modal.show();
if (selectedText) {
var numChars = selectedText.match(/[a-zA-Z]/g).length; //if selected text is longer then 20 chars then I assume the selection is needed for "only-selection"
if (numChars >= 20) {
$('#redactor-search-replace-search').val('');
$('#redactor-search-replace-search').focus();
} else {
$('#redactor-search-replace-search').val(selectedText);
$('#redactor-search-replace-replace').focus();
}
} else {
$('#redactor-search-replace-search').focus();
}
},
run: function () {
var search = $('#redactor-search-replace-search').val(),
replace = $('#redactor-search-replace-replace').val(),
ignoreCase = $('#redactor-search-replace-ignore-case').is(":checked"),
wordBoundary = $('#redactor-search-replace-word-boundary').is(":checked"),
searchSelection = $('#redactor-search-replace-only-selection').is(":checked");
if (search && replace) {
var text = searchSelection ? selectedHtml : this.code.get(); // first is text of current selection. second is whole text
var replacedText = (replaceAll(text, search, replace, ignoreCase, wordBoundary));
if (searchSelection) {
replaceSelectionWithHtml(range, replacedText);
} else {
this.insert.set(replacedText);
}
}
this.modal.close();
this.selection.restore();
}
};
// private functions
function getTemplate() {
return String()
+ '<section id="redactor-modal-search-replace">'
+ '<label>' + 'Search' + '</label>'
+ '<input type="text" size="5" id="redactor-search-replace-search" required />'
+ '<label>' + 'Replace' + '</label>'
+ '<input type="text" size="5" id="redactor-search-replace-replace" required />'
+ '<div><input type="checkbox" id="redactor-search-replace-ignore-case" />' + 'Ignore Case' + '</label></div>'
+ '<div><input type="checkbox" id="redactor-search-replace-word-boundary" />' + 'Match at Word Boundary' + '</label></div>'
+ '<div><input type="checkbox" id="redactor-search-replace-only-selection" />' + 'Search Only in Selection' + '</label></div>'
+ '</section>';
}
function replaceAll(string, find, replace, ignoreCase, wordBoundary) {
var regexOption = 'g' + (ignoreCase ? 'i' : '');
var cleanedFind = escapeRegExp(find);
var wordBoundarieFind = wordBoundary ? '\\b' + cleanedFind + '\\b' : cleanedFind;
var regex = new RegExp(wordBoundarieFind, regexOption);
return string.replace(regex, replace);
}
function escapeRegExp(string) {
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
function replaceSelectionWithHtml(range, html) {
var html;
if (window.getSelection && window.getSelection().getRangeAt) {
range.deleteContents();
var div = document.createElement("div");
div.innerHTML = html;
var frag = document.createDocumentFragment(), child;
while ((child = div.firstChild)) {
frag.appendChild(child);
}
range.insertNode(frag);
} else if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
range.pasteHTML(html);
}
}
};
})(jQuery);