Add an opt-in context menu for files and directories

Apps set browser.contextMenu to a list of { label, action, when?, danger? }
items; when(entry, path) scopes an item to directories, files, or any
other match. Right-click, the Menu key, or a touch long press opens it.
The demo adds sample items, including a .json-only one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-23 18:50:21 -07:00
co-authored by Claude Opus 5.5
parent 13e2654415
commit d8b8012e40
12 changed files with 292 additions and 18 deletions
+13 -1
View File
@@ -1,6 +1,6 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { fileKindLabel, formatFileSize, formatRelativeDate, isRecent, sortEntries } from '../src/file-browser-utils.js';
import { fileKindLabel, formatFileSize, formatRelativeDate, isRecent, menuItemsFor, sortEntries } from '../src/file-browser-utils.js';
test('sortEntries keeps folders first and names natural', () => {
const sorted = sortEntries([
@@ -36,3 +36,15 @@ test('fileKindLabel identifies special and common files', () => {
assert.equal(fileKindLabel('app.js'), 'JavaScript');
assert.equal(fileKindLabel('Dockerfile'), 'File');
});
test('menuItemsFor keeps items whose when() matches the entry', () => {
const items = [
{ label: 'All' },
{ label: 'Folders', when: (entry) => entry.type === 'directory' },
{ label: 'JSON', when: (entry, path) => entry.name.endsWith('.json') && path.length === 2 },
];
const labels = (entry, path) => menuItemsFor(items, entry, path).map(({ label }) => label);
assert.deepEqual(labels({ name: 'src', type: 'directory' }, ['src']), ['All', 'Folders']);
assert.deepEqual(labels({ name: 'a.json', type: 'file' }, ['src', 'a.json']), ['All', 'JSON']);
assert.deepEqual(menuItemsFor(undefined, { name: 'a', type: 'file' }, ['a']), []);
});