diff --git a/apps/vectorcraft-web/Cargo.toml b/apps/vectorcraft-web/Cargo.toml index 70aae32f73..02fc1801d4 100644 --- a/apps/vectorcraft-web/Cargo.toml +++ b/apps/vectorcraft-web/Cargo.toml @@ -18,7 +18,7 @@ vectorcraft-engine = { workspace = true, features = ["bundled-fonts", "heif"] } vectorcraft-ui-egui = { workspace = true } vectorcraft-format = { workspace = true } vectorcraft-text = { workspace = true } -craft = { workspace = true, features = ["ui", "keymap", "automation", "platform"] } +craft = { workspace = true, features = ["ui", "keymap", "automation", "platform", "print"] } egui = { workspace = true } log = { workspace = true } @@ -34,4 +34,4 @@ wasm-bindgen = "0.2" serde_json = { workspace = true } wasm-bindgen-futures = "0.4" js-sys = "0.3" -web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlInputElement", "KeyboardEvent", "HtmlIFrameElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "ClipboardEvent", "DataTransfer", "File", "FileList", "Url", "Location", "CssStyleDeclaration", "Event", "EventTarget", "MouseEvent", "PointerEvent", "TouchEvent", "AddEventListenerOptions", "DomRect", "DragEvent", "Storage", "Navigator", "RequestInit", "Response", "AbortController", "AbortSignal", "Node", "Worker", "MessageEvent", "ErrorEvent", "Performance"] } +web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlInputElement", "KeyboardEvent", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "ClipboardEvent", "DataTransfer", "File", "FileList", "Url", "Location", "CssStyleDeclaration", "Event", "EventTarget", "MouseEvent", "PointerEvent", "TouchEvent", "AddEventListenerOptions", "DomRect", "DragEvent", "Storage", "Navigator", "RequestInit", "Response", "AbortController", "AbortSignal", "Node", "Worker", "MessageEvent", "ErrorEvent", "Performance"] } diff --git a/apps/vectorcraft-web/src/web.rs b/apps/vectorcraft-web/src/web.rs index 64a596e5ba..ca0226632b 100644 --- a/apps/vectorcraft-web/src/web.rs +++ b/apps/vectorcraft-web/src/web.rs @@ -566,9 +566,6 @@ fn blob_url(bytes: &[u8], mime: &str) -> Result { /// print dialog (which picks the printer). struct BrowserPrint; -/// The frame holding the last print job (the next job replaces it). -const PRINT_FRAME_ID: &str = "vectorcraft-print-frame"; - impl PrintService for BrowserPrint { fn printers(&mut self) -> Vec { // Browsers don't tell pages about printers: their print dialog lists them. @@ -576,39 +573,7 @@ impl PrintService for BrowserPrint { } fn print(&mut self, job: &PrintJob) -> Result { - print_pdf(job.pdf)?; + craft::print::browser::print_pdf(job.pdf)?; Ok(format!("Printing “{}”: pick the printer in the browser's print dialog", job.title)) } } - -fn print_pdf(pdf: &[u8]) -> Result<(), String> { - let document = web_sys::window().and_then(|w| w.document()).ok_or("no document")?; - // The previous job's frame and its blob go first. - if let Some(old) = document.get_element_by_id(PRINT_FRAME_ID) { - if let Some(url) = old.get_attribute("src") { - web_sys::Url::revoke_object_url(&url).ok(); - } - old.remove(); - } - let url = blob_url(pdf, "application/pdf")?; - let frame: web_sys::HtmlIFrameElement = document.create_element("iframe").map_err(js_err)?.dyn_into().map_err(|_| "not a frame")?; - frame.set_id(PRINT_FRAME_ID); - // Out of sight but laid out: browsers don't print a frame that isn't. - let style = frame.style(); - for (k, v) in [("position", "fixed"), ("right", "0"), ("bottom", "0"), ("width", "0"), ("height", "0"), ("border", "0")] { - style.set_property(k, v).map_err(js_err)?; - } - let loaded = frame.clone(); - let onload = wasm_bindgen::closure::Closure::once_into_js(move || { - if let Some(w) = loaded.content_window() { - w.focus().ok(); - if let Err(e) = w.print() { - log::error!("printing failed: {e:?}"); - } - } - }); - frame.set_onload(Some(onload.unchecked_ref())); - frame.set_src(&url); - document.body().ok_or("no body")?.append_child(&frame).map_err(js_err)?; - Ok(()) -}