feat: host file and note actions
This commit is contained in:
@@ -310,6 +310,8 @@
|
||||
var createMode = '';
|
||||
var renameTarget = null;
|
||||
var disposed = false;
|
||||
var fileActions = [];
|
||||
var contextMenuEntries = [];
|
||||
var historyStack = Array.isArray(savedHistory.stack) && savedHistory.stack.length ? savedHistory.stack.map(cleanPath) : [currentPath];
|
||||
var historyIndex = Math.max(0, Math.min(Number(savedHistory.index) || 0, historyStack.length - 1));
|
||||
if (historyStack[historyIndex] !== currentPath) {
|
||||
@@ -589,6 +591,34 @@
|
||||
});
|
||||
}
|
||||
|
||||
function contributionContextMatches(item, entry) {
|
||||
var context = String(item && item.context || '').toLowerCase();
|
||||
if (!context || context === '*' || context === 'files' || context === 'vault-entry') return true;
|
||||
if (context === 'file') return !entry || entry.type !== 'folder';
|
||||
if (context === 'folder' || context === 'directory') return !entry || entry.type === 'folder';
|
||||
return false;
|
||||
}
|
||||
|
||||
function loadContributionActions() {
|
||||
var contributions = api && api.contributions;
|
||||
if (!contributions || typeof contributions.list !== 'function') return;
|
||||
Promise.all([
|
||||
contributions.list('fileActions'),
|
||||
contributions.list('contextMenuEntries')
|
||||
]).then(function (result) {
|
||||
if (disposed) return;
|
||||
fileActions = (result[0] || []).filter(function (item) {
|
||||
return item && item.pluginId && item.handler && item.label;
|
||||
});
|
||||
contextMenuEntries = (result[1] || []).filter(function (item) {
|
||||
return item && item.pluginId && item.handler && item.label && contributionContextMatches(item, null);
|
||||
});
|
||||
renderList();
|
||||
}).catch(function (err) {
|
||||
console.error('[files] contribution actions:', err);
|
||||
});
|
||||
}
|
||||
|
||||
function navigateTo(path) {
|
||||
var newPath = cleanPath(path);
|
||||
if (!navigatingHistory) {
|
||||
@@ -809,6 +839,37 @@
|
||||
});
|
||||
}
|
||||
|
||||
function executeContributionAction(action, entry) {
|
||||
if (!action || !entry || !api.commands || typeof api.commands.executeFor !== 'function') return;
|
||||
api.commands.executeFor(action.pluginId, action.handler, {
|
||||
source: 'files',
|
||||
actionId: action.id,
|
||||
path: entry.relativePath,
|
||||
entry: entry,
|
||||
currentPath: scopedPath(currentPath),
|
||||
workspaceRootPath: workspaceRoot
|
||||
}).catch(function (err) {
|
||||
console.error('[files] contribution action failed:', err);
|
||||
});
|
||||
}
|
||||
|
||||
function appendContributionMenuItems(entry) {
|
||||
var menuItems = [];
|
||||
fileActions.forEach(function (action) {
|
||||
menuItems.push(action);
|
||||
});
|
||||
contextMenuEntries.forEach(function (action) {
|
||||
if (contributionContextMatches(action, entry)) menuItems.push(action);
|
||||
});
|
||||
if (menuItems.length === 0) return;
|
||||
ctxMenu.appendChild(ctxSep());
|
||||
menuItems.forEach(function (action) {
|
||||
ctxMenu.appendChild(ctxItem(action.label, '', function () {
|
||||
executeContributionAction(action, entry);
|
||||
}, 'contribution-' + action.id, action.icon || 'open'));
|
||||
});
|
||||
}
|
||||
|
||||
function showCtxMenu(x, y, entry) {
|
||||
ctxTarget = entry;
|
||||
ctxMenu.innerHTML = '';
|
||||
@@ -823,6 +884,7 @@
|
||||
ctxMenu.appendChild(ctxItem(isFolder ? 'Open Folder' : 'Open', '', function () { openEntry(entry); }, 'open', 'open'));
|
||||
ctxMenu.appendChild(ctxItem('Open External', '', function () { openExternalEntry(entry, 'external'); }, 'open-external', 'external'));
|
||||
ctxMenu.appendChild(ctxItem('Show in Explorer', '', function () { openExternalEntry(entry, 'explorer'); }, 'show-in-explorer', 'explorer'));
|
||||
appendContributionMenuItems(entry);
|
||||
ctxMenu.appendChild(ctxSep());
|
||||
ctxMenu.appendChild(ctxItem('Rename', '', function () { beginRename(entry); }, 'rename', 'rename'));
|
||||
if (entry.type !== 'folder') {
|
||||
@@ -1248,6 +1310,7 @@
|
||||
});
|
||||
|
||||
updateHistoryButtons();
|
||||
loadContributionActions();
|
||||
loadEntries();
|
||||
|
||||
containerEl.__filesCleanup = function () {
|
||||
|
||||
@@ -171,6 +171,7 @@
|
||||
var statusText = '';
|
||||
var statusClass = '';
|
||||
var disposed = false;
|
||||
var noteActions = [];
|
||||
|
||||
function notesParent() {
|
||||
return workspaceRoot || '';
|
||||
@@ -313,6 +314,43 @@
|
||||
});
|
||||
}
|
||||
|
||||
function loadContributionActions() {
|
||||
var contributions = api && api.contributions;
|
||||
if (!contributions || typeof contributions.list !== 'function') return;
|
||||
contributions.list('noteActions').then(function (result) {
|
||||
if (disposed) return;
|
||||
noteActions = (result || []).filter(function (item) {
|
||||
return item && item.pluginId && item.handler && item.label;
|
||||
});
|
||||
renderList();
|
||||
}).catch(function (err) {
|
||||
console.error('[notes] contribution actions:', err);
|
||||
});
|
||||
}
|
||||
|
||||
function actionInitial(action) {
|
||||
var label = String(action && action.label || '').trim();
|
||||
return label ? label.charAt(0).toUpperCase() : '+';
|
||||
}
|
||||
|
||||
function executeContributionAction(action, note) {
|
||||
if (!action || !note || !api.commands || typeof api.commands.executeFor !== 'function') return;
|
||||
setStatus(action.label + '...', 'loading');
|
||||
api.commands.executeFor(action.pluginId, action.handler, {
|
||||
source: 'notes',
|
||||
actionId: action.id,
|
||||
path: note.path,
|
||||
note: note,
|
||||
notesScopePath: notesParent(),
|
||||
workspaceRootPath: workspaceRoot
|
||||
}).then(function () {
|
||||
if (!disposed) setStatus(action.label + ' complete', 'success');
|
||||
}).catch(function (err) {
|
||||
console.error('[notes] contribution action failed:', err);
|
||||
if (!disposed) setStatus('Error: ' + (err && err.message ? err.message : err), 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function renderList() {
|
||||
listContainer.innerHTML = '';
|
||||
if (!notes || notes.length === 0) {
|
||||
@@ -320,6 +358,38 @@
|
||||
return;
|
||||
}
|
||||
notes.forEach(function (note) {
|
||||
var actionButtons = [
|
||||
el('button', {
|
||||
className: 'notes-item-btn',
|
||||
title: 'Open',
|
||||
innerHTML: iconSvg('open'),
|
||||
onClick: function (e) { e.stopPropagation(); openNote(note); }
|
||||
}),
|
||||
el('button', {
|
||||
className: 'notes-item-btn',
|
||||
title: 'Rename',
|
||||
'data-note-action': 'rename',
|
||||
innerHTML: iconSvg('rename'),
|
||||
onClick: function (e) { e.stopPropagation(); beginRename(note); }
|
||||
}),
|
||||
el('button', {
|
||||
className: 'notes-item-btn',
|
||||
title: 'Move to Trash',
|
||||
'data-note-action': 'trash',
|
||||
innerHTML: iconSvg('trash'),
|
||||
onClick: function (e) { e.stopPropagation(); confirmTrashNote(note); }
|
||||
})
|
||||
];
|
||||
noteActions.forEach(function (action) {
|
||||
actionButtons.push(el('button', {
|
||||
className: 'notes-item-btn',
|
||||
title: action.label,
|
||||
'aria-label': action.label,
|
||||
'data-note-contribution-action': action.id,
|
||||
textContent: actionInitial(action),
|
||||
onClick: function (e) { e.stopPropagation(); executeContributionAction(action, note); }
|
||||
}));
|
||||
});
|
||||
var row = el('div', {
|
||||
className: 'notes-item' + (note.path === selectedPath ? ' selected' : ''),
|
||||
'data-note-path': note.path,
|
||||
@@ -329,28 +399,7 @@
|
||||
}, [
|
||||
el('span', { className: 'notes-item-icon', innerHTML: iconSvg('note') }),
|
||||
el('span', { className: 'notes-item-name', textContent: note.title || fileName(note.path), title: note.title || note.path }),
|
||||
el('span', { className: 'notes-item-actions' }, [
|
||||
el('button', {
|
||||
className: 'notes-item-btn',
|
||||
title: 'Open',
|
||||
innerHTML: iconSvg('open'),
|
||||
onClick: function (e) { e.stopPropagation(); openNote(note); }
|
||||
}),
|
||||
el('button', {
|
||||
className: 'notes-item-btn',
|
||||
title: 'Rename',
|
||||
'data-note-action': 'rename',
|
||||
innerHTML: iconSvg('rename'),
|
||||
onClick: function (e) { e.stopPropagation(); beginRename(note); }
|
||||
}),
|
||||
el('button', {
|
||||
className: 'notes-item-btn',
|
||||
title: 'Move to Trash',
|
||||
'data-note-action': 'trash',
|
||||
innerHTML: iconSvg('trash'),
|
||||
onClick: function (e) { e.stopPropagation(); confirmTrashNote(note); }
|
||||
})
|
||||
])
|
||||
el('span', { className: 'notes-item-actions' }, actionButtons)
|
||||
]);
|
||||
listContainer.appendChild(row);
|
||||
});
|
||||
@@ -547,6 +596,7 @@
|
||||
|
||||
// ─── Init ───────────────────────────────────────────────
|
||||
|
||||
loadContributionActions();
|
||||
loadNotes();
|
||||
|
||||
containerEl.__notesCleanup = function () {
|
||||
|
||||
Reference in New Issue
Block a user