Skip to content

Commit

Permalink
add duplicate behavior dropdown (#853)
Browse files Browse the repository at this point in the history
* duplicate behavior dropdown

* allow duplicate for existing users

* Update docs/anki-integration.md

Co-authored-by: James Maa <jmaa@berkeley.edu>
Signed-off-by: StefanVukovic99 <stefanvukovic44@gmail.com>

* Update docs/anki-integration.md

Co-authored-by: James Maa <jmaa@berkeley.edu>
Signed-off-by: StefanVukovic99 <stefanvukovic44@gmail.com>

* Update docs/anki-integration.md

Co-authored-by: James Maa <jmaa@berkeley.edu>
Signed-off-by: StefanVukovic99 <stefanvukovic44@gmail.com>

* remove suggestion comment

---------

Signed-off-by: StefanVukovic99 <stefanvukovic44@gmail.com>
Co-authored-by: James Maa <jmaa@berkeley.edu>
  • Loading branch information
StefanVukovic99 and jamesmaa authored Apr 22, 2024
1 parent 808ddef commit c6d93a9
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
11 changes: 6 additions & 5 deletions docs/anki-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,14 @@ be able to create a flashcard for <ruby>箸<rt>はし</rt></ruby> because they s
Once Yomitan is configured, it becomes trivial to create new flashcards with a single click. You will see the following
icons next to term definitions:

- Clicking ![](../img/btn-add-expression.png) adds the current expression as kanji (e.g. 食べる).
- Clicking ![](../img/btn-add-reading.png) adds the current expression as hiragana or katakana (e.g. たべる).
- Clicking ![](../img/btn-add-expression.png) adds the current expression (e.g. 食べる).
- Clicking ![](../img/btn-add-reading.png) adds the current expression's reading (e.g. たべる).

If "Check for card duplicates" is on, and a card for the current definition already exists in the deck, you will see the following icons instead:
If _Check for card duplicates_ is on, and a card for the current definition already exists in the deck, you will see the book icon.
If _When a duplicate is detected_ is set to `Prevent adding`, the icons will appear grayed out. If set to `Allow adding`, the icons will change to:

- Adding the current expression: ![](../img/btn-add-duplicate-expression.png)
- Adding as hiragana or katakana: ![](../img/btn-add-duplicate-reading.png)
- ![](../img/btn-add-duplicate-expression.png): to add the expression
- ![](../img/btn-add-duplicate-reading.png): to add the reading

Below are some troubleshooting tips you can try if you are unable to create new flashcards:

Expand Down
5 changes: 5 additions & 0 deletions ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,11 @@
"type": "boolean",
"default": true
},
"duplicateBehavior": {
"type": "string",
"enum": ["prevent", "new"],
"default": "prevent"
},
"fieldTemplates": {
"type": ["string", "null"],
"default": null
Expand Down
13 changes: 12 additions & 1 deletion ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,8 @@ export class OptionsUtil {
this._updateVersion27,
this._updateVersion28,
this._updateVersion29,
this._updateVersion30
this._updateVersion30,
this._updateVersion31
];
/* eslint-enable @typescript-eslint/unbound-method */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1233,6 +1234,16 @@ export class OptionsUtil {
}
}

/**
* - Added anki.duplicateBehavior
* @type {import('options-util').UpdateFunction}
*/
_updateVersion31(options) {
for (const {options: profileOptions} of options.profiles) {
profileOptions.anki.duplicateBehavior = 'new';
}
}

/**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
Expand Down
19 changes: 17 additions & 2 deletions ext/js/display/display-anki.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export class DisplayAnki {
this._duplicateScope = 'collection';
/** @type {boolean} */
this._duplicateScopeCheckAllModels = false;
/** @type {import('settings').AnkiDuplicateBehavior} */
this._duplicateBehavior = 'prevent';
/** @type {import('settings').AnkiScreenshotFormat} */
this._screenshotFormat = 'png';
/** @type {number} */
Expand Down Expand Up @@ -192,6 +194,7 @@ export class DisplayAnki {
tags,
duplicateScope,
duplicateScopeCheckAllModels,
duplicateBehavior,
suspendNewCards,
checkForDuplicates,
displayTags,
Expand All @@ -212,6 +215,7 @@ export class DisplayAnki {
this._displayTags = displayTags;
this._duplicateScope = duplicateScope;
this._duplicateScopeCheckAllModels = duplicateScopeCheckAllModels;
this._duplicateBehavior = duplicateBehavior;
this._screenshotFormat = format;
this._screenshotQuality = quality;
this._scanLength = scanLength;
Expand Down Expand Up @@ -419,7 +423,7 @@ export class DisplayAnki {

// If entry has noteIds, show the "add duplicate" button.
if (Array.isArray(noteIds) && noteIds.length > 0) {
this._showDuplicateAddButton(button);
this._updateButtonForDuplicate(button);
}
}

Expand All @@ -441,6 +445,17 @@ export class DisplayAnki {
}
}

/**
* @param {HTMLButtonElement} button
*/
_updateButtonForDuplicate(button) {
if (this._duplicateBehavior === 'prevent') {
button.disabled = true;
} else {
this._showDuplicateAddButton(button);
}
}

/**
* @param {number} i
* @param {(?import('anki').NoteInfo)[]} noteInfos
Expand Down Expand Up @@ -550,7 +565,7 @@ export class DisplayAnki {
}
}
// Now that this dictionary entry has a duplicate in Anki, show the "add duplicate" buttons.
this._showDuplicateAddButton(button);
this._updateButtonForDuplicate(button);

this._updateViewNoteButton(dictionaryEntryIndex, [noteId], true);
}
Expand Down
16 changes: 15 additions & 1 deletion ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,6 @@ <h1>Yomitan Settings</h1>
<div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-label">Check for card duplicates</div>
<div class="settings-item-description">When a card is detected as a duplicate, the add buttons will turn red and change appearance.</div>
</div>
<div class="settings-item-right">
<label class="toggle"><input type="checkbox" data-setting="anki.checkForDuplicates"
Expand Down Expand Up @@ -1687,6 +1686,21 @@ <h1>Yomitan Settings</h1>
</p>
</div>
</div>
<div class="settings-item">
<div class="settings-item-inner settings-item-inner-wrappable">
<div class="settings-item-left">
<div class="settings-item-label">
When a duplicate is detected
</div>
</div>
<div class="settings-item-right">
<select data-setting="anki.duplicateBehavior">
<option value="prevent">Prevent adding</option>
<option value="new">Allow adding</option>
</select>
</div>
</div>
</div>
</div>
</div>
<div class="settings-item advanced-only"><div class="settings-item-inner settings-item-inner-wrappable">
Expand Down
3 changes: 2 additions & 1 deletion test/options-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ function createProfileOptionsUpdatedTestData1() {
screenshot: {format: 'png', quality: 92},
terms: {deck: '', model: '', fields: {}},
kanji: {deck: '', model: '', fields: {}},
duplicateBehavior: 'new',
duplicateScope: 'collection',
duplicateScopeCheckAllModels: false,
displayTags: 'never',
Expand Down Expand Up @@ -602,7 +603,7 @@ function createOptionsUpdatedTestData1() {
}
],
profileCurrent: 0,
version: 30,
version: 31,
global: {
database: {
prefixWildcardsSupported: false
Expand Down
3 changes: 3 additions & 0 deletions types/ext/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export type AnkiOptions = {
kanji: AnkiNoteOptions;
duplicateScope: AnkiDuplicateScope;
duplicateScopeCheckAllModels: boolean;
duplicateBehavior: AnkiDuplicateBehavior;
checkForDuplicates: boolean;
fieldTemplates: string | null;
suspendNewCards: boolean;
Expand Down Expand Up @@ -394,6 +395,8 @@ export type AnkiScreenshotFormat = 'png' | 'jpeg';

export type AnkiDuplicateScope = 'collection' | 'deck' | 'deck-root';

export type AnkiDuplicateBehavior = 'prevent' | 'new';

export type AnkiDisplayTags = 'never' | 'always' | 'non-standard';

export type AnkiNoteGuiMode = 'browse' | 'edit';
Expand Down

0 comments on commit c6d93a9

Please sign in to comment.