@@ -32,39 +32,43 @@ function toggleInlineCode(editor: LexicalEditor): boolean {
32
32
33
33
type ShortcutAction = ( editor : LexicalEditor , context : EditorUiContext ) => boolean ;
34
34
35
+ /**
36
+ * List of action functions by their shortcut combo.
37
+ * We use "meta" as an abstraction for ctrl/cmd depending on platform.
38
+ */
35
39
const actionsByKeys : Record < string , ShortcutAction > = {
36
- 'ctrl +s' : ( ) => {
40
+ 'meta +s' : ( ) => {
37
41
window . $events . emit ( 'editor-save-draft' ) ;
38
42
return true ;
39
43
} ,
40
- 'ctrl +enter' : ( ) => {
44
+ 'meta +enter' : ( ) => {
41
45
window . $events . emit ( 'editor-save-page' ) ;
42
46
return true ;
43
47
} ,
44
- 'ctrl +1' : ( editor ) => headerHandler ( editor , 'h1' ) ,
45
- 'ctrl +2' : ( editor ) => headerHandler ( editor , 'h2' ) ,
46
- 'ctrl +3' : ( editor ) => headerHandler ( editor , 'h3' ) ,
47
- 'ctrl +4' : ( editor ) => headerHandler ( editor , 'h4' ) ,
48
- 'ctrl +5' : wrapFormatAction ( toggleSelectionAsParagraph ) ,
49
- 'ctrl +d' : wrapFormatAction ( toggleSelectionAsParagraph ) ,
50
- 'ctrl +6' : wrapFormatAction ( toggleSelectionAsBlockquote ) ,
51
- 'ctrl +q' : wrapFormatAction ( toggleSelectionAsBlockquote ) ,
52
- 'ctrl +7' : wrapFormatAction ( formatCodeBlock ) ,
53
- 'ctrl +e' : wrapFormatAction ( formatCodeBlock ) ,
54
- 'ctrl +8' : toggleInlineCode ,
55
- 'ctrl +shift+e' : toggleInlineCode ,
56
- 'ctrl +9' : wrapFormatAction ( cycleSelectionCalloutFormats ) ,
48
+ 'meta +1' : ( editor ) => headerHandler ( editor , 'h1' ) ,
49
+ 'meta +2' : ( editor ) => headerHandler ( editor , 'h2' ) ,
50
+ 'meta +3' : ( editor ) => headerHandler ( editor , 'h3' ) ,
51
+ 'meta +4' : ( editor ) => headerHandler ( editor , 'h4' ) ,
52
+ 'meta +5' : wrapFormatAction ( toggleSelectionAsParagraph ) ,
53
+ 'meta +d' : wrapFormatAction ( toggleSelectionAsParagraph ) ,
54
+ 'meta +6' : wrapFormatAction ( toggleSelectionAsBlockquote ) ,
55
+ 'meta +q' : wrapFormatAction ( toggleSelectionAsBlockquote ) ,
56
+ 'meta +7' : wrapFormatAction ( formatCodeBlock ) ,
57
+ 'meta +e' : wrapFormatAction ( formatCodeBlock ) ,
58
+ 'meta +8' : toggleInlineCode ,
59
+ 'meta +shift+e' : toggleInlineCode ,
60
+ 'meta +9' : wrapFormatAction ( cycleSelectionCalloutFormats ) ,
57
61
58
- 'ctrl +o' : wrapFormatAction ( ( e ) => toggleSelectionAsList ( e , 'number' ) ) ,
59
- 'ctrl +p' : wrapFormatAction ( ( e ) => toggleSelectionAsList ( e , 'bullet' ) ) ,
60
- 'ctrl +k' : ( editor , context ) => {
62
+ 'meta +o' : wrapFormatAction ( ( e ) => toggleSelectionAsList ( e , 'number' ) ) ,
63
+ 'meta +p' : wrapFormatAction ( ( e ) => toggleSelectionAsList ( e , 'bullet' ) ) ,
64
+ 'meta +k' : ( editor , context ) => {
61
65
editor . getEditorState ( ) . read ( ( ) => {
62
66
const selectedLink = $getNodeFromSelection ( $getSelection ( ) , $isLinkNode ) as LinkNode | null ;
63
67
$showLinkForm ( selectedLink , context ) ;
64
68
} ) ;
65
69
return true ;
66
70
} ,
67
- 'ctrl +shift+k' : ( editor , context ) => {
71
+ 'meta +shift+k' : ( editor , context ) => {
68
72
showLinkSelector ( entity => {
69
73
insertOrUpdateLink ( editor , {
70
74
text : entity . name ,
@@ -79,8 +83,7 @@ const actionsByKeys: Record<string, ShortcutAction> = {
79
83
80
84
function createKeyDownListener ( context : EditorUiContext ) : ( e : KeyboardEvent ) => void {
81
85
return ( event : KeyboardEvent ) => {
82
- // TODO - Mac Cmd support
83
- const combo = `${ event . ctrlKey ? 'ctrl+' : '' } ${ event . shiftKey ? 'shift+' : '' } ${ event . key } ` . toLowerCase ( ) ;
86
+ const combo = keyboardEventToKeyComboString ( event ) ;
84
87
// console.log(`pressed: ${combo}`);
85
88
if ( actionsByKeys [ combo ] ) {
86
89
const handled = actionsByKeys [ combo ] ( context . editor , context ) ;
@@ -92,10 +95,29 @@ function createKeyDownListener(context: EditorUiContext): (e: KeyboardEvent) =>
92
95
} ;
93
96
}
94
97
98
+ function keyboardEventToKeyComboString ( event : KeyboardEvent ) : string {
99
+ const metaKeyPressed = isMac ( ) ? event . metaKey : event . ctrlKey ;
100
+
101
+ const parts = [
102
+ metaKeyPressed ? 'meta' : '' ,
103
+ event . shiftKey ? 'shift' : '' ,
104
+ event . key ,
105
+ ] ;
106
+
107
+ return parts . filter ( Boolean ) . join ( '+' ) . toLowerCase ( ) ;
108
+ }
109
+
110
+ function isMac ( ) : boolean {
111
+ return window . navigator . userAgent . includes ( 'Mac OS X' ) ;
112
+ }
113
+
95
114
function overrideDefaultCommands ( editor : LexicalEditor ) {
96
115
// Prevent default ctrl+enter command
97
116
editor . registerCommand ( KEY_ENTER_COMMAND , ( event ) => {
98
- return event ?. ctrlKey ? true : false
117
+ if ( isMac ( ) ) {
118
+ return event ?. metaKey || false ;
119
+ }
120
+ return event ?. ctrlKey || false ;
99
121
} , COMMAND_PRIORITY_HIGH ) ;
100
122
}
101
123
0 commit comments