mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-08-06 13:38:49 +08:00
improve type handling for commands
This commit is contained in:
parent
3deae61a15
commit
91d4aa7e39
@ -21,7 +21,7 @@ import Node from './Node'
|
||||
import Mark from './Mark'
|
||||
import EventEmitter from './EventEmitter'
|
||||
|
||||
export type Command = (next: Function, editor: Editor, ...args: any) => any
|
||||
export type Command = (next: Function, editor: Editor) => (...args: any) => any
|
||||
|
||||
export interface CommandSpec {
|
||||
[key: string]: Command
|
||||
@ -104,7 +104,7 @@ export class Editor extends EventEmitter {
|
||||
}
|
||||
|
||||
this.commands[name] = this.chainCommand((...args: any) => {
|
||||
return new Promise(resolve => callback(resolve, this.proxy, ...args))
|
||||
return new Promise(resolve => callback(resolve, this.proxy)(...args))
|
||||
})
|
||||
|
||||
return this.proxy
|
||||
|
@ -1,13 +1,15 @@
|
||||
import { Editor } from '../Editor'
|
||||
import { TextSelection } from 'prosemirror-state'
|
||||
|
||||
type ClearContent = (emitUpdate?: Boolean) => any
|
||||
|
||||
declare module '../Editor' {
|
||||
interface Editor {
|
||||
clearContent(emitUpdate?: Boolean): Editor,
|
||||
clearContent: ClearContent,
|
||||
}
|
||||
}
|
||||
|
||||
export default function clearContent(next: Function, editor: Editor, emitUpdate = false): void {
|
||||
export default (next: Function, editor: Editor, emitUpdate = false): ClearContent => emitUpdate => {
|
||||
editor.setContent('', emitUpdate)
|
||||
next()
|
||||
}
|
||||
|
@ -3,9 +3,11 @@ import { TextSelection } from 'prosemirror-state'
|
||||
import sleep from '../utils/sleep'
|
||||
import minMax from '../utils/minMax'
|
||||
|
||||
type Focus = (position?: Position) => any
|
||||
|
||||
declare module '../Editor' {
|
||||
interface Editor {
|
||||
focus(position?: Position): Editor
|
||||
focus: Focus,
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,7 +45,7 @@ function resolveSelection(editor: Editor, position: Position = null): ResolvedSe
|
||||
}
|
||||
}
|
||||
|
||||
export default async function focus(next: Function, editor: Editor, position: Position = null): Promise<void> {
|
||||
export default (next: Function, editor: Editor, position: Position = null): Focus => async () => {
|
||||
const { view, state } = editor
|
||||
|
||||
if ((view.hasFocus() && position === null)) {
|
||||
|
@ -2,13 +2,15 @@ import { DOMParser } from 'prosemirror-model'
|
||||
import { Editor } from '../Editor'
|
||||
import elementFromString from '../utils/elementFromString'
|
||||
|
||||
type InsertHTML = (value: string) => any
|
||||
|
||||
declare module '../Editor' {
|
||||
interface Editor {
|
||||
insertHTML(value: string): Editor,
|
||||
insertHTML: InsertHTML,
|
||||
}
|
||||
}
|
||||
|
||||
export default function insertHTML(next: Function, editor: Editor, value: string): void {
|
||||
export default (next: Function, editor: Editor, value: string): InsertHTML => () => {
|
||||
const { view, state } = editor
|
||||
const { selection } = state
|
||||
const element = elementFromString(value)
|
||||
|
@ -1,12 +1,14 @@
|
||||
import { Editor } from '../Editor'
|
||||
|
||||
type InsertText = (value: string) => any
|
||||
|
||||
declare module '../Editor' {
|
||||
interface Editor {
|
||||
insertText(value: string): Editor,
|
||||
insertText: InsertText,
|
||||
}
|
||||
}
|
||||
|
||||
export default function insertText(next: Function, editor: Editor, value: string): void {
|
||||
export default (next: Function, editor: Editor, value: string): InsertText => () => {
|
||||
const { view, state } = editor
|
||||
const transaction = state.tr.insertText(value)
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
import { Editor } from '../Editor'
|
||||
|
||||
type RemoveMarks = () => any
|
||||
|
||||
declare module '../Editor' {
|
||||
interface Editor {
|
||||
removeMarks(): Editor,
|
||||
removeMarks: RemoveMarks,
|
||||
}
|
||||
}
|
||||
|
||||
export default function removeMarks(next: Function, editor: Editor): void {
|
||||
export default (next: Function, editor: Editor): RemoveMarks => () => {
|
||||
const { state, view, schema } = editor
|
||||
const { selection, tr } = state
|
||||
const { from, to, empty } = selection
|
||||
|
@ -1,19 +1,19 @@
|
||||
import { Editor } from '../Editor'
|
||||
import { TextSelection } from 'prosemirror-state'
|
||||
|
||||
type SetContent = (
|
||||
content: string,
|
||||
emitUpdate?: Boolean,
|
||||
parseOptions?: any,
|
||||
) => any
|
||||
|
||||
declare module '../Editor' {
|
||||
interface Editor {
|
||||
setContent(content: string, emitUpdate?: Boolean, parseOptions?: any): Editor,
|
||||
setContent: SetContent,
|
||||
}
|
||||
}
|
||||
|
||||
export default function setContent(
|
||||
next: Function,
|
||||
editor: Editor,
|
||||
content = null,
|
||||
emitUpdate = false,
|
||||
parseOptions = {},
|
||||
): void {
|
||||
export default (next: Function, editor: Editor): SetContent => (content, emitUpdate, parseOptions) => {
|
||||
if (content === null) {
|
||||
next()
|
||||
return
|
||||
|
@ -3,19 +3,19 @@ import { setBlockType } from 'prosemirror-commands'
|
||||
import { Editor } from '../Editor'
|
||||
import nodeIsActive from '../utils/nodeIsActive'
|
||||
|
||||
type ToggleBlockType = (
|
||||
type: NodeType,
|
||||
toggleType: NodeType,
|
||||
attrs?: {}
|
||||
) => any
|
||||
|
||||
declare module '../Editor' {
|
||||
interface Editor {
|
||||
toggleBlockType(type: NodeType, toggleType: NodeType, attrs?: {}): Editor,
|
||||
toggleBlockType: ToggleBlockType,
|
||||
}
|
||||
}
|
||||
|
||||
export default function toggleBlockType(
|
||||
next: Function,
|
||||
editor: Editor,
|
||||
type: NodeType,
|
||||
toggleType: NodeType,
|
||||
attrs?: {},
|
||||
): void {
|
||||
export default (next: Function, editor: Editor): ToggleBlockType => (type, toggleType, attrs) => {
|
||||
const { view, state } = editor
|
||||
const isActive = nodeIsActive(state, type, attrs)
|
||||
|
||||
|
@ -34,7 +34,7 @@ export default class Bold extends Mark {
|
||||
|
||||
commands(): CommandSpec {
|
||||
return {
|
||||
bold: (next, { view }) => {
|
||||
bold: (next, { view }) => () => {
|
||||
toggleMark(this.type)(view.state, view.dispatch)
|
||||
next()
|
||||
},
|
||||
|
@ -25,7 +25,7 @@ export default class Code extends Mark {
|
||||
|
||||
commands(): CommandSpec {
|
||||
return {
|
||||
code: (next, { view }) => {
|
||||
code: (next, { view }) => () => {
|
||||
toggleMark(this.type)(view.state, view.dispatch)
|
||||
next()
|
||||
},
|
||||
|
@ -47,8 +47,8 @@ export default class Heading extends Node {
|
||||
|
||||
commands(): CommandSpec {
|
||||
return {
|
||||
heading: (next, editor, attrs) => {
|
||||
editor.toggleBlockType(this.type, editor.schema.nodes.paragraph, attrs)
|
||||
heading: next => attrs => {
|
||||
this.editor.toggleBlockType(this.type, this.editor.schema.nodes.paragraph, attrs)
|
||||
next()
|
||||
},
|
||||
}
|
||||
|
@ -34,11 +34,11 @@ export default class History extends Extension {
|
||||
|
||||
commands(): CommandSpec {
|
||||
return {
|
||||
undo: (next, { view }) => {
|
||||
undo: (next, { view }) => () => {
|
||||
undo(view.state, view.dispatch)
|
||||
next()
|
||||
},
|
||||
redo: (next, { view }) => {
|
||||
redo: (next, { view }) => () => {
|
||||
redo(view.state, view.dispatch)
|
||||
next()
|
||||
},
|
||||
|
@ -26,7 +26,7 @@ export default class Italic extends Mark {
|
||||
|
||||
commands(): CommandSpec {
|
||||
return {
|
||||
italic: (next, { view }) => {
|
||||
italic: (next, { view }) => () => {
|
||||
toggleMark(this.type)(view.state, view.dispatch)
|
||||
next()
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user