restructure commands
This commit is contained in:
@@ -3,7 +3,10 @@ import { Command } from '../types'
|
||||
import getMarkType from '../utils/getMarkType'
|
||||
import getMarkAttributes from '../utils/getMarkAttributes'
|
||||
|
||||
export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ tr, state, dispatch }) => {
|
||||
/**
|
||||
* Add a mark with new attributes.
|
||||
*/
|
||||
export const addMark = (typeOrName: string | MarkType, attributes?: {}): Command => ({ tr, state, dispatch }) => {
|
||||
const { selection } = tr
|
||||
const { from, to, empty } = selection
|
||||
const type = getMarkType(typeOrName, state.schema)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (): Command => ({ view }) => {
|
||||
/**
|
||||
* Removes focus from the editor.
|
||||
*/
|
||||
export const blur = (): Command => ({ view }) => {
|
||||
const element = view.dom as HTMLElement
|
||||
|
||||
element.blur()
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (emitUpdate: Boolean = false): Command => ({ commands }) => {
|
||||
/**
|
||||
* Clear the whole document.
|
||||
*/
|
||||
export const clearContent = (emitUpdate: Boolean = false): Command => ({ commands }) => {
|
||||
return commands.setContent('', emitUpdate)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { liftTarget } from 'prosemirror-transform'
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (): Command => ({ state, tr, dispatch }) => {
|
||||
/**
|
||||
* Normalize nodes to a simple paragraph.
|
||||
*/
|
||||
export const clearNodes = (): Command => ({ state, tr, dispatch }) => {
|
||||
const { selection } = tr
|
||||
const { from, to } = selection
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (fn: (props: Parameters<Command>[0]) => boolean): Command => props => {
|
||||
/**
|
||||
* Define a command inline.
|
||||
*/
|
||||
export const command = (fn: (props: Parameters<Command>[0]) => boolean): Command => props => {
|
||||
return fn(props)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { deleteSelection } from 'prosemirror-commands'
|
||||
import { deleteSelection as originalDeleteSelection } from 'prosemirror-commands'
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (): Command => ({ state, dispatch }) => {
|
||||
return deleteSelection(state, dispatch)
|
||||
/**
|
||||
* Delete the selection, if there is one.
|
||||
*/
|
||||
export const deleteSelection = (): Command => ({ state, dispatch }) => {
|
||||
return originalDeleteSelection(state, dispatch)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@ import { Command } from '../types'
|
||||
import getMarkType from '../utils/getMarkType'
|
||||
import getMarkRange from '../utils/getMarkRange'
|
||||
|
||||
export default (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => {
|
||||
/**
|
||||
* Extends the text selection to the current mark.
|
||||
*/
|
||||
export const extendMarkRange = (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => {
|
||||
const type = getMarkType(typeOrName, state.schema)
|
||||
const { doc, selection } = tr
|
||||
const { $from, empty } = selection
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (commands: Command[] | ((props: Parameters<Command>[0]) => Command[])): Command => props => {
|
||||
/**
|
||||
* Runs one command after the other and stops at the first which returns true.
|
||||
*/
|
||||
export const first = (commands: Command[] | ((props: Parameters<Command>[0]) => Command[])): Command => props => {
|
||||
const items = typeof commands === 'function'
|
||||
? commands(props)
|
||||
: commands
|
||||
@@ -35,7 +35,10 @@ function resolveSelection(editor: Editor, position: FocusPosition = null): Resol
|
||||
}
|
||||
}
|
||||
|
||||
export default (position: FocusPosition = null): Command => ({
|
||||
/**
|
||||
* Focus the editor at the given position.
|
||||
*/
|
||||
export const focus = (position: FocusPosition = null): Command => ({
|
||||
editor,
|
||||
view,
|
||||
tr,
|
||||
|
||||
@@ -17,7 +17,10 @@ function selectionToInsertionEnd(tr: Transaction, startLen: number, bias: number
|
||||
tr.setSelection(Selection.near(tr.doc.resolve(end as unknown as number), bias))
|
||||
}
|
||||
|
||||
export default (value: string): Command => ({ tr, state, dispatch }) => {
|
||||
/**
|
||||
* Insert a string of HTML at the current position.
|
||||
*/
|
||||
export const insertHTML = (value: string): Command => ({ tr, state, dispatch }) => {
|
||||
const { selection } = tr
|
||||
const element = elementFromString(value)
|
||||
const slice = DOMParser.fromSchema(state.schema).parseSlice(element)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (value: string): Command => ({ tr, dispatch }) => {
|
||||
/**
|
||||
* Insert a string of text at the current position.
|
||||
*/
|
||||
export const insertText = (value: string): Command => ({ tr, dispatch }) => {
|
||||
if (dispatch) {
|
||||
tr.insertText(value)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { lift } from 'prosemirror-commands'
|
||||
import { lift as originalLift } from 'prosemirror-commands'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import nodeIsActive from '../utils/nodeIsActive'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
export default (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
|
||||
/**
|
||||
* Removes an existing wrap.
|
||||
*/
|
||||
export const lift = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const isActive = nodeIsActive(state, type, attributes)
|
||||
|
||||
@@ -12,5 +15,5 @@ export default (typeOrName: string | NodeType, attributes = {}): Command => ({ s
|
||||
return false
|
||||
}
|
||||
|
||||
return lift(state, dispatch)
|
||||
return originalLift(state, dispatch)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { liftListItem } from 'prosemirror-schema-list'
|
||||
import { liftListItem as originalLiftListItem } from 'prosemirror-schema-list'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {
|
||||
/**
|
||||
* Lift the list item into a wrapping list.
|
||||
*/
|
||||
export const liftListItem = (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
|
||||
return liftListItem(type)(state, dispatch)
|
||||
return originalLiftListItem(type)(state, dispatch)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@ import { Command } from '../types'
|
||||
import getMarkType from '../utils/getMarkType'
|
||||
import getMarkRange from '../utils/getMarkRange'
|
||||
|
||||
export default (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => {
|
||||
/**
|
||||
* Remove all marks in the current selection.
|
||||
*/
|
||||
export const removeMark = (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => {
|
||||
const { selection } = tr
|
||||
const type = getMarkType(typeOrName, state.schema)
|
||||
let { from, to } = selection
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (): Command => ({ tr, state, dispatch }) => {
|
||||
/**
|
||||
* Remove all marks in the current selection.
|
||||
*/
|
||||
export const removeMarks = (): Command => ({ tr, state, dispatch }) => {
|
||||
const { selection } = tr
|
||||
const { from, to, empty } = selection
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
import deleteProps from '../utils/deleteProps'
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (typeOrName: string | NodeType, attributes: string[]): Command => ({ tr, state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const { selection } = tr
|
||||
const { from, to } = selection
|
||||
|
||||
state.doc.nodesBetween(from, to, (node, pos) => {
|
||||
if (node.type === type && dispatch) {
|
||||
tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes))
|
||||
}
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -3,7 +3,10 @@ import getNodeType from '../utils/getNodeType'
|
||||
import deleteProps from '../utils/deleteProps'
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (typeOrName: string | NodeType, attributes: string | string[]): Command => ({ tr, state, dispatch }) => {
|
||||
/**
|
||||
* Resets node attributes to the default value.
|
||||
*/
|
||||
export const resetNodeAttributes = (typeOrName: string | NodeType, attributes: string | string[]): Command => ({ tr, state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const { selection } = tr
|
||||
const { from, to } = selection
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (): Command => ({ tr, dispatch }) => {
|
||||
/**
|
||||
* Scroll the selection into view.
|
||||
*/
|
||||
export const scrollIntoView = (): Command => ({ tr, dispatch }) => {
|
||||
if (dispatch) {
|
||||
tr.scrollIntoView()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { selectAll } from 'prosemirror-commands'
|
||||
import { selectAll as originalSelectAll } from 'prosemirror-commands'
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (): Command => ({ state, dispatch }) => {
|
||||
return selectAll(state, dispatch)
|
||||
/**
|
||||
* Select the whole document.
|
||||
*/
|
||||
export const selectAll = (): Command => ({ state, dispatch }) => {
|
||||
return originalSelectAll(state, dispatch)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { selectParentNode } from 'prosemirror-commands'
|
||||
import { selectParentNode as originalSelectParentNode } from 'prosemirror-commands'
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (): Command => ({ state, dispatch }) => {
|
||||
return selectParentNode(state, dispatch)
|
||||
/**
|
||||
* Select the parent node.
|
||||
*/
|
||||
export const selectParentNode = (): Command => ({ state, dispatch }) => {
|
||||
return originalSelectParentNode(state, dispatch)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { setBlockType } from 'prosemirror-commands'
|
||||
import { setBlockType as originalSetBlockType } from 'prosemirror-commands'
|
||||
import { Command } from '../types'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
export default (typeOrName: string | NodeType, attrs = {}): Command => ({ state, dispatch }) => {
|
||||
/**
|
||||
* Replace a given range with a node.
|
||||
*/
|
||||
export const setBlockType = (typeOrName: string | NodeType, attrs = {}): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
|
||||
return setBlockType(type, attrs)(state, dispatch)
|
||||
return originalSetBlockType(type, attrs)(state, dispatch)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { TextSelection } from 'prosemirror-state'
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor, dispatch }) => {
|
||||
/**
|
||||
* Replace the whole document with new content.
|
||||
*/
|
||||
export const setContent = (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor, dispatch }) => {
|
||||
const { createDocument } = editor
|
||||
const { doc } = tr
|
||||
const document = createDocument(content, parseOptions)
|
||||
|
||||
@@ -3,7 +3,10 @@ import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {
|
||||
/**
|
||||
* Sink the list item down into an inner list.
|
||||
*/
|
||||
export const sinkListItem = (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
|
||||
return originalSinkListItem(type)(state, dispatch)
|
||||
|
||||
@@ -28,7 +28,10 @@ function keepMarks(state: EditorState) {
|
||||
}
|
||||
}
|
||||
|
||||
export default (options: Partial<SplitBlockOptions> = {}): Command => ({ tr, state, dispatch }) => {
|
||||
/**
|
||||
* Forks a new node from an existing node.
|
||||
*/
|
||||
export const splitBlock = (options: Partial<SplitBlockOptions> = {}): Command => ({ tr, state, dispatch }) => {
|
||||
const defaultOptions: SplitBlockOptions = {
|
||||
withAttributes: false,
|
||||
withMarks: true,
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { splitListItem } from 'prosemirror-schema-list'
|
||||
import { splitListItem as originalSplitListItem } from 'prosemirror-schema-list'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {
|
||||
/**
|
||||
* Splits one list item into two list items.
|
||||
*/
|
||||
export const splitListItem = (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
|
||||
return splitListItem(type)(state, dispatch)
|
||||
return originalSplitListItem(type)(state, dispatch)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@ import { Command } from '../types'
|
||||
import nodeIsActive from '../utils/nodeIsActive'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
export default (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attrs = {}): Command => ({ state, commands }) => {
|
||||
/**
|
||||
* Toggle a node with another node.
|
||||
*/
|
||||
export const toggleBlockType = (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attrs = {}): Command => ({ state, commands }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const toggleType = getNodeType(toggleTypeOrName, state.schema)
|
||||
const isActive = nodeIsActive(state, type, attrs)
|
||||
|
||||
@@ -4,7 +4,10 @@ import { Command } from '../types'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
import isList from '../utils/isList'
|
||||
|
||||
export default (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType): Command => ({
|
||||
/**
|
||||
* Toggle between different list types.
|
||||
*/
|
||||
export const toggleList = (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType): Command => ({
|
||||
editor, tr, state, dispatch, chain, commands, can,
|
||||
}) => {
|
||||
const { extensions } = editor.options
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { toggleMark } from 'prosemirror-commands'
|
||||
import { toggleMark as originalToggleMark } from 'prosemirror-commands'
|
||||
import { MarkType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import getMarkType from '../utils/getMarkType'
|
||||
import markIsActive from '../utils/markIsActive'
|
||||
|
||||
export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ state, dispatch, commands }) => {
|
||||
/**
|
||||
* Toggle a mark on and off.
|
||||
*/
|
||||
export const toggleMark = (typeOrName: string | MarkType, attributes?: {}): Command => ({ state, dispatch, commands }) => {
|
||||
const type = getMarkType(typeOrName, state.schema)
|
||||
|
||||
const hasMarkWithDifferentAttributes = attributes
|
||||
@@ -15,5 +18,5 @@ export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ s
|
||||
return commands.addMark(type, attributes)
|
||||
}
|
||||
|
||||
return toggleMark(type, attributes)(state, dispatch)
|
||||
return originalToggleMark(type, attributes)(state, dispatch)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@ import { Command } from '../types'
|
||||
import nodeIsActive from '../utils/nodeIsActive'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
export default (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
|
||||
/**
|
||||
* Wraps nodes in another node, or removes an existing wrap.
|
||||
*/
|
||||
export const toggleWrap = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const isActive = nodeIsActive(state, type, attributes)
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ import { NodeType } from 'prosemirror-model'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
import { Command } from '../types'
|
||||
|
||||
export default (typeOrName: string | NodeType, attributes: {}): Command => ({ tr, state, dispatch }) => {
|
||||
/**
|
||||
* Update attributes of a node.
|
||||
*/
|
||||
export const updateNodeAttributes = (typeOrName: string | NodeType, attributes: {}): Command => ({ tr, state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const { selection } = tr
|
||||
const { from, to } = selection
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { wrapIn } from 'prosemirror-commands'
|
||||
import { wrapIn as originalWrapIn } from 'prosemirror-commands'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import nodeIsActive from '../utils/nodeIsActive'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
export default (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
|
||||
/**
|
||||
* Wraps nodes in another node.
|
||||
*/
|
||||
export const wrapIn = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const isActive = nodeIsActive(state, type, attributes)
|
||||
|
||||
@@ -12,5 +15,5 @@ export default (typeOrName: string | NodeType, attributes = {}): Command => ({ s
|
||||
return false
|
||||
}
|
||||
|
||||
return wrapIn(type, attributes)(state, dispatch)
|
||||
return originalWrapIn(type, attributes)(state, dispatch)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { wrapInList } from 'prosemirror-schema-list'
|
||||
import { wrapInList as originalWrapInList } from 'prosemirror-schema-list'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
export default (typeOrName: string | NodeType, attrs?: {}): Command => ({ state, dispatch }) => {
|
||||
/**
|
||||
* Wrap a node in a list.
|
||||
*/
|
||||
export const wrapInList = (typeOrName: string | NodeType, attrs?: {}): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
|
||||
return wrapInList(type, attrs)(state, dispatch)
|
||||
return originalWrapInList(type, attrs)(state, dispatch)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user