Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Tables: typing with one cell selected replaces its text

moppeeer5c3d25a09255
diff --git a/crates/engine/src/cmd/table.rs b/crates/engine/src/cmd/table.rs
index 99931deeb..8a3c75102 100644
--- a/crates/engine/src/cmd/table.rs
+++ b/crates/engine/src/cmd/table.rs
@@ -1558,6 +1558,22 @@ pub(crate) fn step_cell(s: &mut Session, forward: bool) -> Result<Value> {
Ok(json!({"row": row, "col": col}))
}
+/// With one cell selected (a merged one counts as one), select its whole text instead, so what is
+/// typed next replaces it, as InDesign does. Other selections are left as they are.
+pub(crate) fn one_selected_cell_to_text(s: &mut Session) -> Result<()> {
+ let st = s.doc_mut()?;
+ let (Some(cs), Some(t)) = (st.selection.cells, st.selection.text) else { return Ok(()) };
+ let Some(tb) = st.doc.story(cs.story).and_then(|x| x.tables.get(&cs.table)) else { return Ok(()) };
+ let (row, col) = tb.owner(cs.range.r0, cs.range.c0);
+ if tb.owner(cs.range.r1, cs.range.c1) != (row, col) {
+ return Ok(());
+ }
+ let cell = CellAddr { table: cs.table, row, col };
+ let len = st.doc.text_story(cs.story, Some(cell)).map_or(0, |x| x.len());
+ st.selection = Selection::text(TextSel { story: cs.story, anchor: 0, focus: len, cell: Some(cell), ..t });
+ Ok(())
+}
+
/// Esc in a table: text in a cell becomes that cell's selection, and selected cells the text of
/// the first one (InDesign toggles between the two).
fn toggle_selection(s: &mut Session) -> Result<Value> {
diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs
index dbfbcfe15..305830a59 100644
--- a/crates/engine/src/cmd/text.rs
+++ b/crates/engine/src/cmd/text.rs
@@ -663,6 +663,8 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
if text == "\t" && s.doc()?.selection.text.is_some_and(|t| t.cell.is_some()) {
return super::table::step_cell(s, true);
}
+ // Typing with one cell selected replaces its text, as InDesign.
+ super::table::one_selected_cell_to_text(s)?;
let raw = p.get("raw").and_then(Value::as_bool).unwrap_or(false);
let text = if s.prefs.typographers_quotes && !raw { smart_quotes(s, &text) } else { text };
let tracking = s.doc()?.doc.settings.track_changes;
diff --git a/crates/engine/src/tests_table.rs b/crates/engine/src/tests_table.rs
index 1fb1b6cdc..2557b961a 100644
--- a/crates/engine/src/tests_table.rs
+++ b/crates/engine/src/tests_table.rs
@@ -148,6 +148,15 @@ fn escape_toggles_text_and_cell_selection() {
assert_eq!((t.cell, t.range()), (Some(cell), 0..5), "the cell's text is selected");
s.execute("table.prevCell", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().selection.text.unwrap().cell, Some(designcraft_doc::CellAddr { col: 0, ..cell }));
+ // With the cell selected, typing replaces its text and leaves the caret after it.
+ s.execute("table.nextCell", &json!({})).unwrap();
+ s.execute("table.toggleSelection", &json!({})).unwrap();
+ s.execute("text.insert", &json!({"text": "Fax"})).unwrap();
+ let t = s.execute("table.get", &json!({"table": tid})).unwrap();
+ assert_eq!(t["cells"][1]["text"], "Fax");
+ let sel = s.doc().unwrap().selection.clone();
+ assert!(sel.cells.is_none());
+ assert_eq!(sel.text.unwrap().range(), 3..3);
// A selection that starts inside a merged cell goes back to the merged cell's text.
s.execute("table.select", &json!({"rows": [0, 1], "cols": [0, 0]})).unwrap();
s.execute("table.merge", &json!({})).unwrap();