From 24ed5ec97f0d1f019124bcd80fdb604a2b167815 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Thu, 3 Dec 2026 02:07:44 +0900 Subject: [PATCH] fix(editor): keep dynamic snapshot IDs out of production Wasm Add `ui::SnapshotId` and the `ui::snap_id!` macro. Under `harness`, the macro formats its arguments into the ID. In other builds, `SnapshotId` has zero size. The macro type-checks its arguments in a branch that never runs, so no ID text gets into the binary. `SharedString` gets `From`, so `snapshot` keeps its signature and literal IDs need no change. The toolbar icon button, the toolbar menu row, the context menu row, the block menu row and the image lightbox control take a `SnapshotId`. A caller that gives a plain string does not compile. Remote cursors and selections, presence badges, the doc panel, the record sheet and the block toolbars now build their dynamic IDs with `ui::snap_id!` instead of `format!`. `check-snapshot-prefixes.mjs` checks the prefixes of these IDs and accepts more than one Wasm file. --- .../editor/src/edgeless/remote_presence.rs | 11 +-- .../crates/editor/src/overlays/block_menu.rs | 14 +-- .../editor/src/overlays/bookmark_toolbar.rs | 20 ++-- .../editor/src/overlays/callout_bar/mod.rs | 14 +-- .../editor/src/overlays/context_menu.rs | 12 +-- .../editor/src/overlays/embed_toolbar.rs | 20 ++-- .../editor/src/overlays/image_lightbox.rs | 15 ++- .../editor/src/overlays/image_toolbar.rs | 12 +-- .../crates/editor/src/overlays/link_popup.rs | 20 ++-- .../src/overlays/paint_toolbar/dropdowns.rs | 18 ++-- .../editor/src/overlays/paint_toolbar/mod.rs | 10 +- .../editor/src/overlays/toolbar_chrome/mod.rs | 6 +- .../crates/playground/src/chrome/topbar.rs | 2 +- blocksuite/crates/playground/src/doc_panel.rs | 11 +-- .../view_mobile/record_sheet/render.rs | 18 +--- .../scripts/check-snapshot-prefixes.mjs | 64 ++++++++----- packages/ui/ui/src/lib.rs | 2 + packages/ui/ui/src/snapshot_id.rs | 94 +++++++++++++++++++ 18 files changed, 227 insertions(+), 136 deletions(-) create mode 100644 packages/ui/ui/src/snapshot_id.rs diff --git a/blocksuite/crates/editor/src/edgeless/remote_presence.rs b/blocksuite/crates/editor/src/edgeless/remote_presence.rs index f2f97c5179..fa313309a5 100644 --- a/blocksuite/crates/editor/src/edgeless/remote_presence.rs +++ b/blocksuite/crates/editor/src/edgeless/remote_presence.rs @@ -2,8 +2,8 @@ //! real-time mouse arrow cursor, colored name pill, and `3px` colored selection outlines. use ui::{ - AnyElement, BorderStyle, Bounds, Corners, Edges, FontWeight, Hsla, PathBuilder, SharedString, - canvas, div, point, prelude::*, px, + AnyElement, BorderStyle, Bounds, Corners, Edges, FontWeight, Hsla, PathBuilder, canvas, div, + point, prelude::*, px, }; use crate::theme::TYPOGRAPHY; @@ -52,10 +52,7 @@ pub fn build_remote_presence_overlay( continue; } let peer_color = peer.color; - let snap_id = SharedString::from(format!( - "edgeless.remote_selection.{}.{}", - peer.client_id, sel_id - )); + let snap_id = ui::snap_id!("edgeless.remote_selection.{}.{}", peer.client_id, sel_id); let fully_inside = x >= 0.0 && y >= 0.0 && x + w <= viewport_width && y + h <= viewport_height; let mut outline = div() @@ -123,7 +120,7 @@ pub fn build_remote_presence_overlay( } else { peer.name.clone() }; - let snap_id = SharedString::from(format!("edgeless.remote_cursor.{}", peer.client_id)); + let snap_id = ui::snap_id!("edgeless.remote_cursor.{}", peer.client_id); let fully_inside = cx >= 0.0 && cy >= 0.0 && cx + 96.0 <= viewport_width diff --git a/blocksuite/crates/editor/src/overlays/block_menu.rs b/blocksuite/crates/editor/src/overlays/block_menu.rs index 04942dcba4..0f951daa77 100644 --- a/blocksuite/crates/editor/src/overlays/block_menu.rs +++ b/blocksuite/crates/editor/src/overlays/block_menu.rs @@ -176,7 +176,7 @@ pub(super) fn render_block_menu( let make_row = |idx: usize, id: &'static str, - snap_id: &'static str, + snap_id: ui::SnapshotId, label: &'static str, icon_name: &'static str, icon_turn: f32, @@ -348,7 +348,7 @@ pub(super) fn render_block_menu( .child(make_row( 0, "blk-move-up", - "overlay.block_menu.move_up", + ui::snap_id!("overlay.block_menu.move_up"), "Move Up", "ToggleDownIcon", std::f32::consts::PI, @@ -360,7 +360,7 @@ pub(super) fn render_block_menu( .child(make_row( 1, "blk-move-down", - "overlay.block_menu.move_down", + ui::snap_id!("overlay.block_menu.move_down"), "Move Down", "ToggleDownIcon", 0.0, @@ -372,7 +372,7 @@ pub(super) fn render_block_menu( .child(make_row( 2, "blk-duplicate", - "overlay.block_menu.duplicate", + ui::snap_id!("overlay.block_menu.duplicate"), "Duplicate", "DuplicateIcon", 0.0, @@ -384,7 +384,7 @@ pub(super) fn render_block_menu( .child(make_row( 3, "blk-copy", - "overlay.block_menu.copy", + ui::snap_id!("overlay.block_menu.copy"), "Copy", "CopyIcon", 0.0, @@ -397,7 +397,7 @@ pub(super) fn render_block_menu( .child(make_row( 4, "blk-delete", - "overlay.block_menu.delete", + ui::snap_id!("overlay.block_menu.delete"), "Delete", "DeleteIcon", 0.0, @@ -450,7 +450,7 @@ pub(super) fn render_block_menu( } })) .snapshot( - SharedString::from(format!("overlay.block_menu.turn_into.{}", item.id)), + ui::snap_id!("overlay.block_menu.turn_into.{}", item.id), Some("overlay.block_menu.turn_into_menu"), ) .snapshot_text(item.title) diff --git a/blocksuite/crates/editor/src/overlays/bookmark_toolbar.rs b/blocksuite/crates/editor/src/overlays/bookmark_toolbar.rs index e249ecad91..e3315b055b 100644 --- a/blocksuite/crates/editor/src/overlays/bookmark_toolbar.rs +++ b/blocksuite/crates/editor/src/overlays/bookmark_toolbar.rs @@ -293,7 +293,7 @@ impl Overlays { let cap_btn = toolbar_icon_button( "bm-tb-caption", - "overlay.bookmark_toolbar.caption", + ui::snap_id!("overlay.bookmark_toolbar.caption"), "overlay.bookmark_toolbar", "Caption", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -319,7 +319,7 @@ impl Overlays { more_open, toolbar_icon_button( "bm-tb-more", - "overlay.bookmark_toolbar.more", + ui::snap_id!("overlay.bookmark_toolbar.more"), "overlay.bookmark_toolbar", "More", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -340,7 +340,7 @@ impl Overlays { rect, 1.0, true, - format_args!("overlay.bookmark_toolbar"), + ui::snap_id!("overlay.bookmark_toolbar"), palette, ) .child( @@ -399,7 +399,7 @@ impl Overlays { menu_placed.rect, 1.0, true, - "overlay.bookmark_toolbar.view_menu", + ui::snap_id!("overlay.bookmark_toolbar.view_menu"), "overlay.bookmark_toolbar", palette, ); @@ -415,7 +415,7 @@ impl Overlays { }; panel = panel.child(toolbar_menu_row( format!("bm-view-{suffix}"), - format_args!("overlay.bookmark_toolbar.{suffix}"), + ui::snap_id!("overlay.bookmark_toolbar.{suffix}"), "overlay.bookmark_toolbar.view_menu", label, chrome_icon(ChromeIcon::Bookmark, TOOLBAR_MENU_ICON, palette.icon), @@ -460,14 +460,14 @@ impl Overlays { menu_placed.rect, 1.0, true, - "overlay.bookmark_toolbar.style_menu", + ui::snap_id!("overlay.bookmark_toolbar.style_menu"), "overlay.bookmark_toolbar", palette, ); for (suffix, label, style) in rows { panel = panel.child(toolbar_menu_row( format!("bm-style-{suffix}"), - format_args!("overlay.bookmark_toolbar.style.{suffix}"), + ui::snap_id!("overlay.bookmark_toolbar.style.{suffix}"), "overlay.bookmark_toolbar.style_menu", label, chrome_icon(ChromeIcon::Bookmark, TOOLBAR_MENU_ICON, palette.icon), @@ -509,7 +509,7 @@ impl Overlays { menu_placed.rect, 1.0, true, - "overlay.bookmark_toolbar.more_menu", + ui::snap_id!("overlay.bookmark_toolbar.more_menu"), "overlay.bookmark_toolbar", palette, ); @@ -524,7 +524,7 @@ impl Overlays { ] { panel = panel.child(toolbar_menu_row( format!("bm-more-{suffix}"), - format_args!("overlay.bookmark_toolbar.{suffix}"), + ui::snap_id!("overlay.bookmark_toolbar.{suffix}"), "overlay.bookmark_toolbar.more_menu", label, icon(icon_name, TOOLBAR_MENU_ICON, palette.icon).into_any_element(), @@ -542,7 +542,7 @@ impl Overlays { panel = panel.child(menu_horizontal_separator(palette)); panel = panel.child(toolbar_menu_row( "bm-more-delete", - "overlay.bookmark_toolbar.delete", + ui::snap_id!("overlay.bookmark_toolbar.delete"), "overlay.bookmark_toolbar.more_menu", "Delete", icon("DeleteIcon", TOOLBAR_MENU_ICON, palette.icon).into_any_element(), diff --git a/blocksuite/crates/editor/src/overlays/callout_bar/mod.rs b/blocksuite/crates/editor/src/overlays/callout_bar/mod.rs index 3852047fd6..a135ef2720 100644 --- a/blocksuite/crates/editor/src/overlays/callout_bar/mod.rs +++ b/blocksuite/crates/editor/src/overlays/callout_bar/mod.rs @@ -308,7 +308,7 @@ impl Overlays { if fully_visible { el = el .snapshot( - format_args!("overlay.callout_toolbar.bg.{name}"), + ui::snap_id!("overlay.callout_toolbar.bg.{name}"), Some("overlay.callout_toolbar"), ) .snapshot_focused(selected) @@ -498,7 +498,7 @@ impl Overlays { more_open, toolbar_icon_button( "callout-tb-more", - "overlay.callout_toolbar.more", + ui::snap_id!("overlay.callout_toolbar.more"), "overlay.callout_toolbar", "More", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -519,7 +519,7 @@ impl Overlays { rect, 1.0, true, - format_args!("overlay.callout_toolbar"), + ui::snap_id!("overlay.callout_toolbar"), palette, ) .child( @@ -559,7 +559,7 @@ impl Overlays { placed.rect, 1.0, true, - "overlay.callout_toolbar.color_menu", + ui::snap_id!("overlay.callout_toolbar.color_menu"), "overlay.callout_toolbar", palette, ) @@ -592,7 +592,7 @@ impl Overlays { }; panel = panel.child(toolbar_menu_row( format!("callout-bg-{name}"), - format_args!("overlay.callout_toolbar.bg.{name}"), + ui::snap_id!("overlay.callout_toolbar.bg.{name}"), "overlay.callout_toolbar.color_menu", SharedString::from(label), duotone_text_badge(palette.text_primary, fill, border), @@ -633,13 +633,13 @@ impl Overlays { placed.rect, 1.0, true, - "overlay.callout_toolbar.more_menu", + ui::snap_id!("overlay.callout_toolbar.more_menu"), "overlay.callout_toolbar", palette, ) .child(toolbar_menu_row( "callout-tb-delete", - "overlay.callout_toolbar.delete", + ui::snap_id!("overlay.callout_toolbar.delete"), "overlay.callout_toolbar.more_menu", "Delete", icon("DeleteIcon", TOOLBAR_MENU_ICON, palette.icon).into_any_element(), diff --git a/blocksuite/crates/editor/src/overlays/context_menu.rs b/blocksuite/crates/editor/src/overlays/context_menu.rs index d974b0ab70..9ec34b7938 100644 --- a/blocksuite/crates/editor/src/overlays/context_menu.rs +++ b/blocksuite/crates/editor/src/overlays/context_menu.rs @@ -215,7 +215,7 @@ pub(super) fn render_context_menu( let selected_idx = menu.selected; let make_row = |idx: usize, id: &'static str, - snap_id: &'static str, + snap_id: ui::SnapshotId, label: &'static str, shortcut: Option<&'static str>, icon_name: Option<&'static str>, @@ -313,7 +313,7 @@ pub(super) fn render_context_menu( .child(make_row( 0, "ctx-copy", - "overlay.context_menu.copy", + ui::snap_id!("overlay.context_menu.copy"), "Copy", Some("Ctrl/Cmd+C"), Some("CopyIcon"), @@ -324,7 +324,7 @@ pub(super) fn render_context_menu( .child(make_row( 1, "ctx-cut", - "overlay.context_menu.cut", + ui::snap_id!("overlay.context_menu.cut"), "Cut", Some("Ctrl/Cmd+X"), None, @@ -335,7 +335,7 @@ pub(super) fn render_context_menu( .child(make_row( 2, "ctx-paste", - "overlay.context_menu.paste", + ui::snap_id!("overlay.context_menu.paste"), "Paste", Some("Ctrl/Cmd+V"), None, @@ -350,7 +350,7 @@ pub(super) fn render_context_menu( .child(make_row( 3, "ctx-open-link", - "overlay.context_menu.open_link", + ui::snap_id!("overlay.context_menu.open_link"), "Open Link", None, Some("LinkIcon"), @@ -361,7 +361,7 @@ pub(super) fn render_context_menu( .child(make_row( 4, "ctx-copy-link", - "overlay.context_menu.copy_link", + ui::snap_id!("overlay.context_menu.copy_link"), "Copy Link", None, Some("CopyIcon"), diff --git a/blocksuite/crates/editor/src/overlays/embed_toolbar.rs b/blocksuite/crates/editor/src/overlays/embed_toolbar.rs index 90860ba9f3..5fd1e02a2e 100644 --- a/blocksuite/crates/editor/src/overlays/embed_toolbar.rs +++ b/blocksuite/crates/editor/src/overlays/embed_toolbar.rs @@ -326,7 +326,7 @@ impl Overlays { }; groups.push(vec![toolbar_icon_button( "embed-tb-caption", - "overlay.embed_toolbar.caption", + ui::snap_id!("overlay.embed_toolbar.caption"), "overlay.embed_toolbar", "Caption", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -352,7 +352,7 @@ impl Overlays { more_open, toolbar_icon_button( "embed-tb-more", - "overlay.embed_toolbar.more", + ui::snap_id!("overlay.embed_toolbar.more"), "overlay.embed_toolbar", "More", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -388,7 +388,7 @@ impl Overlays { rect, 1.0, true, - format_args!("overlay.embed_toolbar"), + ui::snap_id!("overlay.embed_toolbar"), palette, ) .child( @@ -447,14 +447,14 @@ impl Overlays { menu_placed.rect, 1.0, true, - "overlay.embed_toolbar.view_menu", + ui::snap_id!("overlay.embed_toolbar.view_menu"), "overlay.embed_toolbar", palette, ); for (suffix, label, selected, action) in rows { panel = panel.child(toolbar_menu_row( format!("embed-view-{suffix}"), - format_args!("overlay.embed_toolbar.view.{suffix}"), + ui::snap_id!("overlay.embed_toolbar.view.{suffix}"), "overlay.embed_toolbar.view_menu", label, chrome_icon(ChromeIcon::Bookmark, TOOLBAR_MENU_ICON, palette.icon), @@ -502,14 +502,14 @@ impl Overlays { menu_placed.rect, 1.0, true, - "overlay.embed_toolbar.style_menu", + ui::snap_id!("overlay.embed_toolbar.style_menu"), "overlay.embed_toolbar", palette, ); for (suffix, label, style) in rows { panel = panel.child(toolbar_menu_row( format!("embed-style-{suffix}"), - format_args!("overlay.embed_toolbar.style.{suffix}"), + ui::snap_id!("overlay.embed_toolbar.style.{suffix}"), "overlay.embed_toolbar.style_menu", label, chrome_icon(ChromeIcon::Bookmark, TOOLBAR_MENU_ICON, palette.icon), @@ -551,7 +551,7 @@ impl Overlays { menu_placed.rect, 1.0, true, - "overlay.embed_toolbar.more_menu", + ui::snap_id!("overlay.embed_toolbar.more_menu"), "overlay.embed_toolbar", palette, ); @@ -566,7 +566,7 @@ impl Overlays { ] { panel = panel.child(toolbar_menu_row( format!("embed-more-{suffix}"), - format_args!("overlay.embed_toolbar.more.{suffix}"), + ui::snap_id!("overlay.embed_toolbar.more.{suffix}"), "overlay.embed_toolbar.more_menu", label, icon(icon_name, TOOLBAR_MENU_ICON, palette.icon).into_any_element(), @@ -584,7 +584,7 @@ impl Overlays { panel = panel.child(menu_horizontal_separator(palette)); panel = panel.child(toolbar_menu_row( "embed-more-delete", - "overlay.embed_toolbar.more.delete", + ui::snap_id!("overlay.embed_toolbar.more.delete"), "overlay.embed_toolbar.more_menu", "Delete", icon("DeleteIcon", TOOLBAR_MENU_ICON, palette.icon).into_any_element(), diff --git a/blocksuite/crates/editor/src/overlays/image_lightbox.rs b/blocksuite/crates/editor/src/overlays/image_lightbox.rs index 8910462804..2d001a16b0 100644 --- a/blocksuite/crates/editor/src/overlays/image_lightbox.rs +++ b/blocksuite/crates/editor/src/overlays/image_lightbox.rs @@ -5,7 +5,7 @@ use blocksuite_playground_doc::read::BlockContent; use ui::{ AnyElement, ClickEvent, Context, FontWeight, MouseButton, MouseDownEvent, MouseMoveEvent, - MouseUpEvent, PinchEvent, ScrollDelta, ScrollEvent, SharedString, div, img, prelude::*, px, + MouseUpEvent, PinchEvent, ScrollDelta, ScrollEvent, div, img, prelude::*, px, }; use crate::{ @@ -177,7 +177,7 @@ impl Overlays { let pct_label = format!("{}%", (zoom * 100.0).round() as i32); let make_ctrl = |id: &'static str, - snap_suffix: &'static str, + snap_id: ui::SnapshotId, label: String, ev: OverlayEvent, cx: &mut Context| { @@ -194,10 +194,7 @@ impl Overlays { .text_size(px(TYPOGRAPHY.font_xs_px)) .font_weight(FontWeight::MEDIUM) .text_color(palette.text_primary) - .snapshot( - SharedString::from(format!("overlay.image_lightbox.{snap_suffix}")), - Some("overlay.image_lightbox"), - ) + .snapshot(snap_id, Some("overlay.image_lightbox")) .snapshot_text(label.clone()) .child(label); if interactive { @@ -224,21 +221,21 @@ impl Overlays { .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) .child(make_ctrl( "lightbox-zoom-out", - "zoom_out", + ui::snap_id!("overlay.image_lightbox.zoom_out"), "\u{2212}".into(), OverlayEvent::ImageLightboxZoom(-0.25), cx, )) .child(make_ctrl( "lightbox-zoom-reset", - "zoom_reset", + ui::snap_id!("overlay.image_lightbox.zoom_reset"), pct_label, OverlayEvent::ImageLightboxZoom(0.0), cx, )) .child(make_ctrl( "lightbox-zoom-in", - "zoom_in", + ui::snap_id!("overlay.image_lightbox.zoom_in"), "+".into(), OverlayEvent::ImageLightboxZoom(0.25), cx, diff --git a/blocksuite/crates/editor/src/overlays/image_toolbar.rs b/blocksuite/crates/editor/src/overlays/image_toolbar.rs index 0165c691b3..eff89d840b 100644 --- a/blocksuite/crates/editor/src/overlays/image_toolbar.rs +++ b/blocksuite/crates/editor/src/overlays/image_toolbar.rs @@ -230,7 +230,7 @@ impl Overlays { if fully_visible { el = el .snapshot( - format_args!("overlay.image_toolbar.{id_suffix}"), + ui::snap_id!("overlay.image_toolbar.{id_suffix}"), Some("overlay.image_toolbar"), ) .snapshot_focused(active); @@ -406,7 +406,7 @@ impl Overlays { .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) .on_click(emit(cx, move |_: &ClickEvent| event.clone())) .snapshot( - format_args!("overlay.image_toolbar.{id_suffix}"), + ui::snap_id!("overlay.image_toolbar.{id_suffix}"), Some("overlay.image_toolbar"), ) .snapshot_focused(active) @@ -527,7 +527,7 @@ impl Overlays { placed.rect, 1.0, true, - "overlay.image_toolbar.more_menu", + ui::snap_id!("overlay.image_toolbar.more_menu"), "overlay.image_toolbar", palette, ); @@ -542,7 +542,7 @@ impl Overlays { ] { panel = panel.child(toolbar_menu_row( format!("img-more-{suffix}"), - format_args!("overlay.image_toolbar.{suffix}"), + ui::snap_id!("overlay.image_toolbar.{suffix}"), "overlay.image_toolbar.more_menu", label, icon(icon_name, TOOLBAR_MENU_ICON, palette.icon).into_any_element(), @@ -561,7 +561,7 @@ impl Overlays { panel = panel.child(menu_horizontal_separator(palette)); panel = panel.child(toolbar_menu_row( "img-more-card-view", - "overlay.image_toolbar.card_view", + ui::snap_id!("overlay.image_toolbar.card_view"), "overlay.image_toolbar.more_menu", "Turn into card view", chrome_icon(ChromeIcon::Bookmark, TOOLBAR_MENU_ICON, palette.icon), @@ -579,7 +579,7 @@ impl Overlays { panel = panel.child(menu_horizontal_separator(palette)); panel = panel.child(toolbar_menu_row( "img-more-delete", - "overlay.image_toolbar.delete", + ui::snap_id!("overlay.image_toolbar.delete"), "overlay.image_toolbar.more_menu", "Delete", icon("DeleteIcon", TOOLBAR_MENU_ICON, palette.icon).into_any_element(), diff --git a/blocksuite/crates/editor/src/overlays/link_popup.rs b/blocksuite/crates/editor/src/overlays/link_popup.rs index a78b6ffa77..5ce1ff9000 100644 --- a/blocksuite/crates/editor/src/overlays/link_popup.rs +++ b/blocksuite/crates/editor/src/overlays/link_popup.rs @@ -366,7 +366,7 @@ impl Overlays { if scene.page_readonly { groups.push(vec![toolbar_icon_button( "link-popup-copy", - "overlay.link_popup.copy", + ui::snap_id!("overlay.link_popup.copy"), "overlay.link_popup", "Copy link", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -380,7 +380,7 @@ impl Overlays { } else { let mut g_actions = vec![toolbar_icon_button( "link-popup-copy", - "overlay.link_popup.copy", + ui::snap_id!("overlay.link_popup.copy"), "overlay.link_popup", "Copy link", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -395,7 +395,7 @@ impl Overlays { if is_ref { g_actions.push(toolbar_icon_button( "link-popup-alias", - "overlay.link_popup.alias", + ui::snap_id!("overlay.link_popup.alias"), "overlay.link_popup", "Edit Alias", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -409,7 +409,7 @@ impl Overlays { } else { g_actions.push(toolbar_icon_button( "link-popup-edit", - "overlay.link_popup.edit", + ui::snap_id!("overlay.link_popup.edit"), "overlay.link_popup", "Edit Description", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -474,7 +474,7 @@ impl Overlays { more_open, toolbar_icon_button( "link-popup-more", - "overlay.link_popup.more", + ui::snap_id!("overlay.link_popup.more"), "overlay.link_popup", "More", FlowRect::new(cursor_x, btn_y, btn, btn), @@ -531,7 +531,7 @@ impl Overlays { menu_placed.rect, progress, interactive, - "overlay.link_popup.view_menu", + ui::snap_id!("overlay.link_popup.view_menu"), "overlay.link_popup", palette, ); @@ -542,7 +542,7 @@ impl Overlays { }; panel = panel.child(toolbar_menu_row( format!("link-popup-view-{suffix}"), - format_args!("overlay.link_popup.{suffix}"), + ui::snap_id!("overlay.link_popup.{suffix}"), "overlay.link_popup.view_menu", label, chrome_icon(ChromeIcon::Bookmark, TOOLBAR_MENU_ICON, palette.icon), @@ -580,13 +580,13 @@ impl Overlays { menu_placed.rect, progress, interactive, - "overlay.link_popup.more_menu", + ui::snap_id!("overlay.link_popup.more_menu"), "overlay.link_popup", palette, ) .child(toolbar_menu_row( "link-popup-unlink", - "overlay.link_popup.unlink", + ui::snap_id!("overlay.link_popup.unlink"), "overlay.link_popup.more_menu", unlink_label, if is_ref { @@ -625,7 +625,7 @@ impl Overlays { placed_rect, progress, interactive, - "overlay.link_popup", + ui::snap_id!("overlay.link_popup"), palette, ) .child(self.toolbar_bounds.measure("link", view_open || more_open)) diff --git a/blocksuite/crates/editor/src/overlays/paint_toolbar/dropdowns.rs b/blocksuite/crates/editor/src/overlays/paint_toolbar/dropdowns.rs index 8a1115fa00..59f6c3d129 100644 --- a/blocksuite/crates/editor/src/overlays/paint_toolbar/dropdowns.rs +++ b/blocksuite/crates/editor/src/overlays/paint_toolbar/dropdowns.rs @@ -43,7 +43,7 @@ pub(super) fn render_turn_into_dropdown( placed.rect, progress, interactive, - "overlay.format_toolbar.turn_into_menu", + ui::snap_id!("overlay.format_toolbar.turn_into_menu"), "overlay.format_toolbar", palette, ); @@ -51,7 +51,7 @@ pub(super) fn render_turn_into_dropdown( let selected = current_id == Some(item.id); let row = toolbar_menu_row( format!("fmt-turn-{}", item.id), - format_args!("overlay.format_toolbar.turn_into.{}", item.id), + ui::snap_id!("overlay.format_toolbar.turn_into.{}", item.id), "overlay.format_toolbar.turn_into_menu", item.title, icon(item.icon, TOOLBAR_MENU_ICON, palette.icon).into_any_element(), @@ -113,7 +113,7 @@ pub(super) fn render_align_dropdown( placed.rect, progress, interactive, - "overlay.format_toolbar.align_menu", + ui::snap_id!("overlay.format_toolbar.align_menu"), "overlay.format_toolbar", palette, ); @@ -121,7 +121,7 @@ pub(super) fn render_align_dropdown( let selected = current_align == Some(align); let row = toolbar_menu_row( format!("fmt-{id}"), - format_args!("overlay.format_toolbar.{id}"), + ui::snap_id!("overlay.format_toolbar.{id}"), "overlay.format_toolbar.align_menu", label, align_glyph(align, palette.icon).into_any_element(), @@ -177,7 +177,7 @@ pub(super) fn render_highlight_dropdown( placed.rect, progress, interactive, - "overlay.format_toolbar.highlight_menu", + ui::snap_id!("overlay.format_toolbar.highlight_menu"), "overlay.format_toolbar", palette, ); @@ -242,7 +242,7 @@ pub(super) fn render_highlight_dropdown( }; let row = toolbar_menu_row( format!("fmt-hl-{section_id}-{}", color.id()), - format_args!( + ui::snap_id!( "overlay.format_toolbar.highlight.{section_id}.{}", color.id() ), @@ -287,7 +287,7 @@ pub(super) fn render_more_dropdown( placed.rect, progress, interactive, - "overlay.format_toolbar.more_menu", + ui::snap_id!("overlay.format_toolbar.more_menu"), "overlay.format_toolbar", palette, ); @@ -302,7 +302,7 @@ pub(super) fn render_more_dropdown( ] { panel = panel.child(toolbar_menu_row( format!("fmt-more-{id}"), - format_args!("overlay.format_toolbar.more.{id}"), + ui::snap_id!("overlay.format_toolbar.more.{id}"), "overlay.format_toolbar.more_menu", label, icon(icon_name, TOOLBAR_MENU_ICON, palette.icon).into_any_element(), @@ -317,7 +317,7 @@ pub(super) fn render_more_dropdown( panel = panel.child(menu_horizontal_separator(palette)); panel = panel.child(toolbar_menu_row( "fmt-more-delete", - "overlay.format_toolbar.more.delete", + ui::snap_id!("overlay.format_toolbar.more.delete"), "overlay.format_toolbar.more_menu", "Delete", icon("DeleteIcon", TOOLBAR_MENU_ICON, palette.icon).into_any_element(), diff --git a/blocksuite/crates/editor/src/overlays/paint_toolbar/mod.rs b/blocksuite/crates/editor/src/overlays/paint_toolbar/mod.rs index 236d607920..0224df03d1 100644 --- a/blocksuite/crates/editor/src/overlays/paint_toolbar/mod.rs +++ b/blocksuite/crates/editor/src/overlays/paint_toolbar/mod.rs @@ -234,7 +234,7 @@ impl Overlays { .justify_center() .gap(px(1.0)) .snapshot( - format_args!("overlay.format_toolbar.{}", action.id()), + ui::snap_id!("overlay.format_toolbar.{}", action.id()), Some("overlay.format_toolbar"), ) .snapshot_focused(active) @@ -331,7 +331,7 @@ impl Overlays { }; g_inline.push(toolbar_icon_button( action.id(), - format_args!("overlay.format_toolbar.{}", action.id()), + ui::snap_id!("overlay.format_toolbar.{}", action.id()), "overlay.format_toolbar", action.label(), FlowRect::new(bx, btn_y, btn, btn), @@ -378,7 +378,7 @@ impl Overlays { if can_create_table { g_conv.push(toolbar_icon_button( FormatAction::CreateTable.id(), - "overlay.format_toolbar.create_table", + ui::snap_id!("overlay.format_toolbar.create_table"), "overlay.format_toolbar", FormatAction::CreateTable.label(), FlowRect::new(cursor_x, btn_y, btn, btn), @@ -393,7 +393,7 @@ impl Overlays { } g_conv.push(toolbar_icon_button( FormatAction::CreateLinkedDoc.id(), - "overlay.format_toolbar.create_linked_doc", + ui::snap_id!("overlay.format_toolbar.create_linked_doc"), "overlay.format_toolbar", FormatAction::CreateLinkedDoc.label(), FlowRect::new(cursor_x, btn_y, btn, btn), @@ -419,7 +419,7 @@ impl Overlays { more_active, toolbar_icon_button( FormatAction::More.id(), - "overlay.format_toolbar.more", + ui::snap_id!("overlay.format_toolbar.more"), "overlay.format_toolbar", FormatAction::More.label(), FlowRect::new(cursor_x, btn_y, btn, btn), diff --git a/blocksuite/crates/editor/src/overlays/toolbar_chrome/mod.rs b/blocksuite/crates/editor/src/overlays/toolbar_chrome/mod.rs index 0493c242b9..1abc0cf60d 100644 --- a/blocksuite/crates/editor/src/overlays/toolbar_chrome/mod.rs +++ b/blocksuite/crates/editor/src/overlays/toolbar_chrome/mod.rs @@ -18,7 +18,7 @@ pub use icons::{ChromeIcon, chrome_icon, duotone_text_badge}; use ui::{ AnyElement, ClickEvent, Context, Div, FontWeight, Hsla, MouseButton, MouseDownEvent, - MouseMoveEvent, ParentElement, SharedString, Stateful, div, prelude::*, px, + MouseMoveEvent, ParentElement, SharedString, SnapshotId, Stateful, div, prelude::*, px, }; use crate::{ @@ -371,7 +371,7 @@ pub fn append_toolbar_groups( #[allow(clippy::too_many_arguments)] pub fn toolbar_icon_button( id: impl Into, - snap_id: impl Into, + snap_id: SnapshotId, snap_parent: &'static str, tooltip: &'static str, anchor_screen: FlowRect, @@ -512,7 +512,7 @@ impl Overlays { #[allow(clippy::too_many_arguments)] pub fn toolbar_menu_row( id: impl Into, - snap_id: impl Into, + snap_id: SnapshotId, snap_parent: &'static str, label: impl Into, leading: AnyElement, diff --git a/blocksuite/crates/playground/src/chrome/topbar.rs b/blocksuite/crates/playground/src/chrome/topbar.rs index 62dcc1375f..7ca298bcd6 100644 --- a/blocksuite/crates/playground/src/chrome/topbar.rs +++ b/blocksuite/crates/playground/src/chrome/topbar.rs @@ -224,7 +224,7 @@ impl PlaygroundView { ); } for presence in &presences[..shown] { - let id = SharedString::from(format!("chrome.presence.{}", presence.client_id)); + let id = ui::snap_id!("chrome.presence.{}", presence.client_id); let name = SharedString::from(presence.name.clone()); let badge = if compact { let initial: String = presence diff --git a/blocksuite/crates/playground/src/doc_panel.rs b/blocksuite/crates/playground/src/doc_panel.rs index c65912900b..7eb734a2ed 100644 --- a/blocksuite/crates/playground/src/doc_panel.rs +++ b/blocksuite/crates/playground/src/doc_panel.rs @@ -327,7 +327,7 @@ impl PlaygroundView { cx.notify(); })) .snapshot( - SharedString::from(format!("overlay.doc_panel.tab.{id}")), + ui::snap_id!("overlay.doc_panel.tab.{id}"), Some("overlay.doc_panel"), ) .snapshot_focused(is_active) @@ -375,8 +375,7 @@ impl PlaygroundView { let indent = f32::from(h.level.saturating_sub(1)) * 12.0; let target_id = h.block_id.clone(); let label = SharedString::from(h.text.clone()); - let snap_id = - SharedString::from(format!("overlay.doc_panel.heading.{}", h.block_id)); + let snap_id = ui::snap_id!("overlay.doc_panel.heading.{}", h.block_id); list_inner = list_inner.child( div() .id(SharedString::from(format!("toc-{}", h.block_id))) @@ -441,10 +440,8 @@ impl PlaygroundView { ("Headings", stats.headings.to_string()), ("References", stats.linked_docs.len().to_string()), ] { - let snap_id = SharedString::from(format!( - "overlay.doc_panel.stat.{}", - key.to_ascii_lowercase() - )); + let snap_id = + ui::snap_id!("overlay.doc_panel.stat.{}", key.to_ascii_lowercase()); body = body.child( div() .h(px(28.0)) diff --git a/blocksuite/crates/playground/src/overlays/view_mobile/record_sheet/render.rs b/blocksuite/crates/playground/src/overlays/view_mobile/record_sheet/render.rs index 21341b9ada..373ba9e43c 100644 --- a/blocksuite/crates/playground/src/overlays/view_mobile/record_sheet/render.rs +++ b/blocksuite/crates/playground/src/overlays/view_mobile/record_sheet/render.rs @@ -136,9 +136,7 @@ impl PlaygroundView { } if visible_in_snap { el = el.snapshot( - SharedString::from(format!( - "overlay.database_record_sheet.checkbox.{idx}" - )), + ui::snap_id!("overlay.database_record_sheet.checkbox.{idx}"), Some("overlay.database_record_sheet"), ); } @@ -188,9 +186,7 @@ impl PlaygroundView { if visible_in_snap { el = el .snapshot( - SharedString::from(format!( - "overlay.database_record_sheet.progress.{idx}" - )), + ui::snap_id!("overlay.database_record_sheet.progress.{idx}"), Some("overlay.database_record_sheet"), ) .snapshot_text(label.clone()); @@ -271,9 +267,7 @@ impl PlaygroundView { if visible_in_snap { el = el .snapshot( - SharedString::from(format!( - "overlay.database_record_sheet.url.{idx}" - )), + ui::snap_id!("overlay.database_record_sheet.url.{idx}"), Some("overlay.database_record_sheet"), ) .snapshot_text(raw.clone()); @@ -295,9 +289,7 @@ impl PlaygroundView { if visible_in_snap { el = el .snapshot( - SharedString::from(format!( - "overlay.database_record_sheet.field.{idx}" - )), + ui::snap_id!("overlay.database_record_sheet.field.{idx}"), Some("overlay.database_record_sheet"), ) .snapshot_text(raw.clone()); @@ -495,7 +487,7 @@ impl PlaygroundView { ); } body_content = body_content.child(note.snapshot( - format_args!("overlay.database_record_sheet.note.{index}"), + ui::snap_id!("overlay.database_record_sheet.note.{index}"), Some("overlay.database_record_sheet"), )); } diff --git a/packages/frontend/scripts/check-snapshot-prefixes.mjs b/packages/frontend/scripts/check-snapshot-prefixes.mjs index 7ed3c8d187..5b38e82fb2 100644 --- a/packages/frontend/scripts/check-snapshot-prefixes.mjs +++ b/packages/frontend/scripts/check-snapshot-prefixes.mjs @@ -2,12 +2,13 @@ // @ts-check /** - * Verifies that production `playground_bg.wasm` contains zero harness snapshot - * ID prefixes in WebAssembly Section 11 (Data). + * Verifies that production Wasm bundles (`playground_bg.wasm`, + * `affine_desktop_bg.wasm`) contain zero harness snapshot ID prefixes in + * WebAssembly Section 11 (Data). * * Modes: * node scripts/check-snapshot-prefixes.mjs --source-only - * node scripts/check-snapshot-prefixes.mjs + * node scripts/check-snapshot-prefixes.mjs ... */ import { createHash } from 'node:crypto'; @@ -40,7 +41,7 @@ export const ZERO_IN_PRODUCTION = Object.freeze([ 'overlay.embed_toolbar', 'overlay.format_toolbar.', 'overlay.image_toolbar.', - 'overlay.link_popup.', + 'overlay.link_popup', 'overlay.callout_toolbar', 'overlay.callout_icon_picker.', 'overlay.table_toolbar.axis_menu', @@ -55,6 +56,14 @@ export const ZERO_IN_PRODUCTION = Object.freeze([ 'edgeless.mindmap.toggle.', 'edgeless.handle.rotate_', 'edgeless.selection_box', + 'edgeless.remote_cursor.', + 'edgeless.remote_selection.', + 'chrome.presence.', + 'overlay.context_menu.', + 'overlay.block_menu.', + 'overlay.image_lightbox.', + 'overlay.doc_panel.', + 'overlay.database_record_sheet.', ]); const WASM_MAGIC = new Uint8Array([0x00, 0x61, 0x73, 0x6d]); @@ -549,13 +558,12 @@ export function scanWasmDataPrefixes(bytes, prefixes = ZERO_IN_PRODUCTION) { * @param {string[]} argv */ function main(argv) { - if (argv.length !== 1) { + if (argv.length === 0 || (argv.includes('--source-only') && argv.length !== 1)) { throw new Error( - 'Usage: node scripts/check-snapshot-prefixes.mjs --source-only | ' + 'Usage: node scripts/check-snapshot-prefixes.mjs --source-only | ...' ); } - const arg = argv[0]; const sourceResult = verifySourcePrefixes(); if (!sourceResult.ok) { throw new Error( @@ -563,32 +571,36 @@ function main(argv) { ); } - if (arg === '--source-only') { + if (argv[0] === '--source-only') { console.log( `Snapshot prefix source liveness check passed (${ZERO_IN_PRODUCTION.length}/${ZERO_IN_PRODUCTION.length} prefixes found across ${sourceResult.scannedFiles} Rust files).` ); return; } - const wasmPath = resolve(arg); - const wasmBytes = readFileSync(wasmPath); - const scan = scanWasmDataPrefixes(wasmBytes); - - if (!scan.ok) { - const lines = [ - `Forbidden snapshot prefixes found in Wasm Section 11 (Data): ${wasmPath}`, - `SHA-256: ${scan.sha256}`, - ...scan.hits.map( - hit => - ` - ${hit.prefix}: count=${hit.count}, first_segment=${hit.firstSegment}, first_offset=${hit.firstOffset}` - ), - ]; - throw new Error(lines.join('\n')); + /** @type {string[]} */ + const failures = []; + for (const arg of argv) { + const wasmPath = resolve(arg); + const scan = scanWasmDataPrefixes(readFileSync(wasmPath)); + if (!scan.ok) { + failures.push( + `Forbidden snapshot prefixes found in Wasm Section 11 (Data): ${wasmPath}`, + `SHA-256: ${scan.sha256}`, + ...scan.hits.map( + hit => + ` - ${hit.prefix}: count=${hit.count}, first_segment=${hit.firstSegment}, first_offset=${hit.firstOffset}` + ) + ); + continue; + } + console.log( + `Wasm Section 11 snapshot prefix gate passed for ${wasmPath} (${ZERO_IN_PRODUCTION.length}/${ZERO_IN_PRODUCTION.length} prefixes at 0, ${scan.segmentCount} segments, ${scan.totalDataBytes} data bytes, sha256=${scan.sha256}).` + ); + } + if (failures.length > 0) { + throw new Error(failures.join('\n')); } - - console.log( - `Wasm Section 11 snapshot prefix gate passed (${ZERO_IN_PRODUCTION.length}/${ZERO_IN_PRODUCTION.length} prefixes at 0, ${scan.segmentCount} segments, ${scan.totalDataBytes} data bytes, sha256=${scan.sha256}).` - ); } const isMain = diff --git a/packages/ui/ui/src/lib.rs b/packages/ui/ui/src/lib.rs index a71b2b1046..70d0825957 100644 --- a/packages/ui/ui/src/lib.rs +++ b/packages/ui/ui/src/lib.rs @@ -52,6 +52,7 @@ mod platform; mod platform_scheduler; mod scene; mod shared_uri; +mod snapshot_id; mod style; mod styled; mod subscription; @@ -100,6 +101,7 @@ pub use refineable::*; pub use scene::*; pub use shared_string::*; pub use shared_uri::*; +pub use snapshot_id::SnapshotId; pub use style::*; pub use styled::*; pub use subscription::*; diff --git a/packages/ui/ui/src/snapshot_id.rs b/packages/ui/ui/src/snapshot_id.rs new file mode 100644 index 0000000000..49d919c6ff --- /dev/null +++ b/packages/ui/ui/src/snapshot_id.rs @@ -0,0 +1,94 @@ +//! Element snapshot IDs that only snapshot builds keep. +//! +//! Build a [`SnapshotId`] with [`snap_id!`](crate::snap_id) and give it to +//! `InteractiveElement::snapshot`. A helper that forwards an ID to `snapshot` takes a +//! `SnapshotId`, so the ID text never reaches a production binary. + +use crate::SharedString; + +/// The ID of an element snapshot. Under `harness` it holds the ID text. In other builds it has +/// zero size, and [`snap_id!`](crate::snap_id) does not evaluate its arguments. +#[cfg(any(test, feature = "harness"))] +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SnapshotId(SharedString); + +/// The ID of an element snapshot. Under `harness` it holds the ID text. In other builds it has +/// zero size, and [`snap_id!`](crate::snap_id) does not evaluate its arguments. +#[cfg(not(any(test, feature = "harness")))] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct SnapshotId; + +#[cfg(any(test, feature = "harness"))] +impl SnapshotId { + #[doc(hidden)] + pub fn from_args(args: core::fmt::Arguments<'_>) -> Self { + Self(args.into()) + } +} + +#[cfg(not(any(test, feature = "harness")))] +impl SnapshotId { + #[doc(hidden)] + pub const NONE: Self = Self; +} + +impl From for SharedString { + #[cfg(any(test, feature = "harness"))] + fn from(id: SnapshotId) -> Self { + id.0 + } + + #[cfg(not(any(test, feature = "harness")))] + #[inline] + fn from(_: SnapshotId) -> Self { + SharedString::new_static("") + } +} + +/// Builds a [`SnapshotId`] with `format!` syntax, inline captures included. +/// +/// Under `harness` the arguments are formatted. In other builds they are type-checked in a +/// branch that never runs, and the macro gives the zero-size ID. +#[cfg(any(test, feature = "harness"))] +#[macro_export] +macro_rules! snap_id { + ($($arg:tt)+) => { + $crate::SnapshotId::from_args(::core::format_args!($($arg)+)) + }; +} + +/// Builds a [`SnapshotId`] with `format!` syntax, inline captures included. +/// +/// Under `harness` the arguments are formatted. In other builds they are type-checked in a +/// branch that never runs, and the macro gives the zero-size ID. +#[cfg(not(any(test, feature = "harness")))] +#[macro_export] +macro_rules! snap_id { + ($($arg:tt)+) => {{ + if false { + let _ = ::core::format_args!($($arg)+); + } + $crate::SnapshotId::NONE + }}; +} + +#[cfg(test)] +mod tests { + use super::SnapshotId; + use crate::SharedString; + + #[test] + fn snap_id_formats_literals_and_captures() { + let bar = "val"; + let index = 3; + let cases: [(SnapshotId, &str); 4] = [ + (crate::snap_id!("foo.bar"), "foo.bar"), + (crate::snap_id!("foo.{bar}"), "foo.val"), + (crate::snap_id!("foo.{}.{index}", bar), "foo.val.3"), + (crate::snap_id!("foo.{{bar}}"), "foo.{bar}"), + ]; + for (id, expected) in cases { + assert_eq!(SharedString::from(id).as_ref(), expected); + } + } +}