From e0c2460a960c739ac4b543f0f185653cb41599e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Tue, 20 Apr 2021 22:58:09 +0200 Subject: [PATCH] feat: add editor prop to KeyboardShortcutCommand --- packages/core/src/Extension.ts | 10 +++++++--- packages/core/src/ExtensionManager.ts | 10 +++++++++- packages/core/src/Mark.ts | 4 ++-- packages/core/src/Node.ts | 4 ++-- packages/core/src/types.ts | 2 ++ 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/core/src/Extension.ts b/packages/core/src/Extension.ts index dbfe75be..b9592bda 100644 --- a/packages/core/src/Extension.ts +++ b/packages/core/src/Extension.ts @@ -1,11 +1,15 @@ import { Plugin, Transaction } from 'prosemirror-state' -import { Command as ProseMirrorCommand } from 'prosemirror-commands' import { InputRule } from 'prosemirror-inputrules' import { Editor } from './Editor' import { Node } from './Node' import { Mark } from './Mark' import mergeDeep from './utilities/mergeDeep' -import { GlobalAttributes, RawCommands, ParentConfig } from './types' +import { + GlobalAttributes, + RawCommands, + ParentConfig, + KeyboardShortcutCommand, +} from './types' import { ExtensionConfig } from '.' declare module '@tiptap/core' { @@ -52,7 +56,7 @@ declare module '@tiptap/core' { editor: Editor, parent: ParentConfig>['addKeyboardShortcuts'], }) => { - [key: string]: ProseMirrorCommand, + [key: string]: KeyboardShortcutCommand, }, /** diff --git a/packages/core/src/ExtensionManager.ts b/packages/core/src/ExtensionManager.ts index 4d129bf9..b17879ac 100644 --- a/packages/core/src/ExtensionManager.ts +++ b/packages/core/src/ExtensionManager.ts @@ -190,7 +190,15 @@ export default class ExtensionManager { ) if (addKeyboardShortcuts) { - const keyMapPlugin = keymap(addKeyboardShortcuts()) + const bindings = Object.fromEntries( + Object + .entries(addKeyboardShortcuts()) + .map(([shortcut, method]) => { + return [shortcut, () => method({ editor: this.editor })] + }), + ) + + const keyMapPlugin = keymap(bindings) plugins.push(keyMapPlugin) } diff --git a/packages/core/src/Mark.ts b/packages/core/src/Mark.ts index f4fa43fc..c51165df 100644 --- a/packages/core/src/Mark.ts +++ b/packages/core/src/Mark.ts @@ -5,7 +5,6 @@ import { MarkType, } from 'prosemirror-model' import { Plugin, Transaction } from 'prosemirror-state' -import { Command as ProseMirrorCommand } from 'prosemirror-commands' import { InputRule } from 'prosemirror-inputrules' import mergeDeep from './utilities/mergeDeep' import { @@ -13,6 +12,7 @@ import { RawCommands, GlobalAttributes, ParentConfig, + KeyboardShortcutCommand, } from './types' import { Node } from './Node' import { MarkConfig } from '.' @@ -64,7 +64,7 @@ declare module '@tiptap/core' { type: MarkType, parent: ParentConfig>['addKeyboardShortcuts'], }) => { - [key: string]: ProseMirrorCommand, + [key: string]: KeyboardShortcutCommand, }, /** diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index 6a84954e..059460f8 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -4,7 +4,6 @@ import { Node as ProseMirrorNode, NodeType, } from 'prosemirror-model' -import { Command as ProseMirrorCommand } from 'prosemirror-commands' import { Plugin, Transaction } from 'prosemirror-state' import { InputRule } from 'prosemirror-inputrules' import mergeDeep from './utilities/mergeDeep' @@ -14,6 +13,7 @@ import { GlobalAttributes, RawCommands, ParentConfig, + KeyboardShortcutCommand, } from './types' import { NodeConfig } from '.' import { Editor } from './Editor' @@ -64,7 +64,7 @@ declare module '@tiptap/core' { type: NodeType, parent: ParentConfig>['addKeyboardShortcuts'], }) => { - [key: string]: ProseMirrorCommand, + [key: string]: KeyboardShortcutCommand, }, /** diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 074f38d2..aca4c137 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -77,6 +77,8 @@ export type Command = (props: CommandProps) => boolean export type CommandSpec = (...args: any[]) => Command +export type KeyboardShortcutCommand = (props: { editor: Editor }) => boolean + export type Attribute = { default: any, rendered?: boolean,