DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into zydvc26s67-web-paragraph-styles-save-delete
zydvc26s67-web0a0a6ce5dd0c
diff --cc crates/engine/src/tests.rsindex add42889b,9d6308983..e331b30a6--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@@ -560,113 -570,102 +570,213 @@@ fn commands_take_non_object_params_with}}+#[test]+fn paragraph_style_edit_persists_attrs_and_rename() {+ let mut s = session();+ let name =+ s.execute("style.paragraph.create", &json!({"name": "Body Copy", "para": {"align": "left"}})).unwrap()["name"].as_str().unwrap().to_string();+ s.execute("style.paragraph.edit", &json!({"name": name, "para": {"align": "center"}})).unwrap();+ {+ let st = s.doc().unwrap();+ let style = st.doc.styles.para(&name).expect("style still exists");+ assert_eq!(style.para.align, Some(designcraft_doc::Align::Center), "edited attribute persisted");+ }+ // Renaming updates the style definition itself, and every place it's referenced.+ s.execute("style.paragraph.edit", &json!({"name": name, "rename": "Body Text"})).unwrap();+ let st = s.doc().unwrap();+ assert!(st.doc.styles.para("Body Text").is_some(), "renamed style exists under the new name");+ assert!(st.doc.styles.para(&name).is_none(), "old name is gone");+}++#[test]+fn paragraph_style_edit_rejects_based_on_cycle() {+ let mut s = session();+ let a = s.execute("style.paragraph.create", &json!({"name": "A"})).unwrap()["name"].as_str().unwrap().to_string();+ let b = s.execute("style.paragraph.create", &json!({"name": "B", "basedOn": a})).unwrap()["name"].as_str().unwrap().to_string();+ let r = s.execute("style.paragraph.edit", &json!({"name": a, "basedOn": b}));+ assert!(r.is_err(), "A based on B based on A must be rejected");+}++#[test]+fn paragraph_style_delete_reassigns_affected_text() {+ let mut s = session();+ let name = s.execute("style.paragraph.create", &json!({"name": "Caption"})).unwrap()["name"].as_str().unwrap().to_string();+ let sid = s.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text"})).unwrap()["story"].as_u64().unwrap();+ s.execute("text.insert", &json!({"text": "Hello, world"})).unwrap();+ s.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 0})).unwrap();+ s.execute("style.paragraph.apply", &json!({"name": name})).unwrap();+ {+ let st = s.doc().unwrap();+ let story = st.doc.stories.get(&designcraft_doc::StoryId(sid)).expect("story exists");+ assert_eq!(story.paras[0].style, name, "style applied before delete");+ }+ s.execute("style.paragraph.delete", &json!({"name": name, "replaceWith": designcraft_doc::BASIC_PARAGRAPH})).unwrap();+ let st = s.doc().unwrap();+ assert!(st.doc.styles.para(&name).is_none(), "deleted style is gone");+ let story = st.doc.stories.get(&designcraft_doc::StoryId(sid)).expect("story exists");+ assert_eq!(story.paras[0].style, designcraft_doc::BASIC_PARAGRAPH, "affected paragraph reassigned to the replacement style");+}++#[test]+fn paragraph_style_delete_rejects_builtin_styles() {+ let mut s = session();+ let r = s.execute("style.paragraph.delete", &json!({"name": designcraft_doc::BASIC_PARAGRAPH}));+ assert!(r.is_err(), "built-in styles can't be deleted");+}++#[test]+fn character_style_edit_persists_attrs_and_rename() {+ let mut s = session();+ let name = s.execute("style.character.create", &json!({"name": "Key Term", "chars": {"fontStyle": "Bold"}})).unwrap()["name"]+ .as_str()+ .unwrap()+ .to_string();+ s.execute("style.character.edit", &json!({"name": name, "chars": {"tracking": 20.0}})).unwrap();+ {+ let st = s.doc().unwrap();+ let style = st.doc.styles.char_style(&name).expect("style still exists");+ assert_eq!(style.chars.tracking, Some(20.0), "edited attribute persisted");+ }+ // Renaming updates the style definition itself, and every place it's referenced.+ s.execute("style.character.edit", &json!({"name": name, "rename": "Emphasis"})).unwrap();+ let st = s.doc().unwrap();+ assert!(st.doc.styles.char_style("Emphasis").is_some(), "renamed style exists under the new name");+ assert!(st.doc.styles.char_style(&name).is_none(), "old name is gone");+}++#[test]+fn character_style_edit_rejects_based_on_cycle() {+ let mut s = session();+ let a = s.execute("style.character.create", &json!({"name": "A"})).unwrap()["name"].as_str().unwrap().to_string();+ let b = s.execute("style.character.create", &json!({"name": "B", "basedOn": a})).unwrap()["name"].as_str().unwrap().to_string();+ let r = s.execute("style.character.edit", &json!({"name": a, "basedOn": b}));+ assert!(r.is_err(), "A based on B based on A must be rejected");+}++#[test]+fn character_style_delete_reassigns_affected_text() {+ let mut s = session();+ let name = s.execute("style.character.create", &json!({"name": "Key Term"})).unwrap()["name"].as_str().unwrap().to_string();+ let r = s.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text"})).unwrap();+ let sid = r["story"].as_u64().unwrap();+ s.execute("text.insert", &json!({"text": "Hello, world"})).unwrap();+ s.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 5})).unwrap();+ s.execute("style.character.apply", &json!({"name": name})).unwrap();+ {+ let st = s.doc().unwrap();+ let story = st.doc.stories.get(&designcraft_doc::StoryId(sid)).expect("story exists");+ assert_eq!(story.chars[0].format.style, name, "style applied before delete");+ }+ s.execute("style.character.delete", &json!({"name": name, "replaceWith": designcraft_doc::NO_CHAR_STYLE})).unwrap();+ let st = s.doc().unwrap();+ assert!(st.doc.styles.char_style(&name).is_none(), "deleted style is gone");+ let story = st.doc.stories.get(&designcraft_doc::StoryId(sid)).expect("story exists");+ assert_eq!(story.chars[0].format.style, designcraft_doc::NO_CHAR_STYLE, "affected run reassigned to the replacement style");+}++#[test]+fn character_style_delete_rejects_builtin_styles() {+ let mut s = session();+ let r = s.execute("style.character.delete", &json!({"name": designcraft_doc::NO_CHAR_STYLE}));+ assert!(r.is_err(), "built-in styles can't be deleted");+}+++ /// Load the text from a frame's out port with the Selection tool and click on the pasteboard:+ /// a new frame is threaded there and the overset text flows into it.+ #[test]+ fn threading_with_the_loaded_text_cursor_on_the_sample() {+ use designcraft_tools::{PointerEvent, PointerKind};+ let mut s = Session::new();+ s.execute("file.newSample", &json!({})).unwrap();+ let d = s.doc().unwrap().doc.clone();+ let (sid, first) = d.stories.values().find(|st| st.frames.len() >= 2).map(|st| (st.id, st.frames[0])).expect("a threaded story");+ // Break the thread so the first frame is overset, and select it.+ s.execute("text.unthread", &json!({"frame": first.0})).unwrap();+ assert!(s.cache.get(&s.doc().unwrap().doc, sid, None).is_overset());+ s.execute("selection.set", &json!({"ids": [first.0]})).unwrap();+ s.set_tool("selection");+ let v = ViewInfo::at_zoom(1.0);+ let layout = s.layout();+ let port = designcraft_tools::thread::port_center(&s.doc().unwrap().doc, &layout, first, true, v.zoom).unwrap();+ let ev = |k, p: designcraft_geom::Point| PointerEvent::new(k, p.x, p.y);+ s.pointer(&ev(PointerKind::Down, port), v).unwrap();+ s.pointer(&ev(PointerKind::Up, port), v).unwrap();+ assert_eq!(s.cursor(port + designcraft_geom::Vec2::new(-2000.0, 0.0), Default::default(), v), designcraft_tools::Cursor::LoadedText);+ // On the pasteboard left of the first spread.+ let b = layout.slots[0].bounds;+ let at = designcraft_geom::Point::new(b.x0 - 300.0, b.y0 + 100.0);+ let undo = s.doc().unwrap().history.undo.len();+ s.pointer(&ev(PointerKind::Down, at), v).unwrap();+ s.pointer(&ev(PointerKind::Up, at), v).unwrap();+ let st = s.doc().unwrap();+ assert_eq!(st.history.undo.len(), undo + 1, "one undo step");+ let frames = st.doc.story(sid).unwrap().frames.clone();+ assert_eq!(frames.len(), 2);+ let new = frames[1];+ assert_eq!(st.selection.items, [new]);+ let cs = s.cache.get(&s.doc().unwrap().doc, sid, None);+ assert!(cs.frames.iter().any(|f| f.frame == new && !f.range.is_empty()), "the text flows into the new frame");+ let nb = s.doc().unwrap().doc.item(new).unwrap().bounds();+ assert!((nb.y0 - layout.to_spread(designcraft_doc::SpreadRef::Doc(0), at).y).abs() < 1e-6, "{nb:?}");+ }++ /// Drag a selection handle of item 0 on spread 0 from the edge point `at` by `by` (canvas units).+ fn drag_handle(s: &mut Session, at: designcraft_geom::Point, by: designcraft_geom::Vec2, v: ViewInfo) {+ use designcraft_tools::{PointerEvent, PointerKind};+ let p = s.layout().to_canvas(designcraft_doc::SpreadRef::Doc(0), at);+ s.pointer(&PointerEvent::new(PointerKind::Down, p.x, p.y), v).unwrap();+ s.pointer(&PointerEvent::new(PointerKind::Drag, p.x + by.x / 2.0, p.y + by.y / 2.0), v).unwrap();+ s.pointer(&PointerEvent::new(PointerKind::Drag, p.x + by.x, p.y + by.y), v).unwrap();+ s.pointer(&PointerEvent::new(PointerKind::Up, p.x + by.x, p.y + by.y), v).unwrap();+ }++ #[test]+ fn text_frame_resize_snaps_to_the_margins() {+ use designcraft_geom::{Point, Vec2};+ for zoom in [1.0, 0.5] {+ let mut s = Session::new();+ s.execute("file.newSample", &json!({})).unwrap();+ let margin = s.doc().unwrap().doc.spreads[0].pages[0].margin_rect();+ let (x0, y0) = (margin.x0 + 40.0, margin.y0 + 40.0);+ s.execute("frame.create", &json!({"rect": [x0, y0, x0 + 200.0, y0 + 200.0], "content": "text"})).unwrap();+ s.set_tool("selection");+ let v = ViewInfo::at_zoom(zoom);+ let b0 = s.doc().unwrap().doc.spreads[0].items.last().unwrap().bounds();+ // Right handle to 1.5 pt short of the right margin.+ drag_handle(&mut s, Point::new(b0.x1, b0.center().y), Vec2::new(margin.x1 - 1.5 - b0.x1, 0.0), v);+ let b = s.doc().unwrap().doc.spreads[0].items.last().unwrap().bounds();+ assert!((b.x1 - margin.x1).abs() < 1e-6, "zoom {zoom}: right edge {} not on the margin {}", b.x1, margin.x1);+ // Bottom handle to 1.5 pt past the bottom margin.+ drag_handle(&mut s, Point::new(b.center().x, b.y1), Vec2::new(0.0, margin.y1 + 1.5 - b.y1), v);+ let b = s.doc().unwrap().doc.spreads[0].items.last().unwrap().bounds();+ assert!((b.y1 - margin.y1).abs() < 1e-6, "zoom {zoom}: bottom edge {} not on the margin {}", b.y1, margin.y1);+ // Top-left corner handle onto the top and left margins.+ drag_handle(&mut s, Point::new(b.x0, b.y0), Vec2::new(margin.x0 + 1.0 - b.x0, margin.y0 - 1.0 - b.y0), v);+ let b = s.doc().unwrap().doc.spreads[0].items.last().unwrap().bounds();+ assert!((b.x0 - margin.x0).abs() < 1e-6 && (b.y0 - margin.y0).abs() < 1e-6, "zoom {zoom}: corner {b:?} not on the margins {margin:?}");+ }+ }++ #[test]+ fn type_tool_frames_snap_to_the_margins() {+ use designcraft_geom::Point;+ use designcraft_tools::{PointerEvent, PointerKind};+ let mut s = session();+ let margin = s.doc().unwrap().doc.spreads[0].pages[0].margin_rect();+ s.set_tool("type");+ let v = ViewInfo::at_zoom(1.0);+ let c = |s: &Session, x: f64, y: f64| s.layout().to_canvas(designcraft_doc::SpreadRef::Doc(0), Point::new(x, y));+ // From 1.5 pt off the top-left margin corner to 1.5 pt off the right margin.+ let a = c(&s, margin.x0 + 1.5, margin.y0 + 1.5);+ let b = c(&s, margin.x1 - 1.5, margin.y0 + 101.5);+ let before = s.doc().unwrap().doc.spreads[0].items.len();+ s.pointer(&PointerEvent::new(PointerKind::Down, a.x, a.y), v).unwrap();+ s.pointer(&PointerEvent::new(PointerKind::Drag, (a.x + b.x) / 2.0, b.y), v).unwrap();+ s.pointer(&PointerEvent::new(PointerKind::Drag, b.x, b.y), v).unwrap();+ s.pointer(&PointerEvent::new(PointerKind::Up, b.x, b.y), v).unwrap();+ let items = &s.doc().unwrap().doc.spreads[0].items;+ assert_eq!(items.len(), before + 1, "a frame was drawn");+ let r = items.last().unwrap().bounds();+ assert!((r.x0 - margin.x0).abs() < 1e-6 && (r.y0 - margin.y0).abs() < 1e-6, "press point {r:?} vs {margin:?}");+ assert!((r.x1 - margin.x1).abs() < 1e-6, "right edge {} vs {}", r.x1, margin.x1);+ }diff --cc crates/ui-egui/src/dialogs.rsindex 616961aea,17a68e7af..09aa3f1d1--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@@ -1914,17 -1906,9 +1921,17 @@@ pub fn confirm(app: &mut DesignApp) ->}r}+ "deleteCharacterStyle" => {+ let name = d.s("name");+ let replace = d.s("replaceWith");+ app.run(+ "style.character.delete",+ json!({"name": name, "replaceWith": if replace.is_empty() { json!(designcraft_doc::NO_CHAR_STYLE) } else { json!(replace) }}),+ )+ }"colorPicker" => {let hex = d.s("hex");- app.run("object.color", json!({"color": hex, "target": d.s("target")}))+ app.run("object.color", json!({"color": hex, "target": d.s("target"), "text": d.b("text")}))}"layerOptions" => {let hidden: Vec<Value> = d.fields.get("layers").and_then(Value::as_array).map(|a| a.iter().filter(|l| l["visible"] == false).map(|l| l["name"].clone()).collect()).unwrap_or_default();diff --cc crates/ui-egui/src/panels/styles.rsindex 70e999c30,d64cc1bd5..fb080c79b--- a/crates/ui-egui/src/panels/styles.rs+++ b/crates/ui-egui/src/panels/styles.rs@@@ -148,19 -148,46 +148,60 @@@ fn style_row(app: &mut DesignApp, ui: &ui.data_mut(|d| d.insert_temp(id, name));});}+ if para && !n.starts_with('[') {+ ui.separator();+ if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Delete Paragraph Style…"))).clicked() {+ app.ui.dialog = Some(crate::dialogs::Dialog::new("deleteParagraphStyle", json!({"name": n})));+ ui.close();+ }+ }+ if !para && !n.starts_with('[') {+ ui.separator();+ if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Delete Character Style…"))).clicked() {+ app.ui.dialog = Some(crate::dialogs::Dialog::new("deleteCharacterStyle", json!({"name": n})));+ ui.close();+ }+ }});}++ #[cfg(test)]+ mod tests {+ use egui_kittest::{Harness, kittest::Queryable};++ use super::*;++ #[test]+ fn character_style_plus_captures_selection_for_reuse() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let frame = app.run("frame.create", json!({"rect": [36, 36, 300, 200], "content": "text", "text": "alpha beta"})).unwrap();+ app.run("text.select", json!({"story": frame["story"], "anchor": 0, "focus": 5})).unwrap();+ app.run("type.char", json!({"underline": true})).unwrap();+ let selection = app.session.doc().unwrap().selection.clone();+ let undo_len = app.session.doc().unwrap().history.undo.len();+ let mut h = Harness::builder().with_size(vec2(360.0, 240.0)).build_ui_state(+ |ui, app: &mut DesignApp| {+ let id = egui::Id::new("test_fonts");+ if ui.data(|d| d.get_temp::<bool>(id)).is_none() {+ crate::theme::install_fonts(ui.ctx(), "");+ ui.data_mut(|d| d.insert_temp(id, true));+ return;+ }+ character(app, ui);+ },+ app,+ );+ h.run_steps(3);+ h.get_by_label("Create New Style from Selection").click();+ h.run_steps(3);+ let app = h.state_mut();+ let style = app.session.doc().unwrap().doc.styles.char_style("Character Style 1").unwrap();+ assert_eq!(style.chars, designcraft_doc::CharAttrs { underline: Some(true), ..Default::default() });+ assert_eq!(app.session.doc().unwrap().selection, selection);+ assert_eq!(app.session.doc().unwrap().history.undo.len(), undo_len + 1);+ app.run("text.select", json!({"story": frame["story"], "anchor": 6, "focus": 10})).unwrap();+ app.run("style.character.apply", json!({"name": "Character Style 1"})).unwrap();+ assert_eq!(app.run("type.selectionAttrs", json!({})).unwrap()["chars"]["underline"], true);+ }+ }