refactor: add filetree sidebar snapshot helper

This commit is contained in:
lix-2026
2026-05-25 06:47:54 +08:00
parent 1abc05e4a3
commit cff0c67cf9
4 changed files with 65 additions and 10 deletions
@@ -2426,6 +2426,22 @@ const SIDEBAR_TREE_JS: &str = r##"
return true;
}
function applyLocalFolderSidebarSnapshot(nextDocument, options) {
var runtimeFn = fileTreeRuntimeFunction('applyLocalFolderSidebarSnapshot');
if (runtimeFn) return runtimeFn(nextDocument, options);
options = options || {};
var pageRootId = String(options.pageRootId || 'sidebar-tree-root');
var fileRootId = String(options.fileRootId || 'sidebar-file-tree-root');
var appliedPage = replaceSidebarTreeFromDocument(nextDocument, pageRootId);
var appliedFile = replaceSidebarTreeFromDocument(nextDocument, fileRootId);
var applied = appliedPage || appliedFile;
if (applied && typeof options.onApplied === 'function') options.onApplied({
pageRootApplied: appliedPage,
fileRootApplied: appliedFile
});
return applied;
}
async function refreshLocalFolderSidebarSnapshot() {
var workspaceId = currentWorkspaceId();
var rootUri = currentRootUri();
@@ -11092,6 +11108,9 @@ mod tests {
assert!(
SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('replaceSidebarTreeFromDocument')")
);
assert!(
SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('applyLocalFolderSidebarSnapshot')")
);
let document_id_body = js_function_body(SIDEBAR_TREE_JS, "fileTreeRowDocumentId");
assert!(
document_id_body.contains("data-document-id")
@@ -11104,6 +11123,14 @@ mod tests {
&& replace_body.contains("current.setAttribute(attr.name, attr.value)"),
"inline fallback 必须保留 DOM copy 行为"
);
let apply_body = js_function_body(SIDEBAR_TREE_JS, "applyLocalFolderSidebarSnapshot");
assert!(
apply_body.contains("options.pageRootId || 'sidebar-tree-root'")
&& apply_body.contains("options.fileRootId || 'sidebar-file-tree-root'")
&& apply_body.contains("replaceSidebarTreeFromDocument(nextDocument, pageRootId)")
&& apply_body.contains("replaceSidebarTreeFromDocument(nextDocument, fileRootId)"),
"inline fallback 必须保留 sidebar/page tree DOM 应用行为"
);
}
#[test]
@@ -11133,10 +11160,13 @@ mod tests {
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("function applyLocalFolderSidebarSnapshot"));
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("applyLocalFolderSidebarSnapshot: applyLocalFolderSidebarSnapshot"));
assert!(FILETREE_RUNTIME_JS
.contains("uploadIntent: String(detail.uploadIntent || 'filetree.folder.drop')"));
assert!(FILETREE_RUNTIME_JS