refactor: extract dnd target and sidebar helpers
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user