b4e1edf89aca93d0b8087f0f63b8d36f.js 18 KB

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