2017-03-05 01:30:10 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2017-2020 Yomichan Authors
|
2017-03-05 01:30:10 +00:00
|
|
|
*
|
|
|
|
* 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
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-03-05 01:30:10 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
|
|
|
* AudioSystem
|
|
|
|
* DOM
|
|
|
|
* DisplayContext
|
|
|
|
* DisplayGenerator
|
2020-04-11 18:23:49 +00:00
|
|
|
* MediaLoader
|
2020-03-11 02:30:36 +00:00
|
|
|
* WindowScroll
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
2020-03-11 02:30:36 +00:00
|
|
|
* docRangeFromPoint
|
|
|
|
* docSentenceExtract
|
|
|
|
*/
|
2017-03-05 01:30:10 +00:00
|
|
|
|
|
|
|
class Display {
|
|
|
|
constructor(spinner, container) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._spinner = spinner;
|
|
|
|
this._container = container;
|
|
|
|
this._definitions = [];
|
|
|
|
this._optionsContext = null;
|
|
|
|
this._options = null;
|
|
|
|
this._context = null;
|
|
|
|
this._index = 0;
|
|
|
|
this._audioPlaying = null;
|
|
|
|
this._audioFallback = null;
|
|
|
|
this._audioSystem = new AudioSystem({
|
2020-04-10 17:44:31 +00:00
|
|
|
audioUriBuilder: {
|
2020-04-14 22:22:51 +00:00
|
|
|
getUri: async (definition, source, details) => {
|
2020-05-24 17:30:40 +00:00
|
|
|
return await api.audioGetUri(definition, source, details);
|
2020-04-10 17:44:31 +00:00
|
|
|
}
|
2020-04-10 20:12:55 +00:00
|
|
|
},
|
|
|
|
useCache: true
|
2020-04-10 17:44:31 +00:00
|
|
|
});
|
2020-07-03 16:02:21 +00:00
|
|
|
this._styleNode = null;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
|
|
|
this._persistentEventListeners = new EventListenerCollection();
|
|
|
|
this._interactive = false;
|
|
|
|
this._eventListenersActive = false;
|
|
|
|
this._clickScanPrevent = false;
|
|
|
|
this._setContentToken = null;
|
|
|
|
this._mediaLoader = new MediaLoader();
|
|
|
|
this._displayGenerator = new DisplayGenerator({mediaLoader: this._mediaLoader});
|
|
|
|
this._windowScroll = new WindowScroll();
|
2020-07-09 00:02:20 +00:00
|
|
|
this._hotkeys = new Map();
|
|
|
|
this._actions = new Map();
|
|
|
|
|
|
|
|
this.registerActions([
|
|
|
|
['close', () => { this.onEscape(); }],
|
|
|
|
['next-entry', () => { this._entryScrollIntoView(this._index + 1, null, true); }],
|
|
|
|
['next-entry-x3', () => { this._entryScrollIntoView(this._index + 3, null, true); }],
|
|
|
|
['previous-entry', () => { this._entryScrollIntoView(this._index - 1, null, true); }],
|
|
|
|
['previous-entry-x3', () => { this._entryScrollIntoView(this._index - 3, null, true); }],
|
|
|
|
['last-entry', () => { this._entryScrollIntoView(this._definitions.length - 1, null, true); }],
|
|
|
|
['first-entry', () => { this._entryScrollIntoView(0, null, true); }],
|
|
|
|
['history-backward', () => { this._sourceTermView(); }],
|
|
|
|
['history-forward', () => { this._nextTermView(); }],
|
|
|
|
['add-note-kanji', () => { this._noteTryAdd('kanji'); }],
|
|
|
|
['add-note-term-kanji', () => { this._noteTryAdd('term-kanji'); }],
|
|
|
|
['add-note-term-kana', () => { this._noteTryAdd('term-kana'); }],
|
|
|
|
['view-note', () => { this._noteTryView(); }],
|
|
|
|
['play-audio', () => { this._playAudioCurrent(); }]
|
|
|
|
]);
|
|
|
|
this.registerHotkeys([
|
|
|
|
{key: 'Escape', modifiers: [], action: 'close'},
|
|
|
|
{key: 'PageUp', modifiers: ['alt'], action: 'previous-entry-x3'},
|
|
|
|
{key: 'PageDown', modifiers: ['alt'], action: 'next-entry-x3'},
|
|
|
|
{key: 'End', modifiers: ['alt'], action: 'last-entry'},
|
|
|
|
{key: 'Home', modifiers: ['alt'], action: 'first-entry'},
|
|
|
|
{key: 'ArrowUp', modifiers: ['alt'], action: 'previous-entry'},
|
|
|
|
{key: 'ArrowDown', modifiers: ['alt'], action: 'next-entry'},
|
|
|
|
{key: 'B', modifiers: ['alt'], action: 'history-backward'},
|
|
|
|
{key: 'F', modifiers: ['alt'], action: 'history-forward'},
|
|
|
|
{key: 'K', modifiers: ['alt'], action: 'add-note-kanji'},
|
|
|
|
{key: 'E', modifiers: ['alt'], action: 'add-note-term-kanji'},
|
|
|
|
{key: 'R', modifiers: ['alt'], action: 'add-note-term-kana'},
|
|
|
|
{key: 'P', modifiers: ['alt'], action: 'play-audio'},
|
|
|
|
{key: 'V', modifiers: ['alt'], action: 'view-note'}
|
2020-02-27 00:48:30 +00:00
|
|
|
]);
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 21:23:08 +00:00
|
|
|
async prepare() {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setInteractive(true);
|
2020-06-28 15:26:43 +00:00
|
|
|
await yomichan.ready();
|
2020-07-03 16:02:21 +00:00
|
|
|
await this._displayGenerator.prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
onError(error) {
|
2020-07-03 16:20:22 +00:00
|
|
|
if (yomichan.isExtensionUnloaded) {
|
|
|
|
this.setContent('extensionUnloaded');
|
2020-07-03 16:02:21 +00:00
|
|
|
} else {
|
|
|
|
yomichan.logError(error);
|
|
|
|
}
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 22:10:37 +00:00
|
|
|
onEscape() {
|
2019-10-08 00:46:02 +00:00
|
|
|
throw new Error('Override me');
|
2017-03-25 22:59:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
onKeyDown(e) {
|
|
|
|
const key = DOM.getKeyFromEvent(e);
|
2020-07-09 00:02:20 +00:00
|
|
|
const handlers = this._hotkeys.get(key);
|
|
|
|
if (typeof handlers === 'undefined') { return false; }
|
|
|
|
|
|
|
|
const eventModifiers = DOM.getActiveModifiers(e);
|
|
|
|
for (const {modifiers, action} of handlers) {
|
|
|
|
if (getSetDifference(modifiers, eventModifiers).size !== 0) { continue; }
|
|
|
|
|
|
|
|
const actionHandler = this._actions.get(action);
|
|
|
|
if (typeof actionHandler === 'undefined') { continue; }
|
|
|
|
|
|
|
|
const result = actionHandler(e);
|
|
|
|
if (result !== false) {
|
2020-07-03 19:58:29 +00:00
|
|
|
e.preventDefault();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getOptions() {
|
|
|
|
return this._options;
|
|
|
|
}
|
|
|
|
|
|
|
|
getOptionsContext() {
|
|
|
|
return this._optionsContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
setOptionsContext(optionsContext) {
|
|
|
|
this._optionsContext = optionsContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateOptions() {
|
|
|
|
this._options = await api.optionsGet(this.getOptionsContext());
|
|
|
|
this._updateDocumentOptions(this._options);
|
|
|
|
this._updateTheme(this._options.general.popupTheme);
|
|
|
|
this.setCustomCss(this._options.general.customPopupCss);
|
|
|
|
}
|
|
|
|
|
|
|
|
addMultipleEventListeners(selector, type, listener, options) {
|
|
|
|
for (const node of this._container.querySelectorAll(selector)) {
|
|
|
|
this._eventListeners.addEventListener(node, type, listener, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
autoPlayAudio() {
|
|
|
|
if (this._definitions.length === 0) { return; }
|
|
|
|
|
|
|
|
this._audioPlay(this._definitions[0], this._getFirstExpressionIndex(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setContent(type, details) {
|
|
|
|
const token = {}; // Unique identifier token
|
|
|
|
this._setContentToken = token;
|
|
|
|
try {
|
|
|
|
this._mediaLoader.unloadAll();
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'terms':
|
|
|
|
await this._setContentTerms(details.definitions, details.context, token);
|
|
|
|
break;
|
|
|
|
case 'kanji':
|
|
|
|
await this._setContentKanji(details.definitions, details.context, token);
|
|
|
|
break;
|
|
|
|
case 'extensionUnloaded':
|
|
|
|
this._setContentExtensionUnloaded();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
} finally {
|
|
|
|
if (this._setContentToken === token) {
|
|
|
|
this._setContentToken = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearContent() {
|
|
|
|
this._container.textContent = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
setCustomCss(css) {
|
|
|
|
if (this._styleNode === null) {
|
|
|
|
if (css.length === 0) { return; }
|
|
|
|
this._styleNode = document.createElement('style');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._styleNode.textContent = css;
|
|
|
|
|
|
|
|
const parent = document.head;
|
|
|
|
if (this._styleNode.parentNode !== parent) {
|
|
|
|
parent.appendChild(this._styleNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getDocumentTitle() {
|
|
|
|
return document.title;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSpinnerVisible(visible) {
|
|
|
|
if (this._spinner !== null) {
|
|
|
|
this._spinner.hidden = !visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 00:02:20 +00:00
|
|
|
registerActions(actions) {
|
|
|
|
for (const [name, handler] of actions) {
|
|
|
|
this._actions.set(name, handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registerHotkeys(hotkeys) {
|
|
|
|
for (const {key, modifiers, action} of hotkeys) {
|
|
|
|
let handlers = this._hotkeys.get(key);
|
|
|
|
if (typeof handlers === 'undefined') {
|
|
|
|
handlers = [];
|
|
|
|
this._hotkeys.set(key, handlers);
|
|
|
|
}
|
|
|
|
handlers.push({modifiers: new Set(modifiers), action});
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onSourceTermView(e) {
|
2017-03-05 01:30:10 +00:00
|
|
|
e.preventDefault();
|
2020-07-03 16:02:21 +00:00
|
|
|
this._sourceTermView();
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
2017-03-05 01:30:10 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onNextTermView(e) {
|
2019-11-28 13:20:18 +00:00
|
|
|
e.preventDefault();
|
2020-07-03 16:02:21 +00:00
|
|
|
this._nextTermView();
|
2019-11-28 13:20:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _onKanjiLookup(e) {
|
2017-08-13 23:11:51 +00:00
|
|
|
try {
|
|
|
|
e.preventDefault();
|
2020-07-03 16:02:21 +00:00
|
|
|
if (!this._context) { return; }
|
2017-03-19 22:45:30 +00:00
|
|
|
|
2019-09-15 18:06:27 +00:00
|
|
|
const link = e.target;
|
2020-07-03 16:02:21 +00:00
|
|
|
this._context.update({
|
|
|
|
index: this._entryIndexFind(link),
|
|
|
|
scroll: this._windowScroll.y
|
2019-12-01 03:38:23 +00:00
|
|
|
});
|
2017-08-13 23:11:51 +00:00
|
|
|
const context = {
|
2020-07-03 16:02:21 +00:00
|
|
|
sentence: this._context.get('sentence'),
|
|
|
|
url: this._context.get('url')
|
2017-08-13 23:11:51 +00:00
|
|
|
};
|
|
|
|
|
2020-05-24 17:30:40 +00:00
|
|
|
const definitions = await api.kanjiFind(link.textContent, this.getOptionsContext());
|
2019-12-27 22:42:36 +00:00
|
|
|
this.setContent('kanji', {definitions, context});
|
2019-11-10 18:55:37 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.onError(error);
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onGlossaryMouseDown(e) {
|
2019-11-26 23:52:05 +00:00
|
|
|
if (DOM.isMouseButtonPressed(e, 'primary')) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._clickScanPrevent = false;
|
2019-11-03 16:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onGlossaryMouseMove() {
|
|
|
|
this._clickScanPrevent = true;
|
2019-11-03 16:56:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onGlossaryMouseUp(e) {
|
|
|
|
if (!this._clickScanPrevent && DOM.isMouseButtonPressed(e, 'primary')) {
|
|
|
|
this._onTermLookup(e);
|
2019-11-03 16:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _onTermLookup(e, {disableScroll, selectText, disableHistory}={}) {
|
2019-10-30 01:58:24 +00:00
|
|
|
try {
|
2020-07-03 16:02:21 +00:00
|
|
|
if (!this._context) { return; }
|
2019-12-01 03:38:23 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const termLookupResults = await this._termLookup(e);
|
2019-11-26 17:29:49 +00:00
|
|
|
if (!termLookupResults) { return; }
|
2019-10-30 01:58:24 +00:00
|
|
|
const {textSource, definitions} = termLookupResults;
|
|
|
|
|
|
|
|
const scannedElement = e.target;
|
2020-07-03 16:02:21 +00:00
|
|
|
const sentenceExtent = this._options.anki.sentenceExt;
|
|
|
|
const layoutAwareScan = this._options.scanning.layoutAwareScan;
|
2020-06-21 20:07:51 +00:00
|
|
|
const sentence = docSentenceExtract(textSource, sentenceExtent, layoutAwareScan);
|
2019-10-30 01:58:24 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._context.update({
|
|
|
|
index: this._entryIndexFind(scannedElement),
|
|
|
|
scroll: this._windowScroll.y
|
2019-12-01 04:08:05 +00:00
|
|
|
});
|
2019-11-26 17:29:49 +00:00
|
|
|
const context = {
|
|
|
|
disableScroll,
|
2019-12-01 03:38:23 +00:00
|
|
|
disableHistory,
|
2019-11-26 17:29:49 +00:00
|
|
|
sentence,
|
2020-07-03 16:02:21 +00:00
|
|
|
url: this._context.get('url')
|
2019-11-26 17:29:49 +00:00
|
|
|
};
|
2019-12-01 03:38:23 +00:00
|
|
|
if (disableHistory) {
|
|
|
|
Object.assign(context, {
|
2020-07-03 16:02:21 +00:00
|
|
|
previous: this._context.previous,
|
|
|
|
next: this._context.next
|
2019-12-01 03:38:23 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Object.assign(context, {
|
2020-07-03 16:02:21 +00:00
|
|
|
previous: this._context
|
2019-12-01 03:38:23 +00:00
|
|
|
});
|
|
|
|
}
|
2019-10-30 01:58:24 +00:00
|
|
|
|
2019-12-27 22:42:36 +00:00
|
|
|
this.setContent('terms', {definitions, context});
|
2019-10-30 01:58:24 +00:00
|
|
|
|
|
|
|
if (selectText) {
|
|
|
|
textSource.select();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.onError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _termLookup(e) {
|
2019-08-03 12:06:28 +00:00
|
|
|
try {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const {length: scanLength, deepDomScan: deepScan, layoutAwareScan} = this._options.scanning;
|
2020-06-21 20:07:51 +00:00
|
|
|
const textSource = docRangeFromPoint(e.clientX, e.clientY, deepScan);
|
2019-08-10 17:33:31 +00:00
|
|
|
if (textSource === null) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2019-10-30 01:58:24 +00:00
|
|
|
let definitions, length;
|
2019-08-31 18:57:24 +00:00
|
|
|
try {
|
2020-06-21 20:07:51 +00:00
|
|
|
textSource.setEndOffset(scanLength, layoutAwareScan);
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2020-05-24 17:30:40 +00:00
|
|
|
({definitions, length} = await api.termsFind(textSource.text(), {}, this.getOptionsContext()));
|
2019-08-31 18:57:24 +00:00
|
|
|
if (definitions.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2020-06-21 20:07:51 +00:00
|
|
|
textSource.setEndOffset(length, layoutAwareScan);
|
2019-08-31 18:57:24 +00:00
|
|
|
} finally {
|
|
|
|
textSource.cleanup();
|
|
|
|
}
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2019-10-30 01:58:24 +00:00
|
|
|
return {textSource, definitions};
|
2019-11-10 18:55:37 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.onError(error);
|
2019-08-03 12:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onAudioPlay(e) {
|
2017-03-19 22:45:30 +00:00
|
|
|
e.preventDefault();
|
2019-09-15 18:06:27 +00:00
|
|
|
const link = e.currentTarget;
|
|
|
|
const entry = link.closest('.entry');
|
2020-07-03 16:02:21 +00:00
|
|
|
const index = this._entryIndexFind(entry);
|
|
|
|
if (index < 0 || index >= this._definitions.length) { return; }
|
2020-02-14 01:42:25 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const expressionIndex = this._indexOf(entry.querySelectorAll('.term-expression .action-play-audio'), link);
|
|
|
|
this._audioPlay(
|
|
|
|
this._definitions[index],
|
2020-01-27 21:24:34 +00:00
|
|
|
// expressionIndex is used in audioPlay to detect result output mode
|
2020-07-03 16:02:21 +00:00
|
|
|
Math.max(expressionIndex, this._options.general.resultOutputMode === 'merge' ? 0 : -1),
|
2020-02-14 01:42:25 +00:00
|
|
|
index
|
2020-01-27 21:24:34 +00:00
|
|
|
);
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onNoteAdd(e) {
|
2017-03-19 22:45:30 +00:00
|
|
|
e.preventDefault();
|
2019-09-15 18:06:27 +00:00
|
|
|
const link = e.currentTarget;
|
2020-07-03 16:02:21 +00:00
|
|
|
const index = this._entryIndexFind(link);
|
|
|
|
if (index < 0 || index >= this._definitions.length) { return; }
|
2020-02-14 01:42:25 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._noteAdd(this._definitions[index], link.dataset.mode);
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onNoteView(e) {
|
2017-07-02 01:27:49 +00:00
|
|
|
e.preventDefault();
|
2019-09-15 18:06:27 +00:00
|
|
|
const link = e.currentTarget;
|
2020-05-24 17:30:40 +00:00
|
|
|
api.noteView(link.dataset.noteId);
|
2017-07-02 01:27:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onWheel(e) {
|
2019-10-03 00:57:48 +00:00
|
|
|
if (e.altKey) {
|
2019-11-28 13:20:18 +00:00
|
|
|
if (e.deltaY !== 0) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._entryScrollIntoView(this._index + (e.deltaY > 0 ? 1 : -1), null, true);
|
2019-10-03 00:57:48 +00:00
|
|
|
e.preventDefault();
|
2017-09-25 20:47:53 +00:00
|
|
|
}
|
2019-11-28 13:24:00 +00:00
|
|
|
} else if (e.shiftKey) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._onHistoryWheel(e);
|
2019-12-07 15:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onHistoryWheel(e) {
|
2020-02-05 12:19:48 +00:00
|
|
|
if (e.altKey) { return; }
|
2019-12-07 15:41:14 +00:00
|
|
|
const delta = -e.deltaX || e.deltaY;
|
|
|
|
if (delta > 0) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._sourceTermView();
|
2019-12-07 15:41:14 +00:00
|
|
|
e.preventDefault();
|
2020-02-05 12:19:48 +00:00
|
|
|
e.stopPropagation();
|
2019-12-07 15:41:14 +00:00
|
|
|
} else if (delta < 0) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._nextTermView();
|
2019-12-07 15:41:14 +00:00
|
|
|
e.preventDefault();
|
2020-02-05 12:19:48 +00:00
|
|
|
e.stopPropagation();
|
2019-11-28 13:20:18 +00:00
|
|
|
}
|
2017-09-25 20:47:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_updateDocumentOptions(options) {
|
2019-12-25 02:45:57 +00:00
|
|
|
const data = document.documentElement.dataset;
|
|
|
|
data.ankiEnabled = `${options.anki.enable}`;
|
2020-03-01 19:45:57 +00:00
|
|
|
data.audioEnabled = `${options.audio.enabled}`;
|
2019-12-25 02:45:57 +00:00
|
|
|
data.compactGlossaries = `${options.general.compactGlossaries}`;
|
2020-01-26 19:00:19 +00:00
|
|
|
data.enableSearchTags = `${options.scanning.enableSearchTags}`;
|
2020-03-01 19:38:16 +00:00
|
|
|
data.showPitchAccentDownstepNotation = `${options.general.showPitchAccentDownstepNotation}`;
|
|
|
|
data.showPitchAccentPositionNotation = `${options.general.showPitchAccentPositionNotation}`;
|
|
|
|
data.showPitchAccentGraph = `${options.general.showPitchAccentGraph}`;
|
2019-12-25 02:45:57 +00:00
|
|
|
data.debug = `${options.general.debugInfo}`;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_updateTheme(themeName) {
|
2019-10-12 21:06:03 +00:00
|
|
|
document.documentElement.dataset.yomichanTheme = themeName;
|
2019-10-12 18:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_setInteractive(interactive) {
|
2019-10-12 03:24:27 +00:00
|
|
|
interactive = !!interactive;
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._interactive === interactive) { return; }
|
|
|
|
this._interactive = interactive;
|
2019-10-12 03:24:27 +00:00
|
|
|
|
|
|
|
if (interactive) {
|
2020-02-16 21:33:48 +00:00
|
|
|
const actionPrevious = document.querySelector('.action-previous');
|
|
|
|
const actionNext = document.querySelector('.action-next');
|
2020-02-24 06:57:48 +00:00
|
|
|
// const navigationHeader = document.querySelector('.navigation-header');
|
2020-02-16 21:33:48 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._persistentEventListeners.addEventListener(document, 'keydown', this.onKeyDown.bind(this), false);
|
|
|
|
this._persistentEventListeners.addEventListener(document, 'wheel', this._onWheel.bind(this), {passive: false});
|
2020-02-16 21:33:48 +00:00
|
|
|
if (actionPrevious !== null) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._persistentEventListeners.addEventListener(actionPrevious, 'click', this._onSourceTermView.bind(this));
|
2020-02-16 21:33:48 +00:00
|
|
|
}
|
|
|
|
if (actionNext !== null) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._persistentEventListeners.addEventListener(actionNext, 'click', this._onNextTermView.bind(this));
|
2020-02-16 21:33:48 +00:00
|
|
|
}
|
2020-02-24 06:57:48 +00:00
|
|
|
// temporarily disabled
|
|
|
|
// if (navigationHeader !== null) {
|
|
|
|
// this.persistentEventListeners.addEventListener(navigationHeader, 'wheel', this.onHistoryWheel.bind(this), {passive: false});
|
|
|
|
// }
|
2019-10-12 03:24:27 +00:00
|
|
|
} else {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._persistentEventListeners.removeAllEventListeners();
|
2019-10-12 03:24:27 +00:00
|
|
|
}
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setEventListenersActive(this._eventListenersActive);
|
2019-10-12 03:24:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_setEventListenersActive(active) {
|
|
|
|
active = !!active && this._interactive;
|
|
|
|
if (this._eventListenersActive === active) { return; }
|
|
|
|
this._eventListenersActive = active;
|
2019-10-12 03:24:27 +00:00
|
|
|
|
|
|
|
if (active) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this.addMultipleEventListeners('.action-add-note', 'click', this._onNoteAdd.bind(this));
|
|
|
|
this.addMultipleEventListeners('.action-view-note', 'click', this._onNoteView.bind(this));
|
|
|
|
this.addMultipleEventListeners('.action-play-audio', 'click', this._onAudioPlay.bind(this));
|
|
|
|
this.addMultipleEventListeners('.kanji-link', 'click', this._onKanjiLookup.bind(this));
|
|
|
|
if (this._options.scanning.enablePopupSearch) {
|
|
|
|
this.addMultipleEventListeners('.term-glossary-item, .tag', 'mouseup', this._onGlossaryMouseUp.bind(this));
|
|
|
|
this.addMultipleEventListeners('.term-glossary-item, .tag', 'mousedown', this._onGlossaryMouseDown.bind(this));
|
|
|
|
this.addMultipleEventListeners('.term-glossary-item, .tag', 'mousemove', this._onGlossaryMouseMove.bind(this));
|
2019-10-12 03:24:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._eventListeners.removeAllEventListeners();
|
2019-10-12 03:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _setContentTerms(definitions, context, token) {
|
2019-11-26 16:43:58 +00:00
|
|
|
if (!context) { throw new Error('Context expected'); }
|
2019-10-12 18:41:24 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setEventListenersActive(false);
|
2019-09-15 22:52:26 +00:00
|
|
|
|
2019-12-27 23:04:39 +00:00
|
|
|
if (context.focus !== false) {
|
|
|
|
window.focus();
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._definitions = definitions;
|
2019-12-27 23:04:39 +00:00
|
|
|
if (context.disableHistory) {
|
|
|
|
delete context.disableHistory;
|
2020-07-03 16:02:21 +00:00
|
|
|
this._context = new DisplayContext('terms', definitions, context);
|
2019-12-27 23:04:39 +00:00
|
|
|
} else {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._context = DisplayContext.push(this._context, 'terms', definitions, context);
|
2019-12-27 23:04:39 +00:00
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2019-12-27 23:04:39 +00:00
|
|
|
for (const definition of definitions) {
|
2020-07-03 16:02:21 +00:00
|
|
|
definition.cloze = this._clozeBuild(context.sentence, definition.source);
|
2019-12-27 23:04:39 +00:00
|
|
|
definition.url = context.url;
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._updateNavigation(this._context.previous, this._context.next);
|
|
|
|
this._setNoContentVisible(definitions.length === 0);
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const container = this._container;
|
2019-12-27 23:15:16 +00:00
|
|
|
container.textContent = '';
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2019-12-27 23:15:16 +00:00
|
|
|
for (let i = 0, ii = definitions.length; i < ii; ++i) {
|
|
|
|
if (i > 0) {
|
|
|
|
await promiseTimeout(1);
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._setContentToken !== token) { return; }
|
2019-12-27 23:15:16 +00:00
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const entry = this._displayGenerator.createTermEntry(definitions[i]);
|
2019-12-27 23:15:16 +00:00
|
|
|
container.appendChild(entry);
|
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2019-12-27 23:04:39 +00:00
|
|
|
const {index, scroll, disableScroll} = context;
|
|
|
|
if (!disableScroll) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._entryScrollIntoView(index || 0, scroll);
|
2019-12-27 23:04:39 +00:00
|
|
|
} else {
|
|
|
|
delete context.disableScroll;
|
2020-07-03 16:02:21 +00:00
|
|
|
this._entrySetCurrent(index || 0);
|
2019-12-27 23:04:39 +00:00
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._options.audio.enabled && this._options.audio.autoPlay) {
|
2019-12-27 23:04:39 +00:00
|
|
|
this.autoPlayAudio();
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setEventListenersActive(true);
|
2017-12-16 18:56:53 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const states = await this._getDefinitionsAddable(definitions, ['term-kanji', 'term-kana']);
|
|
|
|
if (this._setContentToken !== token) { return; }
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._updateAdderButtons(states);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _setContentKanji(definitions, context, token) {
|
2019-11-26 16:43:58 +00:00
|
|
|
if (!context) { throw new Error('Context expected'); }
|
2019-10-12 18:41:24 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setEventListenersActive(false);
|
2019-09-15 22:52:26 +00:00
|
|
|
|
2019-12-27 23:04:39 +00:00
|
|
|
if (context.focus !== false) {
|
|
|
|
window.focus();
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._definitions = definitions;
|
2019-12-27 23:04:39 +00:00
|
|
|
if (context.disableHistory) {
|
|
|
|
delete context.disableHistory;
|
2020-07-03 16:02:21 +00:00
|
|
|
this._context = new DisplayContext('kanji', definitions, context);
|
2019-12-27 23:04:39 +00:00
|
|
|
} else {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._context = DisplayContext.push(this._context, 'kanji', definitions, context);
|
2019-12-27 23:04:39 +00:00
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2019-12-27 23:04:39 +00:00
|
|
|
for (const definition of definitions) {
|
2020-07-03 16:02:21 +00:00
|
|
|
definition.cloze = this._clozeBuild(context.sentence, definition.character);
|
2019-12-27 23:04:39 +00:00
|
|
|
definition.url = context.url;
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._updateNavigation(this._context.previous, this._context.next);
|
|
|
|
this._setNoContentVisible(definitions.length === 0);
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const container = this._container;
|
2019-12-27 23:15:16 +00:00
|
|
|
container.textContent = '';
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2019-12-27 23:15:16 +00:00
|
|
|
for (let i = 0, ii = definitions.length; i < ii; ++i) {
|
|
|
|
if (i > 0) {
|
2020-02-16 01:36:46 +00:00
|
|
|
await promiseTimeout(1);
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._setContentToken !== token) { return; }
|
2019-12-27 23:15:16 +00:00
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const entry = this._displayGenerator.createKanjiEntry(definitions[i]);
|
2019-12-27 23:15:16 +00:00
|
|
|
container.appendChild(entry);
|
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2019-12-27 23:04:39 +00:00
|
|
|
const {index, scroll} = context;
|
2020-07-03 16:02:21 +00:00
|
|
|
this._entryScrollIntoView(index || 0, scroll);
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setEventListenersActive(true);
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const states = await this._getDefinitionsAddable(definitions, ['kanji']);
|
|
|
|
if (this._setContentToken !== token) { return; }
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._updateAdderButtons(states);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:22 +00:00
|
|
|
_setContentExtensionUnloaded() {
|
|
|
|
const errorExtensionUnloaded = document.querySelector('#error-extension-unloaded');
|
2019-10-17 01:36:10 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._container !== null) {
|
|
|
|
this._container.hidden = true;
|
2019-10-17 01:36:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:22 +00:00
|
|
|
if (errorExtensionUnloaded !== null) {
|
|
|
|
errorExtensionUnloaded.hidden = false;
|
2019-10-17 01:36:10 +00:00
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._updateNavigation(null, null);
|
|
|
|
this._setNoContentVisible(false);
|
2019-12-25 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_setNoContentVisible(visible) {
|
2019-12-25 02:45:57 +00:00
|
|
|
const noResults = document.querySelector('#no-results');
|
|
|
|
|
|
|
|
if (noResults !== null) {
|
|
|
|
noResults.hidden = !visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_updateNavigation(previous, next) {
|
2019-12-25 02:45:57 +00:00
|
|
|
const navigation = document.querySelector('#navigation-header');
|
|
|
|
if (navigation !== null) {
|
|
|
|
navigation.hidden = !(previous || next);
|
|
|
|
navigation.dataset.hasPrevious = `${!!previous}`;
|
|
|
|
navigation.dataset.hasNext = `${!!next}`;
|
|
|
|
}
|
2019-10-17 01:36:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_updateAdderButtons(states) {
|
2019-12-27 23:04:39 +00:00
|
|
|
for (let i = 0; i < states.length; ++i) {
|
|
|
|
let noteId = null;
|
2020-02-26 02:22:54 +00:00
|
|
|
for (const [mode, info] of Object.entries(states[i])) {
|
2020-07-03 16:02:21 +00:00
|
|
|
const button = this._adderButtonFind(i, mode);
|
2019-12-27 23:04:39 +00:00
|
|
|
if (button === null) {
|
|
|
|
continue;
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
2019-12-27 23:04:39 +00:00
|
|
|
|
|
|
|
if (!info.canAdd && noteId === null && info.noteId) {
|
|
|
|
noteId = info.noteId;
|
2019-10-10 00:31:09 +00:00
|
|
|
}
|
2019-12-27 23:04:39 +00:00
|
|
|
button.classList.toggle('disabled', !info.canAdd);
|
|
|
|
button.classList.remove('pending');
|
|
|
|
}
|
|
|
|
if (noteId !== null) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._viewerButtonShow(i, noteId);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_entrySetCurrent(index) {
|
|
|
|
index = Math.min(index, this._definitions.length - 1);
|
2017-08-13 23:11:51 +00:00
|
|
|
index = Math.max(index, 0);
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const entryPre = this._getEntry(this._index);
|
2019-09-15 22:44:49 +00:00
|
|
|
if (entryPre !== null) {
|
|
|
|
entryPre.classList.remove('entry-current');
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const entry = this._getEntry(index);
|
2019-09-15 22:44:49 +00:00
|
|
|
if (entry !== null) {
|
|
|
|
entry.classList.add('entry-current');
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._index = index;
|
2019-11-28 11:22:47 +00:00
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_entryScrollIntoView(index, scroll, smooth) {
|
|
|
|
this._windowScroll.stop();
|
2019-08-03 16:46:54 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const entry = this._entrySetCurrent(index);
|
2019-11-28 11:22:47 +00:00
|
|
|
let target;
|
2019-11-28 10:38:25 +00:00
|
|
|
if (scroll !== null) {
|
2019-08-03 16:46:54 +00:00
|
|
|
target = scroll;
|
|
|
|
} else {
|
2020-07-03 16:02:21 +00:00
|
|
|
target = this._index === 0 || entry === null ? 0 : this._getElementTop(entry);
|
2019-12-29 04:03:18 +00:00
|
|
|
|
|
|
|
const header = document.querySelector('#navigation-header');
|
|
|
|
if (header !== null) {
|
|
|
|
target -= header.getBoundingClientRect().height;
|
|
|
|
}
|
2019-08-03 16:46:54 +00:00
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
|
|
|
if (smooth) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._windowScroll.animate(this._windowScroll.x, target, 200);
|
2017-08-13 23:11:51 +00:00
|
|
|
} else {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._windowScroll.toY(target);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_sourceTermView() {
|
|
|
|
if (!this._context || !this._context.previous) { return; }
|
|
|
|
this._context.update({
|
|
|
|
index: this._index,
|
|
|
|
scroll: this._windowScroll.y
|
2019-12-01 03:38:23 +00:00
|
|
|
});
|
2020-07-03 16:02:21 +00:00
|
|
|
const previousContext = this._context.previous;
|
2019-12-01 03:38:23 +00:00
|
|
|
previousContext.set('disableHistory', true);
|
|
|
|
const details = {
|
|
|
|
definitions: previousContext.definitions,
|
|
|
|
context: previousContext.context
|
2019-11-30 02:49:37 +00:00
|
|
|
};
|
2019-12-01 03:38:23 +00:00
|
|
|
this.setContent(previousContext.type, details);
|
2017-03-26 01:08:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_nextTermView() {
|
|
|
|
if (!this._context || !this._context.next) { return; }
|
|
|
|
this._context.update({
|
|
|
|
index: this._index,
|
|
|
|
scroll: this._windowScroll.y
|
2019-12-01 03:38:23 +00:00
|
|
|
});
|
2020-07-03 16:02:21 +00:00
|
|
|
const nextContext = this._context.next;
|
2019-12-01 03:38:23 +00:00
|
|
|
nextContext.set('disableHistory', true);
|
|
|
|
const details = {
|
|
|
|
definitions: nextContext.definitions,
|
|
|
|
context: nextContext.context
|
|
|
|
};
|
|
|
|
this.setContent(nextContext.type, details);
|
2019-11-28 13:20:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_noteTryAdd(mode) {
|
|
|
|
const index = this._index;
|
|
|
|
if (index < 0 || index >= this._definitions.length) { return; }
|
2020-02-14 01:42:25 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const button = this._adderButtonFind(index, mode);
|
2019-09-20 02:03:26 +00:00
|
|
|
if (button !== null && !button.classList.contains('disabled')) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._noteAdd(this._definitions[index], mode);
|
2019-09-20 02:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_noteTryView() {
|
|
|
|
const button = this._viewerButtonFind(this._index);
|
2019-09-20 02:03:26 +00:00
|
|
|
if (button !== null && !button.classList.contains('disabled')) {
|
2020-05-24 17:30:40 +00:00
|
|
|
api.noteView(button.dataset.noteId);
|
2019-09-20 02:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _noteAdd(definition, mode) {
|
2017-08-13 23:11:51 +00:00
|
|
|
try {
|
2019-09-15 18:16:23 +00:00
|
|
|
this.setSpinnerVisible(true);
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-03-15 21:02:34 +00:00
|
|
|
const details = {};
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._noteUsesScreenshot(mode)) {
|
|
|
|
const screenshot = await this._getScreenshot();
|
2019-08-15 23:39:58 +00:00
|
|
|
if (screenshot) {
|
2020-03-15 21:02:34 +00:00
|
|
|
details.screenshot = screenshot;
|
2019-08-15 23:39:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:55:45 +00:00
|
|
|
const context = await this._getNoteContext();
|
2020-05-24 17:30:40 +00:00
|
|
|
const noteId = await api.definitionAdd(definition, mode, context, details, this.getOptionsContext());
|
2017-07-02 01:27:49 +00:00
|
|
|
if (noteId) {
|
2020-07-03 16:02:21 +00:00
|
|
|
const index = this._definitions.indexOf(definition);
|
|
|
|
const adderButton = this._adderButtonFind(index, mode);
|
2019-09-15 22:29:11 +00:00
|
|
|
if (adderButton !== null) {
|
|
|
|
adderButton.classList.add('disabled');
|
|
|
|
}
|
2020-07-03 16:02:21 +00:00
|
|
|
this._viewerButtonShow(index, noteId);
|
2017-03-25 17:46:59 +00:00
|
|
|
} else {
|
2019-10-08 00:46:02 +00:00
|
|
|
throw new Error('Note could not be added');
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
} finally {
|
2019-09-15 18:16:23 +00:00
|
|
|
this.setSpinnerVisible(false);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _audioPlay(definition, expressionIndex, entryIndex) {
|
2017-08-13 23:11:51 +00:00
|
|
|
try {
|
2019-09-15 18:16:23 +00:00
|
|
|
this.setSpinnerVisible(true);
|
2017-03-05 01:30:10 +00:00
|
|
|
|
2017-10-28 15:11:33 +00:00
|
|
|
const expression = expressionIndex === -1 ? definition : definition.expressions[expressionIndex];
|
2017-03-05 01:30:10 +00:00
|
|
|
|
2020-04-17 22:00:28 +00:00
|
|
|
this._stopPlayingAudio();
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-04-10 20:38:53 +00:00
|
|
|
let audio, info;
|
2020-03-07 18:18:48 +00:00
|
|
|
try {
|
2020-07-03 16:02:21 +00:00
|
|
|
const {sources, textToSpeechVoice, customSourceUrl} = this._options.audio;
|
2020-04-10 20:38:53 +00:00
|
|
|
let index;
|
2020-07-03 16:02:21 +00:00
|
|
|
({audio, index} = await this._audioSystem.getDefinitionAudio(expression, sources, {textToSpeechVoice, customSourceUrl}));
|
2020-04-10 20:38:53 +00:00
|
|
|
info = `From source ${1 + index}: ${sources[index]}`;
|
2020-03-07 18:18:48 +00:00
|
|
|
} catch (e) {
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._audioFallback === null) {
|
|
|
|
this._audioFallback = new Audio('/mixed/mp3/button.mp3');
|
2019-10-10 23:58:06 +00:00
|
|
|
}
|
2020-07-03 16:02:21 +00:00
|
|
|
audio = this._audioFallback;
|
2019-10-11 00:13:36 +00:00
|
|
|
info = 'Could not find audio';
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const button = this._audioButtonFindImage(entryIndex, expressionIndex);
|
2019-10-11 00:13:36 +00:00
|
|
|
if (button !== null) {
|
|
|
|
let titleDefault = button.dataset.titleDefault;
|
|
|
|
if (!titleDefault) {
|
2019-11-25 19:21:19 +00:00
|
|
|
titleDefault = button.title || '';
|
2019-10-11 00:13:36 +00:00
|
|
|
button.dataset.titleDefault = titleDefault;
|
|
|
|
}
|
|
|
|
button.title = `${titleDefault}\n${info}`;
|
2017-03-23 04:57:16 +00:00
|
|
|
}
|
2019-10-10 23:58:06 +00:00
|
|
|
|
2020-04-17 22:00:28 +00:00
|
|
|
this._stopPlayingAudio();
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const volume = Math.max(0.0, Math.min(1.0, this._options.audio.volume / 100.0));
|
|
|
|
this._audioPlaying = audio;
|
2019-10-10 23:58:06 +00:00
|
|
|
audio.currentTime = 0;
|
2020-06-21 19:51:36 +00:00
|
|
|
audio.volume = Number.isFinite(volume) ? volume : 1.0;
|
2020-04-17 21:48:55 +00:00
|
|
|
const playPromise = audio.play();
|
|
|
|
if (typeof playPromise !== 'undefined') {
|
|
|
|
try {
|
|
|
|
await playPromise;
|
|
|
|
} catch (e2) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
} finally {
|
2019-09-15 18:16:23 +00:00
|
|
|
this.setSpinnerVisible(false);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
2017-03-18 16:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:00:28 +00:00
|
|
|
_stopPlayingAudio() {
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._audioPlaying !== null) {
|
|
|
|
this._audioPlaying.pause();
|
|
|
|
this._audioPlaying = null;
|
2020-04-17 22:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_noteUsesScreenshot(mode) {
|
|
|
|
const optionsAnki = this._options.anki;
|
2020-02-26 03:24:22 +00:00
|
|
|
const fields = (mode === 'kanji' ? optionsAnki.kanji : optionsAnki.terms).fields;
|
2020-02-26 02:58:12 +00:00
|
|
|
for (const fieldValue of Object.values(fields)) {
|
|
|
|
if (fieldValue.includes('{screenshot}')) {
|
2019-08-15 23:39:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _getScreenshot() {
|
2019-08-15 23:39:58 +00:00
|
|
|
try {
|
2020-07-03 16:02:21 +00:00
|
|
|
await this._setPopupVisibleOverride(false);
|
2020-02-16 01:35:51 +00:00
|
|
|
await promiseTimeout(1); // Wait for popup to be hidden.
|
2019-08-15 23:39:58 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const {format, quality} = this._options.anki.screenshot;
|
2020-05-24 17:30:40 +00:00
|
|
|
const dataUrl = await api.screenshotGet({format, quality});
|
2019-08-15 23:39:58 +00:00
|
|
|
if (!dataUrl || dataUrl.error) { return; }
|
|
|
|
|
|
|
|
return {dataUrl, format};
|
|
|
|
} finally {
|
2020-07-03 16:02:21 +00:00
|
|
|
await this._setPopupVisibleOverride(null);
|
2019-08-15 23:39:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_getFirstExpressionIndex() {
|
|
|
|
return this._options.general.resultOutputMode === 'merge' ? 0 : -1;
|
2017-12-16 18:56:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_setPopupVisibleOverride(visible) {
|
2020-05-24 17:30:40 +00:00
|
|
|
return api.broadcastTab('popupSetVisibleOverride', {visible});
|
2019-08-15 23:39:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_getEntry(index) {
|
|
|
|
const entries = this._container.querySelectorAll('.entry');
|
2019-09-15 22:29:11 +00:00
|
|
|
return index >= 0 && index < entries.length ? entries[index] : null;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_clozeBuild({text, offset}, source) {
|
2019-11-26 15:53:21 +00:00
|
|
|
return {
|
|
|
|
sentence: text.trim(),
|
|
|
|
prefix: text.substring(0, offset).trim(),
|
|
|
|
body: text.substring(offset, offset + source.length),
|
|
|
|
suffix: text.substring(offset + source.length).trim()
|
2017-07-16 19:48:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_entryIndexFind(element) {
|
2019-09-15 18:06:27 +00:00
|
|
|
const entry = element.closest('.entry');
|
2020-07-03 16:02:21 +00:00
|
|
|
return entry !== null ? this._indexOf(this._container.querySelectorAll('.entry'), entry) : -1;
|
2017-03-23 05:28:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_adderButtonFind(index, mode) {
|
|
|
|
const entry = this._getEntry(index);
|
2019-09-15 22:29:11 +00:00
|
|
|
return entry !== null ? entry.querySelector(`.action-add-note[data-mode="${mode}"]`) : null;
|
2017-03-23 05:28:22 +00:00
|
|
|
}
|
2017-07-02 01:27:49 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_viewerButtonFind(index) {
|
|
|
|
const entry = this._getEntry(index);
|
2019-09-15 22:29:11 +00:00
|
|
|
return entry !== null ? entry.querySelector('.action-view-note') : null;
|
2017-07-02 01:27:49 +00:00
|
|
|
}
|
2019-08-15 23:39:58 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_viewerButtonShow(index, noteId) {
|
|
|
|
const viewerButton = this._viewerButtonFind(index);
|
2019-10-10 00:31:09 +00:00
|
|
|
if (viewerButton === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
viewerButton.classList.remove('pending', 'disabled');
|
|
|
|
viewerButton.dataset.noteId = noteId;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_audioButtonFindImage(index, expressionIndex) {
|
|
|
|
const entry = this._getEntry(index);
|
2020-04-10 20:43:57 +00:00
|
|
|
if (entry === null) { return null; }
|
|
|
|
|
|
|
|
const container = (
|
|
|
|
expressionIndex >= 0 ?
|
|
|
|
entry.querySelector(`.term-expression:nth-of-type(${expressionIndex + 1})`) :
|
|
|
|
entry
|
|
|
|
);
|
|
|
|
return container !== null ? container.querySelector('.action-play-audio>img') : null;
|
2019-10-11 00:13:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _getDefinitionsAddable(definitions, modes) {
|
2020-02-16 01:33:20 +00:00
|
|
|
try {
|
2020-03-28 02:11:24 +00:00
|
|
|
const context = await this._getNoteContext();
|
2020-05-24 17:30:40 +00:00
|
|
|
return await api.definitionsAddable(definitions, modes, context, this.getOptionsContext());
|
2020-02-16 01:33:20 +00:00
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_indexOf(nodeList, node) {
|
2019-09-15 18:06:27 +00:00
|
|
|
for (let i = 0, ii = nodeList.length; i < ii; ++i) {
|
|
|
|
if (nodeList[i] === node) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2019-09-15 18:53:30 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_getElementTop(element) {
|
2019-09-15 22:44:49 +00:00
|
|
|
const elementRect = element.getBoundingClientRect();
|
|
|
|
const documentRect = document.documentElement.getBoundingClientRect();
|
|
|
|
return elementRect.top - documentRect.top;
|
|
|
|
}
|
2019-09-20 02:03:26 +00:00
|
|
|
|
2020-03-28 02:11:24 +00:00
|
|
|
async _getNoteContext() {
|
|
|
|
const documentTitle = await this.getDocumentTitle();
|
2020-03-15 21:32:31 +00:00
|
|
|
return {
|
|
|
|
document: {
|
2020-03-28 02:11:24 +00:00
|
|
|
title: documentTitle
|
2020-03-15 21:32:31 +00:00
|
|
|
}
|
|
|
|
};
|
2020-03-15 21:13:00 +00:00
|
|
|
}
|
2020-07-09 00:02:20 +00:00
|
|
|
|
|
|
|
_playAudioCurrent() {
|
|
|
|
const index = this._index;
|
|
|
|
if (index < 0 || index >= this._definitions.length) { return; }
|
|
|
|
|
|
|
|
const entry = this._getEntry(index);
|
|
|
|
if (entry !== null && entry.dataset.type === 'term') {
|
|
|
|
this._audioPlay(this._definitions[index], this._getFirstExpressionIndex(), index);
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|