Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send updates when NoteID is populated #106

Merged
merged 9 commits into from
Apr 14, 2023
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@
"default": true,
"description": "When you run 'Send To Deck' the title (h1) of the markdown file is stored as a tag. This is useful if you have 'daily' notes, you can use the same deck but separate cards by title"
},
"anki.md.card.notecardIdPattern": {
"type": "string",
"default": "<!--\\s*?notecardId\\s*?\\[:=\\]\\s*?(\\d+)\\s*?-->",
"description": "Text to match match the commented notecard ID."
"anki.md.updateCards": {
"type": "boolean",
"default": false,
"description": "Opt in to allow parsed noteIDs to be used for updating existing anki cards. Note: Update doesn't care which deck the card with each noteID are, see: https://github.com/jasonwilliams/anki/pull/106#issuecomment-1483743818"
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/AnkiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ export class AnkiService {
return await this.invoke("addNotes", { notes });
}

async updateFields(card: Card): Promise<any> {
const request = {
note: {
id: card.noteId,
fields: {
Front: card.question,
Back: card.answer,
},
},
};
return await this.invoke("updateNoteFields", request);
}

async guiDeckBrowser(): Promise<any[]> {
return await this.invoke("guiDeckBrowser");
}
Expand Down
4 changes: 3 additions & 1 deletion src/markdown/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export class Transformer {
// Either create a new Deck on Anki or get back the ID of the same-named Deck
await this.deck.createOnAnki();
await this.pushMediaItems(media);
// this.exportCards will return a list of Cards that were just created. Thus we need to insert their note IDs into the markdown
await this.exportCards(cards);
// Call to insert noteID into markdown

return new SendDiff(); // dummy return for the first pull request
}
Expand Down Expand Up @@ -102,7 +104,7 @@ export class Transformer {
throw new Error("No Deck exists for current cards");
}

await this.deck.pushNewCardsToAnki();
await this.deck.createAndUpdateCards();
}

addCardsToDeck(cards: Card[]) {
Expand Down
20 changes: 16 additions & 4 deletions src/models/Deck.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Card } from "./Card";
import { AnkiService } from "../AnkiService";
import { SendDiff } from "./SendDiff";
import { workspace } from "vscode";

export class Deck {
public readonly name: string;
Expand Down Expand Up @@ -106,13 +107,24 @@ export class Deck {

// updates card references in place.
private async _pushNewCardsToAnki(cards: Card[]) {
const ids = await this.ankiService?.addNotes(cards); // this function returns NOTE IDS
const ids = await this.ankiService?.addNotes(cards);
ids?.map((v, i) => (cards[i].noteId = v));
}

async pushNewCardsToAnki() {
const newCards = this.cards.filter((v) => !v.noteId);
this._pushNewCardsToAnki(newCards);
// Calls anki to update the fields of all the passed cards.
private async _pushUpdatedCardsToAnki(cards: Card[]): Promise<Card[]> {
return Promise.all(cards.map((card) => this.ankiService?.updateFields(card)));
}

async createAndUpdateCards() {
// Push the updated cards (based on noteID)
let updateCards: Card[] = [];
let newCards: Card[] = [];
this.cards.forEach((card) => (card.noteId ? updateCards.push(card) : newCards.push(card)));
if (workspace.getConfiguration("anki.md").get("updateCards")) {
await this._pushUpdatedCardsToAnki(updateCards);
}
await this._pushNewCardsToAnki(newCards);
}

// Anki Service Methods
Expand Down