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 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 = {
[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<EditorOptions> = {}) {
super()
this.options = { ...this.options, ...options }

View File

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

View File

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

View File

@ -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)
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

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

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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,
}
}