| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use topcoat::{ |
| 4 | Result, |
| 5 | asset::{AssetBundle, RouterBuilderAssetExt}, |
| 6 | context::{Cx, app_context}, |
| 7 | asset::asset, |
| 8 | cookie::RouterBuilderCookieExt, |
| 9 | font::{Font, RouterBuilderFontExt, font}, |
| 10 | router::{ |
| 11 | Body, BodyLimit, HeaderValue, Layer, LayerFuture, Next, Path, Router, |
| 12 | RouterBuilderDirectoryExt, Slot, StatusCode, header, layout, module_router, page, |
| 13 | query_params, |
| 14 | request::uri, |
| 15 | }, |
| 16 | tailwind, |
| 17 | view::{Unescaped, View, view}, |
| 18 | }; |
| 19 | |
| 20 | use crate::editor_assets::{self, EditorAssets}; |
| 21 | use crate::hub::Hub; |
| 22 | use crate::i18n::{self, Msg, t}; |
| 23 | use crate::media::Media; |
| 24 | use crate::store::{ListFilter, Store}; |
| 25 | |
| 26 | use self::pane::{ListSpec, Mobile}; |
| 27 | use topcoat::view::Child; |
| 28 | use self::shell::{Section, Shelf, SidebarData, notebook_name, sidebar, title_of, toast_region}; |
| 29 | use self::palette::palette_dialog; |
| 30 | |
| 31 | mod lang; |
| 32 | mod media; |
| 33 | mod notebooks; |
| 34 | mod notes; |
| 35 | mod pane; |
| 36 | mod export_all; |
| 37 | mod format; |
| 38 | mod import; |
| 39 | mod palette; |
| 40 | mod scheme; |
| 41 | mod search; |
| 42 | mod shell; |
| 43 | mod trash; |
| 44 | mod unfiled; |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | const ATKINSON_NEXT: Font = font! { |
| 53 | "Atkinson Hyperlegible Next", |
| 54 | @font-face { |
| 55 | src: url(asset!("fonts/atkinson-hyperlegible-next-latin-wght.woff2")) format("woff2") tech("variations"); |
| 56 | font-weight: 200 800; |
| 57 | font-display: swap; |
| 58 | unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; |
| 59 | } |
| 60 | @font-face { |
| 61 | src: url(asset!("fonts/atkinson-hyperlegible-next-latin-ext-wght.woff2")) format("woff2") tech("variations"); |
| 62 | font-weight: 200 800; |
| 63 | font-display: swap; |
| 64 | unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF; |
| 65 | } |
| 66 | }; |
| 67 | const ATKINSON_MONO: Font = font! { |
| 68 | "Atkinson Hyperlegible Mono", |
| 69 | @font-face { |
| 70 | src: url(asset!("fonts/atkinson-hyperlegible-mono-latin-wght.woff2")) format("woff2") tech("variations"); |
| 71 | font-weight: 200 800; |
| 72 | font-display: swap; |
| 73 | unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; |
| 74 | } |
| 75 | @font-face { |
| 76 | src: url(asset!("fonts/atkinson-hyperlegible-mono-latin-ext-wght.woff2")) format("woff2") tech("variations"); |
| 77 | font-weight: 200 800; |
| 78 | font-display: swap; |
| 79 | unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | |
| 85 | struct ImmutableEditorFiles; |
| 86 | |
| 87 | impl Layer for ImmutableEditorFiles { |
| 88 | fn path(&self) -> Option<&Path> { |
| 89 | None |
| 90 | } |
| 91 | |
| 92 | fn handle<'a>(&'a self, cx: &'a Cx, body: Body, next: Next<'a>) -> LayerFuture<'a> { |
| 93 | Box::pin(async move { |
| 94 | let mut response = next.run(cx, body).await?; |
| 95 | let name = uri(cx).path().trim_start_matches('/'); |
| 96 | if let Some(content_type) = editor_assets::content_type(name) { |
| 97 | let status = response.status(); |
| 98 | if status.is_success() || status == StatusCode::NOT_MODIFIED { |
| 99 | let headers = response.headers_mut(); |
| 100 | headers.insert( |
| 101 | header::CACHE_CONTROL, |
| 102 | HeaderValue::from_static("public, max-age=31536000, immutable"), |
| 103 | ); |
| 104 | headers.insert(header::CONTENT_TYPE, HeaderValue::from_static(content_type)); |
| 105 | } |
| 106 | } |
| 107 | Ok(response) |
| 108 | }) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | pub fn router(store: Arc<Store>, hub: Arc<Hub>, media: Arc<Media>) -> Router { |
| 113 | |
| 114 | let editor: Arc<Option<EditorAssets>> = Arc::new(EditorAssets::load()); |
| 115 | module_router!() |
| 116 | .route(export_all::export_zip) |
| 117 | .route(import::import_file) |
| 118 | .route(media::upload_media) |
| 119 | .route(media::serve_media) |
| 120 | .route(media::serve_take) |
| 121 | .route(media::recording_socket) |
| 122 | .route(format::format_code) |
| 123 | |
| 124 | .layer(BodyLimit::max(128 * 1024 * 1024).at("/import/file")) |
| 125 | .layer(ImmutableEditorFiles) |
| 126 | .cookies() |
| 127 | .font(ATKINSON_NEXT) |
| 128 | .font(ATKINSON_MONO) |
| 129 | .public_dir("./assets") |
| 130 | .assets(AssetBundle::load().expect("asset bundle missing: run `topcoat asset bundle`")) |
| 131 | .app_context(store) |
| 132 | .app_context(hub) |
| 133 | .app_context(editor) |
| 134 | .app_context(media) |
| 135 | .build() |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | #[query_params(error = bad_request)] |
| 141 | pub struct ShellQuery { |
| 142 | pub q: Option<String>, |
| 143 | pub toast: Option<String>, |
| 144 | pub undo: Option<String>, |
| 145 | } |
| 146 | |
| 147 | |
| 148 | fn is_note_id(id: &str) -> bool { |
| 149 | !id.is_empty() && id.len() <= 64 && id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | #[layout] |
| 155 | async fn layout(cx: &Cx, slot: Slot<'_>) -> Result<impl View> { |
| 156 | let store = app_context::<Arc<Store>>(cx); |
| 157 | let lang = i18n::lang(cx); |
| 158 | let scheme = crate::scheme::scheme(cx); |
| 159 | let notebooks = store.list_notebooks().await.map_err(topcoat::Error::from_anyhow)?; |
| 160 | let (all_count, unfiled_count, trash_count) = |
| 161 | store.count_notes().await.map_err(topcoat::Error::from_anyhow)?; |
| 162 | let notebook_counts = store.notebook_counts().await.map_err(topcoat::Error::from_anyhow)?; |
| 163 | |
| 164 | let (q, toast_key, undo) = match query_params::<ShellQuery>(cx) { |
| 165 | Ok(query) => (query.q.clone(), query.toast.clone(), query.undo.clone()), |
| 166 | Err(_) => (None, None, None), |
| 167 | }; |
| 168 | let query = q.unwrap_or_default(); |
| 169 | |
| 170 | let uri = uri(cx); |
| 171 | let back = uri |
| 172 | .path_and_query() |
| 173 | .map(|pq| pq.as_str().to_owned()) |
| 174 | .unwrap_or_else(|| "/".to_owned()); |
| 175 | let path = uri.path().to_owned(); |
| 176 | let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect(); |
| 177 | |
| 178 | |
| 179 | |
| 180 | let mut current_notebook = String::new(); |
| 181 | let (section, page_name) = match segments.as_slice() { |
| 182 | [] => (Section::All, t(lang, Msg::AllNotes).to_owned()), |
| 183 | ["unfiled"] => (Section::Unfiled, t(lang, Msg::Unfiled).to_owned()), |
| 184 | ["trash"] => (Section::Trash, t(lang, Msg::Trash).to_owned()), |
| 185 | |
| 186 | ["search", ..] if query.trim().is_empty() => (Section::All, t(lang, Msg::AllNotes).to_owned()), |
| 187 | ["search", ..] => (Section::Other, t(lang, Msg::Search).to_owned()), |
| 188 | ["notebooks", id, ..] => { |
| 189 | let name = notebook_name(¬ebooks, Some(id)); |
| 190 | if name.is_some() { |
| 191 | current_notebook = (*id).to_owned(); |
| 192 | } |
| 193 | (Section::Notebook((*id).to_owned()), name.unwrap_or_default()) |
| 194 | } |
| 195 | ["notes", id, ..] => match store.get_note(id).await.map_err(topcoat::Error::from_anyhow)? { |
| 196 | Some(meta) => { |
| 197 | current_notebook = meta.notebook_id.clone().unwrap_or_default(); |
| 198 | |
| 199 | let section = if meta.trashed_at.is_some() { |
| 200 | Section::Trash |
| 201 | } else if let Some(nb) = &meta.notebook_id { |
| 202 | Section::Notebook(nb.clone()) |
| 203 | } else { |
| 204 | Section::Unfiled |
| 205 | }; |
| 206 | (section, title_of(lang, &meta.title)) |
| 207 | } |
| 208 | None => (Section::All, String::new()), |
| 209 | }, |
| 210 | _ => (Section::Other, String::new()), |
| 211 | }; |
| 212 | let title = if page_name.is_empty() { |
| 213 | t(lang, Msg::AppName).to_owned() |
| 214 | } else { |
| 215 | format!("{page_name} \u{b7} {}", t(lang, Msg::AppName)) |
| 216 | }; |
| 217 | |
| 218 | let shelves: Vec<Shelf> = notebooks |
| 219 | .iter() |
| 220 | .map(|nb| Shelf { |
| 221 | id: nb.id.clone(), |
| 222 | name: nb.name.clone(), |
| 223 | count: notebook_counts.get(&nb.id).copied().unwrap_or(0), |
| 224 | }) |
| 225 | .collect(); |
| 226 | |
| 227 | let toast_message = match toast_key.as_deref() { |
| 228 | Some("trashed") => Some(t(lang, Msg::ToastTrashed)), |
| 229 | Some("restored") => Some(t(lang, Msg::ToastRestored)), |
| 230 | Some("notebook_created") => Some(t(lang, Msg::ToastNotebookCreated)), |
| 231 | Some("notebook_deleted") => Some(t(lang, Msg::ToastNotebookDeleted)), |
| 232 | Some("moved") => Some(t(lang, Msg::ToastMoved)), |
| 233 | Some("version-restored") => Some(t(lang, Msg::ToastVersionRestored)), |
| 234 | _ => None, |
| 235 | }; |
| 236 | let undo_id = match (toast_key.as_deref(), undo) { |
| 237 | (Some("trashed"), Some(id)) if is_note_id(&id) => Some(id), |
| 238 | _ => None, |
| 239 | }; |
| 240 | |
| 241 | let sidebar_data = SidebarData { |
| 242 | all: all_count, |
| 243 | unfiled: unfiled_count, |
| 244 | trash: trash_count, |
| 245 | notebooks: shelves, |
| 246 | section, |
| 247 | current_notebook, |
| 248 | query, |
| 249 | back, |
| 250 | scheme, |
| 251 | }; |
| 252 | let app_name = t(lang, Msg::AppName); |
| 253 | let skip = t(lang, Msg::SkipToContent); |
| 254 | let open_menu = t(lang, Msg::OpenMenu); |
| 255 | let show_sidebar = t(lang, Msg::ShowSidebar); |
| 256 | |
| 257 | Ok(view! { |
| 258 | <!DOCTYPE html> |
| 259 | <html lang=(lang.code()) data-scheme=(scheme.id())> |
| 260 | <head> |
| 261 | <meta charset="utf-8"> |
| 262 | <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"> |
| 263 | <title>(title)</title> |
| 264 | <script>(Unescaped::new_unchecked(shell::THEME_SCRIPT))</script> |
| 265 | <style>(Unescaped::new_unchecked(shell::SHELL_STYLE))</style> |
| 266 | topcoat::font::link(font: ATKINSON_NEXT) |
| 267 | topcoat::font::link(font: ATKINSON_MONO) |
| 268 | <link rel="stylesheet" href=(tailwind::stylesheet!())> |
| 269 | <link rel="stylesheet" href="/editor.css"> |
| 270 | </head> |
| 271 | <body class="bg-background text-foreground antialiased"> |
| 272 | <a |
| 273 | href="#main" |
| 274 | class="sr-only focus:not-sr-only focus:fixed focus:top-2 focus:left-2 focus:z-[60] focus:rounded-control focus:bg-popover focus:px-3 focus:py-2 focus:text-sm focus:text-popover-foreground focus:shadow-float" |
| 275 | >(skip)</a> |
| 276 | <div class="flex h-dvh flex-col overflow-hidden lg:flex-row"> |
| 277 | <header class="flex h-12 shrink-0 items-center gap-2 border-b border-border bg-sidebar pt-[env(safe-area-inset-top)] pr-[max(0.5rem,env(safe-area-inset-right))] pl-[max(0.5rem,env(safe-area-inset-left))] lg:hidden"> |
| 278 | <button |
| 279 | id="drawer-open" |
| 280 | type="button" |
| 281 | aria-controls="sidebar" |
| 282 | aria-expanded="false" |
| 283 | aria-label=(open_menu) |
| 284 | class="inline-flex size-10 items-center justify-center rounded-control text-foreground transition-colors hover:bg-foreground/5" |
| 285 | > |
| 286 | <svg class="size-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> |
| 287 | <path d="M4 6h16M4 12h16M4 18h16"></path> |
| 288 | </svg> |
| 289 | </button> |
| 290 | <a href="/" class="text-base font-semibold tracking-tight">(app_name)</a> |
| 291 | </header> |
| 292 | sidebar(lang: lang, data: sidebar_data) |
| 293 | <div id="drawer-backdrop" data-open="false" aria-hidden="true"></div> |
| 294 | <button |
| 295 | id="sidebar-expand" |
| 296 | type="button" |
| 297 | aria-controls="sidebar" |
| 298 | aria-label=(show_sidebar) |
| 299 | class="fixed top-3 left-3 z-20 size-8 items-center justify-center rounded-control border border-border bg-card text-foreground transition-colors hover:bg-foreground/5" |
| 300 | > |
| 301 | <svg class="size-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> |
| 302 | <rect x="3" y="3" width="18" height="18" rx="2"></rect> |
| 303 | <path d="M9 3v18"></path> |
| 304 | </svg> |
| 305 | </button> |
| 306 | <main id="main" tabindex="-1" class="flex min-h-0 min-w-0 flex-1 flex-col overflow-y-auto focus:outline-none"> |
| 307 | (slot) |
| 308 | </main> |
| 309 | </div> |
| 310 | toast_region(lang: lang, message: toast_message, undo: undo_id) |
| 311 | palette_dialog(lang: lang) |
| 312 | <script>(Unescaped::new_unchecked(shell::SHELL_SCRIPT))</script> |
| 313 | </body> |
| 314 | </html> |
| 315 | }) |
| 316 | } |
| 317 | |
| 318 | |
| 319 | #[page] |
| 320 | async fn index(cx: &Cx) -> Result<impl View> { |
| 321 | all_notes(cx).await |
| 322 | } |
| 323 | |
| 324 | |
| 325 | async fn all_notes(cx: &Cx) -> Result<impl View> { |
| 326 | let store = app_context::<Arc<Store>>(cx); |
| 327 | let notebooks = store.list_notebooks().await.map_err(topcoat::Error::from_anyhow)?; |
| 328 | let page = pane::load_page(cx, store, ListFilter::All) |
| 329 | .await |
| 330 | .map_err(topcoat::Error::from_anyhow)?; |
| 331 | let lang = i18n::lang(cx); |
| 332 | let spec = ListSpec { |
| 333 | title: t(lang, Msg::AllNotes).to_owned(), |
| 334 | heading: true, |
| 335 | count: page.total, |
| 336 | notebook_id: String::new(), |
| 337 | groups: pane::groups(lang, &page.notes, ¬ebooks, &page.previews, None, true), |
| 338 | empty: t(lang, Msg::NoNotesYet), |
| 339 | more: page.more, |
| 340 | }; |
| 341 | let list = pane::list_pane(cx, lang, spec, Child::new(())); |
| 342 | Ok(pane::split( |
| 343 | cx, |
| 344 | Mobile::List, |
| 345 | Child::new(list), |
| 346 | Child::new(pane::empty_detail(cx, lang)), |
| 347 | )) |
| 348 | } |