A spreadsheet opens in the viewer as a table
Browse files at this commit4471b603f1c6e03682c04aa698b8df6fedeb37baParent: eafca5c- feat
- a spreadsheet opens in the viewer as a table
11 changed files +569 -3
- Cargo.lock +98 -0
- crates/izlek-web/Cargo.toml +1 -0
- crates/izlek-web/src/board.rs +9 -1
- crates/izlek-web/src/detail.rs +97 -1
- crates/izlek-web/src/files.rs +46 -1
- crates/izlek-web/src/i18n.rs +6 -0
- crates/izlek-web/src/lib.rs +1 -0
- crates/izlek-web/src/sheet.rs +150 -0
- crates/izlek-web/tests/fixtures/book.xlsx +0 -0
- crates/izlek-web/tests/http.rs +71 -0
- style/main.scss +90 -0
Cargo.lockmodified+98 -0
| @@ -258,6 +258,16 @@ dependencies = [ | ||
| 258 | 258 | "syn 3.0.4", |
| 259 | 259 | ] |
| 260 | 260 | |
| 261 | +[[package]] | |
| 262 | +name = "atoi_simd" | |
| 263 | +version = "0.18.1" | |
| 264 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 265 | +checksum = "f3cdb3708a128e559a30fb830e8a77a5022ee6902806925c216658652b452a44" | |
| 266 | +dependencies = [ | |
| 267 | + "debug_unsafe", | |
| 268 | + "rustversion", | |
| 269 | +] | |
| 270 | + | |
| 261 | 271 | [[package]] |
| 262 | 272 | name = "atomic-waker" |
| 263 | 273 | version = "1.1.2" |
| @@ -469,6 +479,23 @@ version = "1.12.1" | ||
| 469 | 479 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 470 | 480 | checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" |
| 471 | 481 | |
| 482 | +[[package]] | |
| 483 | +name = "calamine" | |
| 484 | +version = "0.36.1" | |
| 485 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 486 | +checksum = "5fa68281b1a76b54a62156474adb06bb380a67e07dd60656e3217152b42183f3" | |
| 487 | +dependencies = [ | |
| 488 | + "atoi_simd", | |
| 489 | + "byteorder", | |
| 490 | + "codepage", | |
| 491 | + "encoding_rs", | |
| 492 | + "fast-float2", | |
| 493 | + "log", | |
| 494 | + "quick-xml", | |
| 495 | + "serde", | |
| 496 | + "zip", | |
| 497 | +] | |
| 498 | + | |
| 472 | 499 | [[package]] |
| 473 | 500 | name = "cc" |
| 474 | 501 | version = "1.4.4" |
| @@ -623,6 +650,15 @@ version = "0.1.3" | ||
| 623 | 650 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 624 | 651 | checksum = "b9e769b5c8c8283982a987c6e948e540254f1058d5a74b8794914d4ef5fc2a24" |
| 625 | 652 | |
| 653 | +[[package]] | |
| 654 | +name = "codepage" | |
| 655 | +version = "0.1.2" | |
| 656 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 657 | +checksum = "48f68d061bc2828ae826206326e61251aca94c1e4a5305cf52d9138639c918b4" | |
| 658 | +dependencies = [ | |
| 659 | + "encoding_rs", | |
| 660 | +] | |
| 661 | + | |
| 626 | 662 | [[package]] |
| 627 | 663 | name = "colorchoice" |
| 628 | 664 | version = "1.0.5" |
| @@ -846,6 +882,12 @@ version = "0.2.0" | ||
| 846 | 882 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 847 | 883 | checksum = "c286de4e81ea2590afc24d754e0f83810c566f50a1388fa75ebd57928c0d9745" |
| 848 | 884 | |
| 885 | +[[package]] | |
| 886 | +name = "debug_unsafe" | |
| 887 | +version = "0.1.4" | |
| 888 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 889 | +checksum = "7eed2c4702fa172d1ce21078faa7c5203e69f5394d48cc436d25928394a867a2" | |
| 890 | + | |
| 849 | 891 | [[package]] |
| 850 | 892 | name = "deranged" |
| 851 | 893 | version = "0.5.8" |
| @@ -988,6 +1030,12 @@ version = "0.3.0" | ||
| 988 | 1030 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 989 | 1031 | checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" |
| 990 | 1032 | |
| 1033 | +[[package]] | |
| 1034 | +name = "fast-float2" | |
| 1035 | +version = "0.2.4" | |
| 1036 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1037 | +checksum = "c6e8948ce679d00a02a94739ea185595dca7118ed04feb991127e443bd3d761f" | |
| 1038 | + | |
| 991 | 1039 | [[package]] |
| 992 | 1040 | name = "fastbloom" |
| 993 | 1041 | version = "0.14.1" |
| @@ -1025,6 +1073,7 @@ checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" | ||
| 1025 | 1073 | dependencies = [ |
| 1026 | 1074 | "crc32fast", |
| 1027 | 1075 | "miniz_oxide", |
| 1076 | + "zlib-rs", | |
| 1028 | 1077 | ] |
| 1029 | 1078 | |
| 1030 | 1079 | [[package]] |
| @@ -1753,6 +1802,7 @@ name = "izlek-web" | ||
| 1753 | 1802 | version = "0.1.0" |
| 1754 | 1803 | dependencies = [ |
| 1755 | 1804 | "async-trait", |
| 1805 | + "calamine", | |
| 1756 | 1806 | "grass", |
| 1757 | 1807 | "http", |
| 1758 | 1808 | "izlek-core", |
| @@ -2442,6 +2492,16 @@ dependencies = [ | ||
| 2442 | 2492 | "syn 2.0.119", |
| 2443 | 2493 | ] |
| 2444 | 2494 | |
| 2495 | +[[package]] | |
| 2496 | +name = "quick-xml" | |
| 2497 | +version = "0.41.0" | |
| 2498 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 2499 | +checksum = "e660451e55124f798a69a5af3f49ccfbefbd41910eefd25caf2393e1f3473ec1" | |
| 2500 | +dependencies = [ | |
| 2501 | + "encoding_rs", | |
| 2502 | + "memchr", | |
| 2503 | +] | |
| 2504 | + | |
| 2445 | 2505 | [[package]] |
| 2446 | 2506 | name = "quote" |
| 2447 | 2507 | version = "1.0.47" |
| @@ -4129,6 +4189,12 @@ dependencies = [ | ||
| 4129 | 4189 | "rand 0.10.2", |
| 4130 | 4190 | ] |
| 4131 | 4191 | |
| 4192 | +[[package]] | |
| 4193 | +name = "typed-path" | |
| 4194 | +version = "0.12.3" | |
| 4195 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4196 | +checksum = "8e28f89b80c87b8fb0cf04ab448d5dd0dd0ade2f8891bae878de66a75a28600e" | |
| 4197 | + | |
| 4132 | 4198 | [[package]] |
| 4133 | 4199 | name = "typeid" |
| 4134 | 4200 | version = "1.0.3" |
| @@ -4677,12 +4743,44 @@ dependencies = [ | ||
| 4677 | 4743 | "syn 3.0.4", |
| 4678 | 4744 | ] |
| 4679 | 4745 | |
| 4746 | +[[package]] | |
| 4747 | +name = "zip" | |
| 4748 | +version = "8.6.0" | |
| 4749 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4750 | +checksum = "2d04a6b5381502aa6087c94c669499eb1602eb9c5e8198e534de571f7154809b" | |
| 4751 | +dependencies = [ | |
| 4752 | + "crc32fast", | |
| 4753 | + "flate2", | |
| 4754 | + "indexmap", | |
| 4755 | + "memchr", | |
| 4756 | + "typed-path", | |
| 4757 | + "zopfli", | |
| 4758 | +] | |
| 4759 | + | |
| 4760 | +[[package]] | |
| 4761 | +name = "zlib-rs" | |
| 4762 | +version = "0.6.7" | |
| 4763 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4764 | +checksum = "34b31d188d9d685a4f9c7b46d6e36631b07058d2cfe190267adce54dc230bf12" | |
| 4765 | + | |
| 4680 | 4766 | [[package]] |
| 4681 | 4767 | name = "zmij" |
| 4682 | 4768 | version = "1.0.23" |
| 4683 | 4769 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 4684 | 4770 | checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" |
| 4685 | 4771 | |
| 4772 | +[[package]] | |
| 4773 | +name = "zopfli" | |
| 4774 | +version = "0.8.3" | |
| 4775 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4776 | +checksum = "f05cd8797d63865425ff89b5c4a48804f35ba0ce8d125800027ad6017d2b5249" | |
| 4777 | +dependencies = [ | |
| 4778 | + "bumpalo", | |
| 4779 | + "crc32fast", | |
| 4780 | + "log", | |
| 4781 | + "simd-adler32", | |
| 4782 | +] | |
| 4783 | + | |
| 4686 | 4784 | [[package]] |
| 4687 | 4785 | name = "zstd" |
| 4688 | 4786 | version = "0.13.3" |
crates/izlek-web/Cargo.tomlmodified+1 -0
| @@ -35,6 +35,7 @@ lettre = { version = "0.11.23", default-features = false, features = [ | ||
| 35 | 35 | "webpki-roots", |
| 36 | 36 | "pool", |
| 37 | 37 | ] } |
| 38 | +calamine = { version = "0.36.1", default-features = false } | |
| 38 | 39 | |
| 39 | 40 | [build-dependencies] |
| 40 | 41 | grass = "0.13" |
crates/izlek-web/src/board.rsmodified+9 -1
| @@ -499,6 +499,7 @@ struct BoardQuery { | ||
| 499 | 499 | task: Option<String>, |
| 500 | 500 | tab: Option<String>, |
| 501 | 501 | file: Option<String>, |
| 502 | + sheet: Option<String>, | |
| 502 | 503 | refusal: Option<String>, |
| 503 | 504 | on: Option<String>, |
| 504 | 505 | sort: Option<String>, |
| @@ -542,6 +543,13 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 542 | 543 | let query = query_params::<BoardQuery>(cx)?; |
| 543 | 544 | let open_task = query.task.clone(); |
| 544 | 545 | let open_tab = crate::detail::Tab::from_query(query.tab.as_deref()); |
| 546 | + // Which sheet of an open workbook is drawn. Anything that is not a number | |
| 547 | + // — a stale or hand-edited query string — is the first sheet. | |
| 548 | + let open_sheet = query | |
| 549 | + .sheet | |
| 550 | + .as_deref() | |
| 551 | + .and_then(|value| value.parse::<usize>().ok()) | |
| 552 | + .unwrap_or(0); | |
| 545 | 553 | // `?task=X&new=1` together would render both modals at once — two |
| 546 | 554 | // document-level datepicker listeners double-stepping the month nav — so |
| 547 | 555 | // an open task wins and `new` is ignored. |
| @@ -596,7 +604,7 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 596 | 604 | if let Some(task_id) = &open_task { |
| 597 | 605 | (crate::detail::task_modal(cx, task_id, query.confirm.as_deref() == Some("delete"), open_tab).await?) |
| 598 | 606 | if let Some(file_id) = &query.file { |
| 599 | - (crate::detail::file_viewer_modal(cx, task_id, file_id, open_tab).await?) | |
| 607 | + (crate::detail::file_viewer_modal(cx, task_id, file_id, open_tab, open_sheet).await?) | |
| 600 | 608 | } |
| 601 | 609 | } |
| 602 | 610 | if open_new { |
crates/izlek-web/src/detail.rsmodified+97 -1
| @@ -2113,7 +2113,13 @@ async fn audio_player_script(cx: &Cx) -> Result { | ||
| 2113 | 2113 | /// mime type with no viewer element: a filename's own link only ever points |
| 2114 | 2114 | /// here when [`crate::files::viewer_kind`] already said yes, but a hand-edited |
| 2115 | 2115 | /// query string gets the same silent no as a stale or foreign id. |
| 2116 | -pub async fn file_viewer_modal(cx: &Cx, task_id: &str, file_id: &str, tab: Tab) -> Result { | |
| 2116 | +pub async fn file_viewer_modal( | |
| 2117 | + cx: &Cx, | |
| 2118 | + task_id: &str, | |
| 2119 | + file_id: &str, | |
| 2120 | + tab: Tab, | |
| 2121 | + sheet_index: usize, | |
| 2122 | +) -> Result { | |
| 2117 | 2123 | let user = match require_user(cx).await { |
| 2118 | 2124 | Ok(user) => user, |
| 2119 | 2125 | Err(_) => return view! { cx => }, |
| @@ -2138,6 +2144,17 @@ pub async fn file_viewer_modal(cx: &Cx, task_id: &str, file_id: &str, tab: Tab) | ||
| 2138 | 2144 | let src = format!("/files/{file_id}"); |
| 2139 | 2145 | let download_href = format!("/files/{file_id}?dl=1"); |
| 2140 | 2146 | let name = attachment.file_name.clone(); |
| 2147 | + // A workbook is the one viewer whose bytes are read here rather than | |
| 2148 | + // handed to an element; every other kind streams from `/files/{id}` and | |
| 2149 | + // never loads the file into the page's own request. | |
| 2150 | + let sheet = if kind == crate::files::ViewerKind::Sheet { | |
| 2151 | + match store.attachment_bytes(file_id).await { | |
| 2152 | + Ok(Some(bytes)) => crate::sheet::read(bytes, sheet_index), | |
| 2153 | + _ => None, | |
| 2154 | + } | |
| 2155 | + } else { | |
| 2156 | + None | |
| 2157 | + }; | |
| 2141 | 2158 | view! { |
| 2142 | 2159 | cx => |
| 2143 | 2160 | <div class="modal-scrim viewer-scrim"> |
| @@ -2166,6 +2183,7 @@ pub async fn file_viewer_modal(cx: &Cx, task_id: &str, file_id: &str, tab: Tab) | ||
| 2166 | 2183 | (audio_player_script(cx).await?) |
| 2167 | 2184 | </div>, |
| 2168 | 2185 | crate::files::ViewerKind::Pdf => <object class="viewer-media viewer-pdf" data=(src) type="application/pdf"></object>, |
| 2186 | + crate::files::ViewerKind::Sheet => (sheet_view(cx, sheet, task_id, file_id, tab, lang).await?), | |
| 2169 | 2187 | } |
| 2170 | 2188 | </div> |
| 2171 | 2189 | </div> |
| @@ -2173,6 +2191,84 @@ pub async fn file_viewer_modal(cx: &Cx, task_id: &str, file_id: &str, tab: Tab) | ||
| 2173 | 2191 | } |
| 2174 | 2192 | } |
| 2175 | 2193 | |
| 2194 | +/// One sheet of a workbook as a table: the tab strip when the book has more | |
| 2195 | +/// than one sheet, the grid with its column letters and row numbers, and the | |
| 2196 | +/// clipped line when the sheet ran past what [`crate::sheet`] draws. A | |
| 2197 | +/// workbook no reader here understands says so in one line — the header's | |
| 2198 | +/// download link is still the whole file. | |
| 2199 | +async fn sheet_view( | |
| 2200 | + cx: &Cx, | |
| 2201 | + sheet: Option<crate::sheet::Sheet>, | |
| 2202 | + task_id: &str, | |
| 2203 | + file_id: &str, | |
| 2204 | + tab: Tab, | |
| 2205 | + lang: Lang, | |
| 2206 | +) -> Result { | |
| 2207 | + let Some(sheet) = sheet else { | |
| 2208 | + return view! { cx => <p class="sheet-note">(t(lang, Key::ThisFileWillNotOpen))</p> }; | |
| 2209 | + }; | |
| 2210 | + let width = sheet.rows.iter().map(Vec::len).max().unwrap_or(0); | |
| 2211 | + let columns: Vec<String> = (0..width).map(crate::sheet::column_name).collect(); | |
| 2212 | + // The tab strip is a row of links, not a control: switching sheets is a | |
| 2213 | + // navigation like every other overlay state on this page. | |
| 2214 | + let tabs: Vec<(String, String, &'static str)> = sheet | |
| 2215 | + .names | |
| 2216 | + .iter() | |
| 2217 | + .enumerate() | |
| 2218 | + .map(|(index, name)| { | |
| 2219 | + ( | |
| 2220 | + name.clone(), | |
| 2221 | + format!( | |
| 2222 | + "/?task={task_id}&tab={}&file={file_id}&sheet={index}", | |
| 2223 | + tab.slug() | |
| 2224 | + ), | |
| 2225 | + if index == sheet.index { | |
| 2226 | + "sheet-tab sheet-tab-on" | |
| 2227 | + } else { | |
| 2228 | + "sheet-tab" | |
| 2229 | + }, | |
| 2230 | + ) | |
| 2231 | + }) | |
| 2232 | + .collect(); | |
| 2233 | + view! { | |
| 2234 | + cx => | |
| 2235 | + <div class="viewer-sheet"> | |
| 2236 | + if tabs.len() > 1 { | |
| 2237 | + <nav class="sheet-tabs"> | |
| 2238 | + for tab in &tabs { | |
| 2239 | + <a class=(tab.2) href=(tab.1.clone())>(tab.0.clone())</a> | |
| 2240 | + } | |
| 2241 | + </nav> | |
| 2242 | + } | |
| 2243 | + <div class="sheet-grid"> | |
| 2244 | + <table class="sheet-table"> | |
| 2245 | + <thead> | |
| 2246 | + <tr> | |
| 2247 | + <th class="sheet-gutter"></th> | |
| 2248 | + for column in &columns { | |
| 2249 | + <th>(column.clone())</th> | |
| 2250 | + } | |
| 2251 | + </tr> | |
| 2252 | + </thead> | |
| 2253 | + <tbody> | |
| 2254 | + for (index, row) in sheet.rows.iter().enumerate() { | |
| 2255 | + <tr> | |
| 2256 | + <th class="sheet-gutter">(format!("{}", index + 1))</th> | |
| 2257 | + for column in 0..width { | |
| 2258 | + <td>(row.get(column).cloned().unwrap_or_default())</td> | |
| 2259 | + } | |
| 2260 | + </tr> | |
| 2261 | + } | |
| 2262 | + </tbody> | |
| 2263 | + </table> | |
| 2264 | + </div> | |
| 2265 | + if sheet.clipped { | |
| 2266 | + <p class="sheet-note">(t(lang, Key::SheetClipped))</p> | |
| 2267 | + } | |
| 2268 | + </div> | |
| 2269 | + } | |
| 2270 | +} | |
| 2271 | + | |
| 2176 | 2272 | /// The "New task" popup: a house modal wearing the same scrim/panel chrome |
| 2177 | 2273 | /// as [`task_modal`], with a form posting straight to `/api/create_task`. |
| 2178 | 2274 | /// Wired off the board top bar's "New task" button as `/?new=1`. |
crates/izlek-web/src/files.rsmodified+46 -1
| @@ -81,7 +81,26 @@ pub(crate) fn sniff(bytes: &[u8]) -> &'static str { | ||
| 81 | 81 | } else if bytes.starts_with(b"%PDF-") { |
| 82 | 82 | "application/pdf" |
| 83 | 83 | } else if bytes.starts_with(b"PK\x03\x04") { |
| 84 | - "application/zip" | |
| 84 | + // Every OOXML and OpenDocument file is a zip; what kind it is lives in | |
| 85 | + // the entry names, which a zip stores uncompressed. `xl/workbook.xml` | |
| 86 | + // is the one part an xlsx cannot be without, and an ods declares its | |
| 87 | + // type in the `mimetype` entry the format requires be stored first. | |
| 88 | + if contains(bytes, b"xl/workbook.xml") { | |
| 89 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | |
| 90 | + } else if contains(bytes, b"opendocument.spreadsheet") { | |
| 91 | + "application/vnd.oasis.opendocument.spreadsheet" | |
| 92 | + } else { | |
| 93 | + "application/zip" | |
| 94 | + } | |
| 95 | + } else if bytes.starts_with(&[0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1]) { | |
| 96 | + // The pre-2007 OLE compound file .xls, .doc and .ppt all share. Its | |
| 97 | + // directory holds stream names in UTF-16, and only a workbook carries | |
| 98 | + // one called `Workbook` — a .doc or .ppt stays an unnamed container. | |
| 99 | + if contains(bytes, b"W\0o\0r\0k\0b\0o\0o\0k\0") { | |
| 100 | + "application/vnd.ms-excel" | |
| 101 | + } else { | |
| 102 | + "application/x-ole-storage" | |
| 103 | + } | |
| 85 | 104 | } else if bytes.starts_with(&[0x1F, 0x8B]) { |
| 86 | 105 | "application/gzip" |
| 87 | 106 | } 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 { | ||
| 123 | 142 | } |
| 124 | 143 | } |
| 125 | 144 | |
| 145 | +/// Whether `haystack` holds `needle` anywhere in it. Used on zip bytes, where | |
| 146 | +/// the entry names sit in the clear even when the entries themselves do not. | |
| 147 | +fn contains(haystack: &[u8], needle: &[u8]) -> bool { | |
| 148 | + haystack | |
| 149 | + .windows(needle.len()) | |
| 150 | + .any(|window| window == needle) | |
| 151 | +} | |
| 152 | + | |
| 126 | 153 | /// The `Content-Disposition` header for one download. `inline` is only ever |
| 127 | 154 | /// true for a mime type a browser renders on its own ([`renders_inline`]) and |
| 128 | 155 | /// the caller did not ask for a forced download (`?dl=1`) — an uploaded HTML |
| @@ -180,6 +207,22 @@ pub(crate) enum ViewerKind { | ||
| 180 | 207 | Video, |
| 181 | 208 | Audio, |
| 182 | 209 | Pdf, |
| 210 | + /// A spreadsheet Izlek reads itself and lays out as a table — no browser | |
| 211 | + /// renders one, so this is the only viewer whose bytes are parsed here | |
| 212 | + /// rather than handed to an element. | |
| 213 | + Sheet, | |
| 214 | +} | |
| 215 | + | |
| 216 | +/// The stored mime types [`crate::sheet`] opens: the two Excel workbook | |
| 217 | +/// formats and OpenDocument's. A file that sniffs as one of these still has | |
| 218 | +/// to parse before a table is drawn; [`ViewerKind::Sheet`] only says to try. | |
| 219 | +pub(crate) fn is_spreadsheet(mime_type: &str) -> bool { | |
| 220 | + matches!( | |
| 221 | + mime_type, | |
| 222 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | |
| 223 | + | "application/vnd.ms-excel" | |
| 224 | + | "application/vnd.oasis.opendocument.spreadsheet" | |
| 225 | + ) | |
| 183 | 226 | } |
| 184 | 227 | |
| 185 | 228 | pub(crate) fn viewer_kind(mime_type: &str) -> Option<ViewerKind> { |
| @@ -193,6 +236,8 @@ pub(crate) fn viewer_kind(mime_type: &str) -> Option<ViewerKind> { | ||
| 193 | 236 | Some(ViewerKind::Audio) |
| 194 | 237 | } else if mime_type == "application/pdf" { |
| 195 | 238 | Some(ViewerKind::Pdf) |
| 239 | + } else if is_spreadsheet(mime_type) { | |
| 240 | + Some(ViewerKind::Sheet) | |
| 196 | 241 | } else { |
| 197 | 242 | None |
| 198 | 243 | } |
crates/izlek-web/src/i18n.rsmodified+6 -0
| @@ -66,6 +66,8 @@ pub enum Key { | ||
| 66 | 66 | Play, |
| 67 | 67 | Pause, |
| 68 | 68 | CloseTheFile, |
| 69 | + SheetClipped, | |
| 70 | + ThisFileWillNotOpen, | |
| 69 | 71 | Esc, |
| 70 | 72 | CloseThisTask, |
| 71 | 73 | Status, |
| @@ -408,6 +410,10 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | ||
| 408 | 410 | (Pause, Tr) => "Duraklat", |
| 409 | 411 | (CloseTheFile, En) => "Close the file", |
| 410 | 412 | (CloseTheFile, Tr) => "Dosyayı kapat", |
| 413 | + (SheetClipped, En) => "First 400 rows, 40 columns.", | |
| 414 | + (SheetClipped, Tr) => "İlk 400 satır, 40 sütun.", | |
| 415 | + (ThisFileWillNotOpen, En) => "This file will not open.", | |
| 416 | + (ThisFileWillNotOpen, Tr) => "Bu dosya açılmıyor.", | |
| 411 | 417 | (Esc, En) => "esc", |
| 412 | 418 | (Esc, Tr) => "esc", |
| 413 | 419 | (CloseThisTask, En) => "Close this task", |
crates/izlek-web/src/lib.rsmodified+1 -0
| @@ -16,4 +16,5 @@ pub mod photo; | ||
| 16 | 16 | pub mod rules; |
| 17 | 17 | pub mod server; |
| 18 | 18 | pub mod settings; |
| 19 | +pub mod sheet; | |
| 19 | 20 | pub mod smtp; |
crates/izlek-web/src/sheet.rsadded+150 -0
| @@ -0,0 +1,150 @@ | ||
| 1 | +//! Reading an uploaded spreadsheet into the rows the file viewer lays out. | |
| 2 | +//! | |
| 3 | +//! No browser renders a workbook, so unlike every other [`crate::files:: | |
| 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 — | |
| 6 | +//! a cell's formula, formatting, charts and images are not carried over. | |
| 7 | + | |
| 8 | +use std::io::Cursor; | |
| 9 | + | |
| 10 | +use calamine::{Data, Reader, open_workbook_auto_from_rs}; | |
| 11 | + | |
| 12 | +/// How much of one sheet is drawn. A workbook is a million rows wide open, | |
| 13 | +/// and a table that size is a page that never paints; past this the view says | |
| 14 | +/// so and the download is the whole file. | |
| 15 | +pub(crate) const MAX_ROWS: usize = 400; | |
| 16 | +pub(crate) const MAX_COLS: usize = 40; | |
| 17 | + | |
| 18 | +/// One sheet of a workbook, ready to lay out: every sheet's name for the tab | |
| 19 | +/// strip, which of them this is, and its cells as display strings. | |
| 20 | +pub(crate) struct Sheet { | |
| 21 | + pub names: Vec<String>, | |
| 22 | + pub index: usize, | |
| 23 | + pub rows: Vec<Vec<String>>, | |
| 24 | + /// True when the sheet ran past [`MAX_ROWS`] or [`MAX_COLS`] and what is | |
| 25 | + /// drawn is a corner of it. | |
| 26 | + pub clipped: bool, | |
| 27 | +} | |
| 28 | + | |
| 29 | +/// The workbook's `index`-th sheet, or `None` when the bytes are not a | |
| 30 | +/// workbook any reader here understands — a `.doc` that sniffed as an OLE | |
| 31 | +/// container, or an `.xlsx` truncated in transit. An out-of-range `index` | |
| 32 | +/// (a hand-edited query string) falls back to the first sheet rather than | |
| 33 | +/// failing. | |
| 34 | +pub(crate) fn read(bytes: Vec<u8>, index: usize) -> Option<Sheet> { | |
| 35 | + let mut book = open_workbook_auto_from_rs(Cursor::new(bytes)).ok()?; | |
| 36 | + let names = book.sheet_names(); | |
| 37 | + if names.is_empty() { | |
| 38 | + return None; | |
| 39 | + } | |
| 40 | + let index = if index < names.len() { index } else { 0 }; | |
| 41 | + let range = book.worksheet_range(&names[index]).ok()?; | |
| 42 | + let clipped = range.height() > MAX_ROWS || range.width() > MAX_COLS; | |
| 43 | + let rows = range | |
| 44 | + .rows() | |
| 45 | + .take(MAX_ROWS) | |
| 46 | + .map(|row| row.iter().take(MAX_COLS).map(cell).collect()) | |
| 47 | + .collect(); | |
| 48 | + Some(Sheet { | |
| 49 | + names, | |
| 50 | + index, | |
| 51 | + rows, | |
| 52 | + clipped, | |
| 53 | + }) | |
| 54 | +} | |
| 55 | + | |
| 56 | +/// One cell as the table shows it. Excel keeps a date as the number of days | |
| 57 | +/// since 1899-12-30 and a duration as a fraction of one day, so both would | |
| 58 | +/// print as a bare float without this; everything else is its own `Display`, | |
| 59 | +/// and an empty cell is an empty string rather than a placeholder. | |
| 60 | +fn cell(data: &Data) -> String { | |
| 61 | + match data { | |
| 62 | + Data::DateTime(stamp) if stamp.is_duration() => { | |
| 63 | + let total = (stamp.as_f64() * 86_400.0).round() as i64; | |
| 64 | + format!( | |
| 65 | + "{}:{:02}:{:02}", | |
| 66 | + total / 3600, | |
| 67 | + (total / 60) % 60, | |
| 68 | + total % 60 | |
| 69 | + ) | |
| 70 | + } | |
| 71 | + Data::DateTime(stamp) => { | |
| 72 | + let (year, month, day, hour, minute, second, _) = stamp.to_ymd_hms_milli(); | |
| 73 | + if (hour, minute, second) == (0, 0, 0) { | |
| 74 | + format!("{year:04}-{month:02}-{day:02}") | |
| 75 | + } else { | |
| 76 | + format!("{year:04}-{month:02}-{day:02} {hour:02}:{minute:02}") | |
| 77 | + } | |
| 78 | + } | |
| 79 | + other => other.to_string(), | |
| 80 | + } | |
| 81 | +} | |
| 82 | + | |
| 83 | +/// The spreadsheet name of a zero-based column: `A`, `Z`, `AA`, `AB`. The | |
| 84 | +/// grid draws these across the top so a cell can be named out loud without | |
| 85 | +/// guessing whether the sheet's first row is a header. | |
| 86 | +pub(crate) fn column_name(index: usize) -> String { | |
| 87 | + let mut name = Vec::new(); | |
| 88 | + let mut left = index; | |
| 89 | + loop { | |
| 90 | + name.push(b'A' + (left % 26) as u8); | |
| 91 | + if left < 26 { | |
| 92 | + break; | |
| 93 | + } | |
| 94 | + left = left / 26 - 1; | |
| 95 | + } | |
| 96 | + name.reverse(); | |
| 97 | + String::from_utf8(name).unwrap_or_default() | |
| 98 | +} | |
| 99 | + | |
| 100 | +#[cfg(test)] | |
| 101 | +mod tests { | |
| 102 | + use super::*; | |
| 103 | + use calamine::{ExcelDateTime, ExcelDateTimeType}; | |
| 104 | + | |
| 105 | + #[test] | |
| 106 | + fn empty_cell_is_empty_string() { | |
| 107 | + assert_eq!(cell(&Data::Empty), ""); | |
| 108 | + } | |
| 109 | + | |
| 110 | + #[test] | |
| 111 | + fn a_whole_number_keeps_no_decimal_point() { | |
| 112 | + assert_eq!(cell(&Data::Float(1.0)), "1"); | |
| 113 | + assert_eq!(cell(&Data::Int(42)), "42"); | |
| 114 | + } | |
| 115 | + | |
| 116 | + #[test] | |
| 117 | + fn a_date_reads_as_a_date_not_a_serial_number() { | |
| 118 | + let stamp = ExcelDateTime::new(45943.0, ExcelDateTimeType::DateTime, false); | |
| 119 | + assert_eq!(cell(&Data::DateTime(stamp)), "2025-10-13"); | |
| 120 | + } | |
| 121 | + | |
| 122 | + #[test] | |
| 123 | + fn a_time_of_day_rides_along_with_the_date() { | |
| 124 | + let stamp = ExcelDateTime::new(45943.5, ExcelDateTimeType::DateTime, false); | |
| 125 | + assert_eq!(cell(&Data::DateTime(stamp)), "2025-10-13 12:00"); | |
| 126 | + } | |
| 127 | + | |
| 128 | + #[test] | |
| 129 | + fn a_duration_reads_as_a_clock() { | |
| 130 | + let stamp = ExcelDateTime::new(0.5, ExcelDateTimeType::TimeDelta, false); | |
| 131 | + assert_eq!(cell(&Data::DateTime(stamp)), "12:00:00"); | |
| 132 | + } | |
| 133 | + | |
| 134 | + #[test] | |
| 135 | + fn columns_carry_past_z() { | |
| 136 | + assert_eq!(column_name(0), "A"); | |
| 137 | + assert_eq!(column_name(25), "Z"); | |
| 138 | + assert_eq!(column_name(26), "AA"); | |
| 139 | + assert_eq!(column_name(27), "AB"); | |
| 140 | + assert_eq!(column_name(51), "AZ"); | |
| 141 | + assert_eq!(column_name(52), "BA"); | |
| 142 | + assert_eq!(column_name(701), "ZZ"); | |
| 143 | + assert_eq!(column_name(702), "AAA"); | |
| 144 | + } | |
| 145 | + | |
| 146 | + #[test] | |
| 147 | + fn bytes_that_are_not_a_workbook_have_no_sheet() { | |
| 148 | + assert!(read(b"not a workbook at all".to_vec(), 0).is_none()); | |
| 149 | + } | |
| 150 | +} |
crates/izlek-web/tests/fixtures/book.xlsxadded+0 -0
Binary file changed
crates/izlek-web/tests/http.rsmodified+71 -0
| @@ -6272,3 +6272,74 @@ async fn a_member_who_may_only_read_still_gets_the_tab_strip() { | ||
| 6272 | 6272 | "no tab strip for a member: {html}" |
| 6273 | 6273 | ); |
| 6274 | 6274 | } |
| 6275 | + | |
| 6276 | +/// A spreadsheet has no browser element behind it: Izlek reads the workbook | |
| 6277 | +/// itself and lays the sheet out as a table, tab strip and all. The upload is | |
| 6278 | +/// a real xlsx, so this covers the sniffer, the reader and the view together. | |
| 6279 | +#[tokio::test] | |
| 6280 | +async fn a_workbook_opens_as_a_table_and_its_other_sheet_is_one_link_away() { | |
| 6281 | + let app = App::open().await; | |
| 6282 | + let admin_cookie = admin(&app).await; | |
| 6283 | + let column = first_column(&app).await; | |
| 6284 | + let task = a_task(&app, &admin_cookie, &column, "Read the book").await; | |
| 6285 | + | |
| 6286 | + let book = include_bytes!("fixtures/book.xlsx"); | |
| 6287 | + app.post_multipart( | |
| 6288 | + "/files", | |
| 6289 | + Some(&admin_cookie), | |
| 6290 | + &[("task_id", &task)], | |
| 6291 | + Some(( | |
| 6292 | + "costs.xlsx", | |
| 6293 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | |
| 6294 | + book, | |
| 6295 | + )), | |
| 6296 | + ) | |
| 6297 | + .await; | |
| 6298 | + let snapshot = app | |
| 6299 | + .post( | |
| 6300 | + "/api/fetch_task", | |
| 6301 | + Some(&admin_cookie), | |
| 6302 | + &[("task_id", &task)], | |
| 6303 | + ) | |
| 6304 | + .await; | |
| 6305 | + let file_id = attachment_id_named(&snapshot.body, "costs.xlsx"); | |
| 6306 | + | |
| 6307 | + let page = app | |
| 6308 | + .get( | |
| 6309 | + &format!("/?task={task}&file={file_id}"), | |
| 6310 | + Some(&admin_cookie), | |
| 6311 | + ) | |
| 6312 | + .await; | |
| 6313 | + assert_eq!(page.status, StatusCode::OK); | |
| 6314 | + let html = String::from_utf8_lossy(&page.bytes); | |
| 6315 | + assert!(html.contains("sheet-table"), "no sheet table: {html}"); | |
| 6316 | + assert!(html.contains("Cable"), "a cell's text is missing: {html}"); | |
| 6317 | + assert!(html.contains("1250"), "a number is missing: {html}"); | |
| 6318 | + assert!(html.contains("Ledger"), "the sheet tab is missing: {html}"); | |
| 6319 | + assert!( | |
| 6320 | + html.contains(&format!("file={file_id}&sheet=1")), | |
| 6321 | + "no link to the second sheet: {html}" | |
| 6322 | + ); | |
| 6323 | + | |
| 6324 | + // The tab strip is navigation, so the second sheet is a page of its own. | |
| 6325 | + let second = app | |
| 6326 | + .get( | |
| 6327 | + &format!("/?task={task}&file={file_id}&sheet=1"), | |
| 6328 | + Some(&admin_cookie), | |
| 6329 | + ) | |
| 6330 | + .await; | |
| 6331 | + let second_html = String::from_utf8_lossy(&second.bytes); | |
| 6332 | + assert!( | |
| 6333 | + second_html.contains("Second sheet cell"), | |
| 6334 | + "the second sheet did not render: {second_html}" | |
| 6335 | + ); | |
| 6336 | + | |
| 6337 | + // A hand-edited sheet index is the first sheet, not an error. | |
| 6338 | + let past_the_end = app | |
| 6339 | + .get( | |
| 6340 | + &format!("/?task={task}&file={file_id}&sheet=99"), | |
| 6341 | + Some(&admin_cookie), | |
| 6342 | + ) | |
| 6343 | + .await; | |
| 6344 | + assert!(String::from_utf8_lossy(&past_the_end.bytes).contains("Cable")); | |
| 6345 | +} |
style/main.scssmodified+90 -0
| @@ -1229,6 +1229,96 @@ img.avatar { | ||
| 1229 | 1229 | |
| 1230 | 1230 | .viewer-pdf { width: 100%; height: 78vh; border: 1px solid var(--border); border-radius: var(--radius-md); } |
| 1231 | 1231 | |
| 1232 | +// A workbook has no element of its own, so the viewer draws it: a tab strip | |
| 1233 | +// when the book has more than one sheet, and a grid that pans inside its own | |
| 1234 | +// box — the document never scrolls. | |
| 1235 | +.viewer-sheet { | |
| 1236 | + display: flex; | |
| 1237 | + flex-direction: column; | |
| 1238 | + gap: 10px; | |
| 1239 | + width: min(86vw, 900px); | |
| 1240 | +} | |
| 1241 | + | |
| 1242 | +.sheet-tabs { | |
| 1243 | + display: flex; | |
| 1244 | + gap: 6px; | |
| 1245 | + overflow-x: auto; | |
| 1246 | + scrollbar-width: none; | |
| 1247 | + | |
| 1248 | + &::-webkit-scrollbar { display: none; } | |
| 1249 | +} | |
| 1250 | + | |
| 1251 | +.sheet-tab { | |
| 1252 | + padding: 4px 10px; | |
| 1253 | + border: 1px solid var(--border); | |
| 1254 | + border-radius: var(--radius-sm); | |
| 1255 | + color: var(--text-dim); | |
| 1256 | + font-size: 12px; | |
| 1257 | + white-space: nowrap; | |
| 1258 | + text-decoration: none; | |
| 1259 | +} | |
| 1260 | + | |
| 1261 | +.sheet-tab-on { | |
| 1262 | + color: var(--text); | |
| 1263 | + border-color: var(--accent); | |
| 1264 | +} | |
| 1265 | + | |
| 1266 | +.sheet-grid { | |
| 1267 | + max-height: 70vh; | |
| 1268 | + overflow: auto; | |
| 1269 | + scrollbar-width: none; | |
| 1270 | + border: 1px solid var(--border); | |
| 1271 | + border-radius: var(--radius-md); | |
| 1272 | + | |
| 1273 | + &::-webkit-scrollbar { display: none; } | |
| 1274 | +} | |
| 1275 | + | |
| 1276 | +.sheet-table { | |
| 1277 | + border-collapse: collapse; | |
| 1278 | + font-size: 12px; | |
| 1279 | + font-variant-numeric: tabular-nums; | |
| 1280 | + | |
| 1281 | + th, | |
| 1282 | + td { | |
| 1283 | + padding: 4px 8px; | |
| 1284 | + border-right: 1px solid var(--border); | |
| 1285 | + border-bottom: 1px solid var(--border); | |
| 1286 | + max-width: 220px; | |
| 1287 | + overflow: hidden; | |
| 1288 | + text-overflow: ellipsis; | |
| 1289 | + white-space: nowrap; | |
| 1290 | + text-align: left; | |
| 1291 | + } | |
| 1292 | + | |
| 1293 | + // The column letters and row numbers are chrome, not data: dim, sticky at | |
| 1294 | + // the edges they name, so a cell keeps its address while the grid pans. | |
| 1295 | + thead th { | |
| 1296 | + position: sticky; | |
| 1297 | + top: 0; | |
| 1298 | + z-index: 1; | |
| 1299 | + background: var(--surface); | |
| 1300 | + color: var(--text-dim); | |
| 1301 | + font-weight: 500; | |
| 1302 | + } | |
| 1303 | + | |
| 1304 | + .sheet-gutter { | |
| 1305 | + position: sticky; | |
| 1306 | + left: 0; | |
| 1307 | + background: var(--surface); | |
| 1308 | + color: var(--text-dim); | |
| 1309 | + font-weight: 500; | |
| 1310 | + text-align: right; | |
| 1311 | + } | |
| 1312 | + | |
| 1313 | + thead .sheet-gutter { z-index: 2; } | |
| 1314 | +} | |
| 1315 | + | |
| 1316 | +.sheet-note { | |
| 1317 | + margin: 0; | |
| 1318 | + color: var(--text-dim); | |
| 1319 | + font-size: 12px; | |
| 1320 | +} | |
| 1321 | + | |
| 1232 | 1322 | .new-task-form { |
| 1233 | 1323 | display: flex; |
| 1234 | 1324 | flex-direction: column; |