/* eslint-disable */ // CodeMirror, copyright (c) by Marijn Haverbeke and others // Distributed under an MIT license: http://codemirror.net/LICENSE // Define search commands. Depends on dialog.js or another // implementation of the openDialog method. // Replace works a little oddly -- it will do the replace on the next // Ctrl-G (or whatever is bound to findNext) press. You prevent a // replace by making sure the match is no longer selected when hitting // Ctrl-G. export default function(CodeMirror) { "use strict"; function searchOverlay(query) { return {token: function(stream) { query.lastIndex = stream.pos; var match = query.exec(stream.string); if (match && match.index == stream.pos) { stream.pos += match[0].length || 1; return "searching"; } else if (match) { stream.pos = match.index; } else { stream.skipToEnd(); } }}; } function SearchState() { this.posFrom = this.posTo = this.lastQuery = this.query = null; this.overlay = null; this.regexp = false; this.caseInsensitive = true; this.wholeWord = false; } function getSearchState(cm) { return cm.state.search || (cm.state.search = new SearchState()); } function getSearchCursor(cm, query, pos) { return cm.getSearchCursor(query, pos, getSearchState(cm).caseInsensitive); } function isMouseClick(event) { if(event.detail > 0) return true; else return false; } function persistentDialog(cm, text, deflt, onEnter, onKeyDown) { var searchField = document.getElementsByClassName("CodeMirror-search-field")[0]; if (!searchField) { cm.openDialog(text, onEnter, { value: deflt, selectValueOnOpen: true, closeOnEnter: false, onClose: function () { clearSearch(cm); }, onKeyDown: onKeyDown, closeOnBlur: false }); searchField = document.getElementsByClassName("CodeMirror-search-field")[0]; var dialog = document.getElementsByClassName("CodeMirror-dialog")[0]; var closeButton = dialog.getElementsByClassName("close")[0]; var state = getSearchState(cm); CodeMirror.on(searchField, "keyup", function (e) { if (e.keyCode !== 13 && searchField.value.length > 1) { // not enter and more than 1 character to search startSearch(cm, getSearchState(cm), searchField.value); } else if (searchField.value.length <= 1) { cm.display.wrapper.querySelector('.CodeMirror-search-results').innerText = ''; } }); CodeMirror.on(closeButton, "click", function () { clearSearch(cm); dialog.parentNode.removeChild(dialog); cm.focus(); }); var upArrow = dialog.getElementsByClassName("up-arrow")[0]; CodeMirror.on(upArrow, "click", function () { CodeMirror.commands.findPrev(cm); searchField.blur(); cm.focus(); }); var downArrow = dialog.getElementsByClassName("down-arrow")[0]; CodeMirror.on(downArrow, "click", function () { CodeMirror.commands.findNext(cm); searchField.blur(); cm.focus(); }); var regexpButton = dialog.getElementsByClassName("CodeMirror-regexp-button")[0]; CodeMirror.on(regexpButton, "click", function (event) { var state = getSearchState(cm); state.regexp = toggle(regexpButton); startSearch(cm, getSearchState(cm), searchField.value); if(isMouseClick(event)) searchField.focus(); }); toggle(regexpButton, state.regexp); var caseSensitiveButton = dialog.getElementsByClassName("CodeMirror-case-button")[0]; CodeMirror.on(caseSensitiveButton, "click", function (event) { var state = getSearchState(cm); state.caseInsensitive = !toggle(caseSensitiveButton); startSearch(cm, getSearchState(cm), searchField.value); if(isMouseClick(event)) searchField.focus(); }); toggle(caseSensitiveButton, !state.caseInsensitive); var wholeWordButton = dialog.getElementsByClassName("CodeMirror-word-button")[0]; CodeMirror.on(wholeWordButton, "click", function (event) { var state = getSearchState(cm); state.wholeWord = toggle(wholeWordButton); startSearch(cm, getSearchState(cm), searchField.value); if(isMouseClick(event)) searchField.focus(); }); toggle(wholeWordButton, state.wholeWord); function toggle(el, initialState) { var currentState, nextState; if (initialState == null) { currentState = el.getAttribute('aria-checked') === 'true'; nextState = !currentState; } else { nextState = initialState; } el.setAttribute('aria-checked', nextState); return nextState; } } else { searchField.focus(); searchField.select(); } } function dialog(cm, text, shortText, deflt, f) { if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true}); else f(prompt(shortText, deflt)); } var lastSelectedIndex = 0; function confirmDialog(cm, text, shortText, fs) { if (cm.openConfirm) cm.openConfirm(text, fs); else if (confirm(shortText)) fs[0](); var dialog = document.getElementsByClassName("CodeMirror-dialog")[0]; var buttons = dialog.getElementsByTagName("button"); buttons[lastSelectedIndex].focus(); for (var i = 0; i < buttons.length; i += 1) { (function (index) { var button = buttons[index]; button.addEventListener("focus", function (e) { lastSelectedIndex = index === buttons.length - 1 ? 0 : index; }); button.addEventListener("keyup", function (e) { if (e.keyCode === 37) { // arrow left var prevButton = index === 0 ? buttons.length - 1 : index - 1; buttons[prevButton].focus(); } if (e.keyCode === 39) { // arrow right var nextButton = index === buttons.length - 1 ? 0 : index + 1; buttons[nextButton].focus(); } if (e.keyCode === 27) { // esc cm.focus(); } }); button.addEventListener("click", function () { if (index === buttons.length - 1) { // "done" lastSelectedIndex = 0; } }) })(i); } } function parseString(string) { return string.replace(/\\(.)/g, function(_, ch) { if (ch == "n") return "\n" if (ch == "r") return "\r" return ch }) } /* Parses the query text and state and returns a RegExp ready for searching */ function parseQuery(query, state) { var emptyQuery = 'x^'; // matches nothing if (query === '') { // empty string matches nothing query = emptyQuery; } else { if (state.regexp === false) { query = parseString(query); query = query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); } if (state.wholeWord) { query += '\\b'; } } var regexp; try { regexp = new RegExp(query, state.caseInsensitive ? "gi" : "g"); } catch (e) { regexp = new RegExp(emptyQuery, 'g'); } // If the resulting regexp will match everything, do not use it if (regexp.test('')) { return new RegExp(emptyQuery, 'g'); } return regexp; } var queryDialog = `