refactor: extract dnd target and sidebar helpers

This commit is contained in:
lix-2026
2026-05-25 04:16:35 +08:00
parent 32adfb38a1
commit 1e097a9597
9 changed files with 201 additions and 57 deletions
@@ -214,6 +214,17 @@ function groupRowsByParent(rows) {
return grouped;
}
function replaceSidebarTreeFromDocument(nextDocument, rootId) {
var current = document.getElementById(rootId);
var next = nextDocument ? nextDocument.getElementById(rootId) : null;
if (!(current instanceof HTMLElement) || !(next instanceof HTMLElement)) return false;
current.innerHTML = next.innerHTML;
Array.from(next.attributes || []).forEach(function(attr) {
current.setAttribute(attr.name, attr.value);
});
return true;
}
function revealFileTreeRow(row) {
if (!(row instanceof HTMLElement)) return;
var node = row.closest('.tree-node');
@@ -351,6 +362,7 @@ window.__mnoteFileTreeRuntime = {
titleOf: titleOf,
fileWorkspaceRelativePath: fileWorkspaceRelativePath,
groupRowsByParent: groupRowsByParent,
replaceSidebarTreeFromDocument: replaceSidebarTreeFromDocument,
fileTreeRowKind: fileTreeRowKind,
fileTreeRowDocumentId: fileTreeRowDocumentId,
fileTreeRowAssetId: fileTreeRowAssetId,
@@ -2414,6 +2414,8 @@ const SIDEBAR_TREE_JS: &str = r##"
}
function replaceSidebarTreeFromDocument(nextDocument, rootId) {
var runtimeFn = fileTreeRuntimeFunction('replaceSidebarTreeFromDocument');
if (runtimeFn) return runtimeFn(nextDocument, rootId);
var current = document.getElementById(rootId);
var next = nextDocument ? nextDocument.getElementById(rootId) : null;
if (!(current instanceof HTMLElement) || !(next instanceof HTMLElement)) return false;
@@ -11087,12 +11089,21 @@ mod tests {
assert!(SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('titleOf')"));
assert!(SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('fileWorkspaceRelativePath')"));
assert!(SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('groupRowsByParent')"));
assert!(
SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('replaceSidebarTreeFromDocument')")
);
let document_id_body = js_function_body(SIDEBAR_TREE_JS, "fileTreeRowDocumentId");
assert!(
document_id_body.contains("data-document-id")
&& document_id_body.contains("data-doc-id"),
"inline fallback 必须保留旧 data attribute 解析"
);
let replace_body = js_function_body(SIDEBAR_TREE_JS, "replaceSidebarTreeFromDocument");
assert!(
replace_body.contains("current.innerHTML = next.innerHTML")
&& replace_body.contains("current.setAttribute(attr.name, attr.value)"),
"inline fallback 必须保留 DOM copy 行为"
);
}
#[test]
@@ -11121,8 +11132,11 @@ mod tests {
assert!(FILETREE_RUNTIME_JS.contains("function titleOf"));
assert!(FILETREE_RUNTIME_JS.contains("function fileWorkspaceRelativePath"));
assert!(FILETREE_RUNTIME_JS.contains("function groupRowsByParent"));
assert!(FILETREE_RUNTIME_JS.contains("function replaceSidebarTreeFromDocument"));
assert!(FILETREE_RUNTIME_JS.contains("appendUploadedAssetRow: appendUploadedAssetRow"));
assert!(FILETREE_RUNTIME_JS.contains("groupRowsByParent: groupRowsByParent"));
assert!(FILETREE_RUNTIME_JS
.contains("replaceSidebarTreeFromDocument: replaceSidebarTreeFromDocument"));
assert!(FILETREE_RUNTIME_JS
.contains("uploadIntent: String(detail.uploadIntent || 'filetree.folder.drop')"));
assert!(FILETREE_RUNTIME_JS
@@ -1284,7 +1284,7 @@ function __wbg_get_imports() {
return ret;
},
__wbindgen_cast_0000000000000006: function(arg0, arg1) {
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("KeyboardEvent")], shim_idx: 883, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("KeyboardEvent")], shim_idx: 736, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
const ret = makeMutClosure(arg0, arg1, wasm_bindgen__convert__closures_____invoke__hd90af689bc3e71bf);
return ret;
},
@@ -1,9 +1,10 @@
//! Block handle 拖拽命中的纯几何 helper。
//! Block handle 拖拽命中的 runtime helper。
//!
//! 本模块不查询 DOM、不处理 Leptos signal,也不分发 editor command
//! 调用方负责传入已经从页面中读取的坐标
//! 本模块只承接拖拽命中所需的窄 helper,不处理 Leptos signal
//! 也不分发 editor command;调用方负责保留编辑器状态编排
use crate::editor_runtime::block_hover_state::DropPlacement;
use web_sys::{window, EventTarget};
pub(crate) struct HandleCorridorGeometry {
pub(crate) stage_left: f64,
@@ -56,6 +57,13 @@ pub(crate) fn drop_indicator_top(
}
}
pub(crate) fn event_target_from_point(client_x: i32, client_y: i32) -> Option<EventTarget> {
window()
.and_then(|win| win.document())
.and_then(|document| document.element_from_point(client_x as f32, client_y as f32))
.map(Into::into)
}
#[cfg(test)]
mod tests {
use super::*;
+3 -6
View File
@@ -22,8 +22,8 @@ use web_sys::{
use editor_runtime::attachment_upload::dispatch_editor_upload_request;
use editor_runtime::block_dnd::{
drop_indicator_top, drop_placement_from_block_point, pointer_in_handle_corridor_geometry,
HandleCorridorGeometry,
drop_indicator_top, drop_placement_from_block_point, event_target_from_point,
pointer_in_handle_corridor_geometry, HandleCorridorGeometry,
};
use editor_runtime::block_menu_overlay;
use editor_runtime::block_menu_legacy_html::{
@@ -5430,10 +5430,7 @@ fn drop_indicator_from_target(
}
fn drop_indicator_from_point(client_x: i32, client_y: i32) -> Option<DropIndicatorState> {
let target = window()
.and_then(|win| win.document())
.and_then(|document| document.element_from_point(client_x as f32, client_y as f32))?;
let target: web_sys::EventTarget = target.into();
let target = event_target_from_point(client_x, client_y)?;
drop_indicator_from_target(target, client_y)
}