仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ user/amftp/View/js/ace/ext-searchbox.js 16.6 KB · 507 行 原始文件
Z zfhsh first commit 2 个月前
1define("ace/ext/searchbox",["require","exports","module","ace/lib/dom","ace/lib/lang","ace/lib/event","ace/keyboard/hash_handler","ace/lib/keys"], function(require, exports, module) {
2"use strict";
3
4var dom = require("../lib/dom");
5var lang = require("../lib/lang");
6var event = require("../lib/event");
7var searchboxCss = "\
8.ace_search {\
9background-color: #e1e7ec;\
10color: #666;\
11border: 1px solid #a4a4a4;\
12border-top: 0 none;\
13overflow: hidden;\
14margin: 0;\
15padding: 4px 6px 0 4px;\
16position: absolute;\
17top: 0;\
18z-index: 39;\
19white-space: normal;\
20}\
21.ace_search.left {\
22border-left: 0 none;\
23border-radius: 0px 0px 5px 0px;\
24left: 0;\
25}\
26.ace_search.right {\
27border-radius: 0px 0px 0px 5px;\
28border-right: 0 none;\
29right: 0;\
30}\
31.ace_search_form, .ace_replace_form {\
32margin: 0 20px 4px 0;\
33overflow: hidden;\
34line-height: 1.9;\
35}\
36.ace_replace_form {\
37margin-right: 0;\
38}\
39.ace_search_form.ace_nomatch {\
40outline: 1px solid red;\
41}\
42.ace_search_field {\
43border-radius: 3px 0 0 3px;\
44background-color: white;\
45color: black;\
46border: 1px solid #cbcbcb;\
47border-right: 0 none;\
48outline: 0;\
49padding: 0;\
50font-size: inherit;\
51margin: 0;\
52line-height: inherit;\
53padding: 0 6px;\
54min-width: 17em;\
55vertical-align: top;\
56min-height: 1.8em;\
57box-sizing: content-box;\
58}\
59.ace_searchbtn {\
60border: 1px solid #cbcbcb;\
61line-height: inherit;\
62display: inline-block;\
63padding: 0 6px;\
64background: #fff;\
65border-right: 0 none;\
66border-left: 1px solid #dcdcdc;\
67cursor: pointer;\
68margin: 0;\
69position: relative;\
70color: #666;\
71}\
72.ace_searchbtn:last-child {\
73border-radius: 0 3px 3px 0;\
74border-right: 1px solid #cbcbcb;\
75}\
76.ace_searchbtn:disabled {\
77background: none;\
78cursor: default;\
79}\
80.ace_searchbtn:hover {\
81background-color: #eef1f6;\
82}\
83.ace_searchbtn.prev, .ace_searchbtn.next {\
84padding: 0px 0.7em;\
85}\
86.ace_searchbtn_close {\
87background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAcCAYAAABRVo5BAAAAZ0lEQVR42u2SUQrAMAhDvazn8OjZBilCkYVVxiis8H4CT0VrAJb4WHT3C5xU2a2IQZXJjiQIRMdkEoJ5Q2yMqpfDIo+XY4k6h+YXOyKqTIj5REaxloNAd0xiKmAtsTHqW8sR2W5f7gCu5nWFUpVjZwAAAABJRU5ErkJggg==) no-repeat 50% 0;\
88border-radius: 50%;\
89border: 0 none;\
90color: #656565;\
91cursor: pointer;\
92font: 16px/16px Arial;\
93padding: 0;\
94height: 14px;\
95width: 14px;\
96top: 9px;\
97right: 7px;\
98position: absolute;\
99}\
100.ace_searchbtn_close:hover {\
101background-color: #656565;\
102background-position: 50% 100%;\
103color: white;\
104}\
105.ace_button {\
106margin-right: 4px;\
107cursor: pointer;\
108-webkit-user-select: none;\
109-moz-user-select: none;\
110-o-user-select: none;\
111-ms-user-select: none;\
112user-select: none;\
113overflow: hidden;\
114opacity: 0.6;\
115border: 1px solid #767272;\
116padding: 0px 2px;\
117box-sizing: border-box!important;\
118color: black;\
119background: #fff;\
120border-radius: 3px;\
121}\
122.ace_button:hover {\
123background-color: #eee;\
124opacity:1;\
125}\
126.ace_button:active {\
127background-color: #ddd;\
128}\
129.ace_button.checked {\
130border-color: #3399ff;\
131opacity:1;\
132}\
133.ace_search_options{\
134margin-top: 12px;\
135margin-bottom: 3px;\
136text-align: left;\
137-webkit-user-select: none;\
138-moz-user-select: none;\
139-o-user-select: none;\
140-ms-user-select: none;\
141user-select: none;\
142clear: both;\
143}\
144.ace_search_counter {\
145float: right;\
146font-family: arial;\
147padding: 0 8px;\
148}";
149var HashHandler = require("../keyboard/hash_handler").HashHandler;
150var keyUtil = require("../lib/keys");
151
152var MAX_COUNT = 999;
153
154dom.importCssString(searchboxCss, "ace_searchbox");
155
156var SearchBox = function(editor, range, showReplaceForm) {
157 var div = dom.createElement("div");
158 dom.buildDom(["div", {class:"ace_search right", id: 'search_block'},
159 ["span", {action: "hide", class: "ace_searchbtn_close"}],
160 ["div", {class: "ace_search_form"},
161 ["input", {class: "ace_search_field", spellcheck: "false"}],
162 ["span", {action: "findPrev", class: "ace_searchbtn prev"}, "上一个"],
163 ["span", {action: "findNext", class: "ace_searchbtn next"}, "下一个"],
164 ["span", {action: "findAll", class: "ace_searchbtn"}, "查找所有"]
165 ],
166 ["div", {class: "ace_replace_form"},
167 ["input", {class: "ace_search_field", spellcheck: "false"}],
168 ["span", {action: "replaceAndFindNext", class: "ace_searchbtn"}, "替换"],
169 ["span", {action: "replaceAll", class: "ace_searchbtn"}, "替换所有"]
170 ],
171 ["div", {class: "ace_search_options"},
172 ["span", {action: "toggleReplace", class: "ace_button", title: "Toggle Replace mode",
173 style: "float:left;margin-top:-2px;padding:0 5px;display:none"}, "+"],
174 ["span", {class: "ace_search_counter"}],
175 ["span", {action: "toggleRegexpMode", class: "ace_button tip", title: "开启正则匹配"}, ".*"],
176 ["span", {action: "toggleCaseSensitive", class: "ace_button tip", title: "区分大小写"}, "Aa"],
177 ["span", {action: "toggleWholeWords", class: "ace_button tip", title: "完整词语匹配"}, "\\b"],
178 ["span", {action: "searchInSelection", class: "ace_button tip", title: "选择匹配 (先选择内容再开启)"}, "S"]
179 ]
180 ], div);
181 this.element = div.firstChild;
182
183 this.setSession = this.setSession.bind(this);
184
185 this.$init();
186 this.setEditor(editor);
187 dom.importCssString(searchboxCss, "ace_searchbox", editor.container);
188};
189
190(function() {
191 this.setEditor = function(editor) {
192 editor.searchBox = this;
193 editor.renderer.scroller.appendChild(this.element);
194 this.editor = editor;
195 // tip_options.className = 'tip-amh';
196 // $('#search_block .tip').poshytip(tip_options);
197 };
198
199 this.setSession = function(e) {
200 this.searchRange = null;
201 this.$syncOptions(true);
202 };
203
204 this.$initElements = function(sb) {
205 this.searchBox = sb.querySelector(".ace_search_form");
206 this.replaceBox = sb.querySelector(".ace_replace_form");
207 this.searchOption = sb.querySelector("[action=searchInSelection]");
208 this.replaceOption = sb.querySelector("[action=toggleReplace]");
209 this.regExpOption = sb.querySelector("[action=toggleRegexpMode]");
210 this.caseSensitiveOption = sb.querySelector("[action=toggleCaseSensitive]");
211 this.wholeWordOption = sb.querySelector("[action=toggleWholeWords]");
212 this.searchInput = this.searchBox.querySelector(".ace_search_field");
213 this.replaceInput = this.replaceBox.querySelector(".ace_search_field");
214 this.searchCounter = sb.querySelector(".ace_search_counter");
215 };
216
217 this.$init = function() {
218 var sb = this.element;
219
220 this.$initElements(sb);
221
222 var _this = this;
223 event.addListener(sb, "mousedown", function(e) {
224 setTimeout(function(){
225 _this.activeInput.focus();
226 }, 0);
227 event.stopPropagation(e);
228 });
229 event.addListener(sb, "click", function(e) {
230 var t = e.target || e.srcElement;
231 var action = t.getAttribute("action");
232 if (action && _this[action])
233 _this[action]();
234 else if (_this.$searchBarKb.commands[action])
235 _this.$searchBarKb.commands[action].exec(_this);
236 event.stopPropagation(e);
237 });
238
239 event.addCommandKeyListener(sb, function(e, hashId, keyCode) {
240 var keyString = keyUtil.keyCodeToString(keyCode);
241 var command = _this.$searchBarKb.findKeyCommand(hashId, keyString);
242 if (command && command.exec) {
243 command.exec(_this);
244 event.stopEvent(e);
245 }
246 });
247
248 this.$onChange = lang.delayedCall(function() {
249 _this.find(false, false);
250 });
251
252 event.addListener(this.searchInput, "input", function() {
253 _this.$onChange.schedule(20);
254 });
255 event.addListener(this.searchInput, "focus", function() {
256 _this.activeInput = _this.searchInput;
257 _this.searchInput.value && _this.highlight();
258 });
259 event.addListener(this.replaceInput, "focus", function() {
260 _this.activeInput = _this.replaceInput;
261 _this.searchInput.value && _this.highlight();
262 });
263 };
264 this.$closeSearchBarKb = new HashHandler([{
265 bindKey: "Esc",
266 name: "closeSearchBar",
267 exec: function(editor) {
268 editor.searchBox.hide();
269 }
270 }]);
271 this.$searchBarKb = new HashHandler();
272 this.$searchBarKb.bindKeys({
273 "Ctrl-f|Command-f": function(sb) {
274 var isReplace = sb.isReplace = !sb.isReplace;
275 sb.replaceBox.style.display = isReplace ? "" : "none";
276 sb.replaceOption.checked = false;
277 sb.$syncOptions();
278 sb.searchInput.focus();
279 },
280 "Ctrl-H|Command-Option-F": function(sb) {
281 if (sb.editor.getReadOnly())
282 return;
283 sb.replaceOption.checked = true;
284 sb.$syncOptions();
285 sb.replaceInput.focus();
286 },
287 "Ctrl-G|Command-G": function(sb) {
288 sb.findNext();
289 },
290 "Ctrl-Shift-G|Command-Shift-G": function(sb) {
291 sb.findPrev();
292 },
293 "esc": function(sb) {
294 setTimeout(function() { sb.hide();});
295 },
296 "Return": function(sb) {
297 if (sb.activeInput == sb.replaceInput)
298 sb.replace();
299 sb.findNext();
300 },
301 "Shift-Return": function(sb) {
302 if (sb.activeInput == sb.replaceInput)
303 sb.replace();
304 sb.findPrev();
305 },
306 "Alt-Return": function(sb) {
307 if (sb.activeInput == sb.replaceInput)
308 sb.replaceAll();
309 sb.findAll();
310 },
311 "Tab": function(sb) {
312 (sb.activeInput == sb.replaceInput ? sb.searchInput : sb.replaceInput).focus();
313 }
314 });
315
316 this.$searchBarKb.addCommands([{
317 name: "toggleRegexpMode",
318 bindKey: {win: "Alt-R|Alt-/", mac: "Ctrl-Alt-R|Ctrl-Alt-/"},
319 exec: function(sb) {
320 sb.regExpOption.checked = !sb.regExpOption.checked;
321 sb.$syncOptions();
322 }
323 }, {
324 name: "toggleCaseSensitive",
325 bindKey: {win: "Alt-C|Alt-I", mac: "Ctrl-Alt-R|Ctrl-Alt-I"},
326 exec: function(sb) {
327 sb.caseSensitiveOption.checked = !sb.caseSensitiveOption.checked;
328 sb.$syncOptions();
329 }
330 }, {
331 name: "toggleWholeWords",
332 bindKey: {win: "Alt-B|Alt-W", mac: "Ctrl-Alt-B|Ctrl-Alt-W"},
333 exec: function(sb) {
334 sb.wholeWordOption.checked = !sb.wholeWordOption.checked;
335 sb.$syncOptions();
336 }
337 }, {
338 name: "toggleReplace",
339 exec: function(sb) {
340 sb.replaceOption.checked = !sb.replaceOption.checked;
341 sb.$syncOptions();
342 }
343 }, {
344 name: "searchInSelection",
345 exec: function(sb) {
346 sb.searchOption.checked = !sb.searchRange;
347 sb.setSearchRange(sb.searchOption.checked && sb.editor.getSelectionRange());
348 sb.$syncOptions();
349 }
350 }]);
351
352 this.setSearchRange = function(range) {
353 this.searchRange = range;
354 if (range) {
355 this.searchRangeMarker = this.editor.session.addMarker(range, "ace_active-line");
356 } else if (this.searchRangeMarker) {
357 this.editor.session.removeMarker(this.searchRangeMarker);
358 this.searchRangeMarker = null;
359 }
360 };
361
362 this.$syncOptions = function(preventScroll) {
363 dom.setCssClass(this.replaceOption, "checked", this.searchRange);
364 dom.setCssClass(this.searchOption, "checked", this.searchOption.checked);
365 this.replaceOption.textContent = this.replaceOption.checked ? "-" : "+";
366 dom.setCssClass(this.regExpOption, "checked", this.regExpOption.checked);
367 dom.setCssClass(this.wholeWordOption, "checked", this.wholeWordOption.checked);
368 dom.setCssClass(this.caseSensitiveOption, "checked", this.caseSensitiveOption.checked);
369 var readOnly = this.editor.getReadOnly();
370 this.replaceOption.style.display = "none";
371 this.replaceBox.style.display = this.replaceOption.checked && !readOnly ? "" : "none";
372 this.find(false, false, preventScroll);
373 };
374
375 this.highlight = function(re) {
376 this.editor.session.highlight(re || this.editor.$search.$options.re);
377 this.editor.renderer.updateBackMarkers();
378 };
379 this.find = function(skipCurrent, backwards, preventScroll) {
380 var range = this.editor.find(this.searchInput.value, {
381 skipCurrent: skipCurrent,
382 backwards: backwards,
383 wrap: true,
384 regExp: this.regExpOption.checked,
385 caseSensitive: this.caseSensitiveOption.checked,
386 wholeWord: this.wholeWordOption.checked,
387 preventScroll: preventScroll,
388 range: this.searchRange
389 });
390 var noMatch = !range && this.searchInput.value;
391 dom.setCssClass(this.searchBox, "ace_nomatch", noMatch);
392 this.editor._emit("findSearchBox", { match: !noMatch });
393 this.highlight();
394 this.updateCounter();
395 };
396 this.updateCounter = function() {
397 var editor = this.editor;
398 var regex = editor.$search.$options.re;
399 var all = 0;
400 var before = 0;
401 if (regex) {
402 var value = this.searchRange
403 ? editor.session.getTextRange(this.searchRange)
404 : editor.getValue();
405
406 var offset = editor.session.doc.positionToIndex(editor.selection.anchor);
407 if (this.searchRange)
408 offset -= editor.session.doc.positionToIndex(this.searchRange.start);
409
410 var last = regex.lastIndex = 0;
411 var m;
412 while ((m = regex.exec(value))) {
413 all++;
414 last = m.index;
415 if (last <= offset)
416 before++;
417 if (all > MAX_COUNT)
418 break;
419 if (!m[0]) {
420 regex.lastIndex = last += 1;
421 if (last >= value.length)
422 break;
423 }
424 }
425 }
426 this.searchCounter.textContent = before + " 总 " + (all > MAX_COUNT ? MAX_COUNT + "+" : all);
427 };
428 this.findNext = function() {
429 this.find(true, false);
430 };
431 this.findPrev = function() {
432 this.find(true, true);
433 };
434 this.findAll = function(){
435 var range = this.editor.findAll(this.searchInput.value, {
436 regExp: this.regExpOption.checked,
437 caseSensitive: this.caseSensitiveOption.checked,
438 wholeWord: this.wholeWordOption.checked
439 });
440 var noMatch = !range && this.searchInput.value;
441 dom.setCssClass(this.searchBox, "ace_nomatch", noMatch);
442 this.editor._emit("findSearchBox", { match: !noMatch });
443 this.highlight();
444 this.hide();
445 };
446 this.replace = function() {
447 if (!this.editor.getReadOnly())
448 this.editor.replace(this.replaceInput.value);
449 };
450 this.replaceAndFindNext = function() {
451 if (!this.editor.getReadOnly()) {
452 this.editor.replace(this.replaceInput.value);
453 this.findNext();
454 }
455 };
456 this.replaceAll = function() {
457 if (!this.editor.getReadOnly())
458 this.editor.replaceAll(this.replaceInput.value);
459 };
460
461 this.hide = function() {
462 this.active = false;
463 this.setSearchRange(null);
464 this.editor.off("changeSession", this.setSession);
465
466 this.element.style.display = "none";
467 this.editor.keyBinding.removeKeyboardHandler(this.$closeSearchBarKb);
468 this.editor.focus();
469 };
470 this.show = function(value, isReplace) {
471 this.active = true;
472 this.editor.on("changeSession", this.setSession);
473 this.element.style.display = "";
474 this.replaceOption.checked = isReplace;
475
476 if (value)
477 this.searchInput.value = value;
478
479 this.searchInput.focus();
480 this.searchInput.select();
481
482 this.editor.keyBinding.addKeyboardHandler(this.$closeSearchBarKb);
483
484 this.$syncOptions(true);
485 };
486
487 this.isFocused = function() {
488 var el = document.activeElement;
489 return el == this.searchInput || el == this.replaceInput;
490 };
491}).call(SearchBox.prototype);
492
493exports.SearchBox = SearchBox;
494
495exports.Search = function(editor, isReplace) {
496 var sb = editor.searchBox || new SearchBox(editor);
497 sb.show(editor.session.getTextRange(), isReplace);
498};
499
500}); (function() {
501 window.require(["ace/ext/searchbox"], function(m) {
502 if (typeof module == "object" && typeof exports == "object" && module) {
503 module.exports = m;
504 }
505 });
506 })();
507