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-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-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get autoPlayAudioDelay() {
|
|
|
|
return this._autoPlayAudioDelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
set autoPlayAudioDelay(value) {
|
|
|
|
this._autoPlayAudioDelay = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare() {
|
|
|
|
this._audioSystem.prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateOptions(options) {
|
|
|
|
const data = document.documentElement.dataset;
|
|
|
|
data.audioEnabled = `${options.audio.enabled && options.audio.sources.length > 0}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupEntries() {
|
2021-02-16 02:46:55 +00:00
|
|
|
this._entriesToken = {};
|
2021-01-23 05:31:23 +00:00
|
|
|
this._cache.clear();
|
2021-01-18 05:16:40 +00:00
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupEntry(entry, definitionIndex) {
|
|
|
|
for (const button of entry.querySelectorAll('.action-play-audio')) {
|
|
|
|
const expressionIndex = this._getAudioPlayButtonExpressionIndex(button);
|
|
|
|
this._eventListeners.addEventListener(button, 'click', this._onAudioPlayButtonClick.bind(this, definitionIndex, expressionIndex), false);
|
2021-01-24 02:13:01 +00:00
|
|
|
this._eventListeners.addEventListener(button, 'contextmenu', this._onAudioPlayButtonContextMenu.bind(this, definitionIndex, expressionIndex), false);
|
|
|
|
this._eventListeners.addEventListener(button, 'menuClose', this._onAudioPlayMenuCloseClick.bind(this, definitionIndex, expressionIndex), false);
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setupEntriesComplete() {
|
2021-01-23 16:18:45 +00:00
|
|
|
const audioOptions = this._getAudioOptions();
|
|
|
|
if (!audioOptions.enabled || !audioOptions.autoPlay) { return; }
|
2021-01-18 05:16:40 +00:00
|
|
|
|
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
|
|
|
|
const definitions = this._display.definitions;
|
|
|
|
if (definitions.length === 0) { return; }
|
|
|
|
|
|
|
|
const firstDefinition = definitions[0];
|
|
|
|
if (firstDefinition.type === 'kanji') { return; }
|
|
|
|
|
|
|
|
const callback = () => {
|
|
|
|
this._autoPlayAudioTimer = null;
|
|
|
|
this.playAudio(0, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this._autoPlayAudioDelay > 0) {
|
|
|
|
this._autoPlayAudioTimer = setTimeout(callback, this._autoPlayAudioDelay);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-23 16:18:45 +00:00
|
|
|
async playAudio(definitionIndex, expressionIndex, sources=null, sourceDetailsMap=null) {
|
2021-01-18 05:16:40 +00:00
|
|
|
this.stopAudio();
|
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
|
2021-01-23 16:18:45 +00:00
|
|
|
const expressionReading = this._getExpressionAndReading(definitionIndex, expressionIndex);
|
2021-02-16 02:46:55 +00:00
|
|
|
if (expressionReading === null) {
|
|
|
|
return {audio: null, source: null, valid: false};
|
|
|
|
}
|
2021-01-18 05:16:40 +00:00
|
|
|
|
2021-01-24 02:13:01 +00:00
|
|
|
const buttons = this._getAudioPlayButtons(definitionIndex, expressionIndex);
|
|
|
|
|
2021-01-23 16:18:45 +00:00
|
|
|
const {expression, reading} = expressionReading;
|
|
|
|
const audioOptions = this._getAudioOptions();
|
2021-01-24 03:46:00 +00:00
|
|
|
const {textToSpeechVoice, customSourceUrl, customSourceType, volume} = audioOptions;
|
2021-01-23 16:18:45 +00:00
|
|
|
if (!Array.isArray(sources)) {
|
|
|
|
({sources} = audioOptions);
|
|
|
|
}
|
|
|
|
if (!(sourceDetailsMap instanceof Map)) {
|
|
|
|
sourceDetailsMap = null;
|
|
|
|
}
|
2021-01-18 05:16:40 +00:00
|
|
|
|
|
|
|
const progressIndicatorVisible = this._display.progressIndicatorVisible;
|
|
|
|
const overrideToken = progressIndicatorVisible.setOverride(true);
|
|
|
|
try {
|
|
|
|
// Create audio
|
|
|
|
let audio;
|
2021-01-23 05:31:23 +00:00
|
|
|
let title;
|
2021-02-16 02:46:55 +00:00
|
|
|
let source = null;
|
2021-01-24 03:46:00 +00:00
|
|
|
const info = await this._createExpressionAudio(sources, sourceDetailsMap, expression, reading, {textToSpeechVoice, customSourceUrl, customSourceType});
|
2021-02-16 02:46:55 +00:00
|
|
|
const valid = (info !== null);
|
|
|
|
if (valid) {
|
2021-01-23 05:31:23 +00:00
|
|
|
({audio, source} = info);
|
2021-01-19 03:01:08 +00:00
|
|
|
const sourceIndex = sources.indexOf(source);
|
2021-01-23 05:31:23 +00:00
|
|
|
title = `From source ${1 + sourceIndex}: ${source}`;
|
|
|
|
} else {
|
2021-01-18 05:16:40 +00:00
|
|
|
audio = this._audioSystem.getFallbackAudio();
|
2021-01-23 05:31:23 +00:00
|
|
|
title = 'Could not find audio';
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop any currently playing audio
|
|
|
|
this.stopAudio();
|
|
|
|
|
|
|
|
// Update details
|
2021-01-24 01:24:52 +00:00
|
|
|
const potentialAvailableAudioCount = this._getPotentialAvailableAudioCount(expression, reading);
|
2021-01-24 02:13:01 +00:00
|
|
|
for (const button of buttons) {
|
2021-01-18 05:16:40 +00:00
|
|
|
const titleDefault = button.dataset.titleDefault || '';
|
2021-01-23 05:31:23 +00:00
|
|
|
button.title = `${titleDefault}\n${title}`;
|
2021-01-24 01:24:52 +00:00
|
|
|
this._updateAudioPlayButtonBadge(button, potentialAvailableAudioCount);
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Play
|
|
|
|
audio.currentTime = 0;
|
|
|
|
audio.volume = Number.isFinite(volume) ? Math.max(0.0, Math.min(1.0, volume / 100.0)) : 1.0;
|
|
|
|
|
|
|
|
const playPromise = audio.play();
|
|
|
|
this._audioPlaying = audio;
|
|
|
|
|
|
|
|
if (typeof playPromise !== 'undefined') {
|
|
|
|
try {
|
|
|
|
await playPromise;
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 02:46:55 +00:00
|
|
|
|
|
|
|
return {audio, source, valid};
|
2021-01-18 05:16:40 +00:00
|
|
|
} finally {
|
|
|
|
progressIndicatorVisible.clearOverride(overrideToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 02:34:10 +00:00
|
|
|
getPrimaryCardAudio(expression, reading) {
|
|
|
|
const cacheEntry = this._getCacheItem(expression, reading, false);
|
|
|
|
const primaryCardAudio = typeof cacheEntry !== 'undefined' ? cacheEntry.primaryCardAudio : null;
|
|
|
|
return primaryCardAudio;
|
|
|
|
}
|
|
|
|
|
2021-01-18 05:16:40 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_onAudioPlayButtonClick(definitionIndex, expressionIndex, e) {
|
|
|
|
e.preventDefault();
|
2021-01-24 02:13:01 +00:00
|
|
|
|
|
|
|
if (e.shiftKey) {
|
|
|
|
this._showAudioMenu(e.currentTarget, definitionIndex, expressionIndex);
|
|
|
|
} else {
|
|
|
|
this.playAudio(definitionIndex, expressionIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onAudioPlayButtonContextMenu(definitionIndex, expressionIndex, e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this._showAudioMenu(e.currentTarget, definitionIndex, expressionIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onAudioPlayMenuCloseClick(definitionIndex, expressionIndex, 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-02-16 02:46:55 +00:00
|
|
|
this._playAudioFromSource(definitionIndex, expressionIndex, item);
|
2021-02-16 02:34:10 +00:00
|
|
|
break;
|
|
|
|
case 'setPrimaryAudio':
|
|
|
|
e.preventDefault();
|
|
|
|
this._setPrimaryAudio(definitionIndex, expressionIndex, item, menu, true);
|
2021-01-24 02:13:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 02:34:10 +00:00
|
|
|
_getCacheItem(expression, reading, create) {
|
|
|
|
const key = this._getExpressionReadingKey(expression, reading);
|
|
|
|
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');
|
|
|
|
if (group === null) { return null; }
|
|
|
|
|
|
|
|
let {source, index} = group.dataset;
|
|
|
|
if (typeof index !== 'undefined') {
|
|
|
|
index = Number.parseInt(index, 10);
|
|
|
|
}
|
|
|
|
const hasIndex = (Number.isFinite(index) && Math.floor(index) === index);
|
|
|
|
if (!hasIndex) {
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
return {source, index, hasIndex};
|
|
|
|
}
|
|
|
|
|
2021-02-16 02:46:55 +00:00
|
|
|
async _playAudioFromSource(definitionIndex, expressionIndex, item) {
|
2021-02-16 02:34:10 +00:00
|
|
|
const sourceInfo = this._getMenuItemSourceInfo(item);
|
|
|
|
if (sourceInfo === null) { return; }
|
|
|
|
|
|
|
|
const {source, index, hasIndex} = sourceInfo;
|
|
|
|
const sourceDetailsMap = hasIndex ? new Map([[source, {start: index, end: index + 1}]]) : null;
|
|
|
|
|
2021-02-16 02:46:55 +00:00
|
|
|
try {
|
|
|
|
const token = this._entriesToken;
|
|
|
|
const {valid} = await this.playAudio(definitionIndex, expressionIndex, [source], sourceDetailsMap);
|
|
|
|
if (valid && token === this._entriesToken) {
|
|
|
|
this._setPrimaryAudio(definitionIndex, expressionIndex, item, null, false);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
2021-02-16 02:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_setPrimaryAudio(definitionIndex, expressionIndex, item, menu, canToggleOff) {
|
|
|
|
const sourceInfo = this._getMenuItemSourceInfo(item);
|
|
|
|
if (sourceInfo === null) { return; }
|
|
|
|
|
|
|
|
const {source, index} = sourceInfo;
|
|
|
|
if (!this._sourceIsDownloadable(source)) { return; }
|
|
|
|
|
|
|
|
const expressionReading = this._getExpressionAndReading(definitionIndex, expressionIndex);
|
|
|
|
if (expressionReading === null) { return; }
|
|
|
|
|
|
|
|
const {expression, reading} = expressionReading;
|
|
|
|
const cacheEntry = this._getCacheItem(expression, reading, true);
|
|
|
|
|
|
|
|
let {primaryCardAudio} = cacheEntry;
|
|
|
|
primaryCardAudio = (!canToggleOff || primaryCardAudio === null || primaryCardAudio.source !== source || primaryCardAudio.index !== index) ? {source, index} : null;
|
|
|
|
cacheEntry.primaryCardAudio = primaryCardAudio;
|
|
|
|
|
2021-02-16 02:46:55 +00:00
|
|
|
if (menu !== null) {
|
|
|
|
this._updateMenuPrimaryCardAudio(menu.bodyNode, expression, reading);
|
|
|
|
}
|
2021-02-16 02:34:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 05:16:40 +00:00
|
|
|
_getAudioPlayButtonExpressionIndex(button) {
|
2021-02-27 21:32:44 +00:00
|
|
|
const expressionNode = button.closest('.expression');
|
2021-01-18 05:16:40 +00:00
|
|
|
if (expressionNode !== null) {
|
|
|
|
const expressionIndex = parseInt(expressionNode.dataset.index, 10);
|
|
|
|
if (Number.isFinite(expressionIndex)) { return expressionIndex; }
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getAudioPlayButtons(definitionIndex, expressionIndex) {
|
|
|
|
const results = [];
|
|
|
|
const {definitionNodes} = this._display;
|
|
|
|
if (definitionIndex >= 0 && definitionIndex < definitionNodes.length) {
|
|
|
|
const node = definitionNodes[definitionIndex];
|
|
|
|
const button1 = (expressionIndex === 0 ? node.querySelector('.action-play-audio') : null);
|
2021-02-27 21:32:44 +00:00
|
|
|
const button2 = node.querySelector(`.expression:nth-of-type(${expressionIndex + 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-01-23 16:18:45 +00:00
|
|
|
async _createExpressionAudio(sources, sourceDetailsMap, expression, reading, details) {
|
2021-02-16 02:34:10 +00:00
|
|
|
const {sourceMap} = this._getCacheItem(expression, reading, true);
|
2021-01-23 03:10:27 +00:00
|
|
|
|
|
|
|
for (let i = 0, ii = sources.length; i < ii; ++i) {
|
|
|
|
const source = sources[i];
|
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;
|
|
|
|
let sourceInfo = sourceMap.get(source);
|
|
|
|
if (typeof sourceInfo === 'undefined') {
|
2021-01-23 05:31:23 +00:00
|
|
|
infoListPromise = this._getExpressionAudioInfoList(source, expression, reading, details);
|
2021-01-23 16:18:45 +00:00
|
|
|
sourceInfo = {infoListPromise, infoList: null};
|
|
|
|
sourceMap.set(source, 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-01-23 16:18:45 +00:00
|
|
|
let start = 0;
|
|
|
|
let end = infoList.length;
|
|
|
|
|
|
|
|
if (sourceDetailsMap !== null) {
|
|
|
|
const sourceDetails = sourceDetailsMap.get(source);
|
|
|
|
if (typeof sourceDetails !== 'undefined') {
|
|
|
|
const {start: start2, end: end2} = sourceDetails;
|
|
|
|
if (this._isInteger(start2)) { start = this._clamp(start2, start, end); }
|
|
|
|
if (this._isInteger(end2)) { end = this._clamp(end2, start, end); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:22:34 +00:00
|
|
|
const {result, cacheUpdated: cacheUpdated2} = await this._createAudioFromInfoList(source, infoList, start, end);
|
|
|
|
if (cacheUpdated || cacheUpdated2) { this._updateOpenMenu(); }
|
|
|
|
if (result !== null) { return result; }
|
2021-01-23 16:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _createAudioFromInfoList(source, infoList, start, end) {
|
2021-03-25 23:22:34 +00:00
|
|
|
let result = null;
|
|
|
|
let 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-03-25 23:22:34 +00:00
|
|
|
cacheUpdated = true;
|
|
|
|
|
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) {
|
|
|
|
result = {audio, source, infoListIndex: i};
|
|
|
|
break;
|
|
|
|
}
|
2021-01-23 16:18:45 +00:00
|
|
|
}
|
2021-03-25 23:22:34 +00:00
|
|
|
return {result, cacheUpdated};
|
2021-01-23 03:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _createAudioFromInfo(info, source) {
|
|
|
|
switch (info.type) {
|
|
|
|
case 'url':
|
|
|
|
return await this._audioSystem.createAudio(info.url, source);
|
|
|
|
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
|
|
|
|
|
|
|
async _getExpressionAudioInfoList(source, expression, reading, details) {
|
2021-02-14 20:53:35 +00:00
|
|
|
const infoList = await yomichan.api.getExpressionAudioInfoList(source, expression, reading, details);
|
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
|
|
|
|
|
|
|
_getExpressionAndReading(definitionIndex, expressionIndex) {
|
|
|
|
const {definitions} = this._display;
|
|
|
|
if (definitionIndex < 0 || definitionIndex >= definitions.length) { return null; }
|
|
|
|
|
|
|
|
const definition = definitions[definitionIndex];
|
|
|
|
if (definition.type === 'kanji') { return null; }
|
|
|
|
|
|
|
|
const {expressions} = definition;
|
|
|
|
if (expressionIndex < 0 || expressionIndex >= expressions.length) { return null; }
|
|
|
|
|
|
|
|
const {expression, reading} = expressions[expressionIndex];
|
|
|
|
return {expression, reading};
|
|
|
|
}
|
|
|
|
|
|
|
|
_getExpressionReadingKey(expression, reading) {
|
|
|
|
return JSON.stringify([expression, reading]);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getAudioOptions() {
|
|
|
|
return this._display.getOptions().audio;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isInteger(value) {
|
|
|
|
return (
|
|
|
|
typeof value === 'number' &&
|
|
|
|
Number.isFinite(value) &&
|
|
|
|
Math.floor(value) === value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_clamp(value, min, max) {
|
|
|
|
return Math.max(min, Math.min(max, value));
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_getPotentialAvailableAudioCount(expression, reading) {
|
2021-02-16 02:34:10 +00:00
|
|
|
const cacheEntry = this._getCacheItem(expression, reading, false);
|
|
|
|
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
|
|
|
|
|
|
|
_showAudioMenu(button, definitionIndex, expressionIndex) {
|
|
|
|
const expressionReading = this._getExpressionAndReading(definitionIndex, expressionIndex);
|
|
|
|
if (expressionReading === null) { return; }
|
|
|
|
|
|
|
|
const {expression, reading} = expressionReading;
|
|
|
|
const popupMenu = this._createMenu(button, expression, 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-02-15 19:31:16 +00:00
|
|
|
_getAudioSources(audioOptions) {
|
|
|
|
const {sources, textToSpeechVoice, customSourceUrl} = audioOptions;
|
|
|
|
const ttsSupported = (textToSpeechVoice.length > 0);
|
|
|
|
const customSupported = (customSourceUrl.length > 0);
|
|
|
|
|
2021-01-24 02:13:01 +00:00
|
|
|
const sourceIndexMap = new Map();
|
2021-02-15 19:31:16 +00:00
|
|
|
const optionsSourcesCount = sources.length;
|
|
|
|
for (let i = 0; i < optionsSourcesCount; ++i) {
|
2021-01-24 02:13:01 +00:00
|
|
|
sourceIndexMap.set(sources[i], i);
|
|
|
|
}
|
|
|
|
|
2021-02-15 19:31:16 +00:00
|
|
|
const rawSources = [
|
|
|
|
['jpod101', 'JapanesePod101', true],
|
|
|
|
['jpod101-alternate', 'JapanesePod101 (Alternate)', true],
|
|
|
|
['jisho', 'Jisho.org', true],
|
|
|
|
['text-to-speech', 'Text-to-speech', ttsSupported],
|
|
|
|
['text-to-speech-reading', 'Text-to-speech (Kana reading)', ttsSupported],
|
|
|
|
['custom', 'Custom', customSupported]
|
|
|
|
];
|
|
|
|
|
|
|
|
const results = [];
|
|
|
|
for (const [source, displayName, supported] of rawSources) {
|
|
|
|
if (!supported) { continue; }
|
2021-02-16 02:34:10 +00:00
|
|
|
const downloadable = this._sourceIsDownloadable(source);
|
2021-01-24 02:13:01 +00:00
|
|
|
let optionsIndex = sourceIndexMap.get(source);
|
2021-02-15 19:31:16 +00:00
|
|
|
const isInOptions = typeof optionsIndex !== 'undefined';
|
|
|
|
if (!isInOptions) {
|
|
|
|
optionsIndex = optionsSourcesCount;
|
|
|
|
}
|
|
|
|
results.push({
|
|
|
|
source,
|
|
|
|
displayName,
|
|
|
|
index: results.length,
|
|
|
|
optionsIndex,
|
2021-02-16 02:34:10 +00:00
|
|
|
isInOptions,
|
|
|
|
downloadable
|
2021-02-15 19:31:16 +00:00
|
|
|
});
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort according to source order in options
|
2021-02-15 19:31:16 +00:00
|
|
|
results.sort((a, b) => {
|
|
|
|
const i = a.optionsIndex - b.optionsIndex;
|
|
|
|
return i !== 0 ? i : a.index - b.index;
|
2021-01-24 02:13:01 +00:00
|
|
|
});
|
|
|
|
|
2021-02-15 19:31:16 +00:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createMenu(sourceButton, expression, reading) {
|
|
|
|
// 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');
|
|
|
|
menuContainerNode.dataset.expression = expression;
|
|
|
|
menuContainerNode.dataset.reading = reading;
|
2021-02-15 19:31:16 +00:00
|
|
|
|
|
|
|
// Set up items based on options and cache data
|
2021-03-25 23:22:34 +00:00
|
|
|
this._createMenuItems(menuContainerNode, menuBodyNode, expression, reading);
|
|
|
|
|
|
|
|
// Update primary card audio display
|
|
|
|
this._updateMenuPrimaryCardAudio(menuBodyNode, expression, reading);
|
|
|
|
|
|
|
|
// Create popup menu
|
|
|
|
this._menuContainer.appendChild(menuContainerNode);
|
|
|
|
return new PopupMenu(sourceButton, menuContainerNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
_createMenuItems(menuContainerNode, menuItemContainer, expression, reading) {
|
|
|
|
const sources = this._getAudioSources(this._getAudioOptions());
|
|
|
|
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-02-16 02:34:10 +00:00
|
|
|
for (const {source, displayName, isInOptions, downloadable} of sources) {
|
2021-02-15 19:31:16 +00:00
|
|
|
const entries = this._getMenuItemEntries(source, expression, reading);
|
|
|
|
for (let i = 0, ii = entries.length; i < ii; ++i) {
|
|
|
|
const {valid, index, name} = entries[i];
|
2021-03-25 23:22:34 +00:00
|
|
|
let node = this._getOrCreateMenuItem(currentItems, source, index);
|
|
|
|
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-02-15 19:31:16 +00:00
|
|
|
let label = displayName;
|
|
|
|
if (ii > 1) { label = `${label} ${i + 1}`; }
|
|
|
|
if (typeof name === 'string' && name.length > 0) { label += `: ${name}`; }
|
|
|
|
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-02-15 19:31:16 +00:00
|
|
|
node.dataset.source = source;
|
2021-01-24 02:13:01 +00:00
|
|
|
if (index !== null) {
|
2021-02-15 19:31:16 +00:00
|
|
|
node.dataset.index = `${index}`;
|
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-03-25 23:22:34 +00:00
|
|
|
_getOrCreateMenuItem(currentItems, source, index) {
|
|
|
|
if (index === null) { index = 0; }
|
|
|
|
index = `${index}`;
|
|
|
|
for (let i = 0, ii = currentItems.length; i < ii; ++i) {
|
|
|
|
const node = currentItems[i];
|
|
|
|
if (source !== node.dataset.source) { continue; }
|
2021-02-16 02:34:10 +00:00
|
|
|
|
2021-03-25 23:22:34 +00:00
|
|
|
let index2 = node.dataset.index;
|
|
|
|
if (typeof index2 === 'undefined') { index2 = '0'; }
|
|
|
|
if (index !== index2) { continue; }
|
|
|
|
|
|
|
|
currentItems.splice(i, 1);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
return null;
|
2021-01-24 02:13:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 19:31:16 +00:00
|
|
|
_getMenuItemEntries(source, expression, reading) {
|
2021-02-16 02:34:10 +00:00
|
|
|
const cacheEntry = this._getCacheItem(expression, reading, false);
|
|
|
|
if (typeof cacheEntry !== 'undefined') {
|
|
|
|
const {sourceMap} = cacheEntry;
|
2021-02-15 19:31:16 +00:00
|
|
|
const sourceInfo = sourceMap.get(source);
|
|
|
|
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
|
|
|
|
|
|
|
_updateMenuPrimaryCardAudio(menuBodyNode, expression, reading) {
|
|
|
|
const primaryCardAudio = this.getPrimaryCardAudio(expression, reading);
|
|
|
|
const {source: primaryCardAudioSource, index: primaryCardAudioIndex} = (primaryCardAudio !== null ? primaryCardAudio : {source: null, index: -1});
|
|
|
|
|
|
|
|
const itemGroups = menuBodyNode.querySelectorAll('.popup-menu-item-group');
|
|
|
|
let sourceIndex = 0;
|
|
|
|
let sourcePre = null;
|
|
|
|
for (const node of itemGroups) {
|
|
|
|
const {source} = node.dataset;
|
|
|
|
if (source !== sourcePre) {
|
|
|
|
sourcePre = source;
|
|
|
|
sourceIndex = 0;
|
|
|
|
} else {
|
|
|
|
++sourceIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isPrimaryCardAudio = (source === primaryCardAudioSource && sourceIndex === primaryCardAudioIndex);
|
|
|
|
node.dataset.isPrimaryCardAudio = `${isPrimaryCardAudio}`;
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 23:22:34 +00:00
|
|
|
|
|
|
|
_updateOpenMenu() {
|
|
|
|
for (const menu of this._openMenus) {
|
|
|
|
const menuContainerNode = menu.containerNode;
|
|
|
|
const {expression, reading} = menuContainerNode.dataset;
|
|
|
|
this._createMenuItems(menuContainerNode, menu.bodyNode, expression, reading);
|
|
|
|
menu.updatePosition();
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|