ext-searchbox.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. border: 1px solid #cbcbcb;\
  10. border-top: 0 none;\
  11. max-width: 325px;\
  12. overflow: hidden;\
  13. margin: 0;\
  14. padding: 4px;\
  15. padding-right: 6px;\
  16. padding-bottom: 0;\
  17. position: absolute;\
  18. top: 0px;\
  19. z-index: 99;\
  20. white-space: normal;\
  21. }\
  22. .ace_search.left {\
  23. border-left: 0 none;\
  24. border-radius: 0px 0px 5px 0px;\
  25. left: 0;\
  26. }\
  27. .ace_search.right {\
  28. border-radius: 0px 0px 0px 5px;\
  29. border-right: 0 none;\
  30. right: 0;\
  31. }\
  32. .ace_search_form, .ace_replace_form {\
  33. border-radius: 3px;\
  34. border: 1px solid #cbcbcb;\
  35. float: left;\
  36. margin-bottom: 4px;\
  37. overflow: hidden;\
  38. }\
  39. .ace_search_form.ace_nomatch {\
  40. outline: 1px solid red;\
  41. }\
  42. .ace_search_field {\
  43. background-color: white;\
  44. color: black;\
  45. border-right: 1px solid #cbcbcb;\
  46. border: 0 none;\
  47. -webkit-box-sizing: border-box;\
  48. -moz-box-sizing: border-box;\
  49. box-sizing: border-box;\
  50. float: left;\
  51. height: 22px;\
  52. outline: 0;\
  53. padding: 0 7px;\
  54. width: 214px;\
  55. margin: 0;\
  56. }\
  57. .ace_searchbtn,\
  58. .ace_replacebtn {\
  59. background: #fff;\
  60. border: 0 none;\
  61. border-left: 1px solid #dcdcdc;\
  62. cursor: pointer;\
  63. float: left;\
  64. height: 22px;\
  65. margin: 0;\
  66. position: relative;\
  67. }\
  68. .ace_searchbtn:last-child,\
  69. .ace_replacebtn:last-child {\
  70. border-top-right-radius: 3px;\
  71. border-bottom-right-radius: 3px;\
  72. }\
  73. .ace_searchbtn:disabled {\
  74. background: none;\
  75. cursor: default;\
  76. }\
  77. .ace_searchbtn {\
  78. background-position: 50% 50%;\
  79. background-repeat: no-repeat;\
  80. width: 27px;\
  81. }\
  82. .ace_searchbtn.prev {\
  83. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAFCAYAAAB4ka1VAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADFJREFUeNpiSU1NZUAC/6E0I0yACYskCpsJiySKIiY0SUZk40FyTEgCjGgKwTRAgAEAQJUIPCE+qfkAAAAASUVORK5CYII=); \
  84. }\
  85. .ace_searchbtn.next {\
  86. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAFCAYAAAB4ka1VAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADRJREFUeNpiTE1NZQCC/0DMyIAKwGJMUAYDEo3M/s+EpvM/mkKwCQxYjIeLMaELoLMBAgwAU7UJObTKsvAAAAAASUVORK5CYII=); \
  87. }\
  88. .ace_searchbtn_close {\
  89. background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAcCAYAAABRVo5BAAAAZ0lEQVR42u2SUQrAMAhDvazn8OjZBilCkYVVxiis8H4CT0VrAJb4WHT3C5xU2a2IQZXJjiQIRMdkEoJ5Q2yMqpfDIo+XY4k6h+YXOyKqTIj5REaxloNAd0xiKmAtsTHqW8sR2W5f7gCu5nWFUpVjZwAAAABJRU5ErkJggg==) no-repeat 50% 0;\
  90. border-radius: 50%;\
  91. border: 0 none;\
  92. color: #656565;\
  93. cursor: pointer;\
  94. float: right;\
  95. font: 16px/16px Arial;\
  96. height: 14px;\
  97. margin: 5px 1px 9px 5px;\
  98. padding: 0;\
  99. text-align: center;\
  100. width: 14px;\
  101. }\
  102. .ace_searchbtn_close:hover {\
  103. background-color: #656565;\
  104. background-position: 50% 100%;\
  105. color: white;\
  106. }\
  107. .ace_replacebtn.prev {\
  108. width: 54px\
  109. }\
  110. .ace_replacebtn.next {\
  111. width: 27px\
  112. }\
  113. .ace_button {\
  114. margin-left: 2px;\
  115. cursor: pointer;\
  116. -webkit-user-select: none;\
  117. -moz-user-select: none;\
  118. -o-user-select: none;\
  119. -ms-user-select: none;\
  120. user-select: none;\
  121. overflow: hidden;\
  122. opacity: 0.7;\
  123. border: 1px solid rgba(100,100,100,0.23);\
  124. padding: 1px;\
  125. -moz-box-sizing: border-box;\
  126. box-sizing: border-box;\
  127. color: black;\
  128. }\
  129. .ace_button:hover {\
  130. background-color: #eee;\
  131. opacity:1;\
  132. }\
  133. .ace_button:active {\
  134. background-color: #ddd;\
  135. }\
  136. .ace_button.checked {\
  137. border-color: #3399ff;\
  138. opacity:1;\
  139. }\
  140. .ace_search_options{\
  141. margin-bottom: 3px;\
  142. text-align: right;\
  143. -webkit-user-select: none;\
  144. -moz-user-select: none;\
  145. -o-user-select: none;\
  146. -ms-user-select: none;\
  147. user-select: none;\
  148. }";
  149. var HashHandler = require("../keyboard/hash_handler").HashHandler;
  150. var keyUtil = require("../lib/keys");
  151. dom.importCssString(searchboxCss, "ace_searchbox");
  152. var html = '<div class="ace_search right">\
  153. <button type="button" action="hide" class="ace_searchbtn_close"></button>\
  154. <div class="ace_search_form">\
  155. <input class="ace_search_field" placeholder="Search for" spellcheck="false"></input>\
  156. <button type="button" action="findNext" class="ace_searchbtn next"></button>\
  157. <button type="button" action="findPrev" class="ace_searchbtn prev"></button>\
  158. <button type="button" action="findAll" class="ace_searchbtn" title="Alt-Enter">All</button>\
  159. </div>\
  160. <div class="ace_replace_form">\
  161. <input class="ace_search_field" placeholder="Replace with" spellcheck="false"></input>\
  162. <button type="button" action="replaceAndFindNext" class="ace_replacebtn">Replace</button>\
  163. <button type="button" action="replaceAll" class="ace_replacebtn">All</button>\
  164. </div>\
  165. <div class="ace_search_options">\
  166. <span action="toggleRegexpMode" class="ace_button" title="RegExp Search">.*</span>\
  167. <span action="toggleCaseSensitive" class="ace_button" title="CaseSensitive Search">Aa</span>\
  168. <span action="toggleWholeWords" class="ace_button" title="Whole Word Search">\\b</span>\
  169. </div>\
  170. </div>'.replace(/>\s+/g, ">");
  171. var SearchBox = function(editor, range, showReplaceForm) {
  172. var div = dom.createElement("div");
  173. div.innerHTML = html;
  174. this.element = div.firstChild;
  175. this.$init();
  176. this.setEditor(editor);
  177. };
  178. (function() {
  179. this.setEditor = function(editor) {
  180. editor.searchBox = this;
  181. editor.container.appendChild(this.element);
  182. this.editor = editor;
  183. };
  184. this.$initElements = function(sb) {
  185. this.searchBox = sb.querySelector(".ace_search_form");
  186. this.replaceBox = sb.querySelector(".ace_replace_form");
  187. this.searchOptions = sb.querySelector(".ace_search_options");
  188. this.regExpOption = sb.querySelector("[action=toggleRegexpMode]");
  189. this.caseSensitiveOption = sb.querySelector("[action=toggleCaseSensitive]");
  190. this.wholeWordOption = sb.querySelector("[action=toggleWholeWords]");
  191. this.searchInput = this.searchBox.querySelector(".ace_search_field");
  192. this.replaceInput = this.replaceBox.querySelector(".ace_search_field");
  193. };
  194. this.$init = function() {
  195. var sb = this.element;
  196. this.$initElements(sb);
  197. var _this = this;
  198. event.addListener(sb, "mousedown", function(e) {
  199. setTimeout(function(){
  200. _this.activeInput.focus();
  201. }, 0);
  202. event.stopPropagation(e);
  203. });
  204. event.addListener(sb, "click", function(e) {
  205. var t = e.target || e.srcElement;
  206. var action = t.getAttribute("action");
  207. if (action && _this[action])
  208. _this[action]();
  209. else if (_this.$searchBarKb.commands[action])
  210. _this.$searchBarKb.commands[action].exec(_this);
  211. event.stopPropagation(e);
  212. });
  213. event.addCommandKeyListener(sb, function(e, hashId, keyCode) {
  214. var keyString = keyUtil.keyCodeToString(keyCode);
  215. var command = _this.$searchBarKb.findKeyCommand(hashId, keyString);
  216. if (command && command.exec) {
  217. command.exec(_this);
  218. event.stopEvent(e);
  219. }
  220. });
  221. this.$onChange = lang.delayedCall(function() {
  222. _this.find(false, false);
  223. });
  224. event.addListener(this.searchInput, "input", function() {
  225. _this.$onChange.schedule(20);
  226. });
  227. event.addListener(this.searchInput, "focus", function() {
  228. _this.activeInput = _this.searchInput;
  229. _this.searchInput.value && _this.highlight();
  230. });
  231. event.addListener(this.replaceInput, "focus", function() {
  232. _this.activeInput = _this.replaceInput;
  233. _this.searchInput.value && _this.highlight();
  234. });
  235. };
  236. this.$closeSearchBarKb = new HashHandler([{
  237. bindKey: "Esc",
  238. name: "closeSearchBar",
  239. exec: function(editor) {
  240. editor.searchBox.hide();
  241. }
  242. }]);
  243. this.$searchBarKb = new HashHandler();
  244. this.$searchBarKb.bindKeys({
  245. "Ctrl-f|Command-f": function(sb) {
  246. var isReplace = sb.isReplace = !sb.isReplace;
  247. sb.replaceBox.style.display = isReplace ? "" : "none";
  248. sb.searchInput.focus();
  249. },
  250. "Ctrl-H|Command-Option-F": function(sb) {
  251. sb.replaceBox.style.display = "";
  252. sb.replaceInput.focus();
  253. },
  254. "Ctrl-G|Command-G": function(sb) {
  255. sb.findNext();
  256. },
  257. "Ctrl-Shift-G|Command-Shift-G": function(sb) {
  258. sb.findPrev();
  259. },
  260. "esc": function(sb) {
  261. setTimeout(function() { sb.hide();});
  262. },
  263. "Return": function(sb) {
  264. if (sb.activeInput == sb.replaceInput)
  265. sb.replace();
  266. sb.findNext();
  267. },
  268. "Shift-Return": function(sb) {
  269. if (sb.activeInput == sb.replaceInput)
  270. sb.replace();
  271. sb.findPrev();
  272. },
  273. "Alt-Return": function(sb) {
  274. if (sb.activeInput == sb.replaceInput)
  275. sb.replaceAll();
  276. sb.findAll();
  277. },
  278. "Tab": function(sb) {
  279. (sb.activeInput == sb.replaceInput ? sb.searchInput : sb.replaceInput).focus();
  280. }
  281. });
  282. this.$searchBarKb.addCommands([{
  283. name: "toggleRegexpMode",
  284. bindKey: {win: "Alt-R|Alt-/", mac: "Ctrl-Alt-R|Ctrl-Alt-/"},
  285. exec: function(sb) {
  286. sb.regExpOption.checked = !sb.regExpOption.checked;
  287. sb.$syncOptions();
  288. }
  289. }, {
  290. name: "toggleCaseSensitive",
  291. bindKey: {win: "Alt-C|Alt-I", mac: "Ctrl-Alt-R|Ctrl-Alt-I"},
  292. exec: function(sb) {
  293. sb.caseSensitiveOption.checked = !sb.caseSensitiveOption.checked;
  294. sb.$syncOptions();
  295. }
  296. }, {
  297. name: "toggleWholeWords",
  298. bindKey: {win: "Alt-B|Alt-W", mac: "Ctrl-Alt-B|Ctrl-Alt-W"},
  299. exec: function(sb) {
  300. sb.wholeWordOption.checked = !sb.wholeWordOption.checked;
  301. sb.$syncOptions();
  302. }
  303. }]);
  304. this.$syncOptions = function() {
  305. dom.setCssClass(this.regExpOption, "checked", this.regExpOption.checked);
  306. dom.setCssClass(this.wholeWordOption, "checked", this.wholeWordOption.checked);
  307. dom.setCssClass(this.caseSensitiveOption, "checked", this.caseSensitiveOption.checked);
  308. this.find(false, false);
  309. };
  310. this.highlight = function(re) {
  311. this.editor.session.highlight(re || this.editor.$search.$options.re);
  312. this.editor.renderer.updateBackMarkers()
  313. };
  314. this.find = function(skipCurrent, backwards, preventScroll) {
  315. var range = this.editor.find(this.searchInput.value, {
  316. skipCurrent: skipCurrent,
  317. backwards: backwards,
  318. wrap: true,
  319. regExp: this.regExpOption.checked,
  320. caseSensitive: this.caseSensitiveOption.checked,
  321. wholeWord: this.wholeWordOption.checked,
  322. preventScroll: preventScroll
  323. });
  324. var noMatch = !range && this.searchInput.value;
  325. dom.setCssClass(this.searchBox, "ace_nomatch", noMatch);
  326. this.editor._emit("findSearchBox", { match: !noMatch });
  327. this.highlight();
  328. };
  329. this.findNext = function() {
  330. this.find(true, false);
  331. };
  332. this.findPrev = function() {
  333. this.find(true, true);
  334. };
  335. this.findAll = function(){
  336. var range = this.editor.findAll(this.searchInput.value, {
  337. regExp: this.regExpOption.checked,
  338. caseSensitive: this.caseSensitiveOption.checked,
  339. wholeWord: this.wholeWordOption.checked
  340. });
  341. var noMatch = !range && this.searchInput.value;
  342. dom.setCssClass(this.searchBox, "ace_nomatch", noMatch);
  343. this.editor._emit("findSearchBox", { match: !noMatch });
  344. this.highlight();
  345. this.hide();
  346. };
  347. this.replace = function() {
  348. if (!this.editor.getReadOnly())
  349. this.editor.replace(this.replaceInput.value);
  350. };
  351. this.replaceAndFindNext = function() {
  352. if (!this.editor.getReadOnly()) {
  353. this.editor.replace(this.replaceInput.value);
  354. this.findNext()
  355. }
  356. };
  357. this.replaceAll = function() {
  358. if (!this.editor.getReadOnly())
  359. this.editor.replaceAll(this.replaceInput.value);
  360. };
  361. this.hide = function() {
  362. this.element.style.display = "none";
  363. this.editor.keyBinding.removeKeyboardHandler(this.$closeSearchBarKb);
  364. this.editor.focus();
  365. };
  366. this.show = function(value, isReplace) {
  367. this.element.style.display = "";
  368. this.replaceBox.style.display = isReplace ? "" : "none";
  369. this.isReplace = isReplace;
  370. if (value)
  371. this.searchInput.value = value;
  372. this.find(false, false, true);
  373. this.searchInput.focus();
  374. this.searchInput.select();
  375. this.editor.keyBinding.addKeyboardHandler(this.$closeSearchBarKb);
  376. };
  377. this.isFocused = function() {
  378. var el = document.activeElement;
  379. return el == this.searchInput || el == this.replaceInput;
  380. }
  381. }).call(SearchBox.prototype);
  382. exports.SearchBox = SearchBox;
  383. exports.Search = function(editor, isReplace) {
  384. var sb = editor.searchBox || new SearchBox(editor);
  385. sb.show(editor.session.getTextRange(), isReplace);
  386. };
  387. });
  388. (function() {
  389. ace.require(["ace/ext/searchbox"], function() {});
  390. })();