DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge PR #1064: Styles: redefine paragraph and character styles from local formatting
aiongg67d597c41665
diff --cc ROADMAP.mdindex a45611fd7,e7ca95e7b..5abe60ac0--- a/ROADMAP.md+++ b/ROADMAP.md@@@ -345,8 -343,8 +345,10 @@@ list) · [roadmap.md](docs/roadmap.md)## Progress log+- **2026-10-15** · Applying text styles removes matching local formatting so capture sources+ inherit later edits; real overrides and named character styles remain.+ - **2026-10-15** · Redefine paragraph and character styles from local formatting, including the+ Styles panel context menu, global updates and one-step undo.- **2026-10-13** · Switching between document and parent pages refreshes the canvas immediately, preventing black parent pages and stale document objects when the zoom stays the same. Ordinary edits retain partial repainting. No readiness percentage change.diff --cc crates/engine/src/cmd/style.rsindex c86aa43e8,d508a8bee..dfbb525a9--- a/crates/engine/src/cmd/style.rs+++ b/crates/engine/src/cmd/style.rs@@@ -1,106 -1,106 +1,205 @@@//! Paragraph / character styles and swatches.+ #[cfg(test)]+ mod redefine_tests {+ use super::*;++ fn sample() -> (Session, Value, Value) {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let a = s.execute("frame.create", &json!({"rect":[36,36,300,200],"content":"text","text":"First header"})).unwrap();+ let b = s.execute("frame.create", &json!({"rect":[36,240,300,400],"content":"text","text":"Second header"})).unwrap();+ s.execute("style.paragraph.create", &json!({"name":"Header","chars":{"size":18,"fill":"[Paper]"}})).unwrap();+ for story in [&a["story"], &b["story"]] {+ s.execute("text.select", &json!({"story":story,"anchor":0,"focus":5})).unwrap();+ s.execute("style.paragraph.apply", &json!({"name":"Header"})).unwrap();+ }+ s.execute("text.select", &json!({"story":a["story"],"anchor":0,"focus":5})).unwrap();+ (s, a["story"].clone(), b["story"].clone())+ }++ #[test]+ fn redefine_paragraph_updates_other_stories_and_clears_the_source_overrides() {+ let (mut s, a, b) = sample();+ s.execute("style.character.create", &json!({"name":"Emphasis","chars":{"underline":true}})).unwrap();+ s.execute("style.character.apply", &json!({"name":"Emphasis"})).unwrap();+ s.execute("type.char", &json!({"size":20,"fill":"[Black]"})).unwrap();+ s.execute("type.para", &json!({"spaceAfter":9})).unwrap();+ let before = s.doc().unwrap().doc.clone();+ let undo = s.doc().unwrap().history.undo.len();+ s.execute("style.paragraph.redefine", &json!({})).unwrap();+ assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);+ assert!(s.doc().unwrap().doc.styles.para("Header").unwrap().chars.underline.is_none());+ for story in [&a, &b] {+ s.execute("text.select", &json!({"story":story,"anchor":0,"focus":5})).unwrap();+ let attrs = s.execute("type.selectionAttrs", &json!({})).unwrap();+ assert_eq!(attrs["chars"]["size"], 20.0);+ assert_eq!(attrs["chars"]["fill"], "[Black]");+ assert_eq!(attrs["para"]["spaceAfter"], 9.0);+ assert_eq!(attrs["chars"]["underline"], story == &a);+ }+ s.execute("style.paragraph.edit", &json!({"name":"Header","chars":{"size":22}})).unwrap();+ s.execute("text.select", &json!({"story":a,"anchor":0,"focus":5})).unwrap();+ assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["size"], 22.0);+ s.execute("edit.undo", &json!({})).unwrap();+ s.execute("edit.undo", &json!({})).unwrap();+ assert_eq!(s.doc().unwrap().doc, before);+ }++ #[test]+ fn redefine_character_does_not_capture_paragraph_font_size() {+ let (mut s, a, b) = sample();+ s.execute("style.character.create", &json!({"name":"Underline","chars":{"underline":true}})).unwrap();+ for story in [&a, &b] {+ s.execute("text.select", &json!({"story":story,"anchor":0,"focus":5})).unwrap();+ s.execute("style.character.apply", &json!({"name":"Underline"})).unwrap();+ }+ s.execute("type.char", &json!({"fill":"[Black]"})).unwrap();+ s.execute("style.character.redefine", &json!({"name":"Underline"})).unwrap();+ let style = s.doc().unwrap().doc.styles.char_style("Underline").unwrap();+ assert!(style.chars.size.is_none());+ assert_eq!(style.chars.underline, Some(true));+ for story in [&a, &b] {+ s.execute("text.select", &json!({"story":story,"anchor":0,"focus":5})).unwrap();+ assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fill"], "[Black]");+ }+ }++ #[test]+ fn redefine_rejects_the_wrong_style_and_the_none_entry_atomically() {+ let (mut s, _, _) = sample();+ let before = s.doc().unwrap().doc.clone();+ let undo = s.doc().unwrap().history.undo.len();+ assert!(s.execute("style.paragraph.redefine", &json!({"name":"Other"})).is_err());+ assert!(s.execute("style.character.redefine", &json!({})).is_err());+ assert_eq!(s.doc().unwrap().doc, before);+ assert_eq!(s.doc().unwrap().history.undo.len(), undo);+ }++ #[test]+ fn redefine_character_at_a_caret_cleans_the_source_run_but_keeps_other_overrides() {+ let (mut s, story, _) = sample();+ s.execute("style.character.create", &json!({"name":"Emphasis","chars":{"underline":true}})).unwrap();+ for (start, end, size) in [(0, 5, 24), (6, 12, 32)] {+ s.execute("text.select", &json!({"story":story,"anchor":start,"focus":end})).unwrap();+ s.execute("style.character.apply", &json!({"name":"Emphasis"})).unwrap();+ s.execute("type.char", &json!({"size":size})).unwrap();+ }+ let before = s.doc().unwrap().doc.clone();+ s.execute("text.select", &json!({"story":story,"anchor":2,"focus":2})).unwrap();+ s.execute("style.character.redefine", &json!({"name":"Emphasis"})).unwrap();+ s.execute("style.character.edit", &json!({"name":"Emphasis","chars":{"size":30}})).unwrap();+ for (start, end, size) in [(0, 5, 30.0), (6, 12, 32.0)] {+ s.execute("text.select", &json!({"story":story,"anchor":start,"focus":end})).unwrap();+ assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["size"], size);+ }+ s.execute("edit.undo", &json!({})).unwrap();+ s.execute("edit.undo", &json!({})).unwrap();+ assert_eq!(s.doc().unwrap().doc, before);+ }+ }++#[cfg(test)]+mod application_inheritance_tests {+ use super::*;++ fn setup(text: &str) -> (Session, Value) {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let f = s.execute("frame.create", &json!({"rect": [36,36,300,300], "content": "text", "text": text})).unwrap();+ (s, f["story"].clone())+ }+ fn select(s: &mut Session, story: &Value, start: usize, end: usize) {+ s.execute("text.select", &json!({"story": story, "anchor": start, "focus": end})).unwrap();+ }+ fn attrs(s: &mut Session) -> Value {+ s.execute("type.selectionAttrs", &json!({})).unwrap()+ }++ #[test]+ fn paragraph_capture_source_follows_later_style_edits_and_undo() {+ let (mut s, story) = setup("First\nSecond");+ select(&mut s, &story, 0, 5);+ s.execute("type.char", &json!({"size": 18, "fill": "[Paper]"})).unwrap();+ s.execute("type.para", &json!({"spaceAfter": 6})).unwrap();+ s.execute("style.paragraph.create", &json!({"name": "Header", "fromSelection": true})).unwrap();+ let before = s.doc().unwrap().doc.clone();+ let undo = s.doc().unwrap().history.undo.len();+ s.execute("style.paragraph.apply", &json!({"name": "Header"})).unwrap();+ assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);+ s.execute("edit.undo", &json!({})).unwrap();+ assert_eq!(s.doc().unwrap().doc, before);+ s.execute("edit.redo", &json!({})).unwrap();+ select(&mut s, &story, 6, 6);+ s.execute("style.paragraph.apply", &json!({"name": "Header"})).unwrap();+ s.execute("style.paragraph.edit", &json!({"name": "Header", "chars": {"size": 20, "fill": "[Black]"}, "para": {"spaceAfter": 12}})).unwrap();+ for pos in [0, 6] {+ select(&mut s, &story, pos, pos + 1);+ let a = attrs(&mut s);+ assert_eq!(a["chars"]["size"], 20.0);+ assert_eq!(a["chars"]["fill"], "[Black]");+ assert_eq!(a["para"]["spaceAfter"], 12.0);+ }+ }++ #[test]+ fn applying_styles_keeps_different_overrides_and_named_character_styles() {+ let (mut s, story) = setup("First\nSecond");+ select(&mut s, &story, 0, 5);+ s.execute("type.char", &json!({"size": 14})).unwrap();+ s.execute("type.para", &json!({"spaceAfter": 9})).unwrap();+ s.execute("style.character.create", &json!({"name": "Emphasis", "chars": {"underline": true}})).unwrap();+ s.execute("style.character.apply", &json!({"name": "Emphasis"})).unwrap();+ s.execute("style.paragraph.create", &json!({"name": "Body", "chars": {"size": 10}, "para": {"spaceAfter": 6}})).unwrap();+ s.execute("style.paragraph.apply", &json!({"name": "Body"})).unwrap();+ let a = attrs(&mut s);+ assert_eq!(a["chars"]["size"], 14.0);+ assert_eq!(a["para"]["spaceAfter"], 9.0);+ assert_eq!(a["characterStyle"], "Emphasis");+ assert_eq!(a["chars"]["underline"], true);+ }++ #[test]+ fn character_capture_source_remains_sparse_and_follows_style_edits() {+ let (mut s, story) = setup("First\nSecond");+ select(&mut s, &story, 0, 5);+ s.execute("type.char", &json!({"underline": true})).unwrap();+ s.execute("style.character.create", &json!({"name": "Emphasis", "fromSelection": true})).unwrap();+ s.execute("style.character.apply", &json!({"name": "Emphasis"})).unwrap();+ s.execute("style.character.edit", &json!({"name": "Emphasis", "chars": {"underline": false}})).unwrap();+ assert_eq!(attrs(&mut s)["chars"]["underline"], false);+ assert!(s.doc().unwrap().doc.styles.char_style("Emphasis").unwrap().chars.size.is_none());+ s.execute("style.paragraph.edit", &json!({"name": designcraft_doc::BASIC_PARAGRAPH, "chars": {"size": 20}})).unwrap();+ assert_eq!(attrs(&mut s)["chars"]["size"], 20.0);+ }++ #[test]+ fn paragraph_selection_excludes_the_next_paragraph_at_its_start() {+ let (mut s, story) = setup("é\nNext");+ select(&mut s, &story, 0, 3);+ s.execute("style.paragraph.create", &json!({"name": "Header"})).unwrap();+ s.execute("style.paragraph.apply", &json!({"name": "Header", "clearOverrides": true})).unwrap();+ select(&mut s, &story, 3, 4);+ assert_eq!(attrs(&mut s)["paragraphStyle"], designcraft_doc::BASIC_PARAGRAPH);+ select(&mut s, &story, 0, 2);+ assert_eq!(attrs(&mut s)["paragraphStyle"], "Header");+ }++ #[test]+ fn clearing_a_partial_selection_preserves_character_overrides_outside_it() {+ let (mut s, story) = setup("First Second");+ select(&mut s, &story, 0, 12);+ s.execute("type.char", &json!({"size": 18})).unwrap();+ select(&mut s, &story, 0, 5);+ s.execute("style.paragraph.apply", &json!({"name": designcraft_doc::BASIC_PARAGRAPH, "clearOverrides": true})).unwrap();+ assert_ne!(attrs(&mut s)["chars"]["size"], 18.0);+ select(&mut s, &story, 6, 12);+ assert_eq!(attrs(&mut s)["chars"]["size"], 18.0);+ }+}+use designcraft_color::{Swatch, SwatchValue};-use designcraft_doc::{CharAttrs, CharacterStyle, ParaAttrs, ParagraphStyle, Styles};+use designcraft_doc::{CharAttrs, CharacterStyle, Effects, Item, ObjectStyle, ParaAttrs, ParagraphStyle, Styles};use serde_json::{Value, json};use super::{CommandSpec, bad, cmd, has_doc, has_text_or_frames, ok, str_param};diff --cc docs/ui-parity.mdindex 777691436,68375ce07..fe8023eed--- a/docs/ui-parity.md+++ b/docs/ui-parity.md@@@ -59,23 -59,18 +59,33 @@@ Counted 2026-10-11: 28 rows, 12 ✓, 1for real work is lower, ~45%, because threading is missing and three of the ~ rows (copy/paste,context menus, typing at a caret) are things a layout artist does every minute, and their failures stop work.+Navigation also supports held Space as Hand and held Cmd+Space as Zoom (Ctrl+Space on+Windows/Linux), with Alt-click zooming out. Releasing restores the previous tool and its state.+The temporary Zoom uses the selected Zoom tool's click and horizontal scrubby drag behavior;+marquee zoom is not implemented. It is suppressed while typing in a document text frame or a+focused text field, and does not interrupt an existing tool drag. Synthetic canvas input tests+cover the cursors, click anchors, scrub/release ownership, restoration, typing guards and undo+history (DC-1).+## Remaining effort+Applying a paragraph or character style now removes local values equal to the applied style,+so text used to capture a style follows later edits to that style. Different local formatting+and named character styles stay intact. Paragraph-style application treats selection endpoints+as exclusive; Clear Overrides removes character formatting only inside a nonempty selection.+Regressions cover the capture/edit workflow at 2:16 and 6:02 in+[Jessica Garrity's styles tutorial](https://www.youtube.com/watch?v=pG9JkAPLgs8).++ Redefining a character style at a caret also normalizes the sampled source run, so subsequent+ style edits reach that text while different local overrides in adjacent runs remain intact.++ The selected style's context menu now offers Redefine Style for paragraph and character styles.+ It incorporates the source text's local changes, updates every use, and removes matching source+ overrides in one undo step. Named character styles remain separate when redefining a paragraph+ style. Commands: `style.paragraph.redefine` and `style.character.redefine` (optional `name`,+ which must match the source). This implements the global color, size and spacing updates at+ about 5:10–6:00 in [Jessica Garrity's styles tutorial](https://www.youtube.com/watch?v=pG9JkAPLgs8).+| Work | Opus 5.5 h ||---|---|| Copy/paste, context menus, threading clicks, caret formatting on every platform, with control-channel regression tests | 30–50 |