Query parser fix (#695)

* Standardize setContent calls

* Rename _queryText to _fullQuery

* Move query parser into Display and update float.html

* Generalize params

* Add "full" parameter to maintain the original full query
This commit is contained in:
toasted-nutbread 2020-07-26 19:29:12 -04:00 committed by GitHub
parent 0512258c8e
commit 313476aa92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 52 deletions

View File

@ -19,7 +19,6 @@
* ClipboardMonitor * ClipboardMonitor
* DOM * DOM
* Display * Display
* QueryParser
* api * api
* wanakana * wanakana
*/ */
@ -33,16 +32,11 @@ class DisplaySearch extends Display {
this._intro = document.querySelector('#intro'); this._intro = document.querySelector('#intro');
this._clipboardMonitorEnable = document.querySelector('#clipboard-monitor-enable'); this._clipboardMonitorEnable = document.querySelector('#clipboard-monitor-enable');
this._wanakanaEnable = document.querySelector('#wanakana-enable'); this._wanakanaEnable = document.querySelector('#wanakana-enable');
this._queryText = '';
this._introVisible = true; this._introVisible = true;
this._introAnimationTimer = null; this._introAnimationTimer = null;
this._clipboardMonitor = new ClipboardMonitor({ this._clipboardMonitor = new ClipboardMonitor({
getClipboard: api.clipboardGet.bind(api) getClipboard: api.clipboardGet.bind(api)
}); });
this._queryParser = new QueryParser({
getOptionsContext: this.getOptionsContext.bind(this),
setSpinnerVisible: this.setSpinnerVisible.bind(this)
});
this._onKeyDownIgnoreKeys = new Map([ this._onKeyDownIgnoreKeys = new Map([
['ANY_MOD', new Set([ ['ANY_MOD', new Set([
'Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageDown', 'PageUp', 'Home', 'End', 'Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageDown', 'PageUp', 'Home', 'End',
@ -66,9 +60,7 @@ class DisplaySearch extends Display {
await super.prepare(); await super.prepare();
await this.updateOptions(); await this.updateOptions();
yomichan.on('optionsUpdated', () => this.updateOptions()); yomichan.on('optionsUpdated', () => this.updateOptions());
await this._queryParser.prepare();
this._queryParser.on('searched', this._onQueryParserSearch.bind(this));
this.on('contentUpdating', this._onContentUpdating.bind(this)); this.on('contentUpdating', this._onContentUpdating.bind(this));
this.setHistorySettings({useBrowserHistory: true}); this.setHistorySettings({useBrowserHistory: true});
@ -152,8 +144,6 @@ class DisplaySearch extends Display {
async updateOptions() { async updateOptions() {
await super.updateOptions(); await super.updateOptions();
const options = this.getOptions();
this._queryParser.setOptions(options);
if (!this._isPrepared) { return; } if (!this._isPrepared) { return; }
const query = this._query.value; const query = this._query.value;
if (query) { if (query) {
@ -164,7 +154,7 @@ class DisplaySearch extends Display {
// Private // Private
_onContentUpdating({type, source, content}) { _onContentUpdating({type, source, content, urlSearchParams}) {
let animate = false; let animate = false;
let valid = false; let valid = false;
switch (type) { switch (type) {
@ -180,33 +170,19 @@ class DisplaySearch extends Display {
source = ''; source = '';
break; break;
} }
if (typeof source !== 'string') { source = ''; } if (typeof source !== 'string') { source = ''; }
let full = urlSearchParams.get('full');
if (full === null) { full = source; }
this._closePopups(); this._closePopups();
this._setQuery(source); this._setQuery(full);
this._setIntroVisible(!valid, animate); this._setIntroVisible(!valid, animate);
this._setTitleText(source); this._setTitleText(source);
this._updateSearchButton(); this._updateSearchButton();
} }
_onQueryParserSearch({type, definitions, sentence, cause, textSource}) {
this.setContent({
focus: false,
history: cause !== 'mouse',
params: {
type,
query: textSource.text(),
wildcards: 'off'
},
state: {
sentence,
url: window.location.href
},
content: {
definitions
}
});
}
_onSearchInput() { _onSearchInput() {
this._updateSearchButton(); this._updateSearchButton();
@ -243,7 +219,7 @@ class DisplaySearch extends Display {
} }
_onSearchQueryUpdated(query, animate) { _onSearchQueryUpdated(query, animate) {
this.setContent({ const details = {
focus: false, focus: false,
history: false, history: false,
params: { params: {
@ -258,7 +234,8 @@ class DisplaySearch extends Display {
definitions: null, definitions: null,
animate animate
} }
}); };
this.setContent(details);
} }
_onWanakanaEnableChange(e) { _onWanakanaEnableChange(e) {
@ -321,10 +298,8 @@ class DisplaySearch extends Display {
// NOP // NOP
} }
} }
if (this._queryText === interpretedQuery) { return; }
this._queryText = interpretedQuery;
this._query.value = interpretedQuery; this._query.value = interpretedQuery;
this._queryParser.setText(interpretedQuery); this.setQueryParserText(interpretedQuery);
} }
_setIntroVisible(visible, animate) { _setIntroVisible(visible, animate) {

View File

@ -21,6 +21,11 @@
<button class="action-button action-next" data-icon="source-term" title="Next term (Alt + F)"></button> <button class="action-button action-next" data-icon="source-term" title="Next term (Alt + F)"></button>
</div></div><div class="navigation-header-spacer"></div> </div></div><div class="navigation-header-spacer"></div>
<div class="scan-disable" hidden>
<div id="query-parser-select-container" class="input-group"></div>
<div id="query-parser-content"></div>
</div>
<div id="definitions"></div> <div id="definitions"></div>
<div id="no-results" hidden> <div id="no-results" hidden>
@ -57,8 +62,11 @@
<script src="/mixed/js/frame-endpoint.js"></script> <script src="/mixed/js/frame-endpoint.js"></script>
<script src="/mixed/js/media-loader.js"></script> <script src="/mixed/js/media-loader.js"></script>
<script src="/mixed/js/scroll.js"></script> <script src="/mixed/js/scroll.js"></script>
<script src="/mixed/js/text-scanner.js"></script>
<script src="/mixed/js/template-handler.js"></script> <script src="/mixed/js/template-handler.js"></script>
<script src="/bg/js/query-parser-generator.js"></script>
<script src="/bg/js/query-parser.js"></script>
<script src="/fg/js/float.js"></script> <script src="/fg/js/float.js"></script>
<script src="/fg/js/float-main.js"></script> <script src="/fg/js/float-main.js"></script>

View File

@ -23,6 +23,7 @@
* Frontend * Frontend
* MediaLoader * MediaLoader
* PopupFactory * PopupFactory
* QueryParser
* WindowScroll * WindowScroll
* api * api
* docRangeFromPoint * docRangeFromPoint
@ -68,6 +69,11 @@ class Display extends EventDispatcher {
this._historyChangeIgnore = false; this._historyChangeIgnore = false;
this._historyHasChanged = false; this._historyHasChanged = false;
this._navigationHeader = document.querySelector('#navigation-header'); this._navigationHeader = document.querySelector('#navigation-header');
this._fullQuery = '';
this._queryParser = new QueryParser({
getOptionsContext: this.getOptionsContext.bind(this),
setSpinnerVisible: this.setSpinnerVisible.bind(this)
});
this.registerActions([ this.registerActions([
['close', () => { this.onEscape(); }], ['close', () => { this.onEscape(); }],
@ -120,8 +126,10 @@ class Display extends EventDispatcher {
async prepare() { async prepare() {
this._setInteractive(true); this._setInteractive(true);
await this._displayGenerator.prepare(); await this._displayGenerator.prepare();
await this._queryParser.prepare();
this._history.prepare(); this._history.prepare();
this._history.on('stateChanged', this._onStateChanged.bind(this)); this._history.on('stateChanged', this._onStateChanged.bind(this));
this._queryParser.on('searched', this._onQueryParserSearch.bind(this));
yomichan.on('extensionUnloaded', this._onExtensionUnloaded.bind(this)); yomichan.on('extensionUnloaded', this._onExtensionUnloaded.bind(this));
api.crossFrame.registerHandlers([ api.crossFrame.registerHandlers([
['popupMessage', {async: 'dynamic', handler: this._onMessage.bind(this)}] ['popupMessage', {async: 'dynamic', handler: this._onMessage.bind(this)}]
@ -188,6 +196,7 @@ class Display extends EventDispatcher {
this._updateDocumentOptions(this._options); this._updateDocumentOptions(this._options);
this._updateTheme(this._options.general.popupTheme); this._updateTheme(this._options.general.popupTheme);
this.setCustomCss(this._options.general.customPopupCss); this.setCustomCss(this._options.general.customPopupCss);
this._queryParser.setOptions(this._options);
} }
addMultipleEventListeners(selector, type, listener, options) { addMultipleEventListeners(selector, type, listener, options) {
@ -313,6 +322,12 @@ class Display extends EventDispatcher {
return data; return data;
} }
setQueryParserText(text) {
if (this._fullQuery === text) { return; }
this._fullQuery = text;
this._queryParser.setText(text);
}
// Message handlers // Message handlers
_onMessage(data) { _onMessage(data) {
@ -427,14 +442,32 @@ class Display extends EventDispatcher {
} }
} }
_onQueryParserSearch({type, definitions, sentence, cause, textSource}) {
const query = textSource.text();
const details = {
focus: false,
history: cause !== 'mouse',
params: this._createSearchParams(type, query, false),
state: {
sentence,
url: window.location.href
},
content: {
definitions
}
};
this.setContent(details);
}
_onExtensionUnloaded() { _onExtensionUnloaded() {
this.setContent({ const details = {
focus: false, focus: false,
history: false, history: false,
params: {type: 'unloaded'}, params: {type: 'unloaded'},
state: {}, state: {},
content: {} content: {}
}); };
this.setContent(details);
} }
_onSourceTermView(e) { _onSourceTermView(e) {
@ -462,14 +495,10 @@ class Display extends EventDispatcher {
const query = link.textContent; const query = link.textContent;
const definitions = await api.kanjiFind(query, this.getOptionsContext()); const definitions = await api.kanjiFind(query, this.getOptionsContext());
this.setContent({ const details = {
focus: false, focus: false,
history: true, history: true,
params: { params: this._createSearchParams('kanji', query, false),
type: 'kanji',
query,
wildcards: 'off'
},
state: { state: {
focusEntry: 0, focusEntry: 0,
sentence: state.sentence, sentence: state.sentence,
@ -478,7 +507,8 @@ class Display extends EventDispatcher {
content: { content: {
definitions definitions
} }
}); };
this.setContent(details);
} catch (error) { } catch (error) {
this.onError(error); this.onError(error);
} }
@ -520,14 +550,11 @@ class Display extends EventDispatcher {
state.scrollY = this._windowScroll.y; state.scrollY = this._windowScroll.y;
this._historyStateUpdate(state); this._historyStateUpdate(state);
this.setContent({ const query = textSource.text();
const details = {
focus: false, focus: false,
history: true, history: true,
params: { params: this._createSearchParams('terms', query, false),
type: 'terms',
query: textSource.text(),
wildcards: 'off'
},
state: { state: {
focusEntry: 0, focusEntry: 0,
sentence, sentence,
@ -536,7 +563,8 @@ class Display extends EventDispatcher {
content: { content: {
definitions definitions
} }
}); };
this.setContent(details);
} catch (error) { } catch (error) {
this.onError(error); this.onError(error);
} }
@ -1136,4 +1164,19 @@ class Display extends EventDispatcher {
this._historyChangeIgnore = historyChangeIgnorePre; this._historyChangeIgnore = historyChangeIgnorePre;
} }
} }
_createSearchParams(type, query, wildcards) {
const params = {};
if (query.length < this._fullQuery.length) {
params.full = this._fullQuery;
}
params.query = query;
if (typeof type === 'string') {
params.type = type;
}
if (!wildcards) {
params.wildcards = 'off';
}
return params;
}
} }