mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-18 16:15:15 +00:00
Tables: Go to Row names the page under the row and reads no cell text
table.goToRow took the page from the frame's center, so a row in a frame that spans two pages of a spread could name the other page, and the view moved there. It now takes the page under the row's first cell. table.get takes cells: false to leave out the cells, and the Go to Row dialog uses it, so opening the dialog on a large table does not copy every cell's text.
This commit is contained in:
1 parent
c2b4514ee6
commit
cc49395696
4 files changed
+59
-19
No files matched your search
@@ -70,19 +70,23 @@ pub(super) fn get(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let g = target(s, p, "table.get")?;
|
||||
let st = s.doc()?;
|
||||
let t = st.doc.story(g.story).and_then(|x| x.tables.get(&g.table)).ok_or_else(|| bad("table.get", "no table"))?;
|
||||
let owners = t.owners();
|
||||
let cells: Vec<Value> = (0..t.nrows())
|
||||
.flat_map(|r| (0..t.ncols()).map(move |c| (r, c)))
|
||||
.filter(|&(r, c)| owners[r * t.ncols() + c] == (r, c))
|
||||
.filter_map(|(r, c)| {
|
||||
let cell = t.cell(r, c)?;
|
||||
Some(json!({"row": r, "col": c, "rowSpan": cell.row_span, "colSpan": cell.col_span, "text": cell.text.text, "fill": cell.fill,
|
||||
"insets": cell.insets, "vj": cell.vj, "firstBaseline": cell.first_baseline, "firstBaselineMin": cell.first_baseline_min, "rotation": cell.rotation, "style": cell.style}))
|
||||
})
|
||||
.collect();
|
||||
Ok(json!({
|
||||
let mut out = json!({
|
||||
"story": g.story.0, "table": g.table, "anchor": st.doc.story(g.story).and_then(|x| x.table_anchor(g.table)),
|
||||
"rows": t.rows, "columns": t.columns, "headerRows": t.header_rows(), "footerRows": t.footer_rows(),
|
||||
"cells": cells, "options": t.options, "range": g.range, "style": t.style,
|
||||
}))
|
||||
"options": t.options, "range": g.range, "style": t.style,
|
||||
});
|
||||
if p.get("cells").and_then(Value::as_bool) != Some(false) {
|
||||
let owners = t.owners();
|
||||
let cells: Vec<Value> = (0..t.nrows())
|
||||
.flat_map(|r| (0..t.ncols()).map(move |c| (r, c)))
|
||||
.filter(|&(r, c)| owners[r * t.ncols() + c] == (r, c))
|
||||
.filter_map(|(r, c)| {
|
||||
let cell = t.cell(r, c)?;
|
||||
Some(json!({"row": r, "col": c, "rowSpan": cell.row_span, "colSpan": cell.col_span, "text": cell.text.text, "fill": cell.fill,
|
||||
"insets": cell.insets, "vj": cell.vj, "firstBaseline": cell.first_baseline, "firstBaselineMin": cell.first_baseline_min, "rotation": cell.rotation, "style": cell.style}))
|
||||
})
|
||||
.collect();
|
||||
out["cells"] = json!(cells);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
@@ -416,6 +416,6 @@ fn table_specs() -> Vec<CommandSpec> {
|
||||
in_cell,
|
||||
|s, _| toggle_selection(s)
|
||||
),
|
||||
cmd!(query "table.get", "Get Table", [], None, "{story?, table?, rows?: [a,b] | row?, cols?: [a,b] | col?} → rows, columns, cells (text, spans, fill), options", in_table_or_ids, get),
|
||||
cmd!(query "table.get", "Get Table", [], None, "{story?, table?, rows?: [a,b] | row?, cols?: [a,b] | col?, cells?: bool (false leaves out the cells)} → rows, columns, cells (text, spans, fill), options", in_table_or_ids, get),
|
||||
]
|
||||
}
|
||||
@@ -72,14 +72,17 @@ pub(super) fn go_to_row(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cells: Vec<(usize, usize)> = owners.get(start..start.saturating_add(nc)).map(<[_]>::to_vec).unwrap_or_default();
|
||||
let st = s.doc()?;
|
||||
let cs = s.cache.get(&st.doc, g.story, None);
|
||||
let frame = cs.frames.iter().find_map(|ft| {
|
||||
let placed = cs.frames.iter().find_map(|ft| {
|
||||
ft.tables
|
||||
.iter()
|
||||
.filter(|t| t.table == g.table)
|
||||
.any(|t| t.cells.iter().any(|c| !c.repeated && cells.contains(&(c.row, c.col))))
|
||||
.then_some(ft.frame)
|
||||
.flat_map(|t| t.cells.iter())
|
||||
.find(|c| !c.repeated && cells.contains(&(c.row, c.col)))
|
||||
.map(|c| (ft.frame, c.rect.center()))
|
||||
});
|
||||
let page = frame.and_then(|f| st.doc.page_of_item(f)).map(|p| p + 1);
|
||||
let frame = placed.map(|(f, _)| f);
|
||||
// The page under the row's first cell: a frame can span two pages of a spread.
|
||||
let page = placed.and_then(|(f, at)| page_under(&st.doc, f, at)).map(|p| p + 1);
|
||||
// The caret is shown in the frame that shows the row.
|
||||
if let Some(f) = frame
|
||||
&& let Some(t) = s.doc_mut()?.selection.text.as_mut()
|
||||
@@ -89,6 +92,15 @@ pub(super) fn go_to_row(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"story": r["story"], "table": r["table"], "range": r["range"], "page": page}))
|
||||
}
|
||||
|
||||
/// The page (from 0) under `at`, a point in the inner space of text frame `frame`.
|
||||
fn page_under(doc: &designcraft_doc::Document, frame: designcraft_doc::ItemId, at: designcraft_geom::Point) -> Option<usize> {
|
||||
let loc = doc.find(frame)?;
|
||||
let designcraft_doc::SpreadRef::Doc(si) = loc.spread else { return None };
|
||||
let it = doc.item_at(&loc)?;
|
||||
let p = doc.parent_xf(&loc) * doc.text_xf(it) * at;
|
||||
Some(doc.first_page_of_spread(si) + doc.spread(loc.spread)?.page_at_x(p.x).unwrap_or(0))
|
||||
}
|
||||
|
||||
/// Move the caret to the next/previous cell (Tab / Shift-Tab), selecting its text. Tab in the last
|
||||
/// cell adds a row.
|
||||
pub(crate) fn step_cell(s: &mut Session, forward: bool) -> Result<Value> {
|
||||
@@ -292,4 +304,28 @@ mod go_to_row_tests {
|
||||
// The header row repeats on page 2; the row itself is the one on page 1.
|
||||
assert_eq!(s.execute("table.goToRow", &json!({"row": 1, "section": "header"})).unwrap()["page"], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_get_without_cells_reads_the_rows_alone() {
|
||||
// The Go to Row dialog reads the row counts and the selection, not every cell's text.
|
||||
let mut s = session();
|
||||
s.execute("table.goToRow", &json!({"row": 2})).unwrap();
|
||||
let t = s.execute("table.get", &json!({"cells": false})).unwrap();
|
||||
assert!(t.get("cells").is_none(), "{t}");
|
||||
assert_eq!((t["headerRows"].clone(), t["footerRows"].clone(), t["range"]["r0"].clone()), (json!(1), json!(1), json!(2)));
|
||||
assert_eq!(t["rows"].as_array().map(Vec::len), Some(5));
|
||||
assert_eq!(s.execute("table.get", &json!({})).unwrap()["cells"].as_array().map(Vec::len), Some(10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn go_to_row_names_the_page_under_the_row_in_a_frame_across_the_spine() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 3, "facingPages": true})).unwrap();
|
||||
// Pages 2 and 3 meet at x = 612; the frame's center is on page 3, the narrow table on page 2.
|
||||
let f = s.execute("frame.create", &json!({"spread": 1, "rect": [400, 72, 900, 400], "content": "text", "text": ""})).unwrap();
|
||||
s.execute("text.select", &json!({"story": f["story"], "anchor": 0, "focus": 0})).unwrap();
|
||||
s.execute("table.insert", &json!({"rows": 2, "cols": 2})).unwrap();
|
||||
s.execute("table.setColumnWidth", &json!({"cols": [0, 1], "width": 50})).unwrap();
|
||||
assert_eq!(s.execute("table.goToRow", &json!({"row": 2})).unwrap()["page"], 2);
|
||||
}
|
||||
}
|
||||
@@ -1506,7 +1506,7 @@ fn doc_unit(app: &DesignApp) -> Unit {
|
||||
/// Table › Go to Row…: opens on the target table's first selected row, numbered within its header,
|
||||
/// body or footer rows.
|
||||
pub fn open_go_to_row(app: &mut DesignApp) {
|
||||
let Ok(t) = app.run("table.get", json!({})) else { return };
|
||||
let Ok(t) = app.run("table.get", json!({"cells": false})) else { return };
|
||||
let (h, f, r) = (count_at(&t, "headerRows"), count_at(&t, "footerRows"), count_at(&t["range"], "r0"));
|
||||
let nr = t["rows"].as_array().map_or(0, Vec::len);
|
||||
let (section, n) = if r < h {
|
||||
|
||||
Reference in new issue
Block a user