From 3a75ecff4c4f376db05fbfba6880e2aa4c08b47c Mon Sep 17 00:00:00 2001 From: DarkSky Date: Thu, 3 Dec 2026 17:43:38 +0900 Subject: [PATCH] feat(desktop): add the outline header and share menu icons Add a Table of Contents header with a button that opens the sidebar. Use the BlockSuite icons in the document menu and the outline header. Keep the last outline tick when the rail has room for it. Clear the active outline entry when its block stops being a heading. --- .../crates/playground/src/page_outline.rs | 36 ++- .../desktop/src/views_extra/outline_rail.rs | 229 +++++++++++++++++- .../src/views_extra/tests/outline_rail.rs | 8 +- .../desktop/src/workbench/topbar/menu.rs | 10 +- packages/ui/tokens/extract-tokens.mjs | 5 + packages/ui/tokens/icons.rs | 30 +++ 6 files changed, 297 insertions(+), 21 deletions(-) diff --git a/blocksuite/crates/playground/src/page_outline.rs b/blocksuite/crates/playground/src/page_outline.rs index 146045c86b..e5d7f35e57 100644 --- a/blocksuite/crates/playground/src/page_outline.rs +++ b/blocksuite/crates/playground/src/page_outline.rs @@ -63,7 +63,13 @@ impl PlaygroundView { { match pin.heading.as_deref() { None => return None, - Some(id) if layout.block(id).is_some() => return Some(id), + Some(id) + if layout + .block(id) + .is_some_and(|block| is_heading(&block.body.kind)) => + { + return Some(id); + } Some(_) => {} } } @@ -100,6 +106,7 @@ impl PlaygroundView { #[cfg(test)] mod tests { use blocksuite_playground_doc::schema::BlockSpec; + use y_octo::{Any, SessionValue}; use super::ParagraphType; use crate::{ @@ -245,4 +252,31 @@ mod tests { session.frame(); assert_eq!(active().as_deref(), Some("h0")); } + + #[test] + fn outline_reveal_releases_a_heading_turned_into_text_in_place() { + let session = outline_session(); + let active = || session.view(|view, _| view.active_outline_heading().map(str::to_owned)); + session.view(|view, cx| view.reveal_outline_heading(Some("h1".into()), cx)); + session.frame(); + assert_eq!(active().as_deref(), Some("h1")); + let scroll_y = session.view(|view, _| view.scroll_y); + + session.view(|view, cx| { + view.edit_document(cx, |doc| { + let page = doc.workspace.ensure_page(HOME_PAGE_ID).unwrap(); + page.set_block_prop( + "h1", + "prop:type", + SessionValue::Any(Any::String("text".into())), + 900, + ) + .unwrap(); + }); + }); + session.frame(); + assert_eq!(session.view(|view, _| view.scroll_y), scroll_y); + assert!(session.view(|view, _| view.flow_layout().unwrap().block("h1").is_some())); + assert_eq!(active().as_deref(), Some("h0")); + } } diff --git a/packages/frontend/desktop/src/views_extra/outline_rail.rs b/packages/frontend/desktop/src/views_extra/outline_rail.rs index 042345e57c..57ea0bb980 100644 --- a/packages/frontend/desktop/src/views_extra/outline_rail.rs +++ b/packages/frontend/desktop/src/views_extra/outline_rail.rs @@ -4,9 +4,10 @@ use std::rc::Rc; use blocksuite_editor::scroll::{ScrollAxis, wheel_delta}; use ui::{ - App, BoxShadow, Context, ElementId, Entity, FontWeight, Hsla, InteractiveElement, IntoElement, - ParentElement, RenderOnce, SharedString, StatefulInteractiveElement, Styled, ViewElement, - Window, div, hsla, prelude::FluentBuilder as _, px, + App, AppContext as _, BoxShadow, Context, ElementId, Entity, FontWeight, Hsla, + InteractiveElement, IntoElement, ParentElement, RenderOnce, SharedString, + StatefulInteractiveElement, Styled, ViewElement, Window, div, hsla, + prelude::FluentBuilder as _, px, svg, }; use ui_kit::VirtualList; use ui_tokens::ThemeMode; @@ -24,6 +25,10 @@ const TICK_HEIGHT: f32 = 16.0; /// Smallest tick space: ticks shrink to their own height before the rail clips them. const TICK_MIN_HEIGHT: f32 = 2.0; const PANEL_PADDING: f32 = 8.0; +/// Height of the panel header: 6 px padding around a 20 px line (`.outline-viewer-header`). +const HEADER_HEIGHT: f32 = 32.0; +/// Side of the `Open in sidebar` button: a 16 px icon in 2 px padding. +const OPEN_BUTTON_SIZE: f32 = 20.0; const PANEL_WIDTH: f32 = 200.0; /// Rows mounted above and below the visible part of the heading list. const OVERSCAN_ROWS: f32 = 2.0; @@ -52,7 +57,7 @@ pub(crate) fn render_outline_rail( cx: &Context, ) -> Option> { if width <= HIDDEN_MAX_PANE_WIDTH - || height - RAIL_TOP - RAIL_BOTTOM < ROW_HEIGHT + 2.0 * PANEL_PADDING + 2.0 + || height - RAIL_TOP - RAIL_BOTTOM < HEADER_HEIGHT + ROW_HEIGHT + 2.0 * PANEL_PADDING + 2.0 || store.get_doc(doc).is_none() { return None; @@ -155,7 +160,7 @@ impl RenderOnce for OutlineRail { let list = VirtualList::fixed(count, ROW_HEIGHT); let list_height = list .total_height() - .min(self.height - 2.0 * PANEL_PADDING - 2.0); + .min(self.height - HEADER_HEIGHT - 2.0 * PANEL_PADDING - 2.0); let max_scroll = list.total_height() - list_height; let scroll = window.use_keyed_state( ElementId::NamedInteger( @@ -182,6 +187,7 @@ impl RenderOnce for OutlineRail { // Web. While the panel stays open, only the wheel moves the list. let reveal = active.map(|index| (index as f32 + 0.5) * ROW_HEIGHT - list_height / 2.0); let reveal_scroll = scroll.clone(); + let collapse = hovered.clone(); rail.interactivity().on_hover(move |inside, _, app| { if *inside && !*hovered.read(app) @@ -299,7 +305,7 @@ impl RenderOnce for OutlineRail { row.style().justify_content = Some(ui::JustifyContent::FlexStart); rows = rows.child(row); } - let panel = panel.child( + let panel = panel.child(self.header(collapse, colors)).child( div() .id("desktop.outline.rail.list") .h(px(list_height)) @@ -355,16 +361,106 @@ impl RenderOnce for OutlineRail { } } +impl OutlineRail { + /// `Table of Contents` and the `Open in sidebar` button above the heading list. The + /// button collapses the panel and opens the Outline tab of the right sidebar for this + /// pane, which hides the rail. + fn header(&self, hovered: Entity, colors: RailColors) -> impl IntoElement + use<> { + let palette = self.palette; + let shell = self.shell.clone(); + let workspace = self.workspace.clone(); + let pane = self.pane; + let mut open = div() + .id(ElementId::NamedInteger( + SharedString::new_static("outline-rail-open"), + ui::hash(&pane), + )) + .snapshot(ui::snap_id!("desktop.outline.rail.open.{pane}"), None) + .size(px(OPEN_BUTTON_SIZE)) + .flex_shrink(0.0) + .flex() + .items_center() + .justify_center() + .rounded(px(4.0)) + .cursor_pointer() + .hover(move |style| style.bg(palette.hover_color)) + .on_click(move |_, _, app| { + hovered.update(app, |hovered, _| *hovered = false); + shell.update(app, |shell, cx| { + if shell.state.store.active_workspace().id != workspace { + return; + } + let Some(index) = shell.state.split.panes.iter().position(|p| p.id == pane) + else { + return; + }; + shell.state.split.focus_pane(index); + shell.state.inspector_tab = Some(crate::DocInspectorTab::Outline); + cx.notify(); + }); + }) + .child( + svg() + .data(ui_tokens::icons::TOC.svg.as_bytes()) + .size(px(OPEN_BUTTON_SIZE - 4.0)) + .text_color(colors.icon), + ); + open.interactivity() + .tooltip(move |_, app| app.new(|_| OpenTooltip(palette)).into()); + div() + .h(px(HEADER_HEIGHT)) + .w_full() + .flex_shrink(0.0) + .p(px(6.0)) + .flex() + .items_center() + .gap(px(4.0)) + .child( + div() + .flex_1() + .min_w_0() + .truncate() + .text_size(px(12.0)) + .line_height(px(20.0)) + .font_weight(FontWeight::MEDIUM) + .text_color(palette.text_secondary) + .child("Table of Contents"), + ) + .child(open) + } +} + +struct OpenTooltip(DesktopPalette); + +impl ui::Render for OpenTooltip { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + div() + .px(px(8.0)) + .py(px(4.0)) + .rounded(px(4.0)) + .bg(self.0.bg_elevated) + .text_color(self.0.text_primary) + .text_size(px(12.0)) + .child("Open in sidebar") + } +} + /// Space of each tick and the number of ticks that fit in `height`. Ticks keep 16 px each /// while they fit, then shrink alike down to their own 2 px height, and the rail clips the -/// rest, as the `flex: 1 1 16px` ticks do in Web. +/// rest, as the `flex: 1 1 16px` ticks do in Web. The count compares against the 2 px floor, +/// so the rounding of `height / count` never drops a tick that fits. fn tick_layout(count: usize, height: f32) -> (f32, usize) { + if count == 0 { + return (TICK_HEIGHT, 0); + } let pitch = (height / count as f32).clamp(TICK_MIN_HEIGHT, TICK_HEIGHT); - (pitch, ((height / pitch).floor() as usize).min(count)) + let shown = ((height / TICK_MIN_HEIGHT).floor() as usize).min(count); + (pitch, shown) } const TICK_TOKEN: &str = "--affine-black-10"; const ACTIVE_TEXT_TOKEN: &str = "--affine-v2-text-emphasis"; +const ICON_TOKEN: &str = "--affine-icon-color"; /// Theme colors of the rail that the desktop palette does not carry. #[derive(Clone, Copy)] @@ -375,6 +471,8 @@ struct RailColors { active_tick: Hsla, /// `text/emphasis`, the color of the active entry in the panel. active_text: Hsla, + /// `--affine-icon-color`, the `Open in sidebar` icon. + icon: Hsla, } impl RailColors { @@ -385,6 +483,7 @@ impl RailColors { tick: color(TICK_TOKEN, tokens.border), active_tick: crate::rgba_to_hsla(tokens.text_primary), active_text: color(ACTIVE_TEXT_TOKEN, tokens.text_emphasis), + icon: color(ICON_TOKEN, tokens.text_secondary), } } } @@ -401,8 +500,11 @@ mod tests { }; use ui_tokens::ThemeMode; - use super::{ACTIVE_TEXT_TOKEN, TICK_HEIGHT, TICK_TOKEN, tick_layout}; - use crate::{DesktopAppState, DesktopShellView, register_desktop_fonts, split_view::PaneId}; + use super::{ACTIVE_TEXT_TOKEN, ICON_TOKEN, RAIL_BOTTOM, TICK_HEIGHT, TICK_TOKEN, tick_layout}; + use crate::{ + DesktopAppState, DesktopShellView, DocInspectorTab, register_desktop_fonts, + split_view::PaneId, + }; const HEIGHT: f32 = 800.0; @@ -592,7 +694,7 @@ mod tests { fn rail_color_tokens_exist_in_both_themes() { for mode in [ThemeMode::Light, ThemeMode::Dark] { let tokens = crate::theme_tokens_for(mode); - for name in [TICK_TOKEN, ACTIVE_TEXT_TOKEN] { + for name in [TICK_TOKEN, ACTIVE_TEXT_TOKEN, ICON_TOKEN] { assert!(tokens.color(name).is_some(), "{name} in {mode:?}"); } } @@ -605,11 +707,116 @@ mod tests { (25, 400.0, (16.0, 25)), (50, 400.0, (8.0, 50)), (512, 400.0, (2.0, 200)), + (45, 400.0, (400.0_f32 / 45.0_f32, 45)), + (50, 428.0, (428.0_f32 / 50.0_f32, 50)), + (0, 400.0, (TICK_HEIGHT, 0)), ] { assert_eq!(tick_layout(count, height), expected, "{count} ticks"); } } + #[test] + fn the_last_of_50_entries_keeps_its_tick_after_its_row_reveals_it() { + let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-03"); + let (doc, ids) = add_sections(&mut state, "Outline", 49, 2); + state.open_doc_in_active_pane(&doc); + let pane = state.split.active().id; + let app = Shell::new(1280, state); + let rail = format!("desktop.outline.rail.{pane}"); + // The rail reaches down to 76 px above the pane bottom: 428 px, where `428 / 50` + // rounds up. + let top = app.element(&rail).unwrap().bounds.y; + let body = app.element("desktop.pane.0").unwrap().bounds; + assert_eq!(body.y + body.height - RAIL_BOTTOM - top, 428.0); + let (x, y) = app.center(&rail); + app.hover(x, y); + let panel = app + .element(&format!("desktop.outline.rail.panel.{pane}")) + .unwrap() + .bounds; + app.scroll( + panel.x + panel.width / 2.0, + panel.y + panel.height / 2.0, + -1.0e6, + ); + app.click(&format!("desktop.outline.rail.heading.{pane}.{}", ids[48])); + assert_eq!( + app.active_heading(pane, &doc).as_deref(), + Some(ids[48].as_str()) + ); + app.hover(1.0, 1.0); + assert!( + app.element(&format!("desktop.outline.rail.panel.{pane}")) + .is_none() + ); + assert_eq!(app.active_tick(pane, 50), Some(49)); + } + + #[test] + fn open_in_sidebar_focuses_its_pane_and_hands_the_outline_to_the_sidebar() { + let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-03"); + let (doc, _) = add_sections(&mut state, "Outline", 60, 2); + state.open_doc_in_active_pane(&doc); + state.open_doc_in_split(&doc); + let panes: Vec = state.split.panes.iter().map(|pane| pane.id).collect(); + assert_eq!(panes.len(), 2); + let active = state.split.active_pane; + let target = 1 - active; + let pane = panes[target]; + let app = Shell::new(2200, state); + let (x, y) = app.center(&format!("desktop.outline.rail.{pane}")); + app.hover(x, y); + let panel = app + .element(&format!("desktop.outline.rail.panel.{pane}")) + .unwrap() + .bounds; + let body = app + .element(&format!("desktop.pane.{target}")) + .unwrap() + .bounds; + assert!( + panel.y + panel.height <= body.y + body.height - RAIL_BOTTOM + 0.01, + "panel {panel:?} in pane {body:?}" + ); + let open = app + .element(&format!("desktop.outline.rail.open.{pane}")) + .unwrap() + .bounds; + assert_eq!((open.width, open.height), (20.0, 20.0)); + // 1 px border, 8 px panel padding, 6 px header padding. + assert_eq!(open.y - panel.y, 15.0); + + app.click(&format!("desktop.outline.rail.open.{pane}")); + app.update(|shell, _, _| { + assert_eq!(shell.state.split.active_pane, target); + assert_eq!(shell.state.inspector_tab, Some(DocInspectorTab::Outline)); + }); + for pane in &panes { + assert!( + app.element(&format!("desktop.outline.rail.{pane}")) + .is_none() + ); + } + + app.update(|shell, _, cx| { + shell.state.inspector_tab = None; + cx.notify(); + }); + app.window.request_frame(); + assert!( + app.element(&format!("desktop.outline.rail.{pane}")) + .is_some() + ); + assert!( + app.element(&format!("desktop.outline.rail.panel.{pane}")) + .is_none() + ); + assert!( + app.element(&format!("desktop.outline.rail.tick.{pane}")) + .is_some() + ); + } + #[test] fn split_panes_of_one_page_track_their_own_active_heading() { let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-03"); diff --git a/packages/frontend/desktop/src/views_extra/tests/outline_rail.rs b/packages/frontend/desktop/src/views_extra/tests/outline_rail.rs index 9392c098e5..4ed62ab4d9 100644 --- a/packages/frontend/desktop/src/views_extra/tests/outline_rail.rs +++ b/packages/frontend/desktop/src/views_extra/tests/outline_rail.rs @@ -100,13 +100,13 @@ fn outline_rail_mounts_a_bounded_window_of_512_headings_while_it_scrolls_and_rev app.hover(&rail); let open = app.bounds(&panel); - // 1 px border and 8 px padding above and below the list. - let list_top = open.y + 9.0; + // 1 px border and 8 px padding around the panel, and the 32 px header above the list. + let list_top = open.y + 9.0 + 32.0; let list_bottom = open.y + open.height - 9.0; // The pane body starts below the 52 px header; the panel keeps 204 px above it and - // 76 px below it, less its border and padding. + // 76 px below it, less its border, padding, and header. let body = app.bounds("desktop.pane.0"); - let list_height = body.height - 52.0 - 204.0 - 76.0 - 18.0; + let list_height = body.height - 52.0 - 204.0 - 76.0 - 18.0 - 32.0; assert_eq!(list_bottom - list_top, list_height); // The showcase document has headings of its own above the added ones. let base = ((app.bounds(&heading(0)).y - list_top) / ROW) as usize; diff --git a/packages/frontend/desktop/src/workbench/topbar/menu.rs b/packages/frontend/desktop/src/workbench/topbar/menu.rs index b05a77c5bc..937b47799a 100644 --- a/packages/frontend/desktop/src/workbench/topbar/menu.rs +++ b/packages/frontend/desktop/src/workbench/topbar/menu.rs @@ -17,11 +17,11 @@ use super::{ }; // Menu glyphs from @blocksuite/icons 2.2.17. -const EDIT_ICON_SVG: &[u8] = br#""#; -const SHARE_ICON_SVG: &[u8] = br#""#; -const OPEN_IN_NEW_ICON_SVG: &[u8] = br#""#; -const PAGE_WIDTH_ICON_SVG: &[u8] = br#""#; -const TOC_ICON_SVG: &[u8] = br#""#; +const EDIT_ICON_SVG: &[u8] = icons::EDIT.svg.as_bytes(); +const SHARE_ICON_SVG: &[u8] = icons::SHARE.svg.as_bytes(); +const OPEN_IN_NEW_ICON_SVG: &[u8] = icons::OPEN_IN_NEW.svg.as_bytes(); +const PAGE_WIDTH_ICON_SVG: &[u8] = icons::PAGE_WIDTH.svg.as_bytes(); +const TOC_ICON_SVG: &[u8] = icons::TOC.svg.as_bytes(); pub(in crate::workbench) struct HeaderOverlay { target: DocTarget, diff --git a/packages/ui/tokens/extract-tokens.mjs b/packages/ui/tokens/extract-tokens.mjs index 0d55e3a992..b5935e01a5 100644 --- a/packages/ui/tokens/extract-tokens.mjs +++ b/packages/ui/tokens/extract-tokens.mjs @@ -111,6 +111,11 @@ const CORE_ICONS = [ ['EXPAND_FULL', 'ExpandFullIcon'], ['HOME', 'HomeIcon'], ['ALL_DOCS', 'AllDocsIcon'], + ['TOC', 'TocIcon'], + ['EDIT', 'EditIcon'], + ['SHARE', 'ShareIcon'], + ['OPEN_IN_NEW', 'OpenInNewIcon'], + ['PAGE_WIDTH', 'LongerIcon'], ]; /** diff --git a/packages/ui/tokens/icons.rs b/packages/ui/tokens/icons.rs index 3de1c04bbb..0e9a42b552 100644 --- a/packages/ui/tokens/icons.rs +++ b/packages/ui/tokens/icons.rs @@ -483,6 +483,31 @@ pub const ALL_DOCS: IconDef = IconDef { svg: "", }; +pub const TOC: IconDef = IconDef { + name: "TocIcon", + svg: "", +}; + +pub const EDIT: IconDef = IconDef { + name: "EditIcon", + svg: "", +}; + +pub const SHARE: IconDef = IconDef { + name: "ShareIcon", + svg: "", +}; + +pub const OPEN_IN_NEW: IconDef = IconDef { + name: "OpenInNewIcon", + svg: "", +}; + +pub const PAGE_WIDTH: IconDef = IconDef { + name: "LongerIcon", + svg: "", +}; + pub const ALL_ICONS: &[IconDef] = &[ ADD_TEXT, ALL_DOCS, @@ -520,6 +545,7 @@ pub const ALL_ICONS: &[IconDef] = &[ DRAG_HANDLE, DUPLICATE, EDGELESS, + EDIT, EXPAND_FULL, EXPAND, FAVORITE, @@ -542,6 +568,7 @@ pub const ALL_ICONS: &[IconDef] = &[ LINK, LINKED_PAGE, LOCK, + PAGE_WIDTH, MINUS, MORE_HORIZONTAL, MORE_VERTICAL, @@ -549,6 +576,7 @@ pub const ALL_ICONS: &[IconDef] = &[ MOVE_RIGHT, NOTE, NUMBERED_LIST, + OPEN_IN_NEW, PAGE, PEN, PLUS, @@ -560,6 +588,7 @@ pub const ALL_ICONS: &[IconDef] = &[ SELECT, SETTINGS, SHAPE, + SHARE, SIDEBAR, SPLIT_VIEW, SQUARE, @@ -567,6 +596,7 @@ pub const ALL_ICONS: &[IconDef] = &[ TAG, TEX, TEXT, + TOC, TODAY, TOGGLE_DOWN, TOGGLE_RIGHT,