refactor: extract editor overlay helpers

This commit is contained in:
lix-2026
2026-05-25 01:06:37 +08:00
parent e1484f4cc0
commit 7975ce0997
7 changed files with 52 additions and 40 deletions
@@ -3,4 +3,5 @@ pub(crate) mod attachment_upload;
pub(crate) mod command_sync;
pub(crate) mod dom_selection;
pub(crate) mod history_safe_commands;
pub(crate) mod overlays;
pub(crate) mod persistence;
@@ -0,0 +1,39 @@
use leptos::prelude::{Set, WriteSignal};
use crate::{
editor_runtime::dom_selection::active_editor_text_selection, editor_stage_element,
};
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct FloatingToolbarAnchor {
pub(crate) top: f64,
pub(crate) left: f64,
}
pub(crate) fn floating_toolbar_anchor_from_selection() -> Option<FloatingToolbarAnchor> {
let selection = active_editor_text_selection()?;
let range = selection.get_range_at(0).ok()?;
let rect = range.get_bounding_client_rect();
if rect.width() <= 0.0 && rect.height() <= 0.0 {
return None;
}
let stage = editor_stage_element()?;
let stage_rect = stage.get_bounding_client_rect();
Some(FloatingToolbarAnchor {
top: (rect.top() - stage_rect.top()).max(60.0),
left: rect.left() + (rect.width() / 2.0) - stage_rect.left(),
})
}
pub(crate) fn sync_text_selection_overlay(
set_text_selection_active: WriteSignal<bool>,
set_floating_toolbar_anchor: WriteSignal<Option<FloatingToolbarAnchor>>,
) -> bool {
let toolbar_anchor = floating_toolbar_anchor_from_selection();
let has_text_selection = toolbar_anchor.is_some();
set_text_selection_active.set(has_text_selection);
set_floating_toolbar_anchor.set(toolbar_anchor);
has_text_selection
}