refactor: extract editor dom event helpers

This commit is contained in:
lix-2026
2026-05-25 07:23:39 +08:00
parent 82074f410c
commit 7956db06c2
6 changed files with 32 additions and 17 deletions
@@ -0,0 +1,23 @@
//! 编辑器 DOM event target 的窄 helper。
//!
//! 本模块只做 EventTarget -> Element 归一化和 selector 命中判断,
//! 不处理编辑器状态、overlay 或命令分发。
use wasm_bindgen::JsCast;
use web_sys::{Element, EventTarget, Node};
pub(crate) fn target_element(target: EventTarget) -> Option<Element> {
target.clone().dyn_into::<Element>().ok().or_else(|| {
target
.dyn_into::<Node>()
.ok()
.and_then(|node| node.parent_element())
})
}
pub(crate) fn event_target_matches_selector(target: Option<EventTarget>, selector: &str) -> bool {
target
.and_then(target_element)
.and_then(|element| element.closest(selector).ok().flatten())
.is_some()
}
@@ -6,6 +6,7 @@ pub(crate) mod block_menu_document;
pub(crate) mod block_menu_overlay;
pub(crate) mod block_menu_legacy_html;
pub(crate) mod command_sync;
pub(crate) mod dom_events;
pub(crate) mod dom_selection;
pub(crate) mod editor_focus;
pub(crate) mod history_safe_commands;
+1 -16
View File
@@ -39,6 +39,7 @@ use editor_runtime::block_hover_state::{
use editor_runtime::command_sync::{
read_editor_snapshot, sync_editor_outputs, sync_persisted_editor_command,
};
use editor_runtime::dom_events::{event_target_matches_selector, target_element};
use editor_runtime::dom_selection::{
runtime_block_id_from_index, selection_payload, selection_summary, SelectionPayload,
};
@@ -4843,22 +4844,6 @@ fn block_label_for_element(block: &Element) -> String {
}
}
fn target_element(target: web_sys::EventTarget) -> Option<Element> {
target.clone().dyn_into::<Element>().ok().or_else(|| {
target
.dyn_into::<Node>()
.ok()
.and_then(|node| node.parent_element())
})
}
fn event_target_matches_selector(target: Option<web_sys::EventTarget>, selector: &str) -> bool {
target
.and_then(target_element)
.and_then(|element| element.closest(selector).ok().flatten())
.is_some()
}
fn current_target_html_element(event: &WheelEvent) -> Option<HtmlElement> {
event.current_target()?.dyn_into::<HtmlElement>().ok()
}