2021-01-18 05:16:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Yomichan Authors
|
|
|
|
*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global
|
|
|
|
* AudioSystem
|
2021-01-24 02:13:01 +00:00
|
|
|
* PopupMenu
|
2021-01-18 05:16:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
class DisplayAudio {
|
|
|
|
constructor(display) {
|
|
|
|
this._display = display;
|
|
|
|
this._audioPlaying = null;
|
2021-01-23 03:10:27 +00:00
|
|
|
this._audioSystem = new AudioSystem();
|
2021-05-30 16:15:07 +00:00
|
|
|
this._playbackVolume = 1.0;
|
|
|
|
this._autoPlay = false;
|
2021-01-18 05:16:40 +00:00
|
|
|
this._autoPlayAudioTimer = null;
|
|
|
|
this._autoPlayAudioDelay = 400;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
2021-01-23 05:31:23 +00:00
|
|
|
this._cache = new Map();
|
2021-01-24 02:13:01 +00:00
|
|
|
this._menuContainer = document.querySelector('#popup-menus');
|
2021-02-16 02:46:55 +00:00
|
|
|
this._entriesToken = {};
|
2021-03-25 23:22:34 +00:00
|
|
|
this._openMenus = new Set();
|
2021-05-30 16:15:07 +00:00
|
|
|
this._audioSources = [];
|
|
|
|
this._audioSourceTypeNames = new Map([
|
|
|
|
['jpod101', 'JapanesePod101'],
|
|
|
|
['jpod101-alternate', 'JapanesePod101 (Alternate)'],
|
|
|
|
['jisho', 'Jisho.org'],
|
|
|
|
['text-to-speech', 'Text-to-speech'],
|
|
|
|
['text-to-speech-reading', 'Text-to-speech (Kana reading)'],
|
|
|
|
['custom', 'Custom URL'],
|
|
|
|
['custom-json', 'Custom URL (JSON)']
|
|
|
|
]);
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get autoPlayAudioDelay() {
|
|
|
|
return this._autoPlayAudioDelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
set autoPlayAudioDelay(value) {
|
|
|
|
this._autoPlayAudioDelay = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare() {
|
|
|
|
this._audioSystem.prepare();
|
2021-10-03 20:46:15 +00:00
|
|
|
this._display.hotkeyHandler.registerActions([
|
|
|
|
['playAudio', this._onHotkeyActionPlayAudio.bind(this)],
|
|
|
|
['playAudioFromSource', this._onHotkeyActionPlayAudioFromSource.bind(this)]
|
|
|
|
]);
|
|
|
|
this._display.registerDirectMessageHandlers([
|
|
|
|
['clearAutoPlayTimer', {async: false, handler: this._onMessageClearAutoPlayTimer.bind(this)}]
|
|
|
|
]);
|
2021-05-30 16:15:07 +00:00
|
|
|
this._display.on('optionsUpdated', this._onOptionsUpdated.bind(this));
|
2021-10-03 20:46:15 +00:00
|
|
|
this._display.on('contentClear', this._onContentClear.bind(this));
|
|
|
|
this._display.on('contentUpdateEntry', this._onContentUpdateEntry.bind(this));
|
|
|
|
this._display.on('contentUpdateComplete', this._onContentUpdateComplete.bind(this));
|
|
|
|
this._display.on('frameVisibilityChange', this._onFrameVisibilityChange.bind(this));
|
2021-05-30 16:15:07 +00:00
|
|
|
this._onOptionsUpdated({options: this._display.getOptions()});
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clearAutoPlayTimer() {
|
|
|
|
if (this._autoPlayAudioTimer === null) { return; }
|
|
|
|
clearTimeout(this._autoPlayAudioTimer);
|
|
|
|
this._autoPlayAudioTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopAudio() {
|
|
|
|
if (this._audioPlaying === null) { return; }
|
|
|
|
this._audioPlaying.pause();
|
|
|
|
this._audioPlaying = null;
|
|
|
|
}
|
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
async playAudio(dictionaryEntryIndex, headwordIndex, sourceType=null) {
|
|
|
|
let sources = this._audioSources;
|
|
|
|
if (sourceType !== null) {
|
|
|
|
sources = [];
|
|
|
|
for (const source of this._audioSources) {
|
|
|
|
if (source.type === sourceType) {
|
|
|
|
sources.push(source);
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 02:46:55 +00:00
|
|
|
}
|
2021-05-30 16:15:07 +00:00
|
|
|
await this._playAudio(dictionaryEntryIndex, headwordIndex, sources, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAnkiNoteMediaAudioDetails(term, reading) {
|
|
|
|
const sources = [];
|
|
|
|
let preferredAudioIndex = null;
|
|
|
|
const primaryCardAudio = this._getPrimaryCardAudio(term, reading);
|
|
|
|
if (primaryCardAudio !== null) {
|
|
|
|
const {index, subIndex} = primaryCardAudio;
|
|
|
|
const source = this._audioSources[index];
|
|
|
|
sources.push(this._getSourceData(source));
|
|
|
|
preferredAudioIndex = subIndex;
|
|
|
|
} else {
|
|
|
|
for (const source of this._audioSources) {
|
|
|
|
if (!source.isInOptions) { continue; }
|
|
|
|
sources.push(this._getSourceData(source));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {sources, preferredAudioIndex};
|
|
|
|
}
|
2021-01-18 05:16:40 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
// Private
|
2021-01-24 02:13:01 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
_onOptionsUpdated({options}) {
|
|
|
|
if (options === null) { return; }
|
2021-05-30 16:41:19 +00:00
|
|
|
const {enabled, autoPlay, volume, sources} = options.audio;
|
2021-05-30 16:15:07 +00:00
|
|
|
this._autoPlay = enabled && autoPlay;
|
|
|
|
this._playbackVolume = Number.isFinite(volume) ? Math.max(0.0, Math.min(1.0, volume / 100.0)) : 1.0;
|
|
|
|
|
|
|
|
const requiredAudioSources = new Set([
|
|
|
|
'jpod101',
|
|
|
|
'jpod101-alternate',
|
|
|
|
'jisho'
|
|
|
|
]);
|
2021-05-30 16:41:19 +00:00
|
|
|
const nameMap = new Map();
|
2021-05-30 16:15:07 +00:00
|
|
|
this._audioSources.length = 0;
|
2021-05-30 16:41:19 +00:00
|
|
|
for (const {type, url, voice} of sources) {
|
|
|
|
this._addAudioSourceInfo(type, url, voice, true, nameMap);
|
2021-05-30 16:15:07 +00:00
|
|
|
requiredAudioSources.delete(type);
|
2021-01-23 16:18:45 +00:00
|
|
|
}
|
2021-05-30 16:15:07 +00:00
|
|
|
for (const type of requiredAudioSources) {
|
2021-05-30 16:41:19 +00:00
|
|
|
this._addAudioSourceInfo(type, '', '', false, nameMap);
|
2021-01-23 16:18:45 +00:00
|
|
|
}
|
2021-01-18 05:16:40 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
const data = document.documentElement.dataset;
|
|
|
|
data.audioEnabled = `${enabled && sources.length > 0}`;
|
2021-02-16 02:46:55 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
this._cache.clear();
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 20:46:15 +00:00
|
|
|
_onContentClear() {
|
|
|
|
this._entriesToken = {};
|
|
|
|
this._cache.clear();
|
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onContentUpdateEntry({element, index}) {
|
|
|
|
for (const button of element.querySelectorAll('.action-play-audio')) {
|
|
|
|
const headwordIndex = this._getAudioPlayButtonHeadwordIndex(button);
|
|
|
|
this._eventListeners.addEventListener(button, 'click', this._onAudioPlayButtonClick.bind(this, index, headwordIndex), false);
|
|
|
|
this._eventListeners.addEventListener(button, 'contextmenu', this._onAudioPlayButtonContextMenu.bind(this, index, headwordIndex), false);
|
|
|
|
this._eventListeners.addEventListener(button, 'menuClose', this._onAudioPlayMenuCloseClick.bind(this, index, headwordIndex), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onContentUpdateComplete() {
|
|
|
|
if (!this._autoPlay || !this._display.frameVisible) { return; }
|
|
|
|
|
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
|
|
|
|
const {dictionaryEntries} = this._display;
|
|
|
|
if (dictionaryEntries.length === 0) { return; }
|
|
|
|
|
|
|
|
const firstDictionaryEntries = dictionaryEntries[0];
|
|
|
|
if (firstDictionaryEntries.type === 'kanji') { return; }
|
|
|
|
|
|
|
|
const callback = () => {
|
|
|
|
this._autoPlayAudioTimer = null;
|
|
|
|
this.playAudio(0, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this._autoPlayAudioDelay > 0) {
|
|
|
|
this._autoPlayAudioTimer = setTimeout(callback, this._autoPlayAudioDelay);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFrameVisibilityChange({value}) {
|
|
|
|
if (!value) {
|
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
this.stopAudio();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onHotkeyActionPlayAudio() {
|
|
|
|
this.playAudio(this._display.selectedIndex, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onHotkeyActionPlayAudioFromSource(source) {
|
|
|
|
this.playAudio(this._display.selectedIndex, 0, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageClearAutoPlayTimer() {
|
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
}
|
|
|
|
|
2021-05-30 16:41:19 +00:00
|
|
|
_addAudioSourceInfo(type, url, voice, isInOptions, nameMap) {
|
2021-05-30 16:15:07 +00:00
|
|
|
const index = this._audioSources.length;
|
|
|
|
const downloadable = this._sourceIsDownloadable(type);
|
2021-05-30 16:41:19 +00:00
|
|
|
let name = this._audioSourceTypeNames.get(type);
|
|
|
|
if (typeof name === 'undefined') { name = 'Unknown'; }
|
|
|
|
|
|
|
|
let entries = nameMap.get(name);
|
|
|
|
if (typeof entries === 'undefined') {
|
|
|
|
entries = [];
|
|
|
|
nameMap.set(name, entries);
|
|
|
|
}
|
|
|
|
const nameIndex = entries.length;
|
|
|
|
if (nameIndex === 1) {
|
|
|
|
entries[0].nameUnique = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const source = {
|
2021-05-30 16:15:07 +00:00
|
|
|
index,
|
|
|
|
type,
|
|
|
|
url,
|
|
|
|
voice,
|
|
|
|
isInOptions,
|
|
|
|
downloadable,
|
2021-05-30 16:41:19 +00:00
|
|
|
name,
|
|
|
|
nameIndex,
|
|
|
|
nameUnique: (nameIndex === 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
entries.push(source);
|
|
|
|
this._audioSources.push(source);
|
2021-02-16 02:34:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_onAudioPlayButtonClick(dictionaryEntryIndex, headwordIndex, e) {
|
2021-01-18 05:16:40 +00:00
|
|
|
e.preventDefault();
|
2021-01-24 02:13:01 +00:00
|
|
|
|
|
|
|
if (e.shiftKey) {
|
2021-04-04 20:22:35 +00:00
|
|
|
this._showAudioMenu(e.currentTarget, dictionaryEntryIndex, headwordIndex);
|
2021-01-24 02:13:01 +00:00
|
|
|
} else {
|
2021-04-04 20:22:35 +00:00
|
|
|
this.playAudio(dictionaryEntryIndex, headwordIndex);
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_onAudioPlayButtonContextMenu(dictionaryEntryIndex, headwordIndex, e) {
|
2021-01-24 02:13:01 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
this._showAudioMenu(e.currentTarget, dictionaryEntryIndex, headwordIndex);
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_onAudioPlayMenuCloseClick(dictionaryEntryIndex, headwordIndex, e) {
|
2021-03-25 23:22:34 +00:00
|
|
|
const {detail: {action, item, menu, shiftKey}} = e;
|
2021-01-24 02:13:01 +00:00
|
|
|
switch (action) {
|
|
|
|
case 'playAudioFromSource':
|
2021-03-25 23:22:34 +00:00
|
|
|
if (shiftKey) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2021-04-04 20:22:35 +00:00
|
|
|
this._playAudioFromSource(dictionaryEntryIndex, headwordIndex, item);
|
2021-02-16 02:34:10 +00:00
|
|
|
break;
|
|
|
|
case 'setPrimaryAudio':
|
|
|
|
e.preventDefault();
|
2021-04-04 20:22:35 +00:00
|
|
|
this._setPrimaryAudio(dictionaryEntryIndex, headwordIndex, item, menu, true);
|
2021-01-24 02:13:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_getCacheItem(term, reading, create) {
|
|
|
|
const key = this._getTermReadingKey(term, reading);
|
2021-02-16 02:34:10 +00:00
|
|
|
let cacheEntry = this._cache.get(key);
|
|
|
|
if (typeof cacheEntry === 'undefined' && create) {
|
|
|
|
cacheEntry = {
|
|
|
|
sourceMap: new Map(),
|
|
|
|
primaryCardAudio: null
|
|
|
|
};
|
|
|
|
this._cache.set(key, cacheEntry);
|
|
|
|
}
|
|
|
|
return cacheEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getMenuItemSourceInfo(item) {
|
|
|
|
const group = item.closest('.popup-menu-item-group');
|
2021-05-30 16:15:07 +00:00
|
|
|
if (group !== null) {
|
|
|
|
let {index, subIndex} = group.dataset;
|
2021-02-16 02:34:10 +00:00
|
|
|
index = Number.parseInt(index, 10);
|
2021-05-30 16:15:07 +00:00
|
|
|
if (index >= 0 && index < this._audioSources.length) {
|
|
|
|
const source = this._audioSources[index];
|
|
|
|
if (typeof subIndex === 'string') {
|
|
|
|
subIndex = Number.parseInt(subIndex, 10);
|
|
|
|
} else {
|
|
|
|
subIndex = null;
|
|
|
|
}
|
|
|
|
return {source, subIndex};
|
|
|
|
}
|
2021-02-16 02:34:10 +00:00
|
|
|
}
|
2021-05-30 16:15:07 +00:00
|
|
|
return {source: null, subIndex: null};
|
|
|
|
}
|
|
|
|
|
|
|
|
async _playAudio(dictionaryEntryIndex, headwordIndex, sources, audioInfoListIndex) {
|
|
|
|
this.stopAudio();
|
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
|
|
|
|
const headword = this._getHeadword(dictionaryEntryIndex, headwordIndex);
|
|
|
|
if (headword === null) {
|
|
|
|
return {audio: null, source: null, valid: false};
|
|
|
|
}
|
|
|
|
|
|
|
|
const buttons = this._getAudioPlayButtons(dictionaryEntryIndex, headwordIndex);
|
|
|
|
|
|
|
|
const {term, reading} = headword;
|
|
|
|
|
|
|
|
const progressIndicatorVisible = this._display.progressIndicatorVisible;
|
|
|
|
const overrideToken = progressIndicatorVisible.setOverride(true);
|
|
|
|
try {
|
|
|
|
// Create audio
|
|
|
|
let audio;
|
|
|
|
let title;
|
|
|
|
let source = null;
|
|
|
|
let subIndex = 0;
|
|
|
|
const info = await this._createTermAudio(term, reading, sources, audioInfoListIndex);
|
|
|
|
const valid = (info !== null);
|
|
|
|
if (valid) {
|
|
|
|
({audio, source, subIndex} = info);
|
|
|
|
const sourceIndex = sources.indexOf(source);
|
|
|
|
title = `From source ${1 + sourceIndex}: ${source}`;
|
|
|
|
} else {
|
|
|
|
audio = this._audioSystem.getFallbackAudio();
|
|
|
|
title = 'Could not find audio';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop any currently playing audio
|
|
|
|
this.stopAudio();
|
|
|
|
|
|
|
|
// Update details
|
|
|
|
const potentialAvailableAudioCount = this._getPotentialAvailableAudioCount(term, reading);
|
|
|
|
for (const button of buttons) {
|
|
|
|
const titleDefault = button.dataset.titleDefault || '';
|
|
|
|
button.title = `${titleDefault}\n${title}`;
|
|
|
|
this._updateAudioPlayButtonBadge(button, potentialAvailableAudioCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play
|
|
|
|
audio.currentTime = 0;
|
|
|
|
audio.volume = this._playbackVolume;
|
|
|
|
|
|
|
|
const playPromise = audio.play();
|
|
|
|
this._audioPlaying = audio;
|
|
|
|
|
|
|
|
if (typeof playPromise !== 'undefined') {
|
|
|
|
try {
|
|
|
|
await playPromise;
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {audio, source, subIndex, valid};
|
|
|
|
} finally {
|
|
|
|
progressIndicatorVisible.clearOverride(overrideToken);
|
2021-02-16 02:34:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
async _playAudioFromSource(dictionaryEntryIndex, headwordIndex, item) {
|
2021-05-30 16:15:07 +00:00
|
|
|
const {source, subIndex} = this._getMenuItemSourceInfo(item);
|
|
|
|
if (source === null) { return; }
|
2021-02-16 02:34:10 +00:00
|
|
|
|
2021-02-16 02:46:55 +00:00
|
|
|
try {
|
|
|
|
const token = this._entriesToken;
|
2021-05-30 16:15:07 +00:00
|
|
|
const {valid} = await this._playAudio(dictionaryEntryIndex, headwordIndex, [source], subIndex);
|
2021-02-16 02:46:55 +00:00
|
|
|
if (valid && token === this._entriesToken) {
|
2021-04-04 20:22:35 +00:00
|
|
|
this._setPrimaryAudio(dictionaryEntryIndex, headwordIndex, item, null, false);
|
2021-02-16 02:46:55 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
2021-02-16 02:34:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_setPrimaryAudio(dictionaryEntryIndex, headwordIndex, item, menu, canToggleOff) {
|
2021-05-30 16:15:07 +00:00
|
|
|
const {source, subIndex} = this._getMenuItemSourceInfo(item);
|
|
|
|
if (source === null || !source.downloadable) { return; }
|
2021-02-16 02:34:10 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
const headword = this._getHeadword(dictionaryEntryIndex, headwordIndex);
|
|
|
|
if (headword === null) { return; }
|
2021-02-16 02:34:10 +00:00
|
|
|
|
2021-05-30 17:29:55 +00:00
|
|
|
const {index} = source;
|
2021-04-04 20:22:35 +00:00
|
|
|
const {term, reading} = headword;
|
|
|
|
const cacheEntry = this._getCacheItem(term, reading, true);
|
2021-02-16 02:34:10 +00:00
|
|
|
|
|
|
|
let {primaryCardAudio} = cacheEntry;
|
2021-05-30 16:15:07 +00:00
|
|
|
primaryCardAudio = (
|
|
|
|
!canToggleOff ||
|
|
|
|
primaryCardAudio === null ||
|
2021-05-30 17:29:55 +00:00
|
|
|
primaryCardAudio.index !== index ||
|
|
|
|
primaryCardAudio.subIndex !== subIndex
|
|
|
|
) ? {index: index, subIndex} : null;
|
2021-02-16 02:34:10 +00:00
|
|
|
cacheEntry.primaryCardAudio = primaryCardAudio;
|
|
|
|
|
2021-02-16 02:46:55 +00:00
|
|
|
if (menu !== null) {
|
2021-04-04 20:22:35 +00:00
|
|
|
this._updateMenuPrimaryCardAudio(menu.bodyNode, term, reading);
|
2021-02-16 02:46:55 +00:00
|
|
|
}
|
2021-02-16 02:34:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_getAudioPlayButtonHeadwordIndex(button) {
|
2021-05-23 17:20:38 +00:00
|
|
|
const headwordNode = button.closest('.headword');
|
2021-04-04 20:22:35 +00:00
|
|
|
if (headwordNode !== null) {
|
|
|
|
const headwordIndex = parseInt(headwordNode.dataset.index, 10);
|
|
|
|
if (Number.isFinite(headwordIndex)) { return headwordIndex; }
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_getAudioPlayButtons(dictionaryEntryIndex, headwordIndex) {
|
2021-01-18 05:16:40 +00:00
|
|
|
const results = [];
|
2021-04-04 20:22:35 +00:00
|
|
|
const {dictionaryEntryNodes} = this._display;
|
|
|
|
if (dictionaryEntryIndex >= 0 && dictionaryEntryIndex < dictionaryEntryNodes.length) {
|
|
|
|
const node = dictionaryEntryNodes[dictionaryEntryIndex];
|
|
|
|
const button1 = (headwordIndex === 0 ? node.querySelector('.action-play-audio') : null);
|
2021-05-23 17:20:38 +00:00
|
|
|
const button2 = node.querySelector(`.headword:nth-of-type(${headwordIndex + 1}) .action-play-audio`);
|
2021-01-18 05:16:40 +00:00
|
|
|
if (button1 !== null) { results.push(button1); }
|
|
|
|
if (button2 !== null) { results.push(button2); }
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
2021-01-23 03:10:27 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
async _createTermAudio(term, reading, sources, audioInfoListIndex) {
|
2021-04-04 20:22:35 +00:00
|
|
|
const {sourceMap} = this._getCacheItem(term, reading, true);
|
2021-01-23 03:10:27 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
for (const source of sources) {
|
|
|
|
const {index} = source;
|
2021-01-23 05:31:23 +00:00
|
|
|
|
2021-03-25 23:22:34 +00:00
|
|
|
let cacheUpdated = false;
|
2021-01-23 16:18:45 +00:00
|
|
|
let infoListPromise;
|
2021-05-30 16:15:07 +00:00
|
|
|
let sourceInfo = sourceMap.get(index);
|
2021-01-23 16:18:45 +00:00
|
|
|
if (typeof sourceInfo === 'undefined') {
|
2021-05-30 16:15:07 +00:00
|
|
|
infoListPromise = this._getTermAudioInfoList(source, term, reading);
|
2021-01-23 16:18:45 +00:00
|
|
|
sourceInfo = {infoListPromise, infoList: null};
|
2021-05-30 16:15:07 +00:00
|
|
|
sourceMap.set(index, sourceInfo);
|
2021-03-25 23:22:34 +00:00
|
|
|
cacheUpdated = true;
|
2021-01-23 16:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let {infoList} = sourceInfo;
|
|
|
|
if (infoList === null) {
|
|
|
|
infoList = await infoListPromise;
|
|
|
|
sourceInfo.infoList = infoList;
|
2021-01-23 05:31:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
const {audio, index: subIndex, cacheUpdated: cacheUpdated2} = await this._createAudioFromInfoList(source, infoList, audioInfoListIndex);
|
2021-03-25 23:22:34 +00:00
|
|
|
if (cacheUpdated || cacheUpdated2) { this._updateOpenMenu(); }
|
2021-05-30 16:15:07 +00:00
|
|
|
if (audio !== null) {
|
|
|
|
return {audio, source, subIndex};
|
|
|
|
}
|
2021-01-23 16:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
async _createAudioFromInfoList(source, infoList, audioInfoListIndex) {
|
|
|
|
let start = 0;
|
|
|
|
let end = infoList.length;
|
|
|
|
if (audioInfoListIndex !== null) {
|
|
|
|
start = Math.max(0, Math.min(end, audioInfoListIndex));
|
|
|
|
end = Math.max(0, Math.min(end, audioInfoListIndex + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
audio: null,
|
|
|
|
index: -1,
|
|
|
|
cacheUpdated: false
|
|
|
|
};
|
2021-01-23 16:18:45 +00:00
|
|
|
for (let i = start; i < end; ++i) {
|
|
|
|
const item = infoList[i];
|
|
|
|
|
|
|
|
let {audio, audioResolved} = item;
|
2021-01-23 05:31:23 +00:00
|
|
|
|
2021-01-23 16:18:45 +00:00
|
|
|
if (!audioResolved) {
|
2021-01-23 05:31:23 +00:00
|
|
|
let {audioPromise} = item;
|
|
|
|
if (audioPromise === null) {
|
|
|
|
audioPromise = this._createAudioFromInfo(item.info, source);
|
|
|
|
item.audioPromise = audioPromise;
|
|
|
|
}
|
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
result.cacheUpdated = true;
|
2021-03-25 23:22:34 +00:00
|
|
|
|
2021-01-23 03:10:27 +00:00
|
|
|
try {
|
2021-01-23 05:31:23 +00:00
|
|
|
audio = await audioPromise;
|
2021-01-23 03:10:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
continue;
|
2021-01-23 16:18:45 +00:00
|
|
|
} finally {
|
|
|
|
item.audioResolved = true;
|
2021-01-23 03:10:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 16:18:45 +00:00
|
|
|
item.audio = audio;
|
2021-01-23 03:10:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:22:34 +00:00
|
|
|
if (audio !== null) {
|
2021-05-30 16:15:07 +00:00
|
|
|
result.audio = audio;
|
|
|
|
result.index = i;
|
2021-03-25 23:22:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-01-23 16:18:45 +00:00
|
|
|
}
|
2021-05-30 16:15:07 +00:00
|
|
|
return result;
|
2021-01-23 03:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _createAudioFromInfo(info, source) {
|
|
|
|
switch (info.type) {
|
|
|
|
case 'url':
|
2021-05-30 16:15:07 +00:00
|
|
|
return await this._audioSystem.createAudio(info.url, source.type);
|
2021-01-23 03:10:27 +00:00
|
|
|
case 'tts':
|
|
|
|
return this._audioSystem.createTextToSpeechAudio(info.text, info.voice);
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported type: ${info.type}`);
|
|
|
|
}
|
|
|
|
}
|
2021-01-23 05:31:23 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
async _getTermAudioInfoList(source, term, reading) {
|
|
|
|
const sourceData = this._getSourceData(source);
|
|
|
|
const infoList = await yomichan.api.getTermAudioInfoList(sourceData, term, reading);
|
2021-01-24 01:24:52 +00:00
|
|
|
return infoList.map((info) => ({info, audioPromise: null, audioResolved: false, audio: null}));
|
2021-01-23 05:31:23 +00:00
|
|
|
}
|
2021-01-23 16:18:45 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_getHeadword(dictionaryEntryIndex, headwordIndex) {
|
|
|
|
const {dictionaryEntries} = this._display;
|
|
|
|
if (dictionaryEntryIndex < 0 || dictionaryEntryIndex >= dictionaryEntries.length) { return null; }
|
2021-01-23 16:18:45 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
const dictionaryEntry = dictionaryEntries[dictionaryEntryIndex];
|
|
|
|
if (dictionaryEntry.type === 'kanji') { return null; }
|
2021-01-23 16:18:45 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
const {headwords} = dictionaryEntry;
|
|
|
|
if (headwordIndex < 0 || headwordIndex >= headwords.length) { return null; }
|
2021-01-23 16:18:45 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
return headwords[headwordIndex];
|
2021-01-23 16:18:45 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_getTermReadingKey(term, reading) {
|
|
|
|
return JSON.stringify([term, reading]);
|
2021-01-23 16:18:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 01:24:52 +00:00
|
|
|
_updateAudioPlayButtonBadge(button, potentialAvailableAudioCount) {
|
|
|
|
if (potentialAvailableAudioCount === null) {
|
|
|
|
delete button.dataset.potentialAvailableAudioCount;
|
|
|
|
} else {
|
|
|
|
button.dataset.potentialAvailableAudioCount = `${potentialAvailableAudioCount}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const badge = button.querySelector('.action-button-badge');
|
|
|
|
if (badge === null) { return; }
|
|
|
|
|
|
|
|
const badgeData = badge.dataset;
|
|
|
|
switch (potentialAvailableAudioCount) {
|
|
|
|
case 0:
|
|
|
|
badgeData.icon = 'cross';
|
|
|
|
badgeData.hidden = false;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case null:
|
|
|
|
delete badgeData.icon;
|
|
|
|
badgeData.hidden = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
badgeData.icon = 'plus-thick';
|
|
|
|
badgeData.hidden = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_getPotentialAvailableAudioCount(term, reading) {
|
|
|
|
const cacheEntry = this._getCacheItem(term, reading, false);
|
2021-02-16 02:34:10 +00:00
|
|
|
if (typeof cacheEntry === 'undefined') { return null; }
|
2021-01-24 01:24:52 +00:00
|
|
|
|
2021-02-16 02:34:10 +00:00
|
|
|
const {sourceMap} = cacheEntry;
|
2021-01-24 01:24:52 +00:00
|
|
|
let count = 0;
|
|
|
|
for (const {infoList} of sourceMap.values()) {
|
|
|
|
if (infoList === null) { continue; }
|
|
|
|
for (const {audio, audioResolved} of infoList) {
|
|
|
|
if (!audioResolved || audio !== null) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2021-01-24 02:13:01 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_showAudioMenu(button, dictionaryEntryIndex, headwordIndex) {
|
|
|
|
const headword = this._getHeadword(dictionaryEntryIndex, headwordIndex);
|
|
|
|
if (headword === null) { return; }
|
2021-01-24 02:13:01 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
const {term, reading} = headword;
|
|
|
|
const popupMenu = this._createMenu(button, term, reading);
|
2021-03-25 23:22:34 +00:00
|
|
|
this._openMenus.add(popupMenu);
|
2021-01-24 02:13:01 +00:00
|
|
|
popupMenu.prepare();
|
2021-03-25 23:22:34 +00:00
|
|
|
popupMenu.on('close', this._onPopupMenuClose.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPopupMenuClose({menu}) {
|
|
|
|
this._openMenus.delete(menu);
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 02:34:10 +00:00
|
|
|
_sourceIsDownloadable(source) {
|
|
|
|
switch (source) {
|
|
|
|
case 'text-to-speech':
|
|
|
|
case 'text-to-speech-reading':
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_createMenu(sourceButton, term, reading) {
|
2021-02-15 19:31:16 +00:00
|
|
|
// Create menu
|
2021-03-25 23:22:34 +00:00
|
|
|
const menuContainerNode = this._display.displayGenerator.instantiateTemplate('audio-button-popup-menu');
|
|
|
|
const menuBodyNode = menuContainerNode.querySelector('.popup-menu-body');
|
2021-04-04 20:22:35 +00:00
|
|
|
menuContainerNode.dataset.term = term;
|
2021-03-25 23:22:34 +00:00
|
|
|
menuContainerNode.dataset.reading = reading;
|
2021-02-15 19:31:16 +00:00
|
|
|
|
|
|
|
// Set up items based on options and cache data
|
2021-04-04 20:22:35 +00:00
|
|
|
this._createMenuItems(menuContainerNode, menuBodyNode, term, reading);
|
2021-03-25 23:22:34 +00:00
|
|
|
|
|
|
|
// Update primary card audio display
|
2021-04-04 20:22:35 +00:00
|
|
|
this._updateMenuPrimaryCardAudio(menuBodyNode, term, reading);
|
2021-03-25 23:22:34 +00:00
|
|
|
|
|
|
|
// Create popup menu
|
|
|
|
this._menuContainer.appendChild(menuContainerNode);
|
|
|
|
return new PopupMenu(sourceButton, menuContainerNode);
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_createMenuItems(menuContainerNode, menuItemContainer, term, reading) {
|
2021-03-25 23:22:34 +00:00
|
|
|
const {displayGenerator} = this._display;
|
2021-01-24 02:13:01 +00:00
|
|
|
let showIcons = false;
|
2021-03-25 23:22:34 +00:00
|
|
|
const currentItems = [...menuItemContainer.children];
|
2021-05-30 16:15:07 +00:00
|
|
|
for (const source of this._audioSources) {
|
2021-05-30 16:41:19 +00:00
|
|
|
const {index, name, nameIndex, nameUnique, isInOptions, downloadable} = source;
|
2021-04-04 20:22:35 +00:00
|
|
|
const entries = this._getMenuItemEntries(source, term, reading);
|
2021-02-15 19:31:16 +00:00
|
|
|
for (let i = 0, ii = entries.length; i < ii; ++i) {
|
2021-05-30 16:41:19 +00:00
|
|
|
const {valid, index: subIndex, name: subName} = entries[i];
|
2021-05-30 16:15:07 +00:00
|
|
|
let node = this._getOrCreateMenuItem(currentItems, index, subIndex);
|
2021-03-25 23:22:34 +00:00
|
|
|
if (node === null) {
|
|
|
|
node = displayGenerator.instantiateTemplate('audio-button-popup-menu-item');
|
|
|
|
}
|
2021-02-15 19:31:16 +00:00
|
|
|
|
2021-02-16 02:34:10 +00:00
|
|
|
const labelNode = node.querySelector('.popup-menu-item-audio-button .popup-menu-item-label');
|
2021-05-30 16:41:19 +00:00
|
|
|
let label = name;
|
|
|
|
if (!nameUnique) {
|
|
|
|
label = `${label} ${nameIndex + 1}`;
|
|
|
|
if (ii > 1) { label = `${label} -`; }
|
|
|
|
}
|
2021-02-15 19:31:16 +00:00
|
|
|
if (ii > 1) { label = `${label} ${i + 1}`; }
|
2021-05-30 16:41:19 +00:00
|
|
|
if (typeof subName === 'string' && subName.length > 0) { label += `: ${subName}`; }
|
2021-02-15 19:31:16 +00:00
|
|
|
labelNode.textContent = label;
|
|
|
|
|
2021-02-16 02:34:10 +00:00
|
|
|
const cardButton = node.querySelector('.popup-menu-item-set-primary-audio-button');
|
|
|
|
cardButton.hidden = !downloadable;
|
|
|
|
|
2021-01-24 02:13:01 +00:00
|
|
|
if (valid !== null) {
|
2021-02-16 02:34:10 +00:00
|
|
|
const icon = node.querySelector('.popup-menu-item-audio-button .popup-menu-item-icon');
|
2021-01-24 02:13:01 +00:00
|
|
|
icon.dataset.icon = valid ? 'checkmark' : 'cross';
|
|
|
|
showIcons = true;
|
|
|
|
}
|
2021-05-30 16:15:07 +00:00
|
|
|
node.dataset.index = `${index}`;
|
|
|
|
if (subIndex !== null) {
|
|
|
|
node.dataset.subIndex = `${subIndex}`;
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
2021-02-15 19:31:16 +00:00
|
|
|
node.dataset.valid = `${valid}`;
|
|
|
|
node.dataset.sourceInOptions = `${isInOptions}`;
|
2021-02-16 02:34:10 +00:00
|
|
|
node.dataset.downloadable = `${downloadable}`;
|
2021-02-15 19:31:16 +00:00
|
|
|
|
2021-03-25 23:22:34 +00:00
|
|
|
menuItemContainer.appendChild(node);
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 23:22:34 +00:00
|
|
|
for (const node of currentItems) {
|
|
|
|
const {parentNode} = node;
|
|
|
|
if (parentNode === null) { continue; }
|
|
|
|
parentNode.removeChild(node);
|
|
|
|
}
|
|
|
|
menuContainerNode.dataset.showIcons = `${showIcons}`;
|
|
|
|
}
|
2021-01-24 02:13:01 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
_getOrCreateMenuItem(currentItems, index, subIndex) {
|
2021-03-25 23:22:34 +00:00
|
|
|
index = `${index}`;
|
2021-05-30 16:15:07 +00:00
|
|
|
subIndex = `${subIndex !== null ? subIndex : 0}`;
|
2021-03-25 23:22:34 +00:00
|
|
|
for (let i = 0, ii = currentItems.length; i < ii; ++i) {
|
|
|
|
const node = currentItems[i];
|
2021-05-30 16:15:07 +00:00
|
|
|
if (index !== node.dataset.index) { continue; }
|
2021-02-16 02:34:10 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
let subIndex2 = node.dataset.subIndex;
|
|
|
|
if (typeof subIndex2 === 'undefined') { subIndex2 = '0'; }
|
|
|
|
if (subIndex !== subIndex2) { continue; }
|
2021-03-25 23:22:34 +00:00
|
|
|
|
|
|
|
currentItems.splice(i, 1);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
return null;
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_getMenuItemEntries(source, term, reading) {
|
|
|
|
const cacheEntry = this._getCacheItem(term, reading, false);
|
2021-02-16 02:34:10 +00:00
|
|
|
if (typeof cacheEntry !== 'undefined') {
|
|
|
|
const {sourceMap} = cacheEntry;
|
2021-05-30 16:15:07 +00:00
|
|
|
const sourceInfo = sourceMap.get(source.index);
|
2021-02-15 19:31:16 +00:00
|
|
|
if (typeof sourceInfo !== 'undefined') {
|
|
|
|
const {infoList} = sourceInfo;
|
|
|
|
if (infoList !== null) {
|
|
|
|
const ii = infoList.length;
|
|
|
|
if (ii === 0) {
|
|
|
|
return [{valid: false, index: null, name: null}];
|
|
|
|
}
|
2021-01-24 02:13:01 +00:00
|
|
|
|
2021-02-15 19:31:16 +00:00
|
|
|
const results = [];
|
|
|
|
for (let i = 0; i < ii; ++i) {
|
|
|
|
const {audio, audioResolved, info: {name}} = infoList[i];
|
|
|
|
const valid = audioResolved ? (audio !== null) : null;
|
|
|
|
const entry = {valid, index: i, name};
|
|
|
|
results.push(entry);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-15 19:31:16 +00:00
|
|
|
return [{valid: null, index: null, name: null}];
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
2021-02-16 02:34:10 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
_getPrimaryCardAudio(term, reading) {
|
|
|
|
const cacheEntry = this._getCacheItem(term, reading, false);
|
|
|
|
return typeof cacheEntry !== 'undefined' ? cacheEntry.primaryCardAudio : null;
|
|
|
|
}
|
2021-02-16 02:34:10 +00:00
|
|
|
|
2021-05-30 16:15:07 +00:00
|
|
|
_updateMenuPrimaryCardAudio(menuBodyNode, term, reading) {
|
|
|
|
const primaryCardAudio = this._getPrimaryCardAudio(term, reading);
|
|
|
|
const primaryCardAudioIndex = (primaryCardAudio !== null ? primaryCardAudio.index : null);
|
|
|
|
const primaryCardAudioSubIndex = (primaryCardAudio !== null ? primaryCardAudio.subIndex : null);
|
2021-02-16 02:34:10 +00:00
|
|
|
const itemGroups = menuBodyNode.querySelectorAll('.popup-menu-item-group');
|
|
|
|
for (const node of itemGroups) {
|
2021-05-30 17:29:55 +00:00
|
|
|
let {index, subIndex} = node.dataset;
|
|
|
|
index = Number.parseInt(index, 10);
|
|
|
|
subIndex = typeof subIndex === 'string' ? Number.parseInt(subIndex, 10) : null;
|
2021-05-30 16:15:07 +00:00
|
|
|
const isPrimaryCardAudio = (index === primaryCardAudioIndex && subIndex === primaryCardAudioSubIndex);
|
2021-02-16 02:34:10 +00:00
|
|
|
node.dataset.isPrimaryCardAudio = `${isPrimaryCardAudio}`;
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 23:22:34 +00:00
|
|
|
|
|
|
|
_updateOpenMenu() {
|
|
|
|
for (const menu of this._openMenus) {
|
|
|
|
const menuContainerNode = menu.containerNode;
|
2021-04-04 20:22:35 +00:00
|
|
|
const {term, reading} = menuContainerNode.dataset;
|
|
|
|
this._createMenuItems(menuContainerNode, menu.bodyNode, term, reading);
|
2021-03-25 23:22:34 +00:00
|
|
|
menu.updatePosition();
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 16:15:07 +00:00
|
|
|
|
|
|
|
_getSourceData(source) {
|
|
|
|
const {type, url, voice} = source;
|
|
|
|
return {type, url, voice};
|
|
|
|
}
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|