From 75454c4af53951d672ae90c4b82bacdcc4fac270 Mon Sep 17 00:00:00 2001 From: Lukas Raab Date: Thu, 9 May 2024 20:00:00 +0200 Subject: [PATCH] A more efficient way of updating tags + Scan current file only --- main.ts | 45 ++++++++++++------- package-lock.json | 4 +- src/anki.ts | 15 ++----- src/file.ts | 13 ++---- src/files-manager.ts | 16 +++---- .../obsidian-to-anki-plugin/manifest.json | 2 +- 6 files changed, 44 insertions(+), 51 deletions(-) diff --git a/main.ts b/main.ts index 4b4c1d30..e131ab4e 100644 --- a/main.ts +++ b/main.ts @@ -183,7 +183,7 @@ export default class MyPlugin extends Plugin { return allTFiles; } - async scanVault() { + async scanVault(file:TFile) { new Notice('Scanning vault, check console for details...'); console.info("Checking connection to Anki...") try { @@ -197,20 +197,23 @@ export default class MyPlugin extends Plugin { const data: ParsedSettings = await settingToData(this.app, this.settings, this.fields_dict) const scanDir = this.app.vault.getAbstractFileByPath(this.settings.Defaults["Scan Directory"]) let manager = null; - if (scanDir !== null) { - let markdownFiles = []; - if (scanDir instanceof TFolder) { - console.info("Using custom scan directory: " + scanDir.path) - markdownFiles = this.getAllTFilesInFolder(scanDir); - } else { - new Notice("Error: incorrect path for scan directory " + this.settings.Defaults["Scan Directory"]) - return - } - manager = new FileManager(this.app, data, markdownFiles, this.file_hashes, this.added_media) - } else { - manager = new FileManager(this.app, data, this.app.vault.getMarkdownFiles(), this.file_hashes, this.added_media); - } - + if (file !== undefined) { + manager = new FileManager(this.app, data, [file], this.file_hashes, this.added_media); + } else { + if (scanDir !== null) { + let markdownFiles = []; + if (scanDir instanceof TFolder) { + console.info("Using custom scan directory: " + scanDir.path) + markdownFiles = this.getAllTFilesInFolder(scanDir); + } else { + new Notice("Error: incorrect path for scan directory " + this.settings.Defaults["Scan Directory"]) + return + } + manager = new FileManager(this.app, data, markdownFiles, this.file_hashes, this.added_media) + } else { + manager = new FileManager(this.app, data, this.app.vault.getMarkdownFiles(), this.file_hashes, this.added_media); + } + } await manager.initialiseFiles() await manager.requests_1() this.added_media = Array.from(manager.added_media_set) @@ -253,14 +256,22 @@ export default class MyPlugin extends Plugin { this.addSettingTab(new SettingsTab(this.app, this)); this.addRibbonIcon('anki', 'Obsidian_to_Anki - Scan Vault', async () => { - await this.scanVault() + await this.scanVault(undefined) }) this.addCommand({ id: 'anki-scan-vault', name: 'Scan Vault', callback: async () => { - await this.scanVault() + await this.scanVault(undefined) + } + }) + + this.addCommand({ + id: 'anki-scan-file', + name: 'Scan Current File', + callback: async () => { + await this.scanVault(this.app.workspace.getActiveFile()) } }) } diff --git a/package-lock.json b/package-lock.json index a2372c62..32893397 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-to-anki-plugin", - "version": "3.4.2", + "version": "3.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "obsidian-to-anki-plugin", - "version": "3.4.2", + "version": "3.6.0", "license": "MIT", "dependencies": { "byte-base64": "^1.1.0", diff --git a/src/anki.ts b/src/anki.ts index 10a79044..8b03a08a 100644 --- a/src/anki.ts +++ b/src/anki.ts @@ -105,19 +105,10 @@ export function changeDeck(card_ids: number[], deck: string): AnkiConnectRequest ) } -export function removeTags(note_ids: number[], tags: string): AnkiConnectRequest { +export function updateNoteTags(note_id: number, tags: string[]): AnkiConnectRequest { return request( - 'removeTags', { - notes: note_ids, - tags: tags - } - ) -} - -export function addTags(note_ids: number[], tags: string): AnkiConnectRequest { - return request( - 'addTags', { - notes: note_ids, + 'updateNoteTags', { + note: note_id, tags: tags } ) diff --git a/src/file.ts b/src/file.ts index 2319d968..261285f6 100644 --- a/src/file.ts +++ b/src/file.ts @@ -231,19 +231,12 @@ abstract class AbstractFile { return AnkiConnect.changeDeck(this.card_ids, this.target_deck) } - getClearTags(): AnkiConnect.AnkiConnectRequest { - let IDs: number[] = [] - for (let parsed of this.notes_to_edit) { - IDs.push(parsed.identifier) - } - return AnkiConnect.removeTags(IDs, this.tags.join(" ")) - } - - getAddTags(): AnkiConnect.AnkiConnectRequest { + getUpdateTags(): AnkiConnect.AnkiConnectRequest { let actions: AnkiConnect.AnkiConnectRequest[] = [] for (let parsed of this.notes_to_edit) { + let tags = parsed.note.tags.join(" ") + " " + this.global_tags actions.push( - AnkiConnect.addTags([parsed.identifier], parsed.note.tags.join(" ") + " " + this.global_tags) + AnkiConnect.updateNoteTags(parsed.identifier, tags.split(" ")) ) } return AnkiConnect.multi(actions) diff --git a/src/files-manager.ts b/src/files-manager.ts index fb4d35f3..dd29161d 100644 --- a/src/files-manager.ts +++ b/src/files-manager.ts @@ -306,24 +306,22 @@ export class FileManager { let temp: AnkiConnect.AnkiConnectRequest[] = [] console.info("Requesting cards to be moved to target deck...") for (let file of this.ownFiles) { - temp.push(file.getChangeDecks()) + let deck = file.getChangeDecks() + if (deck.params.cards && deck.params.cards.length) { + temp.push(deck) + } } requests.push(AnkiConnect.multi(temp)) temp = [] console.info("Requesting tags to be replaced...") for (let file of this.ownFiles) { - let rem = file.getClearTags() - if(rem.params.notes.length) { - temp.push(rem) + let update = file.getUpdateTags() + if(update.params.actions.length) { + temp.push(update) } } requests.push(AnkiConnect.multi(temp)) temp = [] - for (let file of this.ownFiles) { - temp.push(file.getAddTags()) - } - requests.push(AnkiConnect.multi(temp)) - temp = [] await AnkiConnect.invoke('multi', {actions: requests}) console.info("All done!") } diff --git a/tests/defaults/test_vault/.obsidian/plugins/obsidian-to-anki-plugin/manifest.json b/tests/defaults/test_vault/.obsidian/plugins/obsidian-to-anki-plugin/manifest.json index 6fac28ed..e10e8a8e 100644 --- a/tests/defaults/test_vault/.obsidian/plugins/obsidian-to-anki-plugin/manifest.json +++ b/tests/defaults/test_vault/.obsidian/plugins/obsidian-to-anki-plugin/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-to-anki-plugin", "name": "Obsidian_to_Anki", - "version": "3.4.2", + "version": "3.6.0", "minAppVersion": "0.9.20", "description": "This is an Anki integration plugin! Designed for efficient bulk exporting.", "author": "Pseudonium",