refactor: extract mindmap fetch helpers

This commit is contained in:
lix-2026
2026-05-25 09:38:50 +08:00
parent 812504924f
commit 863e68b126
3 changed files with 141 additions and 24 deletions
@@ -490,6 +490,79 @@ function fileTreeDocumentWorkspacesForUploadPreflight(workspaceId, deps) {
});
}
function fileTreeUploadTargetPreflightBody(detail, deps) {
detail = detail || {};
deps = deps || {};
var resolveWorkspaceId = typeof deps.resolveWorkspaceId === 'function' ? deps.resolveWorkspaceId : function() { return ''; };
var currentDocumentId = typeof deps.currentDocumentId === 'function' ? deps.currentDocumentId : function() { return ''; };
var workspaceId = String(detail.workspaceId || resolveWorkspaceId(document.body) || '').trim();
var rows = typeof deps.fileTreeRowsForUploadPreflight === 'function'
? deps.fileTreeRowsForUploadPreflight()
: fileTreeRowsForUploadPreflight(deps);
var documentWorkspaces = fileTreeDocumentWorkspacesForUploadPreflight(workspaceId, Object.assign({}, deps, {
fileTreeRowsForUploadPreflight: function() { return rows; }
}));
return {
workspaceId: workspaceId || null,
targetDocumentId: detail.documentId ? String(detail.documentId) : null,
targetRowId: detail.targetRowId ? String(detail.targetRowId) : null,
focusedRowId: deps.focusedRowId || null,
activeDocumentId: currentDocumentId() || null,
rows: rows,
documentWorkspaces: documentWorkspaces
};
}
function shortMindmapFileName(mindmapId) {
var raw = String(mindmapId || '').trim();
var pathName = raw.indexOf('/') >= 0 ? raw.split('/').pop() : raw;
if (/\.json$/i.test(pathName)) return pathName;
var digits = raw.match(/(\d{4,})$/);
var suffix = digits ? digits[1].slice(-6) : '';
return suffix ? '思维导图' + suffix + '.json' : '思维导图.json';
}
function parseMindmapApiTarget(input) {
try {
var rawUrl = typeof input === 'string'
? input
: input && typeof input.url === 'string'
? input.url
: '';
if (!rawUrl) return null;
var url = new URL(rawUrl, window.location.origin);
var match = /^\/api\/mindmap\/([^/]+)\/([^/]+)$/.exec(url.pathname);
if (!match) return null;
return {
documentId: decodeURIComponent(match[1]),
mindmapId: decodeURIComponent(match[2])
};
} catch (_) {
return null;
}
}
function requestMethod(input, init) {
return String(
init && init.method
? init.method
: input && typeof input.method === 'string'
? input.method
: 'GET'
).toUpperCase();
}
function requestBodyHasMindmapCreateOnly(init) {
var body = init && typeof init.body === 'string' ? init.body : '';
if (!body) return false;
try {
var payload = JSON.parse(body);
return payload && payload.createOnly === true;
} catch (_) {
return false;
}
}
window.__mnoteFileTreeRuntime = {
readProjection: readProjection,
readSidebarDataset: readSidebarDataset,
@@ -525,5 +598,10 @@ window.__mnoteFileTreeRuntime = {
fileTreeDocumentParentsForPreflight: fileTreeDocumentParentsForPreflight,
fileTreeTargetChildrenForPreflight: fileTreeTargetChildrenForPreflight,
fileTreeDropPreflightRows: fileTreeDropPreflightRows,
fileTreeDocumentWorkspacesForUploadPreflight: fileTreeDocumentWorkspacesForUploadPreflight
fileTreeDocumentWorkspacesForUploadPreflight: fileTreeDocumentWorkspacesForUploadPreflight,
fileTreeUploadTargetPreflightBody: fileTreeUploadTargetPreflightBody,
shortMindmapFileName: shortMindmapFileName,
parseMindmapApiTarget: parseMindmapApiTarget,
requestMethod: requestMethod,
requestBodyHasMindmapCreateOnly: requestBodyHasMindmapCreateOnly
};
+45 -9
View File
@@ -3133,16 +3133,24 @@ const SIDEBAR_TREE_JS: &str = r##"
}
async function preflightFileTreeUploadTarget(detail) {
var workspaceId = String(detail && detail.workspaceId || resolveWorkspaceId(document.body) || '').trim();
var body = {
workspaceId: workspaceId || null,
targetDocumentId: detail && detail.documentId ? String(detail.documentId) : null,
targetRowId: detail && detail.targetRowId ? String(detail.targetRowId) : null,
var bodyRuntime = fileTreeRuntimeFunction('fileTreeUploadTargetPreflightBody');
var body = bodyRuntime ? bodyRuntime(detail || {}, Object.assign({}, fileTreeRuntimeDeps(), {
focusedRowId: sidebarFileTreeSelection.focusedRowId || null,
activeDocumentId: currentDocumentId() || null,
rows: fileTreeRowsForUploadPreflight(),
documentWorkspaces: fileTreeDocumentWorkspacesForUploadPreflight(workspaceId)
};
fileTreeRowsForUploadPreflight: fileTreeRowsForUploadPreflight,
currentDocumentId: currentDocumentId,
resolveWorkspaceId: resolveWorkspaceId
})) : (function() {
var workspaceId = String(detail && detail.workspaceId || resolveWorkspaceId(document.body) || '').trim();
return {
workspaceId: workspaceId || null,
targetDocumentId: detail && detail.documentId ? String(detail.documentId) : null,
targetRowId: detail && detail.targetRowId ? String(detail.targetRowId) : null,
focusedRowId: sidebarFileTreeSelection.focusedRowId || null,
activeDocumentId: currentDocumentId() || null,
rows: fileTreeRowsForUploadPreflight(),
documentWorkspaces: fileTreeDocumentWorkspacesForUploadPreflight(workspaceId)
};
})();
var response = await fetch('/api/tree/filetree/upload-target-preflight', {
method: 'POST',
credentials: 'include',
@@ -3743,6 +3751,8 @@ const SIDEBAR_TREE_JS: &str = r##"
}
function shortMindmapFileName(mindmapId) {
var runtimeFn = fileTreeRuntimeFunction('shortMindmapFileName');
if (runtimeFn) return runtimeFn(mindmapId);
var raw = String(mindmapId || '').trim();
var pathName = raw.indexOf('/') >= 0 ? raw.split('/').pop() : raw;
if (/\.json$/i.test(pathName)) return pathName;
@@ -3752,6 +3762,8 @@ const SIDEBAR_TREE_JS: &str = r##"
}
function parseMindmapApiTarget(input) {
var runtimeFn = fileTreeRuntimeFunction('parseMindmapApiTarget');
if (runtimeFn) return runtimeFn(input);
try {
var rawUrl = typeof input === 'string'
? input
@@ -3793,6 +3805,8 @@ const SIDEBAR_TREE_JS: &str = r##"
}
function requestMethod(input, init) {
var runtimeFn = fileTreeRuntimeFunction('requestMethod');
if (runtimeFn) return runtimeFn(input, init);
return String(
init && init.method
? init.method
@@ -3803,6 +3817,8 @@ const SIDEBAR_TREE_JS: &str = r##"
}
function requestBodyHasMindmapCreateOnly(init) {
var runtimeFn = fileTreeRuntimeFunction('requestBodyHasMindmapCreateOnly');
if (runtimeFn) return runtimeFn(init);
var body = init && typeof init.body === 'string' ? init.body : '';
if (!body) return false;
try {
@@ -11195,6 +11211,11 @@ mod tests {
assert!(
FILETREE_RUNTIME_JS.contains("function fileTreeDocumentWorkspacesForUploadPreflight")
);
assert!(FILETREE_RUNTIME_JS.contains("function fileTreeUploadTargetPreflightBody"));
assert!(FILETREE_RUNTIME_JS.contains("function shortMindmapFileName"));
assert!(FILETREE_RUNTIME_JS.contains("function parseMindmapApiTarget"));
assert!(FILETREE_RUNTIME_JS.contains("function requestMethod"));
assert!(FILETREE_RUNTIME_JS.contains("function requestBodyHasMindmapCreateOnly"));
assert!(FILETREE_RUNTIME_JS.contains("function readProjection"));
assert!(FILETREE_RUNTIME_JS.contains("function readSidebarDataset"));
assert!(FILETREE_RUNTIME_JS.contains("function readDatasetProjection"));
@@ -11229,10 +11250,25 @@ mod tests {
assert!(FILETREE_RUNTIME_JS.contains(
"fileTreeDocumentWorkspacesForUploadPreflight: fileTreeDocumentWorkspacesForUploadPreflight"
));
assert!(FILETREE_RUNTIME_JS
.contains("fileTreeUploadTargetPreflightBody: fileTreeUploadTargetPreflightBody"));
assert!(FILETREE_RUNTIME_JS.contains("shortMindmapFileName: shortMindmapFileName"));
assert!(FILETREE_RUNTIME_JS.contains("parseMindmapApiTarget: parseMindmapApiTarget"));
assert!(FILETREE_RUNTIME_JS.contains("requestMethod: requestMethod"));
assert!(FILETREE_RUNTIME_JS
.contains("requestBodyHasMindmapCreateOnly: requestBodyHasMindmapCreateOnly"));
assert!(SIDEBAR_TREE_JS
.contains("fileTreeRuntimeFunction('fileTreeDocumentWorkspacesForUploadPreflight')"));
assert!(SIDEBAR_TREE_JS
.contains("fileTreeRuntimeFunction('fileTreeUploadTargetPreflightBody')"));
assert!(SIDEBAR_TREE_JS
.contains("fileTreeRowsForUploadPreflight: fileTreeRowsForUploadPreflight"));
assert!(SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('shortMindmapFileName')"));
assert!(SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('parseMindmapApiTarget')"));
assert!(SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('requestMethod')"));
assert!(
SIDEBAR_TREE_JS.contains("fileTreeRuntimeFunction('requestBodyHasMindmapCreateOnly')")
);
assert!(FILETREE_RUNTIME_JS
.contains("uploadIntent: String(detail.uploadIntent || 'filetree.folder.drop')"));
assert!(FILETREE_RUNTIME_JS