refactor: extract editor overlay helpers
This commit is contained in:
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user