mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 19:25:00 +03:00
fix(editor): render attachment toolbar icons and hide citation downloads
Use static ChromeIcon::Download and ChromeIcon::Edit in attachment_toolbar.rs instead of missing ui-tokens icon names. Hide the download action when prop:style equals citation or prop:footnoteIdentifier holds a value to match BlockSuite 0.27.5. Add unit tests for attachment toolbar icon SVGs and action sets.
This commit is contained in:
1 parent
72af0e4117
commit
97a7fe4bd3
1 file changed
+260
-30
@@ -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<AttachmentToolbarState> {
|
||||
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<V: OverlayHost>(
|
||||
&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::<BTreeMap<_, _>>(),
|
||||
children: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn expected_actions(
|
||||
state: AttachmentToolbarState,
|
||||
page_readonly: bool,
|
||||
) -> Vec<AttachmentToolbarAction> {
|
||||
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("<svg"),
|
||||
"attachment toolbar icon `{name}` must resolve to a non-empty SVG"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attachment_toolbar_download_and_actions_match_block_metadata() {
|
||||
let cases = [
|
||||
(
|
||||
"regular file with source",
|
||||
attachment_node("notes.zip", "application/zip", "blob-1", vec![]),
|
||||
true,
|
||||
false,
|
||||
Some(vec![
|
||||
AttachmentToolbarAction::Download,
|
||||
AttachmentToolbarAction::Rename,
|
||||
AttachmentToolbarAction::Caption,
|
||||
AttachmentToolbarAction::Copy,
|
||||
AttachmentToolbarAction::Duplicate,
|
||||
AttachmentToolbarAction::Delete,
|
||||
]),
|
||||
),
|
||||
(
|
||||
"citation style hides download",
|
||||
attachment_node(
|
||||
"paper.pdf.bib",
|
||||
"text/plain",
|
||||
"blob-2",
|
||||
vec![("prop:style", json!("citation"))],
|
||||
),
|
||||
true,
|
||||
false,
|
||||
Some(vec![
|
||||
AttachmentToolbarAction::Rename,
|
||||
AttachmentToolbarAction::Caption,
|
||||
AttachmentToolbarAction::Copy,
|
||||
AttachmentToolbarAction::Duplicate,
|
||||
AttachmentToolbarAction::Delete,
|
||||
]),
|
||||
),
|
||||
(
|
||||
"footnoteIdentifier hides download",
|
||||
attachment_node(
|
||||
"ref.txt",
|
||||
"text/plain",
|
||||
"blob-3",
|
||||
vec![("prop:footnoteIdentifier", json!("1"))],
|
||||
),
|
||||
true,
|
||||
false,
|
||||
Some(vec![
|
||||
AttachmentToolbarAction::Rename,
|
||||
AttachmentToolbarAction::Caption,
|
||||
AttachmentToolbarAction::Copy,
|
||||
AttachmentToolbarAction::Duplicate,
|
||||
AttachmentToolbarAction::Delete,
|
||||
]),
|
||||
),
|
||||
(
|
||||
"null or empty footnoteIdentifier keeps download",
|
||||
attachment_node(
|
||||
"archive.tar.gz",
|
||||
"application/gzip",
|
||||
"blob-4",
|
||||
vec![
|
||||
("prop:style", json!("horizontalThin")),
|
||||
("prop:footnoteIdentifier", json!(null)),
|
||||
],
|
||||
),
|
||||
true,
|
||||
false,
|
||||
Some(vec![
|
||||
AttachmentToolbarAction::Download,
|
||||
AttachmentToolbarAction::Rename,
|
||||
AttachmentToolbarAction::Caption,
|
||||
AttachmentToolbarAction::Copy,
|
||||
AttachmentToolbarAction::Duplicate,
|
||||
AttachmentToolbarAction::Delete,
|
||||
]),
|
||||
),
|
||||
(
|
||||
"pdf shows card/embed/peek without download",
|
||||
attachment_node("spec.pdf", "application/pdf", "blob-pdf", vec![]),
|
||||
true,
|
||||
false,
|
||||
Some(vec![
|
||||
AttachmentToolbarAction::CardView,
|
||||
AttachmentToolbarAction::EmbedView,
|
||||
AttachmentToolbarAction::OpenViewer,
|
||||
AttachmentToolbarAction::Caption,
|
||||
AttachmentToolbarAction::Copy,
|
||||
AttachmentToolbarAction::Duplicate,
|
||||
AttachmentToolbarAction::Delete,
|
||||
]),
|
||||
),
|
||||
(
|
||||
"readonly citation hides toolbar completely",
|
||||
attachment_node(
|
||||
"cite.txt",
|
||||
"text/plain",
|
||||
"blob-5",
|
||||
vec![("prop:style", json!("citation"))],
|
||||
),
|
||||
true,
|
||||
true,
|
||||
None,
|
||||
),
|
||||
(
|
||||
"readonly regular file keeps download only",
|
||||
attachment_node("doc.txt", "text/plain", "blob-6", vec![]),
|
||||
true,
|
||||
true,
|
||||
Some(vec![AttachmentToolbarAction::Download]),
|
||||
),
|
||||
];
|
||||
|
||||
for (label, node, can_download_blobs, page_readonly, want) in cases {
|
||||
let got = resolve_attachment_toolbar_state(&node, can_download_blobs, page_readonly)
|
||||
.map(|s| expected_actions(s, page_readonly));
|
||||
assert_eq!(got, want, "case `{label}`");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user