A sheet pages down its rows and across its columns
Browse files at this commita3051e606478b2e5ce0ce4f369b0429ef9acca35Parent: cf7b629- feat
- a sheet pages down its rows and across its columns
7 changed files +394 -70
- crates/izlek-web/src/board.rs +11 -1
- crates/izlek-web/src/detail.rs +77 -15
- crates/izlek-web/src/i18n.rs +6 -3
- crates/izlek-web/src/sheet.rs +170 -49
- crates/izlek-web/tests/fixtures/wide.xlsx +0 -0
- crates/izlek-web/tests/http.rs +77 -0
- style/main.scss +53 -2
crates/izlek-web/src/board.rsmodified+11 -1
| @@ -500,6 +500,8 @@ struct BoardQuery { | |||
| 500 | tab: Option<String>, | 500 | tab: Option<String>, |
| 501 | file: Option<String>, | 501 | file: Option<String>, |
| 502 | sheet: Option<String>, | 502 | sheet: Option<String>, |
| 503 | + rows: Option<String>, | ||
| 504 | + cols: Option<String>, | ||
| 503 | refusal: Option<String>, | 505 | refusal: Option<String>, |
| 504 | on: Option<String>, | 506 | on: Option<String>, |
| 505 | sort: Option<String>, | 507 | sort: Option<String>, |
| @@ -550,6 +552,14 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | |||
| 550 | .as_deref() | 552 | .as_deref() |
| 551 | .and_then(|value| value.parse::<usize>().ok()) | 553 | .and_then(|value| value.parse::<usize>().ok()) |
| 552 | .unwrap_or(0); | 554 | .unwrap_or(0); |
| 555 | + // Which window of it is drawn, in pages of rows and pages of columns. | ||
| 556 | + let page_of = |value: Option<&str>| { | ||
| 557 | + value | ||
| 558 | + .and_then(|value| value.parse::<usize>().ok()) | ||
| 559 | + .unwrap_or(0) | ||
| 560 | + }; | ||
| 561 | + let open_rows = page_of(query.rows.as_deref()); | ||
| 562 | + let open_cols = page_of(query.cols.as_deref()); | ||
| 553 | // `?task=X&new=1` together would render both modals at once — two | 563 | // `?task=X&new=1` together would render both modals at once — two |
| 554 | // document-level datepicker listeners double-stepping the month nav — so | 564 | // document-level datepicker listeners double-stepping the month nav — so |
| 555 | // an open task wins and `new` is ignored. | 565 | // an open task wins and `new` is ignored. |
| @@ -604,7 +614,7 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | |||
| 604 | if let Some(task_id) = &open_task { | 614 | if let Some(task_id) = &open_task { |
| 605 | (crate::detail::task_modal(cx, task_id, query.confirm.as_deref() == Some("delete"), open_tab).await?) | 615 | (crate::detail::task_modal(cx, task_id, query.confirm.as_deref() == Some("delete"), open_tab).await?) |
| 606 | if let Some(file_id) = &query.file { | 616 | if let Some(file_id) = &query.file { |
| 607 | - (crate::detail::file_viewer_modal(cx, task_id, file_id, open_tab, open_sheet).await?) | 617 | + (crate::detail::file_viewer_modal(cx, task_id, file_id, open_tab, open_sheet, open_rows, open_cols).await?) |
| 608 | } | 618 | } |
| 609 | } | 619 | } |
| 610 | if open_new { | 620 | if open_new { |
crates/izlek-web/src/detail.rsmodified+77 -15
| @@ -2119,6 +2119,8 @@ pub async fn file_viewer_modal( | |||
| 2119 | file_id: &str, | 2119 | file_id: &str, |
| 2120 | tab: Tab, | 2120 | tab: Tab, |
| 2121 | sheet_index: usize, | 2121 | sheet_index: usize, |
| 2122 | + row_page: usize, | ||
| 2123 | + column_page: usize, | ||
| 2122 | ) -> Result { | 2124 | ) -> Result { |
| 2123 | let user = match require_user(cx).await { | 2125 | let user = match require_user(cx).await { |
| 2124 | Ok(user) => user, | 2126 | Ok(user) => user, |
| @@ -2153,7 +2155,9 @@ pub async fn file_viewer_modal( | |||
| 2153 | let sheet = if kind == crate::files::ViewerKind::Sheet { | 2155 | let sheet = if kind == crate::files::ViewerKind::Sheet { |
| 2154 | match store.attachment_bytes(file_id).await { | 2156 | match store.attachment_bytes(file_id).await { |
| 2155 | Ok(Some(bytes)) => { | 2157 | Ok(Some(bytes)) => { |
| 2156 | - tokio::task::spawn_blocking(move || crate::sheet::read(bytes, sheet_index)) | 2158 | + tokio::task::spawn_blocking(move || { |
| 2159 | + crate::sheet::read(bytes, sheet_index, row_page, column_page) | ||
| 2160 | + }) | ||
| 2157 | .await | 2161 | .await |
| 2158 | .ok() | 2162 | .ok() |
| 2159 | .flatten() | 2163 | .flatten() |
| @@ -2199,11 +2203,10 @@ pub async fn file_viewer_modal( | |||
| 2199 | } | 2203 | } |
| 2200 | } | 2204 | } |
| 2201 | 2205 | ||
| 2202 | -/// One sheet of a workbook as a table: the tab strip when the book has more | 2206 | +/// One window of one sheet as a table: the tab strip when the book has more |
| 2203 | /// than one sheet, the grid with its column letters and row numbers, and the | 2207 | /// than one sheet, the grid with its column letters and row numbers, and the |
| 2204 | -/// clipped line when the sheet ran past what [`crate::sheet`] draws. A | 2208 | +/// two pagers that move the window. A workbook no reader here understands |
| 2205 | -/// workbook no reader here understands says so in one line — the header's | 2209 | +/// says so in one line — the header's download link is still the whole file. |
| 2206 | -/// download link is still the whole file. | ||
| 2207 | async fn sheet_view( | 2210 | async fn sheet_view( |
| 2208 | cx: &Cx, | 2211 | cx: &Cx, |
| 2209 | sheet: Option<crate::sheet::Sheet>, | 2212 | sheet: Option<crate::sheet::Sheet>, |
| @@ -2212,13 +2215,28 @@ async fn sheet_view( | |||
| 2212 | tab: Tab, | 2215 | tab: Tab, |
| 2213 | lang: Lang, | 2216 | lang: Lang, |
| 2214 | ) -> Result { | 2217 | ) -> Result { |
| 2218 | + use crate::sheet::{COLUMNS_PER_PAGE, ROWS_PER_PAGE, column_name}; | ||
| 2219 | + | ||
| 2215 | let Some(sheet) = sheet else { | 2220 | let Some(sheet) = sheet else { |
| 2216 | return view! { cx => <p class="sheet-note">(t(lang, Key::ThisFileWillNotOpen))</p> }; | 2221 | return view! { cx => <p class="sheet-note">(t(lang, Key::ThisFileWillNotOpen))</p> }; |
| 2217 | }; | 2222 | }; |
| 2218 | let width = sheet.rows.iter().map(Vec::len).max().unwrap_or(0); | 2223 | let width = sheet.rows.iter().map(Vec::len).max().unwrap_or(0); |
| 2219 | - let columns: Vec<String> = (0..width).map(crate::sheet::column_name).collect(); | 2224 | + let columns: Vec<String> = (0..width) |
| 2225 | + .map(|column| column_name(sheet.first_column + column)) | ||
| 2226 | + .collect(); | ||
| 2227 | + let row_page = sheet.first_row / ROWS_PER_PAGE; | ||
| 2228 | + let column_page = sheet.first_column / COLUMNS_PER_PAGE; | ||
| 2229 | + // Every link out of here is the same page with one number moved, so the | ||
| 2230 | + // window a link lands on is the window its own numbers name. | ||
| 2231 | + let at = |sheet_index: usize, rows: usize, columns: usize| { | ||
| 2232 | + format!( | ||
| 2233 | + "/?task={task_id}&tab={}&file={file_id}&sheet={sheet_index}&rows={rows}&cols={columns}", | ||
| 2234 | + tab.slug() | ||
| 2235 | + ) | ||
| 2236 | + }; | ||
| 2220 | // The tab strip is a row of links, not a control: switching sheets is a | 2237 | // The tab strip is a row of links, not a control: switching sheets is a |
| 2221 | - // navigation like every other overlay state on this page. | 2238 | + // navigation like every other overlay state on this page, and it starts |
| 2239 | + // that sheet at its own first window. | ||
| 2222 | let tabs: Vec<(String, String, &'static str)> = sheet | 2240 | let tabs: Vec<(String, String, &'static str)> = sheet |
| 2223 | .names | 2241 | .names |
| 2224 | .iter() | 2242 | .iter() |
| @@ -2226,10 +2244,7 @@ async fn sheet_view( | |||
| 2226 | .map(|(index, name)| { | 2244 | .map(|(index, name)| { |
| 2227 | ( | 2245 | ( |
| 2228 | name.clone(), | 2246 | name.clone(), |
| 2229 | - format!( | 2247 | + at(index, 0, 0), |
| 2230 | - "/?task={task_id}&tab={}&file={file_id}&sheet={index}", | ||
| 2231 | - tab.slug() | ||
| 2232 | - ), | ||
| 2233 | if index == sheet.index { | 2248 | if index == sheet.index { |
| 2234 | "sheet-tab sheet-tab-on" | 2249 | "sheet-tab sheet-tab-on" |
| 2235 | } else { | 2250 | } else { |
| @@ -2238,6 +2253,28 @@ async fn sheet_view( | |||
| 2238 | ) | 2253 | ) |
| 2239 | }) | 2254 | }) |
| 2240 | .collect(); | 2255 | .collect(); |
| 2256 | + let rows_back = (row_page > 0).then(|| at(sheet.index, row_page - 1, column_page)); | ||
| 2257 | + let rows_on = sheet | ||
| 2258 | + .more_rows | ||
| 2259 | + .then(|| at(sheet.index, row_page + 1, column_page)); | ||
| 2260 | + let columns_back = (column_page > 0).then(|| at(sheet.index, row_page, column_page - 1)); | ||
| 2261 | + let columns_on = sheet | ||
| 2262 | + .more_columns | ||
| 2263 | + .then(|| at(sheet.index, row_page, column_page + 1)); | ||
| 2264 | + // "51–100 / 5000" down the rows, "M–X / 40" across the columns: the | ||
| 2265 | + // window's own edges in the same vocabulary the grid labels its gutters | ||
| 2266 | + // with, counted against the whole sheet where the file's own size can be | ||
| 2267 | + // trusted and left uncounted where it cannot. | ||
| 2268 | + let row_range = match sheet.total_rows { | ||
| 2269 | + Some(total) => format!("{}–{} / {total}", sheet.first_row + 1, sheet.last_row()), | ||
| 2270 | + None => format!("{}–{}", sheet.first_row + 1, sheet.last_row()), | ||
| 2271 | + }; | ||
| 2272 | + let first_letter = column_name(sheet.first_column); | ||
| 2273 | + let last_letter = column_name(sheet.last_column().saturating_sub(1)); | ||
| 2274 | + let column_range = match sheet.total_columns { | ||
| 2275 | + Some(total) => format!("{first_letter}–{last_letter} / {total}"), | ||
| 2276 | + None => format!("{first_letter}–{last_letter}"), | ||
| 2277 | + }; | ||
| 2241 | view! { | 2278 | view! { |
| 2242 | cx => | 2279 | cx => |
| 2243 | <div class="viewer-sheet"> | 2280 | <div class="viewer-sheet"> |
| @@ -2261,7 +2298,7 @@ async fn sheet_view( | |||
| 2261 | <tbody> | 2298 | <tbody> |
| 2262 | for (index, row) in sheet.rows.iter().enumerate() { | 2299 | for (index, row) in sheet.rows.iter().enumerate() { |
| 2263 | <tr> | 2300 | <tr> |
| 2264 | - <th class="sheet-gutter">(format!("{}", index + 1))</th> | 2301 | + <th class="sheet-gutter">(format!("{}", sheet.first_row + index + 1))</th> |
| 2265 | for column in 0..width { | 2302 | for column in 0..width { |
| 2266 | <td>(row.get(column).cloned().unwrap_or_default())</td> | 2303 | <td>(row.get(column).cloned().unwrap_or_default())</td> |
| 2267 | } | 2304 | } |
| @@ -2270,9 +2307,34 @@ async fn sheet_view( | |||
| 2270 | </tbody> | 2307 | </tbody> |
| 2271 | </table> | 2308 | </table> |
| 2272 | </div> | 2309 | </div> |
| 2273 | - if sheet.clipped { | 2310 | + <div class="sheet-foot"> |
| 2274 | - <p class="sheet-note">(t(lang, Key::SheetClipped))</p> | 2311 | + <div class="sheet-pager"> |
| 2275 | - } | 2312 | + <span class="sheet-range">(row_range)</span> |
| 2313 | + if let Some(href) = rows_back { | ||
| 2314 | + <a class="sheet-step sheet-step-up" href=(href) aria-label=(t(lang, Key::Previous))>(glyph::chevron(cx).await?)</a> | ||
| 2315 | + } else { | ||
| 2316 | + <span class="sheet-step sheet-step-up sheet-step-off">(glyph::chevron(cx).await?)</span> | ||
| 2317 | + } | ||
| 2318 | + if let Some(href) = rows_on { | ||
| 2319 | + <a class="sheet-step" href=(href) aria-label=(t(lang, Key::Next))>(glyph::chevron(cx).await?)</a> | ||
| 2320 | + } else { | ||
| 2321 | + <span class="sheet-step sheet-step-off">(glyph::chevron(cx).await?)</span> | ||
| 2322 | + } | ||
| 2323 | + </div> | ||
| 2324 | + <div class="sheet-pager"> | ||
| 2325 | + <span class="sheet-range">(column_range)</span> | ||
| 2326 | + if let Some(href) = columns_back { | ||
| 2327 | + <a class="sheet-step sheet-step-left" href=(href) aria-label=(t(lang, Key::Previous))>(glyph::chevron(cx).await?)</a> | ||
| 2328 | + } else { | ||
| 2329 | + <span class="sheet-step sheet-step-left sheet-step-off">(glyph::chevron(cx).await?)</span> | ||
| 2330 | + } | ||
| 2331 | + if let Some(href) = columns_on { | ||
| 2332 | + <a class="sheet-step sheet-step-right" href=(href) aria-label=(t(lang, Key::Next))>(glyph::chevron(cx).await?)</a> | ||
| 2333 | + } else { | ||
| 2334 | + <span class="sheet-step sheet-step-right sheet-step-off">(glyph::chevron(cx).await?)</span> | ||
| 2335 | + } | ||
| 2336 | + </div> | ||
| 2337 | + </div> | ||
| 2276 | </div> | 2338 | </div> |
| 2277 | } | 2339 | } |
| 2278 | } | 2340 | } |
crates/izlek-web/src/i18n.rsmodified+6 -3
| @@ -66,8 +66,9 @@ pub enum Key { | |||
| 66 | Play, | 66 | Play, |
| 67 | Pause, | 67 | Pause, |
| 68 | CloseTheFile, | 68 | CloseTheFile, |
| 69 | - SheetClipped, | ||
| 70 | ThisFileWillNotOpen, | 69 | ThisFileWillNotOpen, |
| 70 | + Previous, | ||
| 71 | + Next, | ||
| 71 | Esc, | 72 | Esc, |
| 72 | CloseThisTask, | 73 | CloseThisTask, |
| 73 | Status, | 74 | Status, |
| @@ -416,10 +417,12 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | |||
| 416 | (Pause, Tr) => "Duraklat", | 417 | (Pause, Tr) => "Duraklat", |
| 417 | (CloseTheFile, En) => "Close the file", | 418 | (CloseTheFile, En) => "Close the file", |
| 418 | (CloseTheFile, Tr) => "Dosyayı kapat", | 419 | (CloseTheFile, Tr) => "Dosyayı kapat", |
| 419 | - (SheetClipped, En) => "First 400 rows, 40 columns.", | ||
| 420 | - (SheetClipped, Tr) => "İlk 400 satır, 40 sütun.", | ||
| 421 | (ThisFileWillNotOpen, En) => "This file will not open.", | 420 | (ThisFileWillNotOpen, En) => "This file will not open.", |
| 422 | (ThisFileWillNotOpen, Tr) => "Bu dosya açılmıyor.", | 421 | (ThisFileWillNotOpen, Tr) => "Bu dosya açılmıyor.", |
| 422 | + (Previous, En) => "Previous", | ||
| 423 | + (Previous, Tr) => "Önceki", | ||
| 424 | + (Next, En) => "Next", | ||
| 425 | + (Next, Tr) => "Sonraki", | ||
| 423 | (Esc, En) => "esc", | 426 | (Esc, En) => "esc", |
| 424 | (Esc, Tr) => "esc", | 427 | (Esc, Tr) => "esc", |
| 425 | (CloseThisTask, En) => "Close this task", | 428 | (CloseThisTask, En) => "Close this task", |
crates/izlek-web/src/sheet.rsmodified+170 -49
| @@ -4,45 +4,85 @@ | |||
| 4 | //! ViewerKind`] this one is not a src on an element: the bytes are parsed | 4 | //! ViewerKind`] this one is not a src on an element: the bytes are parsed |
| 5 | //! here and come out as strings the view puts in a `<table>`. Values only — | 5 | //! here and come out as strings the view puts in a `<table>`. Values only — |
| 6 | //! a cell's formula, formatting, charts and images are not carried over. | 6 | //! a cell's formula, formatting, charts and images are not carried over. |
| 7 | +//! | ||
| 8 | +//! A sheet is read one window at a time, never whole: a page of rows and a | ||
| 9 | +//! page of columns, both moved by links. A book of any size costs the same | ||
| 10 | +//! page, and nothing has to be dragged sideways to reach a column. | ||
| 7 | 11 | ||
| 8 | use std::io::Cursor; | 12 | use std::io::Cursor; |
| 9 | 13 | ||
| 10 | use calamine::{Data, Reader, Xlsx, open_workbook_auto_from_rs, open_workbook_from_rs}; | 14 | use calamine::{Data, Reader, Xlsx, open_workbook_auto_from_rs, open_workbook_from_rs}; |
| 11 | 15 | ||
| 12 | -/// How much of one sheet is drawn. A workbook is a million rows wide open, | 16 | +/// One window of a sheet. Sized to land on a screen rather than to be |
| 13 | -/// and a table that size is a page that never paints; past this the view says | 17 | +/// generous: what does not fit is one link away, and a page that has to be |
| 14 | -/// so and the download is the whole file. | 18 | +/// panned is the thing paging exists to avoid. |
| 15 | -pub(crate) const MAX_ROWS: usize = 400; | 19 | +pub(crate) const ROWS_PER_PAGE: usize = 50; |
| 16 | -pub(crate) const MAX_COLS: usize = 40; | 20 | +pub(crate) const COLUMNS_PER_PAGE: usize = 12; |
| 17 | 21 | ||
| 18 | -/// One sheet of a workbook, ready to lay out: every sheet's name for the tab | 22 | +/// One window of one sheet, ready to lay out: every sheet's name for the tab |
| 19 | -/// strip, which of them this is, and its cells as display strings. | 23 | +/// strip, which of them this is, where the window sits, and whether the sheet |
| 24 | +/// runs past it. | ||
| 20 | pub(crate) struct Sheet { | 25 | pub(crate) struct Sheet { |
| 21 | pub names: Vec<String>, | 26 | pub names: Vec<String>, |
| 22 | pub index: usize, | 27 | pub index: usize, |
| 23 | pub rows: Vec<Vec<String>>, | 28 | pub rows: Vec<Vec<String>>, |
| 24 | - /// True when the sheet ran past [`MAX_ROWS`] or [`MAX_COLS`] and what is | 29 | + /// Zero-based offsets of the window's own corner, for the row numbers and |
| 25 | - /// drawn is a corner of it. | 30 | + /// column letters the grid draws down its edges. |
| 26 | - pub clipped: bool, | 31 | + pub first_row: usize, |
| 32 | + pub first_column: usize, | ||
| 33 | + /// The whole sheet's size, when the file says so and the reading agrees. | ||
| 34 | + /// An xlsx declares its own dimensions and plenty of writers get them | ||
| 35 | + /// wrong, so a count that cannot be trusted is no count at all rather | ||
| 36 | + /// than a number the footer would print as fact. | ||
| 37 | + pub total_rows: Option<usize>, | ||
| 38 | + pub total_columns: Option<usize>, | ||
| 39 | + /// Whether there is another window past this one. Observed, not derived | ||
| 40 | + /// from the count: a cell arriving past the window is proof, and proof is | ||
| 41 | + /// what a pager needs. | ||
| 42 | + pub more_rows: bool, | ||
| 43 | + pub more_columns: bool, | ||
| 27 | } | 44 | } |
| 28 | 45 | ||
| 29 | -/// The workbook's `index`-th sheet, or `None` when the bytes are not a | 46 | +impl Sheet { |
| 30 | -/// workbook any reader here understands — a `.doc` that sniffed as an OLE | 47 | + /// The window's own last row and column, one-based — what the footer |
| 31 | -/// container, or an `.xlsx` truncated in transit. An out-of-range `index` | 48 | + /// counts up to. |
| 32 | -/// (a hand-edited query string) falls back to the first sheet rather than | 49 | + pub fn last_row(&self) -> usize { |
| 33 | -/// failing. | 50 | + self.first_row + self.rows.len() |
| 34 | -pub(crate) fn read(bytes: Vec<u8>, index: usize) -> Option<Sheet> { | 51 | + } |
| 52 | + | ||
| 53 | + pub fn last_column(&self) -> usize { | ||
| 54 | + self.first_column + self.rows.iter().map(Vec::len).max().unwrap_or(0) | ||
| 55 | + } | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +/// The `index`-th sheet of the workbook, windowed to `row_page` and | ||
| 59 | +/// `column_page`. `None` when the bytes are not a workbook any reader here | ||
| 60 | +/// understands — a `.doc` that sniffed as an OLE container, or an `.xlsx` | ||
| 61 | +/// truncated in transit. An out-of-range sheet or page (a hand-edited query | ||
| 62 | +/// string) falls back to the first one rather than failing. | ||
| 63 | +pub(crate) fn read( | ||
| 64 | + bytes: Vec<u8>, | ||
| 65 | + index: usize, | ||
| 66 | + row_page: usize, | ||
| 67 | + column_page: usize, | ||
| 68 | +) -> Option<Sheet> { | ||
| 35 | // xlsx first, because that is the format that holds millions of rows and | 69 | // xlsx first, because that is the format that holds millions of rows and |
| 36 | // the only one calamine will hand over a cell at a time. Reading the | 70 | // the only one calamine will hand over a cell at a time. Reading the |
| 37 | // whole range of a 300,000-row book costs about 380MB and most of a | 71 | // whole range of a 300,000-row book costs about 380MB and most of a |
| 38 | - // second; stopping at the rows that are drawn costs neither. | 72 | + // second; stopping at the end of the window costs neither. |
| 39 | - read_streamed(&bytes, index).or_else(|| read_whole(bytes, index)) | 73 | + read_streamed(&bytes, index, row_page, column_page) |
| 74 | + .or_else(|| read_whole(bytes, index, row_page, column_page)) | ||
| 40 | } | 75 | } |
| 41 | 76 | ||
| 42 | /// The lazy path: an xlsx read cell by cell in stream order, abandoned the | 77 | /// The lazy path: an xlsx read cell by cell in stream order, abandoned the |
| 43 | -/// moment the rows past [`MAX_ROWS`] start. `None` when the bytes are not an | 78 | +/// moment the rows past the window start. `None` when the bytes are not an |
| 44 | /// xlsx at all, which is the caller's cue to try the other formats. | 79 | /// xlsx at all, which is the caller's cue to try the other formats. |
| 45 | -fn read_streamed(bytes: &[u8], index: usize) -> Option<Sheet> { | 80 | +fn read_streamed( |
| 81 | + bytes: &[u8], | ||
| 82 | + index: usize, | ||
| 83 | + row_page: usize, | ||
| 84 | + column_page: usize, | ||
| 85 | +) -> Option<Sheet> { | ||
| 46 | let mut book: Xlsx<Cursor<Vec<u8>>> = open_workbook_from_rs(Cursor::new(bytes.to_vec())).ok()?; | 86 | let mut book: Xlsx<Cursor<Vec<u8>>> = open_workbook_from_rs(Cursor::new(bytes.to_vec())).ok()?; |
| 47 | let names = book.sheet_names(); | 87 | let names = book.sheet_names(); |
| 48 | if names.is_empty() { | 88 | if names.is_empty() { |
| @@ -51,31 +91,44 @@ fn read_streamed(bytes: &[u8], index: usize) -> Option<Sheet> { | |||
| 51 | let index = if index < names.len() { index } else { 0 }; | 91 | let index = if index < names.len() { index } else { 0 }; |
| 52 | let mut cells = book.worksheet_cells_reader(&names[index]).ok()?; | 92 | let mut cells = book.worksheet_cells_reader(&names[index]).ok()?; |
| 53 | let bounds = cells.dimensions(); | 93 | let bounds = cells.dimensions(); |
| 54 | - let (first_row, first_column) = bounds.start; | 94 | + let (sheet_row, sheet_column) = bounds.start; |
| 55 | - let height = bounds.end.0.saturating_sub(first_row) as usize + 1; | 95 | + let declared_rows = bounds.end.0.saturating_sub(sheet_row) as usize + 1; |
| 56 | - let width = bounds.end.1.saturating_sub(first_column) as usize + 1; | 96 | + let declared_columns = bounds.end.1.saturating_sub(sheet_column) as usize + 1; |
| 57 | - let mut clipped = height > MAX_ROWS || width > MAX_COLS; | 97 | + // The asked-for page is honoured whatever the file declares: an xlsx |
| 98 | + // dimension is a claim, not a fact, and clamping to a wrong one would | ||
| 99 | + // snap a reader back to the first window every time they stepped on. | ||
| 100 | + let (first_row, first_column) = (row_page * ROWS_PER_PAGE, column_page * COLUMNS_PER_PAGE); | ||
| 58 | let mut rows: Vec<Vec<String>> = Vec::new(); | 101 | let mut rows: Vec<Vec<String>> = Vec::new(); |
| 102 | + let mut more_rows = false; | ||
| 103 | + let mut more_columns = false; | ||
| 104 | + let mut seen_rows = 0; | ||
| 105 | + let mut seen_columns = 0; | ||
| 59 | while let Ok(Some(found)) = cells.next_cell() { | 106 | while let Ok(Some(found)) = cells.next_cell() { |
| 60 | let (row, column) = found.get_position(); | 107 | let (row, column) = found.get_position(); |
| 61 | // A cell before the sheet's own first row or column is a dimension | 108 | // A cell before the sheet's own first row or column is a dimension |
| 62 | // that lied; there is nowhere to put it, so it is passed over. | 109 | // that lied; there is nowhere to put it, so it is passed over. |
| 63 | let (Some(row), Some(column)) = ( | 110 | let (Some(row), Some(column)) = ( |
| 64 | - row.checked_sub(first_row).map(|row| row as usize), | 111 | + row.checked_sub(sheet_row).map(|row| row as usize), |
| 65 | - column.checked_sub(first_column).map(|column| column as usize), | 112 | + column.checked_sub(sheet_column).map(|column| column as usize), |
| 66 | ) else { | 113 | ) else { |
| 67 | continue; | 114 | continue; |
| 68 | }; | 115 | }; |
| 69 | - // Cells arrive in row order, so the first one past the last drawn row | 116 | + seen_rows = seen_rows.max(row + 1); |
| 70 | - // is the end of the reading — whatever the dimension claimed. | 117 | + seen_columns = seen_columns.max(column + 1); |
| 71 | - if row >= MAX_ROWS { | 118 | + // Cells arrive in row order, so the first one past the window is both |
| 72 | - clipped = true; | 119 | + // the end of the reading and the proof there is another window. |
| 120 | + if row >= first_row + ROWS_PER_PAGE { | ||
| 121 | + more_rows = true; | ||
| 73 | break; | 122 | break; |
| 74 | } | 123 | } |
| 75 | - if column >= MAX_COLS { | 124 | + if column >= first_column + COLUMNS_PER_PAGE { |
| 76 | - clipped = true; | 125 | + more_columns = true; |
| 126 | + continue; | ||
| 127 | + } | ||
| 128 | + if row < first_row || column < first_column { | ||
| 77 | continue; | 129 | continue; |
| 78 | } | 130 | } |
| 131 | + let (row, column) = (row - first_row, column - first_column); | ||
| 79 | if rows.len() <= row { | 132 | if rows.len() <= row { |
| 80 | rows.resize(row + 1, Vec::new()); | 133 | rows.resize(row + 1, Vec::new()); |
| 81 | } | 134 | } |
| @@ -85,18 +138,32 @@ fn read_streamed(bytes: &[u8], index: usize) -> Option<Sheet> { | |||
| 85 | } | 138 | } |
| 86 | line[column] = cell(&Data::from(found.get_value().clone())); | 139 | line[column] = cell(&Data::from(found.get_value().clone())); |
| 87 | } | 140 | } |
| 141 | + // The declared size is believed only where the reading bears it out. A | ||
| 142 | + // sheet whose cells ran past what it declared has a wrong dimension, and | ||
| 143 | + // one abandoned mid-read was never counted at all. | ||
| 144 | + let counted = !more_rows; | ||
| 88 | Some(Sheet { | 145 | Some(Sheet { |
| 89 | names, | 146 | names, |
| 90 | index, | 147 | index, |
| 148 | + total_rows: (counted && declared_rows >= seen_rows).then_some(declared_rows.max(seen_rows)), | ||
| 149 | + total_columns: (declared_columns >= seen_columns).then_some(declared_columns), | ||
| 150 | + more_rows: more_rows || declared_rows > first_row + ROWS_PER_PAGE, | ||
| 151 | + more_columns: more_columns || declared_columns > first_column + COLUMNS_PER_PAGE, | ||
| 91 | rows, | 152 | rows, |
| 92 | - clipped, | 153 | + first_row, |
| 154 | + first_column, | ||
| 93 | }) | 155 | }) |
| 94 | } | 156 | } |
| 95 | 157 | ||
| 96 | /// The whole-workbook path, for the formats with no cell-at-a-time reader: | 158 | /// The whole-workbook path, for the formats with no cell-at-a-time reader: |
| 97 | /// xls, xlsb and ods. All three are bounded by the upload limit and by their | 159 | /// xls, xlsb and ods. All three are bounded by the upload limit and by their |
| 98 | /// own row ceilings, so the range fits in memory in a way an xlsx need not. | 160 | /// own row ceilings, so the range fits in memory in a way an xlsx need not. |
| 99 | -fn read_whole(bytes: Vec<u8>, index: usize) -> Option<Sheet> { | 161 | +fn read_whole( |
| 162 | + bytes: Vec<u8>, | ||
| 163 | + index: usize, | ||
| 164 | + row_page: usize, | ||
| 165 | + column_page: usize, | ||
| 166 | +) -> Option<Sheet> { | ||
| 100 | let mut book = open_workbook_auto_from_rs(Cursor::new(bytes)).ok()?; | 167 | let mut book = open_workbook_auto_from_rs(Cursor::new(bytes)).ok()?; |
| 101 | let names = book.sheet_names(); | 168 | let names = book.sheet_names(); |
| 102 | if names.is_empty() { | 169 | if names.is_empty() { |
| @@ -104,17 +171,33 @@ fn read_whole(bytes: Vec<u8>, index: usize) -> Option<Sheet> { | |||
| 104 | } | 171 | } |
| 105 | let index = if index < names.len() { index } else { 0 }; | 172 | let index = if index < names.len() { index } else { 0 }; |
| 106 | let range = book.worksheet_range(&names[index]).ok()?; | 173 | let range = book.worksheet_range(&names[index]).ok()?; |
| 107 | - let clipped = range.height() > MAX_ROWS || range.width() > MAX_COLS; | 174 | + let total_rows = range.height(); |
| 108 | - let rows = range | 175 | + let total_columns = range.width(); |
| 176 | + let (first_row, first_column) = (row_page * ROWS_PER_PAGE, column_page * COLUMNS_PER_PAGE); | ||
| 177 | + let rows: Vec<Vec<String>> = range | ||
| 109 | .rows() | 178 | .rows() |
| 110 | - .take(MAX_ROWS) | 179 | + .skip(first_row) |
| 111 | - .map(|row| row.iter().take(MAX_COLS).map(cell).collect()) | 180 | + .take(ROWS_PER_PAGE) |
| 181 | + .map(|row| { | ||
| 182 | + row.iter() | ||
| 183 | + .skip(first_column) | ||
| 184 | + .take(COLUMNS_PER_PAGE) | ||
| 185 | + .map(cell) | ||
| 186 | + .collect() | ||
| 187 | + }) | ||
| 112 | .collect(); | 188 | .collect(); |
| 113 | Some(Sheet { | 189 | Some(Sheet { |
| 114 | names, | 190 | names, |
| 115 | index, | 191 | index, |
| 116 | rows, | 192 | rows, |
| 117 | - clipped, | 193 | + first_row, |
| 194 | + first_column, | ||
| 195 | + // Nothing is declared here: the whole range was read, so its size is | ||
| 196 | + // measured rather than believed. | ||
| 197 | + total_rows: Some(total_rows), | ||
| 198 | + total_columns: Some(total_columns), | ||
| 199 | + more_rows: total_rows > first_row + ROWS_PER_PAGE, | ||
| 200 | + more_columns: total_columns > first_column + COLUMNS_PER_PAGE, | ||
| 118 | }) | 201 | }) |
| 119 | } | 202 | } |
| 120 | 203 | ||
| @@ -208,21 +291,59 @@ mod tests { | |||
| 208 | assert_eq!(column_name(702), "AAA"); | 291 | assert_eq!(column_name(702), "AAA"); |
| 209 | } | 292 | } |
| 210 | 293 | ||
| 211 | - /// The clip is what keeps a workbook of any size from becoming a page of | 294 | + /// The window is what keeps a workbook of any size from becoming a page |
| 212 | - /// that size — and on the streamed path, from being read at all past the | 295 | + /// of that size — and on the streamed path, from being read at all past |
| 213 | - /// rows that are drawn. | 296 | + /// the rows it draws. |
| 214 | #[test] | 297 | #[test] |
| 215 | - fn a_book_past_the_clip_is_read_only_as_far_as_it_is_drawn() { | 298 | + fn a_page_holds_one_window_and_the_next_page_holds_the_next() { |
| 216 | let bytes = include_bytes!("../tests/fixtures/tall.xlsx").to_vec(); | 299 | let bytes = include_bytes!("../tests/fixtures/tall.xlsx").to_vec(); |
| 217 | - let sheet = read(bytes, 0).expect("the workbook opens"); | 300 | + let first = read(bytes.clone(), 0, 0, 0).expect("the workbook opens"); |
| 218 | - assert_eq!(sheet.rows.len(), MAX_ROWS); | 301 | + assert_eq!(first.rows.len(), ROWS_PER_PAGE); |
| 219 | - assert!(sheet.clipped); | 302 | + // This fixture declares no dimension, the way plenty of writers |
| 220 | - assert_eq!(sheet.rows[0][0], "Row 1"); | 303 | + // leave one out, so its size is not counted — but the next window is |
| 221 | - assert_eq!(sheet.rows[MAX_ROWS - 1][0], format!("Row {MAX_ROWS}")); | 304 | + // still known to be there, because a cell arrived in it. |
| 305 | + assert_eq!(first.total_rows, None); | ||
| 306 | + assert!(first.more_rows); | ||
| 307 | + assert_eq!(first.first_row, 0); | ||
| 308 | + assert_eq!(first.rows[0][0], "Row 1"); | ||
| 309 | + assert_eq!(first.rows[ROWS_PER_PAGE - 1][0], format!("Row {ROWS_PER_PAGE}")); | ||
| 310 | + | ||
| 311 | + let third = read(bytes.clone(), 0, 2, 0).expect("the workbook opens"); | ||
| 312 | + assert_eq!(third.first_row, 2 * ROWS_PER_PAGE); | ||
| 313 | + assert_eq!( | ||
| 314 | + third.rows[0][0], | ||
| 315 | + format!("Row {}", 2 * ROWS_PER_PAGE + 1) | ||
| 316 | + ); | ||
| 317 | + | ||
| 318 | + // A page nobody links to — typed into the query string — is the | ||
| 319 | + // empty window it names, with the step back to a real one still | ||
| 320 | + // there. Nothing pretends it was a different page. | ||
| 321 | + let past_the_end = read(bytes, 0, 9_000, 0).expect("the workbook opens"); | ||
| 322 | + assert!(past_the_end.rows.is_empty()); | ||
| 323 | + assert!(!past_the_end.more_rows); | ||
| 324 | + } | ||
| 325 | + | ||
| 326 | + /// A wide sheet pages sideways the same way, and the window's own corner | ||
| 327 | + /// is what the grid labels its edges from. | ||
| 328 | + #[test] | ||
| 329 | + fn columns_page_the_same_way_rows_do() { | ||
| 330 | + let bytes = include_bytes!("../tests/fixtures/wide.xlsx").to_vec(); | ||
| 331 | + let first = read(bytes.clone(), 0, 0, 0).expect("the workbook opens"); | ||
| 332 | + assert_eq!(first.total_columns, Some(40)); | ||
| 333 | + assert!(first.more_columns); | ||
| 334 | + assert_eq!(first.rows[0].len(), COLUMNS_PER_PAGE); | ||
| 335 | + assert_eq!(first.rows[0][0], "Col A"); | ||
| 336 | + | ||
| 337 | + let second = read(bytes, 0, 0, 1).expect("the workbook opens"); | ||
| 338 | + assert_eq!(second.first_column, COLUMNS_PER_PAGE); | ||
| 339 | + assert_eq!( | ||
| 340 | + second.rows[0][0], | ||
| 341 | + format!("Col {}", column_name(COLUMNS_PER_PAGE)) | ||
| 342 | + ); | ||
| 222 | } | 343 | } |
| 223 | 344 | ||
| 224 | #[test] | 345 | #[test] |
| 225 | fn bytes_that_are_not_a_workbook_have_no_sheet() { | 346 | fn bytes_that_are_not_a_workbook_have_no_sheet() { |
| 226 | - assert!(read(b"not a workbook at all".to_vec(), 0).is_none()); | 347 | + assert!(read(b"not a workbook at all".to_vec(), 0, 0, 0).is_none()); |
| 227 | } | 348 | } |
| 228 | } | 349 | } |
| \ No newline at end of file | \ No newline at end of file | ||
crates/izlek-web/tests/fixtures/wide.xlsxadded+0 -0
Binary file changed
crates/izlek-web/tests/http.rsmodified+77 -0
| @@ -6494,3 +6494,80 @@ async fn an_admin_sets_the_address_mail_links_point_at() { | |||
| 6494 | "an empty field did not clear the stored address" | 6494 | "an empty field did not clear the stored address" |
| 6495 | ); | 6495 | ); |
| 6496 | } | 6496 | } |
| 6497 | + | ||
| 6498 | +/// A sheet is read one window at a time and the pagers move it: down the rows | ||
| 6499 | +/// and across the columns, each a link like every other overlay state. The | ||
| 6500 | +/// window's own numbers and letters label the grid, so a page says where it | ||
| 6501 | +/// sits without being told. | ||
| 6502 | +#[tokio::test] | ||
| 6503 | +async fn a_big_sheet_pages_down_its_rows_and_across_its_columns() { | ||
| 6504 | + let app = App::open().await; | ||
| 6505 | + let admin_cookie = admin(&app).await; | ||
| 6506 | + let column = first_column(&app).await; | ||
| 6507 | + let task = a_task(&app, &admin_cookie, &column, "Read the big book").await; | ||
| 6508 | + | ||
| 6509 | + let book = include_bytes!("fixtures/wide.xlsx"); | ||
| 6510 | + app.post_multipart( | ||
| 6511 | + "/files", | ||
| 6512 | + Some(&admin_cookie), | ||
| 6513 | + &[("task_id", &task)], | ||
| 6514 | + Some(( | ||
| 6515 | + "wide.xlsx", | ||
| 6516 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||
| 6517 | + book, | ||
| 6518 | + )), | ||
| 6519 | + ) | ||
| 6520 | + .await; | ||
| 6521 | + let snapshot = app | ||
| 6522 | + .post( | ||
| 6523 | + "/api/fetch_task", | ||
| 6524 | + Some(&admin_cookie), | ||
| 6525 | + &[("task_id", &task)], | ||
| 6526 | + ) | ||
| 6527 | + .await; | ||
| 6528 | + let file_id = attachment_id_named(&snapshot.body, "wide.xlsx"); | ||
| 6529 | + | ||
| 6530 | + let first = app | ||
| 6531 | + .get( | ||
| 6532 | + &format!("/?task={task}&file={file_id}"), | ||
| 6533 | + Some(&admin_cookie), | ||
| 6534 | + ) | ||
| 6535 | + .await; | ||
| 6536 | + let html = String::from_utf8_lossy(&first.bytes); | ||
| 6537 | + assert!(html.contains("Col A"), "the first column is missing: {html}"); | ||
| 6538 | + assert!(!html.contains("Col M"), "the window is not 12 wide: {html}"); | ||
| 6539 | + assert!(html.contains("A–L / 40"), "no column count: {html}"); | ||
| 6540 | + assert!(html.contains("1–8 / 8"), "no row count: {html}"); | ||
| 6541 | + assert!( | ||
| 6542 | + html.contains(&format!("file={file_id}&sheet=0&rows=0&cols=1")), | ||
| 6543 | + "no step to the next columns: {html}" | ||
| 6544 | + ); | ||
| 6545 | + | ||
| 6546 | + // Stepping across lands on the next twelve columns, and the grid labels | ||
| 6547 | + // them from where the window sits rather than from A. | ||
| 6548 | + let across = app | ||
| 6549 | + .get( | ||
| 6550 | + &format!("/?task={task}&file={file_id}&sheet=0&rows=0&cols=1"), | ||
| 6551 | + Some(&admin_cookie), | ||
| 6552 | + ) | ||
| 6553 | + .await; | ||
| 6554 | + let html = String::from_utf8_lossy(&across.bytes); | ||
| 6555 | + assert!(html.contains("Col M"), "the second window is wrong: {html}"); | ||
| 6556 | + assert!(html.contains("M–X / 40"), "the count did not move: {html}"); | ||
| 6557 | + assert!( | ||
| 6558 | + html.contains(&format!("file={file_id}&sheet=0&rows=0&cols=0")), | ||
| 6559 | + "no step back to the first columns: {html}" | ||
| 6560 | + ); | ||
| 6561 | + | ||
| 6562 | + // A window nobody links to is the empty one it names, not a redirect and | ||
| 6563 | + // not the first page wearing another page's numbers. | ||
| 6564 | + let nowhere = app | ||
| 6565 | + .get( | ||
| 6566 | + &format!("/?task={task}&file={file_id}&sheet=0&rows=99&cols=0"), | ||
| 6567 | + Some(&admin_cookie), | ||
| 6568 | + ) | ||
| 6569 | + .await; | ||
| 6570 | + let html = String::from_utf8_lossy(&nowhere.bytes); | ||
| 6571 | + assert!(html.contains("sheet-table"), "the grid is gone: {html}"); | ||
| 6572 | + assert!(!html.contains("Col A"), "row 4951 is not row 1: {html}"); | ||
| 6573 | +} | ||
style/main.scssmodified+53 -2
| @@ -1237,7 +1237,7 @@ img.avatar { | |||
| 1237 | // further, and the grid takes whatever is left under the tab strip and pans | 1237 | // further, and the grid takes whatever is left under the tab strip and pans |
| 1238 | // inside that — the panel itself never leaves the screen. | 1238 | // inside that — the panel itself never leaves the screen. |
| 1239 | .viewer:has(.viewer-sheet) { | 1239 | .viewer:has(.viewer-sheet) { |
| 1240 | - width: min(96vw, 1400px); | 1240 | + width: auto; |
| 1241 | max-width: min(96vw, 1400px); | 1241 | max-width: min(96vw, 1400px); |
| 1242 | display: flex; | 1242 | display: flex; |
| 1243 | flex-direction: column; | 1243 | flex-direction: column; |
| @@ -1287,8 +1287,11 @@ img.avatar { | |||
| 1287 | border-color: var(--accent); | 1287 | border-color: var(--accent); |
| 1288 | } | 1288 | } |
| 1289 | 1289 | ||
| 1290 | +// A window is sized to land on a screen, so the grid takes only the room its | ||
| 1291 | +// own rows need — the pagers below it stay in view rather than being pushed | ||
| 1292 | +// off by a table that always claims the whole panel. | ||
| 1290 | .sheet-grid { | 1293 | .sheet-grid { |
| 1291 | - flex: 1; | 1294 | + flex: 0 1 auto; |
| 1292 | min-width: 0; | 1295 | min-width: 0; |
| 1293 | min-height: 0; | 1296 | min-height: 0; |
| 1294 | overflow: auto; | 1297 | overflow: auto; |
| @@ -1345,6 +1348,54 @@ img.avatar { | |||
| 1345 | font-size: 12px; | 1348 | font-size: 12px; |
| 1346 | } | 1349 | } |
| 1347 | 1350 | ||
| 1351 | +// A sheet is read one window at a time, and these move it: the rows down the | ||
| 1352 | +// left, the columns across the right, each counted against the whole sheet. | ||
| 1353 | +// A step with nowhere to go stays in place, dimmed, so the pair never moves | ||
| 1354 | +// under the pointer. | ||
| 1355 | +.sheet-foot { | ||
| 1356 | + display: flex; | ||
| 1357 | + align-items: center; | ||
| 1358 | + justify-content: space-between; | ||
| 1359 | + gap: 16px; | ||
| 1360 | +} | ||
| 1361 | + | ||
| 1362 | +.sheet-pager { | ||
| 1363 | + display: flex; | ||
| 1364 | + align-items: center; | ||
| 1365 | + gap: 4px; | ||
| 1366 | +} | ||
| 1367 | + | ||
| 1368 | +.sheet-range { | ||
| 1369 | + color: var(--text-dim); | ||
| 1370 | + font-size: 12px; | ||
| 1371 | + font-variant-numeric: tabular-nums; | ||
| 1372 | + margin-right: 4px; | ||
| 1373 | +} | ||
| 1374 | + | ||
| 1375 | +.sheet-step { | ||
| 1376 | + display: inline-flex; | ||
| 1377 | + align-items: center; | ||
| 1378 | + justify-content: center; | ||
| 1379 | + width: 26px; | ||
| 1380 | + height: 24px; | ||
| 1381 | + border: 1px solid var(--border); | ||
| 1382 | + border-radius: var(--radius-sm); | ||
| 1383 | + color: var(--text-dim); | ||
| 1384 | + | ||
| 1385 | + &:hover { color: var(--text); border-color: var(--accent); } | ||
| 1386 | +} | ||
| 1387 | + | ||
| 1388 | +// One chevron points down; the other three are that chevron turned. | ||
| 1389 | +.sheet-step-up .glyph { transform: rotate(180deg); } | ||
| 1390 | +.sheet-step-left .glyph { transform: rotate(90deg); } | ||
| 1391 | +.sheet-step-right .glyph { transform: rotate(-90deg); } | ||
| 1392 | + | ||
| 1393 | +.sheet-step-off { | ||
| 1394 | + opacity: 0.3; | ||
| 1395 | + | ||
| 1396 | + &:hover { color: var(--text-dim); border-color: var(--border); } | ||
| 1397 | +} | ||
| 1398 | + | ||
| 1348 | .new-task-form { | 1399 | .new-task-form { |
| 1349 | display: flex; | 1400 | display: flex; |
| 1350 | flex-direction: column; | 1401 | flex-direction: column; |