Skip to content

Commit

Permalink
Rename {selection-text} to {popup-selection-text} (#1207)
Browse files Browse the repository at this point in the history
* Add popup-selection-text + Deprecate & backward compatibility for selection-text

* Lint

* Update test

* Test backward compatibility

* Refactor to handle field migration

* Update test

* Add test update marker

* Fix lint

* Rename variables

* Update changes from code review
  • Loading branch information
khaitruong922 authored Jul 12, 2024
1 parent 502f71c commit 8fc0928
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 134 deletions.
4 changes: 2 additions & 2 deletions docs/anki-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Flashcard fields can be configured with the following steps:
| `{reading}` | Kana reading for the term (empty for terms where the expression is the reading). |
| `{screenshot}` | Screenshot of the web page taken at the time the term was added. |
| `{search-query}` | The full search query shown on the search page. |
| `{selection-text}` | The selected text on the search page or popup. |
| `{popup-selection-text}` | The selected text on the search page or popup. |
| `{sentence}` | Sentence, quote, or phrase that the term appears in from the source content. |
| `{sentence-furigana}` | Sentence, quote, or phrase that the term appears in from the source content, with furigana added. |
| `{single-glossary-DICT-NAME}` | Same as `{glossary}`, but with entries from only a single dictionary. The dictionary name will likely be modified, use the options from the ▼ dropdown. |
Expand Down Expand Up @@ -92,7 +92,7 @@ Flashcard fields can be configured with the following steps:
| `{onyomi-hiragana}` | Onyomi (Chinese reading) for the kanji expressed as hiragana. |
| `{screenshot}` | Screenshot of the web page taken at the time the kanji was added. |
| `{search-query}` | The full search query shown on the search page. |
| `{selection-text}` | The selected text on the search page or popup. |
| `{popup-selection-text}` | The selected text on the search page or popup. |
| `{sentence}` | Sentence, quote, or phrase that the character appears in from the source content. |
| `{sentence-furigana}` | Sentence, quote, or phrase that the character appears in from the source content, with furigana added. |
| `{stroke-count}` | Number of strokes that the kanji character has. |
Expand Down
4 changes: 2 additions & 2 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ These functions are used together in order to request media and other types of o
- <code>"screenshot"</code>
- <code>"clipboardImage"</code>
- <code>"clipboardText"</code>
- <code>"selectionText"</code>
- <code>"popupSelectionText"</code>
- <code>"textFurigana" <i>japaneseText</i> <i>readingMode="default|hiragana|katakana"</i></code>
- <code>"dictionaryMedia" <i>fileName</i> <i>dictionary="Dictionary Name"</i></code>
</details>
Expand All @@ -790,7 +790,7 @@ These functions are used together in order to request media and other types of o
{{#if (hasMedia "clipboardText")}}The clipboard text is: {{getMedia "clipboardText"}}{{/if}}
{{#if (hasMedia "selectionText")}}The selection text is: {{getMedia "selectionText"}}{{/if}}
{{#if (hasMedia "popupSelectionText")}}The popup selection text is: {{getMedia "popupSelectionText"}}{{/if}}
{{#if (hasMedia "textFurigana" "日本語")}}This is an example of text with generated furigana: {{getMedia "textFurigana" "日本語" escape=false}}{{/if}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{<<<<<<<}}
{{#*inline "selection-text"}}
{{~#if (hasMedia "selectionText")}}{{{getMedia "selectionText"}}}{{/if~}}
{{/inline}}
{{=======}}
{{#*inline "popup-selection-text"}}
{{~#if (hasMedia "popupSelectionText")}}{{{getMedia "popupSelectionText"}}}{{/if~}}
{{/inline}}
{{>>>>>>>}}
4 changes: 2 additions & 2 deletions ext/data/templates/default-anki-field-templates.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@
{{~#multiLine}}{{context.fullQuery}}{{/multiLine~}}
{{/inline}}

{{#*inline "selection-text"}}
{{~#if (hasMedia "selectionText")}}{{{getMedia "selectionText"}}}{{/if~}}
{{#*inline "popup-selection-text"}}
{{~#if (hasMedia "popupSelectionText")}}{{{getMedia "popupSelectionText"}}}{{/if~}}
{{/inline}}

{{#*inline "sentence-furigana"}}
Expand Down
6 changes: 3 additions & 3 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class Frontend {
this._application.crossFrame.registerHandlers([
['frontendClosePopup', this._onApiClosePopup.bind(this)],
['frontendCopySelection', this._onApiCopySelection.bind(this)],
['frontendGetSelectionText', this._onApiGetSelectionText.bind(this)],
['frontendGetPopupSelectionText', this._onApiGetPopupSelectionText.bind(this)],
['frontendGetPopupInfo', this._onApiGetPopupInfo.bind(this)],
['frontendGetPageInfo', this._onApiGetPageInfo.bind(this)],
]);
Expand Down Expand Up @@ -298,8 +298,8 @@ export class Frontend {
document.execCommand('copy');
}

/** @type {import('cross-frame-api').ApiHandler<'frontendGetSelectionText'>} */
_onApiGetSelectionText() {
/** @type {import('cross-frame-api').ApiHandler<'frontendGetPopupSelectionText'>} */
_onApiGetPopupSelectionText() {
const selection = document.getSelection();
return selection !== null ? selection.toString() : '';
}
Expand Down
10 changes: 5 additions & 5 deletions ext/js/data/anki-note-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export class AnkiNoteBuilder {
let injectScreenshot = false;
let injectClipboardImage = false;
let injectClipboardText = false;
let injectSelectionText = false;
let injectPopupSelectionText = false;
/** @type {import('anki-note-builder').TextFuriganaDetails[]} */
const textFuriganaDetails = [];
/** @type {import('api').InjectAnkiNoteMediaDictionaryMediaDetails[]} */
Expand All @@ -401,7 +401,7 @@ export class AnkiNoteBuilder {
case 'screenshot': injectScreenshot = true; break;
case 'clipboardImage': injectClipboardImage = true; break;
case 'clipboardText': injectClipboardText = true; break;
case 'selectionText': injectSelectionText = true; break;
case 'popupSelectionText': injectPopupSelectionText = true; break;
case 'textFurigana':
{
const {text, readingMode} = requirement;
Expand Down Expand Up @@ -451,7 +451,7 @@ export class AnkiNoteBuilder {
}

// Inject media
const selectionText = injectSelectionText ? this._getSelectionText() : null;
const popupSelectionText = injectPopupSelectionText ? this._getPopupSelectionText() : null;
const injectedMedia = await this._api.injectAnkiNoteMedia(
timestamp,
dictionaryEntryDetails,
Expand Down Expand Up @@ -480,7 +480,7 @@ export class AnkiNoteBuilder {
screenshot: (typeof screenshotFileName === 'string' ? {value: screenshotFileName} : void 0),
clipboardImage: (typeof clipboardImageFileName === 'string' ? {value: clipboardImageFileName} : void 0),
clipboardText: (typeof clipboardText === 'string' ? {value: clipboardText} : void 0),
selectionText: (typeof selectionText === 'string' ? {value: selectionText} : void 0),
popupSelectionText: (typeof popupSelectionText === 'string' ? {value: popupSelectionText} : void 0),
textFurigana,
dictionaryMedia,
};
Expand All @@ -490,7 +490,7 @@ export class AnkiNoteBuilder {
/**
* @returns {string}
*/
_getSelectionText() {
_getPopupSelectionText() {
const selection = document.getSelection();
return selection !== null ? selection.toString() : '';
}
Expand Down
2 changes: 1 addition & 1 deletion ext/js/data/anki-note-data-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function createAnkiNoteData(marker, {
screenshot: void 0,
clipboardImage: void 0,
clipboardText: void 0,
selectionText: void 0,
popupSelectionText: void 0,
textFurigana: [],
dictionaryMedia: {},
};
Expand Down
4 changes: 2 additions & 2 deletions ext/js/data/anki-template-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function getStandardFieldMarkers(type) {
'reading',
'screenshot',
'search-query',
'selection-text',
'popup-selection-text',
'sentence',
'sentence-furigana',
'tags',
Expand All @@ -86,7 +86,7 @@ export function getStandardFieldMarkers(type) {
'onyomi-hiragana',
'screenshot',
'search-query',
'selection-text',
'popup-selection-text',
'sentence',
'sentence-furigana',
'stroke-count',
Expand Down
21 changes: 21 additions & 0 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ export class OptionsUtil {
this._updateVersion42,
this._updateVersion43,
this._updateVersion44,
this._updateVersion45,
];
/* eslint-enable @typescript-eslint/unbound-method */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1389,6 +1390,26 @@ export class OptionsUtil {
}
}

/**
* - Renamed `selection-text` to `popup-selection-text`
* @type {import('options-util').UpdateFunction}
*/
async _updateVersion45(options) {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v45.handlebars');
const oldMarkerRegex = new RegExp('{selection-text}', 'g');
const newMarker = '{popup-selection-text}';
for (const profile of options.profiles) {
const termsFields = profile.options.anki.terms.fields;
for (const key of Object.keys(termsFields)) {
termsFields[key] = termsFields[key].replace(oldMarkerRegex, newMarker);
}
const kanjiFields = profile.options.anki.kanji.fields;
for (const key of Object.keys(kanjiFields)) {
kanjiFields[key] = kanjiFields[key].replace(oldMarkerRegex, newMarker);
}
}
}

/**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
Expand Down
2 changes: 1 addition & 1 deletion ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -1889,7 +1889,7 @@ export class Display extends EventDispatcher {
/** @type {string} */
let text;
try {
text = await this.invokeContentOrigin('frontendGetSelectionText', void 0);
text = await this.invokeContentOrigin('frontendGetPopupSelectionText', void 0);
} catch (e) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/js/templates/template-renderer-media-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export 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 'popupSelectionText': return this._getSimpleMediaData(media, 'popupSelectionText');
case 'textFurigana': return this._getTextFurigana(media, args[1], namedArgs);
case 'dictionaryMedia': return this._getDictionaryMedia(media, args[1], namedArgs);
default: return null;
Expand Down
2 changes: 1 addition & 1 deletion ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -3300,7 +3300,7 @@ <h5>or click here to upload</h5>
<td>The full search query shown on the search page.</td>
</tr>
<tr>
<td><code class="anki-field-marker">{selection-text}</code></td>
<td><code class="anki-field-marker">{popup-selection-text}</code></td>
<td>The selected text on the search page or popup.</td>
</tr>
<tr>
Expand Down
Loading

0 comments on commit 8fc0928

Please sign in to comment.