Add clipboard-text Anki field (#863)

* Add clipboard-text anki field

* Add markers

* Update clipboard injection
This commit is contained in:
toasted-nutbread 2020-09-26 13:45:48 -04:00 committed by GitHub
parent d27349207d
commit cab5daa22e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 37 additions and 12 deletions

View File

@ -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}`.
@ -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}`.

View File

@ -3,3 +3,7 @@
<img src="{{definition.clipboardImageFileName}}" />
{{~/if~}}
{{/inline}}
{{#*inline "clipboard-text"}}
{{~#if definition.clipboardText~}}{{definition.clipboardText}}{{~/if~}}
{{/inline}}

View File

@ -282,4 +282,8 @@
{{~/if~}}
{{/inline}}
{{#*inline "clipboard-text"}}
{{~#if definition.clipboardText~}}{{definition.clipboardText}}{{~/if~}}
{{/inline}}
{{~> (lookup . "marker") ~}}

View File

@ -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);

View File

@ -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});
@ -464,7 +464,7 @@ class Backend {
timestamp,
audioDetails,
screenshotDetails,
clipboardImage
clipboardDetails
);
}
@ -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) {

View File

@ -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.

View File

@ -46,6 +46,7 @@ class AnkiController {
return [
'audio',
'clipboard-image',
'clipboard-text',
'cloze-body',
'cloze-prefix',
'cloze-suffix',
@ -69,6 +70,7 @@ class AnkiController {
return [
'character',
'clipboard-image',
'clipboard-text',
'cloze-body',
'cloze-prefix',
'cloze-suffix',

View File

@ -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) {

View File

@ -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({