Skip to content

Commit be6529d

Browse files
committed
New WYSIWYG: Added mac shortcut support
1 parent b1a3ea1 commit be6529d

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

resources/js/wysiwyg/services/shortcuts.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,43 @@ function toggleInlineCode(editor: LexicalEditor): boolean {
3232

3333
type ShortcutAction = (editor: LexicalEditor, context: EditorUiContext) => boolean;
3434

35+
/**
36+
* List of action functions by their shortcut combo.
37+
* We use "meta" as an abstraction for ctrl/cmd depending on platform.
38+
*/
3539
const actionsByKeys: Record<string, ShortcutAction> = {
36-
'ctrl+s': () => {
40+
'meta+s': () => {
3741
window.$events.emit('editor-save-draft');
3842
return true;
3943
},
40-
'ctrl+enter': () => {
44+
'meta+enter': () => {
4145
window.$events.emit('editor-save-page');
4246
return true;
4347
},
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),
5761

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) => {
6165
editor.getEditorState().read(() => {
6266
const selectedLink = $getNodeFromSelection($getSelection(), $isLinkNode) as LinkNode | null;
6367
$showLinkForm(selectedLink, context);
6468
});
6569
return true;
6670
},
67-
'ctrl+shift+k': (editor, context) => {
71+
'meta+shift+k': (editor, context) => {
6872
showLinkSelector(entity => {
6973
insertOrUpdateLink(editor, {
7074
text: entity.name,
@@ -79,8 +83,7 @@ const actionsByKeys: Record<string, ShortcutAction> = {
7983

8084
function createKeyDownListener(context: EditorUiContext): (e: KeyboardEvent) => void {
8185
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);
8487
// console.log(`pressed: ${combo}`);
8588
if (actionsByKeys[combo]) {
8689
const handled = actionsByKeys[combo](context.editor, context);
@@ -92,10 +95,29 @@ function createKeyDownListener(context: EditorUiContext): (e: KeyboardEvent) =>
9295
};
9396
}
9497

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+
95114
function overrideDefaultCommands(editor: LexicalEditor) {
96115
// Prevent default ctrl+enter command
97116
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;
99121
}, COMMAND_PRIORITY_HIGH);
100122
}
101123

resources/js/wysiwyg/todo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## Main Todo
88

9-
- Mac: Shortcut support via command.
9+
//
1010

1111
## Secondary Todo
1212

0 commit comments

Comments
 (0)