🎥 Make videos programmatically with React
Move selected Studio items with arrow keys
JonnyBurger8c94be95d576
diff --git a/packages/studio/src/components/PlayPause.tsx b/packages/studio/src/components/PlayPause.tsxindex 8274c6bbf9..ab74bfc373 100644--- a/packages/studio/src/components/PlayPause.tsx+++ b/packages/studio/src/components/PlayPause.tsx@@ -163,11 +163,21 @@ export const PlayPause: React.FC<{const oneFrameBack = useCallback(() => {frameBack(1);- }, [frameBack]);+ ensureFrameIsInViewport({+ direction: 'fit-left',+ durationInFrames: getCurrentDuration(),+ frame: Math.max(0, getCurrentFrame() - 1),+ });+ }, [frameBack, getCurrentFrame]);const oneFrameForward = useCallback(() => {frameForward(1);- }, [frameForward]);+ ensureFrameIsInViewport({+ direction: 'fit-right',+ durationInFrames: getCurrentDuration(),+ frame: Math.min(getCurrentDuration() - 1, getCurrentFrame() + 1),+ });+ }, [frameForward, getCurrentFrame]);const jumpToStart = useCallback(() => {seek(inFrame ?? 0);@@ -198,6 +208,24 @@ export const PlayPause: React.FC<{triggerIfInputFieldFocused: false,keepRegisteredWhenNotHighestContext: false,});+ const commandArrowLeft = keybindings.registerKeybinding({+ event: 'keydown',+ key: 'ArrowLeft',+ callback: oneFrameBack,+ commandCtrlKey: true,+ preventDefault: true,+ triggerIfInputFieldFocused: false,+ keepRegisteredWhenNotHighestContext: false,+ });+ const commandArrowRight = keybindings.registerKeybinding({+ event: 'keydown',+ key: 'ArrowRight',+ callback: oneFrameForward,+ commandCtrlKey: true,+ preventDefault: true,+ triggerIfInputFieldFocused: false,+ keepRegisteredWhenNotHighestContext: false,+ });const space = keybindings.registerKeybinding({event: 'keydown',key: ' ',@@ -238,6 +266,8 @@ export const PlayPause: React.FC<{return () => {arrowLeft.unregister();arrowRight.unregister();+ commandArrowLeft.unregister();+ commandArrowRight.unregister();space.unregister();enter.unregister();a.unregister();@@ -251,6 +281,8 @@ export const PlayPause: React.FC<{onArrowRight,onEnter,onSpace,+ oneFrameBack,+ oneFrameForward,]);useEffect(() => {diff --git a/packages/studio/src/components/SelectedOutlineOverlay.tsx b/packages/studio/src/components/SelectedOutlineOverlay.tsxindex 8ab2a0e8e0..d856c8e4a6 100644--- a/packages/studio/src/components/SelectedOutlineOverlay.tsx+++ b/packages/studio/src/components/SelectedOutlineOverlay.tsx@@ -19,6 +19,7 @@ import {formatFileLocation} from '../helpers/format-file-location';import {getBoxQuadsPonyfill} from '../helpers/get-box-quads-ponyfill';import type {SequenceNodePathInfo} from '../helpers/get-timeline-sequence-sort-key';import {openOriginalPositionInEditor} from '../helpers/open-in-editor';+import {areKeyboardShortcutsDisabled} from '../helpers/use-keybinding';import {EditorShowOutlinesContext} from '../state/editor-outlines';import {ScaleLockContext} from '../state/scale-lock';import {ContextMenuForTarget} from './ContextMenu';@@ -851,6 +852,17 @@ export const getSelectedOutlineDragChanges = ({return changes;};+export const getSelectedOutlineKeyboardNudgeDelta = ({+ direction,+ shiftKey,+}: {+ readonly direction: 'left' | 'right';+ readonly shiftKey: boolean;+}) => {+ const increment = shiftKey ? 10 : 1;+ return direction === 'left' ? -increment : increment;+};+export type SelectedOutlineScaleEdge = 'top' | 'right' | 'bottom' | 'left';type SelectedOutlineScaleEdgeInfo = {@@ -2620,6 +2632,7 @@ export const SelectedOutlineOverlay: React.FC<{const {getDragOverrides, getEffectDragOverrides} = useContext(Internals.VisualModeDragOverridesContext,);+ const {setPropStatuses} = useContext(Internals.VisualModeSettersContext);const {getScaleLockState} = useContext(ScaleLockContext);const {editorShowOutlines} = useContext(EditorShowOutlinesContext);const timelinePosition = Internals.Timeline.useTimelinePosition();@@ -2928,6 +2941,124 @@ export const SelectedOutlineOverlay: React.FC<{);}, [outlineTargets]);+ useEffect(() => {+ if (areKeyboardShortcutsDisabled()) {+ return;+ }++ const onKeyDown = (event: KeyboardEvent) => {+ if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') {+ return;+ }++ if (event.metaKey || event.ctrlKey || event.altKey) {+ return;+ }++ const {activeElement} = document;+ if (+ activeElement instanceof HTMLInputElement ||+ activeElement instanceof HTMLTextAreaElement+ ) {+ return;+ }++ if (selectedItems.length === 0) {+ return;+ }++ event.preventDefault();+ event.stopImmediatePropagation();++ if (allDragTargets.length === 0) {+ return;+ }++ const dragStates = getSelectedOutlineDragStates({+ dragTargets: allDragTargets,+ getDragOverrides,+ timelinePosition,+ });+ const lastValues = getSelectedOutlineDragValues({+ dragStates,+ deltaX: getSelectedOutlineKeyboardNudgeDelta({+ direction: event.key === 'ArrowLeft' ? 'left' : 'right',+ shiftKey: event.shiftKey,+ }),+ deltaY: 0,+ });+ const changes = getSelectedOutlineDragChanges({+ dragStates,+ lastValues,+ });++ if (changes.length === 0) {+ return;+ }++ const staticChanges = changes.filter(+ (change): change is SelectedOutlineStaticDragChange =>+ change.type === 'static',+ );+ const keyframedChanges = changes.filter(+ (change): change is SelectedOutlineKeyframedDragChange =>+ change.type === 'keyframed',+ );+ const [firstDragTarget] = allDragTargets;+ if (firstDragTarget === undefined) {+ throw new Error('Expected a drag target');+ }++ Promise.all([+ staticChanges.length > 0+ ? saveSequenceProps({+ changes: staticChanges,+ setPropStatuses,+ clientId: firstDragTarget.clientId,+ undoLabel:+ changes.length > 1+ ? 'Move selected sequences'+ : 'Move sequence',+ redoLabel:+ changes.length > 1+ ? 'Move selected sequences back'+ : 'Move sequence back',+ })+ : Promise.resolve(),+ ...keyframedChanges.map((change) =>+ callAddSequenceKeyframe({+ fileName: change.fileName,+ nodePath: change.nodePath,+ fieldKey: change.fieldKey,+ sourceFrame: change.sourceFrame,+ value: change.value,+ schema: change.schema,+ setPropStatuses,+ clientId: change.clientId,+ }),+ ),+ ]).catch((err) => {+ showNotification(+ `Could not save sequence props: ${+ err instanceof Error ? err.message : String(err)+ }`,+ 4000,+ );+ });+ };++ window.addEventListener('keydown', onKeyDown, {capture: true});+ return () => {+ window.removeEventListener('keydown', onKeyDown, {capture: true});+ };+ }, [+ allDragTargets,+ getDragOverrides,+ selectedItems.length,+ setPropStatuses,+ timelinePosition,+ ]);+useEffect(() => {if (outlineTargets.length === 0) {setOutlines((prevOutlines) =>diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.tsindex 5dd35bc1d8..773f48fb86 100644--- a/packages/studio/src/test/timeline-selection.test.ts+++ b/packages/studio/src/test/timeline-selection.test.ts@@ -23,6 +23,7 @@ import {getSelectedEffectFieldsBySequenceKey,getSelectedOutlineDragChanges,getSelectedOutlineDragValues,+ getSelectedOutlineKeyboardNudgeDelta,getSelectedOutlineRotationCornerInfo,getSelectedOutlineRotationDeltaDegrees,getSelectedOutlineRotationDragChanges,@@ -1976,6 +1977,70 @@ test('Selected outline dragging can lock movement to the dominant axis', () => {).toEqual({deltaX: 12, deltaY: 13});});+test('Selected outline keyboard nudging moves horizontally by one or ten pixels', () => {+ const schema = {+ 'style.translate': {type: 'translate', default: '0px 0px'},+ } satisfies SequenceSchema;+ const nodePath = makeKey(['body', 0]);+ const dragStates = [+ {+ defaultValue: JSON.stringify('0px 0px'),+ key: Internals.makeSequencePropsSubscriptionKey(nodePath),+ sourceFrame: 12,+ startX: 10,+ startY: 20,+ target: {+ clientId: 'client',+ propStatus: {status: 'static', codeValue: '10px 20px'},+ fieldDefault: '0px 0px',+ keyframeDisplayOffset: 30,+ nodePath,+ schema,+ },+ },+ ] satisfies SelectedOutlineDragState[];++ expect(+ getSelectedOutlineKeyboardNudgeDelta({+ direction: 'left',+ shiftKey: false,+ }),+ ).toBe(-1);+ expect(+ getSelectedOutlineKeyboardNudgeDelta({+ direction: 'right',+ shiftKey: true,+ }),+ ).toBe(10);++ const lastValues = getSelectedOutlineDragValues({+ dragStates,+ deltaX: getSelectedOutlineKeyboardNudgeDelta({+ direction: 'right',+ shiftKey: true,+ }),+ deltaY: 0,+ });++ expect(lastValues.get(dragStates[0].key)).toBe('20px 20px');+ expect(+ getSelectedOutlineDragChanges({+ dragStates,+ lastValues,+ }),+ ).toEqual([+ {+ type: 'static',+ fileName: '/project/src/Comp.tsx',+ nodePath,+ fieldKey: 'style.translate',+ value: '20px 20px',+ defaultValue: JSON.stringify('0px 0px'),+ schema,+ },+ ]);+});+test('Selected outline dragging keyframed translate adds a keyframe at the source frame', () => {const schema = {'style.translate': {type: 'translate', default: '0px 0px'},