2020-04-11 18:23:49 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2020-2022 Yomichan Authors
|
2020-04-11 18:23:49 +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
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-03-14 22:41:15 +00:00
|
|
|
/* global
|
2021-09-04 02:33:58 +00:00
|
|
|
* StringUtil
|
2021-03-14 22:41:15 +00:00
|
|
|
*/
|
|
|
|
|
2022-03-15 02:32:08 +00:00
|
|
|
/**
|
|
|
|
* A callback used when a media file has been loaded.
|
|
|
|
* @callback DisplayContentManager.OnLoadCallback
|
|
|
|
* @param {string} url The URL of the media that was loaded.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback used when a media file should be unloaded.
|
|
|
|
* @callback DisplayContentManager.OnUnloadCallback
|
|
|
|
* @param {boolean} fullyLoaded Whether or not the media was fully loaded.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The content manager which is used when generating HTML display content.
|
|
|
|
*/
|
2022-03-15 01:42:33 +00:00
|
|
|
class DisplayContentManager {
|
2022-03-15 02:32:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a new instance of the class.
|
2022-03-17 23:01:59 +00:00
|
|
|
* @param {Display} display The display instance that owns this object.
|
2022-03-15 02:32:08 +00:00
|
|
|
*/
|
2022-03-17 23:01:59 +00:00
|
|
|
constructor(display) {
|
|
|
|
this._display = display;
|
2020-04-11 18:23:49 +00:00
|
|
|
this._token = {};
|
|
|
|
this._mediaCache = new Map();
|
|
|
|
this._loadMediaData = [];
|
2022-03-17 23:01:59 +00:00
|
|
|
this._eventListeners = new EventListenerCollection();
|
2020-04-11 18:23:49 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 02:32:08 +00:00
|
|
|
/**
|
|
|
|
* Attempts to load the media file from a given dictionary.
|
|
|
|
* @param {string} path The path to the media file in the dictionary.
|
|
|
|
* @param {string} dictionary The name of the dictionary.
|
|
|
|
* @param {DisplayContentManager.OnLoadCallback} onLoad The callback that is executed if the media was loaded successfully.
|
|
|
|
* No assumptions should be made about the synchronicity of this callback.
|
|
|
|
* @param {DisplayContentManager.OnUnloadCallback} onUnload The callback that is executed when the media should be unloaded.
|
|
|
|
*/
|
|
|
|
loadMedia(path, dictionary, onLoad, onUnload) {
|
|
|
|
this._loadMedia(path, dictionary, onLoad, onUnload);
|
2020-04-11 18:23:49 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 02:32:08 +00:00
|
|
|
/**
|
|
|
|
* Unloads all media that has been loaded.
|
|
|
|
*/
|
2020-04-11 18:23:49 +00:00
|
|
|
unloadAll() {
|
|
|
|
for (const {onUnload, loaded} of this._loadMediaData) {
|
|
|
|
if (typeof onUnload === 'function') {
|
|
|
|
onUnload(loaded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._loadMediaData = [];
|
|
|
|
|
|
|
|
for (const map of this._mediaCache.values()) {
|
|
|
|
for (const {url} of map.values()) {
|
|
|
|
if (url !== null) {
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._mediaCache.clear();
|
|
|
|
|
|
|
|
this._token = {};
|
2022-03-17 23:01:59 +00:00
|
|
|
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up attributes and events for a link element.
|
|
|
|
* @param {Element} element The link element.
|
|
|
|
* @param {string} href The URL.
|
|
|
|
* @param {boolean} internal Whether or not the URL is an internal or external link.
|
|
|
|
*/
|
|
|
|
prepareLink(element, href, internal) {
|
|
|
|
element.href = href;
|
|
|
|
if (!internal) {
|
|
|
|
element.target = '_blank';
|
|
|
|
element.rel = 'noreferrer noopener';
|
|
|
|
}
|
|
|
|
this._eventListeners.addEventListener(element, 'click', this._onLinkClick.bind(this));
|
2020-04-11 18:23:49 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 02:32:08 +00:00
|
|
|
async _loadMedia(path, dictionary, onLoad, onUnload) {
|
|
|
|
const token = this._token;
|
|
|
|
const data = {onUnload, loaded: false};
|
|
|
|
|
|
|
|
this._loadMediaData.push(data);
|
|
|
|
|
|
|
|
const media = await this._getMedia(path, dictionary);
|
|
|
|
if (token !== this._token) { return; }
|
|
|
|
|
|
|
|
onLoad(media.url);
|
|
|
|
data.loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _getMedia(path, dictionary) {
|
2020-04-11 18:23:49 +00:00
|
|
|
let cachedData;
|
2021-03-15 02:51:20 +00:00
|
|
|
let dictionaryCache = this._mediaCache.get(dictionary);
|
2020-04-11 18:23:49 +00:00
|
|
|
if (typeof dictionaryCache !== 'undefined') {
|
|
|
|
cachedData = dictionaryCache.get(path);
|
|
|
|
} else {
|
|
|
|
dictionaryCache = new Map();
|
2021-03-15 02:51:20 +00:00
|
|
|
this._mediaCache.set(dictionary, dictionaryCache);
|
2020-04-11 18:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof cachedData === 'undefined') {
|
|
|
|
cachedData = {
|
|
|
|
promise: null,
|
|
|
|
data: null,
|
|
|
|
url: null
|
|
|
|
};
|
|
|
|
dictionaryCache.set(path, cachedData);
|
2021-03-15 02:51:20 +00:00
|
|
|
cachedData.promise = this._getMediaData(path, dictionary, cachedData);
|
2020-04-11 18:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cachedData.promise;
|
|
|
|
}
|
|
|
|
|
2021-03-15 02:51:20 +00:00
|
|
|
async _getMediaData(path, dictionary, cachedData) {
|
2020-04-11 18:23:49 +00:00
|
|
|
const token = this._token;
|
2021-03-15 02:51:20 +00:00
|
|
|
const data = (await yomichan.api.getMedia([{path, dictionary}]))[0];
|
2020-04-11 18:23:49 +00:00
|
|
|
if (token === this._token && data !== null) {
|
2021-09-04 02:33:58 +00:00
|
|
|
const buffer = StringUtil.base64ToArrayBuffer(data.content);
|
|
|
|
const blob = new Blob([buffer], {type: data.mediaType});
|
2020-04-11 18:23:49 +00:00
|
|
|
const url = URL.createObjectURL(blob);
|
|
|
|
cachedData.data = data;
|
|
|
|
cachedData.url = url;
|
|
|
|
}
|
|
|
|
return cachedData;
|
|
|
|
}
|
2022-03-17 23:01:59 +00:00
|
|
|
|
|
|
|
_onLinkClick(e) {
|
|
|
|
const {href} = e.currentTarget;
|
|
|
|
if (typeof href !== 'string') { return; }
|
|
|
|
|
|
|
|
const baseUrl = new URL(location.href);
|
|
|
|
const url = new URL(href, baseUrl);
|
|
|
|
const internal = (url.protocol === baseUrl.protocol && url.host === baseUrl.host);
|
|
|
|
if (!internal) { return; }
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const params = {};
|
|
|
|
for (const [key, value] of url.searchParams.entries()) {
|
|
|
|
params[key] = value;
|
|
|
|
}
|
|
|
|
this._display.setContent({
|
|
|
|
historyMode: 'new',
|
|
|
|
focus: false,
|
|
|
|
params,
|
|
|
|
state: null,
|
|
|
|
content: null
|
|
|
|
});
|
|
|
|
}
|
2020-04-11 18:23:49 +00:00
|
|
|
}
|