refactor: extract block dnd element helpers

This commit is contained in:
lix-2026
2026-05-25 06:21:02 +08:00
parent 6017a0ad7c
commit 8bcf958fe8
5 changed files with 35 additions and 29 deletions
@@ -85,6 +85,26 @@ pub(crate) fn element_within_handle_shell(element: &Element, handle_selector: &s
element.closest(handle_selector).ok().flatten().is_some()
}
pub(crate) fn direct_block_from_element(mut element: Element, root: &Element) -> Option<Element> {
loop {
let parent = element.parent_element()?;
if parent.is_same_node(Some(root)) {
return Some(element);
}
element = parent;
}
}
pub(crate) fn top_level_block_index(root: &Element, block: &Element) -> Option<usize> {
let children = root.children();
(0..children.length()).find_map(|index| {
children
.item(index)
.filter(|child| child.is_same_node(Some(block)))
.map(|_| index as usize)
})
}
#[cfg(test)]
mod tests {
use super::*;
+3 -22
View File
@@ -22,8 +22,9 @@ use web_sys::{
use editor_runtime::attachment_upload::dispatch_editor_upload_request;
use editor_runtime::block_dnd::{
drop_indicator_from_geometry, element_within_handle_shell, event_target_from_point,
pointer_in_handle_corridor_geometry, HandleCorridorGeometry,
direct_block_from_element, drop_indicator_from_geometry, element_within_handle_shell,
event_target_from_point, pointer_in_handle_corridor_geometry, top_level_block_index,
HandleCorridorGeometry,
};
use editor_runtime::block_menu_overlay;
use editor_runtime::block_menu_legacy_html::{
@@ -4837,26 +4838,6 @@ fn handle_lane_left(stage_rect_width: f64) -> f64 {
.max(HANDLE_STAGE_PADDING_LEFT)
}
fn direct_block_from_element(mut element: Element, root: &Element) -> Option<Element> {
loop {
let parent = element.parent_element()?;
if parent.is_same_node(Some(root)) {
return Some(element);
}
element = parent;
}
}
fn top_level_block_index(root: &Element, block: &Element) -> Option<usize> {
let children = root.children();
(0..children.length()).find_map(|index| {
children
.item(index)
.filter(|child| child.is_same_node(Some(block)))
.map(|_| index as usize)
})
}
fn block_label_for_element(block: &Element) -> String {
match block.get_attribute("data-type").as_deref() {
Some("taskList") => "Todo 列表".to_string(),