summaryrefslogtreecommitdiff
path: root/ext/js/display/display-content-manager.js
blob: fb2e7db5f88086f028c2c66997bc8f17e1fabf52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2023  Yomitan Authors
 * Copyright (C) 2020-2022  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/>.
 */

import {EventListenerCollection} from '../core.js';
import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js';
import {yomitan} from '../yomitan.js';

/**
 * 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.
 */
export class DisplayContentManager {
    /**
     * Creates a new instance of the class.
     * @param {Display} display The display instance that owns this object.
     */
    constructor(display) {
        this._display = display;
        this._token = {};
        this._mediaCache = new Map();
        this._loadMediaData = [];
        this._eventListeners = new EventListenerCollection();
    }

    /**
     * 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);
    }

    /**
     * Unloads all media that has been loaded.
     */
    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 = {};

        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));
    }

    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) {
        let cachedData;
        let dictionaryCache = this._mediaCache.get(dictionary);
        if (typeof dictionaryCache !== 'undefined') {
            cachedData = dictionaryCache.get(path);
        } else {
            dictionaryCache = new Map();
            this._mediaCache.set(dictionary, dictionaryCache);
        }

        if (typeof cachedData === 'undefined') {
            cachedData = {
                promise: null,
                data: null,
                url: null
            };
            dictionaryCache.set(path, cachedData);
            cachedData.promise = this._getMediaData(path, dictionary, cachedData);
        }

        return cachedData.promise;
    }

    async _getMediaData(path, dictionary, cachedData) {
        const token = this._token;
        const data = (await yomitan.api.getMedia([{path, dictionary}]))[0];
        if (token === this._token && data !== null) {
            const buffer = ArrayBufferUtil.base64ToArrayBuffer(data.content);
            const blob = new Blob([buffer], {type: data.mediaType});
            const url = URL.createObjectURL(blob);
            cachedData.data = data;
            cachedData.url = url;
        }
        return cachedData;
    }

    _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
        });
    }
}