Generalize AnkiNoteBuilder to not use audioSystem directly (#796)
This commit is contained in:
parent
5d2261acb9
commit
58e5ddfde0
@ -20,9 +20,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class AnkiNoteBuilder {
|
class AnkiNoteBuilder {
|
||||||
constructor({audioSystem, renderTemplate, getClipboardImage=null, getScreenshot=null}) {
|
constructor({renderTemplate, getDefinitionAudio=null, getClipboardImage=null, getScreenshot=null}) {
|
||||||
this._audioSystem = audioSystem;
|
|
||||||
this._renderTemplate = renderTemplate;
|
this._renderTemplate = renderTemplate;
|
||||||
|
this._getDefinitionAudio = getDefinitionAudio;
|
||||||
this._getClipboardImage = getClipboardImage;
|
this._getClipboardImage = getClipboardImage;
|
||||||
this._getScreenshot = getScreenshot;
|
this._getScreenshot = getScreenshot;
|
||||||
}
|
}
|
||||||
@ -130,7 +130,7 @@ class AnkiNoteBuilder {
|
|||||||
if (fileName === null) { return; }
|
if (fileName === null) { return; }
|
||||||
fileName = this._replaceInvalidFileNameCharacters(fileName);
|
fileName = this._replaceInvalidFileNameCharacters(fileName);
|
||||||
|
|
||||||
const {audio} = await this._audioSystem.getDefinitionAudio(
|
const {audio: data} = await this._getDefinitionAudio(
|
||||||
audioSourceDefinition,
|
audioSourceDefinition,
|
||||||
sources,
|
sources,
|
||||||
{
|
{
|
||||||
@ -141,7 +141,6 @@ class AnkiNoteBuilder {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const data = this._arrayBufferToBase64(audio);
|
|
||||||
await anki.storeMediaFile(fileName, data);
|
await anki.storeMediaFile(fileName, data);
|
||||||
|
|
||||||
definition.audioFileName = fileName;
|
definition.audioFileName = fileName;
|
||||||
@ -260,10 +259,6 @@ class AnkiNoteBuilder {
|
|||||||
return fileName.replace(/[<>:"/\\|?*\x00-\x1F]/g, '-');
|
return fileName.replace(/[<>:"/\\|?*\x00-\x1F]/g, '-');
|
||||||
}
|
}
|
||||||
|
|
||||||
_arrayBufferToBase64(arrayBuffer) {
|
|
||||||
return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
|
|
||||||
}
|
|
||||||
|
|
||||||
_stringReplaceAsync(str, regex, replacer) {
|
_stringReplaceAsync(str, regex, replacer) {
|
||||||
let match;
|
let match;
|
||||||
let index = 0;
|
let index = 0;
|
||||||
|
@ -58,8 +58,8 @@ class Backend {
|
|||||||
useCache: false
|
useCache: false
|
||||||
});
|
});
|
||||||
this._ankiNoteBuilder = new AnkiNoteBuilder({
|
this._ankiNoteBuilder = new AnkiNoteBuilder({
|
||||||
audioSystem: this._audioSystem,
|
|
||||||
renderTemplate: this._renderTemplate.bind(this),
|
renderTemplate: this._renderTemplate.bind(this),
|
||||||
|
getDefinitionAudio: this._getDefinitionAudio.bind(this),
|
||||||
getClipboardImage: this._onApiClipboardImageGet.bind(this),
|
getClipboardImage: this._onApiClipboardImageGet.bind(this),
|
||||||
getScreenshot: this._getScreenshot.bind(this)
|
getScreenshot: this._getScreenshot.bind(this)
|
||||||
});
|
});
|
||||||
@ -124,7 +124,8 @@ class Backend {
|
|||||||
['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}],
|
['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}],
|
||||||
['setAllSettings', {async: true, contentScript: false, handler: this._onApiSetAllSettings.bind(this)}],
|
['setAllSettings', {async: true, contentScript: false, handler: this._onApiSetAllSettings.bind(this)}],
|
||||||
['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}],
|
['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}],
|
||||||
['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.bind(this)}]
|
['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.bind(this)}],
|
||||||
|
['getDefinitionAudio', {async: true, contentScript: true, handler: this._onApiGetDefinitionAudio.bind(this)}]
|
||||||
]);
|
]);
|
||||||
this._messageHandlersWithProgress = new Map([
|
this._messageHandlersWithProgress = new Map([
|
||||||
['deleteDictionary', {async: true, contentScript: false, handler: this._onApiDeleteDictionary.bind(this)}]
|
['deleteDictionary', {async: true, contentScript: false, handler: this._onApiDeleteDictionary.bind(this)}]
|
||||||
@ -827,6 +828,10 @@ class Backend {
|
|||||||
return (tab !== null);
|
return (tab !== null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _onApiGetDefinitionAudio({definition, sources, details}) {
|
||||||
|
return this._getDefinitionAudio(definition, sources, details);
|
||||||
|
}
|
||||||
|
|
||||||
// Command handlers
|
// Command handlers
|
||||||
|
|
||||||
async _onCommandSearch(params) {
|
async _onCommandSearch(params) {
|
||||||
@ -1631,4 +1636,8 @@ class Backend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _getDefinitionAudio(definition, sources, details) {
|
||||||
|
return await this._audioSystem.getDefinitionAudio(definition, sources, details);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -185,7 +185,11 @@ class AudioSystem {
|
|||||||
throw new Error('Could not retrieve audio');
|
throw new Error('Could not retrieve audio');
|
||||||
}
|
}
|
||||||
|
|
||||||
return arrayBuffer;
|
return this._arrayBufferToBase64(arrayBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
_arrayBufferToBase64(arrayBuffer) {
|
||||||
|
return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
|
||||||
}
|
}
|
||||||
|
|
||||||
_isAudioValid(audio) {
|
_isAudioValid(audio) {
|
||||||
|
Loading…
Reference in New Issue
Block a user