Skip to content

Commit

Permalink
feat: add editor prop to KeyboardShortcutCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Apr 20, 2021
1 parent d14942e commit e0c2460
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
10 changes: 7 additions & 3 deletions packages/core/src/Extension.ts
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down Expand Up @@ -52,7 +56,7 @@ declare module '@tiptap/core' {
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addKeyboardShortcuts'],
}) => {
[key: string]: ProseMirrorCommand,
[key: string]: KeyboardShortcutCommand,
},

/**
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/ExtensionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ 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 {
Attributes,
RawCommands,
GlobalAttributes,
ParentConfig,
KeyboardShortcutCommand,
} from './types'
import { Node } from './Node'
import { MarkConfig } from '.'
Expand Down Expand Up @@ -64,7 +64,7 @@ declare module '@tiptap/core' {
type: MarkType,
parent: ParentConfig<MarkConfig<Options>>['addKeyboardShortcuts'],
}) => {
[key: string]: ProseMirrorCommand,
[key: string]: KeyboardShortcutCommand,
},

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -14,6 +13,7 @@ import {
GlobalAttributes,
RawCommands,
ParentConfig,
KeyboardShortcutCommand,
} from './types'
import { NodeConfig } from '.'
import { Editor } from './Editor'
Expand Down Expand Up @@ -64,7 +64,7 @@ declare module '@tiptap/core' {
type: NodeType,
parent: ParentConfig<NodeConfig<Options>>['addKeyboardShortcuts'],
}) => {
[key: string]: ProseMirrorCommand,
[key: string]: KeyboardShortcutCommand,
},

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e0c2460

Please sign in to comment.