mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-02 22:30:34 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32984c6c80 | ||
|
|
c86b62966b | ||
|
|
067a81c812 | ||
|
|
0e97d11789 | ||
|
|
196accb4b6 |
@@ -287,8 +287,16 @@ fn current_run(s: &Session) -> RunProps {
|
||||
}
|
||||
None => {
|
||||
let Some(sh) = st.selected_shapes().first().map(|x| (*x).clone()) else { return RunProps::default() };
|
||||
let para = sh.text.as_ref().and_then(|t| t.paragraphs.first().cloned()).unwrap_or_default();
|
||||
let rp = para.runs.first().map(|r| r.props.clone()).unwrap_or_default();
|
||||
// A selected table reads its first selected cell (the whole table without a range: its first cell).
|
||||
let body = match &sh.kind {
|
||||
deckcraft_model::ShapeKind::Table(tb) => {
|
||||
let first = super::table::selected_range(st.selection.cells, sh.id, tb).map_or((0, 0), |(a, _)| a);
|
||||
tb.cell(first.0, first.1).map(|c| &c.text)
|
||||
}
|
||||
_ => sh.text.as_ref(),
|
||||
};
|
||||
let para = body.and_then(|t| t.paragraphs.first().cloned()).unwrap_or_default();
|
||||
let rp = para.runs.first().map(|r| r.props.clone()).unwrap_or_else(|| para.end_props.clone());
|
||||
(sh, para, rp)
|
||||
}
|
||||
};
|
||||
|
||||
@@ -338,7 +338,10 @@ pub(super) fn set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
_ => Some(sh.text.get_or_insert_with(TextBody::default)),
|
||||
};
|
||||
let Some(body) = target else { return Err(bad("text.set", "no text here (tables need `cell`)")) };
|
||||
let keep = body.paragraphs.first().and_then(|p| p.runs.first()).map(|r| r.props.clone()).unwrap_or_default();
|
||||
// The first run's formatting, or for text that is still empty the formatting its end
|
||||
// mark holds (format.* before any text lands there), so a size set before text.set survives.
|
||||
let keep =
|
||||
body.paragraphs.first().map(|p| p.runs.first().map(|r| r.props.clone()).unwrap_or_else(|| p.end_props.clone())).unwrap_or_default();
|
||||
let levels: Vec<(u8, deckcraft_model::text::ParaProps)> = body.paragraphs.iter().map(|p| (p.level, p.props.clone())).collect();
|
||||
let mut nb = TextBody::from_text(&text);
|
||||
for (i, para) in nb.paragraphs.iter_mut().enumerate() {
|
||||
@@ -353,6 +356,11 @@ pub(super) fn set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
for r in &mut para.runs {
|
||||
r.props = keep.clone();
|
||||
}
|
||||
// The end mark keeps it too, so an empty `text` (no runs) carries the formatting to
|
||||
// the text set next.
|
||||
para.end_props = keep.clone();
|
||||
// A run emptied by stripping leading tabs would shadow the end mark for the text typed next.
|
||||
para.normalize();
|
||||
}
|
||||
body.paragraphs = nb.paragraphs;
|
||||
Ok(())
|
||||
|
||||
@@ -25,6 +25,10 @@ pub(crate) fn format_run_levels(
|
||||
// Only the levels of the paragraphs the selected range touches.
|
||||
let levels = if a == b { Vec::new() } else { master_levels(st, &t, t.ordered()) };
|
||||
edit_text(s, |body, _| {
|
||||
// A body with no paragraph has no end mark to hold the formatting of the text typed next.
|
||||
if body.paragraphs.is_empty() {
|
||||
body.paragraphs.push(Default::default());
|
||||
}
|
||||
if levels.is_empty() {
|
||||
ed::format(body, a, b, f);
|
||||
} else {
|
||||
@@ -65,6 +69,9 @@ pub(crate) fn format_run_levels(
|
||||
for (r, row) in t.rows.iter_mut().enumerate() {
|
||||
for (c, cell) in row.cells.iter_mut().enumerate() {
|
||||
if crate::cmd::table::in_range(range, r, c) {
|
||||
if cell.text.paragraphs.is_empty() {
|
||||
cell.text.paragraphs.push(Default::default());
|
||||
}
|
||||
ed::format_all(&mut cell.text, f);
|
||||
}
|
||||
}
|
||||
@@ -72,6 +79,11 @@ pub(crate) fn format_run_levels(
|
||||
}
|
||||
_ => {
|
||||
let body = sh.text.get_or_insert_with(TextBody::default);
|
||||
// Text that is still empty keeps the formatting on an end mark, so a size set
|
||||
// before the text lands survives it; a body with no paragraph has none.
|
||||
if body.paragraphs.is_empty() {
|
||||
body.paragraphs.push(Default::default());
|
||||
}
|
||||
let eff = effective.iter().find(|(i, _)| *i == id).map(|(_, e)| e);
|
||||
match eff {
|
||||
Some(eff) => {
|
||||
|
||||
@@ -7215,6 +7215,67 @@ fn invalid_slide_index_never_falls_back_to_the_selection_in_export_commands() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A size (or colour, bold…) set on a shape before it has any text survives the text that is
|
||||
/// then set: text.set keeps the first run's properties, and an empty paragraph keeps them on its
|
||||
/// end mark, which is what the agent's format.size then set_text sequence relies on.
|
||||
#[test]
|
||||
fn text_set_keeps_the_formatting_of_an_empty_shape() {
|
||||
let mut s = session();
|
||||
let id = s.execute("shape.insert", &json!({"preset": "rect", "rect": [10, 10, 300, 60]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("format.size", &json!({"size": 30})).unwrap();
|
||||
s.execute("format.bold", &json!({"on": true})).unwrap();
|
||||
s.execute("text.set", &json!({"id": id, "text": "Hello"})).unwrap();
|
||||
let props =
|
||||
|s: &Session| s.doc().unwrap().shape(deckcraft_model::ShapeId(id as u32)).unwrap().text.as_ref().unwrap().paragraphs[0].runs[0].props.clone();
|
||||
let run = props(&s);
|
||||
assert_eq!((run.size, run.bold), (Some(30.0), Some(true)), "{run:?}");
|
||||
// Emptied and filled again: the empty paragraph's end mark carried the formatting.
|
||||
s.execute("text.set", &json!({"id": id, "text": ""})).unwrap();
|
||||
s.execute("text.set", &json!({"id": id, "text": "Again"})).unwrap();
|
||||
let run = props(&s);
|
||||
assert_eq!((run.size, run.bold), (Some(30.0), Some(true)), "after an empty text.set: {run:?}");
|
||||
}
|
||||
|
||||
/// The same while editing the text: a bold set in the text editor of a shape with an empty body
|
||||
/// (no paragraph at all) is what the next typed text gets.
|
||||
#[test]
|
||||
fn text_insert_keeps_the_formatting_set_in_an_empty_editor() {
|
||||
let mut s = session();
|
||||
let id = s.execute("shape.insert", &json!({"preset": "rect", "rect": [10, 10, 300, 60]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.edit(|doc, sel| {
|
||||
let sh = deckcraft_model::find_shape_mut(crate::shapes_mut(doc, sel).unwrap(), deckcraft_model::ShapeId(id as u32)).unwrap();
|
||||
sh.text = Some(deckcraft_model::text::TextBody::default());
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("text.edit", &json!({"id": id})).unwrap();
|
||||
s.execute("format.bold", &json!({"on": true})).unwrap();
|
||||
s.execute("text.insert", &json!({"text": "Hi"})).unwrap();
|
||||
let run = s.doc().unwrap().shape(deckcraft_model::ShapeId(id as u32)).unwrap().text.as_ref().unwrap().paragraphs[0].runs[0].props.clone();
|
||||
assert_eq!(run.bold, Some(true), "{run:?}");
|
||||
}
|
||||
|
||||
/// Font toggles on a selected table read the first selected cell, so the second Bold turns it off
|
||||
/// again, also for a cell whose empty text got its paragraph from the first toggle.
|
||||
#[test]
|
||||
fn bold_toggles_on_a_selected_table_cell() {
|
||||
let mut s = session();
|
||||
let id = table_at(&mut s, &[100.0, 100.0], json!([["", "b"], ["c", "d"]]));
|
||||
let bold = |s: &Session, r: usize, c: usize| {
|
||||
let st = s.doc().unwrap();
|
||||
let deckcraft_model::ShapeKind::Table(t) = &st.shape(id).unwrap().kind else { panic!("table") };
|
||||
let p = &t.cell(r, c).unwrap().text.paragraphs[0];
|
||||
p.runs.first().map_or(p.end_props.bold, |r| r.props.bold)
|
||||
};
|
||||
s.execute("table.selectCells", &json!({"id": id, "from": [0, 0], "to": [0, 1]})).unwrap();
|
||||
s.execute("format.bold", &json!({})).unwrap();
|
||||
assert_eq!((bold(&s, 0, 0), bold(&s, 0, 1)), (Some(true), Some(true)));
|
||||
assert_eq!(s.execute("format.state", &json!({})).unwrap()["bold"], json!(true));
|
||||
s.execute("format.bold", &json!({})).unwrap();
|
||||
assert_eq!((bold(&s, 0, 0), bold(&s, 0, 1)), (Some(false), Some(false)));
|
||||
assert_eq!(bold(&s, 1, 0), None, "cells outside the range keep their formatting");
|
||||
}
|
||||
|
||||
/// G6: with no sites given, shape.connect joins the sites that face each other along the
|
||||
/// dominant direction between the two shapes (a box mostly below leaves from the bottom), not
|
||||
/// "the closest pair", which for a diagonal neighbour picked a side site and crossed boxes.
|
||||
|
||||
Reference in New Issue
Block a user