ElyMark: the print page is staged off the window thread (M-14)

paper::stage takes the background executor and inlines images, reads
the style sheet and writes the page there. A gpui test checks nothing
is written before the executor runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-10 14:14:56 -04:00
1 parent ca5ac5bb45
commit 6247771f24
5 files changed
+62 -15

No files matched your search

+10 -14
View File
@@ -15,7 +15,7 @@ use ely_ui::{commands, i18n::text};
use ely_web_content::{Event, WebView};
use gpui::{
AnyElement, App, AppContext as _, Context, Entity, IntoElement as _, ParentElement as _,
Subscription, Window,
Subscription, Task, Window,
};
use serde_json::Value;
@@ -62,7 +62,7 @@ pub struct Lost {
}
impl Lost {
fn of(images: Vec<String>, rendered: &Value) -> Lost {
pub fn of(images: Vec<String>, rendered: &Value) -> Lost {
let diagrams = rendered["failed"].as_array().into_iter().flatten();
let formulas = rendered["formulas"].as_array().into_iter().flatten();
Lost {
@@ -280,19 +280,15 @@ impl Desk {
"html",
move |rendered, cx: &mut App| {
let staged = work.join(format!("print-{n}.html"));
let writing = staged.clone();
let page = match rendered {
Ok(rendered) => {
let print = paper::Print { folder, style, name, page: staged.clone() };
paper::stage(cx.background_executor(), rendered, print)
}
Err(why) => Task::ready(Err(io::Error::other(why))),
};
cx.spawn(async move |cx| {
// Images inline and the page writes off the window thread.
let page = cx
.background_spawn(async move {
rendered.map_err(io::Error::other).and_then(|rendered| {
let (html, images) = paper::Marked::read(&rendered)?.inline(&folder);
let style = format!("<style>{}</style>", std::fs::read_to_string(&style)?);
std::fs::write(&writing, paper::page(&name, &style, &html))?;
Ok(Lost::of(images, &rendered))
})
})
.await;
let page = page.await;
in_window(&desk, cx, |desk, window, cx| match page {
Ok(lost) => {
let job = Job {
+47
View File
@@ -85,6 +85,28 @@ fn kind(path: &Path) -> &'static str {
}
}
/// What the print page is made of.
pub struct Print {
pub folder: std::path::PathBuf,
pub style: std::path::PathBuf,
pub name: String,
pub page: std::path::PathBuf,
}
/// Writes the print page off the window thread, images inline.
pub fn stage(
background: &gpui::BackgroundExecutor,
rendered: serde_json::Value,
print: Print,
) -> gpui::Task<std::io::Result<crate::export::Lost>> {
background.spawn(async move {
let (html, images) = Marked::read(&rendered)?.inline(&print.folder);
let style = format!("<style>{}</style>", std::fs::read_to_string(&print.style)?);
std::fs::write(&print.page, page(&print.name, &style, &html))?;
Ok(crate::export::Lost::of(images, &rendered))
})
}
/// The page's HTML; image `src` values replaced by numbered tokens.
#[derive(Clone, Debug, Default)]
pub struct Marked {
@@ -222,6 +244,31 @@ mod tests {
assert!(Marked::read(&serde_json::json!({ "html": "<p>" })).is_err());
}
#[gpui::test]
fn the_print_page_is_written_off_the_window_thread(cx: &mut gpui::TestAppContext) {
let dir = tempfile::tempdir().expect("a temp dir");
std::fs::write(dir.path().join("a.png"), b"png").expect("an image");
std::fs::write(dir.path().join("export.css"), "p {}").expect("a style");
let rendered = serde_json::json!({
"html": "<p>hi</p><img src=\"T0\"><img src=\"T1\">",
"token": "T",
"images": ["a.png", "gone.png"],
"failed": [3],
});
let print = Print {
folder: dir.path().into(),
style: dir.path().join("export.css"),
name: "notes".into(),
page: dir.path().join("print-1.html"),
};
let staging = stage(&cx.executor(), rendered, print);
assert!(!dir.path().join("print-1.html").exists(), "staged on the window thread");
let lost = cx.foreground_executor().block_test(staging).expect("staged");
let written = std::fs::read_to_string(dir.path().join("print-1.html")).expect("the page");
assert!(written.contains("<style>p {}</style>") && written.contains("data:image/png;base64,cG5n"));
assert_eq!(format!("{lost:?}"), r#"Lost { images: ["gone.png"], diagrams: [3], formulas: [] }"#);
}
#[test]
fn titles_escape() {
assert!(page("a<b>&", "", "").contains("<title>a&lt;b&gt;&amp;</title>"));