refactor: extract table overlay helpers
This commit is contained in:
@@ -6,6 +6,7 @@ use crate::{
|
||||
editor_runtime::editor_focus::active_editor_stage,
|
||||
editor_stage_element,
|
||||
};
|
||||
use web_sys::{window, Element};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) struct FloatingToolbarAnchor {
|
||||
@@ -51,6 +52,63 @@ pub(crate) struct TableSelectionOverlayState {
|
||||
pub(crate) index: usize,
|
||||
}
|
||||
|
||||
pub(crate) fn table_overlay_anchor_from_table(table: &Element) -> Option<TableOverlayAnchor> {
|
||||
let stage = editor_stage_element()?;
|
||||
let table_rect = table.get_bounding_client_rect();
|
||||
let stage_rect = stage.get_bounding_client_rect();
|
||||
if table_rect.width() <= 0.0 || table_rect.height() <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let rows = table.get_elements_by_tag_name("tr").length() as usize;
|
||||
let cols = table
|
||||
.query_selector("tr")
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|row| row.children().length() as usize)
|
||||
.unwrap_or(0);
|
||||
if rows == 0 || cols == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(TableOverlayAnchor {
|
||||
top: table_rect.top() - stage_rect.top(),
|
||||
left: table_rect.left() - stage_rect.left(),
|
||||
width: table_rect.width(),
|
||||
height: table_rect.height(),
|
||||
rows,
|
||||
cols,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn current_table_overlay_anchor() -> Option<TableOverlayAnchor> {
|
||||
let table = window()
|
||||
.and_then(|win| win.document())
|
||||
.and_then(|document| {
|
||||
document
|
||||
.query_selector(".editor-surface .ProseMirror table")
|
||||
.ok()
|
||||
.flatten()
|
||||
})?;
|
||||
table_overlay_anchor_from_table(&table)
|
||||
}
|
||||
|
||||
pub(crate) fn table_row_aux_style(anchor: &TableOverlayAnchor, index: usize) -> String {
|
||||
let row_height = anchor.height / anchor.rows.max(1) as f64;
|
||||
format!(
|
||||
"left:-24px;top:{:.1}px;",
|
||||
(row_height * index as f64) + (row_height / 2.0) - 9.0
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn table_col_aux_style(anchor: &TableOverlayAnchor, index: usize) -> String {
|
||||
let col_width = anchor.width / anchor.cols.max(1) as f64;
|
||||
format!(
|
||||
"left:{:.1}px;top:-24px;",
|
||||
(col_width * index as f64) + (col_width / 2.0) - 9.0
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn floating_toolbar_anchor_from_selection() -> Option<FloatingToolbarAnchor> {
|
||||
let selection = active_editor_text_selection()?;
|
||||
let range = selection.get_range_at(0).ok()?;
|
||||
|
||||
Reference in New Issue
Block a user