Files
site-builder/craft/src/editor/selection-commands.ts
T
shadowdao c68e459430 feat(site-builder): complete shortcut set on the command registry with a cheat sheet
Core editor shortcuts become registry commands run by the single
useCommandShortcuts dispatcher: undo/redo, delete, duplicate (after +
select), copy/paste, Alt+arrow move, 1/2/3 device, Cmd+S/Shift+P/P site
commands and Cmd+/ cheat sheet. Tab/Esc no longer fire from buttons or
dialogs. Context menu labels come from formatShortcut.

Refs #39
2026-10-07 11:47:55 -07:00

125 lines
4.3 KiB
TypeScript

import { registerCommands, type Command } from './commands';
import { getParentId, getSiblingIds, getChildIds } from '../utils/craft-helpers';
/**
* Selection navigation (parent / siblings / first child), shared by the
* breadcrumb, the keyboard layer and (later) the command palette. Pure
* functions over `query` / `actions` so they are testable on a real editor.
*/
function nodeData(query: any, id: string): any {
try {
return query.node(id).get()?.data ?? null;
} catch {
return null;
}
}
/**
* True for a Section's own inner container (linked as `section-inner`). Only
* that key is skipped: Columns' `col-N` containers are real, user-meaningful
* steps in the ancestry and stay selectable.
*/
export function isSectionInnerNode(query: any, id: string): boolean {
const parentId = getParentId(query, id);
if (!parentId) return false;
return nodeData(query, parentId)?.linkedNodes?.['section-inner'] === id;
}
/** The node Esc / "select parent" should land on: the parent, hopping over a
* Section's inner container. Null when there is none (top level / ROOT). */
export function getSelectableParentId(query: any, id: string): string | null {
let parentId = getParentId(query, id);
if (parentId && parentId !== 'ROOT' && isSectionInnerNode(query, parentId)) {
parentId = getParentId(query, parentId);
}
return parentId && parentId !== 'ROOT' ? parentId : null;
}
/** Select the parent. Never selects ROOT. Returns the newly selected id. */
export function selectParent(query: any, actions: any, id: string): string | null {
if (!id || id === 'ROOT') return null;
const parentId = getSelectableParentId(query, id);
if (!parentId) return null;
actions.selectNode(parentId);
return parentId;
}
/** Esc behaviour: climb to the parent; at the top level clear the selection. */
export function escapeSelection(query: any, actions: any, id: string): void {
if (!selectParent(query, actions, id)) actions.clearEvents();
}
function selectSibling(query: any, actions: any, id: string, step: 1 | -1): string | null {
if (!id || id === 'ROOT') return null;
const sibs = getSiblingIds(query, id);
if (sibs.length < 2) return null;
const idx = sibs.indexOf(id);
if (idx === -1) return null;
const next = sibs[(idx + step + sibs.length) % sibs.length];
actions.selectNode(next);
return next;
}
export function selectNextSibling(query: any, actions: any, id: string): string | null {
return selectSibling(query, actions, id, 1);
}
export function selectPrevSibling(query: any, actions: any, id: string): string | null {
return selectSibling(query, actions, id, -1);
}
export function selectFirstChild(query: any, actions: any, id: string): string | null {
if (!id) return null;
const first = getChildIds(query, id)[0];
if (!first) return null;
actions.selectNode(first);
return first;
}
const oneNonRootSelected = (ctx: { selectedIds: string[] }) =>
ctx.selectedIds.length === 1 && ctx.selectedIds[0] !== 'ROOT';
// Sibling cycling only claims Tab when it will move the selection, so focus
// navigation still works with a lone node.
const hasSiblings = (ctx: { query: any; selectedIds: string[] }) =>
oneNonRootSelected(ctx) && getSiblingIds(ctx.query, ctx.selectedIds[0]).length >= 2;
export const selectionCommands: Command[] = [
{
id: 'select.parent',
title: 'Select parent',
group: 'select',
shortcut: 'escape',
when: oneNonRootSelected,
run: ({ query, actions, selectedIds }) => escapeSelection(query, actions, selectedIds[0]),
},
{
id: 'select.next-sibling',
title: 'Select next sibling',
group: 'select',
shortcut: 'tab',
when: hasSiblings,
run: ({ query, actions, selectedIds }) => void selectNextSibling(query, actions, selectedIds[0]),
},
{
id: 'select.prev-sibling',
title: 'Select previous sibling',
group: 'select',
shortcut: 'shift+tab',
when: hasSiblings,
run: ({ query, actions, selectedIds }) => void selectPrevSibling(query, actions, selectedIds[0]),
},
{
id: 'select.first-child',
title: 'Select first child',
group: 'select',
when: oneNonRootSelected,
run: ({ query, actions, selectedIds }) => void selectFirstChild(query, actions, selectedIds[0]),
},
];
export function registerSelectionCommands(): () => void {
return registerCommands(selectionCommands);
}