mirror of
https://github.com/gosticks/flashcards-obsidian.git
synced 2025-10-16 12:05:33 +00:00
Implement deck change #15
This commit is contained in:
parent
67f170bb10
commit
03490e085b
@ -92,6 +92,7 @@ export class Anki {
|
||||
// This means that the delta from the current tags on Anki and the generated one should be added/removed
|
||||
// That's what the current approach does, but in the future if the API it is made more consistent
|
||||
// then mergeTags(...) is not needed anymore
|
||||
let ids: number[] = []
|
||||
|
||||
for (let card of cards) {
|
||||
updateActions.push({
|
||||
@ -102,11 +103,29 @@ export class Anki {
|
||||
})
|
||||
|
||||
updateActions = updateActions.concat(this.mergeTags(card.oldTags, card.tags, card.id))
|
||||
ids.push(card.id)
|
||||
}
|
||||
|
||||
// Update deck
|
||||
updateActions.push({
|
||||
"action": "changeDeck",
|
||||
"params": {
|
||||
"cards": ids,
|
||||
"deck": cards[0].deckName
|
||||
}
|
||||
})
|
||||
|
||||
return this.invoke("multi", 6, { "actions": updateActions })
|
||||
}
|
||||
|
||||
public async changeDeck(ids: number[], deckName: string) {
|
||||
return await this.invoke("changeDeck" , 6, {"cards" : ids, "deck": deckName})
|
||||
}
|
||||
|
||||
public async cardsInfo(ids: number[]) {
|
||||
return await this.invoke("cardsInfo" , 6, {"cards" : ids})
|
||||
}
|
||||
|
||||
public async getCards(ids: number[]) {
|
||||
return await this.invoke("notesInfo", 6, { "notes": ids })
|
||||
}
|
||||
|
||||
@ -64,9 +64,9 @@ export class CardsService {
|
||||
let ankiBlocks = this.parser.getAnkiIDsBlocks(this.file)
|
||||
let ankiCards = ankiBlocks ? await this.anki.getCards(this.getAnkiIDs(ankiBlocks)) : undefined
|
||||
|
||||
|
||||
let cards: Card[] = this.parser.generateFlashcards(this.file, deckName, vaultName, filePath, globalTags)
|
||||
let [cardsToCreate, cardsToUpdate] = this.filterByUpdate(ankiCards, cards)
|
||||
let cardIds : number[] = this.getCardsIds(ankiCards, cards)
|
||||
let cardsToDelete: number[] = this.parser.getCardsToDelete(this.file)
|
||||
|
||||
console.info("Flashcards: Cards to create")
|
||||
@ -81,6 +81,18 @@ export class CardsService {
|
||||
await this.updateCardsOnAnki(cardsToUpdate)
|
||||
await this.insertCardsOnAnki(cardsToCreate, frontmatter, deckName)
|
||||
|
||||
// Update decks if needed
|
||||
let deckNeedToBeChanged = await this.deckNeedToBeChanged(cardIds, deckName)
|
||||
if (deckNeedToBeChanged) {
|
||||
try {
|
||||
this.anki.changeDeck(cardIds, deckName)
|
||||
this.notifications.push("Cards moved in new deck")
|
||||
}
|
||||
catch {
|
||||
return ["Error: Could not update deck the file."]
|
||||
}
|
||||
}
|
||||
|
||||
// Update file
|
||||
if (this.updateFile) {
|
||||
try {
|
||||
@ -275,6 +287,33 @@ export class CardsService {
|
||||
return [cardsToCreate, cardsToUpdate]
|
||||
}
|
||||
|
||||
public async deckNeedToBeChanged(cardsIds : number[], deckName: string) {
|
||||
let cardsInfo = await this.anki.cardsInfo(cardsIds)
|
||||
console.log("cards infoooooo")
|
||||
console.log(cardsInfo)
|
||||
if (cardsInfo.length !== 0) {
|
||||
return cardsInfo[0].deckName !== deckName
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
public getCardsIds(ankiCards: any, generatedCards: Card[]) : number[] {
|
||||
let ids : number[] = []
|
||||
|
||||
if (ankiCards) {
|
||||
for (let flashcard of generatedCards) {
|
||||
let ankiCard = undefined
|
||||
if (flashcard.inserted) {
|
||||
ankiCard = ankiCards.filter((card: any) => Number(card.noteId) === flashcard.id)[0]
|
||||
ids = ids.concat(ankiCard.cards)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ids
|
||||
}
|
||||
|
||||
public parseGlobalTags(file: String) : string[] {
|
||||
let globalTags: string[] = []
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user