fix(studio): the sheet's keys leave a key to the control that took it, and the layers tree works by keyboard

The sheet listened on the window in the capture phase, before any control saw
a key. Delete on a focused gradient stop removed the whole shape, an arrow on
the angle dial or the radial centre also moved the shape, and Delete, the
arrows and Ctrl+V pressed on a button of the assistant's window acted on the
page although the window stops its keys. The sheet now listens on the
document as the key bubbles, after every control had it and still before the
page's Escape on the window, and leaves a key whose default was prevented. A
gradient stop takes Delete and Backspace even when the last two stops cannot
go.

The layers panel was a tree a keyboard could not walk: only its toggles could
be focused. The tree is now one stop in the Tab order, the row last focused or
else the selected one. Up and Down move along the rows, Home and End to the
ends, Right opens a group or steps into it and Left closes it or steps from a
member up to its group (mirrored right to left), Enter or Space selects a row
as a click does with Shift adding, and F2 renames it. Enter and Escape in the
name field hand the focus back to its row, and the row in focus is ringed. A
hidden or locked layer that cannot be clicked on the page is reached this way.
This commit is contained in:
Maurice
2026-10-24 04:06:37 +00:00
parent 84e18e4954
commit 117af4a8a7
8 changed files with 320 additions and 15 deletions
@@ -1,9 +1,11 @@
import type { BookDocument, BookElement } from '@trek/shared';
import { bookPageSetupSchema, bookShapeElementSchema, bookTextElementSchema } from '@trek/shared';
import { beforeEach, describe, expect, it } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render } from '../../../tests/helpers/render';
import { useStudioStore } from '../../store/studioStore';
import { AngleDial } from './paint/AngleDial';
import { GradientStopBar } from './paint/GradientStopBar';
import { StudioCanvas } from './StudioCanvas';
/**
@@ -267,6 +269,55 @@ describe('the keyboard', () => {
expect(again.defaultPrevented).toBe(false);
});
it('leaves a key to the control that takes it: a gradient stop, the angle dial', () => {
load([shape('a', 10, 10)]);
store().select(['a']);
draw();
const fade = (n: number) => ({
type: 'linear' as const,
angle: 90,
stops: Array.from({ length: n }, (_, i) => ({ at: i / (n - 1), color: '#112233' })),
});
const onChange = vi.fn();
const labels = { bar: 'bar', stop: (pos: number) => `stop ${pos}` };
const three = render(
<GradientStopBar paint={fade(3)} selected={1} onSelect={() => {}} onChange={onChange} labels={labels} />
);
fireEvent.keyDown(must(three.container.querySelectorAll('.st-stop')[1]), { key: 'Delete' });
expect(onChange.mock.calls[0]?.[0].stops).toHaveLength(2);
three.unmount();
// The last two stops cannot go, and the key still does not reach the shape.
const two = render(
<GradientStopBar paint={fade(2)} selected={0} onSelect={() => {}} onChange={onChange} labels={labels} />
);
fireEvent.keyDown(must(two.container.querySelector('.st-stop')), { key: 'Backspace' });
two.unmount();
expect(elements().map((e) => e.id)).toEqual(['a']);
const dial = render(<AngleDial value={90} onChange={() => {}} label="Angle" />);
fireEvent.keyDown(must(dial.container.querySelector('.st-dial')), { key: 'ArrowRight', shiftKey: true });
expect(el('a').frame).toMatchObject({ x: 10, y: 10 });
});
it("leaves the keys pressed in the assistant's window to it", () => {
load([shape('a', 10, 10)]);
store().select(['a']);
store().copy(0, ['a']);
draw();
// As the Studio's assistant window does it: every key pressed in it stops there (StudioAssistantPanel.tsx).
const chat = render(
<aside className="st-assistant" onKeyDown={(e) => e.stopPropagation()}>
<button type="button">Apply</button>
</aside>
);
const button = must(chat.container.querySelector('button'));
fireEvent.keyDown(button, { key: 'Delete' });
fireEvent.keyDown(button, { key: 'ArrowDown' });
fireEvent.keyDown(button, { key: 'v', ctrlKey: true });
expect(elements().map((e) => e.id)).toEqual(['a']);
expect(el('a').frame).toMatchObject({ x: 10, y: 10 });
});
it('leaves the keys to a field being typed in', () => {
load([shape('a', 10, 10)]);
store().select(['a']);