DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
File Info: keywords with a comma keep their place, and the dialog stores into its own document
dotCooCoobfc36622e083
diff --git a/crates/doc/src/file_info.rs b/crates/doc/src/file_info.rsindex 72e79ee16..de450e687 100644--- a/crates/doc/src/file_info.rs+++ b/crates/doc/src/file_info.rs@@ -32,6 +32,29 @@ pub fn parse_keywords(text: &str) -> Vec<String> {clean_keywords(text.split([';', ',']))}+/// Keywords typed as one line, read as [`parse_keywords`] reads them, except that a keyword of+/// `kept` that holds a semicolon or comma stays one keyword wherever it still stands whole+/// between separators.+pub fn parse_keywords_keeping(text: &str, kept: &[String]) -> Vec<String> {+ let mut whole: Vec<&str> = kept.iter().map(|k| k.trim()).filter(|k| k.contains([';', ','])).collect();+ whole.sort_by_key(|k| std::cmp::Reverse(k.len()));+ let separator = |c: char| c == ';' || c == ',';+ let mut words = Vec::new();+ let mut rest = text;+ loop {+ rest = rest.trim_start_matches(|c: char| separator(c) || c.is_whitespace());+ if rest.is_empty() {+ break;+ }+ let kept_here = whole.iter().find(|k| rest.strip_prefix(**k).is_some_and(|after| after.trim_start().chars().next().is_none_or(separator)));+ let end = kept_here.map_or_else(|| rest.find(separator).unwrap_or(rest.len()), |k| k.len());+ let (word, after) = rest.split_at_checked(end).unwrap_or((rest, ""));+ words.push(word);+ rest = after;+ }+ clean_keywords(words)+}+/// Trimmed keywords without empty or repeated ones, in their first order.pub fn clean_keywords<'a>(words: impl IntoIterator<Item = &'a str>) -> Vec<String> {let mut seen = std::collections::HashSet::new();@@ -64,6 +87,17 @@ mod tests {assert!(parse_keywords(" ; , ").is_empty());}+ #[test]+ fn keywords_with_separators_stay_whole_when_another_one_changes() {+ let kept = ["Washington, DC".to_string(), "map".into(), "a; b".into()];+ assert_eq!(parse_keywords_keeping("Washington, DC; atlas", &kept), ["Washington, DC", "atlas"]);+ assert_eq!(parse_keywords_keeping("atlas, a; b ;Washington, DC", &kept), ["atlas", "a; b", "Washington, DC"]);+ // Edited, a keyword is read like any other text.+ assert_eq!(parse_keywords_keeping("Washington, D.C.; map", &kept), ["Washington", "D.C.", "map"]);+ assert_eq!(parse_keywords_keeping("Washington, DCX", &kept), ["Washington", "DCX"]);+ assert_eq!(parse_keywords_keeping(" spring; catalog, shoes ;;spring , ", &[]), ["spring", "catalog", "shoes"]);+ }+#[test]fn the_metadata_title_falls_back_to_the_document_name() {let mut d = Document::new(&NewDocument { title: "Flyer".into(), ..Default::default() });diff --git a/crates/engine/src/cmd/file_info.rs b/crates/engine/src/cmd/file_info.rsindex fe0e2424a..bc6f88ba6 100644--- a/crates/engine/src/cmd/file_info.rs+++ b/crates/engine/src/cmd/file_info.rs@@ -30,7 +30,12 @@ pub fn specs() -> Vec<CommandSpec> {fn file_info(s: &mut Session, p: &Value) -> Result<Value> {let cur = s.doc()?.doc.file_info.clone();- let Some(o) = p.as_object().filter(|o| !o.is_empty()) else { return Ok(to_json(&cur)) };+ let o = match p {+ Value::Null => return Ok(to_json(&cur)),+ Value::Object(o) if o.is_empty() => return Ok(to_json(&cur)),+ Value::Object(o) => o,+ _ => return Err(bad(ID, "the parameters must be an object of fields")),+ };let next = apply(&cur, o)?;if next == cur {return Ok(to_json(&cur));@@ -164,6 +169,13 @@ mod tests {assert!(s.execute("file.info", &p).is_err(), "{p}");}assert!(s.doc().unwrap().doc.file_info.is_empty(), "a refused call changes nothing");+ // Parameters that are not an object are refused; null reads like {}.+ s.execute("file.info", &json!({"author": "Ana Ruiz"})).unwrap();+ for p in [json!(3), json!("author"), json!(true), json!(["author"])] {+ let e = s.execute("file.info", &p).unwrap_err().to_string();+ assert!(e.contains("must be an object"), "{p}: {e}");+ }+ assert_eq!(s.execute("file.info", &serde_json::Value::Null).unwrap()["author"], "Ana Ruiz");}#[test]diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 78a05d0e8..198960583 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -338,13 +338,13 @@ pub fn open_print(app: &mut DesignApp) {}/// File › Export PDF…: the "Export PDF" options dialog, seeded from the persisted settings-/// (`UiState.pdf_export`) and the active document. `pages` is per-job (not persisted); `flatten`-/// falls back to the global flattener preset when the persisted value is empty.+/// (`UiState.pdf_export`). `pages` is per-job (not persisted); `flatten` falls back to the global+/// flattener preset when the persisted value is empty. `title` starts empty: a title given to the+/// dialog is the PDF's title and names the file, and without one the file is named after the+/// document and the PDF takes its title from File Info.pub fn open_pdf_export(app: &mut DesignApp) {let mut fields = serde_json::to_value(&app.ui.pdf_export).unwrap_or_else(|_| json!({}));if let Value::Object(m) = &mut fields {- let title = app.session.active().map(|st| st.doc.title.clone()).unwrap_or_default();- m.insert("title".into(), json!(title));m.insert("pages".into(), json!("all"));if app.ui.pdf_export.flatten.is_empty() {m.insert("flatten".into(), json!(app.ui.flattener));@@ -1269,10 +1269,13 @@ pub fn open_file_info(app: &mut DesignApp) {};f.insert(key.into(), Value::String(v));}- // The keywords as shown: OK sends them only when they were edited, so a keyword holding a- // semicolon or comma stays one keyword.+ // The keywords as shown and as stored: OK sends them only when they were edited, and then a+ // stored keyword holding a semicolon or comma that is still there whole stays one keyword.let shown = f.get("keywords").cloned().unwrap_or_default();f.insert("keywordsShown".into(), shown);+ f.insert("keywordsStored".into(), cur["keywords"].clone());+ // The document the fields belong to, should another one become active before OK.+ f.insert("uid".into(), json!(app.session.active().map(|doc| doc.uid)));app.ui.dialog = Some(Dialog::new("fileInfo", Value::Object(f)));}@@ -1294,15 +1297,40 @@ fn file_info_dialog(app: &crate::DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {}/// File Info's OK: every field as typed (an empty one clears it); the keywords only when they-/// were edited.+/// were edited. The fields go to the document the dialog was opened for, even when another one+/// became active meanwhile, and the active document stays active; when the dialog's document has+/// closed, nothing is stored.fn confirm_file_info(app: &mut DesignApp, d: &Dialog) -> Result<Value, String> {let keywords_edited = d.fields.get("keywordsShown").and_then(Value::as_str) != Some(d.s("keywords").as_str());+ let stored: Vec<String> = match d.fields.get("keywordsStored") {+ Some(Value::Array(words)) => words.iter().filter_map(Value::as_str).map(str::to_string).collect(),+ _ => Vec::new(),+ };let p: Map<String, Value> = FILE_INFO_FIELDS.iter().filter(|(key, _)| *key != "keywords" || keywords_edited)- .map(|(key, _)| (key.to_string(), Value::String(d.s(key))))+ .map(|(key, _)| {+ let v = if *key == "keywords" {+ json!(designcraft_doc::file_info::parse_keywords_keeping(&d.s(key), &stored))+ } else {+ Value::String(d.s(key))+ };+ (key.to_string(), v)+ }).collect();- app.run("file.info", Value::Object(p))+ let uid = d.fields.get("uid").and_then(Value::as_u64);+ let active = app.session.active().map(|doc| doc.uid);+ if uid.is_none() || uid == active {+ return app.run("file.info", Value::Object(p));+ }+ let index = |app: &DesignApp, uid: Option<u64>| app.session.documents().iter().position(|doc| Some(doc.uid) == uid);+ let Some(i) = index(app, uid) else { return Ok(Value::Null) };+ app.run("file.activate", json!({"index": i}))?;+ let r = app.run("file.info", Value::Object(p));+ if let Some(back) = index(app, active) {+ app.run("file.activate", json!({"index": back}))?;+ }+ r}/// The edges of Document Setup's margins, bleed and slug (field suffixes).@@ -3420,12 +3448,14 @@ pub fn confirm(app: &mut DesignApp) -> Result<Value, String> {params["pages"] = json!(pages);}let title = d.s("title");- // The document's name names the file; the PDF's title then comes from File Info.+ // Without a title the document's name names the file, and the PDF's title comes from+ // File Info.let doc_name = app.session.active().map(|st| st.doc.title.clone()).unwrap_or_default();- if !title.is_empty() && title != doc_name {+ if !title.is_empty() {params["title"] = json!(title);}- let name = if title.is_empty() { "Export.pdf".to_string() } else { format!("{title}.pdf") };+ let stem = if title.is_empty() { doc_name } else { title };+ let name = if stem.is_empty() { "Export.pdf".to_string() } else { format!("{stem}.pdf") };match app.pick_save(&name) {Some(path) => {params["path"] = json!(path);@@ -7177,6 +7207,12 @@ mod tests {confirm(&mut app).unwrap();let info = app.session.active().unwrap().doc.file_info.clone();assert_eq!((info.author.as_str(), info.keywords.clone()), ("Studio", vec!["Washington, DC".to_string(), "map".to_string()]));+ // Another keyword edited: the one with a comma in it still stays one keyword.+ crate::menus::activate(&mut app, "file.info", &Value::Null);+ assert_eq!(app.ui.dialog.as_ref().unwrap().s("keywords"), "Washington, DC; map");+ setup_fields(&mut app, json!({"keywords": "Washington, DC; atlas"}));+ confirm(&mut app).unwrap();+ assert_eq!(app.session.active().unwrap().doc.file_info.keywords, ["Washington, DC", "atlas"]);let writes: Arc<Mutex<Vec<Vec<u8>>>> = Arc::new(Mutex::new(Vec::new()));let sink = writes.clone();@@ -7195,6 +7231,36 @@ mod tests {assert_eq!(names.lock().unwrap().as_slice(), ["flyer-v3.pdf"]);let pdf = String::from_utf8_lossy(&writes.lock().unwrap()[0]).into_owned();assert!(pdf.contains("/Title(Spring Catalog)") && !pdf.contains("/Title(flyer-v3)"), "the PDF's title is File Info's");+ // A title given to the export is the PDF's title, even when it is the document's name.+ open_pdf_export(&mut app);+ setup_fields(&mut app, json!({"title": "flyer-v3"}));+ confirm(&mut app).unwrap();+ let asked = names.lock().unwrap().last().cloned().unwrap_or_default();+ assert_eq!(std::path::Path::new(&asked).file_name().and_then(|n| n.to_str()), Some("flyer-v3.pdf"), "{asked}");+ let pdf = String::from_utf8_lossy(&writes.lock().unwrap()[1]).into_owned();+ assert!(pdf.contains("/Title(flyer-v3)") && !pdf.contains("/Title(Spring Catalog)"), "the given title wins");+ }++ /// File Info's OK stores the fields in the document the dialog was opened for, even when+ /// another document became active meanwhile; once that document has closed, OK stores nothing.+ #[test]+ fn file_info_dialog_stores_into_the_document_it_was_opened_for() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"title": "first"})).unwrap();+ crate::menus::activate(&mut app, "file.info", &Value::Null);+ app.run("file.new", json!({"title": "second"})).unwrap();+ setup_fields(&mut app, json!({"author": "Ana Ruiz"}));+ confirm(&mut app).unwrap();+ let docs = app.session.documents();+ let author = |title: &str| docs.iter().find(|d| d.doc.title == title).map(|d| d.doc.file_info.author.clone());+ assert_eq!((author("first").as_deref(), author("second").as_deref()), (Some("Ana Ruiz"), Some("")));+ assert_eq!(app.session.active().map(|d| d.doc.title.as_str()), Some("second"), "the active document stays active");++ crate::menus::activate(&mut app, "file.info", &Value::Null);+ app.run("file.close", json!({})).unwrap();+ setup_fields(&mut app, json!({"author": "Studio"}));+ confirm(&mut app).unwrap();+ assert!(app.session.documents().iter().all(|d| d.doc.file_info.author != "Studio"), "the dialog's document closed");}#[test]