Merge pull request #972 from hgiesel/ephemeral
Move ephemeral card functionality from clayout to Note class
This commit is contained in:
commit
86f8cba22c
@ -3,12 +3,14 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import copy
|
||||||
import pprint
|
import pprint
|
||||||
from typing import Any, List, Optional, Sequence, Tuple
|
from typing import Any, List, Optional, Sequence, Tuple
|
||||||
|
|
||||||
import anki # pylint: disable=unused-import
|
import anki # pylint: disable=unused-import
|
||||||
import anki._backend.backend_pb2 as _pb
|
import anki._backend.backend_pb2 as _pb
|
||||||
from anki import hooks
|
from anki import hooks
|
||||||
|
from anki.consts import MODEL_STD
|
||||||
from anki.models import NoteType
|
from anki.models import NoteType
|
||||||
from anki.utils import joinFields
|
from anki.utils import joinFields
|
||||||
|
|
||||||
@ -75,6 +77,31 @@ class Note:
|
|||||||
def joinedFields(self) -> str:
|
def joinedFields(self) -> str:
|
||||||
return joinFields(self.fields)
|
return joinFields(self.fields)
|
||||||
|
|
||||||
|
def ephemeral_card(
|
||||||
|
self, ord: int = 0, *, fill_empty: bool = False
|
||||||
|
) -> anki.cards.Card:
|
||||||
|
card = anki.cards.Card(self.col)
|
||||||
|
card.ord = ord
|
||||||
|
card.did = 1
|
||||||
|
|
||||||
|
model = self.model()
|
||||||
|
template = copy.copy(
|
||||||
|
model["tmpls"][ord] if model["type"] == MODEL_STD else model["tmpls"][0]
|
||||||
|
)
|
||||||
|
# may differ in cloze case
|
||||||
|
template["ord"] = card.ord
|
||||||
|
|
||||||
|
output = anki.template.TemplateRenderContext.from_card_layout(
|
||||||
|
self,
|
||||||
|
card,
|
||||||
|
notetype=model,
|
||||||
|
template=template,
|
||||||
|
fill_empty=fill_empty,
|
||||||
|
).render()
|
||||||
|
card.set_render_output(output)
|
||||||
|
card._note = self
|
||||||
|
return card
|
||||||
|
|
||||||
def cards(self) -> List[anki.cards.Card]:
|
def cards(self) -> List[anki.cards.Card]:
|
||||||
return [self.col.getCard(id) for id in self.card_ids()]
|
return [self.col.getCard(id) for id in self.card_ids()]
|
||||||
|
|
||||||
|
@ -1,19 +1,16 @@
|
|||||||
# Copyright: Ankitects Pty Ltd and contributors
|
# Copyright: Ankitects Pty Ltd and contributors
|
||||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
import copy
|
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
from concurrent.futures import Future
|
from concurrent.futures import Future
|
||||||
from typing import Any, Dict, List, Match, Optional
|
from typing import Any, Dict, List, Match, Optional
|
||||||
|
|
||||||
import aqt
|
import aqt
|
||||||
from anki.cards import Card
|
|
||||||
from anki.consts import *
|
from anki.consts import *
|
||||||
from anki.errors import TemplateError
|
from anki.errors import TemplateError
|
||||||
from anki.lang import without_unicode_isolation
|
from anki.lang import without_unicode_isolation
|
||||||
from anki.notes import Note
|
from anki.notes import Note
|
||||||
from anki.template import TemplateRenderContext
|
|
||||||
from aqt import AnkiQt, gui_hooks
|
from aqt import AnkiQt, gui_hooks
|
||||||
from aqt.forms.browserdisp import Ui_Dialog
|
from aqt.forms.browserdisp import Ui_Dialog
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
@ -475,8 +472,9 @@ 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, fill_empty=self.fill_empty_action_toggled
|
||||||
|
)
|
||||||
ti = self.maybeTextInput
|
ti = self.maybeTextInput
|
||||||
|
|
||||||
bodyclass = theme_manager.body_classes_for_card_ord(
|
bodyclass = theme_manager.body_classes_for_card_ord(
|
||||||
@ -536,24 +534,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
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user