refactor: extract filetree preflight helpers
This commit is contained in:
@@ -371,6 +371,94 @@ function fileTreeUploadTargetFallback(detail, deps) {
|
||||
};
|
||||
}
|
||||
|
||||
function fileTreeRowsForUploadPreflight(deps) {
|
||||
deps = deps || {};
|
||||
var fileTreeRowKind = typeof deps.fileTreeRowKind === 'function' ? deps.fileTreeRowKind : function(row) {
|
||||
return row instanceof HTMLElement ? String(row.getAttribute('data-row-kind') || '').trim() : '';
|
||||
};
|
||||
var fileTreeRowDocumentId = typeof deps.fileTreeRowDocumentId === 'function' ? deps.fileTreeRowDocumentId : function(row) {
|
||||
return row instanceof HTMLElement ? String(row.getAttribute('data-document-id') || row.getAttribute('data-doc-id') || '').trim() : '';
|
||||
};
|
||||
var fileTreeRowAssetId = typeof deps.fileTreeRowAssetId === 'function' ? deps.fileTreeRowAssetId : function(row) {
|
||||
return row instanceof HTMLElement ? String(row.getAttribute('data-asset-id') || '').trim() : '';
|
||||
};
|
||||
return Array.from(document.querySelectorAll('.tree-row[data-shell-mode="filetree"]')).map(function(row) {
|
||||
var documentId = fileTreeRowDocumentId(row);
|
||||
var assetId = fileTreeRowAssetId(row) || null;
|
||||
var badge = row.querySelector('.tree-kind-badge');
|
||||
return {
|
||||
rowId: row.getAttribute('data-row-id') || '',
|
||||
rowKind: fileTreeRowKind(row),
|
||||
documentId: documentId || null,
|
||||
assetId: assetId,
|
||||
assetDocumentId: documentId || null,
|
||||
assetType: badge ? badge.getAttribute('data-kind') : null,
|
||||
storagePath: null
|
||||
};
|
||||
}).filter(function(row) {
|
||||
return row.rowId || row.documentId || row.assetId;
|
||||
});
|
||||
}
|
||||
|
||||
function fileTreeDocumentParentsForPreflight(deps) {
|
||||
deps = deps || {};
|
||||
var fileTreeRowDocumentId = typeof deps.fileTreeRowDocumentId === 'function' ? deps.fileTreeRowDocumentId : function(row) {
|
||||
return row instanceof HTMLElement ? String(row.getAttribute('data-document-id') || row.getAttribute('data-doc-id') || '').trim() : '';
|
||||
};
|
||||
return Array.from(document.querySelectorAll('#sidebar-file-tree-root .tree-row[data-shell-mode="filetree"]')).map(function(row) {
|
||||
var documentId = fileTreeRowDocumentId(row);
|
||||
if (!documentId) return null;
|
||||
var parentRowId = row.getAttribute('data-parent-id') || '';
|
||||
var parentDocumentId = parentRowId.indexOf('doc:') === 0 ? parentRowId.slice(4) : parentRowId || null;
|
||||
return { documentId: documentId, parentId: parentDocumentId };
|
||||
}).filter(Boolean);
|
||||
}
|
||||
|
||||
function fileTreeTargetChildrenForPreflight(targetRow, deps) {
|
||||
deps = deps || {};
|
||||
var fileTreeRowKind = typeof deps.fileTreeRowKind === 'function' ? deps.fileTreeRowKind : function(row) {
|
||||
return row instanceof HTMLElement ? String(row.getAttribute('data-row-kind') || '').trim() : '';
|
||||
};
|
||||
var fileTreeRowDocumentId = typeof deps.fileTreeRowDocumentId === 'function' ? deps.fileTreeRowDocumentId : function(row) {
|
||||
return row instanceof HTMLElement ? String(row.getAttribute('data-document-id') || row.getAttribute('data-doc-id') || '').trim() : '';
|
||||
};
|
||||
var fileTreeRowAssetId = typeof deps.fileTreeRowAssetId === 'function' ? deps.fileTreeRowAssetId : function(row) {
|
||||
return row instanceof HTMLElement ? String(row.getAttribute('data-asset-id') || '').trim() : '';
|
||||
};
|
||||
var rowTitle = typeof deps.rowTitle === 'function' ? deps.rowTitle : function(row) {
|
||||
if (!(row instanceof HTMLElement)) return '';
|
||||
var titleNode = row.querySelector('.tree-link-title');
|
||||
return String(titleNode && titleNode.textContent || row.getAttribute('title') || '').trim();
|
||||
};
|
||||
var node = targetRow instanceof HTMLElement ? targetRow.closest('.tree-node') : null;
|
||||
if (!node) return [];
|
||||
return Array.from(node.querySelectorAll(':scope > .tree-children > .tree-node > .tree-row[data-shell-mode="filetree"]')).map(function(row) {
|
||||
return {
|
||||
rowKind: fileTreeRowKind(row),
|
||||
documentId: fileTreeRowDocumentId(row) || null,
|
||||
assetId: fileTreeRowAssetId(row) || null,
|
||||
title: rowTitle(row)
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function fileTreeDropPreflightRows(deps) {
|
||||
deps = deps || {};
|
||||
var cssEscape = typeof deps.cssEscape === 'function' ? deps.cssEscape : function(value) { return String(value).replace(/["\\]/g, '\\$&'); };
|
||||
var rowTitle = typeof deps.rowTitle === 'function' ? deps.rowTitle : function(row) {
|
||||
if (!(row instanceof HTMLElement)) return '';
|
||||
var titleNode = row.querySelector('.tree-link-title');
|
||||
return String(titleNode && titleNode.textContent || row.getAttribute('title') || '').trim();
|
||||
};
|
||||
var rows = typeof deps.fileTreeRowsForUploadPreflight === 'function'
|
||||
? deps.fileTreeRowsForUploadPreflight()
|
||||
: fileTreeRowsForUploadPreflight(deps);
|
||||
return rows.map(function(row) {
|
||||
var domRow = document.querySelector('#sidebar-file-tree-root .tree-row[data-shell-mode="filetree"][data-row-id="' + cssEscape(row.rowId) + '"]');
|
||||
return Object.assign({}, row, { title: rowTitle(domRow) });
|
||||
});
|
||||
}
|
||||
|
||||
window.__mnoteFileTreeRuntime = {
|
||||
readProjection: readProjection,
|
||||
readSidebarDataset: readSidebarDataset,
|
||||
@@ -400,5 +488,9 @@ window.__mnoteFileTreeRuntime = {
|
||||
revealFileTreeRow: revealFileTreeRow,
|
||||
revealFileTreeAssetRow: revealFileTreeAssetRow,
|
||||
appendUploadedAssetRow: appendUploadedAssetRow,
|
||||
fileTreeUploadTargetFallback: fileTreeUploadTargetFallback
|
||||
fileTreeUploadTargetFallback: fileTreeUploadTargetFallback,
|
||||
fileTreeRowsForUploadPreflight: fileTreeRowsForUploadPreflight,
|
||||
fileTreeDocumentParentsForPreflight: fileTreeDocumentParentsForPreflight,
|
||||
fileTreeTargetChildrenForPreflight: fileTreeTargetChildrenForPreflight,
|
||||
fileTreeDropPreflightRows: fileTreeDropPreflightRows
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user