diff options
Diffstat (limited to 'ext/mixed/js')
-rw-r--r-- | ext/mixed/js/display.js | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 8901ba71..a2707bd0 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -81,6 +81,9 @@ class Display { const clickedElement = $(e.target); const textSource = docRangeFromPoint({x: e.clientX, y: e.clientY}); + if (textSource === null) { + return false; + } textSource.setEndOffset(this.options.scanning.length); const {definitions, length} = await apiTermsFind(textSource.text()); @@ -436,7 +439,15 @@ class Display { try { this.spinner.show(); - const noteId = await apiDefinitionAdd(definition, mode); + const context = {}; + if (this.noteUsesScreenshot()) { + const screenshot = await this.getScreenshot(); + if (screenshot) { + context.screenshot = screenshot; + } + } + + const noteId = await apiDefinitionAdd(definition, mode, context); if (noteId) { const index = this.definitions.indexOf(definition); Display.adderButtonFind(index, mode).addClass('disabled'); @@ -489,10 +500,39 @@ class Display { } } + noteUsesScreenshot() { + const fields = this.options.anki.terms.fields; + for (const name in fields) { + if (fields[name].includes('{screenshot}')) { + return true; + } + } + return false; + } + + async getScreenshot() { + try { + await this.setPopupVisible(false); + await Display.delay(1); // Wait for popup to be hidden. + + const {format, quality} = this.options.anki.screenshot; + const dataUrl = await apiScreenshotGet({format, quality}); + if (!dataUrl || dataUrl.error) { return; } + + return {dataUrl, format}; + } finally { + await this.setPopupVisible(true); + } + } + get firstExpressionIndex() { return this.options.general.resultOutputMode === 'merge' ? 0 : -1; } + setPopupVisible(visible) { + return apiForward('popupSetVisible', {visible}); + } + static clozeBuild(sentence, source) { const result = { sentence: sentence.text.trim() @@ -518,4 +558,8 @@ class Display { static viewerButtonFind(index) { return $('.entry').eq(index).find('.action-view-note'); } + + static delay(time) { + return new Promise((resolve) => setTimeout(resolve, time)); + } } |