aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed
diff options
context:
space:
mode:
authorAlex Yatskov <FooSoft@users.noreply.github.com>2019-08-17 09:05:33 -0700
committerGitHub <noreply@github.com>2019-08-17 09:05:33 -0700
commit8ebac935e8c0ee167514d5726b1e2e16921e4957 (patch)
tree905897bbe19b754597b31b89332e09d45a163c5e /ext/mixed
parent1d25c8a744837b02b5d4364d93df65dd4d0b7ed6 (diff)
parent3a5b06bd1078d39cf9b29cbc05a0cde13c96ca94 (diff)
Merge pull request #182 from toasted-nutbread/anki-screenshot
Anki screenshot
Diffstat (limited to 'ext/mixed')
-rw-r--r--ext/mixed/js/display.js43
1 files changed, 42 insertions, 1 deletions
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index 8433c4b5..01cb406e 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -435,7 +435,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');
@@ -488,10 +496,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()
@@ -517,4 +554,8 @@ class Display {
static viewerButtonFind(index) {
return $('.entry').eq(index).find('.action-view-note');
}
+
+ static delay(time) {
+ return new Promise((resolve) => setTimeout(resolve, time));
+ }
}