Skip to content

🎥 Make videos programmatically with React

quick switcher Cmd+K

Jonny Burger32ab0e57def2
diff --git a/packages/cli/src/editor/components/Editor.tsx b/packages/cli/src/editor/components/Editor.tsx
index 070d8c6d67..990d9ec3da 100644
--- a/packages/cli/src/editor/components/Editor.tsx
+++ b/packages/cli/src/editor/components/Editor.tsx
@@ -12,6 +12,7 @@ import {KeyboardShortcuts} from './KeyboardShortcutsModal';
import NewComposition from './NewComposition/NewComposition';
import {NoRegisterRoot} from './NoRegisterRoot';
import {NotificationCenter} from './Notifications/NotificationCenter';
+import QuickSwitcher from './QuickSwitcher/QuickSwitcher';
import {UpdateModal} from './UpdateModal/UpdateModal';
import {ZoomPersistor} from './ZoomPersistor';
@@ -61,13 +62,19 @@ export const Editor: React.FC = () => {
{Root === null ? <NoRegisterRoot /> : <EditorContent />}
<GlobalKeybindings />
</Internals.CanUseRemotionHooksProvider>
+ <NotificationCenter />
+
+ {modalContextType && modalContextType.type === 'quick-switcher' && (
+ // Quick switcher here because requires timeline zoom ctx
+ <QuickSwitcher />
+ )}
</div>
</TimelineZoomContext>
- <NotificationCenter />
{modalContextType && modalContextType.type === 'new-comp' && (
<NewComposition initialCompType={modalContextType.compType} />
)}
+
{modalContextType && modalContextType.type === 'update' && (
<UpdateModal info={modalContextType.info} />
)}
diff --git a/packages/cli/src/editor/components/GlobalKeybindings.tsx b/packages/cli/src/editor/components/GlobalKeybindings.tsx
index dd26014fb7..8a5c1a8fd7 100644
--- a/packages/cli/src/editor/components/GlobalKeybindings.tsx
+++ b/packages/cli/src/editor/components/GlobalKeybindings.tsx
@@ -21,6 +21,16 @@ export const GlobalKeybindings: React.FC = () => {
},
commandCtrlKey: false,
});
+ const cmdKKey = keybindings.registerKeybinding({
+ event: 'keydown',
+ key: 'k',
+ callback: () => {
+ setSelectedModal({
+ type: 'quick-switcher',
+ });
+ },
+ commandCtrlKey: true,
+ });
const cKey = keybindings.registerKeybinding({
event: 'keypress',
@@ -45,6 +55,7 @@ export const GlobalKeybindings: React.FC = () => {
nKey.unregister();
cKey.unregister();
questionMark.unregister();
+ cmdKKey.unregister();
};
}, [keybindings, setCheckerboard, setSelectedModal]);
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsx
index 884dad6bad..d42f14008d 100644
--- a/packages/cli/src/editor/components/PlayPause.tsx
+++ b/packages/cli/src/editor/components/PlayPause.tsx
@@ -71,10 +71,10 @@ export const PlayPause: React.FC<{
const onEnter = useCallback(
(e: KeyboardEvent) => {
if (playing) {
+ // Don't prevent keyboard navigation
+ e.preventDefault();
pauseAndReturnToPlayStart();
}
-
- e.preventDefault();
},
[pauseAndReturnToPlayStart, playing]
);
diff --git a/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcher.tsx b/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcher.tsx
new file mode 100644
index 0000000000..306579f932
--- /dev/null
+++ b/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcher.tsx
@@ -0,0 +1,20 @@
+import React, {useCallback, useContext} from 'react';
+import {ModalsContext} from '../../state/modals';
+import {ModalContainer} from '../ModalContainer';
+import {QuickSwitcherContent} from './QuickSwitcherContent';
+
+const QuickSwitcher: React.FC = () => {
+ const {setSelectedModal} = useContext(ModalsContext);
+
+ const onQuit = useCallback(() => {
+ setSelectedModal(null);
+ }, [setSelectedModal]);
+
+ return (
+ <ModalContainer onOutsideClick={onQuit} onEscape={onQuit}>
+ <QuickSwitcherContent />
+ </ModalContainer>
+ );
+};
+
+export default QuickSwitcher;
diff --git a/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherContent.tsx b/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherContent.tsx
new file mode 100644
index 0000000000..f57fafa09b
--- /dev/null
+++ b/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherContent.tsx
@@ -0,0 +1,158 @@
+import React, {
+ useCallback,
+ useContext,
+ useEffect,
+ useMemo,
+ useState,
+} from 'react';
+import {Internals} from 'remotion';
+import {INPUT_BORDER_COLOR_UNHOVERED} from '../../helpers/colors';
+import {isCompositionStill} from '../../helpers/is-composition-still';
+import {useKeybinding} from '../../helpers/use-keybinding';
+import {ModalsContext} from '../../state/modals';
+import {useSelectComposition} from '../InitialCompositionLoader';
+import {fuzzySearch} from './fuzzy-search';
+import type {TQuickSwitcherResult} from './QuickSwitcherResult';
+import {QuickSwitcherResult} from './QuickSwitcherResult';
+
+const input: React.CSSProperties = {
+ padding: 4,
+ border: '2px solid ' + INPUT_BORDER_COLOR_UNHOVERED,
+ width: '100%',
+};
+
+const container: React.CSSProperties = {
+ width: 500,
+};
+
+const content: React.CSSProperties = {
+ padding: 16,
+};
+
+const results: React.CSSProperties = {
+ overflowY: 'auto',
+ height: 300,
+};
+
+const loopIndex = (index: number, length: number) => {
+ if (index < 0) {
+ index += length;
+ }
+
+ return index % length;
+};
+
+// Separate component to correctly capture keybindings
+export const QuickSwitcherContent: React.FC = () => {
+ const {compositions} = useContext(Internals.CompositionManager);
+ const [state, setState] = useState({
+ query: '',
+ selectedIndex: 0,
+ });
+ const selectComposition = useSelectComposition();
+
+ const {setSelectedModal} = useContext(ModalsContext);
+
+ const keybindings = useKeybinding();
+ const resultsArray = useMemo((): TQuickSwitcherResult[] => {
+ return fuzzySearch(
+ state.query,
+ compositions.map((c) => {
+ return {
+ id: 'composition-' + c.id,
+ title: c.id,
+ type: 'composition',
+ onSelected: () => {
+ selectComposition(c, true);
+ setSelectedModal(null);
+ },
+ compositionType: isCompositionStill(c) ? 'still' : 'composition',
+ };
+ })
+ );
+ }, [compositions, state.query, selectComposition, setSelectedModal]);
+
+ const onArrowDown = useCallback(() => {
+ setState((s) => {
+ return {
+ ...s,
+ selectedIndex: s.selectedIndex + 1,
+ };
+ });
+ }, []);
+
+ const onArrowUp = useCallback(() => {
+ setState((s) => {
+ return {
+ ...s,
+ selectedIndex: s.selectedIndex - 1,
+ };
+ });
+ }, []);
+
+ // Arrow up
+ useEffect(() => {
+ const binding = keybindings.registerKeybinding({
+ key: 'ArrowUp',
+ callback: onArrowUp,
+ commandCtrlKey: false,
+ event: 'keydown',
+ });
+
+ return () => {
+ binding.unregister();
+ };
+ }, [keybindings, onArrowDown, onArrowUp]);
+
+ // Arrow down
+ useEffect(() => {
+ const binding = keybindings.registerKeybinding({
+ key: 'ArrowDown',
+ callback: onArrowDown,
+ commandCtrlKey: false,
+ event: 'keydown',
+ });
+
+ return () => {
+ binding.unregister();
+ };
+ }, [keybindings, onArrowDown]);
+
+ const onTextChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
+ (e) => {
+ setState({query: e.target.value, selectedIndex: 0});
+ },
+ []
+ );
+
+ const selectedIndexRounded = loopIndex(
+ state.selectedIndex,
+ resultsArray.length
+ );
+
+ return (
+ <div style={container}>
+ <div style={content}>
+ <input
+ type="text"
+ style={input}
+ autoFocus
+ value={state.query}
+ onChange={onTextChange}
+ placeholder="Search compositions..."
+ />
+ </div>
+ <div style={results}>
+ {resultsArray.map((result, i) => {
+ return (
+ <QuickSwitcherResult
+ key={result.id}
+ selected={selectedIndexRounded === i}
+ result={result}
+ />
+ );
+ })}
+ </div>
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherResult.tsx b/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherResult.tsx
new file mode 100644
index 0000000000..216fe84312
--- /dev/null
+++ b/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherResult.tsx
@@ -0,0 +1,122 @@
+import React, {useEffect, useMemo, useRef, useState} from 'react';
+import {getBackgroundFromHoverState, LIGHT_TEXT} from '../../helpers/colors';
+import {useKeybinding} from '../../helpers/use-keybinding';
+import {FilmIcon} from '../../icons/film';
+import {StillIcon} from '../../icons/still';
+import {Spacing} from '../layout';
+
+type QuickSwitcherResultDetail = {
+ type: 'composition';
+ compositionType: 'composition' | 'still';
+};
+
+export type TQuickSwitcherResult = {
+ title: string;
+ id: string;
+ onSelected: () => void;
+} & QuickSwitcherResultDetail;
+
+const container: React.CSSProperties = {
+ paddingLeft: 16,
+ paddingRight: 16,
+ paddingTop: 3,
+ paddingBottom: 3,
+ display: 'flex',
+ flexDirection: 'row',
+ alignItems: 'center',
+ cursor: 'pointer',
+};
+
+const label: React.CSSProperties = {
+ flex: 1,
+ whiteSpace: 'nowrap',
+ textOverflow: 'ellipsis',
+ overflow: 'hidden',
+};
+
+const iconStyle: React.CSSProperties = {
+ width: 14,
+ height: 14,
+};
+
+export const QuickSwitcherResult: React.FC<{
+ result: TQuickSwitcherResult;
+ selected: boolean;
+}> = ({result, selected}) => {
+ const [hovered, setIsHovered] = useState(false);
+ const ref = useRef<HTMLDivElement>(null);
+ const keybindings = useKeybinding();
+
+ useEffect(() => {
+ const {current} = ref;
+ if (!current) {
+ return;
+ }
+
+ const onMouseEnter = () => setIsHovered(true);
+ const onMouseLeave = () => setIsHovered(false);
+
+ current.addEventListener('mouseenter', onMouseEnter);
+ current.addEventListener('mouseleave', onMouseLeave);
+
+ return () => {
+ current.removeEventListener('mouseenter', onMouseEnter);
+ current.removeEventListener('mouseleave', onMouseLeave);
+ };
+ }, []);
+
+ useEffect(() => {
+ if (!selected) {
+ return;
+ }
+
+ const binding = keybindings.registerKeybinding({
+ key: 'Enter',
+ callback: result.onSelected,
+ commandCtrlKey: false,
+ event: 'keydown',
+ });
+
+ return () => {
+ binding.unregister();
+ };
+ }, [keybindings, result.onSelected, selected]);
+ useEffect(() => {
+ if (selected) {
+ ref.current?.scrollIntoView({
+ block: 'nearest',
+ inline: 'center',
+ });
+ }
+ }, [selected]);
+
+ const style = useMemo(() => {
+ return {
+ ...container,
+ backgroundColor: getBackgroundFromHoverState({
+ hovered,
+ selected,
+ }),
+ };
+ }, [hovered, selected]);
+
+ const labelStyle = useMemo(() => {
+ return {
+ ...label,
+ color: selected || hovered ? 'white' : LIGHT_TEXT,
+ fontSize: 15,
+ };
+ }, [hovered, selected]);
+
+ return (
+ <div ref={ref} key={result.id} style={style} onClick={result.onSelected}>
+ {result.compositionType === 'still' ? (
+ <StillIcon style={iconStyle} />
+ ) : (
+ <FilmIcon style={iconStyle} />
+ )}
+ <Spacing x={1} />
+ <div style={labelStyle}>{result.title}</div>
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/QuickSwitcher/fuzzy-search.ts b/packages/cli/src/editor/components/QuickSwitcher/fuzzy-search.ts
new file mode 100644
index 0000000000..d3d8c830d3
--- /dev/null
+++ b/packages/cli/src/editor/components/QuickSwitcher/fuzzy-search.ts
@@ -0,0 +1,28 @@
+import type {TQuickSwitcherResult} from './QuickSwitcherResult';
+
+export function fuzzySearch(
+ query: string,
+ dataset: TQuickSwitcherResult[]
+): TQuickSwitcherResult[] {
+ const q = query ? query.trim().toLowerCase() : '';
+
+ const matchingIndices: number[] = [];
+ if (q.length === 0) {
+ for (let i = 0; i < dataset.length; i++) {
+ matchingIndices.push(i);
+ }
+
+ return dataset.filter((_, i) => matchingIndices.includes(i));
+ }
+
+ dataset.forEach((d, index) => {
+ const s = d.title.trim().toLowerCase();
+ let i = 0;
+ let n = -1;
+ let l;
+ // eslint-disable-next-line no-bitwise
+ for (; (l = q[i++]); ) if (!~(n = s.indexOf(l, n + 1))) return;
+ matchingIndices.push(index);
+ });
+ return dataset.filter((_, i) => matchingIndices.includes(i));
+}
diff --git a/packages/cli/src/editor/state/modals.ts b/packages/cli/src/editor/state/modals.ts
index ce73efb726..d4e2d2a459 100644
--- a/packages/cli/src/editor/state/modals.ts
+++ b/packages/cli/src/editor/state/modals.ts
@@ -13,6 +13,9 @@ export type ModalState =
type: 'update';
info: UpdateInfo;
}
+ | {
+ type: 'quick-switcher';
+ }
| {
type: 'shortcuts';
};