Make MediaUtil a static class (#1525)

This commit is contained in:
toasted-nutbread 2021-03-14 18:04:19 -04:00 committed by GitHub
parent 06b02c3cf2
commit 52a4d874ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 16 deletions

View File

@ -47,13 +47,11 @@ class Backend {
}); });
this._anki = new AnkiConnect(); this._anki = new AnkiConnect();
this._mecab = new Mecab(); this._mecab = new Mecab();
this._mediaUtil = new MediaUtil();
this._clipboardReader = new ClipboardReader({ this._clipboardReader = new ClipboardReader({
// eslint-disable-next-line no-undef // eslint-disable-next-line no-undef
document: (typeof document === 'object' && document !== null ? document : null), document: (typeof document === 'object' && document !== null ? document : null),
pasteTargetSelector: '#clipboard-paste-target', pasteTargetSelector: '#clipboard-paste-target',
imagePasteTargetSelector: '#clipboard-image-paste-target', imagePasteTargetSelector: '#clipboard-image-paste-target'
mediaUtil: this._mediaUtil
}); });
this._clipboardMonitor = new ClipboardMonitor({ this._clipboardMonitor = new ClipboardMonitor({
japaneseUtil: this._japaneseUtil, japaneseUtil: this._japaneseUtil,
@ -1750,7 +1748,7 @@ class Backend {
return null; return null;
} }
let extension = this._mediaUtil.getFileExtensionFromAudioMediaType(contentType); let extension = MediaUtil.getFileExtensionFromAudioMediaType(contentType);
if (extension === null) { extension = '.mp3'; } if (extension === null) { extension = '.mp3'; }
let fileName = this._generateAnkiNoteMediaFileName('yomichan_audio', extension, timestamp, definitionDetails); let fileName = this._generateAnkiNoteMediaFileName('yomichan_audio', extension, timestamp, definitionDetails);
fileName = fileName.replace(/\]/g, ''); fileName = fileName.replace(/\]/g, '');
@ -1764,7 +1762,7 @@ class Backend {
const dataUrl = await this._getScreenshot(tabId, frameId, format, quality); const dataUrl = await this._getScreenshot(tabId, frameId, format, quality);
const {mediaType, data} = this._getDataUrlInfo(dataUrl); const {mediaType, data} = this._getDataUrlInfo(dataUrl);
const extension = this._mediaUtil.getFileExtensionFromImageMediaType(mediaType); const extension = MediaUtil.getFileExtensionFromImageMediaType(mediaType);
if (extension === null) { if (extension === null) {
throw new Error('Unknown media type for screenshot image'); throw new Error('Unknown media type for screenshot image');
} }
@ -1782,7 +1780,7 @@ class Backend {
} }
const {mediaType, data} = this._getDataUrlInfo(dataUrl); const {mediaType, data} = this._getDataUrlInfo(dataUrl);
const extension = this._mediaUtil.getFileExtensionFromImageMediaType(mediaType); const extension = MediaUtil.getFileExtensionFromImageMediaType(mediaType);
if (extension === null) { if (extension === null) {
throw new Error('Unknown media type for clipboard image'); throw new Error('Unknown media type for clipboard image');
} }

View File

@ -15,6 +15,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/* global
* MediaUtil
*/
/** /**
* Class which can read text and images from the clipboard. * Class which can read text and images from the clipboard.
*/ */
@ -25,14 +29,13 @@ class ClipboardReader {
* @param pasteTargetSelector The selector for the paste target element. * @param pasteTargetSelector The selector for the paste target element.
* @param imagePasteTargetSelector The selector for the image paste target element. * @param imagePasteTargetSelector The selector for the image paste target element.
*/ */
constructor({document=null, pasteTargetSelector=null, imagePasteTargetSelector=null, mediaUtil=null}) { constructor({document=null, pasteTargetSelector=null, imagePasteTargetSelector=null}) {
this._document = document; this._document = document;
this._browser = null; this._browser = null;
this._pasteTarget = null; this._pasteTarget = null;
this._pasteTargetSelector = pasteTargetSelector; this._pasteTargetSelector = pasteTargetSelector;
this._imagePasteTarget = null; this._imagePasteTarget = null;
this._imagePasteTargetSelector = imagePasteTargetSelector; this._imagePasteTargetSelector = imagePasteTargetSelector;
this._mediaUtil = mediaUtil;
} }
/** /**
@ -107,7 +110,6 @@ class ClipboardReader {
// See browser-specific notes in getText // See browser-specific notes in getText
if ( if (
this._isFirefox() && this._isFirefox() &&
this._mediaUtil !== null &&
typeof navigator.clipboard !== 'undefined' && typeof navigator.clipboard !== 'undefined' &&
typeof navigator.clipboard.read === 'function' typeof navigator.clipboard.read === 'function'
) { ) {
@ -120,7 +122,7 @@ class ClipboardReader {
} }
for (const file of files) { for (const file of files) {
if (this._mediaUtil.getFileExtensionFromImageMediaType(file.type) !== null) { if (MediaUtil.getFileExtensionFromImageMediaType(file.type) !== null) {
return await this._readFileAsDataURL(file); return await this._readFileAsDataURL(file);
} }
} }

View File

@ -25,7 +25,6 @@ class DictionaryImporter {
constructor() { constructor() {
this._schemas = new Map(); this._schemas = new Map();
this._jsonSchemaValidator = new JsonSchemaValidator(); this._jsonSchemaValidator = new JsonSchemaValidator();
this._mediaUtil = new MediaUtil();
} }
async importDictionary(dictionaryDatabase, archiveSource, details, onProgress) { async importDictionary(dictionaryDatabase, archiveSource, details, onProgress) {
@ -325,7 +324,7 @@ class DictionaryImporter {
} }
const content = await file.async('base64'); const content = await file.async('base64');
const mediaType = this._mediaUtil.getImageMediaTypeFromFileName(path); const mediaType = MediaUtil.getImageMediaTypeFromFileName(path);
if (mediaType === null) { if (mediaType === null) {
throw new Error(`Could not determine media type for image at path ${JSON.stringify(path)} for ${errorSource}`); throw new Error(`Could not determine media type for image at path ${JSON.stringify(path)} for ${errorSource}`);
} }

View File

@ -26,7 +26,7 @@ class MediaUtil {
* @returns The file extension, including the '.', or an empty string * @returns The file extension, including the '.', or an empty string
* if there is no file extension. * if there is no file extension.
*/ */
getFileNameExtension(path) { static getFileNameExtension(path) {
const match = /\.[^./\\]*$/.exec(path); const match = /\.[^./\\]*$/.exec(path);
return match !== null ? match[0] : ''; return match !== null ? match[0] : '';
} }
@ -37,7 +37,7 @@ class MediaUtil {
* @returns The media type string if it can be determined from the file path, * @returns The media type string if it can be determined from the file path,
* otherwise null. * otherwise null.
*/ */
getImageMediaTypeFromFileName(path) { static getImageMediaTypeFromFileName(path) {
switch (this.getFileNameExtension(path).toLowerCase()) { switch (this.getFileNameExtension(path).toLowerCase()) {
case '.apng': case '.apng':
return 'image/apng'; return 'image/apng';
@ -74,7 +74,7 @@ class MediaUtil {
* @returns A file extension including the dot for the media type, * @returns A file extension including the dot for the media type,
* otherwise null. * otherwise null.
*/ */
getFileExtensionFromImageMediaType(mediaType) { static getFileExtensionFromImageMediaType(mediaType) {
switch (mediaType) { switch (mediaType) {
case 'image/apng': case 'image/apng':
return '.apng'; return '.apng';
@ -105,7 +105,7 @@ class MediaUtil {
* @returns A file extension including the dot for the media type, * @returns A file extension including the dot for the media type,
* otherwise null. * otherwise null.
*/ */
getFileExtensionFromAudioMediaType(mediaType) { static getFileExtensionFromAudioMediaType(mediaType) {
switch (mediaType) { switch (mediaType) {
case 'audio/mpeg': case 'audio/mpeg':
case 'audio/mp3': case 'audio/mp3':