From d5859163139e38f12e372938534f1a49c55c2201 Mon Sep 17 00:00:00 2001 From: Abdo Date: Thu, 12 Jun 2025 05:00:33 +0300 Subject: [PATCH] Fix lint errors --- qt/aqt/editor.py | 6 ++-- qt/aqt/mediasrv.py | 6 ++-- rslib/src/media/service.rs | 41 +++++++++++++++++++-------- ts/routes/editor/NoteEditor.svelte | 45 +++++++++++++++++++----------- 4 files changed, 64 insertions(+), 34 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index 8424b3b35..c0aa30452 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -33,7 +33,7 @@ from anki.cards import Card from anki.collection import Config from anki.hooks import runFilter from anki.httpclient import HttpClient -from anki.models import NotetypeDict, NotetypeId, StockNotetype +from anki.models import NotetypeDict, StockNotetype from anki.notes import Note, NoteId from anki.utils import checksum, is_mac, is_win, namedtmp from aqt import AnkiQt, gui_hooks @@ -948,10 +948,10 @@ class EditorWebView(AnkiWebView): clip = self.editor.mw.app.clipboard() assert clip is not None clip.dataChanged.connect(self._on_clipboard_change) - self.settings().setAttribute( + self.settings().setAttribute( # type: ignore QWebEngineSettings.WebAttribute.JavascriptCanPaste, True ) - self.settings().setAttribute( + self.settings().setAttribute( # type: ignore QWebEngineSettings.WebAttribute.JavascriptCanAccessClipboard, True ) gui_hooks.editor_web_view_did_init(self) diff --git a/qt/aqt/mediasrv.py b/qt/aqt/mediasrv.py index 8ae7f03d6..ce30e3aad 100644 --- a/qt/aqt/mediasrv.py +++ b/qt/aqt/mediasrv.py @@ -707,10 +707,12 @@ async def open_file_picker() -> bytes: def cb(filename: str | None) -> None: loop.call_soon_threadsafe(future.set_result, filename) + window = aqt.mw.app.activeWindow() + assert window is not None getFile( - parent=aqt.mw.app.activeWindow(), + parent=window, title=req.title, - cb=cb, + cb=cast(Callable[[Any], None], cb), filter=f"{req.filter_description} ({' '.join(f'*.{ext}' for ext in req.extensions)})", key=req.key, ) diff --git a/rslib/src/media/service.rs b/rslib/src/media/service.rs index b69f4014d..b0515e65a 100644 --- a/rslib/src/media/service.rs +++ b/rslib/src/media/service.rs @@ -43,14 +43,17 @@ impl crate::services::MediaService for Collection { .into()) } - fn add_media_from_path(&mut self, input: AddMediaFromPathRequest) -> error::Result { - let base_name = Path::new(&input.path).file_name().unwrap_or_default().to_str().unwrap_or_default(); + fn add_media_from_path( + &mut self, + input: AddMediaFromPathRequest, + ) -> error::Result { + let base_name = Path::new(&input.path) + .file_name() + .unwrap_or_default() + .to_str() + .unwrap_or_default(); let data = std::fs::read(&input.path)?; - Ok(self - .media()? - .add_file(&base_name, &data)? - .to_string() - .into()) + Ok(self.media()?.add_file(base_name, &data)?.to_string().into()) } fn trash_media_files(&mut self, input: TrashMediaFilesRequest) -> error::Result<()> { @@ -80,13 +83,27 @@ impl crate::services::MediaService for Collection { Ok(files.into_iter().collect::>().into()) } - - fn extract_media_files(&mut self, html: anki_proto::generic::String) -> error::Result { - let files = extract_media_refs(&html.val).iter().map(|r| r.fname_decoded.to_string()).collect::>(); + fn extract_media_files( + &mut self, + html: anki_proto::generic::String, + ) -> error::Result { + let files = extract_media_refs(&html.val) + .iter() + .map(|r| r.fname_decoded.to_string()) + .collect::>(); Ok(files.into()) } - fn get_absolute_media_path(&mut self, path: anki_proto::generic::String) -> error::Result { - Ok(self.media()?.media_folder.join(path.val).to_string_lossy().to_string().into()) + fn get_absolute_media_path( + &mut self, + path: anki_proto::generic::String, + ) -> error::Result { + Ok(self + .media()? + .media_folder + .join(path.val) + .to_string_lossy() + .to_string() + .into()) } } diff --git a/ts/routes/editor/NoteEditor.svelte b/ts/routes/editor/NoteEditor.svelte index 3086a1095..49fcb8532 100644 --- a/ts/routes/editor/NoteEditor.svelte +++ b/ts/routes/editor/NoteEditor.svelte @@ -24,7 +24,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } import { registerPackage } from "@tslib/runtime-require"; - import { filenameToLink, openFilePickerForImageOcclusion, readImageFromClipboard } from "./rich-text-input/data-transfer"; + import { + filenameToLink, + openFilePickerForImageOcclusion, + readImageFromClipboard, + } from "./rich-text-input/data-transfer"; import contextProperty from "$lib/sveltelib/context-property"; import lifecycleHooks from "$lib/sveltelib/lifecycle-hooks"; @@ -543,7 +547,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import PreviewButton from "./PreviewButton.svelte"; import { NoteFieldsCheckResponse_State, type Note } from "@generated/anki/notes_pb"; - $: isIOImageLoaded = false; $: ioImageLoadedStore.set(isIOImageLoaded); let imageOcclusionMode: IOMode | undefined; @@ -552,7 +555,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html async function pickIOImage() { imageOcclusionMode = undefined; const filename = await openFilePickerForImageOcclusion(); - if(!filename) { + if (!filename) { return; } setupMaskEditor(filename); @@ -564,7 +567,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } async function setupMaskEditor(filename: string) { - if(mode == "add") { + if (mode == "add") { setupMaskEditorForNewNote(filename); } else { setupMaskEditorForExistingNote(filename); @@ -596,9 +599,13 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } async function setupMaskEditorForNewNote(imagePath: string) { - const imageFieldHtml = filenameToLink((await addMediaFromPath({ - path: imagePath, - })).val); + const imageFieldHtml = filenameToLink( + ( + await addMediaFromPath({ + path: imagePath, + }) + ).val, + ); setupMaskEditorInner({ html: imageFieldHtml, mode: { @@ -611,25 +618,29 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html async function setupMaskEditorForExistingNote(imagePath: string | null = null) { if (imagePath) { - const imageFieldHtml = filenameToLink((await addMediaFromPath({ - path: imagePath, - })).val); + const imageFieldHtml = filenameToLink( + ( + await addMediaFromPath({ + path: imagePath, + }) + ).val, + ); resetIOImage(imagePath, () => {}); setImageField(imageFieldHtml); } setupMaskEditorInner({ - html: note!.fields[ioFields.image], - mode: { - kind: "edit", - noteId: note!.id, - }, - }); + html: note!.fields[ioFields.image], + mode: { + kind: "edit", + noteId: note!.id, + }, + }); } async function setupMaskEditorFromClipboard() { const path = await readImageFromClipboard(); console.log("setupMaskEditorFromClipboard path", path); - if(path) { + if (path) { setupMaskEditor(path); } else { alert(tr.editingNoImageFoundOnClipboard());