Skip to content

Commit

Permalink
Refactor to handle field migration
Browse files Browse the repository at this point in the history
  • Loading branch information
khaitruong922 committed Jul 12, 2024
1 parent 7bee89a commit 629d820
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
{{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText"}}{{/getMedia}}{{/if~}}
{{/inline}}

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

{{#*inline "sentence-furigana"}}
{{~#if definition.cloze~}}
{{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}}
Expand Down
10 changes: 0 additions & 10 deletions ext/data/templates/anki-field-templates-upgrade-v33.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,3 @@
{{~#if (hasMedia "selectionText")}}{{{getMedia "selectionText"}}}{{/if~}}
{{/inline}}
{{>>>>>>>}}

{{<<<<<<<}}
{{#*inline "popup-selection-text"}}
{{~#if (hasMedia "selectionText")}}{{getMedia "selectionText"}}{{/if~}}
{{/inline}}
{{=======}}
{{#*inline "popup-selection-text"}}
{{~#if (hasMedia "selectionText")}}{{{getMedia "selectionText"}}}{{/if~}}
{{/inline}}
{{>>>>>>>}}
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 "selectionText")}}{{getMedia "selectionText"}}{{/if~}}
{{/inline}}
{{>>>>>>>}}
4 changes: 0 additions & 4 deletions ext/data/templates/default-anki-field-templates.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,6 @@
{{~#multiLine}}{{context.fullQuery}}{{/multiLine~}}
{{/inline}}

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

{{#*inline "popup-selection-text"}}
{{~#if (hasMedia "selectionText")}}{{{getMedia "selectionText"}}}{{/if~}}
{{/inline}}
Expand Down
9 changes: 0 additions & 9 deletions ext/js/data/anki-template-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export function getStandardFieldMarkers(type) {
'reading',
'screenshot',
'search-query',
'selection-text',
'popup-selection-text',
'sentence',
'sentence-furigana',
Expand Down Expand Up @@ -87,7 +86,6 @@ export function getStandardFieldMarkers(type) {
'onyomi-hiragana',
'screenshot',
'search-query',
'selection-text',
'popup-selection-text',
'sentence',
'sentence-furigana',
Expand All @@ -100,13 +98,6 @@ export function getStandardFieldMarkers(type) {
}
}

/**
* @returns {string[]}
*/
export function getDeprecatedFieldMarkers() {
return ['selection-text'];
}

/**
* @param {import('settings').ProfileOptions} options
* @returns {string}
Expand Down
16 changes: 16 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,21 @@ export class OptionsUtil {
}
}

/**
* - Rename `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');
for (const profile of options.profiles) {
const fields = profile.options.anki.terms.fields;
for (const key in fields) {

Check failure on line 1401 in ext/js/data/options-util.js

View workflow job for this annotation

GitHub Actions / Static Analysis

The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype
const regex = new RegExp('{selection-text}', 'g');
fields[key] = fields[key].replace(regex, '{popup-selection-text}');
}
}
}

/**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
Expand Down
3 changes: 1 addition & 2 deletions ext/js/pages/settings/anki-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {EventListenerCollection} from '../../core/event-listener-collection.js';
import {ExtensionError} from '../../core/extension-error.js';
import {log} from '../../core/log.js';
import {toError} from '../../core/to-error.js';
import {getDeprecatedFieldMarkers, getDynamicFieldMarkers, getStandardFieldMarkers} from '../../data/anki-template-util.js';
import {getDynamicFieldMarkers, getStandardFieldMarkers} from '../../data/anki-template-util.js';
import {stringContainsAnyFieldMarker} from '../../data/anki-util.js';
import {getRequiredPermissionsForAnkiFieldValue, hasPermissions, setPermissionsGranted} from '../../data/permissions-util.js';
import {querySelectorNotNull} from '../../dom/query-selector.js';
Expand Down Expand Up @@ -341,7 +341,6 @@ export class AnkiController {
if (types.includes('term')) {
markers.push(...getDynamicFieldMarkers(dictionaries));
}
markers = markers.filter((marker) => !getDeprecatedFieldMarkers().includes(marker));
markers = [...new Set(markers.sort())];

const fragment = document.createDocumentFragment();
Expand Down
Loading

0 comments on commit 629d820

Please sign in to comment.