improve command handling

This commit is contained in:
Philipp Kühn
2020-09-22 10:49:38 +02:00
parent 0aa5a4c474
commit 119fdd0dff
31 changed files with 41 additions and 38 deletions

View File

@@ -37,7 +37,13 @@ export interface CommandSpec {
export interface Commands {} export interface Commands {}
// export type CommandNames = Extract<keyof Commands, string> export type CommandNames = Extract<keyof Commands, string>
export type SingleCommands = {
[Command in keyof Commands]: Commands[Command] extends (...args: any[]) => any
? (...args: Parameters<Commands[Command]>) => boolean
: never
}
export type ChainedCommands = { export type ChainedCommands = {
[Command in keyof Commands]: Commands[Command] extends (...args: any[]) => any [Command in keyof Commands]: Commands[Command] extends (...args: any[]) => any
@@ -62,6 +68,10 @@ interface EditorOptions {
editable: boolean, editable: boolean,
} }
declare module './Editor' {
interface Editor extends SingleCommands {}
}
@magicMethods @magicMethods
export class Editor extends EventEmitter { export class Editor extends EventEmitter {
@@ -85,7 +95,6 @@ export class Editor extends EventEmitter {
editable: true, editable: true,
} }
constructor(options: Partial<EditorOptions> = {}) { constructor(options: Partial<EditorOptions> = {}) {
super() super()
this.options = { ...this.options, ...options } this.options = { ...this.options, ...options }

View File

@@ -3,7 +3,7 @@ import { Command } from '../Editor'
type BlurCommand = () => Command type BlurCommand = () => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
blur: BlurCommand, blur: BlurCommand,
} }
} }

View File

@@ -3,7 +3,7 @@ import { Command } from '../Editor'
type ClearContentCommand = (emitUpdate?: Boolean) => Command type ClearContentCommand = (emitUpdate?: Boolean) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
clearContent: ClearContentCommand, clearContent: ClearContentCommand,
} }
} }

View File

@@ -4,17 +4,11 @@ import { deleteSelection as originalDeleteSelection } from 'prosemirror-commands
type DeleteSelectionCommand = () => Command type DeleteSelectionCommand = () => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
deleteSelection: DeleteSelectionCommand, deleteSelection: DeleteSelectionCommand,
} }
} }
// declare module '../Editor' {
// interface Commands {
// deleteSelection: DeleteSelectionCommand,
// }
// }
export const deleteSelection: DeleteSelectionCommand = () => ({ state, dispatch }) => { export const deleteSelection: DeleteSelectionCommand = () => ({ state, dispatch }) => {
return originalDeleteSelection(state, dispatch) return originalDeleteSelection(state, dispatch)
} }

View File

@@ -5,7 +5,7 @@ import minMax from '../utils/minMax'
type FocusCommand = (position?: Position) => Command type FocusCommand = (position?: Position) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
focus: FocusCommand focus: FocusCommand
} }
} }

View File

@@ -7,7 +7,7 @@ import {ReplaceStep, ReplaceAroundStep} from "prosemirror-transform"
type InsertHTMLCommand = (value: string) => Command type InsertHTMLCommand = (value: string) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
insertHTML: InsertHTMLCommand, insertHTML: InsertHTMLCommand,
} }
} }

View File

@@ -3,7 +3,7 @@ import { Command } from '../Editor'
type InsertTextCommand = (value: string) => Command type InsertTextCommand = (value: string) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
insertText: InsertTextCommand, insertText: InsertTextCommand,
} }
} }

View File

@@ -6,7 +6,7 @@ import getNodeType from '../utils/getNodeType'
type LiftListItem = (typeOrName: string | NodeType) => Command type LiftListItem = (typeOrName: string | NodeType) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
liftListItem: LiftListItem, liftListItem: LiftListItem,
} }
} }

View File

@@ -6,7 +6,7 @@ import getMarkRange from '../utils/getMarkRange'
type RemoveMarkCommand = (typeOrName: string | MarkType) => Command type RemoveMarkCommand = (typeOrName: string | MarkType) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
toggleMark: RemoveMarkCommand, toggleMark: RemoveMarkCommand,
} }
} }

View File

@@ -3,7 +3,7 @@ import { Command } from '../Editor'
type RemoveMarksCommand = () => Command type RemoveMarksCommand = () => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
removeMarks: RemoveMarksCommand, removeMarks: RemoveMarksCommand,
} }
} }

View File

@@ -14,7 +14,7 @@ type ReplaceWithNodeCommand = (
) => Command ) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
replaceText: ReplaceWithNodeCommand, replaceText: ReplaceWithNodeCommand,
} }
} }

View File

@@ -4,7 +4,7 @@ import { selectAll as originalSelectAll } from 'prosemirror-commands'
type SelectAllCommand = () => Command type SelectAllCommand = () => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
selectAll: SelectAllCommand, selectAll: SelectAllCommand,
} }
} }

View File

@@ -4,7 +4,7 @@ import { selectParentNode as originalSelectParentNode } from 'prosemirror-comman
type SelectParentNodeCommand = () => Command type SelectParentNodeCommand = () => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
selectParentNode: SelectParentNodeCommand, selectParentNode: SelectParentNodeCommand,
} }
} }

View File

@@ -8,7 +8,7 @@ type SetContentCommand = (
) => Command ) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
setContent: SetContentCommand, setContent: SetContentCommand,
} }
} }

View File

@@ -6,7 +6,7 @@ import getNodeType from '../utils/getNodeType'
type SinkListItem = (typeOrName: string | NodeType) => Command type SinkListItem = (typeOrName: string | NodeType) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
sinkListItem: SinkListItem, sinkListItem: SinkListItem,
} }
} }

View File

@@ -6,7 +6,7 @@ import getNodeType from '../utils/getNodeType'
type SplitListItem = (typeOrName: string | NodeType) => Command type SplitListItem = (typeOrName: string | NodeType) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
splitListItem: SplitListItem, splitListItem: SplitListItem,
} }
} }

View File

@@ -6,7 +6,7 @@ import getMarkType from '../utils/getMarkType'
type ToggleMarkCommand = (typeOrName: string | MarkType) => Command type ToggleMarkCommand = (typeOrName: string | MarkType) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
toggleMark: ToggleMarkCommand, toggleMark: ToggleMarkCommand,
} }
} }

View File

@@ -11,7 +11,7 @@ type ToggleNodeCommand = (
) => Command ) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
toggleNode: ToggleNodeCommand, toggleNode: ToggleNodeCommand,
} }
} }

View File

@@ -9,7 +9,7 @@ type UpdateMarkCommand = (
) => Command ) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Commands {
updateMark: UpdateMarkCommand, updateMark: UpdateMarkCommand,
} }
} }

View File

@@ -4,7 +4,7 @@ import { textblockTypeInputRule } from 'prosemirror-inputrules'
export type BlockquoteCommand = () => Command export type BlockquoteCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
blockquote: BlockquoteCommand, blockquote: BlockquoteCommand,
} }
} }

View File

@@ -3,7 +3,7 @@ import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core'
export type BoldCommand = () => Command export type BoldCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
bold: BoldCommand, bold: BoldCommand,
} }
} }

View File

@@ -4,7 +4,7 @@ import { wrappingInputRule } from 'prosemirror-inputrules'
export type BulletListCommand = () => Command export type BulletListCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
bulletList: BulletListCommand, bulletList: BulletListCommand,
} }
} }

View File

@@ -4,7 +4,7 @@ import { textblockTypeInputRule } from 'prosemirror-inputrules'
export type CodeBlockCommand = () => Command export type CodeBlockCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
codeBlock: CodeBlockCommand, codeBlock: CodeBlockCommand,
} }
} }

View File

@@ -3,7 +3,7 @@ import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core'
export type CodeCommand = () => Command export type CodeCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
code: CodeCommand, code: CodeCommand,
} }
} }

View File

@@ -4,7 +4,7 @@ import { chainCommands, exitCode } from 'prosemirror-commands'
export type HardBreakCommand = () => Command export type HardBreakCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
hardBreak: HardBreakCommand, hardBreak: HardBreakCommand,
} }
} }

View File

@@ -10,7 +10,7 @@ export interface HeadingOptions {
export type HeadingCommand = (level: Level) => Command export type HeadingCommand = (level: Level) => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
heading: HeadingCommand, heading: HeadingCommand,
} }
} }

View File

@@ -8,7 +8,7 @@ import {
} from 'prosemirror-history' } from 'prosemirror-history'
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
undo: () => Command, undo: () => Command,
redo: () => Command, redo: () => Command,
} }

View File

@@ -3,7 +3,7 @@ import { Command, Node, nodeInputRule } from '@tiptap/core'
export type HorizontalRuleCommand = () => Command export type HorizontalRuleCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
horizontalRule: HorizontalRuleCommand, horizontalRule: HorizontalRuleCommand,
} }
} }

View File

@@ -3,7 +3,7 @@ import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core'
export type ItalicCommand = () => Command export type ItalicCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
italic: ItalicCommand, italic: ItalicCommand,
} }
} }

View File

@@ -3,7 +3,7 @@ import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core'
type StrikeCommand = () => Command type StrikeCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
strike: StrikeCommand, strike: StrikeCommand,
} }
} }

View File

@@ -3,7 +3,7 @@ import { Command, Mark } from '@tiptap/core'
export type UnderlineCommand = () => Command export type UnderlineCommand = () => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Editor { interface Commands {
underline: UnderlineCommand, underline: UnderlineCommand,
} }
} }