Fix add-on usages of addMedia (#1721)

* Expose old .addMedia again, and use a new resolve_media for built-in uses

* Add an explaining comment for addMedia

* Add some docstrings (dae)
This commit is contained in:
Henrik Giesel 2022-03-16 01:29:06 +01:00 committed by GitHub
parent 02ba50f277
commit 2d00b6659f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 14 deletions

View file

@ -688,13 +688,15 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
###################################################################### ######################################################################
def onAddMedia(self) -> None: def onAddMedia(self) -> None:
"""Show a file selection screen, then add the selected media.
This expects initial setup to have been done by TemplateButtons.svelte."""
extension_filter = " ".join( extension_filter = " ".join(
f"*.{extension}" for extension in sorted(itertools.chain(pics, audio)) f"*.{extension}" for extension in sorted(itertools.chain(pics, audio))
) )
filter = f"{tr.editing_media()} ({extension_filter})" filter = f"{tr.editing_media()} ({extension_filter})"
def accept(file: str) -> None: def accept(file: str) -> None:
self.addMedia(file) self.resolve_media(file)
file = getFile( file = getFile(
parent=self.widget, parent=self.widget,
@ -707,8 +709,20 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
self.parentWindow.activateWindow() self.parentWindow.activateWindow()
def addMedia(self, path: str, canDelete: bool = False) -> None: def addMedia(self, path: str, canDelete: bool = False) -> None:
"""canDelete is a legacy arg and is ignored.""" """Legacy routine used by add-ons to add a media file and update the current field.
canDelete is ignored."""
try:
html = self._addMedia(path)
except Exception as e:
showWarning(str(e))
return
self.web.eval(f"setFormat('inserthtml', {json.dumps(html)});")
def resolve_media(self, path: str) -> None:
"""Finish inserting media into a field.
This expects initial setup to have been done by TemplateButtons.svelte."""
try: try:
html = self._addMedia(path) html = self._addMedia(path)
except Exception as e: except Exception as e:
@ -716,7 +730,7 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
return return
self.web.eval( self.web.eval(
f'require("anki/TemplateButtons").mediaResolve({json.dumps(html)})' f'require("anki/TemplateButtons").resolveMedia({json.dumps(html)})'
) )
def _addMedia(self, path: str, canDelete: bool = False) -> str: def _addMedia(self, path: str, canDelete: bool = False) -> str:
@ -734,7 +748,7 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
self.parentWindow, self.parentWindow,
self.mw, self.mw,
True, True,
lambda file: self.addMedia(file, canDelete=True), self.resolve_media,
) )
# Media downloads # Media downloads

View file

@ -31,13 +31,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
let mediaPromise: Promise<string>; let mediaPromise: Promise<string>;
let resolve: (media: string) => void; let resolve: (media: string) => void;
function mediaResolve(media: string): void { function resolveMedia(media: string): void {
resolve(media); resolve?.(media);
} }
registerPackage("anki/TemplateButtons", { mediaResolve }); function attachMediaOnFocus(): void {
function onAttachment(): void {
if (!editingInputIsRichText($focusedInput)) { if (!editingInputIsRichText($focusedInput)) {
return; return;
} }
@ -51,9 +49,13 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
bridgeCommand("attach"); bridgeCommand("attach");
} }
registerPackage("anki/TemplateButtons", {
resolveMedia,
});
const recordCombination = "F5"; const recordCombination = "F5";
function onRecord(): void { function attachRecordingOnFocus(): void {
if (!editingInputIsRichText($focusedInput)) { if (!editingInputIsRichText($focusedInput)) {
return; return;
} }
@ -87,11 +89,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
)})" )})"
iconSize={70} iconSize={70}
{disabled} {disabled}
on:click={onAttachment} on:click={attachMediaOnFocus}
> >
{@html paperclipIcon} {@html paperclipIcon}
</IconButton> </IconButton>
<Shortcut keyCombination={attachmentCombination} on:action={onAttachment} /> <Shortcut
keyCombination={attachmentCombination}
on:action={attachMediaOnFocus}
/>
</ButtonGroupItem> </ButtonGroupItem>
<ButtonGroupItem> <ButtonGroupItem>
@ -101,11 +106,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
)})" )})"
iconSize={70} iconSize={70}
{disabled} {disabled}
on:click={onRecord} on:click={attachRecordingOnFocus}
> >
{@html micIcon} {@html micIcon}
</IconButton> </IconButton>
<Shortcut keyCombination={recordCombination} on:action={onRecord} /> <Shortcut
keyCombination={recordCombination}
on:action={attachRecordingOnFocus}
/>
</ButtonGroupItem> </ButtonGroupItem>
<ButtonGroupItem id="cloze"> <ButtonGroupItem id="cloze">