Merge W0-commands (Refs #39 #49 #34)

# Conflicts:
#	craft/src/styles/editor.css
This commit is contained in:
shadowdao committed 2026-10-07 11:20:25 -07:00
commit ae871c9cdc
11 files changed
+1004

No files matched your search

@@ -0,0 +1,79 @@
import { describe, test, expect, vi, afterEach, beforeEach } from 'vitest';
import React from 'react';
import { renderEditorHarness, EditorHarness } from '../../test-utils/editorHarness';
import { registerCommands, __resetCommandsForTests } from '../../editor/commands';
import { TopBarCommandMenu } from './TopBarCommandMenu';
let h: EditorHarness;
const run = vi.fn();
beforeEach(() => {
__resetCommandsForTests();
run.mockReset();
registerCommands([
{ id: 'site.download', title: 'Download site', group: 'site', shortcut: 'mod+shift+e', run },
{ id: 'site.disabled', title: 'Disabled thing', group: 'site', when: () => false, run },
{ id: 'site.hidden', title: 'Hidden thing', group: 'site', hidden: true, run },
{ id: 'edit.duplicate', title: 'Duplicate', group: 'edit', run },
]);
h = renderEditorHarness();
h.mountChild(<TopBarCommandMenu />);
});
afterEach(() => {
h.unmount();
__resetCommandsForTests();
});
const button = () => h.container.querySelector('.topbar-command-btn') as HTMLButtonElement;
const items = () => Array.from(h.container.querySelectorAll('.topbar-command-item')) as HTMLButtonElement[];
describe('TopBarCommandMenu', () => {
test('lists only visible site commands, with shortcut labels and disabled state', () => {
expect(items()).toHaveLength(0);
h.act(() => button().click());
const titles = items().map((i) => i.querySelector('.topbar-command-title')!.textContent);
expect(titles).toEqual(['Download site', 'Disabled thing']);
expect(items()[0].querySelector('.topbar-command-shortcut')!.textContent).toMatch(/Shift\+E$|⇧E$/);
expect(items()[0].disabled).toBe(false);
expect(items()[1].disabled).toBe(true);
});
test('click runs the command and closes the menu', async () => {
h.act(() => button().click());
await h.act(async () => {
items()[0].click();
});
expect(run).toHaveBeenCalledTimes(1);
expect(items()).toHaveLength(0);
});
test('disabled item does not run', async () => {
h.act(() => button().click());
await h.act(async () => {
items()[1].click();
});
expect(run).not.toHaveBeenCalled();
});
test('Escape closes; outside mousedown closes', () => {
h.act(() => button().click());
expect(items().length).toBeGreaterThan(0);
h.act(() => {
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
});
expect(items()).toHaveLength(0);
h.act(() => button().click());
h.act(() => {
document.body.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
});
expect(items()).toHaveLength(0);
});
test('late registration shows up without reopening', () => {
h.act(() => button().click());
h.act(() => {
registerCommands([{ id: 'site.late', title: 'Late one', group: 'site', run }]);
});
expect(items().map((i) => i.textContent)).toContain('Late one');
});
});
@@ -0,0 +1,80 @@
import React, { useCallback, useEffect, useRef, useState, useSyncExternalStore } from 'react';
import { getCommands, subscribeCommands, useCommandContext, useRunCommand } from '../../editor/commands';
import { formatShortcut } from '../../utils/shortcuts';
/**
* Top-bar "⋯" menu listing every registered `site` command. Features add their
* entry with `registerCommand({group: 'site', ...})` instead of editing the
* TopBar. Closes on Escape / outside click (same pattern as TopBarOverflowMenu).
*/
export const TopBarCommandMenu: React.FC = () => {
const [open, setOpen] = useState(false);
const wrapRef = useRef<HTMLDivElement>(null);
const commands = useSyncExternalStore(subscribeCommands, getCommands, getCommands);
const ctx = useCommandContext();
const run = useRunCommand();
const close = useCallback(() => setOpen(false), []);
useEffect(() => {
if (!open) return;
const onDown = (e: MouseEvent) => {
if (wrapRef.current && !wrapRef.current.contains(e.target as Node)) close();
};
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') close();
};
document.addEventListener('mousedown', onDown);
document.addEventListener('keydown', onKey);
return () => {
document.removeEventListener('mousedown', onDown);
document.removeEventListener('keydown', onKey);
};
}, [open, close]);
const items = commands.filter((c) => c.group === 'site' && !c.hidden);
return (
<div className="topbar-command-menu" ref={wrapRef}>
<button
type="button"
className="topbar-command-btn"
aria-label="More actions"
aria-haspopup="menu"
aria-expanded={open}
title="More actions"
onClick={() => setOpen((o) => !o)}
>
<i className="fa fa-ellipsis-h" aria-hidden="true" />
</button>
{open && (
<div className="topbar-command-dropdown" role="menu">
{items.length === 0 && <div className="topbar-command-empty">No actions available</div>}
{items.map((c) => {
let enabled = true;
try {
enabled = c.when ? !!c.when(ctx) : true;
} catch {
enabled = false;
}
return (
<button
key={c.id}
type="button"
role="menuitem"
className="topbar-command-item"
disabled={!enabled}
onClick={() => {
close();
void run(c.id);
}}
>
<span className="topbar-command-title">{c.title}</span>
{c.shortcut && <span className="topbar-command-shortcut">{formatShortcut(c.shortcut)}</span>}
</button>
);
})}
</div>
)}
</div>
);
};