From 56f7d549003ebfe6b787b924d2d216f571c07136 Mon Sep 17 00:00:00 2001 From: Aristotelis <5459332+glutanimate@users.noreply.github.com> Date: Sun, 22 Oct 2023 02:40:40 +0200 Subject: [PATCH] Add APIs for IO mask editing (#2758) * Add simple mask editor add-on API * Signal completed mask editor image loading to Python * Add API methods for querying mask editor state, fix formatting * Use event forwarding to propagate image loaded event Should fix mobile support by moving all bridgeCommand calls to `NoteEditor.svelte` * Add shape classes to mask editor API --------- Co-authored-by: Glutanimate --- qt/aqt/editor.py | 10 ++++ qt/tools/genhooks_gui.py | 9 ++++ ts/editor/NoteEditor.svelte | 19 ++++++- ts/image-occlusion/ImageOcclusionPage.svelte | 2 +- ts/image-occlusion/MaskEditor.svelte | 26 +++++++--- ts/image-occlusion/mask-editor.ts | 12 ++++- ts/image-occlusion/shapes/index.ts | 1 + ts/image-occlusion/shapes/to-cloze.ts | 2 +- ts/image-occlusion/tools/add-from-cloze.ts | 26 +++------- ts/image-occlusion/tools/api.ts | 54 ++++++++++++++++++++ ts/image-occlusion/tools/from-shapes.ts | 34 ++++++++++++ ts/image-occlusion/tools/lib.ts | 14 +++-- 12 files changed, 175 insertions(+), 34 deletions(-) create mode 100644 ts/image-occlusion/tools/api.ts create mode 100644 ts/image-occlusion/tools/from-shapes.ts diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index 5087e1579..b706c324c 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -493,6 +493,16 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too EditorState(int(new_state_id)), EditorState(int(old_state_id)) ) + elif cmd.startswith("ioImageLoaded"): + (_, path_or_nid_data) = cmd.split(":", 1) + path_or_nid = json.loads(path_or_nid_data) + if self.addMode: + gui_hooks.editor_mask_editor_did_load_image(self, path_or_nid) + else: + gui_hooks.editor_mask_editor_did_load_image( + self, NoteId(int(path_or_nid)) + ) + elif cmd in self._links: return self._links[cmd](self) diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index bb7cf10dc..57c5443cf 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -1155,6 +1155,15 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) doc="""Called when the input state of the editor changes, e.g. when switching to an image occlusion note type.""", ), + Hook( + name="editor_mask_editor_did_load_image", + args=["editor: aqt.editor.Editor", "path_or_nid: str | anki.notes.NoteId"], + doc="""Called when the image occlusion mask editor has completed + loading an image. + + When adding new notes `path_or_nid` will be the path to the image file. + When editing existing notes `path_or_nid` will be the note id.""", + ), # Tag ################### Hook(name="tag_editor_did_process_key", args=["tag_edit: TagEdit", "evt: QEvent"]), diff --git a/ts/editor/NoteEditor.svelte b/ts/editor/NoteEditor.svelte index 7ae589082..da9e1938f 100644 --- a/ts/editor/NoteEditor.svelte +++ b/ts/editor/NoteEditor.svelte @@ -42,7 +42,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html