fix: open markdown attachments in resource tabs
This commit is contained in:
@@ -78,7 +78,8 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
};
|
||||
|
||||
function closestAction(target, selector) {
|
||||
return target && typeof target.closest === 'function' ? target.closest(selector) : null;
|
||||
var node = target && target.nodeType === Node.TEXT_NODE ? target.parentElement : target;
|
||||
return node && typeof node.closest === 'function' ? node.closest(selector) : null;
|
||||
}
|
||||
|
||||
function toggleWorkspaceSidebar(trigger) {
|
||||
@@ -7135,6 +7136,21 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
}
|
||||
|
||||
function localFileOpenPathFromHref(href) {
|
||||
try {
|
||||
var url = new URL(String(href || ''), window.location.origin);
|
||||
if (url.pathname !== '/api/local-folder/files/open') return '';
|
||||
return String(url.searchParams.get('path') || '').trim();
|
||||
} catch (_) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function fileNameFromPath(path) {
|
||||
var value = String(path || '').trim();
|
||||
return value.indexOf('/') >= 0 ? value.split('/').pop() : value;
|
||||
}
|
||||
|
||||
function isOnlyOfficeAttachmentHref(href) {
|
||||
try {
|
||||
var url = new URL(String(href || ''), window.location.origin);
|
||||
@@ -7169,9 +7185,10 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
function detailFromEditorAttachmentLink(link) {
|
||||
var rawHref = link instanceof HTMLAnchorElement ? link.href : '';
|
||||
var params = attachmentQueryParams(rawHref);
|
||||
var fileName = params.get('fileName') || (link ? link.textContent : '') || '未命名附件';
|
||||
var localFilePath = localFileOpenPathFromHref(rawHref);
|
||||
var fileName = params.get('fileName') || fileNameFromPath(localFilePath) || (link ? link.textContent : '') || '未命名附件';
|
||||
var fileType = params.get('fileType') || inferOnlyOfficeFileType(fileName, '');
|
||||
var assetId = params.get('assetId') || (link ? link.getAttribute('data-asset-id') : '') || '';
|
||||
var assetId = params.get('assetId') || (link ? link.getAttribute('data-asset-id') : '') || (localFilePath ? 'local-file:' + localFilePath : '');
|
||||
var fileUrl = params.get('fileUrl') || '';
|
||||
var href = rawHref;
|
||||
if (!isOnlyOfficeAttachmentHref(rawHref) && fileType) {
|
||||
@@ -7205,14 +7222,16 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
if (!(link instanceof HTMLAnchorElement)) return;
|
||||
var href = link.getAttribute('href') || '';
|
||||
var params = attachmentQueryParams(href);
|
||||
var fileName = params.get('fileName') || link.textContent || '';
|
||||
var localFilePath = localFileOpenPathFromHref(href);
|
||||
var fileName = params.get('fileName') || fileNameFromPath(localFilePath) || link.textContent || '';
|
||||
var className = link.getAttribute('class') || '';
|
||||
var shouldEnhance = isOnlyOfficeAttachmentHref(href)
|
||||
|| className.indexOf('mnote-uploaded-attachment-row') >= 0
|
||||
|| isOfficeFileName(fileName);
|
||||
|| isOfficeFileName(fileName)
|
||||
|| Boolean(localFilePath && (isPdfAttachmentFileName(fileName) || isCodeAttachmentFileName(fileName)));
|
||||
if (!shouldEnhance) return;
|
||||
link.setAttribute('data-mnote-attachment-link', 'true');
|
||||
var assetId = params.get('assetId') || link.getAttribute('data-asset-id') || '';
|
||||
var assetId = params.get('assetId') || link.getAttribute('data-asset-id') || (localFilePath ? 'local-file:' + localFilePath : '');
|
||||
if (assetId) link.setAttribute('data-asset-id', assetId);
|
||||
if (isOnlyOfficeAttachmentHref(href)) {
|
||||
link.setAttribute('href', buildOnlyOfficeOpenPath({
|
||||
@@ -7458,6 +7477,27 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
var attachmentObserver = new MutationObserver(function() { enhanceEditorAttachmentLinks(); });
|
||||
attachmentObserver.observe(document.documentElement, { childList: true, subtree: true });
|
||||
|
||||
function interceptEditorAttachmentLink(event) {
|
||||
var editorAttachmentLink = closestAction(event.target, '.editor-surface .ProseMirror a[data-mnote-attachment-link="true"], .editor-surface .ProseMirror a.mnote-uploaded-attachment-row');
|
||||
if (!(editorAttachmentLink instanceof HTMLAnchorElement)) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (typeof event.stopImmediatePropagation === 'function') event.stopImmediatePropagation();
|
||||
openEditorAttachmentLink(editorAttachmentLink);
|
||||
}
|
||||
|
||||
window.addEventListener('mousedown', interceptEditorAttachmentLink, true);
|
||||
window.addEventListener('click', interceptEditorAttachmentLink, true);
|
||||
|
||||
document.addEventListener('click', function(e) {
|
||||
var editorAttachmentLink = closestAction(e.target, '.editor-surface .ProseMirror a[data-mnote-attachment-link="true"], .editor-surface .ProseMirror a.mnote-uploaded-attachment-row');
|
||||
if (!(editorAttachmentLink instanceof HTMLAnchorElement)) return;
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
if (typeof e.stopImmediatePropagation === 'function') e.stopImmediatePropagation();
|
||||
openEditorAttachmentLink(editorAttachmentLink);
|
||||
}, true);
|
||||
|
||||
document.addEventListener('mouseover', function(event) {
|
||||
var link = closestAction(event.target, '.editor-surface .ProseMirror a[data-mnote-attachment-link="true"], .editor-surface .ProseMirror a.mnote-uploaded-attachment-row');
|
||||
if (!(link instanceof HTMLAnchorElement)) return;
|
||||
|
||||
Reference in New Issue
Block a user