136 lines
6.5 KiB
JavaScript
136 lines
6.5 KiB
JavaScript
// MNote 主壳文件树运行时外置模块。
|
|
// 当前先承接 SIDEBAR_TREE_JS 中低风险的行数据解析 helper;
|
|
// inline script 保留 fallback,避免模块加载顺序影响首屏行为。
|
|
|
|
function fileTreeRowKind(row) {
|
|
if (!(row instanceof HTMLElement)) return '';
|
|
return String(row.getAttribute('data-row-kind') || '').trim();
|
|
}
|
|
|
|
function fileTreeRowDocumentId(row) {
|
|
if (!(row instanceof HTMLElement)) return '';
|
|
return String(row.getAttribute('data-document-id') || row.getAttribute('data-doc-id') || '').trim();
|
|
}
|
|
|
|
function fileTreeRowAssetId(row, deps) {
|
|
if (!(row instanceof HTMLElement)) return '';
|
|
var assetId = String(row.getAttribute('data-asset-id') || '').trim();
|
|
if (assetId) return assetId;
|
|
var currentSourceKind = deps && typeof deps.currentSourceKind === 'function' ? deps.currentSourceKind : function() { return ''; };
|
|
if (currentSourceKind() === 'local_folder' && fileTreeRowKind(row) === 'asset') {
|
|
return String(row.getAttribute('data-row-id') || '').trim();
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function decodeLocalEncodedPath(value) {
|
|
var path = String(value || '').trim().replace(/~2F/g, '/');
|
|
if (!path) return '';
|
|
try {
|
|
return decodeURIComponent(path);
|
|
} catch (_) {
|
|
return path;
|
|
}
|
|
}
|
|
|
|
function fileTreeRowLocalRelativePath(row, deps) {
|
|
if (!(row instanceof HTMLElement)) return '';
|
|
var direct = String(row.getAttribute('data-local-relative-path') || '').trim();
|
|
if (direct) return direct;
|
|
var localFilePathFromAssetId = deps && typeof deps.localFilePathFromAssetId === 'function'
|
|
? deps.localFilePathFromAssetId
|
|
: function() { return ''; };
|
|
var assetPath = localFilePathFromAssetId(fileTreeRowAssetId(row, deps));
|
|
if (assetPath) return assetPath;
|
|
var rowId = String(row.getAttribute('data-row-id') || '').trim();
|
|
var nodeId = String(row.getAttribute('data-node-id') || '').trim();
|
|
var documentId = fileTreeRowDocumentId(row);
|
|
var kind = fileTreeRowKind(row);
|
|
if (rowId.indexOf('local:asset:') === 0) return rowId.slice('local:asset:'.length);
|
|
if (rowId.indexOf('local:markdown:') === 0) return rowId.slice('local:markdown:'.length);
|
|
if (rowId.indexOf('local:folder:') === 0) return rowId.slice('local:folder:'.length);
|
|
if (rowId.indexOf('local:node:') === 0) return rowId.slice('local:node:'.length);
|
|
if (nodeId.indexOf('local:node:') === 0) return nodeId.slice('local:node:'.length);
|
|
if ((kind === 'document' || kind === 'doc' || kind === 'markdown') && documentId.indexOf('local-md:') === 0) {
|
|
return decodeLocalEncodedPath(documentId.slice('local-md:'.length));
|
|
}
|
|
if ((kind === 'folder' || kind === 'directory' || kind === 'index') && documentId.indexOf('local-dir:') === 0) {
|
|
return decodeLocalEncodedPath(documentId.slice('local-dir:'.length));
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function fileTreeRowLocalUploadTargetRelativePath(row, deps) {
|
|
if (!(row instanceof HTMLElement)) return '';
|
|
var kind = fileTreeRowKind(row);
|
|
var relativePath = fileTreeRowLocalRelativePath(row, deps);
|
|
if (!relativePath) return '';
|
|
if (kind === 'folder' || kind === 'directory') return relativePath;
|
|
var lastSlash = relativePath.lastIndexOf('/');
|
|
return lastSlash >= 0 ? relativePath.slice(0, lastSlash) : '';
|
|
}
|
|
|
|
function isFileTreeDownloadableAssetRow(row, deps) {
|
|
if (!(row instanceof HTMLElement)) return false;
|
|
var kind = fileTreeRowKind(row);
|
|
if (kind === 'document' || kind === 'doc' || kind === 'index' || kind === 'markdown' || kind === 'folder' || kind === 'directory') return false;
|
|
return Boolean(fileTreeRowAssetId(row, deps));
|
|
}
|
|
|
|
function isFileTreeDownloadableRow(row, deps) {
|
|
if (!(row instanceof HTMLElement)) return false;
|
|
var currentSourceKind = deps && typeof deps.currentSourceKind === 'function' ? deps.currentSourceKind : function() { return ''; };
|
|
if (currentSourceKind() !== 'local_folder') return isFileTreeDownloadableAssetRow(row, deps);
|
|
return Boolean(fileTreeRowLocalRelativePath(row, deps) || isFileTreeDownloadableAssetRow(row, deps));
|
|
}
|
|
|
|
function fileTreeAssetDownloadDetail(row, deps) {
|
|
if (!isFileTreeDownloadableRow(row, deps)) return null;
|
|
var rowTitle = deps && typeof deps.rowTitle === 'function' ? deps.rowTitle : function() { return ''; };
|
|
var resolveWorkspaceId = deps && typeof deps.resolveWorkspaceId === 'function' ? deps.resolveWorkspaceId : function() { return ''; };
|
|
var title = rowTitle(row);
|
|
var relativePath = fileTreeRowLocalRelativePath(row, deps);
|
|
var assetId = fileTreeRowAssetId(row, deps) || (relativePath ? 'local-file:' + relativePath : '');
|
|
return {
|
|
contextKind: 'filetree',
|
|
documentId: fileTreeRowDocumentId(row),
|
|
rowId: row.getAttribute('data-row-id') || '',
|
|
rowKind: fileTreeRowKind(row),
|
|
assetId: assetId,
|
|
localRelativePath: relativePath,
|
|
title: title,
|
|
fileName: title,
|
|
workspaceId: resolveWorkspaceId(row)
|
|
};
|
|
}
|
|
|
|
function fileTreeRowsByRowIds(rowIds, deps) {
|
|
var cssEscape = deps && typeof deps.cssEscape === 'function' ? deps.cssEscape : function(value) { return String(value).replace(/["\\]/g, '\\$&'); };
|
|
return (rowIds || []).map(function(rowId) {
|
|
return document.querySelector('#sidebar-file-tree-root .tree-row[data-shell-mode="filetree"][data-row-id="' + cssEscape(rowId) + '"]');
|
|
}).filter(function(row) { return row instanceof HTMLElement; });
|
|
}
|
|
|
|
function fileTreeChildCount(documentId, deps) {
|
|
if (!documentId) return 0;
|
|
var cssEscape = deps && typeof deps.cssEscape === 'function' ? deps.cssEscape : function(value) { return String(value).replace(/["\\]/g, '\\$&'); };
|
|
var row = document.querySelector('#sidebar-file-tree-root .tree-row[data-shell-mode="filetree"][data-row-id="' + cssEscape('doc:' + documentId) + '"]');
|
|
var node = row ? row.closest('.tree-node') : null;
|
|
var children = node ? node.querySelectorAll(':scope > .tree-children > .tree-node > .tree-row[data-shell-mode="filetree"][data-row-kind="document"], :scope > .tree-children > .tree-node > .tree-row[data-shell-mode="filetree"][data-row-kind="doc"]') : [];
|
|
return children.length;
|
|
}
|
|
|
|
window.__mnoteFileTreeRuntime = {
|
|
fileTreeRowKind: fileTreeRowKind,
|
|
fileTreeRowDocumentId: fileTreeRowDocumentId,
|
|
fileTreeRowAssetId: fileTreeRowAssetId,
|
|
decodeLocalEncodedPath: decodeLocalEncodedPath,
|
|
fileTreeRowLocalRelativePath: fileTreeRowLocalRelativePath,
|
|
fileTreeRowLocalUploadTargetRelativePath: fileTreeRowLocalUploadTargetRelativePath,
|
|
isFileTreeDownloadableAssetRow: isFileTreeDownloadableAssetRow,
|
|
isFileTreeDownloadableRow: isFileTreeDownloadableRow,
|
|
fileTreeAssetDownloadDetail: fileTreeAssetDownloadDetail,
|
|
fileTreeRowsByRowIds: fileTreeRowsByRowIds,
|
|
fileTreeChildCount: fileTreeChildCount
|
|
};
|