mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
fix(editor): guard code menu actions and translate pane input bounds
This commit is contained in:
1 parent
155f30f3b8
commit
dfc8446ba2
9 files changed
+423
-32
No files matched your search
@@ -12,8 +12,8 @@ use crate::{
|
||||
|
||||
use super::{
|
||||
CONTEXT_MENU_PADDING, CONTEXT_MENU_RADIUS, CONTEXT_MENU_ROW_HEIGHT, OverlayEvent, OverlayHost,
|
||||
POPOVER_BORDER, POPOVER_EDGE_MARGIN, Presence, SLASH_ITEMS, SlashCommand, SlashItem, emit,
|
||||
icon, icon_rotated, place, place_context_menu,
|
||||
OverlayScene, POPOVER_BORDER, POPOVER_EDGE_MARGIN, Presence, SLASH_ITEMS, SlashCommand,
|
||||
SlashItem, emit, icon, icon_rotated, place, place_context_menu,
|
||||
};
|
||||
|
||||
/// Width of the drag-handle block action menu in logical pixels.
|
||||
@@ -318,7 +318,10 @@ pub(super) fn render_block_menu<V: OverlayHost>(
|
||||
cx.stop_propagation();
|
||||
let px_x = f32::from(ev.position.x);
|
||||
let px_y = f32::from(ev.position.y);
|
||||
if handle_bounds.is_some_and(|r| r.contains(px_x, px_y)) {
|
||||
let pane_origin = view.overlay_pane_origin();
|
||||
if handle_bounds.is_some_and(|r| {
|
||||
OverlayScene::rect_to_window(r, pane_origin).contains(px_x, px_y)
|
||||
}) {
|
||||
view.dispatch_overlay(OverlayEvent::CloseBlockMenu, window, cx);
|
||||
}
|
||||
}),
|
||||
@@ -326,8 +329,13 @@ pub(super) fn render_block_menu<V: OverlayHost>(
|
||||
.on_mouse_down_out(cx.listener(move |view, ev: &MouseDownEvent, window, cx| {
|
||||
let px_x = f32::from(ev.position.x);
|
||||
let px_y = f32::from(ev.position.y);
|
||||
if sub_bounds.is_some_and(|r| r.contains(px_x, px_y))
|
||||
|| handle_bounds.is_some_and(|r| r.contains(px_x, px_y))
|
||||
let pane_origin = view.overlay_pane_origin();
|
||||
if [sub_bounds, handle_bounds]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.any(|rect| {
|
||||
OverlayScene::rect_to_window(rect, pane_origin).contains(px_x, px_y)
|
||||
})
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2,20 +2,56 @@
|
||||
|
||||
use blocksuite_playground_doc::read::BlockContent;
|
||||
use ui::{
|
||||
AnyElement, ClickEvent, Context, MouseButton, MouseDownEvent, SharedString, Window, div,
|
||||
AnyElement, App, ClickEvent, Context, MouseButton, MouseDownEvent, SharedString, Window, div,
|
||||
prelude::*, px,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
block_flow::{FormFactor, code_line_number_default},
|
||||
overlays::TOOLBAR_CHROME_RADIUS,
|
||||
theme::Palette,
|
||||
};
|
||||
|
||||
use super::{
|
||||
CODE_MORE_MENU_WIDTH, CodeMoreActionKind, OverlayEvent, OverlayHost, OverlayScene, Overlays,
|
||||
POPOVER_EDGE_MARGIN, POPOVER_GAP, Placement, emit, icon, place, place_popover,
|
||||
POPOVER_EDGE_MARGIN, POPOVER_GAP, Placement, icon, place, place_popover,
|
||||
};
|
||||
|
||||
/// Document identity from the frame that created the listener.
|
||||
#[derive(Clone)]
|
||||
pub(super) struct CodeToolbarTarget {
|
||||
pub(super) block_id: SharedString,
|
||||
doc_id: SharedString,
|
||||
generation: u64,
|
||||
}
|
||||
|
||||
impl CodeToolbarTarget {
|
||||
pub(super) fn new(overlays: &Overlays, scene: &OverlayScene<'_>, block_id: &str) -> Self {
|
||||
Self {
|
||||
block_id: block_id.to_owned().into(),
|
||||
doc_id: scene.snapshot.doc_id.clone().into(),
|
||||
generation: overlays.reset_generation,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn on_click<V: OverlayHost>(
|
||||
&self,
|
||||
cx: &Context<V>,
|
||||
event: OverlayEvent,
|
||||
) -> impl Fn(&ClickEvent, &mut Window, &mut App) + 'static {
|
||||
let target = self.clone();
|
||||
cx.listener(move |view, _: &ClickEvent, window, cx| {
|
||||
if view.code_toolbar_target_is_current(
|
||||
target.generation,
|
||||
&target.doc_id,
|
||||
&target.block_id,
|
||||
) {
|
||||
view.dispatch_overlay(event.clone(), window, cx);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn toggle_switch(on: bool, palette: &Palette) -> AnyElement {
|
||||
let track_bg = if on { palette.brand } else { palette.border };
|
||||
let knob_x = if on { 14.0 } else { 2.0 };
|
||||
@@ -105,6 +141,7 @@ impl Overlays {
|
||||
);
|
||||
menu.rect = Some(placed.rect);
|
||||
|
||||
let target = CodeToolbarTarget::new(self, scene, &block_id);
|
||||
let hover_bg = palette.hover;
|
||||
let err_bg = palette.error_background;
|
||||
let err_fg = palette.error;
|
||||
@@ -119,7 +156,7 @@ impl Overlays {
|
||||
action,
|
||||
};
|
||||
let mut row = div()
|
||||
.id(SharedString::from(format!("code-more-{suffix}")))
|
||||
.id(suffix)
|
||||
.h(px(row_h))
|
||||
.px(px(8.0))
|
||||
.rounded(px(4.0))
|
||||
@@ -152,7 +189,7 @@ impl Overlays {
|
||||
}
|
||||
})
|
||||
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
|
||||
.on_click(emit(cx, move |_: &ClickEvent| event.clone()));
|
||||
.on_click(target.on_click(cx, event));
|
||||
}
|
||||
row
|
||||
};
|
||||
@@ -162,7 +199,7 @@ impl Overlays {
|
||||
.flex()
|
||||
.flex_col()
|
||||
.p(px(pad))
|
||||
.rounded(px(8.0))
|
||||
.rounded(px(TOOLBAR_CHROME_RADIUS))
|
||||
.bg(palette.panel)
|
||||
.border_1()
|
||||
.border_color(palette.border)
|
||||
@@ -197,7 +234,7 @@ impl Overlays {
|
||||
.child(divider());
|
||||
}
|
||||
|
||||
let generation = self.reset_generation;
|
||||
let generation = target.generation;
|
||||
let doc_id = &scene.snapshot.doc_id;
|
||||
root = root
|
||||
.child(make_row(
|
||||
@@ -245,8 +282,7 @@ impl Overlays {
|
||||
}
|
||||
|
||||
if interactive {
|
||||
let trigger_in_window =
|
||||
trigger.translated(scene.pane_origin.x.into(), scene.pane_origin.y.into());
|
||||
let trigger_in_window = OverlayScene::rect_to_window(trigger, scene.pane_origin);
|
||||
root = root.occlude().on_mouse_down_out(cx.listener(
|
||||
move |view, event: &MouseDownEvent, window, cx| {
|
||||
let x = event.position.x.into();
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
use blocksuite_playground_doc::read::BlockContent;
|
||||
use ui::{
|
||||
AnyElement, ClickEvent, Context, FontWeight, MouseButton, MouseDownEvent, ScrollEvent,
|
||||
SharedString, div, prelude::*, px,
|
||||
AnyElement, ClickEvent, Context, ElementId, FontWeight, MouseButton, MouseDownEvent,
|
||||
ScrollEvent, div, prelude::*, px,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -18,6 +18,8 @@ use super::{
|
||||
place_touch_docked_toolbar,
|
||||
};
|
||||
|
||||
use super::more_menu::CodeToolbarTarget;
|
||||
|
||||
type TouchItem<'a> = (f32, Box<dyn FnOnce(bool) -> AnyElement + 'a>);
|
||||
|
||||
impl Overlays {
|
||||
@@ -94,6 +96,7 @@ impl Overlays {
|
||||
let brand_bg = palette.brand.opacity(0.14);
|
||||
let danger = palette.error;
|
||||
let snap_prefix = flow_block.snapshot_id();
|
||||
let target = CodeToolbarTarget::new(self, scene, block_id);
|
||||
|
||||
let make_icon_item = |suffix: &'static str,
|
||||
active: bool,
|
||||
@@ -102,8 +105,7 @@ impl Overlays {
|
||||
glyph: AnyElement,
|
||||
cx: &mut Context<V>|
|
||||
-> TouchItem<'_> {
|
||||
let click_handler = emit(cx, move |_: &ClickEvent| ev.clone());
|
||||
let dom_id = SharedString::from(format!("code-touch-{suffix}-{block_id}"));
|
||||
let click_handler = target.on_click(cx, ev);
|
||||
(
|
||||
btn,
|
||||
Box::new(move |fully_visible| {
|
||||
@@ -121,7 +123,7 @@ impl Overlays {
|
||||
.hover(move |s| s.bg(hover_bg))
|
||||
.child(glyph);
|
||||
div()
|
||||
.id(dom_id)
|
||||
.id(suffix)
|
||||
.flex_none()
|
||||
.w(px(btn))
|
||||
.h(px(btn))
|
||||
@@ -153,7 +155,6 @@ impl Overlays {
|
||||
let chevron_size = if compact { 12.0 } else { 16.0 };
|
||||
let lang_ev = OverlayEvent::OpenCodeLangPicker(block_id.to_string());
|
||||
let click_handler = emit(cx, move |_: &ClickEvent| lang_ev.clone());
|
||||
let dom_id = SharedString::from(format!("code-touch-lang-{block_id}"));
|
||||
let text_col = palette.text_primary;
|
||||
let chevron = icon("ArrowDownSmallIcon", chevron_size, palette.icon);
|
||||
items.push((
|
||||
@@ -163,7 +164,7 @@ impl Overlays {
|
||||
return div().into_any_element();
|
||||
}
|
||||
div()
|
||||
.id(dom_id)
|
||||
.id("lang")
|
||||
.w(px(lang_w))
|
||||
.h(px(btn))
|
||||
.px(px(lang_padding))
|
||||
@@ -195,7 +196,6 @@ impl Overlays {
|
||||
let preview_label = if in_preview { "Code" } else { "Preview" };
|
||||
let preview_ev = OverlayEvent::ToggleCodePreview(block_id.to_string());
|
||||
let click_handler = emit(cx, move |_: &ClickEvent| preview_ev.clone());
|
||||
let dom_id = SharedString::from(format!("code-touch-preview-{block_id}"));
|
||||
let text_col = if in_preview {
|
||||
palette.brand
|
||||
} else {
|
||||
@@ -208,7 +208,7 @@ impl Overlays {
|
||||
return div().into_any_element();
|
||||
}
|
||||
div()
|
||||
.id(dom_id)
|
||||
.id("preview")
|
||||
.w(px(preview_w))
|
||||
.h(px(btn))
|
||||
.px(px(6.0))
|
||||
@@ -336,7 +336,10 @@ impl Overlays {
|
||||
|
||||
let strip = Self::build_touch_scroll_strip(avail_w, btn, gap, scroll_x, palette, items);
|
||||
let mut root = place(rect)
|
||||
.id(SharedString::from(format!("code-touch-toolbar-{block_id}")))
|
||||
.id((
|
||||
ElementId::from("code-touch-toolbar"),
|
||||
target.block_id.clone(),
|
||||
))
|
||||
.occlude()
|
||||
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
|
||||
.on_mouse_down_out(|_: &MouseDownEvent, _, _| {})
|
||||
@@ -363,7 +366,7 @@ impl Overlays {
|
||||
action: CodeMoreActionKind::Delete,
|
||||
};
|
||||
let del_btn = div()
|
||||
.id(SharedString::from(format!("code-touch-delete-{block_id}")))
|
||||
.id("delete")
|
||||
.flex_none()
|
||||
.w(px(btn))
|
||||
.h(px(btn))
|
||||
@@ -374,7 +377,7 @@ impl Overlays {
|
||||
.cursor_pointer()
|
||||
.hover(move |s| s.bg(hover_bg))
|
||||
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
|
||||
.on_click(emit(cx, move |_: &ClickEvent| del_ev.clone()))
|
||||
.on_click(target.on_click(cx, del_ev))
|
||||
.snapshot(format_args!("{snap_prefix}.delete"), Some("root.canvas"))
|
||||
.snapshot_text("Delete")
|
||||
.child(icon("DeleteIcon", 16.0, danger));
|
||||
|
||||
@@ -603,7 +603,9 @@ impl Overlays {
|
||||
));
|
||||
submenu_panel = Some(panel.into_any_element());
|
||||
}
|
||||
let submenu_rect = popup.submenu_rect;
|
||||
let submenu_rect = popup
|
||||
.submenu_rect
|
||||
.map(|rect| OverlayScene::rect_to_window(rect, scene.pane_origin));
|
||||
let available =
|
||||
(placed_rect.w - 2.0 * (TOOLBAR_CHROME_PX + TOOLBAR_CHROME_BORDER)).max(1.0);
|
||||
let strip = self.scroll_toolbar(
|
||||
|
||||
@@ -148,6 +148,15 @@ use ui::{Context, Motion, MotionCurve, Pixels, Point, Window};
|
||||
pub trait OverlayHost: 'static + Sized {
|
||||
fn block_url(&self, doc_id: &str, block_id: &str) -> String;
|
||||
|
||||
/// Window coordinates of the editor pane's top-left corner.
|
||||
fn overlay_pane_origin(&self) -> Point<Pixels> {
|
||||
Point::default()
|
||||
}
|
||||
|
||||
/// Check the target of a rendered code toolbar action.
|
||||
fn code_toolbar_target_is_current(&self, generation: u64, doc_id: &str, block_id: &str)
|
||||
-> bool;
|
||||
|
||||
/// The current `(language, text)` for the open code menu in this overlay generation.
|
||||
fn code_menu_source(
|
||||
&self,
|
||||
@@ -286,6 +295,11 @@ pub struct OverlayScene<'a> {
|
||||
}
|
||||
|
||||
impl OverlayScene<'_> {
|
||||
/// Translate pane-local bounds for comparison with window input events.
|
||||
pub(crate) fn rect_to_window(rect: FlowRect, pane_origin: Point<Pixels>) -> FlowRect {
|
||||
rect.translated(pane_origin.x.into(), pane_origin.y.into())
|
||||
}
|
||||
|
||||
/// The page viewport below the top bar.
|
||||
pub fn page_viewport(&self) -> PageViewport {
|
||||
PageViewport {
|
||||
@@ -410,9 +424,14 @@ pub struct Overlays {
|
||||
}
|
||||
|
||||
impl Overlays {
|
||||
/// Check that the editor has not reset since a code toolbar was rendered.
|
||||
pub fn code_toolbar_generation_is_current(&self, generation: u64) -> bool {
|
||||
self.reset_generation == generation
|
||||
}
|
||||
|
||||
/// Check that a rendered copy action still belongs to the open code menu.
|
||||
pub fn code_menu_is_current(&self, generation: u64, block_id: &str) -> bool {
|
||||
self.reset_generation == generation
|
||||
self.code_toolbar_generation_is_current(generation)
|
||||
&& self
|
||||
.code_more_menu()
|
||||
.is_some_and(|menu| menu.block_id == block_id)
|
||||
@@ -501,3 +520,223 @@ impl Overlays {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod pane_dismissal_tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
block_flow::{FlowStats, FormFactor, page_geometry},
|
||||
theme::{PlaygroundTheme, register_bundled_fonts},
|
||||
};
|
||||
use ui::{
|
||||
AppContext, Application, Bounds, Direction, HeadlessPlatform, Render, WindowBounds,
|
||||
WindowOptions, div, point, prelude::*, px, size,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum Surface {
|
||||
Toolbar,
|
||||
BlockMenu,
|
||||
LinkPopup,
|
||||
Slash,
|
||||
}
|
||||
|
||||
struct Pane {
|
||||
origin: Point<Pixels>,
|
||||
surface: Surface,
|
||||
exempt: Vec<FlowRect>,
|
||||
dismissals: usize,
|
||||
}
|
||||
|
||||
impl OverlayHost for Pane {
|
||||
fn block_url(&self, _: &str, _: &str) -> String {
|
||||
String::new()
|
||||
}
|
||||
|
||||
fn overlay_pane_origin(&self) -> Point<Pixels> {
|
||||
self.origin
|
||||
}
|
||||
|
||||
fn code_toolbar_target_is_current(&self, _: u64, _: &str, _: &str) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn code_menu_source(&self, _: u64, _: &str, _: &str) -> Option<(&str, &str)> {
|
||||
None
|
||||
}
|
||||
|
||||
fn dispatch_overlay(&mut self, event: OverlayEvent, _: &mut Window, _: &mut Context<Self>) {
|
||||
if matches!(
|
||||
event,
|
||||
OverlayEvent::CloseBlockSubmenu
|
||||
| OverlayEvent::CloseBlockMenu
|
||||
| OverlayEvent::CloseLinkPopup
|
||||
| OverlayEvent::CloseSlashMenu
|
||||
) {
|
||||
self.dismissals += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Pane {
|
||||
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let snapshot = PageSnapshot {
|
||||
doc_id: "doc".into(),
|
||||
root: None,
|
||||
detached: vec![],
|
||||
};
|
||||
let layout = FlowLayout {
|
||||
blocks: vec![],
|
||||
width: 400.0,
|
||||
height: 800.0,
|
||||
direction: Direction::Ltr,
|
||||
stats: FlowStats::default(),
|
||||
};
|
||||
let scene = OverlayScene {
|
||||
snapshot: &snapshot,
|
||||
layout: &layout,
|
||||
geometry: page_geometry(590.0),
|
||||
window: (590.0, 800.0),
|
||||
pane_origin: self.origin,
|
||||
bottom_inset: 0.0,
|
||||
bottom_toolbar_inset: 0.0,
|
||||
scroll_y: 0.0,
|
||||
direction: Direction::Ltr,
|
||||
form_factor: FormFactor::Desktop,
|
||||
is_touch: false,
|
||||
focused_code_block: None,
|
||||
selected_image_block: None,
|
||||
can_download_blobs: false,
|
||||
selected_attachment_block: None,
|
||||
selected_bookmark_block: None,
|
||||
selected_embed_block: None,
|
||||
selected_callout_block: None,
|
||||
selected_latex_block: None,
|
||||
selection: None,
|
||||
screen_transform: None,
|
||||
suppress_floating_format_buttons: false,
|
||||
page_readonly: false,
|
||||
local_collapsed: &BTreeSet::new(),
|
||||
topbar_height: 0.0,
|
||||
};
|
||||
let palette = PlaygroundTheme::Light.palette();
|
||||
let mut overlays = Overlays::new();
|
||||
let anchor = FlowRect::new(80.0, 200.0, 40.0, 26.0);
|
||||
let children = match self.surface {
|
||||
Surface::Toolbar => {
|
||||
let panel_rect = FlowRect::new(80.0, 240.0, 176.0, 90.0);
|
||||
let panel = toolbar_dropdown_panel(
|
||||
"test", panel_rect, 1.0, true, "test", "root", &palette,
|
||||
);
|
||||
self.exempt = vec![anchor, panel_rect];
|
||||
vec![dismiss_submenu_outside(panel, anchor, cx).into_any_element()]
|
||||
}
|
||||
Surface::BlockMenu => {
|
||||
overlays.open_block_menu("block".into(), (80.0, 200.0), true, true, 0.0);
|
||||
let menu = overlays.block_menu.as_mut().unwrap();
|
||||
menu.handle_screen_rect = Some(anchor);
|
||||
menu.turn_into_open = true;
|
||||
let children = block_menu::render_block_menu(
|
||||
menu,
|
||||
scene.popover_viewport(),
|
||||
&palette,
|
||||
200.0,
|
||||
cx,
|
||||
);
|
||||
self.exempt = vec![anchor, menu.submenu_rect.unwrap()];
|
||||
children
|
||||
}
|
||||
Surface::LinkPopup => {
|
||||
overlays.open_link_popup(
|
||||
LinkPopupTarget::Link {
|
||||
block_id: "block".into(),
|
||||
start_utf16: 0,
|
||||
end_utf16: 3,
|
||||
url: "https://example.test/".into(),
|
||||
},
|
||||
anchor,
|
||||
false,
|
||||
0.0,
|
||||
);
|
||||
overlays.block_submenu = Some(BlockToolbarSubmenu::LinkPopupMore);
|
||||
let children = overlays.render_link_popup(&scene, &palette, 200.0, cx);
|
||||
self.exempt = vec![overlays.link_popup().unwrap().submenu_rect.unwrap()];
|
||||
children
|
||||
}
|
||||
Surface::Slash => {
|
||||
overlays.open_slash("block".into(), 0, Some(anchor), false, 0.0);
|
||||
overlays.slash.as_mut().unwrap().submenu_open = true;
|
||||
let children = overlays.render_slash(&scene, &palette, 200.0, cx);
|
||||
self.exempt = vec![overlays.slash_menu().unwrap().submenu_rect.unwrap()];
|
||||
children
|
||||
}
|
||||
};
|
||||
div().size_full().relative().child(
|
||||
div()
|
||||
.absolute()
|
||||
.left(self.origin.x)
|
||||
.top(self.origin.y)
|
||||
.w(px(scene.window.0))
|
||||
.h(px(scene.window.1))
|
||||
.children(children),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlay_dismissal_respects_pane_origin_for_triggers_and_submenus() {
|
||||
for origin in [point(px(0.0), px(0.0)), point(px(850.0), px(84.0))] {
|
||||
for surface in [
|
||||
Surface::Toolbar,
|
||||
Surface::BlockMenu,
|
||||
Surface::LinkPopup,
|
||||
Surface::Slash,
|
||||
] {
|
||||
let platform = HeadlessPlatform::new(1440, 900, 1.0);
|
||||
let app = Application::with_platform(platform.clone()).run_embedded(|_| {});
|
||||
let window = app.update(|cx| {
|
||||
register_bundled_fonts(cx).unwrap();
|
||||
cx.open_window(
|
||||
WindowOptions {
|
||||
window_bounds: Some(WindowBounds::Windowed(Bounds::new(
|
||||
point(px(0.0), px(0.0)),
|
||||
size(px(1440.0), px(900.0)),
|
||||
))),
|
||||
..WindowOptions::default()
|
||||
},
|
||||
|_, cx| {
|
||||
cx.new(|_| Pane {
|
||||
origin,
|
||||
surface,
|
||||
exempt: Vec::new(),
|
||||
dismissals: 0,
|
||||
})
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
});
|
||||
let headless = platform.windows().into_iter().next().unwrap();
|
||||
headless.request_frame();
|
||||
let positions =
|
||||
app.update(|cx| window.update(cx, |pane, _, _| pane.exempt.clone()).unwrap());
|
||||
assert!(!positions.is_empty(), "{surface:?}");
|
||||
let press = |position| {
|
||||
headless.simulate_click(position);
|
||||
app.update(|cx| window.update(cx, |pane, _, _| pane.dismissals).unwrap())
|
||||
};
|
||||
let first = positions[0];
|
||||
for rect in positions {
|
||||
let position =
|
||||
origin + point(px(rect.x + rect.w * 0.5), px(rect.y + rect.h * 0.5));
|
||||
assert_eq!(press(position), 0, "{surface:?} at {origin:?}");
|
||||
}
|
||||
let outside = if origin.x > px(0.0) {
|
||||
point(px(first.x + first.w * 0.5), px(first.y + first.h * 0.5))
|
||||
} else {
|
||||
point(px(730.0), px(10.0))
|
||||
};
|
||||
assert_eq!(press(outside), 1, "{surface:?} at {origin:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -370,6 +370,8 @@ impl Overlays {
|
||||
.snapshot_focused(interactive)
|
||||
.snapshot_text(menu.query.as_str());
|
||||
if interactive {
|
||||
let submenu_in_window =
|
||||
submenu_rect.map(|rect| OverlayScene::rect_to_window(rect, scene.pane_origin));
|
||||
root = root
|
||||
.occlude()
|
||||
.on_scroll(emit(cx, move |event: &ScrollEvent| {
|
||||
@@ -377,7 +379,7 @@ impl Overlays {
|
||||
}))
|
||||
.on_mouse_down_out(cx.listener(move |this, ev: &MouseDownEvent, window, cx| {
|
||||
let (mx, my) = (f32::from(ev.position.x), f32::from(ev.position.y));
|
||||
if submenu_rect.is_some_and(|r| r.contains(mx, my)) {
|
||||
if submenu_in_window.is_some_and(|r| r.contains(mx, my)) {
|
||||
return;
|
||||
}
|
||||
this.dispatch_overlay(OverlayEvent::CloseSlashMenu, window, cx);
|
||||
|
||||
@@ -454,6 +454,7 @@ pub fn dismiss_submenu_outside<V: OverlayHost>(
|
||||
cx: &mut Context<V>,
|
||||
) -> Stateful<Div> {
|
||||
panel.on_mouse_down_out(cx.listener(move |view, ev: &MouseDownEvent, window, cx| {
|
||||
let bar = OverlayScene::rect_to_window(bar, view.overlay_pane_origin());
|
||||
if !bar.contains(f32::from(ev.position.x), f32::from(ev.position.y)) {
|
||||
view.dispatch_overlay(OverlayEvent::CloseBlockSubmenu, window, cx);
|
||||
}
|
||||
|
||||
@@ -81,6 +81,28 @@ impl OverlayHost for crate::PlaygroundView {
|
||||
)
|
||||
}
|
||||
|
||||
fn overlay_pane_origin(&self) -> ui::Point<ui::Pixels> {
|
||||
self.pane_origin()
|
||||
}
|
||||
|
||||
fn code_toolbar_target_is_current(
|
||||
&self,
|
||||
generation: u64,
|
||||
doc_id: &str,
|
||||
block_id: &str,
|
||||
) -> bool {
|
||||
self.overlays.code_toolbar_generation_is_current(generation)
|
||||
&& self.snapshot.as_deref().is_some_and(|snapshot| {
|
||||
snapshot.doc_id == doc_id
|
||||
&& snapshot.block(block_id).is_some_and(|block| {
|
||||
matches!(
|
||||
block.content,
|
||||
blocksuite_playground_doc::read::BlockContent::Code { .. }
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn code_menu_source(
|
||||
&self,
|
||||
generation: u64,
|
||||
@@ -3348,7 +3370,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn code_menu_copy_ignores_mouse_release_after_workspace_switch() {
|
||||
fn code_menu_actions_ignore_mouse_release_after_workspace_switch() {
|
||||
fn document(room: &str, text: &str) -> crate::DocState {
|
||||
let mut state = crate::DocState::new(room, 501).unwrap();
|
||||
state
|
||||
@@ -3389,8 +3411,24 @@ mod tests {
|
||||
state
|
||||
}
|
||||
|
||||
let encode = |state: &crate::DocState| {
|
||||
state
|
||||
.workspace
|
||||
.page(HOME_PAGE_ID)
|
||||
.unwrap()
|
||||
.encode_update_v1()
|
||||
.unwrap()
|
||||
};
|
||||
let session = open(1280, 800, PlaygroundTheme::Light);
|
||||
for action in ["copy_markdown", "copy_block_link"] {
|
||||
let mut changed = Vec::new();
|
||||
for action in [
|
||||
"delete",
|
||||
"duplicate",
|
||||
"wrap",
|
||||
"line_number",
|
||||
"copy_markdown",
|
||||
"copy_block_link",
|
||||
] {
|
||||
session.view(|view, cx| view.set_document(document("workspace-a", "first"), cx));
|
||||
session.overlay(OverlayEvent::ToggleCodeMoreMenu {
|
||||
block_id: "code".into(),
|
||||
@@ -3402,6 +3440,7 @@ mod tests {
|
||||
session.app.update(|cx| {
|
||||
cx.write_to_clipboard(ClipboardItem::new_string("previous clipboard".into()));
|
||||
});
|
||||
let original = session.view(|view, _| encode(view.document()));
|
||||
session
|
||||
.headless
|
||||
.dispatch_input(PlatformInput::MouseDown(MouseDownEvent {
|
||||
@@ -3411,7 +3450,12 @@ mod tests {
|
||||
click_count: 1,
|
||||
first_mouse: false,
|
||||
}));
|
||||
session.view(|view, cx| view.set_document(document("workspace-b", "second"), cx));
|
||||
let replacement = document("workspace-b", "second");
|
||||
let expected = encode(&replacement);
|
||||
session.view(|view, cx| {
|
||||
assert_eq!(encode(view.document()), original);
|
||||
view.set_document(replacement, cx);
|
||||
});
|
||||
session
|
||||
.headless
|
||||
.dispatch_input(PlatformInput::MouseUp(MouseUpEvent {
|
||||
@@ -3428,6 +3472,13 @@ mod tests {
|
||||
Some("previous clipboard".into()),
|
||||
"{action} must leave the clipboard alone after a workspace switch",
|
||||
);
|
||||
if session.view(|view, _| encode(view.document())) != expected {
|
||||
changed.push(action);
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
changed.is_empty(),
|
||||
"stale actions changed the replacement: {changed:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,7 @@ impl PlaygroundView {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use blocksuite_playground_doc::read::BlockContent;
|
||||
use ui::{
|
||||
FrameSnapshot, PlatformInput, ScrollDelta, ScrollEvent, ScrollSource, TouchEvent, TouchId,
|
||||
TouchPhase, point, px,
|
||||
@@ -97,7 +98,11 @@ mod tests {
|
||||
|
||||
use crate::{
|
||||
FormFactor, PlaygroundOptions,
|
||||
overlays::tests::{Session, element, open_with_options},
|
||||
block_flow::FlowRect,
|
||||
overlays::{
|
||||
OverlayEvent,
|
||||
tests::{Session, element, open_with_options},
|
||||
},
|
||||
};
|
||||
|
||||
fn activate(session: &Session, frame: &FrameSnapshot, id: &str, touch: bool) {
|
||||
@@ -124,6 +129,50 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn code_toolbar_menu_edits_apply_and_undo_in_one_step() {
|
||||
let session = open_with_options(1280, 800, PlaygroundOptions::default());
|
||||
let state = || {
|
||||
session.view(|view, _| {
|
||||
let snapshot = view.page_snapshot().unwrap();
|
||||
(
|
||||
snapshot.block_count(),
|
||||
snapshot.block("showcase-code").map(|block| {
|
||||
let BlockContent::Code { wrap, .. } = &block.content else {
|
||||
panic!("expected a code block");
|
||||
};
|
||||
let line_number = block
|
||||
.props
|
||||
.get("prop:lineNumber")
|
||||
.and_then(|value| value.as_bool())
|
||||
.unwrap_or(true);
|
||||
(*wrap, line_number)
|
||||
}),
|
||||
)
|
||||
})
|
||||
};
|
||||
let (count, flags) = state();
|
||||
let (wrap, line_number) = flags.unwrap();
|
||||
|
||||
for (action, expected) in [
|
||||
("wrap", (count, Some((!wrap, line_number)))),
|
||||
("line_number", (count, Some((wrap, !line_number)))),
|
||||
("duplicate", (count + 1, flags)),
|
||||
("delete", (count - 1, None)),
|
||||
] {
|
||||
session.overlay(OverlayEvent::ToggleCodeMoreMenu {
|
||||
block_id: "showcase-code".into(),
|
||||
anchor: FlowRect::new(400.0, 200.0, 28.0, 28.0),
|
||||
});
|
||||
let frame = session.settle();
|
||||
session.click(&frame, &format!("overlay.code_more_menu.{action}"));
|
||||
assert_eq!(state(), expected, "{action}");
|
||||
session.undo();
|
||||
assert_eq!(state(), (count, flags), "{action} undo");
|
||||
session.overlay(OverlayEvent::CloseCodeMoreMenu);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn code_more_copies_markdown_and_links_and_keeps_phone_actions_reachable() {
|
||||
let block = "page.block.showcase-code";
|
||||
|
||||
Reference in new issue
Block a user