refactor: delegate filetree asset append
This commit is contained in:
@@ -120,6 +120,119 @@ function fileTreeChildCount(documentId, deps) {
|
||||
return children.length;
|
||||
}
|
||||
|
||||
function defaultEscapeHtml(value) {
|
||||
return String(value == null ? '' : value)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function defaultObjectIdentityAttr(identity) {
|
||||
try {
|
||||
return JSON.stringify(identity || {});
|
||||
} catch (_) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function revealFileTreeRow(row) {
|
||||
if (!(row instanceof HTMLElement)) return;
|
||||
var node = row.closest('.tree-node');
|
||||
while (node && node.parentElement) {
|
||||
if (node.parentElement.classList && node.parentElement.classList.contains('tree-children')) {
|
||||
node.parentElement.classList.remove('tree-children--collapsed');
|
||||
var parentNode = node.parentElement.closest('.tree-node');
|
||||
var parentRow = parentNode ? parentNode.querySelector(':scope > .tree-row') : null;
|
||||
if (parentRow instanceof HTMLElement) {
|
||||
parentRow.setAttribute('aria-expanded', 'true');
|
||||
var toggle = parentRow.querySelector('[data-rust-action="toggle"]');
|
||||
if (toggle) toggle.setAttribute('aria-expanded', 'true');
|
||||
}
|
||||
}
|
||||
node = node.parentElement.closest('.tree-node');
|
||||
}
|
||||
try { row.scrollIntoView({ block: 'nearest' }); } catch (_) {}
|
||||
}
|
||||
|
||||
function revealFileTreeAssetRow(assetId, deps) {
|
||||
if (!assetId) return false;
|
||||
var cssEscape = deps && typeof deps.cssEscape === 'function' ? deps.cssEscape : function(value) { return String(value).replace(/["\\]/g, '\\$&'); };
|
||||
var row = document.querySelector('.tree-row[data-shell-mode="filetree"][data-asset-id="' + cssEscape(assetId) + '"]');
|
||||
if (!(row instanceof HTMLElement)) return false;
|
||||
revealFileTreeRow(row);
|
||||
return true;
|
||||
}
|
||||
|
||||
function appendUploadedAssetRow(asset, documentId, deps) {
|
||||
deps = deps || {};
|
||||
var cssEscape = typeof deps.cssEscape === 'function' ? deps.cssEscape : function(value) { return String(value).replace(/["\\]/g, '\\$&'); };
|
||||
var escapeHtml = typeof deps.escapeHtml === 'function' ? deps.escapeHtml : defaultEscapeHtml;
|
||||
var objectIdentityAttr = typeof deps.objectIdentityAttr === 'function' ? deps.objectIdentityAttr : defaultObjectIdentityAttr;
|
||||
var uploadedAssetTitle = typeof deps.uploadedAssetTitle === 'function' ? deps.uploadedAssetTitle : function(nextAsset) {
|
||||
return String(nextAsset && (nextAsset.file_name || nextAsset.title || nextAsset.name) || '未命名附件').trim() || '未命名附件';
|
||||
};
|
||||
var uploadedAssetType = typeof deps.uploadedAssetType === 'function' ? deps.uploadedAssetType : function(nextAsset) {
|
||||
return String(nextAsset && (nextAsset.asset_type || nextAsset.assetType || nextAsset.mime_type || '') || '').trim();
|
||||
};
|
||||
var fileTreeIconKindForFileName = typeof deps.fileTreeIconKindForFileName === 'function' ? deps.fileTreeIconKindForFileName : function() { return ''; };
|
||||
var currentDocumentId = typeof deps.currentDocumentId === 'function' ? deps.currentDocumentId : function() { return ''; };
|
||||
var currentSourceKind = typeof deps.currentSourceKind === 'function' ? deps.currentSourceKind : function() { return ''; };
|
||||
var assetId = String(asset && asset.id || '').trim();
|
||||
if (!assetId) return false;
|
||||
if (revealFileTreeAssetRow(assetId, { cssEscape: cssEscape })) return true;
|
||||
var objectKind = String(asset && (asset.objectKind || asset.resourceKind || '') || '').trim();
|
||||
if (!objectKind && (String(asset && (asset.asset_type || asset.assetType) || '').trim() === 'mindmap' || /\.mindmap\.json$/i.test(assetId))) {
|
||||
objectKind = 'mindmap';
|
||||
}
|
||||
var targetDocumentId = String(documentId || asset.document_id || asset.documentId || currentDocumentId() || '').trim();
|
||||
var parentRow = targetDocumentId
|
||||
? document.querySelector('.tree-row[data-shell-mode="filetree"][data-row-id="' + cssEscape('doc:' + targetDocumentId) + '"]')
|
||||
: null;
|
||||
if (!parentRow && currentSourceKind() === 'local_folder' && objectKind === 'mindmap') return false;
|
||||
if (!parentRow) parentRow = document.querySelector('.tree-row[data-shell-mode="filetree"][data-row-kind="document"]');
|
||||
var root = document.querySelector('#sidebar-file-tree-root .tree-root');
|
||||
if (!root && !parentRow) return false;
|
||||
var parentLi = parentRow ? parentRow.closest('.tree-node') : null;
|
||||
var children = parentLi ? parentLi.querySelector(':scope > .tree-children') : null;
|
||||
if (parentLi && !children) {
|
||||
children = document.createElement('ul');
|
||||
children.className = 'tree-children';
|
||||
parentLi.appendChild(children);
|
||||
}
|
||||
if (children) {
|
||||
children.classList.remove('tree-children--collapsed');
|
||||
if (parentRow) {
|
||||
parentRow.setAttribute('aria-expanded', 'true');
|
||||
var toggle = parentRow.querySelector('[data-rust-action="toggle"]');
|
||||
if (toggle) toggle.setAttribute('aria-expanded', 'true');
|
||||
}
|
||||
}
|
||||
var container = children || root;
|
||||
var li = document.createElement('li');
|
||||
li.className = 'tree-node';
|
||||
var objectIdentity = {
|
||||
objectKind: objectKind || 'attachment',
|
||||
documentId: targetDocumentId || null,
|
||||
blockId: null,
|
||||
assetId: assetId
|
||||
};
|
||||
li.setAttribute('data-node-id', 'asset:' + assetId);
|
||||
var title = uploadedAssetTitle(asset);
|
||||
var iconKind = fileTreeIconKindForFileName(title) || uploadedAssetType(asset) || 'file';
|
||||
if (objectKind === 'mindmap') iconKind = 'mindmap';
|
||||
li.innerHTML =
|
||||
'<div class="tree-row" role="treeitem" aria-level="' + (parentRow ? '2' : '1') + '" aria-expanded="false" data-rust-rendered-row="filetree" data-testid="filetree-asset-row" data-row-id="' + escapeHtml('asset:' + assetId) + '" data-row-kind="asset" data-node-id="' + escapeHtml('asset:' + assetId) + '" data-document-id="' + escapeHtml(targetDocumentId) + '" data-doc-id="' + escapeHtml(targetDocumentId) + '" data-asset-id="' + escapeHtml(assetId) + '" data-object-identity="' + escapeHtml(objectIdentityAttr(objectIdentity)) + '" data-object-kind="' + escapeHtml(objectKind || 'attachment') + '" data-shell-mode="filetree" data-selected="false" data-active="false" draggable="true">' +
|
||||
'<span class="tree-spacer" aria-hidden="true"></span><span class="tree-kind-badge" data-kind="' + escapeHtml(iconKind) + '" aria-hidden="true"></span>' +
|
||||
'<button type="button" class="tree-link" data-rust-action="open" data-row-id="' + escapeHtml('asset:' + assetId) + '" data-document-id="' + escapeHtml(targetDocumentId) + '" data-asset-id="' + escapeHtml(assetId) + '"><span class="tree-link-title">' + escapeHtml(title) + '</span></button>' +
|
||||
'<div class="tree-actions"><button type="button" class="tree-action" data-testid="filetree-action-menu" data-rust-action="menu" data-row-id="' + escapeHtml('asset:' + assetId) + '" aria-label="更多操作">…</button></div></div>';
|
||||
container.appendChild(li);
|
||||
revealFileTreeRow(li.querySelector('.tree-row'));
|
||||
document.documentElement.setAttribute('data-mnote-last-upload-asset-id', assetId);
|
||||
return true;
|
||||
}
|
||||
|
||||
function fileTreeUploadTargetFallback(detail, deps) {
|
||||
detail = detail || {};
|
||||
deps = deps || {};
|
||||
@@ -161,5 +274,8 @@ window.__mnoteFileTreeRuntime = {
|
||||
fileTreeAssetDownloadDetail: fileTreeAssetDownloadDetail,
|
||||
fileTreeRowsByRowIds: fileTreeRowsByRowIds,
|
||||
fileTreeChildCount: fileTreeChildCount,
|
||||
revealFileTreeRow: revealFileTreeRow,
|
||||
revealFileTreeAssetRow: revealFileTreeAssetRow,
|
||||
appendUploadedAssetRow: appendUploadedAssetRow,
|
||||
fileTreeUploadTargetFallback: fileTreeUploadTargetFallback
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user