Skip to content

Commit

Permalink
Tidy up i/o notetype check and fix error
Browse files Browse the repository at this point in the history
- Make it a method on editor
- Use .get(), because the setting doesn't exist on older notetypes
- Pass the bool value into the ts code, instead of the enum
  • Loading branch information
dae committed Jun 23, 2023
1 parent 088e195 commit a3c659f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 37 deletions.
5 changes: 2 additions & 3 deletions qt/aqt/addcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
add_close_shortcut,
askUser,
downArrow,
is_image_occlusion_notetype,
openHelp,
restoreGeom,
saveGeom,
Expand Down Expand Up @@ -149,7 +148,7 @@ def setupButtons(self) -> None:
self.historyButton = b

def show_hide_add_buttons(self) -> None:
if is_image_occlusion_notetype(self.editor):
if self.editor.current_notetype_is_image_occlusion():
self.addButton.setVisible(False)
self.addButtonHideAll.setVisible(True)
self.addButtonHideOne.setVisible(True)
Expand Down Expand Up @@ -311,7 +310,7 @@ def _note_can_be_added(self, note: Note) -> bool:
# no problem, duplicate, and confirmed cloze cases
problem = None
if result == NoteFieldsCheckResult.EMPTY:
if is_image_occlusion_notetype(self.editor):
if self.editor.current_notetype_is_image_occlusion():
problem = tr.notetypes_no_occlusion_created()
else:
problem = tr.adding_the_first_field_is_empty()
Expand Down
10 changes: 2 additions & 8 deletions qt/aqt/editcurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
from anki.errors import NotFoundError
from aqt import gui_hooks
from aqt.qt import *
from aqt.utils import (
disable_help_button,
is_image_occlusion_notetype,
restoreGeom,
saveGeom,
tr,
)
from aqt.utils import disable_help_button, restoreGeom, saveGeom, tr


class EditCurrent(QDialog):
Expand Down Expand Up @@ -41,7 +35,7 @@ def __init__(self, mw: aqt.AnkiQt) -> None:
self.show()

def setupButtons(self) -> None:
if is_image_occlusion_notetype(self.editor):
if self.editor.current_notetype_is_image_occlusion():
bb = self.form.buttonBox
ar = QDialogButtonBox.ButtonRole.ActionRole
# add io hide all button
Expand Down
15 changes: 8 additions & 7 deletions qt/aqt/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def oncallback(arg: Any) -> None:
setShrinkImages({json.dumps(self.mw.col.get_config("shrinkEditorImages", True))});
setCloseHTMLTags({json.dumps(self.mw.col.get_config("closeHTMLTags", True))});
triggerChanges();
setOriginalStockKind({json.dumps(self.note.note_type()["originalStockKind"])});
setIsImageOcclusion({json.dumps(self.current_notetype_is_image_occlusion())});
"""

if self.addMode:
Expand All @@ -569,6 +569,12 @@ def oncallback(arg: Any) -> None:
f'require("anki/ui").loaded.then(() => {{ {js} }})', oncallback
)

def current_notetype_is_image_occlusion(self) -> bool:
return bool(self.note) and (
self.note.note_type().get("originalStockKind", None)
== StockNotetype.OriginalStockKind.ORIGINAL_STOCK_KIND_IMAGE_OCCLUSION
)

def _save_current_note(self) -> None:
"Call after note is updated with data from webview."
update_note(parent=self.widget, note=self.note).run_in_background(
Expand Down Expand Up @@ -1512,12 +1518,7 @@ def set_cloze_button(editor: Editor) -> None:


def set_image_occlusion_button(editor: Editor) -> None:
action = (
"show"
if editor.note.note_type()["originalStockKind"]
== StockNotetype.OriginalStockKind.ORIGINAL_STOCK_KIND_IMAGE_OCCLUSION
else "hide"
)
action = "show" if editor.current_notetype_is_image_occlusion() else "hide"
editor.web.eval(
'require("anki/ui").loaded.then(() =>'
f'require("anki/NoteEditor").instances[0].toolbar.toolbar.{action}("image-occlusion-button")'
Expand Down
12 changes: 0 additions & 12 deletions qt/aqt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from anki._legacy import DeprecatedNamesMixinForModule
from anki.collection import Collection, HelpPage
from anki.lang import TR, tr_legacyglobal # pylint: disable=unused-import
from anki.models import StockNotetype
from anki.utils import (
invalid_filename,
is_mac,
Expand Down Expand Up @@ -1287,14 +1286,3 @@ def meta(self) -> bool:

def __getattr__(name: str) -> Any:
return _deprecated_names.__getattr__(name)


######################################################################


# image occlusion utils
def is_image_occlusion_notetype(editor) -> bool:
return (
editor.note.note_type()["originalStockKind"]
== StockNotetype.OriginalStockKind.ORIGINAL_STOCK_KIND_IMAGE_OCCLUSION
)
13 changes: 6 additions & 7 deletions ts/editor/NoteEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
</script>

<script lang="ts">
import { StockNotetype_OriginalStockKind } from "@tslib/anki/notetypes_pb";
import { bridgeCommand } from "@tslib/bridgecommand";
import * as tr from "@tslib/ftl";
import { onMount, tick } from "svelte";
Expand Down Expand Up @@ -248,9 +247,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
return noteId;
}
let originalStockKind: number | null = null;
function setOriginalStockKind(kind: number) {
originalStockKind = kind;
let isImageOcclusion = false;
function setIsImageOcclusion(val: boolean) {
isImageOcclusion = val;
}
let cols: ("dupe" | "")[] = [];
Expand Down Expand Up @@ -447,15 +446,15 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
function setOcclusionField(occludeInactive: boolean) {
// set fields data for occlusion and image fields for io notes type
if (originalStockKind == StockNotetype_OriginalStockKind.IMAGE_OCCLUSION) {
if (isImageOcclusion) {
const occlusionsData = exportShapesToClozeDeletions(occludeInactive);
fieldStores[0].set(occlusionsData.clozes);
}
}
// hide first two fields for occlusion type, first contains occlusion data and second contains image
function hideFieldInOcclusionType(index: number) {
if (originalStockKind == StockNotetype_OriginalStockKind.IMAGE_OCCLUSION) {
if (isImageOcclusion) {
if (index == 0 || index == 1) {
return true;
}
Expand Down Expand Up @@ -497,7 +496,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
setShrinkImages,
setCloseHTMLTags,
triggerChanges,
setOriginalStockKind,
setIsImageOcclusion,
toggleMaskEditor,
setupMaskEditor,
setOcclusionField,
Expand Down

0 comments on commit a3c659f

Please sign in to comment.