Store document, folder, collection, and tag favorites in the userdata$<uid>$favorite table with fractional indices and soft-delete removal. Fall back to legacy docProperties favorite flags when no favorite table exists, and copy them into the table on the first favorite write. Load and persist the favorite table across SQLite read-write sessions, read-only workspace opens, Web Desktop IndexedDB stores, and cloud sync. Render and expand favorite documents, folders, collections, and tags in the desktop sidebar Favorites section apart from the Organize tree. Add menu actions to add or remove favorites and drag reorder for top-level Favorites rows without changing db$folders order. Copy the database to a separate path before writing a test -wal sidecar in sqlite_wal_blobs to avoid close races.
6.3 KiB
Working in packages/frontend/desktop/ (for agents)
packages/frontend/desktop/ (affine-desktop) is the L4 pure-Rust Web Desktop and Electron desktop application shell inside the client workspace rooted at packages/frontend/Cargo.toml.
Scope and architecture
affine-desktopowns the multi-paneWorkbench, collapsible and resizableAppSidebar(workspace switcher, Quick Search, New Page/Edgeless, All Docs, Favorites, Journals, Folders/Collections/Tags tree, Trash, and Settings), multi-tab Split View with per-paneEntity<playground::PlaygroundView>synchronized incrementally via Yjs V1 state-vector diffs (encode_diff_v1) throughDesktopDocStoreand per-paneNavHistory,Cmd/Ctrl+KQuickSearch, centeredPeekView, physically isolatedComposerDraftStorewith SHA-256 content-addressed attachments and one-way idempotent importers,wasm32Web Desktop mount entry (wasm_desktop_main), and thedesktop-harnessoffscreen verification CLI.- Native read-only
.affine/<spaceId>.dbinspection opens viaSqliteDocStorage::open_read_only(sqlite_loader.rs) with lazy per-document hydration and zero CRDT writeback. The read worker reuses one read-only connection of the current backing and reopens it when the backing changes, the file is replaced, or the last backing handle is dropped. It holds no read transaction between requests. - Interactive mutations operate on in-memory
DesktopDocStoresessions until the host binds aWorkspaceSessionto the active workspace: the native host with its SQLite store and transport (DesktopShellView::connect_workspace), Web Desktop with its IndexedDB store (DesktopShellView::bind_local_workspaces,workbench/local_workspace/). From then on thatWorkspaceSessionis the single writer of the workspace (doc_store/writer.rs): shell commands (create, journal, rename, favorite, trash, restore, delete, folder placement) write to it first and the store projects the result. Editor and store document edits join one ordered queue, and every session write merges that queue first. - Favorites live in the
userdata$<uid>$favoritetable (doc_store/favorites.rs), one row per document, folder, collection, or tag, ordered by fractionalindex. The Favorites section expands its folders, collections, and tags apart from the other sections, and a drag between its top-level rows changes only the favorite order. A workspace without the table reads the table of the0.27.5__local__user, else the documents thatdb$docPropertiesflags withfavorite: true; the first favorite write copies them into the table. doc_store/views.rsbuilds the document lists, sidebar counts, folder tree, and Quick Search index once per metadata revision. Custom property filters also key the views on the revisions of the property tables. A table change that no store command made also builds the views again. The sidebar tree flattens into rows by expansion state and mounts only the rows thatui_kit::VirtualListplaces in the viewport and overscan.
Web Desktop (wasm_desktop_main) opens each showcase workspace from the documents that hosts/web/desktop/start.js reads from its nbstore IndexedDB store local:workspace:<workspaceId> (the workspace root, the db$* tables, the settings and favorite userdata$* tables, and one document per page), or seeds a workspace that the store does not hold. Each workspace has a session of its own, and the shell binds the session of the active workspace as its writer. Every 250 ms on the UI scheduler clock, and at once when the active workspace, its document list, or the open document changes, the shell writes the session updates and deletions of every workspace to the store and the route to the ?workspace=&doc= query. An explicit mode=page or mode=edgeless sets the initial view mode for that document and stays in the URL until the route changes. flush_web_desktop writes them at once; the page calls it when it is hidden or unloaded. The shell also writes the app tabs of the active workspace (route, view mode, pin state, and split views of each tab) to the store with LocalDocStore::set_tabs, and Web Desktop keeps them in localStorage. At startup, before the first write, each workspace opens the tabs that the store kept for it, and then the route and the split view of the URL open in the active workspace. Without IndexedDB, or when the stored documents do not decode, the showcase runs in memory only.
The workbench drives the connected session every second on the UI scheduler clock. SQLite runs on the host runtime. When the workbench switches workspaces or disconnects, it commits the bound session to its original SQLite store before it stops the timer.
Domain boundaries and invariants
- Keep
#![forbid(unsafe_code)]at the crate root. - Depend only on workspace crates allowed by
packages/frontend/scripts/check-invariants.sh(ui-tokens,ui,ui-kit,blocksuite-playground-doc,blocksuite-preview,blocksuite-editor,playground,affine-store,affine-client,affine-legacy-import). Never depend onpackages/frontend/mobileorpackages/frontend/hosts/*. - Keep
ComposerDraftStorephysically isolated from collaborative<spaceId>.dbworkspace databases. Never store unpublished drafts inside<spaceId>.db. - Drive all time-based logic from explicit timestamps or the scheduler clock. Never call system wall clocks (
Instant::now,SystemTime::now,Zoned::now,Timestamp::now). - Keep zero
is_headlessbranches and zero inline#[cfg(any(test, feature = "harness"))]branches in shipping view modules. - Keep every
.rsfile at or below 800 non-test lines, zerosuper::super::, andwasm32-unknown-unknownbuilds warning-free.
Verification
Run inside packages/frontend/ with root NAPI flags cleared:
CARGO_ENCODED_RUSTFLAGS="" cargo check --locked -p affine-desktop --target wasm32-unknown-unknown
CARGO_ENCODED_RUSTFLAGS="" cargo test --locked -p affine-desktop
CARGO_ENCODED_RUSTFLAGS="" cargo run --locked -p affine-desktop --features harness --bin desktop-harness -- audit
CARGO_ENCODED_RUSTFLAGS="" cargo run --locked -p affine-desktop --features harness --bin desktop-harness -- gallery --out evidence/desktop
CARGO_ENCODED_RUSTFLAGS="" cargo run --locked -p affine-desktop --features harness --bin desktop-harness -- scale
./scripts/check-invariants.sh