DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into find-change-format
moppeeera627bfe47cce
diff --cc crates/engine/src/cmd/find.rsindex d6a43855d,a40e8da51..b87a7b226--- a/crates/engine/src/cmd/find.rs+++ b/crates/engine/src/cmd/find.rs@@@ -61,7 -61,7 +61,7 @@@ pub fn specs() -> Vec<CommandSpec>}),cmd!(query "find.find", "Find", ["Edit", "Find/Change"], None,- "{find, grep?: bool, caseSensitive?: bool, wholeWord?: bool, scope?: document|story|selection, story?, change?|first?|attrs?|charStyle?|paraStyle?: ignored (find.change takes them; one request serves all three Find/Change commands)} → matches [{story, start, end, text}]",- "{find, grep?: bool, caseSensitive?: bool, wholeWord?: bool, scope?: document|story|selection, story?, change?|first?|attrs?: ignored (find.change takes them; one request serves all three Find/Change commands)} → matches [{story, start, end, text, cell?: {table, row, col}}] (document scope also searches table cells; start/end then index the cell's text)",++ "{find, grep?: bool, caseSensitive?: bool, wholeWord?: bool, scope?: document|story|selection, story?, change?|first?|attrs?|charStyle?|paraStyle?: ignored (find.change takes them; one request serves all three Find/Change commands)} → matches [{story, start, end, text, cell?: {table, row, col}}] (document scope also searches table cells; start/end then index the cell's text)",has_doc, find),cmd!("find.change",@@@ -314,23 -336,11 +336,23 @@@ fn change(s: &mut Session, p: &Value) -}}super::text::validate_char_fill(&s.doc()?.doc, &over, "find.change")?;+ let char_style = str_param(p, "charStyle").map(str::to_string);+ let para_style = str_param(p, "paraStyle").map(str::to_string);+ let styles = &s.doc()?.doc.styles;+ if let Some(n) = char_style.as_ref().filter(|n| styles.char_style(n).is_none()) {+ return Err(bad("find.change", format!("no character style `{n}`")));+ }+ if let Some(n) = para_style.as_ref().filter(|n| styles.para(n).is_none()) {+ return Err(bad("find.change", format!("no paragraph style `{n}`")));+ }+ // InDesign: with a Change Format and nothing in Change to, the found text is only formatted.+ let format_only = str_param(p, "change").unwrap_or("").is_empty() && (char_style.is_some() || para_style.is_some() || !over.is_empty());let targets = scope(s, p)?.targets;s.edit(|d, sel| {+ let styles = d.styles.clone();let mut count = 0usize;- for (sid, r) in targets {- let Some(story) = d.story(sid) else { continue };+ for (sid, cell, r) in targets {+ let Some(story) = d.text_story(sid, cell) else { continue };// Collect matches first (positions refer to the original text), apply back to front.let text = story.text.get(r.clone()).ok_or_else(|| bad("find", "invalid selection bounds or UTF-8 boundary"))?;let edits: Vec<(std::ops::Range<usize>, String)> = if grep {@@@ -342,24 -352,11 +364,24 @@@.into_iter().map(|(m, out)| (r.start + m.start..r.start + m.end, out)).collect();- let Some(st) = d.story_mut(sid) else { continue };+ let Some(st) = d.text_story_mut(sid, cell) else { continue };for (range, new) in edits.iter().rev() {- st.replace(range.clone(), new);- if !over.is_empty() && !new.is_empty() {- st.format_chars(range.start..range.start + new.len(), |f| f.over.merge(&over));+ let changed = if format_only {+ range.clone()+ } else {+ st.replace(range.clone(), new);+ range.start..range.start + new.len()+ };+ if !changed.is_empty() {+ if let Some(n) = ¶_style {+ super::style::apply_para_style(st, &styles, n, &changed, false);+ }+ if let Some(n) = &char_style {+ super::style::apply_char_style(st, &styles, n, &changed);+ }+ if !over.is_empty() {+ st.format_chars(changed, |f| f.over.merge(&over));+ }}count += 1;}