refactor: delegate media asset upload runtime

This commit is contained in:
lix-2026
2026-05-25 02:24:48 +08:00
parent a3abeadfe8
commit d22c45dfa5
4 changed files with 92 additions and 2 deletions
@@ -298,6 +298,66 @@ async function insertUploadedAssetIntoEditor(asset, targetRoot, deps) {
return false;
}
async function uploadFileToMediaAsset(file, plan, options, deps) {
deps = deps || {};
options = options || {};
var getCurrentSourceKind = typeof deps.currentSourceKind === 'function' ? deps.currentSourceKind : function() { return ''; };
var getCurrentRootUri = typeof deps.currentRootUri === 'function' ? deps.currentRootUri : currentRootUri;
var getCurrentDocumentId = typeof deps.currentDocumentId === 'function' ? deps.currentDocumentId : function() { return ''; };
var resolveEditorRoot = typeof deps.editorRootFromUploadOptions === 'function'
? deps.editorRootFromUploadOptions
: function(nextOptions) { return editorRootFromUploadOptions(nextOptions, deps); };
var insertIntoEditor = typeof deps.insertUploadedAssetIntoEditor === 'function'
? deps.insertUploadedAssetIntoEditor
: function(asset, targetRoot) { return insertUploadedAssetIntoEditor(asset, targetRoot, deps); };
var dispatch = typeof deps.dispatchEvent === 'function'
? deps.dispatchEvent
: function(event) { return window.dispatchEvent(event); };
var EventCtor = typeof deps.CustomEvent === 'function' ? deps.CustomEvent : window.CustomEvent;
if (getCurrentSourceKind() === 'local_folder') {
var rootUri = getCurrentRootUri();
var documentId = String(plan && plan.targetDocumentId || getCurrentDocumentId() || '').trim();
var localResult = await uploadLocalFolderAsset(file, plan, {
rootUri: rootUri,
documentId: documentId,
timeoutMs: 15000
});
var localAsset = localResult && localResult.asset ? localResult.asset : null;
if (!localAsset) {
throw new Error('上传失败');
}
if (options.insertIntoEditor) {
await insertIntoEditor(localAsset, resolveEditorRoot(options));
}
if (typeof deps.refreshLocalFolderSidebarSnapshot === 'function') {
void deps.refreshLocalFolderSidebarSnapshot();
}
dispatch(new EventCtor('wolai:local-assets-changed', {
detail: { docId: documentId, asset: localAsset, assetIds: [localAsset.id] }
}));
return localAsset;
}
var mediaResult = await uploadMediaAsset(file, plan, { timeoutMs: 15000 });
var mediaAsset = mediaResult && mediaResult.asset ? mediaResult.asset : null;
if (!mediaAsset) {
throw new Error('上传失败');
}
if (typeof deps.appendUploadedAssetRow === 'function') {
deps.appendUploadedAssetRow(mediaAsset, plan && plan.targetDocumentId);
}
if (options.insertIntoEditor) {
await insertIntoEditor(mediaAsset, resolveEditorRoot(options));
}
dispatch(new EventCtor('wolai:assets-changed', {
detail: {
docId: plan && plan.targetDocumentId,
asset: mediaAsset,
assetIds: [mediaAsset.id]
}
}));
return mediaAsset;
}
async function uploadFilesWithResolvedTarget(files, detail, options, deps) {
deps = deps || {};
var resolveFileTreeUploadTarget = typeof deps.resolveFileTreeUploadTarget === 'function' ? deps.resolveFileTreeUploadTarget : null;
@@ -460,6 +520,7 @@ window.__mnoteLocalUploadRuntime = {
uploadLocalFolderAsset: uploadLocalFolderAsset,
uploadMediaAsset: uploadMediaAsset,
insertUploadedAssetIntoEditor: insertUploadedAssetIntoEditor,
uploadFileToMediaAsset: uploadFileToMediaAsset,
uploadFilesWithResolvedTarget: uploadFilesWithResolvedTarget,
localAssetOpenUrl: localAssetOpenUrl,
uploadedAssetType: uploadedAssetType,
@@ -4014,6 +4014,25 @@ const SIDEBAR_TREE_JS: &str = r##"
}
async function uploadFileToMediaAsset(file, plan, options) {
var runtimeFn = localUploadRuntimeFunction('uploadFileToMediaAsset');
if (runtimeFn) {
return await runtimeFn(file, plan, options || {}, {
currentSourceKind: currentSourceKind,
currentRootUri: currentRootUri,
currentDocumentId: currentDocumentId,
editorRootFromUploadOptions: editorRootFromUploadOptions,
appendUploadedAssetRow: appendUploadedAssetRow,
refreshLocalFolderSidebarSnapshot: refreshLocalFolderSidebarSnapshot,
dispatchEvent: function(event) { return window.dispatchEvent(event); },
CustomEvent: CustomEvent,
buildOnlyOfficeAssetOpenUrl: buildOnlyOfficeAssetOpenUrl,
fetchCurrentOnlyOfficeUserId: fetchCurrentOnlyOfficeUserId,
buildOnlyOfficeOpenPath: buildOnlyOfficeOpenPath,
inferOnlyOfficeFileType: inferOnlyOfficeFileType,
enhanceEditorAttachmentLinks: enhanceEditorAttachmentLinks,
cssEscape: cssEscape
});
}
if (currentSourceKind() === 'local_folder') {
var rootUri = currentRootUri();
var documentId = String(plan && plan.targetDocumentId || currentDocumentId() || '').trim();
@@ -10972,6 +10991,7 @@ mod tests {
assert!(
SIDEBAR_TREE_JS.contains("localUploadRuntimeFunction('insertUploadedAssetIntoEditor')")
);
assert!(SIDEBAR_TREE_JS.contains("localUploadRuntimeFunction('uploadFileToMediaAsset')"));
assert!(SIDEBAR_TREE_JS.contains("localUploadRuntimeFunction('uploadMediaAsset')"));
assert!(
SIDEBAR_TREE_JS.contains("localUploadRuntimeFunction('uploadFilesWithResolvedTarget')")
@@ -10993,8 +11013,10 @@ mod tests {
assert!(LOCAL_UPLOAD_RUNTIME_JS.contains("function uploadLocalFolderAsset"));
assert!(LOCAL_UPLOAD_RUNTIME_JS.contains("function uploadMediaAsset"));
assert!(LOCAL_UPLOAD_RUNTIME_JS.contains("function insertUploadedAssetIntoEditor"));
assert!(LOCAL_UPLOAD_RUNTIME_JS.contains("function uploadFileToMediaAsset"));
assert!(LOCAL_UPLOAD_RUNTIME_JS.contains("function uploadFilesWithResolvedTarget"));
assert!(LOCAL_UPLOAD_RUNTIME_JS.contains("data-mnote-last-upload-inserted"));
assert!(LOCAL_UPLOAD_RUNTIME_JS.contains("uploadFileToMediaAsset: uploadFileToMediaAsset"));
assert!(LOCAL_UPLOAD_RUNTIME_JS.contains("uploadFilesWithResolvedTarget"));
assert!(LOCAL_UPLOAD_RUNTIME_JS.contains("__mnoteLastEditorUploadRoot"));
}