diff options
Diffstat (limited to 'ext/mixed')
-rw-r--r-- | ext/mixed/js/display-context.js | 55 | ||||
-rw-r--r-- | ext/mixed/js/display.js | 126 |
2 files changed, 118 insertions, 63 deletions
diff --git a/ext/mixed/js/display-context.js b/ext/mixed/js/display-context.js new file mode 100644 index 00000000..4b399881 --- /dev/null +++ b/ext/mixed/js/display-context.js @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2019 Alex Yatskov <alex@foosoft.net> + * Author: Alex Yatskov <alex@foosoft.net> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +class DisplayContext { + constructor(type, definitions, context) { + this.type = type; + this.definitions = definitions; + this.context = context; + } + + get(key) { + return this.context[key]; + } + + set(key, value) { + this.context[key] = value; + } + + update(data) { + Object.assign(this.context, data); + } + + get previous() { + return this.context.previous; + } + + get next() { + return this.context.next; + } + + static push(self, type, definitions, context) { + const newContext = new DisplayContext(type, definitions, context); + if (self !== null) { + newContext.update({previous: self}); + self.update({next: newContext}); + } + return newContext; + } +} diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 4d32377f..ab50aac0 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -64,27 +64,17 @@ class Display { try { e.preventDefault(); if (!this.context) { return; } - this.context.details.context.next = null; const link = e.target; - const {type, details} = this.context; + this.context.update({ + index: this.entryIndexFind(link), + scroll: this.windowScroll.y + }); const context = { - source: { - type, - details: { - definitions: this.definitions, - context: Object.assign({}, details.context, { - index: this.entryIndexFind(link), - scroll: this.windowScroll.y - }) - } - }, - sentence: details.context.sentence, - url: details.context.url + sentence: this.context.get('sentence'), + url: this.context.get('url') }; - this.windowScroll.toY(0); - const definitions = await apiKanjiFind(link.textContent, this.getOptionsContext()); this.setContentKanji(definitions, context); } catch (error) { @@ -111,7 +101,7 @@ class Display { async onTermLookup(e, {disableScroll, selectText, disableHistory}={}) { try { if (!this.context) { return; } - this.context.details.context.next = null; + const termLookupResults = await this.termLookup(e); if (!termLookupResults) { return; } const {textSource, definitions} = termLookupResults; @@ -119,29 +109,29 @@ class Display { const scannedElement = e.target; const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); - const {type, details} = this.context; const context = { - source: disableHistory ? details.context.source : { - type, - details: { - definitions: this.definitions, - context: Object.assign({}, details.context, { - index: this.entryIndexFind(scannedElement), - scroll: this.windowScroll.y - }) - } - }, disableScroll, + disableHistory, sentence, - url: details.context.url + url: this.context.get('url') }; + if (disableHistory) { + Object.assign(context, { + previous: this.context.previous, + next: this.context.next + }); + } else { + this.context.update({ + index: this.entryIndexFind(scannedElement), + scroll: this.windowScroll.y + }); + Object.assign(context, { + previous: this.context + }); + } this.setContentTerms(definitions, context); - if (!disableScroll) { - this.windowScroll.toY(0); - } - if (selectText) { textSource.select(); } @@ -354,16 +344,18 @@ class Display { } this.definitions = definitions; - this.context = { - type: 'terms', - details: {definitions, context} - }; + if (context.disableHistory) { + delete context.disableHistory; + this.context = new DisplayContext('terms', definitions, context); + } else { + this.context = DisplayContext.push(this.context, 'terms', definitions, context); + } const sequence = ++this.sequence; const params = { definitions, - source: context.source, - next: context.next, + source: this.context.previous, + next: this.context.next, addable: options.anki.enable, grouped: options.general.resultOutputMode === 'group', merged: options.general.resultOutputMode === 'merge', @@ -383,6 +375,7 @@ class Display { if (!disableScroll) { this.entryScrollIntoView(index || 0, scroll); } else { + delete context.disableScroll; this.entrySetCurrent(index || 0); } @@ -412,16 +405,18 @@ class Display { } this.definitions = definitions; - this.context = { - type: 'kanji', - details: {definitions, context} - }; + if (context.disableHistory) { + delete context.disableHistory; + this.context = new DisplayContext('kanji', definitions, context); + } else { + this.context = DisplayContext.push(this.context, 'kanji', definitions, context); + } const sequence = ++this.sequence; const params = { definitions, - source: context.source, - next: context.next, + source: this.context.previous, + next: this.context.next, addable: options.anki.enable, debug: options.general.debugInfo }; @@ -531,28 +526,33 @@ class Display { } sourceTermView() { - if (!this.context || !this.context.details.context.source) { return; } - const {type, details} = this.context; - const sourceContext = details.context.source; - sourceContext.details.context.next = { - type, - details: { - definitions: this.definitions, - context: Object.assign({}, details.context, { - index: this.index, - scroll: this.windowScroll.y - }) - } + if (!this.context || !this.context.previous) { return; } + this.context.update({ + index: this.index, + scroll: this.windowScroll.y + }); + const previousContext = this.context.previous; + previousContext.set('disableHistory', true); + const details = { + definitions: previousContext.definitions, + context: previousContext.context }; - this.setContent(sourceContext.type, sourceContext.details); + this.setContent(previousContext.type, details); } nextTermView() { - if (!this.context.details.context.next) { return; } - this.context.details.context.index = this.index; - this.context.details.context.scroll = this.windowScroll.y; - const {type, details} = this.context.details.context.next; - this.setContent(type, details); + if (!this.context || !this.context.next) { return; } + this.context.update({ + index: this.index, + scroll: this.windowScroll.y + }); + const nextContext = this.context.next; + nextContext.set('disableHistory', true); + const details = { + definitions: nextContext.definitions, + context: nextContext.context + }; + this.setContent(nextContext.type, details); } noteTryAdd(mode) { |