remember last used search text in editor

This commit is contained in:
tophf 2017-12-14 11:29:44 +03:00
parent cc5a254e01
commit 748b9afdb4

View File

@ -51,14 +51,7 @@ onDOMscriptReady('/codemirror.js').then(() => {
setupLivePrefs(); setupLivePrefs();
rerouteHotkeys(true); rerouteHotkeys(true);
setupFindHooks();
for (const name of ['find', 'findNext', 'findPrev', 'replace']) {
ORIGINAL_COMMAND[name] = CodeMirror.commands[name];
}
for (const name of ['openDialog', 'openConfirm']) {
ORIGINAL_METHOD[name] = CodeMirror.prototype[name];
}
Object.assign(CodeMirror.commands, COMMANDS);
}).observe(document, {childList: true, subtree: true}); }).observe(document, {childList: true, subtree: true});
return; return;
@ -300,6 +293,19 @@ onDOMscriptReady('/codemirror.js').then(() => {
///////////////////// /////////////////////
function setupFindHooks() {
for (const name of ['find', 'findNext', 'findPrev', 'replace']) {
ORIGINAL_COMMAND[name] = CodeMirror.commands[name];
}
for (const name of ['openDialog', 'openConfirm']) {
ORIGINAL_METHOD[name] = CodeMirror.prototype[name];
}
Object.assign(CodeMirror.commands, COMMANDS);
chrome.storage.local.get('editSearchText', data => {
searchState = {query: data.editSearchText || null};
});
}
function shouldIgnoreCase(query) { function shouldIgnoreCase(query) {
// treat all-lowercase non-regexp queries as case-insensitive // treat all-lowercase non-regexp queries as case-insensitive
return typeof query === 'string' && query === query.toLowerCase(); return typeof query === 'string' && query === query.toLowerCase();
@ -307,11 +313,10 @@ onDOMscriptReady('/codemirror.js').then(() => {
function updateState(cm, newState) { function updateState(cm, newState) {
if (!newState) { if (!newState) {
const query = (cm.state.search || {}).query; if ((cm.state.search || {}).overlay) {
if (query !== null && query !== undefined) {
return cm.state.search; return cm.state.search;
} }
if (!searchState) { if (!searchState.overlay) {
return null; return null;
} }
newState = searchState; newState = searchState;
@ -353,9 +358,16 @@ onDOMscriptReady('/codemirror.js').then(() => {
function find(activeCM) { function find(activeCM) {
activeCM = focusClosestCM(activeCM); activeCM = focusClosestCM(activeCM);
const state = activeCM.state;
if (searchState.query && !(state.search || {}).lastQuery) {
(state.search = state.search || {}).query = searchState.query;
}
customizeOpenDialog(activeCM, template.find, function (query) { customizeOpenDialog(activeCM, template.find, function (query) {
this(query); this(query);
searchState = activeCM.state.search; searchState = state.search;
if (searchState.query) {
chrome.storage.local.set({editSearchText: searchState.query});
}
if (!searchState.query || if (!searchState.query ||
editors.length === 1 || editors.length === 1 ||
CodeMirror.cmpPos(searchState.posFrom, searchState.posTo)) { CodeMirror.cmpPos(searchState.posFrom, searchState.posTo)) {
@ -370,7 +382,7 @@ onDOMscriptReady('/codemirror.js').then(() => {
function findNext(activeCM, reverse) { function findNext(activeCM, reverse) {
let state = updateState(activeCM); let state = updateState(activeCM);
if (!state || !state.query) { if (!state || !state.overlay) {
find(activeCM); find(activeCM);
return; return;
} }