Skip to content

Commit b6358e2

Browse files
committed
refactor(handlers): move lastkeys to module level and rename function
- Move lastkeys state outside handlers and assign inside each handler - Rename setupKeyboardShortcuts to handleKeyboardShortcuts for consistency - Simplify hotKeys function by removing redundant lastkeys assignments
1 parent f0843ba commit b6358e2

3 files changed

Lines changed: 93 additions & 126 deletions

File tree

src/livecodes/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { customEvents } from './events/custom-events';
4545
import { exportJSON } from './export/export-json';
4646
import { getFormatter } from './formatter';
4747
import type { Formatter } from './formatter/models';
48-
import { setupKeyboardShortcuts } from './handlers/keyboard-shortcuts';
48+
import { handleKeyboardShortcuts } from './handlers';
4949
import {
5050
aboutScreen,
5151
customSettingsScreen,
@@ -4960,7 +4960,7 @@ const basicHandlers = () => {
49604960
handleChangeLanguage();
49614961
handleChangeContent();
49624962
// Setup keyboard shortcuts with dependency injection
4963-
setupKeyboardShortcuts({
4963+
handleKeyboardShortcuts({
49644964
eventsManager,
49654965
getActiveEditor,
49664966
getConfig,

src/livecodes/handlers/__tests__/keyboard-shortcuts.test.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as UI from '../../UI/selectors';
22
import { ctrl } from '../../utils';
3-
import { setupKeyboardShortcuts, type KeyboardShortcutDeps } from '../keyboard-shortcuts';
3+
import { handleKeyboardShortcuts, type KeyboardShortcutDeps } from '../keyboard-shortcuts';
44

55
// Mock the UI selectors module
66
jest.mock('../../UI/selectors', () => ({
@@ -96,9 +96,9 @@ describe('Keyboard Shortcuts Handler', () => {
9696
document.querySelectorAll = jest.fn().mockReturnValue([{ classList: { add: jest.fn() } }]);
9797
});
9898

99-
describe('setupKeyboardShortcuts', () => {
99+
describe('handleKeyboardShortcuts', () => {
100100
it('should add event listener to window', () => {
101-
setupKeyboardShortcuts(mockDeps);
101+
handleKeyboardShortcuts(mockDeps);
102102

103103
expect(mockEventsManager.addEventListener).toHaveBeenCalledWith(
104104
window,
@@ -109,13 +109,13 @@ describe('Keyboard Shortcuts Handler', () => {
109109
});
110110

111111
it('should handle keyboard events and update lastkeys', () => {
112-
setupKeyboardShortcuts(mockDeps);
112+
handleKeyboardShortcuts(mockDeps);
113113
simulateKeydown({ ctrlKey: true, code: 'KeyP' });
114114
expect(mockActiveEditor.monaco.trigger).toHaveBeenCalled();
115115
});
116116

117117
it('should handle non-modifier keys by updating lastkeys', () => {
118-
setupKeyboardShortcuts(mockDeps);
118+
handleKeyboardShortcuts(mockDeps);
119119
// Should not throw
120120
expect(() => {
121121
simulateKeydown({ key: 'a' });
@@ -124,7 +124,7 @@ describe('Keyboard Shortcuts Handler', () => {
124124
});
125125

126126
describe('Command Palette (Ctrl+P)', () => {
127-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
127+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
128128

129129
it('should trigger Monaco command palette on Ctrl+P', () => {
130130
const event = simulateKeydown({ ctrlKey: true, code: 'KeyP' });
@@ -150,7 +150,7 @@ describe('Keyboard Shortcuts Handler', () => {
150150
});
151151

152152
describe('Prevent Bookmark (Ctrl+D)', () => {
153-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
153+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
154154

155155
it('should prevent default on Ctrl+D', () => {
156156
const event = simulateKeydown({ ctrlKey: true, code: 'KeyD' });
@@ -164,7 +164,7 @@ describe('Keyboard Shortcuts Handler', () => {
164164
});
165165

166166
describe('Console Toggle (Ctrl+Alt+C)', () => {
167-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
167+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
168168

169169
it('should toggle console on Ctrl+Alt+C', () => {
170170
const mockConsoleButton = { dispatchEvent: jest.fn() };
@@ -186,7 +186,7 @@ describe('Keyboard Shortcuts Handler', () => {
186186
});
187187

188188
describe('Console Maximize (Ctrl+Alt+F after Ctrl+Alt+C)', () => {
189-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
189+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
190190

191191
it('should maximize console on Ctrl+Alt+F after Ctrl+Alt+C', () => {
192192
const mockConsoleButton = { dispatchEvent: jest.fn() };
@@ -211,7 +211,7 @@ describe('Keyboard Shortcuts Handler', () => {
211211

212212
// Press Ctrl+Alt+F without prior Ctrl+Alt+C (in non-embed mode, this triggers focus mode)
213213
mockDeps.isEmbed = false;
214-
setupKeyboardShortcuts(mockDeps);
214+
handleKeyboardShortcuts(mockDeps);
215215
simulateKeydown({ ctrlKey: true, altKey: true, code: 'KeyF' });
216216

217217
// Console maximize should not be triggered (dblclick event)
@@ -223,7 +223,7 @@ describe('Keyboard Shortcuts Handler', () => {
223223
});
224224

225225
describe('Run Tests (Ctrl+Alt+T)', () => {
226-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
226+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
227227

228228
it('should run tests on Ctrl+Alt+T', () => {
229229
const mockRunTestsButton = { click: jest.fn() };
@@ -245,7 +245,7 @@ describe('Keyboard Shortcuts Handler', () => {
245245
});
246246

247247
describe('Run Code (Shift+Enter)', () => {
248-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
248+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
249249

250250
it('should run code on Shift+Enter', () => {
251251
const mockRunButton = { click: jest.fn() };
@@ -267,7 +267,7 @@ describe('Keyboard Shortcuts Handler', () => {
267267
});
268268

269269
describe('Result Toggle (Ctrl+Alt+R)', () => {
270-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
270+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
271271

272272
it('should toggle result on Ctrl+Alt+R', () => {
273273
const mockResultButton = { click: jest.fn() };
@@ -281,7 +281,7 @@ describe('Keyboard Shortcuts Handler', () => {
281281
});
282282

283283
describe('Zoom Toggle (Ctrl+Alt+Z)', () => {
284-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
284+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
285285

286286
it('should toggle zoom on Ctrl+Alt+Z', () => {
287287
const mockZoomButton = { click: jest.fn() };
@@ -295,7 +295,7 @@ describe('Keyboard Shortcuts Handler', () => {
295295
});
296296

297297
describe('Focus Editor (Ctrl+Alt+E)', () => {
298-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
298+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
299299

300300
it('should focus editor on Ctrl+Alt+E', () => {
301301
const event = simulateKeydown({ ctrlKey: true, altKey: true, code: 'KeyE' });
@@ -306,7 +306,7 @@ describe('Keyboard Shortcuts Handler', () => {
306306
});
307307

308308
describe('Escape Key Handling', () => {
309-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
309+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
310310

311311
it('should hide menus and return "Esc" on first escape', () => {
312312
simulateKeydown({ code: 'Escape' });
@@ -364,7 +364,7 @@ describe('Keyboard Shortcuts Handler', () => {
364364
});
365365

366366
describe('Editor Switching (Ctrl+Alt+1/2/3/Arrow)', () => {
367-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
367+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
368368

369369
it('should switch to editor 1 on Ctrl+Alt+1', () => {
370370
const event = simulateKeydown({ ctrlKey: true, altKey: true, key: '1' });
@@ -416,7 +416,7 @@ describe('Keyboard Shortcuts Handler', () => {
416416
});
417417

418418
describe('Project Management Shortcuts (non-embed only)', () => {
419-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
419+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
420420

421421
it('should create new project on Ctrl+Alt+N when not embed', () => {
422422
const mockNewLink = { click: jest.fn() };
@@ -478,7 +478,7 @@ describe('Keyboard Shortcuts Handler', () => {
478478
describe('Embed Mode Restrictions', () => {
479479
beforeEach(() => {
480480
mockDeps.isEmbed = true;
481-
setupKeyboardShortcuts(mockDeps);
481+
handleKeyboardShortcuts(mockDeps);
482482
});
483483

484484
it('should return false for new project when in embed mode', () => {
@@ -539,7 +539,7 @@ describe('Keyboard Shortcuts Handler', () => {
539539
});
540540

541541
describe('Edge Cases and Error Handling', () => {
542-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
542+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
543543

544544
it('should handle missing UI elements gracefully', () => {
545545
(UI.getConsoleButton as jest.Mock).mockReturnValue(null);
@@ -554,7 +554,7 @@ describe('Keyboard Shortcuts Handler', () => {
554554

555555
it('should handle missing split gracefully', () => {
556556
mockDeps.split = undefined;
557-
setupKeyboardShortcuts(mockDeps);
557+
handleKeyboardShortcuts(mockDeps);
558558

559559
expect(() => {
560560
simulateKeydown({ ctrlKey: true, altKey: true, key: '1' });
@@ -567,7 +567,7 @@ describe('Keyboard Shortcuts Handler', () => {
567567
getActiveEditor: jest.fn().mockReturnValue({}),
568568
toolsPane: undefined,
569569
};
570-
setupKeyboardShortcuts(incompleteDeps);
570+
handleKeyboardShortcuts(incompleteDeps);
571571

572572
// Should not throw even with missing toolsPane
573573
expect(() => {
@@ -578,7 +578,7 @@ describe('Keyboard Shortcuts Handler', () => {
578578
});
579579

580580
describe('Key Combination Validation', () => {
581-
beforeEach(() => setupKeyboardShortcuts(mockDeps));
581+
beforeEach(() => handleKeyboardShortcuts(mockDeps));
582582

583583
it('should correctly identify Ctrl key combinations', () => {
584584
const event1 = simulateKeydown({ ctrlKey: true, code: 'KeyD' });

0 commit comments

Comments
 (0)