improve type handling for commands
This commit is contained in:
@@ -21,7 +21,7 @@ import Node from './Node'
|
|||||||
import Mark from './Mark'
|
import Mark from './Mark'
|
||||||
import EventEmitter from './EventEmitter'
|
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 {
|
export interface CommandSpec {
|
||||||
[key: string]: Command
|
[key: string]: Command
|
||||||
@@ -104,7 +104,7 @@ export class Editor extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.commands[name] = this.chainCommand((...args: any) => {
|
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
|
return this.proxy
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
import { Editor } from '../Editor'
|
import { Editor } from '../Editor'
|
||||||
import { TextSelection } from 'prosemirror-state'
|
import { TextSelection } from 'prosemirror-state'
|
||||||
|
|
||||||
|
type ClearContent = (emitUpdate?: Boolean) => any
|
||||||
|
|
||||||
declare module '../Editor' {
|
declare module '../Editor' {
|
||||||
interface 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)
|
editor.setContent('', emitUpdate)
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ import { TextSelection } from 'prosemirror-state'
|
|||||||
import sleep from '../utils/sleep'
|
import sleep from '../utils/sleep'
|
||||||
import minMax from '../utils/minMax'
|
import minMax from '../utils/minMax'
|
||||||
|
|
||||||
|
type Focus = (position?: Position) => any
|
||||||
|
|
||||||
declare module '../Editor' {
|
declare module '../Editor' {
|
||||||
interface 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
|
const { view, state } = editor
|
||||||
|
|
||||||
if ((view.hasFocus() && position === null)) {
|
if ((view.hasFocus() && position === null)) {
|
||||||
|
|||||||
@@ -2,13 +2,15 @@ import { DOMParser } from 'prosemirror-model'
|
|||||||
import { Editor } from '../Editor'
|
import { Editor } from '../Editor'
|
||||||
import elementFromString from '../utils/elementFromString'
|
import elementFromString from '../utils/elementFromString'
|
||||||
|
|
||||||
|
type InsertHTML = (value: string) => any
|
||||||
|
|
||||||
declare module '../Editor' {
|
declare module '../Editor' {
|
||||||
interface 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 { view, state } = editor
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const element = elementFromString(value)
|
const element = elementFromString(value)
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
import { Editor } from '../Editor'
|
import { Editor } from '../Editor'
|
||||||
|
|
||||||
|
type InsertText = (value: string) => any
|
||||||
|
|
||||||
declare module '../Editor' {
|
declare module '../Editor' {
|
||||||
interface 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 { view, state } = editor
|
||||||
const transaction = state.tr.insertText(value)
|
const transaction = state.tr.insertText(value)
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
import { Editor } from '../Editor'
|
import { Editor } from '../Editor'
|
||||||
|
|
||||||
|
type RemoveMarks = () => any
|
||||||
|
|
||||||
declare module '../Editor' {
|
declare module '../Editor' {
|
||||||
interface 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 { state, view, schema } = editor
|
||||||
const { selection, tr } = state
|
const { selection, tr } = state
|
||||||
const { from, to, empty } = selection
|
const { from, to, empty } = selection
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
import { Editor } from '../Editor'
|
import { Editor } from '../Editor'
|
||||||
import { TextSelection } from 'prosemirror-state'
|
import { TextSelection } from 'prosemirror-state'
|
||||||
|
|
||||||
|
type SetContent = (
|
||||||
|
content: string,
|
||||||
|
emitUpdate?: Boolean,
|
||||||
|
parseOptions?: any,
|
||||||
|
) => any
|
||||||
|
|
||||||
declare module '../Editor' {
|
declare module '../Editor' {
|
||||||
interface Editor {
|
interface Editor {
|
||||||
setContent(content: string, emitUpdate?: Boolean, parseOptions?: any): Editor,
|
setContent: SetContent,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function setContent(
|
export default (next: Function, editor: Editor): SetContent => (content, emitUpdate, parseOptions) => {
|
||||||
next: Function,
|
|
||||||
editor: Editor,
|
|
||||||
content = null,
|
|
||||||
emitUpdate = false,
|
|
||||||
parseOptions = {},
|
|
||||||
): void {
|
|
||||||
if (content === null) {
|
if (content === null) {
|
||||||
next()
|
next()
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -3,19 +3,19 @@ import { setBlockType } from 'prosemirror-commands'
|
|||||||
import { Editor } from '../Editor'
|
import { Editor } from '../Editor'
|
||||||
import nodeIsActive from '../utils/nodeIsActive'
|
import nodeIsActive from '../utils/nodeIsActive'
|
||||||
|
|
||||||
|
type ToggleBlockType = (
|
||||||
|
type: NodeType,
|
||||||
|
toggleType: NodeType,
|
||||||
|
attrs?: {}
|
||||||
|
) => any
|
||||||
|
|
||||||
declare module '../Editor' {
|
declare module '../Editor' {
|
||||||
interface Editor {
|
interface Editor {
|
||||||
toggleBlockType(type: NodeType, toggleType: NodeType, attrs?: {}): Editor,
|
toggleBlockType: ToggleBlockType,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function toggleBlockType(
|
export default (next: Function, editor: Editor): ToggleBlockType => (type, toggleType, attrs) => {
|
||||||
next: Function,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
toggleType: NodeType,
|
|
||||||
attrs?: {},
|
|
||||||
): void {
|
|
||||||
const { view, state } = editor
|
const { view, state } = editor
|
||||||
const isActive = nodeIsActive(state, type, attrs)
|
const isActive = nodeIsActive(state, type, attrs)
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ export default class Bold extends Mark {
|
|||||||
|
|
||||||
commands(): CommandSpec {
|
commands(): CommandSpec {
|
||||||
return {
|
return {
|
||||||
bold: (next, { view }) => {
|
bold: (next, { view }) => () => {
|
||||||
toggleMark(this.type)(view.state, view.dispatch)
|
toggleMark(this.type)(view.state, view.dispatch)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export default class Code extends Mark {
|
|||||||
|
|
||||||
commands(): CommandSpec {
|
commands(): CommandSpec {
|
||||||
return {
|
return {
|
||||||
code: (next, { view }) => {
|
code: (next, { view }) => () => {
|
||||||
toggleMark(this.type)(view.state, view.dispatch)
|
toggleMark(this.type)(view.state, view.dispatch)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -47,8 +47,8 @@ export default class Heading extends Node {
|
|||||||
|
|
||||||
commands(): CommandSpec {
|
commands(): CommandSpec {
|
||||||
return {
|
return {
|
||||||
heading: (next, editor, attrs) => {
|
heading: next => attrs => {
|
||||||
editor.toggleBlockType(this.type, editor.schema.nodes.paragraph, attrs)
|
this.editor.toggleBlockType(this.type, this.editor.schema.nodes.paragraph, attrs)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,11 +34,11 @@ export default class History extends Extension {
|
|||||||
|
|
||||||
commands(): CommandSpec {
|
commands(): CommandSpec {
|
||||||
return {
|
return {
|
||||||
undo: (next, { view }) => {
|
undo: (next, { view }) => () => {
|
||||||
undo(view.state, view.dispatch)
|
undo(view.state, view.dispatch)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
redo: (next, { view }) => {
|
redo: (next, { view }) => () => {
|
||||||
redo(view.state, view.dispatch)
|
redo(view.state, view.dispatch)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export default class Italic extends Mark {
|
|||||||
|
|
||||||
commands(): CommandSpec {
|
commands(): CommandSpec {
|
||||||
return {
|
return {
|
||||||
italic: (next, { view }) => {
|
italic: (next, { view }) => () => {
|
||||||
toggleMark(this.type)(view.state, view.dispatch)
|
toggleMark(this.type)(view.state, view.dispatch)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user