Fix history replacement issue (#1917)

* Remove unused "history" field of event details

* Change setContent history parameter to historyMode
This commit is contained in:
toasted-nutbread 2021-08-30 21:55:08 -04:00 committed by GitHub
parent f565cc5a47
commit 3e7f3af63c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 21 deletions

View File

@ -515,7 +515,7 @@ class Frontend {
const {url} = optionsContext; const {url} = optionsContext;
const details = { const details = {
focus, focus,
history: false, historyMode: 'clear',
params: { params: {
type, type,
query, query,

View File

@ -112,7 +112,7 @@ class DisplayHistory extends EventDispatcher {
} }
_triggerStateChanged(synthetic) { _triggerStateChanged(synthetic) {
this.trigger('stateChanged', {history: this, synthetic}); this.trigger('stateChanged', {synthetic});
} }
_updateHistoryFromCurrent(replace) { _updateHistoryFromCurrent(replace) {

View File

@ -347,7 +347,8 @@ class Display extends EventDispatcher {
} }
setContent(details) { setContent(details) {
const {focus, history, params, state, content} = details; const {focus, params, state, content} = details;
const historyMode = this._historyHasChanged ? details.historyMode : 'clear';
if (focus) { if (focus) {
window.focus(); window.focus();
@ -359,12 +360,18 @@ class Display extends EventDispatcher {
} }
const url = `${location.protocol}//${location.host}${location.pathname}?${urlSearchParams.toString()}`; const url = `${location.protocol}//${location.host}${location.pathname}?${urlSearchParams.toString()}`;
if (history && this._historyHasChanged) { switch (historyMode) {
this._updateHistoryState(); case 'clear':
this._history.pushState(state, content, url);
} else {
this._history.clear(); this._history.clear();
this._history.replaceState(state, content, url); this._history.replaceState(state, content, url);
break;
case 'overwrite':
this._history.replaceState(state, content, url);
break;
default: // 'new'
this._updateHistoryState();
this._history.pushState(state, content, url);
break;
} }
} }
@ -430,7 +437,7 @@ class Display extends EventDispatcher {
); );
const details = { const details = {
focus: false, focus: false,
history: false, historyMode: 'clear',
params: this._createSearchParams(type, query, false), params: this._createSearchParams(type, query, false),
state, state,
content: { content: {
@ -638,14 +645,14 @@ class Display extends EventDispatcher {
_onQueryParserSearch({type, dictionaryEntries, sentence, inputInfo: {eventType}, textSource, optionsContext}) { _onQueryParserSearch({type, dictionaryEntries, sentence, inputInfo: {eventType}, textSource, optionsContext}) {
const query = textSource.text(); const query = textSource.text();
const historyState = this._history.state; const historyState = this._history.state;
const history = ( const historyMode = (
eventType === 'click' || eventType === 'click' ||
!isObject(historyState) || !isObject(historyState) ||
historyState.cause !== 'queryParser' historyState.cause !== 'queryParser'
); ) ? 'new' : 'overwrite';
const details = { const details = {
focus: false, focus: false,
history, historyMode,
params: this._createSearchParams(type, query, false), params: this._createSearchParams(type, query, false),
state: { state: {
sentence, sentence,
@ -665,7 +672,7 @@ class Display extends EventDispatcher {
if (this._contentType === type) { return; } if (this._contentType === type) { return; }
const details = { const details = {
focus: false, focus: false,
history: false, historyMode: 'clear',
params: {type}, params: {type},
state: {}, state: {},
content: { content: {
@ -725,7 +732,7 @@ class Display extends EventDispatcher {
const dictionaryEntries = await yomichan.api.kanjiFind(query, optionsContext); const dictionaryEntries = await yomichan.api.kanjiFind(query, optionsContext);
const details = { const details = {
focus: false, focus: false,
history: true, historyMode: 'new',
params: this._createSearchParams('kanji', query, false), params: this._createSearchParams('kanji', query, false),
state: { state: {
focusEntry: 0, focusEntry: 0,
@ -1448,7 +1455,7 @@ class Display extends EventDispatcher {
const documentTitle = document.title; const documentTitle = document.title;
const details = { const details = {
focus: false, focus: false,
history: true, historyMode: 'new',
params: { params: {
type, type,
query, query,

View File

@ -185,12 +185,12 @@ class SearchDisplayController {
e.preventDefault(); e.preventDefault();
e.stopImmediatePropagation(); e.stopImmediatePropagation();
this._display.blurElement(e.currentTarget); this._display.blurElement(e.currentTarget);
this._search(true, true, true, null); this._search(true, 'new', true, null);
} }
_onSearch(e) { _onSearch(e) {
e.preventDefault(); e.preventDefault();
this._search(true, true, true, null); this._search(true, 'new', true, null);
} }
_onCopy() { _onCopy() {
@ -205,7 +205,7 @@ class SearchDisplayController {
} }
this._queryInput.value = text; this._queryInput.value = text;
this._updateSearchHeight(true); this._updateSearchHeight(true);
this._search(animate, false, autoSearchContent, ['clipboard']); this._search(animate, 'clear', autoSearchContent, ['clipboard']);
} }
_onWanakanaEnableChange(e) { _onWanakanaEnableChange(e) {
@ -362,7 +362,7 @@ class SearchDisplayController {
}); });
} }
_search(animate, history, lookup, flags) { _search(animate, historyMode, lookup, flags) {
const query = this._queryInput.value; const query = this._queryInput.value;
const depth = this._display.depth; const depth = this._display.depth;
const url = window.location.href; const url = window.location.href;
@ -373,7 +373,7 @@ class SearchDisplayController {
} }
const details = { const details = {
focus: false, focus: false,
history, historyMode,
params: { params: {
query query
}, },