diff --git a/pylib/anki/notes.py b/pylib/anki/notes.py index a9be4f69a..19b8e5fb8 100644 --- a/pylib/anki/notes.py +++ b/pylib/anki/notes.py @@ -11,7 +11,7 @@ import anki # pylint: disable=unused-import import anki._backend.backend_pb2 as _pb from anki import hooks from anki.consts import MODEL_STD -from anki.models import NoteType +from anki.models import NoteType, Template from anki.utils import joinFields @@ -78,15 +78,23 @@ class Note: return joinFields(self.fields) def ephemeral_card( - self, ord: int = 0, *, fill_empty: bool = False + self, + ord: int = 0, + *, + custom_note_type: NoteType = None, + custom_template: Template = None, + fill_empty: bool = False, ) -> anki.cards.Card: card = anki.cards.Card(self.col) card.ord = ord card.did = 1 - model = self.model() + model = custom_note_type or self.model() template = copy.copy( - model["tmpls"][ord] if model["type"] == MODEL_STD else model["tmpls"][0] + custom_template + or ( + model["tmpls"][ord] if model["type"] == MODEL_STD else model["tmpls"][0] + ) ) # may differ in cloze case template["ord"] = card.ord diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index 06035ca4f..fc935d753 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -1,18 +1,15 @@ # Copyright: Ankitects Pty Ltd and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -import copy import json import re from concurrent.futures import Future from typing import Any, Dict, List, Match, Optional import aqt -from anki.cards import Card from anki.consts import * from anki.errors import TemplateError from anki.lang import without_unicode_isolation from anki.notes import Note -from anki.template import TemplateRenderContext from aqt import AnkiQt, gui_hooks from aqt.forms.browserdisp import Ui_Dialog from aqt.qt import * @@ -474,7 +471,12 @@ class CardLayout(QDialog): def _renderPreview(self) -> None: self.cancelPreviewTimer() - c = self.rendered_card = self.ephemeral_card_for_rendering() + c = self.rendered_card = self.note.ephemeral_card( + self.ord, + custom_note_type=self.model, + custom_template=self.current_template(), + fill_empty=self.fill_empty_action_toggled, + ) ti = self.maybeTextInput @@ -535,24 +537,6 @@ class CardLayout(QDialog): repl = answerRepl return re.sub(r"\[\[type:.+?\]\]", repl, txt) - def ephemeral_card_for_rendering(self) -> Card: - card = Card(self.col) - card.ord = self.ord - card.did = 1 - template = copy.copy(self.current_template()) - # may differ in cloze case - template["ord"] = card.ord - output = TemplateRenderContext.from_card_layout( - self.note, - card, - notetype=self.model, - template=template, - fill_empty=self.fill_empty_action_toggled, - ).render() - card.set_render_output(output) - card._note = self.note - return card - # Card operations ######################################################################