DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
PR-1255: TOC style references in indented properties resolve; the picker keeps a trailing caret
moppeeer0c5dd0ede832
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rsindex 76e2104f1..260911582 100644--- a/crates/idml/src/import.rs+++ b/crates/idml/src/import.rs@@ -1498,10 +1498,12 @@ impl<'r> Importer<'r> {let story = top.iter().find(|e| e.local() == "Story" && e.get("AppliedTOCStyle") == Some(id))?;Some((ts, *self.story_ids.get(story.get("Self")?)?))})?;- let known = |r: Option<&str>| r.map(|r| self.para_style_ref(r)).filter(|n| self.styles.para(n).is_some());+ // Property children are element text: formatting whitespace around a reference is dropped+ // unless the reference as written names a style.+ let known = |r: Option<&str>| r.map(|r| self.listed_para_style_ref(r)).filter(|n| self.styles.para(n).is_some());// Character styles the document has; [None] is stored as no style.let known_chars = |r: Option<&str>| {- r.map(|r| self.char_style_ref(r)).filter(|n| n != designcraft_doc::NO_CHAR_STYLE && self.styles.char_style(n).is_some())+ r.map(|r| self.listed_char_style_ref(r)).filter(|n| n != designcraft_doc::NO_CHAR_STYLE && self.styles.char_style(n).is_some())};let entries: Vec<TocEntry> = ts.elements()@@ -1538,6 +1540,11 @@ impl<'r> Importer<'r> {fn para_style_ref(&self, r: &str) -> String {self.para_names.get(r).cloned().unwrap_or_else(|| names::style_name_in(PARA_BUILTINS, &unescape_id(r.trim_start_matches("ParagraphStyle/"))))}+ /// A paragraph style reference written as an element's text: as written when it names a+ /// style, otherwise without surrounding whitespace (see [`Self::listed_char_style_ref`]).+ fn listed_para_style_ref(&self, r: &str) -> String {+ if self.para_names.contains_key(r) { self.para_style_ref(r) } else { self.para_style_ref(r.trim()) }+ }/// A character style reference written as an element's text (nested, line and GREP styles)./// A style name may end in a space, so the reference is used as written when it names a/// style; only otherwise is surrounding whitespace dropped.diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rsindex 1c992192a..582242255 100644--- a/crates/idml/src/tests.rs+++ b/crates/idml/src/tests.rs@@ -4266,3 +4266,26 @@ fn toc_style_entries_read_id_prefixed_separators_and_styles() {assert_eq!(entry.page_number_style.as_deref(), Some("TOC Page Number"));assert_eq!(toc.title_style.as_deref(), Some("TOC/TOC Header"));}++#[test]+fn toc_style_references_in_indented_properties_resolve() {+ // Property children laid out on their own lines: the whitespace around a reference isn't+ // part of the style name.+ let styles = TOC_STYLES+ .replace(+ ">ParagraphStyle/$ID/TOC%3aTOC body text<",+ ">+ ParagraphStyle/$ID/TOC%3aTOC body text+ <",+ )+ .replace(+ ">CharacterStyle/TOC Page Number<",+ ">+ CharacterStyle/TOC Page Number+ <",+ );+ let d = import_idml(&fixture_with_table_styles(&styles, TOC_STORY)).unwrap();+ let entry = d.toc.as_ref().unwrap().entries.iter().find(|e| e.style == "Heading 1").unwrap();+ assert_eq!(entry.entry_style.as_deref(), Some("TOC/TOC body text"));+ assert_eq!(entry.page_number_style.as_deref(), Some("TOC Page Number"));+}diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex af1e75146..69cc222ea 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -6221,6 +6221,15 @@ fn toc_separator_typed(s: &str) -> String {designcraft_doc::story::toc_separator_text(s)}+/// Appends the special-character `code` (such as `^y`) to the separator field's notation. A+/// trailing unescaped caret is a literal caret: it's escaped first, so it doesn't swallow the code.+fn toc_separator_append(field: &mut String, code: &str) {+ if field.chars().rev().take_while(|&c| c == '^').count() % 2 == 1 {+ field.push('^');+ }+ field.push_str(code);+}+fn toc_rows(d: &Dialog) -> Vec<TocRow> {d.fields.get("included").cloned().and_then(|v| serde_json::from_value(v).ok()).unwrap_or_default()}@@ -6414,7 +6423,7 @@ fn toc_dialog(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {ui.menu_button("▸", |ui| {for (label, code) in [("Tab Character", "^t"), ("Right Indent Tab", "^y"), ("Em Space", "^m"), ("En Space", "^>")] {if ui.button(tr(label)).clicked() {- r.separator.push_str(code);+ toc_separator_append(&mut r.separator, code);ui.close();}}@@ -8008,6 +8017,12 @@ mod tests {for text in ["^y", "^t^y", "^m^>", "unknown ^q and trailing ^", "a\t\u{E006}\u{2003}\u{2002}z"] {assert_eq!(toc_separator_typed(&toc_separator_shown(text)), text);}+ // The picker after a literal caret: the caret stays, and the code is the code.+ for (field, typed) in [("^", "^\u{E006}"), ("^^", "^\u{E006}"), ("a", "a\u{E006}"), ("^^^", "^^\u{E006}")] {+ let mut f = field.to_string();+ toc_separator_append(&mut f, "^y");+ assert_eq!(toc_separator_typed(&f), typed, "{field:?} → {f:?}");+ }}#[test]