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::*;