mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-26 17:17:27 +00:00
sheets: a pending cut ends when a sheet edit puts another sheet in its place
A cut names its sheet by its place among the tabs, so after inserting, deleting or moving a sheet before it, a paste took cells and rules from whichever sheet had that place. The cut now ends when its place holds another sheet (or none); a cut whose sheet kept its place stays, and a copy reads its own snapshot and stays.
This commit is contained in:
@@ -29,9 +29,26 @@ pub(super) fn insert_sheet(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
Ok(at)
|
||||
})?;
|
||||
d.sheet_selections.insert(idx.min(d.sheet_selections.len()), crate::Selection::default());
|
||||
end_cut(s);
|
||||
Ok(json!({"index": idx, "name": name}))
|
||||
}
|
||||
|
||||
/// A pending cut names its sheet by its place among the tabs, so when inserting, deleting or
|
||||
/// moving a sheet puts another sheet (or none) in that place, the cut ends, rather than letting a
|
||||
/// paste take cells and rules from whichever sheet has the place now. A copy reads its own
|
||||
/// snapshot and stays, and so does a cut whose sheet kept its place.
|
||||
fn end_cut(s: &mut Session) {
|
||||
let Ok(d) = s.doc() else { return };
|
||||
let moved = s.clipboard.as_ref().is_some_and(|c| {
|
||||
let then = c.wb.sheet(c.sheet).map(|sh| sh.uid.get());
|
||||
let now = d.wb.sheet(c.sheet).map(|sh| sh.uid.get());
|
||||
c.cut && c.doc_uid == d.uid && (then.is_none() || then != now)
|
||||
});
|
||||
if moved {
|
||||
s.clipboard = None;
|
||||
}
|
||||
}
|
||||
|
||||
fn edit_doc<R>(d: &mut crate::DocState, f: impl FnOnce(&mut Ctx) -> Result<R>) -> Result<R> {
|
||||
crate::cmd::commit(d, f)
|
||||
}
|
||||
@@ -71,6 +88,7 @@ pub(super) fn delete_sheet(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
if i < d.sheet_selections.len() {
|
||||
d.sheet_selections.remove(i);
|
||||
}
|
||||
end_cut(s);
|
||||
ok()
|
||||
}
|
||||
|
||||
@@ -115,7 +133,7 @@ pub(super) fn move_sheet(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
let i = target_sheet(s, p)?;
|
||||
let copy = bool_param(p, "copy").unwrap_or(false);
|
||||
let to = p.get("to").and_then(Json::as_u64).map(|v| v as usize).ok_or_else(|| bad("sheet.move", "missing `to`"))?;
|
||||
edit(s, |cx| {
|
||||
let out = edit(s, |cx| {
|
||||
let n = cx.wb.sheets.len();
|
||||
let Some(src) = cx.wb.sheets.get(i).cloned() else { return Err(bad("sheet.move", "no such sheet")) };
|
||||
if copy {
|
||||
@@ -216,7 +234,9 @@ pub(super) fn move_sheet(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
}
|
||||
cx.structural = true;
|
||||
Ok(Json::Null)
|
||||
})
|
||||
})?;
|
||||
end_cut(s);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub(super) fn tab_color(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
|
||||
@@ -7569,6 +7569,45 @@ fn a_cut_is_not_transposed_or_tiled() {
|
||||
assert_eq!(s.doc().unwrap().wb.sheets[0].cond_formats[0].ranges.iter().map(|r| r.a1()).collect::<Vec<_>>(), vec!["E1:G1".to_string()]);
|
||||
}
|
||||
|
||||
/// A cut names its sheet by its place among the tabs: inserting, deleting or moving a sheet that
|
||||
/// puts another sheet in that place ends it, so a paste never takes cells (and rules) from
|
||||
/// whichever sheet has that place now. A copy stays, and so does a cut whose sheet kept its place.
|
||||
#[test]
|
||||
fn a_cut_ends_when_the_tabs_change() {
|
||||
let mut s = s();
|
||||
s.execute("home.insertSheet", json!({"name": "Other", "before": 1})).unwrap();
|
||||
s.execute("range.setValues", json!({"range": "Sheet1!A1", "values": [[1]]})).unwrap();
|
||||
s.execute("range.setValues", json!({"range": "Other!A1", "values": [[7]]})).unwrap();
|
||||
let at = |s: &Session, sheet: &str, a1: &str| {
|
||||
let d = s.doc().unwrap();
|
||||
d.wb.sheet(d.wb.sheet_index(sheet).unwrap()).unwrap().value(CellRef::parse(a1).unwrap())
|
||||
};
|
||||
for step in [json!(["sheet.move", {"sheet": 0, "to": 1}]), json!(["home.insertSheet", {"name": "New", "before": 0}])] {
|
||||
s.execute("sheet.activate", json!({"sheet": s.doc().unwrap().wb.sheet_index("Sheet1").unwrap()})).unwrap();
|
||||
s.execute("edit.cut", json!({"range": "A1"})).unwrap();
|
||||
s.execute(step[0].as_str().unwrap(), step[1].clone()).unwrap();
|
||||
assert!(s.clipboard.is_none(), "{step}");
|
||||
let other = s.doc().unwrap().wb.sheet_index("Other").unwrap();
|
||||
s.execute("sheet.activate", json!({"sheet": other})).unwrap();
|
||||
assert!(s.execute("edit.paste", json!({"at": "C1"})).is_err(), "{step}");
|
||||
assert_eq!((at(&s, "Sheet1", "A1"), at(&s, "Other", "A1")), (Value::Number(1.0), Value::Number(7.0)), "{step}");
|
||||
}
|
||||
let new = s.doc().unwrap().wb.sheet_index("New").unwrap();
|
||||
s.execute("sheet.activate", json!({"sheet": s.doc().unwrap().wb.sheet_index("Sheet1").unwrap()})).unwrap();
|
||||
s.execute("edit.cut", json!({"range": "A1"})).unwrap();
|
||||
s.execute("home.deleteSheet", json!({"sheet": new})).unwrap();
|
||||
assert!(s.clipboard.is_none());
|
||||
// A copy stays, and so does a cut whose sheet keeps its place.
|
||||
s.execute("edit.copy", json!({"range": "A1"})).unwrap();
|
||||
s.execute("home.insertSheet", json!({"name": "More", "before": 0})).unwrap();
|
||||
assert!(s.clipboard.is_some());
|
||||
let last = s.doc().unwrap().wb.sheets.len();
|
||||
s.execute("sheet.activate", json!({"sheet": s.doc().unwrap().wb.sheet_index("Sheet1").unwrap()})).unwrap();
|
||||
s.execute("edit.cut", json!({"range": "A1"})).unwrap();
|
||||
s.execute("home.insertSheet", json!({"name": "After", "before": last})).unwrap();
|
||||
assert!(s.clipboard.is_some());
|
||||
}
|
||||
|
||||
/// A cut pasted into a row the filter hides would skip every cell it writes, so the cells would
|
||||
/// leave the sheet with the cut: it is refused and nothing changes. A visible row takes it.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user