From 119fdd0dff5ab675ac4358e5f0882177e755f7aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Tue, 22 Sep 2020 10:49:38 +0200 Subject: [PATCH] improve command handling --- packages/core/src/Editor.ts | 13 +++++++++++-- packages/core/src/commands/blur.ts | 2 +- packages/core/src/commands/clearContent.ts | 2 +- packages/core/src/commands/deleteSelection.ts | 8 +------- packages/core/src/commands/focus.ts | 2 +- packages/core/src/commands/insertHTML.ts | 2 +- packages/core/src/commands/insertText.ts | 2 +- packages/core/src/commands/liftListItem.ts | 2 +- packages/core/src/commands/removeMark.ts | 2 +- packages/core/src/commands/removeMarks.ts | 2 +- packages/core/src/commands/replaceWithNode.ts | 2 +- packages/core/src/commands/selectAll.ts | 2 +- packages/core/src/commands/selectParentNode.ts | 2 +- packages/core/src/commands/setContent.ts | 2 +- packages/core/src/commands/sinkListItem.ts | 2 +- packages/core/src/commands/splitListItem.ts | 2 +- packages/core/src/commands/toggleMark.ts | 2 +- packages/core/src/commands/toggleNode.ts | 2 +- packages/core/src/commands/updateMark.ts | 2 +- packages/extension-blockquote/index.ts | 2 +- packages/extension-bold/index.ts | 2 +- packages/extension-bullet-list/index.ts | 2 +- packages/extension-code-block/index.ts | 2 +- packages/extension-code/index.ts | 2 +- packages/extension-hard-break/index.ts | 2 +- packages/extension-heading/index.ts | 2 +- packages/extension-history/index.ts | 2 +- packages/extension-horizontal-rule/index.ts | 2 +- packages/extension-italic/index.ts | 2 +- packages/extension-strike/index.ts | 2 +- packages/extension-underline/index.ts | 2 +- 31 files changed, 41 insertions(+), 38 deletions(-) diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 6c1d4131a..23f2dcc8e 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -37,7 +37,13 @@ export interface CommandSpec { export interface Commands {} -// export type CommandNames = Extract +export type CommandNames = Extract + +export type SingleCommands = { + [Command in keyof Commands]: Commands[Command] extends (...args: any[]) => any + ? (...args: Parameters) => boolean + : never +} export type ChainedCommands = { [Command in keyof Commands]: Commands[Command] extends (...args: any[]) => any @@ -62,6 +68,10 @@ interface EditorOptions { editable: boolean, } +declare module './Editor' { + interface Editor extends SingleCommands {} +} + @magicMethods export class Editor extends EventEmitter { @@ -85,7 +95,6 @@ export class Editor extends EventEmitter { editable: true, } - constructor(options: Partial = {}) { super() this.options = { ...this.options, ...options } diff --git a/packages/core/src/commands/blur.ts b/packages/core/src/commands/blur.ts index 3815fb7fa..bf8d28f59 100644 --- a/packages/core/src/commands/blur.ts +++ b/packages/core/src/commands/blur.ts @@ -3,7 +3,7 @@ import { Command } from '../Editor' type BlurCommand = () => Command declare module '../Editor' { - interface Editor { + interface Commands { blur: BlurCommand, } } diff --git a/packages/core/src/commands/clearContent.ts b/packages/core/src/commands/clearContent.ts index 978c8c1d4..beb87c555 100644 --- a/packages/core/src/commands/clearContent.ts +++ b/packages/core/src/commands/clearContent.ts @@ -3,7 +3,7 @@ import { Command } from '../Editor' type ClearContentCommand = (emitUpdate?: Boolean) => Command declare module '../Editor' { - interface Editor { + interface Commands { clearContent: ClearContentCommand, } } diff --git a/packages/core/src/commands/deleteSelection.ts b/packages/core/src/commands/deleteSelection.ts index c2fde9f62..7d74c5df0 100644 --- a/packages/core/src/commands/deleteSelection.ts +++ b/packages/core/src/commands/deleteSelection.ts @@ -4,17 +4,11 @@ import { deleteSelection as originalDeleteSelection } from 'prosemirror-commands type DeleteSelectionCommand = () => Command declare module '../Editor' { - interface Editor { + interface Commands { deleteSelection: DeleteSelectionCommand, } } -// declare module '../Editor' { -// interface Commands { -// deleteSelection: DeleteSelectionCommand, -// } -// } - export const deleteSelection: DeleteSelectionCommand = () => ({ state, dispatch }) => { return originalDeleteSelection(state, dispatch) } diff --git a/packages/core/src/commands/focus.ts b/packages/core/src/commands/focus.ts index 893e0d7b4..6291d2c1b 100644 --- a/packages/core/src/commands/focus.ts +++ b/packages/core/src/commands/focus.ts @@ -5,7 +5,7 @@ import minMax from '../utils/minMax' type FocusCommand = (position?: Position) => Command declare module '../Editor' { - interface Editor { + interface Commands { focus: FocusCommand } } diff --git a/packages/core/src/commands/insertHTML.ts b/packages/core/src/commands/insertHTML.ts index a21df16d8..362602d69 100644 --- a/packages/core/src/commands/insertHTML.ts +++ b/packages/core/src/commands/insertHTML.ts @@ -7,7 +7,7 @@ import {ReplaceStep, ReplaceAroundStep} from "prosemirror-transform" type InsertHTMLCommand = (value: string) => Command declare module '../Editor' { - interface Editor { + interface Commands { insertHTML: InsertHTMLCommand, } } diff --git a/packages/core/src/commands/insertText.ts b/packages/core/src/commands/insertText.ts index e53d3901b..e48609e9c 100644 --- a/packages/core/src/commands/insertText.ts +++ b/packages/core/src/commands/insertText.ts @@ -3,7 +3,7 @@ import { Command } from '../Editor' type InsertTextCommand = (value: string) => Command declare module '../Editor' { - interface Editor { + interface Commands { insertText: InsertTextCommand, } } diff --git a/packages/core/src/commands/liftListItem.ts b/packages/core/src/commands/liftListItem.ts index da8cdf806..0508e2aea 100644 --- a/packages/core/src/commands/liftListItem.ts +++ b/packages/core/src/commands/liftListItem.ts @@ -6,7 +6,7 @@ import getNodeType from '../utils/getNodeType' type LiftListItem = (typeOrName: string | NodeType) => Command declare module '../Editor' { - interface Editor { + interface Commands { liftListItem: LiftListItem, } } diff --git a/packages/core/src/commands/removeMark.ts b/packages/core/src/commands/removeMark.ts index 42207aff7..71456afb4 100644 --- a/packages/core/src/commands/removeMark.ts +++ b/packages/core/src/commands/removeMark.ts @@ -6,7 +6,7 @@ import getMarkRange from '../utils/getMarkRange' type RemoveMarkCommand = (typeOrName: string | MarkType) => Command declare module '../Editor' { - interface Editor { + interface Commands { toggleMark: RemoveMarkCommand, } } diff --git a/packages/core/src/commands/removeMarks.ts b/packages/core/src/commands/removeMarks.ts index 8b4d28476..f71400d27 100644 --- a/packages/core/src/commands/removeMarks.ts +++ b/packages/core/src/commands/removeMarks.ts @@ -3,7 +3,7 @@ import { Command } from '../Editor' type RemoveMarksCommand = () => Command declare module '../Editor' { - interface Editor { + interface Commands { removeMarks: RemoveMarksCommand, } } diff --git a/packages/core/src/commands/replaceWithNode.ts b/packages/core/src/commands/replaceWithNode.ts index e1d32e2ff..233897d6c 100644 --- a/packages/core/src/commands/replaceWithNode.ts +++ b/packages/core/src/commands/replaceWithNode.ts @@ -14,7 +14,7 @@ type ReplaceWithNodeCommand = ( ) => Command declare module '../Editor' { - interface Editor { + interface Commands { replaceText: ReplaceWithNodeCommand, } } diff --git a/packages/core/src/commands/selectAll.ts b/packages/core/src/commands/selectAll.ts index fcb3d3ea4..293bc8c63 100644 --- a/packages/core/src/commands/selectAll.ts +++ b/packages/core/src/commands/selectAll.ts @@ -4,7 +4,7 @@ import { selectAll as originalSelectAll } from 'prosemirror-commands' type SelectAllCommand = () => Command declare module '../Editor' { - interface Editor { + interface Commands { selectAll: SelectAllCommand, } } diff --git a/packages/core/src/commands/selectParentNode.ts b/packages/core/src/commands/selectParentNode.ts index dbc29f92a..32ccbc05d 100644 --- a/packages/core/src/commands/selectParentNode.ts +++ b/packages/core/src/commands/selectParentNode.ts @@ -4,7 +4,7 @@ import { selectParentNode as originalSelectParentNode } from 'prosemirror-comman type SelectParentNodeCommand = () => Command declare module '../Editor' { - interface Editor { + interface Commands { selectParentNode: SelectParentNodeCommand, } } diff --git a/packages/core/src/commands/setContent.ts b/packages/core/src/commands/setContent.ts index 50a7641c2..8daa605df 100644 --- a/packages/core/src/commands/setContent.ts +++ b/packages/core/src/commands/setContent.ts @@ -8,7 +8,7 @@ type SetContentCommand = ( ) => Command declare module '../Editor' { - interface Editor { + interface Commands { setContent: SetContentCommand, } } diff --git a/packages/core/src/commands/sinkListItem.ts b/packages/core/src/commands/sinkListItem.ts index e350cdba7..fc1743fba 100644 --- a/packages/core/src/commands/sinkListItem.ts +++ b/packages/core/src/commands/sinkListItem.ts @@ -6,7 +6,7 @@ import getNodeType from '../utils/getNodeType' type SinkListItem = (typeOrName: string | NodeType) => Command declare module '../Editor' { - interface Editor { + interface Commands { sinkListItem: SinkListItem, } } diff --git a/packages/core/src/commands/splitListItem.ts b/packages/core/src/commands/splitListItem.ts index 9411eab77..75dc79f44 100644 --- a/packages/core/src/commands/splitListItem.ts +++ b/packages/core/src/commands/splitListItem.ts @@ -6,7 +6,7 @@ import getNodeType from '../utils/getNodeType' type SplitListItem = (typeOrName: string | NodeType) => Command declare module '../Editor' { - interface Editor { + interface Commands { splitListItem: SplitListItem, } } diff --git a/packages/core/src/commands/toggleMark.ts b/packages/core/src/commands/toggleMark.ts index 5b2fe95a3..2dad49370 100644 --- a/packages/core/src/commands/toggleMark.ts +++ b/packages/core/src/commands/toggleMark.ts @@ -6,7 +6,7 @@ import getMarkType from '../utils/getMarkType' type ToggleMarkCommand = (typeOrName: string | MarkType) => Command declare module '../Editor' { - interface Editor { + interface Commands { toggleMark: ToggleMarkCommand, } } diff --git a/packages/core/src/commands/toggleNode.ts b/packages/core/src/commands/toggleNode.ts index 09d9ff3a6..95642645f 100644 --- a/packages/core/src/commands/toggleNode.ts +++ b/packages/core/src/commands/toggleNode.ts @@ -11,7 +11,7 @@ type ToggleNodeCommand = ( ) => Command declare module '../Editor' { - interface Editor { + interface Commands { toggleNode: ToggleNodeCommand, } } diff --git a/packages/core/src/commands/updateMark.ts b/packages/core/src/commands/updateMark.ts index 354dbdcf9..c66a6449e 100644 --- a/packages/core/src/commands/updateMark.ts +++ b/packages/core/src/commands/updateMark.ts @@ -9,7 +9,7 @@ type UpdateMarkCommand = ( ) => Command declare module '../Editor' { - interface Editor { + interface Commands { updateMark: UpdateMarkCommand, } } diff --git a/packages/extension-blockquote/index.ts b/packages/extension-blockquote/index.ts index 565c11531..5c2fac4cf 100644 --- a/packages/extension-blockquote/index.ts +++ b/packages/extension-blockquote/index.ts @@ -4,7 +4,7 @@ import { textblockTypeInputRule } from 'prosemirror-inputrules' export type BlockquoteCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { blockquote: BlockquoteCommand, } } diff --git a/packages/extension-bold/index.ts b/packages/extension-bold/index.ts index 400d1e886..2ac2a9e59 100644 --- a/packages/extension-bold/index.ts +++ b/packages/extension-bold/index.ts @@ -3,7 +3,7 @@ import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core' export type BoldCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { bold: BoldCommand, } } diff --git a/packages/extension-bullet-list/index.ts b/packages/extension-bullet-list/index.ts index c2e2e87a3..2ed6b84f4 100644 --- a/packages/extension-bullet-list/index.ts +++ b/packages/extension-bullet-list/index.ts @@ -4,7 +4,7 @@ import { wrappingInputRule } from 'prosemirror-inputrules' export type BulletListCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { bulletList: BulletListCommand, } } diff --git a/packages/extension-code-block/index.ts b/packages/extension-code-block/index.ts index 77612d77a..91aa93090 100644 --- a/packages/extension-code-block/index.ts +++ b/packages/extension-code-block/index.ts @@ -4,7 +4,7 @@ import { textblockTypeInputRule } from 'prosemirror-inputrules' export type CodeBlockCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { codeBlock: CodeBlockCommand, } } diff --git a/packages/extension-code/index.ts b/packages/extension-code/index.ts index c7f624e51..7731cb500 100644 --- a/packages/extension-code/index.ts +++ b/packages/extension-code/index.ts @@ -3,7 +3,7 @@ import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core' export type CodeCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { code: CodeCommand, } } diff --git a/packages/extension-hard-break/index.ts b/packages/extension-hard-break/index.ts index 77bc1fc03..1baaa68ab 100644 --- a/packages/extension-hard-break/index.ts +++ b/packages/extension-hard-break/index.ts @@ -4,7 +4,7 @@ import { chainCommands, exitCode } from 'prosemirror-commands' export type HardBreakCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { hardBreak: HardBreakCommand, } } diff --git a/packages/extension-heading/index.ts b/packages/extension-heading/index.ts index d38d69c84..82749be93 100644 --- a/packages/extension-heading/index.ts +++ b/packages/extension-heading/index.ts @@ -10,7 +10,7 @@ export interface HeadingOptions { export type HeadingCommand = (level: Level) => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { heading: HeadingCommand, } } diff --git a/packages/extension-history/index.ts b/packages/extension-history/index.ts index 19b76388e..0f389dbe5 100644 --- a/packages/extension-history/index.ts +++ b/packages/extension-history/index.ts @@ -8,7 +8,7 @@ import { } from 'prosemirror-history' declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { undo: () => Command, redo: () => Command, } diff --git a/packages/extension-horizontal-rule/index.ts b/packages/extension-horizontal-rule/index.ts index 7a4be7877..9398a6d77 100644 --- a/packages/extension-horizontal-rule/index.ts +++ b/packages/extension-horizontal-rule/index.ts @@ -3,7 +3,7 @@ import { Command, Node, nodeInputRule } from '@tiptap/core' export type HorizontalRuleCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { horizontalRule: HorizontalRuleCommand, } } diff --git a/packages/extension-italic/index.ts b/packages/extension-italic/index.ts index 59ea8a956..3bf2e1f3e 100644 --- a/packages/extension-italic/index.ts +++ b/packages/extension-italic/index.ts @@ -3,7 +3,7 @@ import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core' export type ItalicCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { italic: ItalicCommand, } } diff --git a/packages/extension-strike/index.ts b/packages/extension-strike/index.ts index 815fe5474..e84705641 100644 --- a/packages/extension-strike/index.ts +++ b/packages/extension-strike/index.ts @@ -3,7 +3,7 @@ import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core' type StrikeCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { strike: StrikeCommand, } } diff --git a/packages/extension-underline/index.ts b/packages/extension-underline/index.ts index 465caae59..0ba4541ef 100644 --- a/packages/extension-underline/index.ts +++ b/packages/extension-underline/index.ts @@ -3,7 +3,7 @@ import { Command, Mark } from '@tiptap/core' export type UnderlineCommand = () => Command declare module '@tiptap/core/src/Editor' { - interface Editor { + interface Commands { underline: UnderlineCommand, } }