Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge branch 'table-menu-insert' into table-convert-dialogs

moppeeerd712386a327b
diff --cc crates/ui-egui/src/dialogs.rs
index 35bbbf066,26be062f5..0a2962991
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@@ -3210,22 -3134,11 +3210,23 @@@ pub fn confirm(app: &mut DesignApp) ->
None => Err(format!("no page \"{typed}\"")),
}
}
+ "convertTextToTable" => {
+ let same = d.s("columnSeparator") == d.s("rowSeparator");
+ let style = Some(d.s("tableStyle")).filter(|s| !s.is_empty() && s != designcraft_doc::BASIC_TABLE);
+ app.run(
+ "table.convertFromText",
+ json!({"columnSeparator": d.s("columnSeparator"), "rowSeparator": d.s("rowSeparator"),
+ "columns": if same { json!(d.n("columns").unwrap_or(1.0).max(1.0) as u64) } else { Value::Null }, "tableStyle": style}),
+ )
+ }
+ "convertTableToText" => {
+ app.run("table.convertToText", json!({"columnSeparator": d.s("columnSeparator"), "rowSeparator": d.s("rowSeparator")}))
+ }
"insertTable" => {
- // The table and its style in one step; a style that is gone leaves the document alone.
+ // The table and its style in one step; a style that is gone leaves the document alone,
+ // and the dialog stays open to be corrected.
let style = Some(d.s("tableStyle")).filter(|s| !s.is_empty());
- app.run(
+ let r = app.run(
"table.insert",
json!({"rows": d.n("bodyRows").unwrap_or(4.0).max(1.0) as u64, "cols": d.n("columns").unwrap_or(4.0).max(1.0) as u64,
"headerRows": d.n("headerRows").unwrap_or(0.0).max(0.0) as u64, "footerRows": d.n("footerRows").unwrap_or(0.0).max(0.0) as u64,
@@@ -9835,40 -9753,10 +9841,42 @@@ mod tests
ctl(&mut app, "ui.dialog.set", json!({"field": "tableStyle", "value": "Gone"}));
assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], false);
assert_eq!(tables(&app), 0);
+ let d = app.ui.dialog.as_ref().expect("the dialog stays open to be corrected");
+ assert_eq!((d.id.as_str(), d.s("tableStyle")), ("insertTable", "Gone".to_string()));
}
+ /// Convert Text to Table: available with text selected (not with a caret alone); its
+ /// separators and Number of Columns shape the table. Convert Table to Text takes it back with
+ /// the separators chosen there.
+ #[test]
+ fn convert_dialogs_use_their_separators() {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({})).unwrap();
+ let text = "Name:\nAddress:\nCity:\nState:";
+ let sid = app.run("frame.create", json!({"rect": [72, 72, 372, 372], "content": "text", "text": text})).unwrap()["story"].clone();
+ app.run("text.select", json!({"story": sid, "anchor": 0, "focus": 0})).unwrap();
+ assert!(!crate::menus::menu_enabled(&app, "app.convertTextToTableDialog"), "a caret alone");
+ assert!(!crate::menus::menu_enabled(&app, "app.convertTableToTextDialog"), "no table");
+ app.run("text.select", json!({"story": sid, "anchor": 0, "focus": text.len()})).unwrap();
+ assert!(crate::menus::menu_enabled(&app, "app.convertTextToTableDialog"));
+ app.run("app.convertTextToTableDialog", json!({})).unwrap();
+ assert_eq!(app.ui.dialog.as_ref().map(|d| d.id.as_str()), Some("convertTextToTable"));
+ for (field, value) in [("columnSeparator", json!("paragraph")), ("rowSeparator", json!("paragraph")), ("columns", json!(2))] {
+ ctl(&mut app, "ui.dialog.set", json!({"field": field, "value": value}));
+ }
+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);
+ let t = app.run("table.get", json!({})).unwrap();
+ let len = |k: &str| t[k].as_array().map_or(0, Vec::len);
+ assert_eq!((len("rows"), len("columns")), (2, 2), "{t}");
+ assert!(crate::menus::menu_enabled(&app, "app.convertTableToTextDialog"));
+ app.run("app.convertTableToTextDialog", json!({})).unwrap();
+ ctl(&mut app, "ui.dialog.set", json!({"field": "columnSeparator", "value": ", "}));
+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);
+ let story = app.session.active().unwrap().doc.story(designcraft_doc::StoryId(sid.as_u64().unwrap())).unwrap().text.clone();
+ // (The empty paragraph after a table that ends its story stays after the text.)
+ assert_eq!(story.trim_end(), "Name:, Address:\nCity:, State:");
+ }
+
#[test]
fn character_style_options_set_exactly_the_edited_attributes() {
let mut app = styled_app();