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
}
+5 -37
View File
@@ -25,8 +25,10 @@ use editor_runtime::command_sync::{
read_editor_snapshot, sync_editor_outputs, sync_persisted_editor_command,
};
use editor_runtime::dom_selection::{
active_editor_text_selection, has_active_text_selection, node_is_within_root,
selection_summary,
has_active_text_selection, node_is_within_root, selection_summary,
};
use editor_runtime::overlays::{
floating_toolbar_anchor_from_selection, sync_text_selection_overlay, FloatingToolbarAnchor,
};
use editor_runtime::persistence::{
load_persisted_document, normalize_identity_value, persist_document_state,
@@ -4785,12 +4787,6 @@ struct HoveredBlockState {
height: f64,
}
#[derive(Clone, Debug, PartialEq)]
struct FloatingToolbarAnchor {
top: f64,
left: f64,
}
#[derive(Clone, Debug, PartialEq)]
struct ImageToolbarAnchor {
top: f64,
@@ -5025,7 +5021,7 @@ pub(crate) fn editor_root_element() -> Option<Element> {
.and_then(|document| document.query_selector(EDITOR_ROOT_SELECTOR).ok().flatten())
}
fn editor_stage_element() -> Option<Element> {
pub(crate) fn editor_stage_element() -> Option<Element> {
window()
.and_then(|win| win.document())
.and_then(|document| {
@@ -5338,23 +5334,6 @@ fn top_level_block_range(document: &Value, index: usize) -> Option<TiptapRange>
None
}
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(),
})
}
fn slash_menu_anchor_style() -> String {
const MENU_WIDTH: f64 = 316.0;
const MENU_HEIGHT: f64 = 430.0;
@@ -6090,17 +6069,6 @@ fn open_block_menu_overlay(
should_open
}
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
}
fn sync_editor_overlay_state(
set_editor_focused: WriteSignal<bool>,
set_text_selection_active: WriteSignal<bool>,