diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-07-07 20:00:30 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-07 20:00:30 -0400 |
commit | 315dc425e489dc96c2f2d4e13b500105d724ec52 (patch) | |
tree | 380f1f793b158d5a7652dd08fa0cd10d7378679f /ext | |
parent | 9935e154f1d2d9881c636b9a93f9d31b04621287 (diff) |
Anki card selected text (#1809)
* Add support for injecting the text selection into Anki cards
* Add selection-text
* Upgrade to add {selection-text}
* Update descriptions
* Update test data
Diffstat (limited to 'ext')
-rw-r--r-- | ext/data/templates/anki-field-templates-upgrade-v13.handlebars | 4 | ||||
-rw-r--r-- | ext/data/templates/default-anki-field-templates.handlebars | 4 | ||||
-rw-r--r-- | ext/js/data/anki-note-builder.js | 11 | ||||
-rw-r--r-- | ext/js/data/options-util.js | 1 | ||||
-rw-r--r-- | ext/js/pages/settings/anki-controller.js | 2 | ||||
-rw-r--r-- | ext/js/templates/template-renderer-media-provider.js | 1 | ||||
-rw-r--r-- | ext/settings.html | 4 |
7 files changed, 26 insertions, 1 deletions
diff --git a/ext/data/templates/anki-field-templates-upgrade-v13.handlebars b/ext/data/templates/anki-field-templates-upgrade-v13.handlebars index ebf9069e..78820b95 100644 --- a/ext/data/templates/anki-field-templates-upgrade-v13.handlebars +++ b/ext/data/templates/anki-field-templates-upgrade-v13.handlebars @@ -1,3 +1,7 @@ +{{#*inline "selection-text"}} + {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText" format="text"}}{{/getMedia}}{{/if~}} +{{/inline}} + {{<<<<<<<}} {{#each glossary}}{{#multiLine}}{{.}}{{/multiLine}}{{/each}} {{=======}} diff --git a/ext/data/templates/default-anki-field-templates.handlebars b/ext/data/templates/default-anki-field-templates.handlebars index 62da796d..c9ee2833 100644 --- a/ext/data/templates/default-anki-field-templates.handlebars +++ b/ext/data/templates/default-anki-field-templates.handlebars @@ -378,4 +378,8 @@ {{~#multiLine}}{{context.fullQuery}}{{/multiLine~}} {{/inline}} +{{#*inline "selection-text"}} + {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText" format="text"}}{{/getMedia}}{{/if~}} +{{/inline}} + {{~> (lookup . "marker") ~}} diff --git a/ext/js/data/anki-note-builder.js b/ext/js/data/anki-note-builder.js index 65740254..23dd648b 100644 --- a/ext/js/data/anki-note-builder.js +++ b/ext/js/data/anki-note-builder.js @@ -283,6 +283,7 @@ class AnkiNoteBuilder { let injectScreenshot = false; let injectClipboardImage = false; let injectClipboardText = false; + let injectSelectionText = false; const dictionaryMediaDetails = []; for (const requirement of requirements) { const {type} = requirement; @@ -291,6 +292,7 @@ class AnkiNoteBuilder { case 'screenshot': injectScreenshot = true; break; case 'clipboardImage': injectClipboardImage = true; break; case 'clipboardText': injectClipboardText = true; break; + case 'selectionText': injectSelectionText = true; break; case 'dictionaryMedia': { const {dictionary, path} = requirement; @@ -323,7 +325,8 @@ class AnkiNoteBuilder { } // Inject media - const {audioFileName, screenshotFileName, clipboardImageFileName, clipboardText, dictionaryMedia: dictionaryMediaArray, errors} = await yomichan.api.injectAnkiNoteMedia( + const selectionText = injectSelectionText ? this._getSelectionText() : null; + const injectedMedia = await yomichan.api.injectAnkiNoteMedia( timestamp, dictionaryEntryDetails, audioDetails, @@ -331,6 +334,7 @@ class AnkiNoteBuilder { clipboardDetails, dictionaryMediaDetails ); + const {audioFileName, screenshotFileName, clipboardImageFileName, clipboardText, dictionaryMedia: dictionaryMediaArray, errors} = injectedMedia; // Format results const dictionaryMedia = {}; @@ -348,8 +352,13 @@ class AnkiNoteBuilder { screenshot: (typeof screenshotFileName === 'string' ? {fileName: screenshotFileName} : null), clipboardImage: (typeof clipboardImageFileName === 'string' ? {fileName: clipboardImageFileName} : null), clipboardText: (typeof clipboardText === 'string' ? {text: clipboardText} : null), + selectionText: (typeof selectionText === 'string' ? {text: selectionText} : null), dictionaryMedia }; return {media, errors}; } + + _getSelectionText() { + return document.getSelection().toString(); + } } diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index baa727d2..3d36fc2e 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -854,6 +854,7 @@ class OptionsUtil { // Version 13 changes: // Handlebars templates updated to use formatGlossary. // Handlebars templates updated to use new media format. + // Added {selection-text} field marker. await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v13.handlebars'); return options; } diff --git a/ext/js/pages/settings/anki-controller.js b/ext/js/pages/settings/anki-controller.js index 28bb21a3..b98de1b6 100644 --- a/ext/js/pages/settings/anki-controller.js +++ b/ext/js/pages/settings/anki-controller.js @@ -102,6 +102,7 @@ class AnkiController { 'reading', 'screenshot', 'search-query', + 'selection-text', 'sentence', 'tags', 'url' @@ -121,6 +122,7 @@ class AnkiController { 'onyomi', 'screenshot', 'search-query', + 'selection-text', 'sentence', 'stroke-count', 'tags', diff --git a/ext/js/templates/template-renderer-media-provider.js b/ext/js/templates/template-renderer-media-provider.js index db4a6d18..4fd40c67 100644 --- a/ext/js/templates/template-renderer-media-provider.js +++ b/ext/js/templates/template-renderer-media-provider.js @@ -78,6 +78,7 @@ class TemplateRendererMediaProvider { case 'screenshot': return this._getSimpleMediaData(media, 'screenshot'); case 'clipboardImage': return this._getSimpleMediaData(media, 'clipboardImage'); case 'clipboardText': return this._getSimpleMediaData(media, 'clipboardText'); + case 'selectionText': return this._getSimpleMediaData(media, 'selectionText'); case 'dictionaryMedia': return this._getDictionaryMedia(media, args[1], namedArgs); default: return null; } diff --git a/ext/settings.html b/ext/settings.html index d8d31039..49bcd722 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -2859,6 +2859,10 @@ <td>The full search query shown on the search page.</td> </tr> <tr> + <td><code class="anki-field-marker">{selection-text}</code></td> + <td>The selected text on the search page or popup.</td> + </tr> + <tr> <td><code class="anki-field-marker">{sentence}</code></td> <td>Sentence, quote, or phrase that the term or kanji appears in from the source content.</td> </tr> |