Fix search widget when swapping document in new code viewer

This commit is contained in:
Raymond Hill 2023-03-16 12:36:02 -04:00
parent aa21952379
commit fd9bb02aab
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 29 additions and 5 deletions

View File

@ -208,7 +208,7 @@ async function setURL(resourceURL) {
afterDoc = new CodeMirror.Doc(r.text, r.mime || ''); afterDoc = new CodeMirror.Doc(r.text, r.mime || '');
urlToDocMap.set(afterURL, afterDoc); urlToDocMap.set(afterURL, afterDoc);
} }
cmEditor.swapDoc(afterDoc); swapDoc(afterDoc);
currentURL = afterURL; currentURL = afterURL;
setInputURL(afterURL); setInputURL(afterURL);
const a = qs$('.cm-search-widget .sourceURL'); const a = qs$('.cm-search-widget .sourceURL');
@ -244,6 +244,20 @@ function removeURL(url) {
/******************************************************************************/ /******************************************************************************/
function swapDoc(doc) {
const r = cmEditor.swapDoc(doc);
if ( self.searchThread ) {
self.searchThread.setHaystack(cmEditor.getValue());
}
const input = qs$('.cm-search-widget-input input[type="search"]');
if ( input.value !== '' ) {
qs$('.cm-search-widget').dispatchEvent(new Event('input'));
}
return r;
}
/******************************************************************************/
async function start() { async function start() {
await setURL(params.get('url')); await setURL(params.get('url'));
@ -254,7 +268,7 @@ async function start() {
dom.on('#reloadURL', 'click', ( ) => { dom.on('#reloadURL', 'click', ( ) => {
const input = qs$('#header input[type="url"]'); const input = qs$('#header input[type="url"]');
const url = input.value; const url = input.value;
const beforeDoc = cmEditor.swapDoc(new CodeMirror.Doc('', '')); const beforeDoc = swapDoc(new CodeMirror.Doc('', ''));
fetchResource(url).then(r => { fetchResource(url).then(r => {
if ( urlToDocMap.has(url) === false ) { return; } if ( urlToDocMap.has(url) === false ) { return; }
const afterDoc = r !== undefined const afterDoc = r !== undefined
@ -262,7 +276,7 @@ async function start() {
: beforeDoc; : beforeDoc;
urlToDocMap.set(url, afterDoc); urlToDocMap.set(url, afterDoc);
if ( currentURL !== url ) { return; } if ( currentURL !== url ) { return; }
cmEditor.swapDoc(afterDoc); swapDoc(afterDoc);
}); });
}); });

View File

@ -70,8 +70,18 @@
); );
}; };
const searchWidgetInputHandler = function(cm) { const searchWidgetInputHandler = function(cm, ev) {
let state = getSearchState(cm); const state = getSearchState(cm);
if ( ev.isTrusted !== true ) {
if ( state.queryText === '' ) {
clearSearch(cm);
} else {
cm.operation(function() {
startSearch(cm, state);
});
}
return;
}
if ( queryTextFromSearchWidget(cm) === state.queryText ) { return; } if ( queryTextFromSearchWidget(cm) === state.queryText ) { return; }
if ( state.queryTimer !== null ) { if ( state.queryTimer !== null ) {
clearTimeout(state.queryTimer); clearTimeout(state.queryTimer);