@@ -38,29 +38,9 @@ type ShortcutAction = (editor: LexicalEditor, context: EditorUiContext) => boole
3838 * List of action functions by their shortcut combo.
3939 * We use "meta" as an abstraction for ctrl/cmd depending on platform.
4040 */
41- const actionsByKeys : Record < string , ShortcutAction > = {
42- 'meta+s' : ( ) => {
43- window . $events . emit ( 'editor-save-draft' ) ;
44- return true ;
45- } ,
46- 'meta+enter' : ( ) => {
47- window . $events . emit ( 'editor-save-page' ) ;
48- return true ;
49- } ,
50- 'meta+1' : ( editor , context ) => headerHandler ( context , 'h2' ) ,
51- 'meta+2' : ( editor , context ) => headerHandler ( context , 'h3' ) ,
52- 'meta+3' : ( editor , context ) => headerHandler ( context , 'h4' ) ,
53- 'meta+4' : ( editor , context ) => headerHandler ( context , 'h5' ) ,
54- 'meta+5' : wrapFormatAction ( toggleSelectionAsParagraph ) ,
55- 'meta+d' : wrapFormatAction ( toggleSelectionAsParagraph ) ,
56- 'meta+6' : wrapFormatAction ( toggleSelectionAsBlockquote ) ,
57- 'meta+q' : wrapFormatAction ( toggleSelectionAsBlockquote ) ,
58- 'meta+7' : wrapFormatAction ( formatCodeBlock ) ,
59- 'meta+e' : wrapFormatAction ( formatCodeBlock ) ,
41+ const baseActionsByKeys : Record < string , ShortcutAction > = {
6042 'meta+8' : toggleInlineCode ,
6143 'meta+shift+e' : toggleInlineCode ,
62- 'meta+9' : wrapFormatAction ( cycleSelectionCalloutFormats ) ,
63-
6444 'meta+o' : wrapFormatAction ( ( e ) => toggleSelectionAsList ( e , 'number' ) ) ,
6545 'meta+p' : wrapFormatAction ( ( e ) => toggleSelectionAsList ( e , 'bullet' ) ) ,
6646 'meta+k' : ( editor , context ) => {
@@ -87,12 +67,39 @@ const actionsByKeys: Record<string, ShortcutAction> = {
8767 } ,
8868} ;
8969
90- function createKeyDownListener ( context : EditorUiContext ) : ( e : KeyboardEvent ) => void {
70+ /**
71+ * An extended set of the above, used for fuller-featured editors with heavier block-level formatting.
72+ */
73+ const extendedActionsByKeys : Record < string , ShortcutAction > = {
74+ ...baseActionsByKeys ,
75+ 'meta+s' : ( ) => {
76+ window . $events . emit ( 'editor-save-draft' ) ;
77+ return true ;
78+ } ,
79+ 'meta+enter' : ( ) => {
80+ window . $events . emit ( 'editor-save-page' ) ;
81+ return true ;
82+ } ,
83+ 'meta+1' : ( editor , context ) => headerHandler ( context , 'h2' ) ,
84+ 'meta+2' : ( editor , context ) => headerHandler ( context , 'h3' ) ,
85+ 'meta+3' : ( editor , context ) => headerHandler ( context , 'h4' ) ,
86+ 'meta+4' : ( editor , context ) => headerHandler ( context , 'h5' ) ,
87+ 'meta+5' : wrapFormatAction ( toggleSelectionAsParagraph ) ,
88+ 'meta+d' : wrapFormatAction ( toggleSelectionAsParagraph ) ,
89+ 'meta+6' : wrapFormatAction ( toggleSelectionAsBlockquote ) ,
90+ 'meta+7' : wrapFormatAction ( formatCodeBlock ) ,
91+ 'meta+e' : wrapFormatAction ( formatCodeBlock ) ,
92+ 'meta+q' : wrapFormatAction ( toggleSelectionAsBlockquote ) ,
93+ 'meta+9' : wrapFormatAction ( cycleSelectionCalloutFormats ) ,
94+ } ;
95+
96+ function createKeyDownListener ( context : EditorUiContext , useExtended : boolean ) : ( e : KeyboardEvent ) => void {
97+ const keySetToUse = useExtended ? extendedActionsByKeys : baseActionsByKeys ;
9198 return ( event : KeyboardEvent ) => {
9299 const combo = keyboardEventToKeyComboString ( event ) ;
93100 // console.log(`pressed: ${combo}`);
94- if ( actionsByKeys [ combo ] ) {
95- const handled = actionsByKeys [ combo ] ( context . editor , context ) ;
101+ if ( keySetToUse [ combo ] ) {
102+ const handled = keySetToUse [ combo ] ( context . editor , context ) ;
96103 if ( handled ) {
97104 event . stopPropagation ( ) ;
98105 event . preventDefault ( ) ;
@@ -127,8 +134,8 @@ function overrideDefaultCommands(editor: LexicalEditor) {
127134 } , COMMAND_PRIORITY_HIGH ) ;
128135}
129136
130- export function registerShortcuts ( context : EditorUiContext ) {
131- const listener = createKeyDownListener ( context ) ;
137+ export function registerShortcuts ( context : EditorUiContext , useExtended : boolean ) {
138+ const listener = createKeyDownListener ( context , useExtended ) ;
132139 overrideDefaultCommands ( context . editor ) ;
133140
134141 return context . editor . registerRootListener ( ( rootElement : null | HTMLElement , prevRootElement : null | HTMLElement ) => {
0 commit comments