Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Preserve additive shortcut modifiers

JonnyBurger0d8827e88d11
diff --git a/packages/studio/src/components/KeyboardShortcutsSettings.tsx b/packages/studio/src/components/KeyboardShortcutsSettings.tsx
index f461e8a0fb..6030a88899 100644
--- a/packages/studio/src/components/KeyboardShortcutsSettings.tsx
+++ b/packages/studio/src/components/KeyboardShortcutsSettings.tsx
@@ -25,6 +25,7 @@ import {
formatKeyboardShortcut,
getKeyboardShortcutsForAction,
keyboardShortcutGroups,
+ keyboardShortcutsOverlap,
shortcutFromKeyboardEvent,
} from './keyboard-shortcuts';
import {Spacing} from './layout';
@@ -417,7 +418,7 @@ export const KeyboardShortcutsSettings: React.FC = () => {
item.actionId,
configuredShortcuts,
).some((candidate) =>
- isSameShortcut(candidate, value),
+ keyboardShortcutsOverlap(candidate, value),
),
);
if (conflict) {
diff --git a/packages/studio/src/components/keyboard-shortcuts.ts b/packages/studio/src/components/keyboard-shortcuts.ts
index 5975d8f0d7..c32b700beb 100644
--- a/packages/studio/src/components/keyboard-shortcuts.ts
+++ b/packages/studio/src/components/keyboard-shortcuts.ts
@@ -239,11 +239,18 @@ export const keyboardEventMatchesShortcut = ({
event.key.toLowerCase() === normalizeKey(value.key).toLowerCase() &&
commandOrControl === (value.commandOrControl ?? false) &&
(isMac ? !event.ctrlKey : !event.metaKey) &&
- event.shiftKey === (value.shift ?? false) &&
- event.altKey === (value.alt ?? false)
+ (!value.shift || event.shiftKey) &&
+ (!value.alt || event.altKey)
);
};
+export const keyboardShortcutsOverlap = (
+ first: StudioKeyboardShortcut,
+ second: StudioKeyboardShortcut,
+) =>
+ first.key.toLowerCase() === second.key.toLowerCase() &&
+ (first.commandOrControl ?? false) === (second.commandOrControl ?? false);
+
export const keyboardEventMatchesAction = (
event: KeyboardEvent,
action: StudioKeyboardShortcutAction,
diff --git a/packages/studio/src/test/keyboard-shortcuts.test.ts b/packages/studio/src/test/keyboard-shortcuts.test.ts
index 3064076d66..330188af3d 100644
--- a/packages/studio/src/test/keyboard-shortcuts.test.ts
+++ b/packages/studio/src/test/keyboard-shortcuts.test.ts
@@ -4,6 +4,7 @@ import {
formatKeyboardShortcut,
formatKeyboardShortcutForAria,
keyboardEventMatchesShortcut,
+ keyboardShortcutsOverlap,
} from '../components/keyboard-shortcuts';
import {isMac} from '../helpers/is-mac';
@@ -35,7 +36,13 @@ test('includes main-row and numeric-keypad zoom-in shortcuts', () => {
]);
});
-test('matches all shortcut modifiers exactly', () => {
+test('matches shortcut modifiers', () => {
+ expect(
+ keyboardEventMatchesShortcut({
+ event: event({altKey: true, key: 'a', shiftKey: true}),
+ shortcut: {key: 'a'},
+ }),
+ ).toBe(true);
expect(
keyboardEventMatchesShortcut({
event: event(isMac ? {metaKey: true} : {ctrlKey: true}),
@@ -46,11 +53,23 @@ test('matches all shortcut modifiers exactly', () => {
keyboardEventMatchesShortcut({
event: event(
isMac
- ? {metaKey: true, shiftKey: true}
- : {ctrlKey: true, shiftKey: true},
+ ? {altKey: true, metaKey: true, shiftKey: true}
+ : {altKey: true, ctrlKey: true, shiftKey: true},
),
shortcut: {key: 'k', commandOrControl: true},
}),
+ ).toBe(true);
+ expect(
+ keyboardEventMatchesShortcut({
+ event: event({key: 'k'}),
+ shortcut: {key: 'k', shift: true},
+ }),
+ ).toBe(false);
+ expect(
+ keyboardEventMatchesShortcut({
+ event: event({key: 'k'}),
+ shortcut: {alt: true, key: 'k'},
+ }),
).toBe(false);
expect(
keyboardEventMatchesShortcut({
@@ -60,6 +79,18 @@ test('matches all shortcut modifiers exactly', () => {
).toBe(true);
});
+test('detects overlapping shortcuts', () => {
+ expect(keyboardShortcutsOverlap({key: 'a'}, {key: 'A', shift: true})).toBe(
+ true,
+ );
+ expect(
+ keyboardShortcutsOverlap({alt: true, key: 'a'}, {key: 'a', shift: true}),
+ ).toBe(true);
+ expect(
+ keyboardShortcutsOverlap({key: 'a'}, {commandOrControl: true, key: 'a'}),
+ ).toBe(false);
+});
+
test('formats a shortcut for display', () => {
expect(
formatKeyboardShortcut({key: 'ArrowLeft', commandOrControl: true}),