ext-searchbox.js 12 KB

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