Skip to content

Commit

Permalink
feat: update notes without noteid
Browse files Browse the repository at this point in the history
with matching logic (note type + sort field)
close #34
  • Loading branch information
timgreen committed Mar 13, 2023
1 parent 17640ee commit 82f0e24
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/core/src/actions/ankiconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,38 @@ export async function ankiConnectSync(
reporter?.endStoreMedia();

reporter?.startNotes();
// fetch all the notes in the target deck
const noteIdsInDeck = await invoke({
action: "findNotes",
version: 6,
request: { query: `deck:"${deck.deckName}"` },
});
const notesInDeck = await invoke({
action: "notesInfo",
version: 6,
request: {
notes: noteIdsInDeck,
},
});
// map the existing notes from model + first field to noteId.
const noteIdMap = new Map<string, number>();
for (const n of notesInDeck) {
noteIdMap.set(
`${n.modelName}|${
Object.values(n.fields).find((f) => f.order === 0)!.value
}`,
n.noteId,
);
}
// assign the existing noteIds.
for (var n of deck.notes) {
const key = `${n.modelName}|${n.values[n.inOrderFields[0]]}`;
const noteId = noteIdMap.get(key);
if (noteId) {
n.noteId = noteId;
}
}

const notesToUpdate = deck.notes.filter((note) => note.noteId);
const notesToInsert = deck.notes.filter((note) => !note.noteId);

Expand Down

0 comments on commit 82f0e24

Please sign in to comment.