Skip to content

Commit

Permalink
Track n anki field values also inherit html markup on update-last-card
Browse files Browse the repository at this point in the history
  • Loading branch information
killergerbah committed Aug 4, 2024
1 parent 03ae88d commit 12fad84
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
33 changes: 21 additions & 12 deletions common/anki/anki.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AudioClip } from '@project/common/audio-clip';
import { CardModel, Image } from '@project/common';
import { HttpFetcher, Fetcher } from '@project/common';
import { AnkiSettings } from '@project/common/settings';
import { AnkiSettings, AnkiSettingsFieldKey } from '@project/common/settings';
import sanitize from 'sanitize-filename';
import { extractText, sourceString } from '@project/common/util';

Expand Down Expand Up @@ -331,17 +331,10 @@ export class Anki {
if (infoResponse.result.length > 0 && infoResponse.result[0].noteId === lastNoteId) {
const info = infoResponse.result[0];

if (
this.settingsProvider.sentenceField &&
info.fields &&
typeof info.fields[this.settingsProvider.sentenceField]?.value === 'string' &&
typeof params.note.fields[this.settingsProvider.sentenceField] === 'string'
) {
params.note.fields[this.settingsProvider.sentenceField] = inheritHtmlMarkup(
params.note.fields[this.settingsProvider.sentenceField],
info.fields[this.settingsProvider.sentenceField].value
);
}
this._inheritHtmlMarkupFromField('sentenceField', info, params);
this._inheritHtmlMarkupFromField('track1Field', info, params);
this._inheritHtmlMarkupFromField('track2Field', info, params);
this._inheritHtmlMarkupFromField('track3Field', info, params);

await this._executeAction('updateNoteFields', params, ankiConnectUrl);

Expand Down Expand Up @@ -405,6 +398,22 @@ export class Anki {
);
}

private _inheritHtmlMarkupFromField(fieldKey: AnkiSettingsFieldKey, info: any, params: any) {
const fieldName = this.settingsProvider[fieldKey];

if (
fieldName &&
info.fields &&
typeof info.fields[fieldName]?.value === 'string' &&
typeof params.note.fields[fieldName] === 'string'
) {
params.note.fields[fieldName] = inheritHtmlMarkup(
params.note.fields[fieldName],
info.fields[fieldName].value
);
}
}

private async _executeAction(action: string, params: any, ankiConnectUrl?: string) {
const body: any = {
action: action,
Expand Down
12 changes: 12 additions & 0 deletions common/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ export interface MiscSettings {
readonly tabName: string;
}

export type AnkiSettingsFieldKey =
| 'sentenceField'
| 'definitionField'
| 'audioField'
| 'imageField'
| 'wordField'
| 'sourceField'
| 'urlField'
| 'track1Field'
| 'track2Field'
| 'track3Field';

export interface AnkiSettings {
readonly ankiConnectUrl: string;
readonly deck: string;
Expand Down

0 comments on commit 12fad84

Please sign in to comment.