Skip to content

Commit 75dc7c4

Browse files
Give feedback about what changed on sync
1 parent b213d8f commit 75dc7c4

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/main.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ export default class AnkiLink extends Plugin {
1616
this.addRibbonIcon(
1717
"circle-question-mark",
1818
"Sample",
19-
(evt: MouseEvent) => {
20-
// Called when the user clicks the icon.
21-
const numStr = syncVaultNotes(this.app).then((n) => n.toString());
22-
numStr.then(
23-
(n) => new Notice(n),
24-
(e) => console.error(e),
25-
);
19+
async (_evt: MouseEvent) => {
20+
await this.runSyncAndNotify();
2621
},
2722
);
2823

@@ -31,8 +26,7 @@ export default class AnkiLink extends Plugin {
3126
id: "sync-cards",
3227
name: "Sync cards",
3328
callback: async () => {
34-
const added = await syncVaultNotes(this.app);
35-
new Notice(`Synced flashcards. Added ${added} note${added === 1 ? "" : "s"}.`);
29+
await this.runSyncAndNotify();
3630
},
3731
});
3832

@@ -67,4 +61,16 @@ export default class AnkiLink extends Plugin {
6761
const template = "> [!flashcard] ";
6862
editor.replaceSelection(template);
6963
}
64+
65+
private async runSyncAndNotify(): Promise<void> {
66+
try {
67+
const { added, modified } = await syncVaultNotes(this.app);
68+
new Notice(
69+
`Synced flashcards.\nAdded ${added} card${added === 1 ? "" : "s"},\nmodified ${modified} card${modified === 1 ? "" : "s"}.`,
70+
);
71+
} catch (error) {
72+
console.error(error);
73+
new Notice("Failed to sync flashcards. Check console for details.");
74+
}
75+
}
7076
}

src/syncUtil.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,51 @@ interface ParsedNoteData {
1919

2020
interface NoteSyncResult {
2121
added: number;
22+
modified: number;
2223
linesModified: boolean;
2324
lines: string[];
2425
}
2526

26-
export async function syncVaultNotes(app: App): Promise<number> {
27+
export interface SyncSummary {
28+
added: number;
29+
modified: number;
30+
}
31+
32+
export async function syncVaultNotes(app: App): Promise<SyncSummary> {
2733
const markdownFiles = app.vault.getMarkdownFiles();
2834
await addMissingDecks();
2935

3036
let totalAdded = 0;
37+
let totalModified = 0;
3138
for (const file of markdownFiles) {
32-
totalAdded += await syncSingleFile(app, file);
39+
const result = await syncSingleFile(app, file);
40+
totalAdded += result.added;
41+
totalModified += result.modified;
3342
}
34-
return totalAdded;
43+
return { added: totalAdded, modified: totalModified };
3544
}
3645

37-
async function syncSingleFile(app: App, file: TFile): Promise<number> {
46+
async function syncSingleFile(app: App, file: TFile): Promise<SyncSummary> {
3847
const originalLines = (await app.vault.read(file)).split("\n");
3948
const notesData = parseDocument(originalLines);
40-
if (notesData.length === 0) return 0;
49+
if (notesData.length === 0) return { added: 0, modified: 0 };
4150

4251
let totalAdded = 0;
52+
let totalModified = 0;
4353
let linesModified = false;
4454
let lines = originalLines;
4555
for (const noteData of notesData) {
4656
const result = await syncSingleNote(noteData, lines);
4757
totalAdded += result.added;
58+
totalModified += result.modified;
4859
linesModified = linesModified || result.linesModified;
4960
lines = result.lines;
5061
}
5162

5263
if (linesModified) {
5364
await app.vault.modify(file, lines.join("\n"));
5465
}
55-
return totalAdded;
66+
return { added: totalAdded, modified: totalModified };
5667
}
5768

5869
async function syncSingleNote(noteData: ParsedNoteData, lines: string[]): Promise<NoteSyncResult> {
@@ -70,15 +81,16 @@ async function syncSingleNote(noteData: ParsedNoteData, lines: string[]): Promis
7081
const ankiFields = ankiNote.fields;
7182
if (obsidianFields.Front !== ankiFields.Front.value || obsidianFields.Back !== ankiFields.Back.value) {
7283
await updateNoteById(ankiNote.noteId, obsidianFields);
84+
return { added: 0, modified: 1, linesModified: false, lines };
7385
}
74-
return { added: 0, linesModified: false, lines };
86+
return { added: 0, modified: 0, linesModified: false, lines };
7587
}
7688

7789
async function createAndWriteNoteId(noteData: ParsedNoteData, lines: string[]): Promise<NoteSyncResult> {
7890
const newId = await sendNote(noteData.note);
7991
const updatedLines = [...lines];
8092
updatedLines[noteData.index] = `> [!flashcard] %%${newId}%% ${noteData.note.fields.Front}`;
81-
return { added: 1, linesModified: true, lines: updatedLines };
93+
return { added: 1, modified: 0, linesModified: true, lines: updatedLines };
8294
}
8395

8496
async function addMissingDecks() {

0 commit comments

Comments
 (0)