return editor for all core command types

This commit is contained in:
Philipp Kühn
2020-08-17 16:38:13 +02:00
parent 9ad1442bb6
commit 3c60bb6406
14 changed files with 49 additions and 47 deletions

View File

@@ -1,14 +1,14 @@
import { Editor } from '../Editor' import { Editor } from '../Editor'
type ClearContent = (emitUpdate?: Boolean) => any type ClearContentCommand = (emitUpdate?: Boolean) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
clearContent: ClearContent, clearContent: ClearContentCommand,
} }
} }
export default (next: Function, editor: Editor): ClearContent => (emitUpdate = false) => { export default (next: Function, editor: Editor) => (emitUpdate = false) => {
editor.setContent('', emitUpdate) editor.setContent('', emitUpdate)
next() next()
} }

View File

@@ -1,15 +1,15 @@
import { Editor } from '../Editor' import { Editor } from '../Editor'
import { deleteSelection } from 'prosemirror-commands' import { deleteSelection } from 'prosemirror-commands'
type DeleteSelection = () => any type DeleteSelectionCommand = () => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
deleteSelection: DeleteSelection, deleteSelection: DeleteSelectionCommand,
} }
} }
export default (next: Function, { state, view }: Editor): DeleteSelection => () => { export default (next: Function, { state, view }: Editor) => () => {
deleteSelection(state, view.dispatch) deleteSelection(state, view.dispatch)
next() next()
} }

View File

@@ -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 FocusCommand = (position?: Position) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
focus(position?: Position): Editor, focus: FocusCommand
} }
} }

View File

@@ -2,15 +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 type InsertHTMLCommand = (value: string) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
insertHTML: InsertHTML, insertHTML: InsertHTMLCommand,
} }
} }
export default (next: Function, editor: Editor): InsertHTML => value => { export default (next: Function, editor: Editor) => (value: string) => {
const { view, state } = editor const { view, state } = editor
const { selection } = state const { selection } = state
const element = elementFromString(value) const element = elementFromString(value)

View File

@@ -1,14 +1,14 @@
import { Editor } from '../Editor' import { Editor } from '../Editor'
type InsertText = (value: string) => any type InsertTextCommand = (value: string) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
insertText: InsertText, insertText: InsertTextCommand,
} }
} }
export default (next: Function, editor: Editor): InsertText => value => { export default (next: Function, editor: Editor) => (value: string) => {
const { view, state } = editor const { view, state } = editor
const transaction = state.tr.insertText(value) const transaction = state.tr.insertText(value)

View File

@@ -3,15 +3,15 @@ import { MarkType } from 'prosemirror-model'
import getMarkType from '../utils/getMarkType' import getMarkType from '../utils/getMarkType'
import getMarkRange from '../utils/getMarkRange' import getMarkRange from '../utils/getMarkRange'
type RemoveMark = (type: string | MarkType) => any type RemoveMarkCommand = (typeOrName: string | MarkType) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
toggleMark: RemoveMark, toggleMark: RemoveMarkCommand,
} }
} }
export default (next: Function, editor: Editor): RemoveMark => typeOrName => { export default (next: Function, editor: Editor) => (typeOrName: string | MarkType) => {
const { view, state, schema } = editor const { view, state, schema } = editor
const { tr, selection } = state const { tr, selection } = state
const type = getMarkType(typeOrName, schema) const type = getMarkType(typeOrName, schema)

View File

@@ -1,14 +1,14 @@
import { Editor } from '../Editor' import { Editor } from '../Editor'
type RemoveMarks = () => any type RemoveMarksCommand = () => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
removeMarks: RemoveMarks, removeMarks: RemoveMarksCommand,
} }
} }
export default (next: Function, editor: Editor): RemoveMarks => () => { export default (next: Function, editor: Editor) => () => {
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

View File

@@ -7,19 +7,19 @@ interface Range {
to: number, to: number,
} }
type ReplaceWithNode = ( type ReplaceWithNodeCommand = (
type: NodeType, typeOrName: NodeType,
attrs: {}, attrs: {},
range?: Range, range?: Range,
) => any ) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
replaceText: ReplaceWithNode, replaceText: ReplaceWithNodeCommand,
} }
} }
export default (next: Function, editor: Editor): ReplaceWithNode => (typeOrName, attrs, range) => { export default (next: Function, editor: Editor) => (typeOrName: NodeType, attrs: {}, range?: Range) => {
const { view, state, schema } = editor const { view, state, schema } = editor
const { $from, $to } = state.selection const { $from, $to } = state.selection
const type = getNodeType(typeOrName, schema) const type = getNodeType(typeOrName, schema)

View File

@@ -1,15 +1,15 @@
import { Editor } from '../Editor' import { Editor } from '../Editor'
import { selectAll } from 'prosemirror-commands' import { selectAll } from 'prosemirror-commands'
type SelectAll = () => any type SelectAllCommand = () => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
selectAll: SelectAll, selectAll: SelectAllCommand,
} }
} }
export default (next: Function, { state, view }: Editor): SelectAll => () => { export default (next: Function, { state, view }: Editor) => () => {
selectAll(state, view.dispatch) selectAll(state, view.dispatch)
next() next()
} }

View File

@@ -1,15 +1,15 @@
import { Editor } from '../Editor' import { Editor } from '../Editor'
import { selectParentNode } from 'prosemirror-commands' import { selectParentNode } from 'prosemirror-commands'
type SelectParentNode = () => any type SelectParentNodeCommand = () => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
selectParentNode: SelectParentNode, selectParentNode: SelectParentNodeCommand,
} }
} }
export default (next: Function, { state, view }: Editor): SelectParentNode => () => { export default (next: Function, { state, view }: Editor) => () => {
selectParentNode(state, view.dispatch) selectParentNode(state, view.dispatch)
next() next()
} }

View File

@@ -1,19 +1,19 @@
import { Editor } from '../Editor' import { Editor } from '../Editor'
import { TextSelection } from 'prosemirror-state' import { TextSelection } from 'prosemirror-state'
type SetContent = ( type SetContentCommand = (
content: string, content: string,
emitUpdate?: Boolean, emitUpdate?: Boolean,
parseOptions?: any, parseOptions?: any,
) => any ) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
setContent: SetContent, setContent: SetContentCommand,
} }
} }
export default (next: Function, editor: Editor): SetContent => (content, emitUpdate = false, parseOptions = {}) => { export default (next: Function, editor: Editor) => (content: string, emitUpdate: Boolean = false, parseOptions = {}) => {
if (content === null) { if (content === null) {
next() next()
return return

View File

@@ -3,15 +3,15 @@ import { toggleMark } from 'prosemirror-commands'
import { MarkType } from 'prosemirror-model' import { MarkType } from 'prosemirror-model'
import getMarkType from '../utils/getMarkType' import getMarkType from '../utils/getMarkType'
type ToggleMark = (type: string | MarkType) => any type ToggleMarkCommand = (typeOrName: string | MarkType) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
toggleMark: ToggleMark, toggleMark: ToggleMarkCommand,
} }
} }
export default (next: Function, editor: Editor): ToggleMark => (typeOrName) => { export default (next: Function, editor: Editor) => (typeOrName: string | MarkType) => {
const { view, state, schema } = editor const { view, state, schema } = editor
const type = getMarkType(typeOrName, schema) const type = getMarkType(typeOrName, schema)

View File

@@ -4,19 +4,19 @@ import { Editor } from '../Editor'
import nodeIsActive from '../utils/nodeIsActive' import nodeIsActive from '../utils/nodeIsActive'
import getNodeType from '../utils/getNodeType' import getNodeType from '../utils/getNodeType'
type ToggleNode = ( type ToggleNodeCommand = (
type: string | NodeType, typeOrName: string | NodeType,
toggleType: string | NodeType, toggleType: string | NodeType,
attrs?: {} attrs?: {}
) => any ) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
toggleNode: ToggleNode, toggleNode: ToggleNodeCommand,
} }
} }
export default (next: Function, editor: Editor): ToggleNode => (typeOrName, toggleTypeOrName, attrs) => { export default (next: Function, editor: Editor) => (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attrs = {}) => {
const { view, state, schema } = editor const { view, state, schema } = editor
const type = getNodeType(typeOrName, schema) const type = getNodeType(typeOrName, schema)
const toggleType = getNodeType(toggleTypeOrName, schema) const toggleType = getNodeType(toggleTypeOrName, schema)

View File

@@ -3,18 +3,18 @@ import { MarkType } from 'prosemirror-model'
import getMarkType from '../utils/getMarkType' import getMarkType from '../utils/getMarkType'
import getMarkRange from '../utils/getMarkRange' import getMarkRange from '../utils/getMarkRange'
type UpdateMark = ( type UpdateMarkCommand = (
type: string | MarkType, typeOrName: string | MarkType,
attrs: {}, attrs: {},
) => any ) => Editor
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
updateMark: UpdateMark, updateMark: UpdateMarkCommand,
} }
} }
export default (next: Function, editor: Editor): UpdateMark => (typeOrName, attrs) => { export default (next: Function, editor: Editor) => (typeOrName: string | MarkType, attrs = {}) => {
const { view, state, schema } = editor const { view, state, schema } = editor
const { tr, selection, doc } = state const { tr, selection, doc } = state
let { from, to, $from, empty } = selection let { from, to, $from, empty } = selection