Simplify loadNote() arg passing

This commit is contained in:
Abdo 2025-08-27 03:30:19 +03:00
parent 2ff2ad6bca
commit becac3f764
2 changed files with 46 additions and 39 deletions

View file

@ -427,7 +427,15 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
gui_hooks.editor_did_load_note(self) gui_hooks.editor_did_load_note(self)
assert self.mw.pm.profile is not None assert self.mw.pm.profile is not None
js = f"loadNote({json.dumps(self.nid)}, {mid}, {json.dumps(focus_to)}, {json.dumps(original_note_id)}, {json.dumps(self.mw.reviewer.card.id if self.mw.reviewer.card else None)}, true);" load_args = dict(
nid=self.nid,
mid=mid,
focusTo=focus_to,
originalNoteId=original_note_id,
reviewerCardId=self.mw.reviewer.card.id if self.mw.reviewer.card else None,
initial=True,
)
js = f"loadNote({json.dumps(load_args)});"
self.web.evalWithCallback( self.web.evalWithCallback(
f'require("anki/ui").loaded.then(() => {{ {js} }})', oncallback f'require("anki/ui").loaded.then(() => {{ {js} }})', oncallback
) )

View file

@ -28,6 +28,16 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
lastIOImagePath: Writable<string | null>; lastIOImagePath: Writable<string | null>;
} }
interface LoadNoteArgs {
nid: bigint | null;
notetypeId: bigint | null;
focusTo: number;
originalNoteId: bigint | null;
reviewerCardId: bigint | null;
initial: boolean;
copyFromNote: Note | null;
}
import { registerPackage } from "@tslib/runtime-require"; import { registerPackage } from "@tslib/runtime-require";
import { import {
filenameToLink, filenameToLink,
@ -305,7 +315,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export let selectedDeck: DeckNameId | null = null; export let selectedDeck: DeckNameId | null = null;
async function onNotetypeChange(notetype: NotetypeNameId) { async function onNotetypeChange(notetype: NotetypeNameId) {
loadNote(0n, notetype.id, 0, null, reviewerCard?.id ?? null, false, note); loadNote({ notetypeId: notetype.id, copyFromNote: note });
if ( if (
!( !(
await getConfigBool({ await getConfigBool({
@ -319,7 +329,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
lastAddedNote = null; lastAddedNote = null;
} }
let notetypeMeta: NotetypeIdAndModTime; let notetypeMeta: NotetypeIdAndModTime | null = null;
function setNotetypeMeta(notetype: Notetype): void { function setNotetypeMeta(notetype: Notetype): void {
notetypeMeta = { id: notetype.id, modTime: notetype.mtimeSecs }; notetypeMeta = { id: notetype.id, modTime: notetype.mtimeSecs };
// Discard the saved state of the fields if the notetype has been modified. // Discard the saved state of the fields if the notetype has been modified.
@ -521,10 +531,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
setClozeHint(hint); setClozeHint(hint);
} }
async function loadNewNote() {
await loadNote(0n, notetypeMeta.id, 0, null, reviewerCard?.id ?? null);
}
async function noteCanBeAdded(): Promise<boolean> { async function noteCanBeAdded(): Promise<boolean> {
let problem: string | null = null; let problem: string | null = null;
const result = await noteFieldsCheck(note!); const result = await noteFieldsCheck(note!);
@ -580,7 +586,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
note.id = noteId; note.id = noteId;
addNoteToHistory(note!); addNoteToHistory(note!);
lastAddedNote = note; lastAddedNote = note;
await loadNewNote(); await loadNote();
} }
export async function addCurrentNote(deckId: bigint) { export async function addCurrentNote(deckId: bigint) {
@ -622,7 +628,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export function getNoteInfo() { export function getNoteInfo() {
return { return {
id: note.id.toString() ?? null, id: note.id.toString() ?? null,
mid: notetypeMeta.id.toString(), mid: notetypeMeta!.id.toString(),
fields: note.fields ?? [], fields: note.fields ?? [],
}; };
} }
@ -837,7 +843,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
mode: { mode: {
kind: "add", kind: "add",
imagePath: imagePath, imagePath: imagePath,
notetypeId: notetypeMeta.id, notetypeId: notetypeMeta!.id,
}, },
}); });
} }
@ -950,15 +956,15 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
}); });
} }
async function loadNoteInner( async function loadNoteInner({
nid: bigint | null, nid,
notetypeId: bigint, notetypeId,
focusTo: number, focusTo,
originalNoteId: bigint | null, originalNoteId,
reviewerCardId: bigint | null, reviewerCardId,
initial: boolean = false, initial,
copyFromNote: Note | null = null, copyFromNote,
) { }: LoadNoteArgs) {
let homeDeckId = 0n; let homeDeckId = 0n;
if (reviewerCardId) { if (reviewerCardId) {
reviewerCard = await getCard({ cid: reviewerCardId }); reviewerCard = await getCard({ cid: reviewerCardId });
@ -1123,17 +1129,17 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
triggerChanges(); triggerChanges();
} }
async function loadNote( async function loadNote({
nid: bigint | null, nid = note?.id,
notetypeId: bigint, notetypeId = notetypeMeta?.id,
focusTo: number, focusTo = 0,
originalNoteId: bigint | null, originalNoteId = null,
reviewerCardId: bigint | null, reviewerCardId = reviewerCard ? reviewerCard.id : null,
initial: boolean = false, initial = false,
copyFromNote: Note | null = null, copyFromNote = null,
) { }: Partial<LoadNoteArgs> = {}) {
loadDebouncer.schedule(async () => { loadDebouncer.schedule(async () => {
await loadNoteInner( await loadNoteInner({
nid, nid,
notetypeId, notetypeId,
focusTo, focusTo,
@ -1141,26 +1147,19 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
reviewerCardId, reviewerCardId,
initial, initial,
copyFromNote, copyFromNote,
); });
}); });
} }
async function reloadNote() { async function reloadNote() {
await loadNote(note!.id, notetypeMeta.id, 0, null, reviewerCard?.id ?? null); await loadNote();
} }
async function reloadNoteIfEmpty() { async function reloadNoteIfEmpty() {
const isEmpty = const isEmpty =
(await noteFieldsCheck(note!)).state == NoteFieldsCheckResponse_State.EMPTY; (await noteFieldsCheck(note!)).state == NoteFieldsCheckResponse_State.EMPTY;
if (isEmpty) { if (isEmpty) {
await loadNote( await loadNote({ initial: true });
note!.id,
notetypeMeta.id,
0,
null,
reviewerCard?.id ?? null,
true,
);
} }
} }