diff --git a/Cargo.lock b/Cargo.lock index 7662690..cae0023 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -258,6 +258,16 @@ dependencies = [ "syn 3.0.4", ] +[[package]] +name = "atoi_simd" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cdb3708a128e559a30fb830e8a77a5022ee6902806925c216658652b452a44" +dependencies = [ + "debug_unsafe", + "rustversion", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -469,6 +479,23 @@ version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" +[[package]] +name = "calamine" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fa68281b1a76b54a62156474adb06bb380a67e07dd60656e3217152b42183f3" +dependencies = [ + "atoi_simd", + "byteorder", + "codepage", + "encoding_rs", + "fast-float2", + "log", + "quick-xml", + "serde", + "zip", +] + [[package]] name = "cc" version = "1.4.4" @@ -623,6 +650,15 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e769b5c8c8283982a987c6e948e540254f1058d5a74b8794914d4ef5fc2a24" +[[package]] +name = "codepage" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48f68d061bc2828ae826206326e61251aca94c1e4a5305cf52d9138639c918b4" +dependencies = [ + "encoding_rs", +] + [[package]] name = "colorchoice" version = "1.0.5" @@ -846,6 +882,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c286de4e81ea2590afc24d754e0f83810c566f50a1388fa75ebd57928c0d9745" +[[package]] +name = "debug_unsafe" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eed2c4702fa172d1ce21078faa7c5203e69f5394d48cc436d25928394a867a2" + [[package]] name = "deranged" version = "0.5.8" @@ -988,6 +1030,12 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" +[[package]] +name = "fast-float2" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6e8948ce679d00a02a94739ea185595dca7118ed04feb991127e443bd3d761f" + [[package]] name = "fastbloom" version = "0.14.1" @@ -1025,6 +1073,7 @@ checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" dependencies = [ "crc32fast", "miniz_oxide", + "zlib-rs", ] [[package]] @@ -1753,6 +1802,7 @@ name = "izlek-web" version = "0.1.0" dependencies = [ "async-trait", + "calamine", "grass", "http", "izlek-core", @@ -2442,6 +2492,16 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "quick-xml" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e660451e55124f798a69a5af3f49ccfbefbd41910eefd25caf2393e1f3473ec1" +dependencies = [ + "encoding_rs", + "memchr", +] + [[package]] name = "quote" version = "1.0.47" @@ -4129,6 +4189,12 @@ dependencies = [ "rand 0.10.2", ] +[[package]] +name = "typed-path" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e28f89b80c87b8fb0cf04ab448d5dd0dd0ade2f8891bae878de66a75a28600e" + [[package]] name = "typeid" version = "1.0.3" @@ -4677,12 +4743,44 @@ dependencies = [ "syn 3.0.4", ] +[[package]] +name = "zip" +version = "8.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d04a6b5381502aa6087c94c669499eb1602eb9c5e8198e534de571f7154809b" +dependencies = [ + "crc32fast", + "flate2", + "indexmap", + "memchr", + "typed-path", + "zopfli", +] + +[[package]] +name = "zlib-rs" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34b31d188d9d685a4f9c7b46d6e36631b07058d2cfe190267adce54dc230bf12" + [[package]] name = "zmij" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" +[[package]] +name = "zopfli" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f05cd8797d63865425ff89b5c4a48804f35ba0ce8d125800027ad6017d2b5249" +dependencies = [ + "bumpalo", + "crc32fast", + "log", + "simd-adler32", +] + [[package]] name = "zstd" version = "0.13.3" diff --git a/crates/izlek-web/Cargo.toml b/crates/izlek-web/Cargo.toml index 3eb8271..8248807 100644 --- a/crates/izlek-web/Cargo.toml +++ b/crates/izlek-web/Cargo.toml @@ -35,6 +35,7 @@ lettre = { version = "0.11.23", default-features = false, features = [ "webpki-roots", "pool", ] } +calamine = { version = "0.36.1", default-features = false } [build-dependencies] grass = "0.13" diff --git a/crates/izlek-web/src/board.rs b/crates/izlek-web/src/board.rs index 995beed..6c7dcdc 100644 --- a/crates/izlek-web/src/board.rs +++ b/crates/izlek-web/src/board.rs @@ -499,6 +499,7 @@ struct BoardQuery { task: Option, tab: Option, file: Option, + sheet: Option, refusal: Option, on: Option, sort: Option, @@ -542,6 +543,13 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { let query = query_params::(cx)?; let open_task = query.task.clone(); let open_tab = crate::detail::Tab::from_query(query.tab.as_deref()); + // Which sheet of an open workbook is drawn. Anything that is not a number + // — a stale or hand-edited query string — is the first sheet. + let open_sheet = query + .sheet + .as_deref() + .and_then(|value| value.parse::().ok()) + .unwrap_or(0); // `?task=X&new=1` together would render both modals at once — two // document-level datepicker listeners double-stepping the month nav — so // an open task wins and `new` is ignored. @@ -596,7 +604,7 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { if let Some(task_id) = &open_task { (crate::detail::task_modal(cx, task_id, query.confirm.as_deref() == Some("delete"), open_tab).await?) if let Some(file_id) = &query.file { - (crate::detail::file_viewer_modal(cx, task_id, file_id, open_tab).await?) + (crate::detail::file_viewer_modal(cx, task_id, file_id, open_tab, open_sheet).await?) } } if open_new { diff --git a/crates/izlek-web/src/detail.rs b/crates/izlek-web/src/detail.rs index 4515055..7efcc76 100644 --- a/crates/izlek-web/src/detail.rs +++ b/crates/izlek-web/src/detail.rs @@ -2113,7 +2113,13 @@ async fn audio_player_script(cx: &Cx) -> Result { /// mime type with no viewer element: a filename's own link only ever points /// here when [`crate::files::viewer_kind`] already said yes, but a hand-edited /// query string gets the same silent no as a stale or foreign id. -pub async fn file_viewer_modal(cx: &Cx, task_id: &str, file_id: &str, tab: Tab) -> Result { +pub async fn file_viewer_modal( + cx: &Cx, + task_id: &str, + file_id: &str, + tab: Tab, + sheet_index: usize, +) -> Result { let user = match require_user(cx).await { Ok(user) => user, Err(_) => return view! { cx => }, @@ -2138,6 +2144,17 @@ pub async fn file_viewer_modal(cx: &Cx, task_id: &str, file_id: &str, tab: Tab) let src = format!("/files/{file_id}"); let download_href = format!("/files/{file_id}?dl=1"); let name = attachment.file_name.clone(); + // A workbook is the one viewer whose bytes are read here rather than + // handed to an element; every other kind streams from `/files/{id}` and + // never loads the file into the page's own request. + let sheet = if kind == crate::files::ViewerKind::Sheet { + match store.attachment_bytes(file_id).await { + Ok(Some(bytes)) => crate::sheet::read(bytes, sheet_index), + _ => None, + } + } else { + None + }; view! { cx => , crate::files::ViewerKind::Pdf => , + crate::files::ViewerKind::Sheet => (sheet_view(cx, sheet, task_id, file_id, tab, lang).await?), } @@ -2173,6 +2191,84 @@ pub async fn file_viewer_modal(cx: &Cx, task_id: &str, file_id: &str, tab: Tab) } } +/// One sheet of a workbook as a table: the tab strip when the book has more +/// than one sheet, the grid with its column letters and row numbers, and the +/// clipped line when the sheet ran past what [`crate::sheet`] draws. A +/// workbook no reader here understands says so in one line — the header's +/// download link is still the whole file. +async fn sheet_view( + cx: &Cx, + sheet: Option, + task_id: &str, + file_id: &str, + tab: Tab, + lang: Lang, +) -> Result { + let Some(sheet) = sheet else { + return view! { cx =>

(t(lang, Key::ThisFileWillNotOpen))

}; + }; + let width = sheet.rows.iter().map(Vec::len).max().unwrap_or(0); + let columns: Vec = (0..width).map(crate::sheet::column_name).collect(); + // The tab strip is a row of links, not a control: switching sheets is a + // navigation like every other overlay state on this page. + let tabs: Vec<(String, String, &'static str)> = sheet + .names + .iter() + .enumerate() + .map(|(index, name)| { + ( + name.clone(), + format!( + "/?task={task_id}&tab={}&file={file_id}&sheet={index}", + tab.slug() + ), + if index == sheet.index { + "sheet-tab sheet-tab-on" + } else { + "sheet-tab" + }, + ) + }) + .collect(); + view! { + cx => +
+ if tabs.len() > 1 { + + } +
+ + + + + for column in &columns { + + } + + + + for (index, row) in sheet.rows.iter().enumerate() { + + + for column in 0..width { + + } + + } + +
(column.clone())
(format!("{}", index + 1))(row.get(column).cloned().unwrap_or_default())
+
+ if sheet.clipped { +

(t(lang, Key::SheetClipped))

+ } +
+ } +} + /// The "New task" popup: a house modal wearing the same scrim/panel chrome /// as [`task_modal`], with a form posting straight to `/api/create_task`. /// Wired off the board top bar's "New task" button as `/?new=1`. diff --git a/crates/izlek-web/src/files.rs b/crates/izlek-web/src/files.rs index 26957ca..5ab4e60 100644 --- a/crates/izlek-web/src/files.rs +++ b/crates/izlek-web/src/files.rs @@ -81,7 +81,26 @@ pub(crate) fn sniff(bytes: &[u8]) -> &'static str { } else if bytes.starts_with(b"%PDF-") { "application/pdf" } else if bytes.starts_with(b"PK\x03\x04") { - "application/zip" + // Every OOXML and OpenDocument file is a zip; what kind it is lives in + // the entry names, which a zip stores uncompressed. `xl/workbook.xml` + // is the one part an xlsx cannot be without, and an ods declares its + // type in the `mimetype` entry the format requires be stored first. + if contains(bytes, b"xl/workbook.xml") { + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + } else if contains(bytes, b"opendocument.spreadsheet") { + "application/vnd.oasis.opendocument.spreadsheet" + } else { + "application/zip" + } + } else if bytes.starts_with(&[0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1]) { + // The pre-2007 OLE compound file .xls, .doc and .ppt all share. Its + // directory holds stream names in UTF-16, and only a workbook carries + // one called `Workbook` — a .doc or .ppt stays an unnamed container. + if contains(bytes, b"W\0o\0r\0k\0b\0o\0o\0k\0") { + "application/vnd.ms-excel" + } else { + "application/x-ole-storage" + } } else if bytes.starts_with(&[0x1F, 0x8B]) { "application/gzip" } else if bytes.len() >= 12 && &bytes[4..8] == b"ftyp" && &bytes[8..12] == b"avif" { @@ -123,6 +142,14 @@ pub(crate) fn sniff(bytes: &[u8]) -> &'static str { } } +/// Whether `haystack` holds `needle` anywhere in it. Used on zip bytes, where +/// the entry names sit in the clear even when the entries themselves do not. +fn contains(haystack: &[u8], needle: &[u8]) -> bool { + haystack + .windows(needle.len()) + .any(|window| window == needle) +} + /// The `Content-Disposition` header for one download. `inline` is only ever /// true for a mime type a browser renders on its own ([`renders_inline`]) and /// the caller did not ask for a forced download (`?dl=1`) — an uploaded HTML @@ -180,6 +207,22 @@ pub(crate) enum ViewerKind { Video, Audio, Pdf, + /// A spreadsheet Izlek reads itself and lays out as a table — no browser + /// renders one, so this is the only viewer whose bytes are parsed here + /// rather than handed to an element. + Sheet, +} + +/// The stored mime types [`crate::sheet`] opens: the two Excel workbook +/// formats and OpenDocument's. A file that sniffs as one of these still has +/// to parse before a table is drawn; [`ViewerKind::Sheet`] only says to try. +pub(crate) fn is_spreadsheet(mime_type: &str) -> bool { + matches!( + mime_type, + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + | "application/vnd.ms-excel" + | "application/vnd.oasis.opendocument.spreadsheet" + ) } pub(crate) fn viewer_kind(mime_type: &str) -> Option { @@ -193,6 +236,8 @@ pub(crate) fn viewer_kind(mime_type: &str) -> Option { Some(ViewerKind::Audio) } else if mime_type == "application/pdf" { Some(ViewerKind::Pdf) + } else if is_spreadsheet(mime_type) { + Some(ViewerKind::Sheet) } else { None } diff --git a/crates/izlek-web/src/i18n.rs b/crates/izlek-web/src/i18n.rs index f7cce6b..e2cd20e 100644 --- a/crates/izlek-web/src/i18n.rs +++ b/crates/izlek-web/src/i18n.rs @@ -66,6 +66,8 @@ pub enum Key { Play, Pause, CloseTheFile, + SheetClipped, + ThisFileWillNotOpen, Esc, CloseThisTask, Status, @@ -408,6 +410,10 @@ pub fn t(lang: Lang, key: Key) -> &'static str { (Pause, Tr) => "Duraklat", (CloseTheFile, En) => "Close the file", (CloseTheFile, Tr) => "Dosyayı kapat", + (SheetClipped, En) => "First 400 rows, 40 columns.", + (SheetClipped, Tr) => "İlk 400 satır, 40 sütun.", + (ThisFileWillNotOpen, En) => "This file will not open.", + (ThisFileWillNotOpen, Tr) => "Bu dosya açılmıyor.", (Esc, En) => "esc", (Esc, Tr) => "esc", (CloseThisTask, En) => "Close this task", diff --git a/crates/izlek-web/src/lib.rs b/crates/izlek-web/src/lib.rs index 9f3a542..376e3b4 100644 --- a/crates/izlek-web/src/lib.rs +++ b/crates/izlek-web/src/lib.rs @@ -16,4 +16,5 @@ pub mod photo; pub mod rules; pub mod server; pub mod settings; +pub mod sheet; pub mod smtp; diff --git a/crates/izlek-web/src/sheet.rs b/crates/izlek-web/src/sheet.rs new file mode 100644 index 0000000..d0ff119 --- /dev/null +++ b/crates/izlek-web/src/sheet.rs @@ -0,0 +1,150 @@ +//! Reading an uploaded spreadsheet into the rows the file viewer lays out. +//! +//! No browser renders a workbook, so unlike every other [`crate::files:: +//! ViewerKind`] this one is not a src on an element: the bytes are parsed +//! here and come out as strings the view puts in a ``. Values only — +//! a cell's formula, formatting, charts and images are not carried over. + +use std::io::Cursor; + +use calamine::{Data, Reader, open_workbook_auto_from_rs}; + +/// How much of one sheet is drawn. A workbook is a million rows wide open, +/// and a table that size is a page that never paints; past this the view says +/// so and the download is the whole file. +pub(crate) const MAX_ROWS: usize = 400; +pub(crate) const MAX_COLS: usize = 40; + +/// One sheet of a workbook, ready to lay out: every sheet's name for the tab +/// strip, which of them this is, and its cells as display strings. +pub(crate) struct Sheet { + pub names: Vec, + pub index: usize, + pub rows: Vec>, + /// True when the sheet ran past [`MAX_ROWS`] or [`MAX_COLS`] and what is + /// drawn is a corner of it. + pub clipped: bool, +} + +/// The workbook's `index`-th sheet, or `None` when the bytes are not a +/// workbook any reader here understands — a `.doc` that sniffed as an OLE +/// container, or an `.xlsx` truncated in transit. An out-of-range `index` +/// (a hand-edited query string) falls back to the first sheet rather than +/// failing. +pub(crate) fn read(bytes: Vec, index: usize) -> Option { + let mut book = open_workbook_auto_from_rs(Cursor::new(bytes)).ok()?; + let names = book.sheet_names(); + if names.is_empty() { + return None; + } + let index = if index < names.len() { index } else { 0 }; + let range = book.worksheet_range(&names[index]).ok()?; + let clipped = range.height() > MAX_ROWS || range.width() > MAX_COLS; + let rows = range + .rows() + .take(MAX_ROWS) + .map(|row| row.iter().take(MAX_COLS).map(cell).collect()) + .collect(); + Some(Sheet { + names, + index, + rows, + clipped, + }) +} + +/// One cell as the table shows it. Excel keeps a date as the number of days +/// since 1899-12-30 and a duration as a fraction of one day, so both would +/// print as a bare float without this; everything else is its own `Display`, +/// and an empty cell is an empty string rather than a placeholder. +fn cell(data: &Data) -> String { + match data { + Data::DateTime(stamp) if stamp.is_duration() => { + let total = (stamp.as_f64() * 86_400.0).round() as i64; + format!( + "{}:{:02}:{:02}", + total / 3600, + (total / 60) % 60, + total % 60 + ) + } + Data::DateTime(stamp) => { + let (year, month, day, hour, minute, second, _) = stamp.to_ymd_hms_milli(); + if (hour, minute, second) == (0, 0, 0) { + format!("{year:04}-{month:02}-{day:02}") + } else { + format!("{year:04}-{month:02}-{day:02} {hour:02}:{minute:02}") + } + } + other => other.to_string(), + } +} + +/// The spreadsheet name of a zero-based column: `A`, `Z`, `AA`, `AB`. The +/// grid draws these across the top so a cell can be named out loud without +/// guessing whether the sheet's first row is a header. +pub(crate) fn column_name(index: usize) -> String { + let mut name = Vec::new(); + let mut left = index; + loop { + name.push(b'A' + (left % 26) as u8); + if left < 26 { + break; + } + left = left / 26 - 1; + } + name.reverse(); + String::from_utf8(name).unwrap_or_default() +} + +#[cfg(test)] +mod tests { + use super::*; + use calamine::{ExcelDateTime, ExcelDateTimeType}; + + #[test] + fn empty_cell_is_empty_string() { + assert_eq!(cell(&Data::Empty), ""); + } + + #[test] + fn a_whole_number_keeps_no_decimal_point() { + assert_eq!(cell(&Data::Float(1.0)), "1"); + assert_eq!(cell(&Data::Int(42)), "42"); + } + + #[test] + fn a_date_reads_as_a_date_not_a_serial_number() { + let stamp = ExcelDateTime::new(45943.0, ExcelDateTimeType::DateTime, false); + assert_eq!(cell(&Data::DateTime(stamp)), "2025-10-13"); + } + + #[test] + fn a_time_of_day_rides_along_with_the_date() { + let stamp = ExcelDateTime::new(45943.5, ExcelDateTimeType::DateTime, false); + assert_eq!(cell(&Data::DateTime(stamp)), "2025-10-13 12:00"); + } + + #[test] + fn a_duration_reads_as_a_clock() { + let stamp = ExcelDateTime::new(0.5, ExcelDateTimeType::TimeDelta, false); + assert_eq!(cell(&Data::DateTime(stamp)), "12:00:00"); + } + + #[test] + fn columns_carry_past_z() { + assert_eq!(column_name(0), "A"); + assert_eq!(column_name(25), "Z"); + assert_eq!(column_name(26), "AA"); + assert_eq!(column_name(27), "AB"); + assert_eq!(column_name(51), "AZ"); + assert_eq!(column_name(52), "BA"); + assert_eq!(column_name(701), "ZZ"); + assert_eq!(column_name(702), "AAA"); + } + + #[test] + fn bytes_that_are_not_a_workbook_have_no_sheet() { + assert!(read(b"not a workbook at all".to_vec(), 0).is_none()); + } +} diff --git a/crates/izlek-web/tests/fixtures/book.xlsx b/crates/izlek-web/tests/fixtures/book.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..9fd3b0671eb3a33ce1e1ff8c2a3f0b8baccdb9cf GIT binary patch literal 1971 zcmZ`)dpOg382|o^l}vIeS~zD)E?r!v|xS|h7;A@|6oM%hGl*vw2)iX@dv=a@@1 zAyJadT+3at&T=VqkVx2KI?h9nbF%95$M5(3JdFq(FfTzw8vfL?Mzi~FWUNKFCvR}!Q)eR*Njls?JiTr@w^jeH{18n?d6F*Rjq|> zde>qGZP)2o%1W-iI|13;OWqe(kGhqGf+sqMveFM6t_)GZA+*v_%w#e{gE`i=_NY1U zkLwPZ?QI6j(|27u7~{5aI5#?6doV(Kn^wYET!HgV`qsT{6sucJsnPpX0CwkXG{+lp z(AMmxw_!!v2b=O^W1G5v7rr>yP>P#CJi=I2IP+0FGxe{Xe0ph&Z+Cq4PG_De8d1Opu&;X~!ou;sV*=PSy1L003WJ2G&)iCM?c0l4NLXwn zD~ZzN{F>gpohzMPrGG5Ga2xkTTW#^nhAbgTbZd+Xn_9Hn)fP5sIBY2^WQ_1I_MLBH zJ2q%sQdV}5Ax71sSHL#)yEnYQbb=IsNE7X_kQ7iCa+9+}Q0o8ycnAPF06^Fgz0jcG z!(Kr_hiBX2+yzbmSAUuH$d@H~Lgzv`mdf)-Nmgm&FCu6GY?8cx`;cz7NXWv&2H2UA zZ${x6T_n>hif3%sF0$Vfg+rQJUsWr4y1C1O-uzbQa|L5*DbdsT^~BBfF>>#sUIlA} z$mjhYW(C|4Nj7N*{+j7mX3Cb6G2vPe2tczCf)2Y zVYC$iW;*_&wM?}WbEv3mmyxnAlCgneFT>Gy1Eb>|s2a$0fIrtFah%p^Nj04?gbJiLhxuq9f6Hd0@sVV zPSOngjkyh)bw>Eh?a%D}E+cE~supMghz8CLf@%<^H0~IeQy_9EL93p}RXzNVVKGY5Ui3G~yl+?=G z+BwbsJ-FCDL6*}xd2Wi0w5E1*9oJQQB|*Ov>2OaKCw(!=W1_BCGGD=Dgg4zkA{>`P zjk<5H($9%}iQNAI8joSID_)sp#H zh3c4WiPCxm01$wms$)2-j(t3C))+Rz&Cx00nByy0NzDw)0jU;s%bM2xj#mv5vpV>f z(}~5R=|&O8?1ScGz3dHFPNp#BQ~e4|2VQbT{P%C9UEbsVzF=R;Eiy70g^~7J@HJXG z-12{uBaFO9d&R{iHNg+q2oo*tCO7dtCeD!93h&Gv4b`hh%g_cis@sBHlIgph2spewbyhhlm6z`0Sr>00(gp019(o{{pbR6IlQN literal 0 HcmV?d00001 diff --git a/crates/izlek-web/tests/http.rs b/crates/izlek-web/tests/http.rs index f417924..7be8500 100644 --- a/crates/izlek-web/tests/http.rs +++ b/crates/izlek-web/tests/http.rs @@ -6272,3 +6272,74 @@ async fn a_member_who_may_only_read_still_gets_the_tab_strip() { "no tab strip for a member: {html}" ); } + +/// A spreadsheet has no browser element behind it: Izlek reads the workbook +/// itself and lays the sheet out as a table, tab strip and all. The upload is +/// a real xlsx, so this covers the sniffer, the reader and the view together. +#[tokio::test] +async fn a_workbook_opens_as_a_table_and_its_other_sheet_is_one_link_away() { + let app = App::open().await; + let admin_cookie = admin(&app).await; + let column = first_column(&app).await; + let task = a_task(&app, &admin_cookie, &column, "Read the book").await; + + let book = include_bytes!("fixtures/book.xlsx"); + app.post_multipart( + "/files", + Some(&admin_cookie), + &[("task_id", &task)], + Some(( + "costs.xlsx", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + book, + )), + ) + .await; + let snapshot = app + .post( + "/api/fetch_task", + Some(&admin_cookie), + &[("task_id", &task)], + ) + .await; + let file_id = attachment_id_named(&snapshot.body, "costs.xlsx"); + + let page = app + .get( + &format!("/?task={task}&file={file_id}"), + Some(&admin_cookie), + ) + .await; + assert_eq!(page.status, StatusCode::OK); + let html = String::from_utf8_lossy(&page.bytes); + assert!(html.contains("sheet-table"), "no sheet table: {html}"); + assert!(html.contains("Cable"), "a cell's text is missing: {html}"); + assert!(html.contains("1250"), "a number is missing: {html}"); + assert!(html.contains("Ledger"), "the sheet tab is missing: {html}"); + assert!( + html.contains(&format!("file={file_id}&sheet=1")), + "no link to the second sheet: {html}" + ); + + // The tab strip is navigation, so the second sheet is a page of its own. + let second = app + .get( + &format!("/?task={task}&file={file_id}&sheet=1"), + Some(&admin_cookie), + ) + .await; + let second_html = String::from_utf8_lossy(&second.bytes); + assert!( + second_html.contains("Second sheet cell"), + "the second sheet did not render: {second_html}" + ); + + // A hand-edited sheet index is the first sheet, not an error. + let past_the_end = app + .get( + &format!("/?task={task}&file={file_id}&sheet=99"), + Some(&admin_cookie), + ) + .await; + assert!(String::from_utf8_lossy(&past_the_end.bytes).contains("Cable")); +} diff --git a/style/main.scss b/style/main.scss index 0bef26d..6c3f469 100644 --- a/style/main.scss +++ b/style/main.scss @@ -1229,6 +1229,96 @@ img.avatar { .viewer-pdf { width: 100%; height: 78vh; border: 1px solid var(--border); border-radius: var(--radius-md); } +// A workbook has no element of its own, so the viewer draws it: a tab strip +// when the book has more than one sheet, and a grid that pans inside its own +// box — the document never scrolls. +.viewer-sheet { + display: flex; + flex-direction: column; + gap: 10px; + width: min(86vw, 900px); +} + +.sheet-tabs { + display: flex; + gap: 6px; + overflow-x: auto; + scrollbar-width: none; + + &::-webkit-scrollbar { display: none; } +} + +.sheet-tab { + padding: 4px 10px; + border: 1px solid var(--border); + border-radius: var(--radius-sm); + color: var(--text-dim); + font-size: 12px; + white-space: nowrap; + text-decoration: none; +} + +.sheet-tab-on { + color: var(--text); + border-color: var(--accent); +} + +.sheet-grid { + max-height: 70vh; + overflow: auto; + scrollbar-width: none; + border: 1px solid var(--border); + border-radius: var(--radius-md); + + &::-webkit-scrollbar { display: none; } +} + +.sheet-table { + border-collapse: collapse; + font-size: 12px; + font-variant-numeric: tabular-nums; + + th, + td { + padding: 4px 8px; + border-right: 1px solid var(--border); + border-bottom: 1px solid var(--border); + max-width: 220px; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + text-align: left; + } + + // The column letters and row numbers are chrome, not data: dim, sticky at + // the edges they name, so a cell keeps its address while the grid pans. + thead th { + position: sticky; + top: 0; + z-index: 1; + background: var(--surface); + color: var(--text-dim); + font-weight: 500; + } + + .sheet-gutter { + position: sticky; + left: 0; + background: var(--surface); + color: var(--text-dim); + font-weight: 500; + text-align: right; + } + + thead .sheet-gutter { z-index: 2; } +} + +.sheet-note { + margin: 0; + color: var(--text-dim); + font-size: 12px; +} + .new-task-form { display: flex; flex-direction: column;