diff --git a/packages/core/src/commands/keyboardShortcut.ts b/packages/core/src/commands/keyboardShortcut.ts index b5c7d71a..915224df 100644 --- a/packages/core/src/commands/keyboardShortcut.ts +++ b/packages/core/src/commands/keyboardShortcut.ts @@ -1,6 +1,6 @@ import { RawCommands } from '../types' - -const mac = typeof navigator !== 'undefined' ? /Mac/.test(navigator.platform) : false +import { isiOS } from '../utilities/isiOS' +import { isMacOS } from '../utilities/isMacOS' function normalizeKeyName(name: string) { const parts = name.split(/-(?!$)/) @@ -27,7 +27,7 @@ function normalizeKeyName(name: string) { } else if (/^s(hift)?$/i.test(mod)) { shift = true } else if (/^mod$/i.test(mod)) { - if (mac) { + if (isiOS() || isMacOS()) { meta = true } else { ctrl = true diff --git a/packages/core/src/extensions/keymap.ts b/packages/core/src/extensions/keymap.ts index 36808748..3514f3bf 100644 --- a/packages/core/src/extensions/keymap.ts +++ b/packages/core/src/extensions/keymap.ts @@ -1,5 +1,7 @@ import { Plugin, PluginKey, Selection } from 'prosemirror-state' import { createChainableState } from '../helpers/createChainableState' +import { isiOS } from '../utilities/isiOS' +import { isMacOS } from '../utilities/isMacOS' import { CommandManager } from '../CommandManager' import { Extension } from '../Extension' @@ -38,13 +40,15 @@ export const Keymap = Extension.create({ () => commands.selectNodeForward(), ]) - return { - Enter: () => this.editor.commands.first(({ commands }) => [ - () => commands.newlineInCode(), - () => commands.createParagraphNear(), - () => commands.liftEmptyBlock(), - () => commands.splitBlock(), - ]), + const handleEnter = () => this.editor.commands.first(({ commands }) => [ + () => commands.newlineInCode(), + () => commands.createParagraphNear(), + () => commands.liftEmptyBlock(), + () => commands.splitBlock(), + ]) + + const baseKeymap = { + Enter: handleEnter, 'Mod-Enter': () => this.editor.commands.exitCode(), Backspace: handleBackspace, 'Mod-Backspace': handleBackspace, @@ -52,11 +56,31 @@ export const Keymap = Extension.create({ Delete: handleDelete, 'Mod-Delete': handleDelete, 'Mod-a': () => this.editor.commands.selectAll(), - 'Ctrl-a': () => this.editor.commands.selectTextblockStart(), - 'Ctrl-e': () => this.editor.commands.selectTextblockEnd(), + } + + const pcKeymap = { + ...baseKeymap, Home: () => this.editor.commands.selectTextblockStart(), End: () => this.editor.commands.selectTextblockStart(), } + + const macKeymap = { + ...baseKeymap, + 'Ctrl-h': handleBackspace, + 'Alt-Backspace': handleBackspace, + 'Ctrl-d': handleDelete, + 'Ctrl-Alt-Backspace': handleDelete, + 'Alt-Delete': handleDelete, + 'Alt-d': handleDelete, + 'Ctrl-a': () => this.editor.commands.selectTextblockStart(), + 'Ctrl-e': () => this.editor.commands.selectTextblockEnd(), + } + + if (isiOS() || isMacOS()) { + return macKeymap + } + + return pcKeymap }, addProseMirrorPlugins() { diff --git a/packages/core/src/utilities/isMacOS.ts b/packages/core/src/utilities/isMacOS.ts new file mode 100644 index 00000000..c5274637 --- /dev/null +++ b/packages/core/src/utilities/isMacOS.ts @@ -0,0 +1,5 @@ +export function isMacOS(): boolean { + return typeof navigator !== 'undefined' + ? /Mac/.test(navigator.platform) + : false +}