Fix kanji note creation (#1069)

* Fix kanji note creation

* Move try/catch for consistency

* Move audio normalization
This commit is contained in:
toasted-nutbread 2020-11-26 22:53:58 -05:00 committed by GitHub
parent 3f7c76dbc7
commit 8449322e1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 98 deletions

View File

@ -458,16 +458,15 @@ class Backend {
return results; return results;
} }
async _onApiInjectAnkiNoteMedia({expression, reading, timestamp, audioDetails, screenshotDetails, clipboardDetails}, sender) { async _onApiInjectAnkiNoteMedia({timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails}, sender) {
if (isObject(screenshotDetails)) { if (isObject(screenshotDetails)) {
const {id: tabId, windowId} = (sender && sender.tab ? sender.tab : {}); const {id: tabId, windowId} = (sender && sender.tab ? sender.tab : {});
screenshotDetails = Object.assign({}, screenshotDetails, {tabId, windowId}); screenshotDetails = Object.assign({}, screenshotDetails, {tabId, windowId});
} }
return await this._injectAnkNoteMedia( return await this._injectAnkNoteMedia(
this._anki, this._anki,
expression,
reading,
timestamp, timestamp,
definitionDetails,
audioDetails, audioDetails,
screenshotDetails, screenshotDetails,
clipboardDetails clipboardDetails
@ -1497,18 +1496,28 @@ class Backend {
return await this._audioDownloader.downloadAudio(sources, expression, reading, details); return await this._audioDownloader.downloadAudio(sources, expression, reading, details);
} }
async _injectAnkNoteMedia(ankiConnect, expression, reading, timestamp, audioDetails, screenshotDetails, clipboardDetails) { async _injectAnkNoteMedia(ankiConnect, timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails) {
const screenshotFileName = ( let screenshotFileName = null;
screenshotDetails !== null ? let clipboardImageFileName = null;
await this._injectAnkNoteScreenshot(ankiConnect, expression, reading, timestamp, screenshotDetails) :
null
);
const clipboardImageFileName = (
clipboardDetails !== null && clipboardDetails.image ?
await this._injectAnkNoteClipboardImage(ankiConnect, expression, reading, timestamp) :
null
);
let clipboardText = null; let clipboardText = null;
let audioFileName = null;
try {
if (screenshotDetails !== null) {
screenshotFileName = await this._injectAnkNoteScreenshot(ankiConnect, timestamp, definitionDetails, screenshotDetails);
}
} catch (e) {
// NOP
}
try {
if (clipboardDetails !== null && clipboardDetails.image) {
clipboardImageFileName = await this._injectAnkNoteClipboardImage(ankiConnect, timestamp, definitionDetails);
}
} catch (e) {
// NOP
}
try { try {
if (clipboardDetails !== null && clipboardDetails.text) { if (clipboardDetails !== null && clipboardDetails.text) {
clipboardText = await this._clipboardReader.getText(); clipboardText = await this._clipboardReader.getText();
@ -1516,92 +1525,102 @@ class Backend {
} catch (e) { } catch (e) {
// NOP // NOP
} }
const audioFileName = (
audioDetails !== null ? try {
await this._injectAnkNoteAudio(ankiConnect, expression, reading, timestamp, audioDetails) : if (audioDetails !== null) {
null audioFileName = await this._injectAnkNoteAudio(ankiConnect, timestamp, definitionDetails, audioDetails);
); }
} catch (e) {
// NOP
}
return {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName}; return {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName};
} }
async _injectAnkNoteAudio(ankiConnect, expression, reading, timestamp, details) { async _injectAnkNoteAudio(ankiConnect, timestamp, definitionDetails, details) {
try { const {type, expression, reading} = definitionDetails;
if (!reading && !expression) { if (type === 'kanji') {
throw new Error('Invalid reading and expression'); throw new Error('Cannot inject audio for kanji');
}
if (!reading && !expression) {
throw new Error('Invalid reading and expression');
}
const {sources, customSourceUrl} = details;
const data = await this._downloadDefinitionAudio(
sources,
expression,
reading,
{
textToSpeechVoice: null,
customSourceUrl,
binary: true,
disableCache: true
} }
);
let fileName = 'yomichan'; let fileName = this._generateAnkiNoteMediaFileName('yomichan_audio', '.mp3', timestamp, definitionDetails);
if (reading) { fileName += `_${reading}`; } fileName = fileName.replace(/\]/g, '');
if (expression) { fileName += `_${expression}`; } await ankiConnect.storeMediaFile(fileName, data);
fileName += '.mp3';
fileName = fileName.replace(/\]/g, '');
fileName = this._replaceInvalidFileNameCharacters(fileName);
const {sources, customSourceUrl} = details; return fileName;
const data = await this._downloadDefinitionAudio( }
sources,
expression, async _injectAnkNoteScreenshot(ankiConnect, timestamp, definitionDetails, details) {
reading, const {windowId, tabId, ownerFrameId, format, quality} = details;
const dataUrl = await this._getScreenshot(windowId, tabId, ownerFrameId, format, quality);
const {mediaType, data} = this._getDataUrlInfo(dataUrl);
const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType);
if (extension === null) { throw new Error('Unknown image media type'); }
const fileName = this._generateAnkiNoteMediaFileName('yomichan_browser_screenshot', extension, timestamp, definitionDetails);
await ankiConnect.storeMediaFile(fileName, data);
return fileName;
}
async _injectAnkNoteClipboardImage(ankiConnect, timestamp, definitionDetails) {
const dataUrl = await this._clipboardReader.getImage();
if (dataUrl === null) {
throw new Error('No clipboard image');
}
const {mediaType, data} = this._getDataUrlInfo(dataUrl);
const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType);
if (extension === null) { throw new Error('Unknown image media type'); }
const fileName = this._generateAnkiNoteMediaFileName('yomichan_clipboard_image', extension, timestamp, definitionDetails);
await ankiConnect.storeMediaFile(fileName, data);
return fileName;
}
_generateAnkiNoteMediaFileName(prefix, extension, timestamp, definitionDetails) {
let fileName = prefix;
switch (definitionDetails.type) {
case 'kanji':
{ {
textToSpeechVoice: null, const {character} = definitionDetails;
customSourceUrl, if (character) { fileName += `_${character}`; }
binary: true,
disableCache: true
} }
); break;
default:
await ankiConnect.storeMediaFile(fileName, data); {
const {reading, expression} = definitionDetails;
return fileName; if (reading) { fileName += `_${reading}`; }
} catch (e) { if (expression) { fileName += `_${expression}`; }
return null; }
break;
} }
}
async _injectAnkNoteScreenshot(ankiConnect, expression, reading, timestamp, details) { fileName += `_${this._ankNoteDateToString(new Date(timestamp))}`;
try { fileName += extension;
const now = new Date(timestamp);
const {windowId, tabId, ownerFrameId, format, quality} = details; fileName = this._replaceInvalidFileNameCharacters(fileName);
const dataUrl = await this._getScreenshot(windowId, tabId, ownerFrameId, format, quality);
const {mediaType, data} = this._getDataUrlInfo(dataUrl); return fileName;
const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType);
if (extension === null) { throw new Error('Unknown image media type'); }
let fileName = `yomichan_browser_screenshot_${reading}_${this._ankNoteDateToString(now)}.${extension}`;
fileName = this._replaceInvalidFileNameCharacters(fileName);
await ankiConnect.storeMediaFile(fileName, data);
return fileName;
} catch (e) {
return null;
}
}
async _injectAnkNoteClipboardImage(ankiConnect, expression, reading, timestamp) {
try {
const now = new Date(timestamp);
const dataUrl = await this._clipboardReader.getImage();
if (dataUrl === null) {
throw new Error('No clipboard image');
}
const {mediaType, data} = this._getDataUrlInfo(dataUrl);
const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType);
if (extension === null) { throw new Error('Unknown image media type'); }
let fileName = `yomichan_clipboard_image_${reading}_${this._ankNoteDateToString(now)}.${extension}`;
fileName = this._replaceInvalidFileNameCharacters(fileName);
await ankiConnect.storeMediaFile(fileName, data);
return fileName;
} catch (e) {
return null;
}
} }
_replaceInvalidFileNameCharacters(fileName) { _replaceInvalidFileNameCharacters(fileName) {

View File

@ -81,8 +81,8 @@ const api = (() => {
return this._invoke('getAnkiNoteInfo', {notes, duplicateScope}); return this._invoke('getAnkiNoteInfo', {notes, duplicateScope});
} }
injectAnkiNoteMedia(expression, reading, timestamp, audioDetails, screenshotDetails, clipboardDetails) { injectAnkiNoteMedia(timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails) {
return this._invoke('injectAnkiNoteMedia', {expression, reading, timestamp, audioDetails, screenshotDetails, clipboardDetails}); return this._invoke('injectAnkiNoteMedia', {timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails});
} }
noteView(noteId) { noteView(noteId) {

View File

@ -1471,7 +1471,7 @@ class Display extends EventDispatcher {
const timestamp = Date.now(); const timestamp = Date.now();
const ownerFrameId = this._ownerFrameId; const ownerFrameId = this._ownerFrameId;
const {fields} = modeOptions; const {fields} = modeOptions;
const {expression, reading} = this._getDefinitionPrimaryExpressionAndReading(definition); const definitionDetails = this._getDefinitionDetailsForNote(definition);
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null); const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null);
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null); const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null);
const clipboardDetails = { const clipboardDetails = {
@ -1479,9 +1479,8 @@ class Display extends EventDispatcher {
text: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-text') text: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-text')
}; };
const {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName} = await api.injectAnkiNoteMedia( const {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName} = await api.injectAnkiNoteMedia(
expression,
reading,
timestamp, timestamp,
definitionDetails,
audioDetails, audioDetails,
screenshotDetails, screenshotDetails,
clipboardDetails clipboardDetails
@ -1507,11 +1506,13 @@ class Display extends EventDispatcher {
}); });
} }
async _getAudioInfo(source, expression, reading, details) { _getDefinitionDetailsForNote(definition) {
return await api.getDefinitionAudioInfo(source, expression, reading, details); const {type} = definition;
} if (type === 'kanji') {
const {character} = definition;
return {type, character};
}
_getDefinitionPrimaryExpressionAndReading(definition) {
const termDetailsList = definition.expressions; const termDetailsList = definition.expressions;
let bestIndex = -1; let bestIndex = -1;
for (let i = 0, ii = termDetailsList.length; i < ii; ++i) { for (let i = 0, ii = termDetailsList.length; i < ii; ++i) {
@ -1524,7 +1525,11 @@ class Display extends EventDispatcher {
} }
} }
const {expression, reading} = termDetailsList[Math.max(0, bestIndex)]; const {expression, reading} = termDetailsList[Math.max(0, bestIndex)];
return {expression, reading}; return {type, expression, reading};
}
async _getAudioInfo(source, expression, reading, details) {
return await api.getDefinitionAudioInfo(source, expression, reading, details);
} }
_areSame(set, array) { _areSame(set, array) {