refactor: externalize local upload helpers
Add local-upload-runtime.js as a browser runtime module for upload and attachment helper functions, with sidebar fallback wrappers and a fixed runtime asset route. While validating task479, fix the editor attachment delete path so DOM-selected local attachment links are mapped back to a Tiptap text selection before delete_selection(), preserving undo history and adjacent links.
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
// MNote 本地上传运行时外置模块。
|
||||
// 当前先承接 SIDEBAR_TREE_JS 中的纯辅助函数。
|
||||
// SIDEBAR_TREE_JS 会优先委托到 window.__mnoteLocalUploadRuntime;
|
||||
// 模块未加载时,inline fallback 保持旧行为。
|
||||
|
||||
function uploadedAssetTitle(asset) {
|
||||
return String(asset && (asset.file_name || asset.title || asset.name) || '未命名附件').trim() || '未命名附件';
|
||||
}
|
||||
|
||||
function uploadedAssetUrl(asset) {
|
||||
return String(asset && (asset.sourcePath || asset.file_url || asset.signedUrl || asset.signed_url || asset.thumbnail_url) || '').trim();
|
||||
}
|
||||
|
||||
function currentRootUri() {
|
||||
var params = new URLSearchParams(window.location.search);
|
||||
var fromUrl = (params.get('rootUri') || '').trim();
|
||||
if (fromUrl) return fromUrl;
|
||||
return document.body instanceof HTMLElement
|
||||
? (document.body.getAttribute('data-mnote-root-uri') || '').trim()
|
||||
: '';
|
||||
}
|
||||
|
||||
function localAssetOpenUrl(asset, download, context) {
|
||||
if (!isLocalUploadedAsset(asset)) return '';
|
||||
var rootUri = String(context && context.rootUri || asset && (asset.rootUri || asset.root_uri) || '').trim() || currentRootUri();
|
||||
var rootRelativePath = String(asset && (asset.rootRelativePath || asset.root_relative_path) || '').trim();
|
||||
if (!rootUri || !rootRelativePath) return '';
|
||||
var url = new URL('/api/local-folder/files/open', window.location.origin);
|
||||
url.searchParams.set('rootUri', rootUri);
|
||||
url.searchParams.set('path', rootRelativePath);
|
||||
if (download) url.searchParams.set('download', 'true');
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
function uploadedAssetType(asset) {
|
||||
return String(asset && (asset.asset_type || asset.assetType || asset.mime_type || '') || '').trim();
|
||||
}
|
||||
|
||||
function fileTreeIconKindForFileName(fileName) {
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
var ext = name.indexOf('.') >= 0 ? name.split('.').pop() : '';
|
||||
if (['doc', 'docx', 'odt', 'rtf', 'ppt', 'pptx', 'odp', 'xls', 'xlsx', 'ods', 'csv'].indexOf(ext) >= 0) return 'office';
|
||||
if (ext === 'md' || ext === 'markdown') return 'markdown';
|
||||
if (ext === 'pdf') return 'pdf';
|
||||
if (['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'bmp'].indexOf(ext) >= 0) return 'image';
|
||||
return '';
|
||||
}
|
||||
|
||||
function isLocalUploadedAsset(asset) {
|
||||
var id = String(asset && asset.id || '').trim();
|
||||
return String(asset && asset.sourceKind || '').trim() === 'local_folder' || id.indexOf('local:asset:') === 0;
|
||||
}
|
||||
|
||||
function uploadedAssetExtension(asset) {
|
||||
var match = uploadedAssetTitle(asset).toLowerCase().match(/\.([a-z0-9]+)$/);
|
||||
return match ? match[1] : '';
|
||||
}
|
||||
|
||||
function isNonOfficeAttachmentName(name, ext) {
|
||||
var codeFileNames = [
|
||||
'.dockerignore', '.editorconfig', '.env', '.eslintrc', '.gitattributes', '.gitignore', '.npmrc', '.prettierrc',
|
||||
'dockerfile', 'makefile', 'cmakelists.txt', 'gemfile', 'rakefile', 'procfile'
|
||||
];
|
||||
return [
|
||||
'pdf', 'toml', 'json', 'yaml', 'yml', 'md', 'markdown', 'txt', 'ini', 'env', 'xml', 'html', 'htm', 'css', 'scss',
|
||||
'less', 'js', 'jsx', 'ts', 'tsx', 'mjs', 'cjs', 'py', 'rs', 'go', 'java', 'c', 'cpp', 'h', 'hpp', 'cs',
|
||||
'php', 'rb', 'sh', 'bash', 'zsh', 'sql', 'lock', 'log', 'vue', 'svelte', 'astro', 'jsonc', 'json5',
|
||||
'mts', 'cts', 'lua', 'dart', 'kt', 'kts', 'swift', 'scala', 'gradle', 'groovy', 'clj',
|
||||
'ex', 'exs', 'erl', 'hrl', 'fs', 'fsx', 'r', 'jl', 'm', 'mm', 'pl', 'pm', 'ps1', 'bat', 'cmd',
|
||||
'psm1', 'psd1', 'dockerfile', 'containerfile', 'proto', 'graphql', 'gql', 'prisma', 'tf', 'tfvars',
|
||||
'hcl', 'nix', 'cmake', 'bazel', 'bzl', 'properties', 'conf', 'cfg', 'config', 'service', 'desktop',
|
||||
'gitignore', 'gitattributes', 'editorconfig', 'npmrc', 'prettierrc', 'eslintrc'
|
||||
].indexOf(ext) >= 0 || codeFileNames.indexOf(name) >= 0;
|
||||
}
|
||||
|
||||
function attachmentExtensionFromFileName(fileName) {
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
return name.indexOf('.') >= 0 ? name.split('.').pop() : '';
|
||||
}
|
||||
|
||||
function isPdfAttachmentFileName(fileName) {
|
||||
return attachmentExtensionFromFileName(fileName) === 'pdf';
|
||||
}
|
||||
|
||||
function isCodeAttachmentFileName(fileName) {
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
var ext = attachmentExtensionFromFileName(name);
|
||||
return ext !== 'pdf' && isNonOfficeAttachmentName(name, ext);
|
||||
}
|
||||
|
||||
function inferCodeAttachmentLanguage(fileName) {
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
var ext = attachmentExtensionFromFileName(name);
|
||||
var byName = {
|
||||
'dockerfile': 'dockerfile',
|
||||
'makefile': 'makefile',
|
||||
'cmakelists.txt': 'cmake',
|
||||
'.gitignore': 'gitignore',
|
||||
'.gitattributes': 'gitattributes',
|
||||
'.editorconfig': 'ini',
|
||||
'.env': 'dotenv'
|
||||
};
|
||||
if (byName[name]) return byName[name];
|
||||
var byExt = {
|
||||
bash: 'bash', bat: 'batch', c: 'c', cjs: 'javascript', cmd: 'batch', conf: 'text', cpp: 'cpp',
|
||||
cs: 'csharp', css: 'css', cts: 'typescript', dart: 'dart', dockerfile: 'dockerfile', env: 'dotenv',
|
||||
go: 'go', gql: 'graphql', gradle: 'groovy', graphql: 'graphql', h: 'c', hcl: 'hcl', hpp: 'cpp',
|
||||
htm: 'html', html: 'html', ini: 'ini', java: 'java', js: 'javascript', json: 'json', json5: 'json',
|
||||
jsonc: 'jsonc', jsx: 'javascript', kt: 'kotlin', kts: 'kotlin', less: 'less', log: 'text',
|
||||
lua: 'lua', m: 'objective-c', markdown: 'markdown', md: 'markdown', mjs: 'javascript',
|
||||
mts: 'typescript', nix: 'nix', php: 'php', pl: 'perl', pm: 'perl', prisma: 'prisma',
|
||||
proto: 'protobuf', ps1: 'powershell', py: 'python', r: 'r', rb: 'ruby', rs: 'rust',
|
||||
scss: 'scss', sh: 'bash', sql: 'sql', svelte: 'svelte', swift: 'swift', tf: 'terraform',
|
||||
tfvars: 'terraform', toml: 'toml', ts: 'typescript', tsx: 'typescript', txt: 'text',
|
||||
vue: 'vue', xml: 'xml', yaml: 'yaml', yml: 'yaml', zsh: 'bash'
|
||||
};
|
||||
return byExt[ext] || 'text';
|
||||
}
|
||||
|
||||
function attachmentClassForFileName(fileName) {
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
var ext = name.indexOf('.') >= 0 ? name.split('.').pop() : '';
|
||||
if (['doc', 'docx', 'odt', 'rtf'].indexOf(ext) >= 0) return 'mnote-uploaded-attachment-row mnote-uploaded-attachment-word';
|
||||
if (['ppt', 'pptx', 'odp'].indexOf(ext) >= 0) return 'mnote-uploaded-attachment-row mnote-uploaded-attachment-ppt';
|
||||
if (['xls', 'xlsx', 'ods', 'csv'].indexOf(ext) >= 0) return 'mnote-uploaded-attachment-row mnote-uploaded-attachment-sheet';
|
||||
if (ext === 'pdf') return 'mnote-uploaded-attachment-row mnote-uploaded-attachment-pdf';
|
||||
if (isNonOfficeAttachmentName(name, ext)) {
|
||||
return 'mnote-uploaded-attachment-row mnote-uploaded-attachment-code';
|
||||
}
|
||||
return 'mnote-uploaded-attachment-row mnote-uploaded-attachment-file';
|
||||
}
|
||||
|
||||
function uploadedAttachmentClass(asset) {
|
||||
return attachmentClassForFileName(uploadedAssetTitle(asset));
|
||||
}
|
||||
|
||||
function uploadedFileSize(asset) {
|
||||
var size = Number(asset && (asset.file_size || asset.fileSize) || 0);
|
||||
if (!Number.isFinite(size) || size <= 0) return '';
|
||||
if (size >= 1024 * 1024) return (size / 1024 / 1024).toFixed(size >= 10 * 1024 * 1024 ? 1 : 2) + ' MB';
|
||||
if (size >= 1024) return (size / 1024).toFixed(size >= 100 * 1024 ? 0 : 2) + ' KB';
|
||||
return String(Math.round(size)) + ' B';
|
||||
}
|
||||
|
||||
window.__mnoteLocalUploadRuntime = {
|
||||
uploadedAssetTitle: uploadedAssetTitle,
|
||||
uploadedAssetUrl: uploadedAssetUrl,
|
||||
localAssetOpenUrl: localAssetOpenUrl,
|
||||
uploadedAssetType: uploadedAssetType,
|
||||
fileTreeIconKindForFileName: fileTreeIconKindForFileName,
|
||||
isLocalUploadedAsset: isLocalUploadedAsset,
|
||||
uploadedAssetExtension: uploadedAssetExtension,
|
||||
isNonOfficeAttachmentName: isNonOfficeAttachmentName,
|
||||
attachmentExtensionFromFileName: attachmentExtensionFromFileName,
|
||||
isPdfAttachmentFileName: isPdfAttachmentFileName,
|
||||
isCodeAttachmentFileName: isCodeAttachmentFileName,
|
||||
inferCodeAttachmentLanguage: inferCodeAttachmentLanguage,
|
||||
attachmentClassForFileName: attachmentClassForFileName,
|
||||
uploadedAttachmentClass: uploadedAttachmentClass,
|
||||
uploadedFileSize: uploadedFileSize
|
||||
};
|
||||
@@ -98,6 +98,10 @@ pub fn build_router(state: AppState) -> Router {
|
||||
"/api/mnote-browser-runtime/resource-open-runtime.js",
|
||||
get(web_shell::resource_open_runtime_asset),
|
||||
)
|
||||
.route(
|
||||
"/api/mnote-browser-runtime/local-upload-runtime.js",
|
||||
get(web_shell::local_upload_runtime_asset),
|
||||
)
|
||||
.route(
|
||||
"/api/mnote-browser-runtime/tree-live-controller.js",
|
||||
get(web_shell::tree_live_controller_runtime_asset),
|
||||
|
||||
@@ -4381,6 +4381,20 @@ pub async fn resource_open_runtime_asset() -> Response {
|
||||
.unwrap_or_else(|_| Response::new(Body::empty()))
|
||||
}
|
||||
|
||||
pub async fn local_upload_runtime_asset() -> Response {
|
||||
const JS: &str = include_str!("../../browser/local-upload-runtime.js");
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
"application/javascript; charset=utf-8",
|
||||
)
|
||||
.header(header::CACHE_CONTROL, "no-store")
|
||||
.header(HEADER_MNOTE_WEB_OWNER, "mnote-web")
|
||||
.body(Body::from(JS))
|
||||
.unwrap_or_else(|_| Response::new(Body::empty()))
|
||||
}
|
||||
|
||||
pub async fn tree_live_controller_runtime_asset() -> Response {
|
||||
const JS: &str = include_str!("../../browser/tree-live-controller.js");
|
||||
Response::builder()
|
||||
|
||||
@@ -3140,15 +3140,27 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
}
|
||||
|
||||
function localUploadRuntimeFunction(name) {
|
||||
var runtime = window.__mnoteLocalUploadRuntime;
|
||||
var fn = runtime && runtime[name];
|
||||
return typeof fn === 'function' ? fn : null;
|
||||
}
|
||||
|
||||
function uploadedAssetTitle(asset) {
|
||||
var runtimeFn = localUploadRuntimeFunction('uploadedAssetTitle');
|
||||
if (runtimeFn) return runtimeFn(asset);
|
||||
return String(asset && (asset.file_name || asset.title || asset.name) || '未命名附件').trim() || '未命名附件';
|
||||
}
|
||||
|
||||
function uploadedAssetUrl(asset) {
|
||||
var runtimeFn = localUploadRuntimeFunction('uploadedAssetUrl');
|
||||
if (runtimeFn) return runtimeFn(asset);
|
||||
return String(asset && (asset.sourcePath || asset.file_url || asset.signedUrl || asset.signed_url || asset.thumbnail_url) || '').trim();
|
||||
}
|
||||
|
||||
function localAssetOpenUrl(asset, download) {
|
||||
var runtimeFn = localUploadRuntimeFunction('localAssetOpenUrl');
|
||||
if (runtimeFn) return runtimeFn(asset, download, { rootUri: currentRootUri() });
|
||||
if (!isLocalUploadedAsset(asset)) return '';
|
||||
var rootUri = String(asset && (asset.rootUri || asset.root_uri) || '').trim() || currentRootUri();
|
||||
var rootRelativePath = String(asset && (asset.rootRelativePath || asset.root_relative_path) || '').trim();
|
||||
@@ -3161,10 +3173,14 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
|
||||
function uploadedAssetType(asset) {
|
||||
var runtimeFn = localUploadRuntimeFunction('uploadedAssetType');
|
||||
if (runtimeFn) return runtimeFn(asset);
|
||||
return String(asset && (asset.asset_type || asset.assetType || asset.mime_type || '') || '').trim();
|
||||
}
|
||||
|
||||
function fileTreeIconKindForFileName(fileName) {
|
||||
var runtimeFn = localUploadRuntimeFunction('fileTreeIconKindForFileName');
|
||||
if (runtimeFn) return runtimeFn(fileName);
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
var ext = name.indexOf('.') >= 0 ? name.split('.').pop() : '';
|
||||
if (['doc', 'docx', 'odt', 'rtf', 'ppt', 'pptx', 'odp', 'xls', 'xlsx', 'ods', 'csv'].indexOf(ext) >= 0) return 'office';
|
||||
@@ -3179,16 +3195,22 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
|
||||
function isLocalUploadedAsset(asset) {
|
||||
var runtimeFn = localUploadRuntimeFunction('isLocalUploadedAsset');
|
||||
if (runtimeFn) return runtimeFn(asset);
|
||||
var id = String(asset && asset.id || '').trim();
|
||||
return String(asset && asset.sourceKind || '').trim() === 'local_folder' || id.indexOf('local:asset:') === 0;
|
||||
}
|
||||
|
||||
function uploadedAssetExtension(asset) {
|
||||
var runtimeFn = localUploadRuntimeFunction('uploadedAssetExtension');
|
||||
if (runtimeFn) return runtimeFn(asset);
|
||||
var match = uploadedAssetTitle(asset).toLowerCase().match(/\.([a-z0-9]+)$/);
|
||||
return match ? match[1] : '';
|
||||
}
|
||||
|
||||
function isNonOfficeAttachmentName(name, ext) {
|
||||
var runtimeFn = localUploadRuntimeFunction('isNonOfficeAttachmentName');
|
||||
if (runtimeFn) return runtimeFn(name, ext);
|
||||
var codeFileNames = [
|
||||
'.dockerignore', '.editorconfig', '.env', '.eslintrc', '.gitattributes', '.gitignore', '.npmrc', '.prettierrc',
|
||||
'dockerfile', 'makefile', 'cmakelists.txt', 'gemfile', 'rakefile', 'procfile'
|
||||
@@ -3206,21 +3228,29 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
|
||||
function attachmentExtensionFromFileName(fileName) {
|
||||
var runtimeFn = localUploadRuntimeFunction('attachmentExtensionFromFileName');
|
||||
if (runtimeFn) return runtimeFn(fileName);
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
return name.indexOf('.') >= 0 ? name.split('.').pop() : '';
|
||||
}
|
||||
|
||||
function isPdfAttachmentFileName(fileName) {
|
||||
var runtimeFn = localUploadRuntimeFunction('isPdfAttachmentFileName');
|
||||
if (runtimeFn) return runtimeFn(fileName);
|
||||
return attachmentExtensionFromFileName(fileName) === 'pdf';
|
||||
}
|
||||
|
||||
function isCodeAttachmentFileName(fileName) {
|
||||
var runtimeFn = localUploadRuntimeFunction('isCodeAttachmentFileName');
|
||||
if (runtimeFn) return runtimeFn(fileName);
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
var ext = attachmentExtensionFromFileName(name);
|
||||
return ext !== 'pdf' && isNonOfficeAttachmentName(name, ext);
|
||||
}
|
||||
|
||||
function inferCodeAttachmentLanguage(fileName) {
|
||||
var runtimeFn = localUploadRuntimeFunction('inferCodeAttachmentLanguage');
|
||||
if (runtimeFn) return runtimeFn(fileName);
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
var ext = attachmentExtensionFromFileName(name);
|
||||
var byName = {
|
||||
@@ -3250,6 +3280,8 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
|
||||
function attachmentClassForFileName(fileName) {
|
||||
var runtimeFn = localUploadRuntimeFunction('attachmentClassForFileName');
|
||||
if (runtimeFn) return runtimeFn(fileName);
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
var ext = name.indexOf('.') >= 0 ? name.split('.').pop() : '';
|
||||
if (['doc', 'docx', 'odt', 'rtf'].indexOf(ext) >= 0) return 'mnote-uploaded-attachment-row mnote-uploaded-attachment-word';
|
||||
@@ -3263,6 +3295,8 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
|
||||
function uploadedAttachmentClass(asset) {
|
||||
var runtimeFn = localUploadRuntimeFunction('uploadedAttachmentClass');
|
||||
if (runtimeFn) return runtimeFn(asset);
|
||||
return attachmentClassForFileName(uploadedAssetTitle(asset));
|
||||
}
|
||||
|
||||
@@ -3296,6 +3330,8 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
|
||||
function uploadedFileSize(asset) {
|
||||
var runtimeFn = localUploadRuntimeFunction('uploadedFileSize');
|
||||
if (runtimeFn) return runtimeFn(asset);
|
||||
var size = Number(asset && (asset.file_size || asset.fileSize) || 0);
|
||||
if (!Number.isFinite(size) || size <= 0) return '';
|
||||
if (size >= 1024 * 1024) return (size / 1024 / 1024).toFixed(size >= 10 * 1024 * 1024 ? 1 : 2) + ' MB';
|
||||
@@ -10417,6 +10453,7 @@ pub fn PageLayout(
|
||||
<div hidden data-testid="mnote-admin-access-policy-template-user" inner_html={user_access_policy_template}></div>
|
||||
<script inner_html={crate::ssr::pages::admin::ADMIN_POLICY_SCRIPT.to_string()}></script>
|
||||
<script type="module" src="/api/mnote-browser-runtime/resource-open-runtime.js"></script>
|
||||
<script type="module" src="/api/mnote-browser-runtime/local-upload-runtime.js"></script>
|
||||
<script inner_html={SIDEBAR_TREE_JS.to_string()}></script>
|
||||
<script type="module" src="/api/mnote-browser-runtime/tree-live-controller.js"></script>
|
||||
</aside>
|
||||
|
||||
Reference in New Issue
Block a user