Fix lint errors

This commit is contained in:
Abdo 2025-06-12 05:00:33 +03:00
parent 13c2dd201a
commit d585916313
4 changed files with 64 additions and 34 deletions

View file

@ -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)

View file

@ -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,
)

View file

@ -43,14 +43,17 @@ impl crate::services::MediaService for Collection {
.into())
}
fn add_media_from_path(&mut self, input: AddMediaFromPathRequest) -> error::Result<generic::String> {
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<generic::String> {
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::<Vec<_>>().into())
}
fn extract_media_files(&mut self, html: anki_proto::generic::String) -> error::Result<generic::StringList> {
let files = extract_media_refs(&html.val).iter().map(|r| r.fname_decoded.to_string()).collect::<Vec<_>>();
fn extract_media_files(
&mut self,
html: anki_proto::generic::String,
) -> error::Result<generic::StringList> {
let files = extract_media_refs(&html.val)
.iter()
.map(|r| r.fname_decoded.to_string())
.collect::<Vec<_>>();
Ok(files.into())
}
fn get_absolute_media_path(&mut self, path: anki_proto::generic::String) -> error::Result<generic::String> {
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<generic::String> {
Ok(self
.media()?
.media_folder
.join(path.val)
.to_string_lossy()
.to_string()
.into())
}
}

View file

@ -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());