diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index cfccc3be40..a2c5fb427c 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -906,6 +906,7 @@ pub fn state(s: &Session) -> Value { "italic": all.iter().all(shown_italic), "underline": all.iter().all(|r| r.underline != Underline::None), "underlineColor": same(&|r| json!(r.underline_color.map_or_else(|| "auto".to_string(), |c| c.hex()))), + "shading": same(&|r| json!(r.shading.map(|c| c.hex()))), "strike": all.iter().all(|r| r.strike), "border": all.iter().all(|r| r.border.is_some()), "subscript": all.iter().all(|r| r.vert_align == VertAlign::Subscript), diff --git a/crates/engine/src/cmd/para.rs b/crates/engine/src/cmd/para.rs index 2f60046cad..1eeafa87d3 100644 --- a/crates/engine/src/cmd/para.rs +++ b/crates/engine/src/cmd/para.rs @@ -304,7 +304,7 @@ pub fn specs() -> Vec { /// start, so all of a paragraph's text is still text, except in the last paragraph of its story, /// cell or box, where it stands for the paragraph with its mark. A selected picture, shape or text /// box is its object character, not text (a field's object character is). -fn shades_text(s: &Session) -> bool { +pub fn shades_text(s: &Session) -> bool { let (a, b) = s.sel.ordered(); if a == b || a.story != b.story || a.path != b.path { return false; diff --git a/crates/ui-egui/src/colors.rs b/crates/ui-egui/src/colors.rs index 582e026170..21454614db 100644 --- a/crates/ui-egui/src/colors.rs +++ b/crates/ui-egui/src/colors.rs @@ -252,7 +252,9 @@ fn cell_shading(table: &wordcraft_doc::Table, cells: impl IntoIterator Option { use wordcraft_doc::InlineObject; @@ -266,6 +268,10 @@ fn current_of(app: &mut WordApp, target: &Target) -> Option { let key = if cmd == "format.color" { "color" } else { "underlineColor" }; st.get(key).and_then(Value::as_str).and_then(parse) } + // The command shades the selected text, not its paragraph, when the selection is text within one. + "para.shading" if wordcraft_engine::cmd::para::shades_text(s) => { + wordcraft_engine::cmd::format::state(s).get("shading").and_then(Value::as_str).and_then(parse) + } "para.shading" => agree( wordcraft_engine::cmd::selected_paths(s) .iter() @@ -291,7 +297,8 @@ fn current_of(app: &mut WordApp, target: &Target) -> Option { InlineObject::Shape { stroke, .. } => *stroke, o => o.canvas_style().and_then(|c| c.stroke), })), - "shape.effects" => agree(objects().map(|o| match &o { + // Glow is applied to shapes only, so a canvas in the selection is not read. + "shape.effects" => agree(objects().filter(|o| matches!(o, InlineObject::Shape { .. })).map(|o| match &o { InlineObject::Shape { effects, .. } => effects.glow.as_ref().map(|g| g.color), _ => None, })), @@ -835,6 +842,22 @@ mod tests { assert_eq!(current_of(&mut app, &shading), Some(c(0xAA)), "a collapsed caret reads its paragraph"); } + /// Text selected inside a paragraph is what Shading changes, so Current must not report the + /// paragraph's shading for it (#7984). + #[test] + fn partial_selection_reads_the_text_shading_not_the_paragraph() { + let mut app = WordApp::new(Session::new(wordcraft_doc::Document::from_text("hello world")), crate::Services::default()); + let at = app.session.sel.focus.clone(); + app.session.doc.para_mut(at.story, &at.path).unwrap().props.shading = Some(Rgb(0xAA, 0xAA, 0xAA)); + let mut end = at.clone(); + end.off = 5; + app.session.sel = wordcraft_engine::Selection { anchor: at, focus: end }; + let shading = Target::cmd("para.shading"); + assert_eq!(current_of(&mut app, &shading), None, "the text has no shading, whatever its paragraph has"); + let _ = app.session.run("format.shading", &json!({"color": "112233"})); + assert_eq!(current_of(&mut app, &shading), Some(Rgb(0x11, 0x22, 0x33)), "the selected run's shading"); + } + /// A ragged table's missing cells do not blank Current when the real cells agree (#7984). #[test] fn table_shading_skips_missing_cells() {