Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Add clipboard-text Anki field (#863)
Browse files Browse the repository at this point in the history
* Add clipboard-text anki field

* Add markers

* Update clipboard injection
  • Loading branch information
toasted-nutbread authored Sep 26, 2020
1 parent d273492 commit cab5daa
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Flashcard fields can be configured with the following steps:
-------|------------
`{audio}` | Audio sample of a native speaker's pronunciation in MP3 format (if available).
`{clipboard-image}` | An image which is stored in the system clipboard, if present.
`{clipboard-text}` | Text which is stored in the system clipboard, if present.
`{cloze-body}` | Raw, inflected term as it appeared before being reduced to dictionary form by Yomichan.
`{cloze-prefix}` | Fragment of the containing `{sentence}` starting at the beginning of `{sentence}` until the beginning of `{cloze-body}`.
`{cloze-suffix}` | Fragment of the containing `{sentence}` starting at the end of `{cloze-body}` until the end of `{sentence}`.
Expand All @@ -180,6 +181,7 @@ Flashcard fields can be configured with the following steps:
-------|------------
`{character}` | Unicode glyph representing the current kanji.
`{clipboard-image}` | An image which is stored in the system clipboard, if present.
`{clipboard-text}` | Text which is stored in the system clipboard, if present.
`{cloze-body}` | Raw, inflected parent term as it appeared before being reduced to dictionary form by Yomichan.
`{cloze-prefix}` | Fragment of the containing `{sentence}` starting at the beginning of `{sentence}` until the beginning of `{cloze-body}`.
`{cloze-suffix}` | Fragment of the containing `{sentence}` starting at the end of `{cloze-body}` until the end of `{sentence}`.
Expand Down
4 changes: 4 additions & 0 deletions ext/bg/data/anki-field-templates-upgrade-v4.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
<img src="{{definition.clipboardImageFileName}}" />
{{~/if~}}
{{/inline}}

{{#*inline "clipboard-text"}}
{{~#if definition.clipboardText~}}{{definition.clipboardText}}{{~/if~}}
{{/inline}}
4 changes: 4 additions & 0 deletions ext/bg/data/default-anki-field-templates.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,8 @@
{{~/if~}}
{{/inline}}

{{#*inline "clipboard-text"}}
{{~#if definition.clipboardText~}}{{definition.clipboardText}}{{~/if~}}
{{/inline}}

{{~> (lookup . "marker") ~}}
4 changes: 2 additions & 2 deletions ext/bg/js/anki-note-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class AnkiNoteBuilder {
modeOptions: {fields, deck, model},
audioDetails=null,
screenshotDetails=null,
clipboardImage=false,
clipboardDetails=null,
errors=null
}) {
if (anki !== null) {
await this._injectMedia(anki, definition, fields, mode, audioDetails, screenshotDetails, clipboardImage);
await this._injectMedia(anki, definition, fields, mode, audioDetails, screenshotDetails, clipboardDetails);
}

const fieldEntries = Object.entries(fields);
Expand Down
18 changes: 13 additions & 5 deletions ext/bg/js/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class Backend {
return results;
}

async _onApiInjectAnkiNoteMedia({expression, reading, timestamp, audioDetails, screenshotDetails, clipboardImage}, sender) {
async _onApiInjectAnkiNoteMedia({expression, reading, timestamp, audioDetails, screenshotDetails, clipboardDetails}, sender) {
if (isObject(screenshotDetails)) {
const {id: tabId, windowId} = (sender && sender.tab ? sender.tab : {});
screenshotDetails = Object.assign({}, screenshotDetails, {tabId, windowId});
Expand All @@ -464,7 +464,7 @@ class Backend {
timestamp,
audioDetails,
screenshotDetails,
clipboardImage
clipboardDetails
);
}

Expand Down Expand Up @@ -1512,23 +1512,31 @@ class Backend {
return await this._audioDownloader.downloadAudio(sources, expression, reading, details);
}

async _injectAnkNoteMedia(ankiConnect, expression, reading, timestamp, audioDetails, screenshotDetails, clipboardImage) {
async _injectAnkNoteMedia(ankiConnect, expression, reading, timestamp, audioDetails, screenshotDetails, clipboardDetails) {
const screenshotFileName = (
screenshotDetails !== null ?
await this._injectAnkNoteScreenshot(ankiConnect, expression, reading, timestamp, screenshotDetails) :
null
);
const clipboardImageFileName = (
clipboardImage ?
clipboardDetails !== null && clipboardDetails.image ?
await this._injectAnkNoteClipboardImage(ankiConnect, expression, reading, timestamp) :
null
);
let clipboardText = null;
try {
if (clipboardDetails !== null && clipboardDetails.text) {
clipboardText = await this._clipboardReader.getText();
}
} catch (e) {
// NOP
}
const audioFileName = (
audioDetails !== null ?
await this._injectAnkNoteAudio(ankiConnect, expression, reading, timestamp, audioDetails) :
null
);
return {screenshotFileName, clipboardImageFileName, audioFileName};
return {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName};
}

async _injectAnkNoteAudio(ankiConnect, expression, reading, timestamp, details) {
Expand Down
1 change: 1 addition & 0 deletions ext/bg/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ class OptionsUtil {
// Options conditions converted to string representations.
// Added usePopupWindow.
// Updated handlebars templates to include "clipboard-image" definition.
// Updated handlebars templates to include "clipboard-text" definition.
// Added hideDelay.
// Added inputs to profileOptions.scanning.
// Added pointerEventsEnabled to profileOptions.scanning.
Expand Down
2 changes: 2 additions & 0 deletions ext/bg/js/settings/anki-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class AnkiController {
return [
'audio',
'clipboard-image',
'clipboard-text',
'cloze-body',
'cloze-prefix',
'cloze-suffix',
Expand All @@ -69,6 +70,7 @@ class AnkiController {
return [
'character',
'clipboard-image',
'clipboard-text',
'cloze-body',
'cloze-prefix',
'cloze-suffix',
Expand Down
4 changes: 2 additions & 2 deletions ext/mixed/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ const api = (() => {
return this._invoke('getAnkiNoteInfo', {notes, duplicateScope});
}

injectAnkiNoteMedia(expression, reading, timestamp, audioDetails, screenshotDetails, clipboardImage) {
return this._invoke('injectAnkiNoteMedia', {expression, reading, timestamp, audioDetails, screenshotDetails, clipboardImage});
injectAnkiNoteMedia(expression, reading, timestamp, audioDetails, screenshotDetails, clipboardDetails) {
return this._invoke('injectAnkiNoteMedia', {expression, reading, timestamp, audioDetails, screenshotDetails, clipboardDetails});
}

noteView(noteId) {
Expand Down
10 changes: 7 additions & 3 deletions ext/mixed/js/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -1389,18 +1389,22 @@ class Display extends EventDispatcher {
const {expression, reading} = Array.isArray(definitionExpressions) ? definitionExpressions[0] : definition;
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null);
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null);
const clipboardImage = (this._ankiNoteBuilder.containsMarker(fields, 'clipboard-image'));
const {screenshotFileName, clipboardImageFileName, audioFileName} = await api.injectAnkiNoteMedia(
const clipboardDetails = {
image: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-image'),
text: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-text')
};
const {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName} = await api.injectAnkiNoteMedia(
expression,
reading,
timestamp,
audioDetails,
screenshotDetails,
clipboardImage
clipboardDetails
);
if (screenshotFileName !== null) { definition.screenshotFileName = screenshotFileName; }
if (clipboardImageFileName !== null) { definition.clipboardImageFileName = clipboardImageFileName; }
if (audioFileName !== null) { definition.audioFileName = audioFileName; }
if (clipboardText !== null) { definition.clipboardText = clipboardText; }
}

return await this._ankiNoteBuilder.createNote({
Expand Down

0 comments on commit cab5daa

Please sign in to comment.