Allow for passing in custom note types for rendering ephemeral cards

This commit is contained in:
Henrik Giesel 2021-02-12 01:53:03 +01:00
parent 6e9dfefb24
commit 270cef63f4
2 changed files with 15 additions and 21 deletions

View File

@ -78,7 +78,11 @@ class Note:
return joinFields(self.fields) return joinFields(self.fields)
def ephemeral_card( def ephemeral_card(
self, ord: int = 0, *, fill_empty: bool = False self,
ord: int = 0,
*,
custom_note_type: NoteType = None,
fill_empty: bool = False,
) -> anki.cards.Card: ) -> anki.cards.Card:
card = anki.cards.Card(self.col) card = anki.cards.Card(self.col)
card.ord = ord card.ord = ord
@ -86,7 +90,11 @@ class Note:
model = self.model() model = self.model()
template = copy.copy( template = copy.copy(
model["tmpls"][ord] if model["type"] == MODEL_STD else model["tmpls"][0] custom_note_type or (
model["tmpls"][ord]
if model["type"] == MODEL_STD
else model["tmpls"][0]
)
) )
# may differ in cloze case # may differ in cloze case
template["ord"] = card.ord template["ord"] = card.ord

View File

@ -474,7 +474,11 @@ class CardLayout(QDialog):
def _renderPreview(self) -> None: def _renderPreview(self) -> None:
self.cancelPreviewTimer() 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.current_template(),
fill_empty=self.fill_empty_action_toggled,
)
ti = self.maybeTextInput ti = self.maybeTextInput
@ -535,24 +539,6 @@ class CardLayout(QDialog):
repl = answerRepl repl = answerRepl
return re.sub(r"\[\[type:.+?\]\]", repl, txt) 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 # Card operations
###################################################################### ######################################################################