Alow passing deck/notetype ID on AddCards init

This commit is contained in:
Abdo 2025-08-27 03:53:04 +03:00
parent 60f7df7210
commit 603b12967c
4 changed files with 49 additions and 22 deletions

View file

@ -23,7 +23,12 @@ from aqt.utils import (
class NewAddCards(QMainWindow):
def __init__(self, mw: AnkiQt) -> None:
def __init__(
self,
mw: AnkiQt,
deck_id: DeckId | None = None,
notetype_id: NotetypeId | None = None,
) -> None:
super().__init__(None, Qt.WindowType.Window)
self._close_event_has_cleaned_up = False
self._close_callback: Callable[[], None] = self._close
@ -37,7 +42,7 @@ class NewAddCards(QMainWindow):
self.setMinimumWidth(400)
self.setupEditor()
add_close_shortcut(self)
self._load_new_note()
self._load_new_note(deck_id, notetype_id)
restoreGeom(self, "add")
gui_hooks.add_cards_did_init(self)
if not is_mac:
@ -62,15 +67,23 @@ class NewAddCards(QMainWindow):
editor_mode=aqt.editor.EditorMode.ADD_CARDS,
)
def reopen(self, mw: AnkiQt) -> None:
self.editor.reload_note_if_empty()
def reopen(
self,
mw: AnkiQt,
deck_id: DeckId | None = None,
notetype_id: NotetypeId | None = None,
) -> None:
self.editor.reload_note_if_empty(deck_id, notetype_id)
def helpRequested(self) -> None:
openHelp(HelpPage.ADDING_CARD_AND_NOTE)
def _load_new_note(self) -> None:
def _load_new_note(
self, deck_id: DeckId | None = None, notetype_id: NotetypeId | None = None
) -> None:
self.editor.load_note(
mid=self.mw.col.models.current()["id"],
mid=notetype_id,
deck_id=deck_id,
focus_to=0,
)

View file

@ -8,7 +8,7 @@ import json
import math
import re
from collections.abc import Callable, Sequence
from typing import Any, cast
from typing import Any
from markdown import markdown
@ -79,7 +79,6 @@ from aqt.utils import (
tr,
)
from ..addcards import NewAddCards as AddCards
from ..changenotetype import change_notetype_dialog
from .card_info import BrowserCardInfo
from .find_and_replace import FindAndReplaceDialog
@ -269,11 +268,10 @@ class Browser(QMainWindow):
return None
def add_card(self, deck_id: DeckId):
add_cards = cast(AddCards, aqt.dialogs.open("NewAddCards", self.mw))
add_cards.set_deck(deck_id)
args = [self.mw, deck_id]
if note_type_id := self.get_active_note_type_id():
add_cards.set_note_type(note_type_id)
args.append(note_type_id)
aqt.dialogs.open("NewAddCards", *args)
# If in the Browser we open Preview and press Ctrl+W there,
# both Preview and Browser windows get closed by Qt out of the box.

View file

@ -14,6 +14,7 @@ from random import randrange
from typing import Any
from anki.cards import Card
from anki.decks import DeckId
from anki.hooks import runFilter
from anki.models import NotetypeId
from anki.notes import Note, NoteId
@ -411,7 +412,8 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
@on_editor_ready
def load_note(
self,
mid: int,
mid: int | None = None,
deck_id: DeckId | None = None,
original_note_id: NoteId | None = None,
focus_to: int | None = None,
) -> None:
@ -433,6 +435,7 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
focusTo=focus_to,
originalNoteId=original_note_id,
reviewerCardId=self.mw.reviewer.card.id if self.mw.reviewer.card else None,
deckId=deck_id,
initial=True,
)
js = f"loadNote({json.dumps(load_args)});"
@ -443,8 +446,12 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
def reload_note(self) -> None:
self.web.eval("reloadNote();")
def reload_note_if_empty(self) -> None:
self.web.eval("reloadNoteIfEmpty();")
def reload_note_if_empty(
self, deck_id: DeckId | None = None, notetype_id: NotetypeId | None = None
) -> None:
self.web.eval(
f"reloadNoteIfEmpty({json.dumps(deck_id)}, {json.dumps(notetype_id)});"
)
def call_after_note_saved(
self, callback: Callable, keepFocus: bool = False

View file

@ -31,6 +31,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
interface LoadNoteArgs {
nid: bigint | null;
notetypeId: bigint | null;
deckId: bigint | null;
focusTo: number;
originalNoteId: bigint | null;
reviewerCardId: bigint | null;
@ -959,6 +960,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
async function loadNoteInner({
nid,
notetypeId,
deckId,
focusTo,
originalNoteId,
reviewerCardId,
@ -970,13 +972,18 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
reviewerCard = await getCard({ cid: reviewerCardId });
homeDeckId = reviewerCard.originalDeckId || reviewerCard.deckId;
}
if (initial) {
if (initial && mode === "add") {
const chooserDefaults = await defaultsForAdding({
homeDeckOfCurrentReviewCard: homeDeckId,
});
notetypeChooser.select(chooserDefaults.notetypeId);
deckChooser.select(chooserDefaults.deckId);
notetypeId = chooserDefaults.notetypeId;
if (!deckId) {
deckId = chooserDefaults.deckId;
}
if (!notetypeId) {
notetypeId = chooserDefaults.notetypeId;
}
deckChooser.select(deckId);
notetypeChooser.select(notetypeId);
}
const notetype = await getNotetype({
@ -1131,7 +1138,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
async function loadNote({
nid = note?.id,
notetypeId = notetypeMeta?.id,
notetypeId = notetypeMeta ? notetypeMeta.id : null,
deckId = null,
focusTo = 0,
originalNoteId = null,
reviewerCardId = reviewerCard ? reviewerCard.id : null,
@ -1142,6 +1150,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
await loadNoteInner({
nid,
notetypeId,
deckId,
focusTo,
originalNoteId,
reviewerCardId,
@ -1155,11 +1164,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
await loadNote();
}
async function reloadNoteIfEmpty() {
async function reloadNoteIfEmpty(deckId: bigint | null, notetypeId: bigint | null) {
const isEmpty =
(await noteFieldsCheck(note!)).state == NoteFieldsCheckResponse_State.EMPTY;
if (isEmpty) {
await loadNote({ initial: true });
await loadNote({ initial: true, deckId, notetypeId });
}
}