diff --git a/blocksuite/crates/editor/src/overlays/attachment_toolbar.rs b/blocksuite/crates/editor/src/overlays/attachment_toolbar.rs index caffee9459..b47649e32b 100644 --- a/blocksuite/crates/editor/src/overlays/attachment_toolbar.rs +++ b/blocksuite/crates/editor/src/overlays/attachment_toolbar.rs @@ -1,7 +1,7 @@ //! Floating selection toolbar for Page-mode `affine:attachment` blocks, //! matching BlockSuite `0.27.5` `blocks/attachment/src/configs/toolbar.ts`. -use blocksuite_playground_doc::read::BlockContent; +use blocksuite_playground_doc::read::{BlockContent, BlockNode}; use ui::{ AnyElement, ClickEvent, Context, FontWeight, MouseButton, MouseDownEvent, SharedString, div, prelude::*, px, @@ -10,9 +10,9 @@ use ui::{ use crate::theme::{FONT_SANS, Palette, TYPOGRAPHY}; use super::{ - OverlayEvent, OverlayHost, OverlayScene, Overlays, POPOVER_BORDER, POPOVER_EDGE_MARGIN, - POPOVER_GAP, Placement, TOOLBAR_BUTTON, TOOLBAR_GAP, TOOLBAR_PADDING, TOOLBAR_RADIUS, - TOOLBAR_SEPARATOR_MARGIN, emit, icon, place, place_popover, + ChromeIcon, OverlayEvent, OverlayHost, OverlayScene, Overlays, POPOVER_BORDER, + POPOVER_EDGE_MARGIN, POPOVER_GAP, Placement, TOOLBAR_BUTTON, TOOLBAR_GAP, TOOLBAR_PADDING, + TOOLBAR_RADIUS, TOOLBAR_SEPARATOR_MARGIN, chrome_icon, emit, icon, place, place_popover, }; /// Actions offered by the floating `overlay.attachment_toolbar` above a selected `affine:attachment`. @@ -29,6 +29,53 @@ pub enum AttachmentToolbarAction { Delete, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct AttachmentToolbarState { + is_pdf: bool, + embed: bool, + has_source: bool, + has_caption: bool, + download: bool, +} + +fn resolve_attachment_toolbar_state( + node: &BlockNode, + can_download_blobs: bool, + page_readonly: bool, +) -> Option { + let BlockContent::Attachment { + name, + mime_type, + source_id, + caption, + embed, + .. + } = &node.content + else { + return None; + }; + let is_pdf = mime_type.eq_ignore_ascii_case("application/pdf") + || name.to_ascii_lowercase().ends_with(".pdf"); + let is_citation = node.props.get("prop:style").and_then(|v| v.as_str()) == Some("citation"); + let has_footnote_identifier = node + .props + .get("prop:footnoteIdentifier") + .is_some_and(|v| !v.is_null() && v.as_str() != Some("") && v.as_bool() != Some(false)); + let has_source = !source_id.is_empty(); + let download = + !is_pdf && !is_citation && !has_footnote_identifier && can_download_blobs && has_source; + if page_readonly && !(download || is_pdf && has_source) { + return None; + } + Some(AttachmentToolbarState { + is_pdf, + embed: *embed, + has_source, + has_caption: !caption.is_empty(), + download, + }) +} + impl Overlays { pub(super) fn render_attachment_toolbar( &mut self, @@ -49,30 +96,17 @@ impl Overlays { if !scene.anchor_visible(&block_world) { return None; } - let (is_pdf, embed, has_source, has_caption) = - scene - .snapshot - .block(block_id) - .and_then(|n| match &n.content { - BlockContent::Attachment { - name, - mime_type, - source_id, - caption, - embed, - .. - } => { - let is_pdf = mime_type.eq_ignore_ascii_case("application/pdf") - || name.to_ascii_lowercase().ends_with(".pdf"); - Some((is_pdf, *embed, !source_id.is_empty(), !caption.is_empty())) - } - _ => None, - })?; - - let download = !is_pdf && scene.can_download_blobs && has_source; - if scene.page_readonly && !(download || is_pdf && has_source) { - return None; - } + let AttachmentToolbarState { + is_pdf, + embed, + has_source, + has_caption, + download, + } = resolve_attachment_toolbar_state( + scene.snapshot.block(block_id)?, + scene.can_download_blobs, + scene.page_readonly, + )?; let vp = scene.popover_viewport(); let touch = scene.is_touch || scene.form_factor.is_touch_form_factor(); @@ -264,7 +298,7 @@ impl Overlays { "download", false, AttachmentToolbarAction::Download, - icon("DownloadIcon", 16.0, palette.icon).into_any_element(), + chrome_icon(ChromeIcon::Download, 16.0, palette.icon), cx, )); } @@ -284,7 +318,7 @@ impl Overlays { "rename", false, AttachmentToolbarAction::Rename, - icon("EditIcon", 16.0, palette.icon).into_any_element(), + chrome_icon(ChromeIcon::Edit, 16.0, palette.icon), cx, )); } @@ -392,3 +426,199 @@ impl Overlays { Some(root.into_any_element()) } } + +#[cfg(test)] +mod tests { + use std::collections::BTreeMap; + + use blocksuite_playground_doc::read::{BlockContent, BlockNode}; + use serde_json::json; + + use super::{ + AttachmentToolbarAction, AttachmentToolbarState, resolve_attachment_toolbar_state, + }; + + fn attachment_node( + name: &str, + mime_type: &str, + source_id: &str, + props: Vec<(&str, serde_json::Value)>, + ) -> BlockNode { + BlockNode { + id: "att-1".to_string(), + flavour: "affine:attachment".to_string(), + version: Some(1), + content: BlockContent::Attachment { + name: name.to_string(), + size: 1024, + mime_type: mime_type.to_string(), + source_id: source_id.to_string(), + caption: String::new(), + embed: false, + }, + props: props + .into_iter() + .map(|(k, v)| (k.to_string(), v)) + .collect::>(), + children: Vec::new(), + } + } + + fn expected_actions( + state: AttachmentToolbarState, + page_readonly: bool, + ) -> Vec { + let mut actions = Vec::new(); + if state.download { + actions.push(AttachmentToolbarAction::Download); + } + if page_readonly && state.is_pdf { + actions.push(AttachmentToolbarAction::OpenViewer); + } else if !page_readonly { + if !state.is_pdf { + actions.push(AttachmentToolbarAction::Rename); + } else { + actions.push(AttachmentToolbarAction::CardView); + actions.push(AttachmentToolbarAction::EmbedView); + if state.has_source { + actions.push(AttachmentToolbarAction::OpenViewer); + } + } + actions.push(AttachmentToolbarAction::Caption); + actions.push(AttachmentToolbarAction::Copy); + actions.push(AttachmentToolbarAction::Duplicate); + actions.push(AttachmentToolbarAction::Delete); + } + actions + } + + #[test] + fn attachment_toolbar_icons_resolve_non_empty_svgs() { + for name in ["AddTextIcon", "CopyIcon", "DuplicateIcon", "DeleteIcon"] { + let def = ui_tokens::icon_by_name(name) + .unwrap_or_else(|| panic!("attachment toolbar icon `{name}` missing in ui_tokens")); + assert!( + def.svg.contains("