aboutsummaryrefslogtreecommitdiff
path: root/ext/js/display/display.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-09-27 19:07:28 -0400
committerGitHub <noreply@github.com>2021-09-27 19:07:28 -0400
commitb0f6c41f5dc7f498f2948f846dd273bcb1bc1f0b (patch)
tree09f17dd0c6d549528d680c6f982efeb55610858d /ext/js/display/display.js
parentb784e5b11a596a456eb8879d394fde64bc63aaef (diff)
Search query offset value (#1968)
* Add type property to TextSource* classes * Use type property rather than instanceof * Expose a sentence offset value * Use offset added to URL * Improve fallback sentence for Anki note context
Diffstat (limited to 'ext/js/display/display.js')
-rw-r--r--ext/js/display/display.js46
1 files changed, 33 insertions, 13 deletions
diff --git a/ext/js/display/display.js b/ext/js/display/display.js
index 0509e22a..0e82af54 100644
--- a/ext/js/display/display.js
+++ b/ext/js/display/display.js
@@ -73,6 +73,7 @@ class Display extends EventDispatcher {
this._titleMaxLength = 1000;
this._query = '';
this._fullQuery = '';
+ this._queryOffset = 0;
this._documentUtil = new DocumentUtil();
this._progressIndicator = document.querySelector('#progress-indicator');
this._progressIndicatorTimer = null;
@@ -208,6 +209,10 @@ class Display extends EventDispatcher {
return this._fullQuery;
}
+ get queryOffset() {
+ return this._queryOffset;
+ }
+
get frameVisible() {
return this._frameVisible;
}
@@ -432,7 +437,7 @@ class Display extends EventDispatcher {
const details = {
focus: false,
historyMode: 'clear',
- params: this._createSearchParams(type, query, false),
+ params: this._createSearchParams(type, query, false, this._queryOffset),
state,
content: {
dictionaryEntries: null,
@@ -598,9 +603,14 @@ class Display extends EventDispatcher {
const isTerms = (type === 'terms');
let queryFull = urlSearchParams.get('full');
queryFull = (queryFull !== null ? queryFull : query);
+ let queryOffset = urlSearchParams.get('offset');
+ if (queryOffset !== null) {
+ queryOffset = Number.parseInt(queryOffset, 10);
+ if (!Number.isFinite(queryOffset)) { queryOffset = null; }
+ }
const wildcardsEnabled = (urlSearchParams.get('wildcards') !== 'off');
const lookup = (urlSearchParams.get('lookup') !== 'false');
- await this._setContentTermsOrKanji(token, isTerms, query, queryFull, lookup, wildcardsEnabled, eventArgs);
+ await this._setContentTermsOrKanji(token, isTerms, query, queryFull, queryOffset, lookup, wildcardsEnabled, eventArgs);
}
break;
case 'unloaded':
@@ -633,7 +643,7 @@ class Display extends EventDispatcher {
}
}
- _onQueryParserSearch({type, dictionaryEntries, sentence, inputInfo: {eventType}, textSource, optionsContext}) {
+ _onQueryParserSearch({type, dictionaryEntries, sentence, inputInfo: {eventType}, textSource, optionsContext, sentenceOffset}) {
const query = textSource.text();
const historyState = this._history.state;
const historyMode = (
@@ -644,7 +654,7 @@ class Display extends EventDispatcher {
const details = {
focus: false,
historyMode,
- params: this._createSearchParams(type, query, false),
+ params: this._createSearchParams(type, query, false, sentenceOffset),
state: {
sentence,
optionsContext,
@@ -724,7 +734,7 @@ class Display extends EventDispatcher {
const details = {
focus: false,
historyMode: 'new',
- params: this._createSearchParams('kanji', query, false),
+ params: this._createSearchParams('kanji', query, false, null),
state: {
focusEntry: 0,
optionsContext,
@@ -887,7 +897,7 @@ class Display extends EventDispatcher {
}
}
- async _setContentTermsOrKanji(token, isTerms, query, queryFull, lookup, wildcardsEnabled, eventArgs) {
+ async _setContentTermsOrKanji(token, isTerms, query, queryFull, queryOffset, lookup, wildcardsEnabled, eventArgs) {
let {state, content} = this._history;
let changeHistory = false;
if (!isObject(content)) {
@@ -912,7 +922,11 @@ class Display extends EventDispatcher {
changeHistory = true;
}
- this._setFullQuery(queryFull);
+ if (queryOffset !== null) {
+ queryOffset = Math.max(0, Math.min(queryFull.length - query.length, queryOffset));
+ }
+
+ this._setFullQuery(queryFull, queryOffset);
this._setTitleText(query);
let {dictionaryEntries} = content;
@@ -1015,13 +1029,13 @@ class Display extends EventDispatcher {
this._updateNavigation(false, false);
this._setNoContentVisible(false);
this._setTitleText('');
- this._setFullQuery('');
+ this._setFullQuery('', 0);
}
_clearContent() {
this._container.textContent = '';
this._setTitleText('');
- this._setFullQuery('');
+ this._setFullQuery('', 0);
}
_setNoContentVisible(visible) {
@@ -1032,8 +1046,9 @@ class Display extends EventDispatcher {
}
}
- _setFullQuery(text) {
+ _setFullQuery(text, offset) {
this._fullQuery = text;
+ this._queryOffset = offset;
this._updateQueryParser();
}
@@ -1200,12 +1215,17 @@ class Display extends EventDispatcher {
}
}
- _createSearchParams(type, query, wildcards) {
+ _createSearchParams(type, query, wildcards, sentenceOffset) {
const params = {};
- if (query.length < this._fullQuery.length) {
- params.full = this._fullQuery;
+ const fullQuery = this._fullQuery;
+ const includeFull = (query.length < fullQuery.length);
+ if (includeFull) {
+ params.full = fullQuery;
}
params.query = query;
+ if (includeFull && sentenceOffset !== null) {
+ params.offset = `${sentenceOffset}`;
+ }
if (typeof type === 'string') {
params.type = type;
}