Skip to content

Visualize Your Ideas With Code

feat: keyboard navigation for scene graph (#1101)

Jacobe5bd5f7157d7
diff --git a/packages/2d/src/editor/PreviewOverlayConfig.tsx b/packages/2d/src/editor/PreviewOverlayConfig.tsx
index 9a74f71b..4bd0c605 100644
--- a/packages/2d/src/editor/PreviewOverlayConfig.tsx
+++ b/packages/2d/src/editor/PreviewOverlayConfig.tsx
@@ -11,7 +11,7 @@ import {usePluginState} from './Provider';
function Component({children}: {children?: ComponentChildren}) {
const state = useViewportContext();
- const {scene, selectedKey} = usePluginState();
+ const {scene, selectNode} = usePluginState();
const matrix = useViewportMatrix();
return (
@@ -26,10 +26,9 @@ function Component({children}: {children?: ComponentChildren}) {
event.y - state.rect.y,
).transformAsPoint(matrix.inverse());
- selectedKey.value = scene.value.inspectPosition(
- position.x,
- position.y,
- ) as string;
+ selectNode(
+ scene.value.inspectPosition(position.x, position.y) as string,
+ );
}}
>
{children}
@@ -38,8 +37,8 @@ function Component({children}: {children?: ComponentChildren}) {
}
function drawHook() {
- const {selectedKey, hoveredKey, afterRender, scene} = usePluginState();
- selectedKey.value;
+ const {selectedNode, hoveredKey, afterRender, scene} = usePluginState();
+ selectedNode.value;
hoveredKey.value;
afterRender.value;
@@ -47,13 +46,13 @@ function drawHook() {
const currentScene = scene.peek();
if (!currentScene) return;
- let node = currentScene.getNode(selectedKey.value);
+ let node = selectedNode.value;
if (node) {
currentScene.drawOverlay(node.key, matrix, ctx);
}
node = currentScene.getNode(hoveredKey.value);
- if (node && hoveredKey.value !== selectedKey.value) {
+ if (node && hoveredKey.value !== selectedNode.value?.key) {
ctx.globalAlpha = 0.5;
currentScene.drawOverlay(hoveredKey.value, matrix, ctx);
}
diff --git a/packages/2d/src/editor/Provider.tsx b/packages/2d/src/editor/Provider.tsx
index a612991f..b69f7ac5 100644
--- a/packages/2d/src/editor/Provider.tsx
+++ b/packages/2d/src/editor/Provider.tsx
@@ -1,4 +1,4 @@
-import {Scene2D} from '@motion-canvas/2d';
+import {Node, Scene2D} from '@motion-canvas/2d';
import {SceneRenderEvent} from '@motion-canvas/core';
import {useApplication, useCurrentScene} from '@motion-canvas/ui';
import {
@@ -10,13 +10,16 @@ import {
} from '@preact/signals';
import {ComponentChildren, createContext} from 'preact';
import {useContext, useMemo} from 'preact/hooks';
+import {SignalSet} from './utils';
export interface PluginState {
- selectedKey: Signal<string | null>;
+ selectedNode: ReadonlySignal<Node | null>;
hoveredKey: Signal<string | null>;
- openNodes: Map<string, boolean>;
+ selectNode: (nodeKey: string | null) => void;
+ openNodes: SignalSet<string>;
+ openDetached: Signal<boolean>;
+ visibleNodes: Set<string>;
scene: ReadonlySignal<Scene2D | null>;
- selectedChain: ReadonlySignal<Set<string>>;
afterRender: ReadonlySignal<number>;
}
@@ -33,32 +36,38 @@ export function Provider({children}: {children?: ComponentChildren}) {
const currentScene = useCurrentScene();
const state = useMemo(() => {
- const scene = signal(currentScene as Scene2D);
- const selectedKey = signal<string | null>(null);
const afterRender = signal(0);
- const hoveredKey = signal<string | null>(null);
- const openNodes = new Map<string, boolean>();
- const selectedChain = computed(() => {
- const chain = new Set<string>();
- const key = selectedKey.value;
- const selectedNode = scene.value?.getNode(key);
- if (selectedNode) {
- let node = selectedNode.parent() ?? null;
- while (node) {
- chain.add(node.key);
- node = node.parent();
- }
+ const openDetached = signal(false);
+ const visibleNodes = new Set<string>();
+ const scene = signal(currentScene as Scene2D);
+ const selectedNode = computed(() => {
+ afterRender.value;
+ const {key, payload} = inspection.value;
+ if (key === NodeInspectorKey) {
+ return scene.value?.getNode(payload as string) ?? null;
}
-
- return chain;
+ return null;
});
+ const hoveredKey = signal<string | null>(null);
+ const openNodes = new SignalSet<string>();
+ const selectNode = (nodeKey: string | null) => {
+ const {key, payload} = inspection.peek();
+
+ if (key === NodeInspectorKey && !nodeKey) {
+ inspection.value = {key: '', payload: null};
+ } else if (payload !== nodeKey) {
+ inspection.value = {key: NodeInspectorKey, payload: nodeKey};
+ }
+ };
return {
- selectedKey,
+ selectedNode,
+ selectNode,
hoveredKey,
afterRender,
openNodes,
- selectedChain,
+ openDetached,
+ visibleNodes,
scene,
} satisfies PluginState;
}, []);
@@ -73,21 +82,24 @@ export function Provider({children}: {children?: ComponentChildren}) {
}),
);
+ // Expand all nodes necessary to reveal the selected one:
useSignalEffect(() => {
- const {key, payload} = inspection.value;
- if (key === NodeInspectorKey) {
- state.selectedKey.value = payload as string;
- }
- });
+ const selectedNode = state.selectedNode.value;
- useSignalEffect(() => {
- const nodeKey = state.selectedKey.value;
- const {key, payload} = inspection.peek();
+ if (selectedNode && !state.visibleNodes.has(selectedNode.key ?? '')) {
+ let node = selectedNode.parent() ?? null;
+ if (!node && selectedNode !== selectedNode.view()) {
+ state.openDetached.value = true;
+ }
- if (key === NodeInspectorKey && !nodeKey) {
- inspection.value = {key: '', payload: null};
- } else if (payload !== nodeKey) {
- inspection.value = {key: NodeInspectorKey, payload: nodeKey};
+ while (node) {
+ state.openNodes.add(node.key);
+ const parent = node.parent();
+ if (!parent && node !== node.view()) {
+ state.openDetached.value = true;
+ }
+ node = parent;
+ }
}
});
diff --git a/packages/2d/src/editor/SceneGraphTabConfig.tsx b/packages/2d/src/editor/SceneGraphTabConfig.tsx
index ec5e5b77..d2912399 100644
--- a/packages/2d/src/editor/SceneGraphTabConfig.tsx
+++ b/packages/2d/src/editor/SceneGraphTabConfig.tsx
@@ -8,32 +8,33 @@ import {
useApplication,
usePanels,
useReducedMotion,
+ useSurfaceShortcuts,
} from '@motion-canvas/ui';
-
import {useSignalEffect} from '@preact/signals';
import {useEffect, useRef} from 'preact/hooks';
import {usePluginState} from './Provider';
-import {DetachedRoot, ViewRoot} from './tree';
+import {SCENE_GRAPH_SHORTCUTS} from './shortcuts';
+import {DetachedRoot, useKeyboardNavigation, ViewRoot} from './tree';
function TabComponent({tab}: PluginTabProps) {
const {sidebar} = usePanels();
const inspectorTab = useRef<HTMLButtonElement>(null);
const reducedMotion = useReducedMotion();
- const {selectedKey} = usePluginState();
+ const {selectedNode, selectNode} = usePluginState();
const {logger} = useApplication();
useEffect(
() =>
logger.onInspected.subscribe(key => {
sidebar.set(tab);
- selectedKey.value = key;
+ selectNode(key);
}),
[tab],
);
useSignalEffect(() => {
if (
- selectedKey.value &&
+ selectedNode.value &&
sidebar.current.peek() !== tab &&
!reducedMotion &&
inspectorTab.current &&
@@ -59,14 +60,18 @@ function TabComponent({tab}: PluginTabProps) {
}
function PaneComponent() {
- const {selectedKey} = usePluginState();
+ const ref = useSurfaceShortcuts<HTMLDivElement>(SCENE_GRAPH_SHORTCUTS);
+ const {selectNode} = usePluginState();
+
+ useKeyboardNavigation();
return (
<Pane
+ forwardRef={ref}
title="Scene Graph"
id="scene-graph-pane"
onClick={() => {
- selectedKey.value = null;
+ selectNode(null);
}}
>
<ViewRoot />
diff --git a/packages/2d/src/editor/index.ts b/packages/2d/src/editor/index.ts
index 37b6f5b2..65fa7d34 100644
--- a/packages/2d/src/editor/index.ts
+++ b/packages/2d/src/editor/index.ts
@@ -5,6 +5,7 @@ import {NodeInspectorConfig} from './NodeInspectorConfig';
import {PreviewOverlayConfig} from './PreviewOverlayConfig';
import {Provider} from './Provider';
import {SceneGraphTabConfig} from './SceneGraphTabConfig';
+import {SCENE_GRAPH_SHORTCUTS} from './shortcuts';
export default makeEditorPlugin(() => {
return {
@@ -13,5 +14,6 @@ export default makeEditorPlugin(() => {
previewOverlay: PreviewOverlayConfig,
tabs: [SceneGraphTabConfig],
inspectors: [NodeInspectorConfig],
+ shortcuts: [SCENE_GRAPH_SHORTCUTS],
};
});
diff --git a/packages/2d/src/editor/shortcuts.ts b/packages/2d/src/editor/shortcuts.ts
new file mode 100644
index 00000000..f6eeb7fe
--- /dev/null
+++ b/packages/2d/src/editor/shortcuts.ts
@@ -0,0 +1,27 @@
+import {makeShortcuts} from '@motion-canvas/ui';
+export const SCENE_GRAPH_SHORTCUTS = makeShortcuts('scene-graph', {
+ moveUp: {
+ display: '↑',
+ description: 'Go up',
+ key: 'ArrowUp',
+ modifiers: {},
+ },
+ modeDown: {
+ display: '↓',
+ description: 'Go down',
+ key: 'ArrowDown',
+ modifiers: {},
+ },
+ expandOrMoveDown: {
+ display: '→',
+ description: 'Expand / Enter',
+ key: 'ArrowRight',
+ modifiers: {},
+ },
+ collapseOrMoveUp: {
+ display: '←',
+ description: 'Collapse / Leave',
+ key: 'ArrowLeft',
+ modifiers: {},
+ },
+});
diff --git a/packages/2d/src/editor/tree/DetachedRoot.tsx b/packages/2d/src/editor/tree/DetachedRoot.tsx
index 5d9085ee..c5ac3bc9 100644
--- a/packages/2d/src/editor/tree/DetachedRoot.tsx
+++ b/packages/2d/src/editor/tree/DetachedRoot.tsx
@@ -1,22 +1,26 @@
-import {useSignal} from '@preact/signals';
import {usePluginState} from '../Provider';
import {NodeElement} from './NodeElement';
import {TreeElement} from './TreeElement';
import {TreeRoot} from './TreeRoot';
export function DetachedRoot() {
- const {afterRender, scene} = usePluginState();
- const open = useSignal(false);
+ const {afterRender, openDetached, scene} = usePluginState();
const currentScene = scene.value;
const children = currentScene ? [...currentScene.getDetachedNodes()] : [];
afterRender.value;
return children.length > 0 ? (
<TreeRoot>
- <TreeElement open={open} label="Detached nodes">
- {children.map(child => (
- <NodeElement node={child} depth={1} />
- ))}
+ <TreeElement
+ open={openDetached.value}
+ hasChildren
+ onToggle={value => {
+ openDetached.value = value;
+ }}
+ label="Detached nodes"
+ >
+ {openDetached.value &&
+ children.map(child => <NodeElement node={child} depth={1} />)}
</TreeElement>
</TreeRoot>
) : null;
diff --git a/packages/2d/src/editor/tree/NodeElement.tsx b/packages/2d/src/editor/tree/NodeElement.tsx
index 8805c4b7..2157cd28 100644
--- a/packages/2d/src/editor/tree/NodeElement.tsx
+++ b/packages/2d/src/editor/tree/NodeElement.tsx
@@ -1,6 +1,6 @@
import {NODE_NAME, Node} from '@motion-canvas/2d';
import {useComputed, useSignal, useSignalEffect} from '@preact/signals';
-import {useRef} from 'preact/hooks';
+import {useEffect, useRef} from 'preact/hooks';
import {usePluginState} from '../Provider';
import {IconMap} from '../icons/IconMap';
import {TreeElement} from './TreeElement';
@@ -11,38 +11,34 @@ interface NodeElementProps {
}
export function NodeElement({node, depth = 0}: NodeElementProps) {
- const {selectedKey, hoveredKey, openNodes, selectedChain, afterRender} =
- usePluginState();
+ const {
+ selectedNode,
+ visibleNodes,
+ selectNode,
+ hoveredKey,
+ openNodes,
+ afterRender,
+ } = usePluginState();
const ref = useRef<HTMLDivElement>(null);
- const open = useSignal(
- selectedChain.peek().has(node.key) || (openNodes.get(node.key) ?? false),
- );
const nodeSignal = useSignal(node);
nodeSignal.value = node;
+ const open = useComputed(() => openNodes.has(nodeSignal.value.key));
+
+ useEffect(() => {
+ visibleNodes.add(node.key);
+ return () => {
+ visibleNodes.delete(node.key);
+ };
+ }, [node.key]);
+
const children = useComputed(() => {
afterRender.value;
return nodeSignal.value.peekChildren();
});
useSignalEffect(() => {
- open.value = openNodes.get(nodeSignal.value.key) ?? false;
- });
-
- useSignalEffect(() => {
- const chain = selectedChain.value;
- if (chain.has(nodeSignal.value.key)) {
- open.value = true;
- }
- });
-
- useSignalEffect(() => {
- openNodes.set(nodeSignal.value.key, open.value);
- });
-
- useSignalEffect(() => {
- const key = selectedKey.value;
- if (node.key === key) {
+ if (node.key === selectedNode.value?.key) {
ref.current?.scrollIntoView({block: 'nearest', behavior: 'instant'});
}
});
@@ -52,19 +48,22 @@ export function NodeElement({node, depth = 0}: NodeElementProps) {
return (
<TreeElement
forwardRef={ref}
- open={open}
+ open={open.value}
+ hasChildren={children.value.length > 0}
+ onToggle={value => openNodes.toggle(node.key, value)}
depth={depth}
icon={<Icon />}
label={node.key}
- selected={selectedKey.value === node.key}
+ selected={selectedNode.value === node}
onClick={event => {
- selectedKey.value = node.key;
+ selectNode(node.key);
event.stopPropagation();
}}
onPointerEnter={() => (hoveredKey.value = node.key)}
onPointerLeave={() => (hoveredKey.value = null)}
>
- {children.value.length > 0 &&
+ {open.value &&
+ children.value.length > 0 &&
children.value.map(child => (
<NodeElement node={child} depth={depth + 1} />
))}
diff --git a/packages/2d/src/editor/tree/TreeElement.tsx b/packages/2d/src/editor/tree/TreeElement.tsx
index 80fd29bb..449b21c7 100644
--- a/packages/2d/src/editor/tree/TreeElement.tsx
+++ b/packages/2d/src/editor/tree/TreeElement.tsx
@@ -1,5 +1,4 @@
-import {Collapse, Toggle} from '@motion-canvas/ui';
-import {Signal} from '@preact/signals';
+import {Toggle} from '@motion-canvas/ui';
import {clsx} from 'clsx';
import {ComponentChildren, JSX} from 'preact';
import {Ref} from 'preact/hooks';
@@ -8,12 +7,17 @@ import styles from './index.module.scss';
const DEPTH_VAR = '--depth';
interface TreeElementProps
- extends Omit<JSX.HTMLAttributes<HTMLDivElement>, 'label' | 'icon'> {
+ extends Omit<
+ JSX.HTMLAttributes<HTMLDivElement>,
+ 'label' | 'icon' | 'onToggle'
+ > {
icon?: ComponentChildren;
label: ComponentChildren;
children?: ComponentChildren;
selected?: boolean;
- open: Signal<boolean>;
+ open: boolean;
+ hasChildren?: boolean;
+ onToggle?: (value: boolean) => void;
depth?: number;
forwardRef?: Ref<HTMLDivElement>;
}
@@ -22,51 +26,46 @@ export function TreeElement({
label,
children,
selected,
+ hasChildren,
depth = 0,
open,
+ onToggle,
icon,
forwardRef,
...props
}: TreeElementProps) {
- const hasChildren = !!children;
-
return (
<>
<div
- ref={forwardRef}
className={clsx(
- styles.label,
+ styles.entry,
selected && styles.active,
hasChildren && styles.parent,
)}
onDblClick={() => {
if (hasChildren) {
- open.value = !open.value;
+ onToggle?.(!open);
}
}}
{...props}
style={{[DEPTH_VAR]: `${depth}`}}
>
- {hasChildren && (
- <Toggle
- animated={false}
- open={open.value}
- onToggle={value => {
- open.value = value;
- }}
- onDblClick={e => {
- e.stopPropagation();
- }}
- />
- )}
- {icon}
- {label}
+ <div ref={forwardRef} className={styles.label}>
+ {hasChildren && (
+ <Toggle
+ animated={false}
+ open={open}
+ onToggle={onToggle}
+ onDblClick={e => {
+ e.stopPropagation();
+ }}
+ />
+ )}
+ {icon}
+ {label}
+ </div>
</div>
- {hasChildren && (
- <Collapse open={open.value} animated={false}>
- {children}
- </Collapse>
- )}
+ {children}
</>
);
}
diff --git a/packages/2d/src/editor/tree/index.module.scss b/packages/2d/src/editor/tree/index.module.scss
index a41cea56..fb33710d 100644
--- a/packages/2d/src/editor/tree/index.module.scss
+++ b/packages/2d/src/editor/tree/index.module.scss
@@ -2,16 +2,17 @@
margin-top: -1px;
padding: 3px 0 4px;
border-top: 2px solid var(--background-color);
+ width: max-content;
+ min-width: 100%;
}
-.label {
+.entry {
--depth: 0;
padding: 4px 0 4px calc(var(--depth) * 22px + 24px);
border-radius: var(--radius);
user-select: none;
cursor: pointer;
- display: flex;
&:hover {
background-color: var(--surface-color-hover);
@@ -26,6 +27,12 @@
&.parent {
padding-left: calc(var(--depth) * 22px);
}
+}
+
+.label {
+ width: max-content;
+ display: flex;
+ padding-right: 8px;
> svg {
width: 20px;
diff --git a/packages/2d/src/editor/tree/index.ts b/packages/2d/src/editor/tree/index.ts
index 0621afd2..8c298004 100644
--- a/packages/2d/src/editor/tree/index.ts
+++ b/packages/2d/src/editor/tree/index.ts
@@ -1,3 +1,4 @@
export * from './DetachedRoot';
export * from './NodeElement';
export * from './ViewRoot';
+export * from './navigation';
diff --git a/packages/2d/src/editor/tree/navigation.ts b/packages/2d/src/editor/tree/navigation.ts
new file mode 100644
index 00000000..ccd01079
--- /dev/null
+++ b/packages/2d/src/editor/tree/navigation.ts
@@ -0,0 +1,145 @@
+import {Node} from '@motion-canvas/2d';
+import {useShortcuts} from '@motion-canvas/ui';
+import {useComputed} from '@preact/signals';
+import {usePluginState} from '../Provider';
+import {SCENE_GRAPH_SHORTCUTS} from '../shortcuts';
+
+export function useKeyboardNavigation() {
+ const {selectedNode, scene, afterRender, openNodes, selectNode} =
+ usePluginState();
+
+ const detachedNodes = useComputed(() => {
+ afterRender.value;
+ const detachedNodes: string[] = [];
+ for (const node of scene.value?.getDetachedNodes() ?? []) {
+ detachedNodes.push(node.key);
+ }
+ return detachedNodes;
+ });
+
+ function selectView() {
+ selectNode(scene.value?.getView()?.key ?? null);
+ }
+
+ function findNextNodeUp(node: Node): string | null {
+ const detachedIndex = detachedNodes.value.indexOf(node.key);
+ if (detachedIndex > 0) {
+ return detachedNodes.value[detachedIndex - 1];
+ } else if (detachedIndex === 0) {
+ // Go back to the scene graph after reaching the top of detached nodes:
+ return findLowestNode();
+ }
+
+ const parent = node.parent();
+ if (!parent) {
+ return null;
+ }
+
+ const siblings = parent.peekChildren();
+ const index = siblings.indexOf(node);
+ if (index < 1) {
+ return parent.key;
+ }
+
+ let current = siblings[index - 1];
+ while (current) {
+ const children = current.peekChildren();
+ if (openNodes.has(current.key) && children.length > 0) {
+ current = children.at(-1)!;
+ } else {
+ return current.key;
+ }
+ }
+
+ return null;
+ }
+
+ function findNextNodeDown(node: Node): string | null {
+ if (openNodes.has(node.key)) {
+ const children = node.peekChildren();
+ if (children.length > 0) {
+ return children[0].key;
+ }
+ }
+
+ let current = node;
+ while (current) {
+ const parent = current.parent();
+ if (!parent) {
+ const detachedIndex = detachedNodes.value.indexOf(node.key);
+ if (detachedIndex >= 0) {
+ return detachedNodes.value[detachedIndex + 1] ?? null;
+ }
+
+ // Try jumping down to the detached section after reaching the bottom:
+ return detachedNodes.value[0] ?? null;
+ }
+
+ const siblings = parent.peekChildren();
+ const index = siblings.indexOf(current);
+ if (index < siblings.length - 1) {
+ return siblings[index + 1].key;
+ }
+
+ current = parent;
+ }
+
+ return null;
+ }
+
+ function findLowestNode(): string | null {
+ let current: Node | undefined = scene.value?.getView();
+ while (current) {
+ const children = current.peekChildren();
+ if (openNodes.has(current.key) && children.length > 0) {
+ current = children.at(-1)!;
+ } else {
+ return current.key;
+ }
+ }
+ return null;
+ }
+
+ useShortcuts(SCENE_GRAPH_SHORTCUTS, {
+ moveUp: () => {
+ const node = selectedNode.value;
+ if (!node) {
+ selectView();
+ return;
+ }
+
+ selectNode(findNextNodeUp(node) ?? node.key);
+ },
+ collapseOrMoveUp: () => {
+ const node = selectedNode.value;
+ if (!node) {
+ selectView();
+ return;
+ }
+
+ if (!openNodes.delete(node.key)) {
+ selectNode(node.parent()?.key ?? node.key);
+ }
+ },
+ modeDown: () => {
+ const node = selectedNode.value;
+ if (!node) {
+ selectView();
+ return;
+ }
+
+ selectNode(findNextNodeDown(node) ?? node.key);
+ },
+ expandOrMoveDown: () => {
+ const node = selectedNode.value;
+ if (!node) {
+ selectView();
+ return;
+ }
+
+ if (node.peekChildren().length === 0 || !openNodes.add(node.key)) {
+ selectNode(findNextNodeDown(node) ?? node.key);
+ }
+ },
+ });
+}
diff --git a/packages/2d/src/editor/utils/SignalSet.ts b/packages/2d/src/editor/utils/SignalSet.ts
new file mode 100644
index 00000000..46f458d3
--- /dev/null
+++ b/packages/2d/src/editor/utils/SignalSet.ts
@@ -0,0 +1,37 @@
+import {signal} from '@preact/signals';
+
+export class SignalSet<T> {
+ private readonly set = new Set<T>();
+ private readonly signal = signal(0);
+
+ public has(value: T) {
+ this.signal.value;
+ return this.set.has(value);
+ }
+
+ public peekHas(value: T) {
+ return this.set.has(value);
+ }
+
+ public add(value: T): boolean {
+ if (this.set.has(value)) {
+ return false;
+ }
+
+ this.set.add(value);
+ this.signal.value++;
+ return true;
+ }
+
+ public delete(value: T): boolean {
+ const result = this.set.delete(value);
+ if (result) {
+ this.signal.value++;
+ }
+ return result;
+ }
+
+ public toggle(value: T, state: boolean): boolean {
+ return state ? this.add(value) : this.delete(value);
+ }
+}
diff --git a/packages/2d/src/editor/utils/index.ts b/packages/2d/src/editor/utils/index.ts
new file mode 100644
index 00000000..6e605a13
--- /dev/null
+++ b/packages/2d/src/editor/utils/index.ts
@@ -0,0 +1 @@
+export * from './SignalSet';
diff --git a/packages/ui/src/components/tabs/Pane.tsx b/packages/ui/src/components/tabs/Pane.tsx
index 9959f5c2..9f9ae486 100644
--- a/packages/ui/src/components/tabs/Pane.tsx
+++ b/packages/ui/src/components/tabs/Pane.tsx
@@ -1,16 +1,18 @@
import {ComponentChildren, JSX} from 'preact';
+import {Ref} from 'preact/hooks';
import {Header} from '../layout';
import styles from './Tabs.module.scss';
export interface PaneProps extends JSX.HTMLAttributes<HTMLDivElement> {
+ forwardRef?: Ref<HTMLDivElement>;
title: string;
id?: string;
children: ComponentChildren;
}
-export function Pane({title, id, children, ...props}: PaneProps) {
+export function Pane({title, id, children, forwardRef, ...props}: PaneProps) {
return (
- <div className={styles.pane} id={id} {...props}>
+ <div ref={forwardRef} className={styles.pane} id={id} {...props}>
<Header>{title}</Header>
{children}
</div>
diff --git a/packages/ui/src/contexts/index.ts b/packages/ui/src/contexts/index.ts
index a5799c5a..3721de92 100644
--- a/packages/ui/src/contexts/index.ts
+++ b/packages/ui/src/contexts/index.ts
@@ -1,4 +1,5 @@
export * from './application';
export * from './panels';
+export * from './shortcuts';
export * from './timeline';
export * from './viewport';
diff --git a/packages/ui/src/contexts/shortcuts.tsx b/packages/ui/src/contexts/shortcuts.tsx
index ecf7263b..d3af34a4 100644
--- a/packages/ui/src/contexts/shortcuts.tsx
+++ b/packages/ui/src/contexts/shortcuts.tsx
@@ -2,6 +2,7 @@ import {Signal, useSignal} from '@preact/signals';
import {ComponentChildren, createContext} from 'preact';
import {Ref, useContext, useEffect, useRef} from 'preact/hooks';
import {MouseButton} from '../utils';
+import {useApplication} from './application';
export interface Action {
name: string;
@@ -35,7 +36,7 @@ export type ShortcutCallbacks<T extends string> = {
[Key in T]?: ShortcutCallback;
};
-function makeShortcuts<T extends string>(
+export function makeShortcuts<T extends string>(
context: string,
shortcuts: ShortcutMap<T>,
): ShortcutConfig<T> {
@@ -207,13 +208,8 @@ interface ShortcutsContextValue {
const ShortcutsContext = createContext<ShortcutsContextValue>(null);
-export function ShortcutsProvider({
- configs,
- children,
-}: {
- children: ComponentChildren;
- configs: ShortcutConfig<string>[];
-}) {
+export function ShortcutsProvider({children}: {children: ComponentChildren}) {
+ const {plugins} = useApplication();
const global = useSignal<string | null>(null);
const surface = useSignal<string | null>(null);
const action = useSignal<Action | null>(null);
@@ -224,7 +220,13 @@ export function ShortcutsProvider({
});
const callbacks = useRef<CallbackMap>(new Map());
const configMap: ConfigMap = new Map();
- for (const config of configs) {
+ for (const config of [
+ GLOBAL_EDITOR_SHORTCUTS,
+ GLOBAL_PRESENTER_SHORTCUTS,
+ TIMELINE_SHORTCUTS,
+ VIEWPORT_SHORTCUTS,
+ ...plugins.flatMap(plugin => plugin.shortcuts ?? []),
+ ]) {
const shortcutMap = configMap.get(config.context) ?? {};
for (const [key, shortcut] of Object.entries(config.shortcuts)) {
if (key in shortcutMap) {
diff --git a/packages/ui/src/main.tsx b/packages/ui/src/main.tsx
index daf2bb77..5ab3317a 100644
--- a/packages/ui/src/main.tsx
+++ b/packages/ui/src/main.tsx
@@ -10,14 +10,11 @@ import {
import {ComponentChild, render} from 'preact';
import {Editor} from './Editor';
import {ProjectData, ProjectSelection} from './ProjectSelection';
-import {ApplicationProvider, PanelsProvider} from './contexts';
import {
- GLOBAL_EDITOR_SHORTCUTS,
- GLOBAL_PRESENTER_SHORTCUTS,
+ ApplicationProvider,
+ PanelsProvider,
ShortcutsProvider,
- TIMELINE_SHORTCUTS,
- VIEWPORT_SHORTCUTS,
-} from './contexts/shortcuts';
+} from './contexts';
import GridPlugin from './plugin/GridPlugin';
import {projectNameSignal} from './signals';
import {getItem, setItem} from './utils';
@@ -27,6 +24,8 @@ const ExperimentalHooks = [
'provider',
'previewOverlay',
'presenterOverlay',
+ 'inspectors',
+ 'shortcuts',
] as const;
function renderRoot(vnode: ComponentChild) {
@@ -163,14 +162,7 @@ export function editor(project: Project) {
}}
>
<PanelsProvider>
- <ShortcutsProvider
- configs={[
- GLOBAL_EDITOR_SHORTCUTS,
- GLOBAL_PRESENTER_SHORTCUTS,
- TIMELINE_SHORTCUTS,
- VIEWPORT_SHORTCUTS,
- ]}
- >
+ <ShortcutsProvider>
<Editor />
</ShortcutsProvider>
</PanelsProvider>
diff --git a/packages/ui/src/plugin/EditorPlugin.ts b/packages/ui/src/plugin/EditorPlugin.ts
index 9b021b1f..51189751 100644
--- a/packages/ui/src/plugin/EditorPlugin.ts
+++ b/packages/ui/src/plugin/EditorPlugin.ts
@@ -1,5 +1,6 @@
import {Plugin} from '@motion-canvas/core';
import {FunctionComponent} from 'preact';
+import {ShortcutConfig} from '../contexts';
/**
* Properties passed to the {@link PluginTabConfig.tabComponent} and
@@ -118,4 +119,8 @@ export interface EditorPlugin extends Plugin {
* Configuration for custom inspectors displayed on the right.
*/
inspectors?: PluginInspectorConfig[];
+ /**
+ * Configuration for custom shortcuts used in the editor.
+ */
+ shortcuts?: ShortcutConfig<string>[];
}