@@ -45,6 +45,7 @@ import { customEvents } from './events/custom-events';
4545import { exportJSON } from './export/export-json' ;
4646import { getFormatter } from './formatter' ;
4747import type { Formatter } from './formatter/models' ;
48+ import { handleKeyboardShortcuts } from './handlers' ;
4849import {
4950 aboutScreen ,
5051 customSettingsScreen ,
@@ -2566,199 +2567,6 @@ const handleChangeContent = () => {
25662567 } ) ;
25672568} ;
25682569
2569- const handleKeyboardShortcuts = ( ) => {
2570- let lastkeys = '' ;
2571-
2572- const hotKeys = async ( e : KeyboardEvent ) => {
2573- // Ctrl + P opens the command palette
2574- const activeEditor = getActiveEditor ( ) ;
2575- if ( ctrl ( e ) && e . code === 'KeyP' && activeEditor . monaco ) {
2576- e . preventDefault ( ) ;
2577- activeEditor . monaco . trigger ( 'anyString' , 'editor.action.quickCommand' ) ;
2578- lastkeys = 'Ctrl + P' ;
2579- return ;
2580- }
2581-
2582- // Ctrl + D prevents browser bookmark dialog
2583- if ( ctrl ( e ) && e . code === 'KeyD' ) {
2584- e . preventDefault ( ) ;
2585- lastkeys = 'Ctrl + D' ;
2586- return ;
2587- }
2588-
2589- // Ctrl + Alt + C: toggle console
2590- if ( ctrl ( e ) && e . altKey && e . code === 'KeyC' ) {
2591- e . preventDefault ( ) ;
2592- lastkeys = 'Ctrl + Alt + C' ;
2593- UI . getConsoleButton ( ) ?. dispatchEvent ( new Event ( 'touchstart' ) ) ;
2594- return ;
2595- }
2596-
2597- // Ctrl + Alt + C, F: maximize console
2598- if ( ctrl ( e ) && e . altKey && e . code === 'KeyF' && lastkeys === 'Ctrl + Alt + C' ) {
2599- e . preventDefault ( ) ;
2600- lastkeys = 'Ctrl + Alt + C, F' ;
2601- UI . getConsoleButton ( ) ?. dispatchEvent ( new Event ( 'dblclick' ) ) ;
2602- return ;
2603- }
2604-
2605- // Ctrl + Alt + T runs tests
2606- if ( ctrl ( e ) && e . altKey && e . code === 'KeyT' ) {
2607- e . preventDefault ( ) ;
2608- UI . getRunTestsButton ( ) ?. click ( ) ;
2609- lastkeys = 'Ctrl + Alt + T' ;
2610- return ;
2611- }
2612-
2613- // Shift + Enter triggers run
2614- if ( e . shiftKey && e . key === 'Enter' ) {
2615- e . preventDefault ( ) ;
2616- UI . getRunButton ( ) ?. click ( ) ;
2617- lastkeys = 'Shift + Enter' ;
2618- return ;
2619- }
2620-
2621- // Ctrl + Alt + R toggles result page
2622- if ( ctrl ( e ) && e . altKey && e . code === 'KeyR' ) {
2623- e . preventDefault ( ) ;
2624- UI . getResultButton ( ) ?. click ( ) ;
2625- lastkeys = 'Ctrl + Alt + R' ;
2626- return ;
2627- }
2628-
2629- // Ctrl + Alt + Z toggles result zoom
2630- if ( ctrl ( e ) && e . altKey && e . code === 'KeyZ' ) {
2631- e . preventDefault ( ) ;
2632- UI . getZoomButton ( ) ?. click ( ) ;
2633- lastkeys = 'Ctrl + Alt + Z' ;
2634- return ;
2635- }
2636-
2637- // Ctrl + Alt + E focuses active editor
2638- if ( ctrl ( e ) && e . altKey && e . code === 'KeyE' ) {
2639- e . preventDefault ( ) ;
2640- getActiveEditor ( ) . focus ( ) ;
2641- lastkeys = 'Ctrl + Alt + E' ;
2642- return ;
2643- }
2644-
2645- // Esc closes dropdown menus
2646- // Esc + Esc moves focus out of editor
2647- // Esc + Esc + Esc moves focus to logo
2648- if ( e . code === 'Escape' ) {
2649- document . querySelectorAll ( '.menu-scroller' ) . forEach ( ( el ) => el . classList . add ( 'hidden' ) ) ;
2650- if ( lastkeys === 'Esc' ) {
2651- e . preventDefault ( ) ;
2652- if (
2653- ( toolsPane ?. getStatus ( ) === 'open' || toolsPane ?. getStatus ( ) === 'full' ) &&
2654- toolsPane . getActiveTool ( ) === 'console'
2655- ) {
2656- UI . getConsoleButton ( ) ?. focus ( ) ;
2657- } else {
2658- UI . getFocusButton ( ) ?. focus ( ) ;
2659- }
2660- lastkeys = 'Esc + Esc' ;
2661- return ;
2662- }
2663- if ( lastkeys === 'Esc + Esc' ) {
2664- e . preventDefault ( ) ;
2665- UI . getLogoLink ( ) ?. focus ( ) ;
2666- lastkeys = 'Esc + Esc + Esc' ;
2667- return ;
2668- }
2669- lastkeys = 'Esc' ;
2670- return ;
2671- }
2672-
2673- // Ctrl + Alt + (1-3) activates editor 1-3
2674- // Ctrl + Alt + (ArrowLeft/ArrowRight) activates previous/next editor
2675- const editorIds = ( [ 'markup' , 'style' , 'script' ] as EditorId [ ] ) . filter (
2676- ( id ) => getConfig ( ) [ id ] . hideTitle !== true ,
2677- ) ;
2678- if ( ctrl ( e ) && e . altKey && [ '1' , '2' , '3' , 'ArrowLeft' , 'ArrowRight' ] . includes ( e . key ) ) {
2679- e . preventDefault ( ) ;
2680- split ?. show ( 'code' ) ;
2681- const index = [ '1' , '2' , '3' ] . includes ( e . key )
2682- ? Number ( e . key ) - 1
2683- : e . key === 'ArrowLeft'
2684- ? editorIds . findIndex ( ( id ) => id === getConfig ( ) . activeEditor ) - 1 || 0
2685- : e . key === 'ArrowRight'
2686- ? editorIds . findIndex ( ( id ) => id === getConfig ( ) . activeEditor ) + 1 || 0
2687- : 0 ;
2688- const editorIndex =
2689- index === editorIds . length ? 0 : index === - 1 ? editorIds . length - 1 : index ;
2690- showEditor ( editorIds [ editorIndex ] as EditorId ) ;
2691- lastkeys = 'Ctrl + Alt + ' + e . key ;
2692- return ;
2693- }
2694-
2695- if ( isEmbed ) return ;
2696-
2697- // Ctrl + Alt + N: new project
2698- if ( ctrl ( e ) && e . altKey && e . code === 'KeyN' ) {
2699- e . preventDefault ( ) ;
2700- UI . getNewLink ( ) ?. click ( ) ;
2701- lastkeys = 'Ctrl + Alt + N' ;
2702- return ;
2703- }
2704-
2705- // Ctrl + O: open project
2706- if ( ctrl ( e ) && e . code === 'KeyO' ) {
2707- e . preventDefault ( ) ;
2708- UI . getOpenLink ( ) ?. click ( ) ;
2709- lastkeys = 'Ctrl + O' ;
2710- return ;
2711- }
2712-
2713- // Ctrl + Alt + I: import
2714- if ( ctrl ( e ) && e . altKey && e . code === 'KeyI' ) {
2715- e . preventDefault ( ) ;
2716- UI . getImportLink ( ) ?. click ( ) ;
2717- lastkeys = 'Ctrl + Alt + I' ;
2718- return ;
2719- }
2720-
2721- // Ctrl + Alt + S: share
2722- if ( ctrl ( e ) && e . altKey && e . code === 'KeyS' ) {
2723- e . preventDefault ( ) ;
2724- UI . getShareLink ( ) ?. click ( ) ;
2725- lastkeys = 'Ctrl + Alt + S' ;
2726- return ;
2727- }
2728-
2729- // Ctrl + Shift + S forks the project (save as...)
2730- if ( ctrl ( e ) && e . shiftKey && e . code === 'KeyS' ) {
2731- e . preventDefault ( ) ;
2732- UI . getForkLink ( ) ?. click ( ) ;
2733- lastkeys = 'Ctrl + Shift + S' ;
2734- return ;
2735- }
2736-
2737- // Ctrl + S saves the project
2738- if ( ctrl ( e ) && e . code === 'KeyS' ) {
2739- e . preventDefault ( ) ;
2740- UI . getSaveLink ( ) ?. click ( ) ;
2741- lastkeys = 'Ctrl + S' ;
2742- return ;
2743- }
2744-
2745- // Ctrl + Alt + F toggles focus mode
2746- if ( ctrl ( e ) && e . altKey && e . code === 'KeyF' ) {
2747- e . preventDefault ( ) ;
2748- UI . getFocusButton ( ) ?. click ( ) ;
2749- lastkeys = 'Ctrl + Alt + F' ;
2750- return ;
2751- }
2752-
2753- if ( ! ctrl ( e ) && ! e . altKey && ! e . shiftKey ) {
2754- lastkeys = e . key ;
2755- return ;
2756- }
2757- } ;
2758-
2759- eventsManager . addEventListener ( window , 'keydown' , hotKeys , true ) ;
2760- } ;
2761-
27622570const handleKeyboardShortcutsScreen = ( ) => {
27632571 if ( isEmbed ) return ;
27642572
@@ -5151,7 +4959,17 @@ const basicHandlers = () => {
51514959 handleSelectEditor ( ) ;
51524960 handleChangeLanguage ( ) ;
51534961 handleChangeContent ( ) ;
5154- handleKeyboardShortcuts ( ) ;
4962+ // Setup keyboard shortcuts with dependency injection
4963+ handleKeyboardShortcuts ( {
4964+ eventsManager,
4965+ getActiveEditor,
4966+ getConfig,
4967+ showEditor,
4968+ run,
4969+ toolsPane,
4970+ split,
4971+ isEmbed,
4972+ } ) ;
51554973 handleRunButton ( ) ;
51564974 handleResultButton ( ) ;
51574975 handleShareButton ( ) ;
0 commit comments