Move apiDefinitionAdd implementation into Backend

This commit is contained in:
toasted-nutbread 2019-12-09 21:25:34 -05:00
parent 7091c8c5c0
commit c9cd29889d
2 changed files with 62 additions and 60 deletions

View File

@ -49,28 +49,8 @@ function apiKanjiFind(text, optionsContext) {
return utilBackend()._onApiKanjiFind({text, optionsContext});
}
async function apiDefinitionAdd(definition, mode, context, optionsContext) {
const options = await apiOptionsGet(optionsContext);
if (mode !== 'kanji') {
await audioInject(
definition,
options.anki.terms.fields,
options.audio.sources,
optionsContext
);
}
if (context && context.screenshot) {
await _apiInjectScreenshot(
definition,
options.anki.terms.fields,
context.screenshot
);
}
const note = await dictNoteFormat(definition, mode, options);
return utilBackend().anki.addNote(note);
function apiDefinitionAdd(definition, mode, context, optionsContext) {
return utilBackend()._onApiDefinitionAdd({definition, mode, context, optionsContext});
}
async function apiDefinitionsAddable(definitions, modes, optionsContext) {
@ -189,42 +169,6 @@ async function apiAudioGetUrl(definition, source, optionsContext) {
return audioGetUrl(definition, source, optionsContext);
}
async function _apiInjectScreenshot(definition, fields, screenshot) {
let usesScreenshot = false;
for (const name in fields) {
if (fields[name].includes('{screenshot}')) {
usesScreenshot = true;
break;
}
}
if (!usesScreenshot) {
return;
}
const dateToString = (date) => {
const year = date.getUTCFullYear();
const month = date.getUTCMonth().toString().padStart(2, '0');
const day = date.getUTCDate().toString().padStart(2, '0');
const hours = date.getUTCHours().toString().padStart(2, '0');
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
};
const now = new Date(Date.now());
const filename = `yomichan_browser_screenshot_${definition.reading}_${dateToString(now)}.${screenshot.format}`;
const data = screenshot.dataUrl.replace(/^data:[\w\W]*?,/, '');
try {
await utilBackend().anki.storeMediaFile(filename, data);
} catch (e) {
return;
}
definition.screenshotFileName = filename;
}
function apiScreenshotGet(options, sender) {
if (!(sender && sender.tab)) {
return Promise.resolve();

View File

@ -315,8 +315,28 @@ class Backend {
return results;
}
_onApiDefinitionAdd({definition, mode, context, optionsContext}) {
return apiDefinitionAdd(definition, mode, context, optionsContext);
async _onApiDefinitionAdd({definition, mode, context, optionsContext}) {
const options = await this.getOptions(optionsContext);
if (mode !== 'kanji') {
await audioInject(
definition,
options.anki.terms.fields,
options.audio.sources,
optionsContext
);
}
if (context && context.screenshot) {
await this._injectScreenshot(
definition,
options.anki.terms.fields,
context.screenshot
);
}
const note = await dictNoteFormat(definition, mode, options);
return this.anki.addNote(note);
}
_onApiDefinitionsAddable({definitions, modes, optionsContext}) {
@ -362,6 +382,44 @@ class Backend {
_onApiClipboardGet() {
return apiClipboardGet();
}
// Utilities
async _injectScreenshot(definition, fields, screenshot) {
let usesScreenshot = false;
for (const name in fields) {
if (fields[name].includes('{screenshot}')) {
usesScreenshot = true;
break;
}
}
if (!usesScreenshot) {
return;
}
const dateToString = (date) => {
const year = date.getUTCFullYear();
const month = date.getUTCMonth().toString().padStart(2, '0');
const day = date.getUTCDate().toString().padStart(2, '0');
const hours = date.getUTCHours().toString().padStart(2, '0');
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
};
const now = new Date(Date.now());
const filename = `yomichan_browser_screenshot_${definition.reading}_${dateToString(now)}.${screenshot.format}`;
const data = screenshot.dataUrl.replace(/^data:[\w\W]*?,/, '');
try {
await this.anki.storeMediaFile(filename, data);
} catch (e) {
return;
}
definition.screenshotFileName = filename;
}
}
Backend._messageHandlers = new Map([