ext-searchbox.js 16 KB

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