refactoring

This commit is contained in:
Philipp Kühn
2021-02-10 18:05:02 +01:00
parent f70974678b
commit 55ff908423
69 changed files with 519 additions and 527 deletions

View File

@@ -1,8 +1,14 @@
import { Command, Commands } from '../types'
/**
* Removes focus from the editor.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Removes focus from the editor.
*/
blur: () => Command,
}
}
export const blur: Commands['blur'] = () => ({ view }) => {
const element = view.dom as HTMLElement
@@ -10,9 +16,3 @@ export const blur: Commands['blur'] = () => ({ view }) => {
return true
}
declare module '@tiptap/core' {
interface Commands {
blur: () => Command,
}
}

View File

@@ -1,14 +1,14 @@
import { Command, Commands } from '../types'
/**
* Clear the whole document.
*/
export const clearContent: Commands['clearContent'] = (emitUpdate = false) => ({ commands }) => {
return commands.setContent('', emitUpdate)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Clear the whole document.
*/
clearContent: (emitUpdate: Boolean) => Command,
}
}
export const clearContent: Commands['clearContent'] = (emitUpdate = false) => ({ commands }) => {
return commands.setContent('', emitUpdate)
}

View File

@@ -1,9 +1,15 @@
import { liftTarget } from 'prosemirror-transform'
import { Command, Commands } from '../types'
/**
* Normalize nodes to a simple paragraph.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Normalize nodes to a simple paragraph.
*/
clearNodes: () => Command,
}
}
export const clearNodes: Commands['clearNodes'] = () => ({ state, tr, dispatch }) => {
const { selection } = tr
const { from, to } = selection
@@ -30,9 +36,3 @@ export const clearNodes: Commands['clearNodes'] = () => ({ state, tr, dispatch }
return true
}
declare module '@tiptap/core' {
interface Commands {
clearNodes: () => Command,
}
}

View File

@@ -1,14 +1,14 @@
import { Command, Commands } from '../types'
/**
* Define a command inline.
*/
export const command: Commands['command'] = fn => props => {
return fn(props)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Define a command inline.
*/
command: (fn: (props: Parameters<Command>[0]) => boolean) => Command,
}
}
export const command: Commands['command'] = fn => props => {
return fn(props)
}

View File

@@ -1,15 +1,15 @@
import { createParagraphNear as originalCreateParagraphNear } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Create a paragraph nearby.
*/
export const createParagraphNear: Commands['createParagraphNear'] = () => ({ state, dispatch }) => {
return originalCreateParagraphNear(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Create a paragraph nearby.
*/
createParagraphNear: () => Command,
}
}
export const createParagraphNear: Commands['createParagraphNear'] = () => ({ state, dispatch }) => {
return originalCreateParagraphNear(state, dispatch)
}

View File

@@ -1,8 +1,14 @@
import { Command, Commands, Range } from '../types'
/**
* Delete a given range.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Delete a given range.
*/
deleteRange: (range: Range) => Command,
}
}
export const deleteRange: Commands['deleteRange'] = range => ({ tr, dispatch }) => {
const { from, to } = range
@@ -12,9 +18,3 @@ export const deleteRange: Commands['deleteRange'] = range => ({ tr, dispatch })
return true
}
declare module '@tiptap/core' {
interface Commands {
deleteRange: (range: Range) => Command,
}
}

View File

@@ -1,15 +1,15 @@
import { deleteSelection as originalDeleteSelection } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Delete the selection, if there is one.
*/
export const deleteSelection: Commands['deleteSelection'] = () => ({ state, dispatch }) => {
return originalDeleteSelection(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Delete the selection, if there is one.
*/
deleteSelection: () => Command,
}
}
export const deleteSelection: Commands['deleteSelection'] = () => ({ state, dispatch }) => {
return originalDeleteSelection(state, dispatch)
}

View File

@@ -1,14 +1,14 @@
import { Command, Commands } from '../types'
/**
* Trigger enter.
*/
export const enter: Commands['enter'] = () => ({ commands }) => {
return commands.keyboardShortcut('Enter')
}
declare module '@tiptap/core' {
interface Commands {
/**
* Trigger enter.
*/
enter: () => Command,
}
}
export const enter: Commands['enter'] = () => ({ commands }) => {
return commands.keyboardShortcut('Enter')
}

View File

@@ -1,15 +1,15 @@
import { exitCode as originalExitCode } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Exit from a code block.
*/
export const exitCode: Commands['exitCode'] = () => ({ state, dispatch }) => {
return originalExitCode(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Exit from a code block.
*/
exitCode: () => Command,
}
}
export const exitCode: Commands['exitCode'] = () => ({ state, dispatch }) => {
return originalExitCode(state, dispatch)
}

View File

@@ -4,9 +4,15 @@ import { Command, Commands } from '../types'
import getMarkType from '../helpers/getMarkType'
import getMarkRange from '../helpers/getMarkRange'
/**
* Extends the text selection to the current mark.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Extends the text selection to the current mark.
*/
extendMarkRange: (typeOrName: string | MarkType) => Command,
}
}
export const extendMarkRange: Commands['extendMarkRange'] = typeOrName => ({ tr, state, dispatch }) => {
const type = getMarkType(typeOrName, state.schema)
const { doc, selection } = tr
@@ -24,9 +30,3 @@ export const extendMarkRange: Commands['extendMarkRange'] = typeOrName => ({ tr,
return true
}
declare module '@tiptap/core' {
interface Commands {
extendMarkRange: (typeOrName: string | MarkType) => Command,
}
}

View File

@@ -1,8 +1,14 @@
import { Command, Commands } from '../types'
/**
* Runs one command after the other and stops at the first which returns true.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Runs one command after the other and stops at the first which returns true.
*/
first: (commands: Command[] | ((props: Parameters<Command>[0]) => Command[])) => Command,
}
}
export const first: Commands['first'] = commands => props => {
const items = typeof commands === 'function'
? commands(props)
@@ -16,9 +22,3 @@ export const first: Commands['first'] = commands => props => {
return false
}
declare module '@tiptap/core' {
interface Commands {
first: (commands: Command[] | ((props: Parameters<Command>[0]) => Command[])) => Command,
}
}

View File

@@ -30,9 +30,15 @@ function resolveSelection(state: EditorState, position: FocusPosition = null) {
}
}
/**
* Focus the editor at the given position.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Focus the editor at the given position.
*/
focus: (position?: FocusPosition) => Command,
}
}
export const focus: Commands['focus'] = (position = null) => ({
editor,
view,
@@ -62,9 +68,3 @@ export const focus: Commands['focus'] = (position = null) => ({
return true
}
declare module '@tiptap/core' {
interface Commands {
focus: (position?: FocusPosition) => Command,
}
}

View File

@@ -17,9 +17,15 @@ function selectionToInsertionEnd(tr: Transaction, startLen: number, bias: number
tr.setSelection(Selection.near(tr.doc.resolve(end as unknown as number), bias))
}
/**
* Insert a string of HTML at the current position.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Insert a string of HTML at the current position.
*/
insertHTML: (value: string) => Command,
}
}
export const insertHTML: Commands['insertHTML'] = value => ({ tr, state, dispatch }) => {
const { selection } = tr
const element = elementFromString(value)
@@ -32,9 +38,3 @@ export const insertHTML: Commands['insertHTML'] = value => ({ tr, state, dispatc
return true
}
declare module '@tiptap/core' {
interface Commands {
insertHTML: (value: string) => Command,
}
}

View File

@@ -1,8 +1,14 @@
import { Command, Commands } from '../types'
/**
* Insert a string of text at the current position.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Insert a string of text at the current position.
*/
insertText: (value: string) => Command,
}
}
export const insertText: Commands['insertText'] = value => ({ tr, dispatch }) => {
if (dispatch) {
tr.insertText(value)
@@ -10,9 +16,3 @@ export const insertText: Commands['insertText'] = value => ({ tr, dispatch }) =>
return true
}
declare module '@tiptap/core' {
interface Commands {
insertText: (value: string) => Command,
}
}

View File

@@ -1,15 +1,15 @@
import { joinBackward as originalJoinBackward } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Join two nodes backward.
*/
export const joinBackward: Commands['joinBackward'] = () => ({ state, dispatch }) => {
return originalJoinBackward(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Join two nodes backward.
*/
joinBackward: () => Command,
}
}
export const joinBackward: Commands['joinBackward'] = () => ({ state, dispatch }) => {
return originalJoinBackward(state, dispatch)
}

View File

@@ -1,15 +1,15 @@
import { joinForward as originalJoinForward } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Join two nodes forward.
*/
export const joinForward: Commands['joinForward'] = () => ({ state, dispatch }) => {
return originalJoinForward(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Join two nodes forward.
*/
joinForward: () => Command,
}
}
export const joinForward: Commands['joinForward'] = () => ({ state, dispatch }) => {
return originalJoinForward(state, dispatch)
}

View File

@@ -56,9 +56,15 @@ function normalizeKeyName(name: string) {
return result
}
/**
* Trigger a keyboard shortcut.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Trigger a keyboard shortcut.
*/
keyboardShortcut: (name: string) => Command,
}
}
export const keyboardShortcut: Commands['keyboardShortcut'] = name => ({
editor,
view,
@@ -93,9 +99,3 @@ export const keyboardShortcut: Commands['keyboardShortcut'] = name => ({
return true
}
declare module '@tiptap/core' {
interface Commands {
keyboardShortcut: (name: string) => Command,
}
}

View File

@@ -4,9 +4,15 @@ import { Command, Commands, AnyObject } from '../types'
import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType'
/**
* Removes an existing wrap.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Removes an existing wrap.
*/
lift: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
export const lift: Commands['lift'] = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
const isActive = isNodeActive(state, type, attributes)
@@ -17,9 +23,3 @@ export const lift: Commands['lift'] = (typeOrName, attributes = {}) => ({ state,
return originalLift(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
lift: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}

View File

@@ -1,15 +1,15 @@
import { liftEmptyBlock as originalLiftEmptyBlock } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Lift block if empty.
*/
export const liftEmptyBlock: Commands['liftEmptyBlock'] = () => ({ state, dispatch }) => {
return originalLiftEmptyBlock(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Lift block if empty.
*/
liftEmptyBlock: () => Command,
}
}
export const liftEmptyBlock: Commands['liftEmptyBlock'] = () => ({ state, dispatch }) => {
return originalLiftEmptyBlock(state, dispatch)
}

View File

@@ -3,17 +3,17 @@ import { NodeType } from 'prosemirror-model'
import { Command, Commands } from '../types'
import getNodeType from '../helpers/getNodeType'
/**
* Lift the list item into a wrapping list.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Lift the list item into a wrapping list.
*/
liftListItem: (typeOrName: string | NodeType) => Command,
}
}
export const liftListItem: Commands['liftListItem'] = typeOrName => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
return originalLiftListItem(type)(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
liftListItem: (typeOrName: string | NodeType) => Command,
}
}

View File

@@ -1,15 +1,15 @@
import { newlineInCode as originalNewlineInCode } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Add a newline character in code.
*/
export const newlineInCode: Commands['newlineInCode'] = () => ({ state, dispatch }) => {
return originalNewlineInCode(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Add a newline character in code.
*/
newlineInCode: () => Command,
}
}
export const newlineInCode: Commands['newlineInCode'] = () => ({ state, dispatch }) => {
return originalNewlineInCode(state, dispatch)
}

View File

@@ -1,18 +1,18 @@
import { NodeType } from 'prosemirror-model'
import { Command, Commands, AnyObject } from '../types'
/**
* Replaces text with a node.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Replaces text with a node.
*/
replace: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
export const replace: Commands['replace'] = (typeOrName, attributes = {}) => ({ state, commands }) => {
const { from, to } = state.selection
const range = { from, to }
return commands.replaceRange(range, typeOrName, attributes)
}
declare module '@tiptap/core' {
interface Commands {
replace: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}

View File

@@ -7,9 +7,15 @@ import {
AnyObject,
} from '../types'
/**
* Replaces text with a node within a range.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Replaces text with a node within a range.
*/
replaceRange: (range: Range, typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
export const replaceRange: Commands['replaceRange'] = (range, typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
const { from, to } = range
@@ -26,9 +32,3 @@ export const replaceRange: Commands['replaceRange'] = (range, typeOrName, attrib
return true
}
declare module '@tiptap/core' {
interface Commands {
replaceRange: (range: Range, typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}

View File

@@ -3,9 +3,15 @@ import getNodeType from '../helpers/getNodeType'
import deleteProps from '../utilities/deleteProps'
import { Command, Commands } from '../types'
/**
* Resets node attributes to the default value.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Resets node attributes to the default value.
*/
resetNodeAttributes: (typeOrName: string | NodeType, attributes: string | string[]) => Command,
}
}
export const resetNodeAttributes: Commands['resetNodeAttributes'] = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
const { selection } = tr
@@ -19,9 +25,3 @@ export const resetNodeAttributes: Commands['resetNodeAttributes'] = (typeOrName,
return true
}
declare module '@tiptap/core' {
interface Commands {
resetNodeAttributes: (typeOrName: string | NodeType, attributes: string | string[]) => Command,
}
}

View File

@@ -1,8 +1,14 @@
import { Command, Commands } from '../types'
/**
* Scroll the selection into view.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Scroll the selection into view.
*/
scrollIntoView: () => Command,
}
}
export const scrollIntoView: Commands['scrollIntoView'] = () => ({ tr, dispatch }) => {
if (dispatch) {
tr.scrollIntoView()
@@ -10,9 +16,3 @@ export const scrollIntoView: Commands['scrollIntoView'] = () => ({ tr, dispatch
return true
}
declare module '@tiptap/core' {
interface Commands {
scrollIntoView: () => Command,
}
}

View File

@@ -1,15 +1,15 @@
import { selectAll as originalSelectAll } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Select the whole document.
*/
export const selectAll: Commands['selectAll'] = () => ({ state, dispatch }) => {
return originalSelectAll(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Select the whole document.
*/
selectAll: () => Command,
}
}
export const selectAll: Commands['selectAll'] = () => ({ state, dispatch }) => {
return originalSelectAll(state, dispatch)
}

View File

@@ -1,15 +1,15 @@
import { selectNodeBackward as originalSelectNodeBackward } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Select a node backward.
*/
export const selectNodeBackward: Commands['selectNodeBackward'] = () => ({ state, dispatch }) => {
return originalSelectNodeBackward(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Select a node backward.
*/
selectNodeBackward: () => Command,
}
}
export const selectNodeBackward: Commands['selectNodeBackward'] = () => ({ state, dispatch }) => {
return originalSelectNodeBackward(state, dispatch)
}

View File

@@ -1,15 +1,15 @@
import { selectNodeForward as originalSelectNodeForward } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Select a node forward.
*/
export const selectNodeForward: Commands['selectNodeForward'] = () => ({ state, dispatch }) => {
return originalSelectNodeForward(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Select a node forward.
*/
selectNodeForward: () => Command,
}
}
export const selectNodeForward: Commands['selectNodeForward'] = () => ({ state, dispatch }) => {
return originalSelectNodeForward(state, dispatch)
}

View File

@@ -1,15 +1,15 @@
import { selectParentNode as originalSelectParentNode } from 'prosemirror-commands'
import { Command, Commands } from '../types'
/**
* Select the parent node.
*/
export const selectParentNode: Commands['selectParentNode'] = () => ({ state, dispatch }) => {
return originalSelectParentNode(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Select the parent node.
*/
selectParentNode: () => Command,
}
}
export const selectParentNode: Commands['selectParentNode'] = () => ({ state, dispatch }) => {
return originalSelectParentNode(state, dispatch)
}

View File

@@ -1,9 +1,15 @@
import { TextSelection } from 'prosemirror-state'
import { AnyObject, Command, Commands } from '../types'
/**
* Replace the whole document with new content.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Replace the whole document with new content.
*/
setContent: (content: string, emitUpdate?: Boolean, parseOptions?: AnyObject) => Command,
}
}
export const setContent: Commands['setContent'] = (content, emitUpdate = false, parseOptions = {}) => ({ tr, editor, dispatch }) => {
const { createDocument } = editor
const { doc } = tr
@@ -18,9 +24,3 @@ export const setContent: Commands['setContent'] = (content, emitUpdate = false,
return true
}
declare module '@tiptap/core' {
interface Commands {
setContent: (content: string, emitUpdate?: Boolean, parseOptions?: AnyObject) => Command,
}
}

View File

@@ -3,9 +3,15 @@ import { AnyObject, Command, Commands } from '../types'
import getMarkType from '../helpers/getMarkType'
import getMarkAttributes from '../helpers/getMarkAttributes'
/**
* Add a mark with new attributes.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Add a mark with new attributes.
*/
setMark: (typeOrName: string | MarkType, attributes?: AnyObject) => Command,
}
}
export const setMark: Commands['setMark'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
const { selection } = tr
const { from, to, empty } = selection
@@ -26,9 +32,3 @@ export const setMark: Commands['setMark'] = (typeOrName, attributes = {}) => ({
return true
}
declare module '@tiptap/core' {
interface Commands {
setMark: (typeOrName: string | MarkType, attributes?: AnyObject) => Command,
}
}

View File

@@ -3,17 +3,17 @@ import { setBlockType } from 'prosemirror-commands'
import { AnyObject, Command, Commands } from '../types'
import getNodeType from '../helpers/getNodeType'
/**
* Replace a given range with a node.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Replace a given range with a node.
*/
setNode: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
export const setNode: Commands['setNode'] = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
return setBlockType(type, attributes)(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
setNode: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}

View File

@@ -3,17 +3,17 @@ import { NodeType } from 'prosemirror-model'
import { Command, Commands } from '../types'
import getNodeType from '../helpers/getNodeType'
/**
* Sink the list item down into an inner list.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Sink the list item down into an inner list.
*/
sinkListItem: (typeOrName: string | NodeType) => Command,
}
}
export const sinkListItem: Commands['sinkListItem'] = typeOrName => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
return originalSinkListItem(type)(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
sinkListItem: (typeOrName: string | NodeType) => Command,
}
}

View File

@@ -15,11 +15,7 @@ function defaultBlockAt(match: ContentMatch) {
return null
}
export interface SplitBlockOptions {
keepMarks: boolean,
}
function keepMarks(state: EditorState) {
function ensureMarks(state: EditorState) {
const marks = state.storedMarks
|| (state.selection.$to.parentOffset && state.selection.$from.marks())
@@ -28,19 +24,21 @@ function keepMarks(state: EditorState) {
}
}
/**
* Forks a new node from an existing node.
*/
export const splitBlock: Commands['splitBlock'] = (options = {}) => ({
declare module '@tiptap/core' {
interface Commands {
/**
* Forks a new node from an existing node.
*/
splitBlock: (options?: { keepMarks?: boolean }) => Command,
}
}
export const splitBlock: Commands['splitBlock'] = ({ keepMarks = true } = {}) => ({
tr,
state,
dispatch,
editor,
}) => {
const defaultOptions: SplitBlockOptions = {
keepMarks: true,
}
const config = { ...defaultOptions, ...options }
const { selection, doc } = tr
const { $from, $to } = selection
const extensionAttributes = editor.extensionManager.attributes
@@ -56,8 +54,8 @@ export const splitBlock: Commands['splitBlock'] = (options = {}) => ({
}
if (dispatch) {
if (config.keepMarks) {
keepMarks(state)
if (keepMarks) {
ensureMarks(state)
}
tr.split($from.pos).scrollIntoView()
@@ -117,8 +115,8 @@ export const splitBlock: Commands['splitBlock'] = (options = {}) => ({
}
}
if (config.keepMarks) {
keepMarks(state)
if (keepMarks) {
ensureMarks(state)
}
tr.scrollIntoView()
@@ -126,9 +124,3 @@ export const splitBlock: Commands['splitBlock'] = (options = {}) => ({
return true
}
declare module '@tiptap/core' {
interface Commands {
splitBlock: (options?: Partial<SplitBlockOptions>) => Command,
}
}

View File

@@ -10,9 +10,15 @@ import { Command, Commands } from '../types'
import getNodeType from '../helpers/getNodeType'
import getSplittedAttributes from '../helpers/getSplittedAttributes'
/**
* Splits one list item into two list items.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Splits one list item into two list items.
*/
splitListItem: (typeOrName: string | NodeType) => Command,
}
}
export const splitListItem: Commands['splitListItem'] = typeOrName => ({
tr, state, dispatch, editor,
}) => {
@@ -110,9 +116,3 @@ export const splitListItem: Commands['splitListItem'] = typeOrName => ({
return true
}
declare module '@tiptap/core' {
interface Commands {
splitListItem: (typeOrName: string | NodeType) => Command,
}
}

View File

@@ -4,9 +4,15 @@ import getNodeType from '../helpers/getNodeType'
import findParentNode from '../helpers/findParentNode'
import isList from '../helpers/isList'
/**
* Toggle between different list types.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle between different list types.
*/
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType) => Command,
}
}
export const toggleList: Commands['toggleList'] = (listTypeOrName, itemTypeOrName) => ({
editor, tr, state, dispatch, chain, commands, can,
}) => {
@@ -53,9 +59,3 @@ export const toggleList: Commands['toggleList'] = (listTypeOrName, itemTypeOrNam
return commands.wrapInList(listType)
}
declare module '@tiptap/core' {
interface Commands {
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType) => Command,
}
}

View File

@@ -3,9 +3,15 @@ import { AnyObject, Command, Commands } from '../types'
import getMarkType from '../helpers/getMarkType'
import isMarkActive from '../helpers/isMarkActive'
/**
* Toggle a mark on and off.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle a mark on and off.
*/
toggleMark: (typeOrName: string | MarkType, attributes?: AnyObject) => Command,
}
}
export const toggleMark: Commands['toggleMark'] = (typeOrName, attributes = {}) => ({ state, commands }) => {
const type = getMarkType(typeOrName, state.schema)
const isActive = isMarkActive(state, type, attributes)
@@ -16,9 +22,3 @@ export const toggleMark: Commands['toggleMark'] = (typeOrName, attributes = {})
return commands.setMark(type, attributes)
}
declare module '@tiptap/core' {
interface Commands {
toggleMark: (typeOrName: string | MarkType, attributes?: AnyObject) => Command,
}
}

View File

@@ -3,9 +3,15 @@ import { AnyObject, Command, Commands } from '../types'
import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType'
/**
* Toggle a node with another node.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle a node with another node.
*/
toggleNode: (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
export const toggleNode: Commands['toggleNode'] = (typeOrName, toggleTypeOrName, attributes = {}) => ({ state, commands }) => {
const type = getNodeType(typeOrName, state.schema)
const toggleType = getNodeType(toggleTypeOrName, state.schema)
@@ -17,9 +23,3 @@ export const toggleNode: Commands['toggleNode'] = (typeOrName, toggleTypeOrName,
return commands.setNode(type, attributes)
}
declare module '@tiptap/core' {
interface Commands {
toggleNode: (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}

View File

@@ -4,9 +4,15 @@ import { AnyObject, Command, Commands } from '../types'
import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType'
/**
* Wraps nodes in another node, or removes an existing wrap.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Wraps nodes in another node, or removes an existing wrap.
*/
toggleWrap: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
export const toggleWrap: Commands['toggleWrap'] = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
const isActive = isNodeActive(state, type, attributes)
@@ -17,9 +23,3 @@ export const toggleWrap: Commands['toggleWrap'] = (typeOrName, attributes = {})
return wrapIn(type, attributes)(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
toggleWrap: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}

View File

@@ -1,15 +1,15 @@
import { undoInputRule as originalUndoInputRule } from 'prosemirror-inputrules'
import { Command, Commands } from '../types'
/**
* Undo an input rule.
*/
export const undoInputRule: Commands['undoInputRule'] = () => ({ state, dispatch }) => {
return originalUndoInputRule(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
/**
* Undo an input rule.
*/
undoInputRule: () => Command,
}
}
export const undoInputRule: Commands['undoInputRule'] = () => ({ state, dispatch }) => {
return originalUndoInputRule(state, dispatch)
}

View File

@@ -1,8 +1,14 @@
import { Command, Commands } from '../types'
/**
* Remove all marks in the current selection.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Remove all marks in the current selection.
*/
unsetAllMarks: () => Command,
}
}
export const unsetAllMarks: Commands['unsetAllMarks'] = () => ({ tr, state, dispatch }) => {
const { selection } = tr
const { from, to, empty } = selection
@@ -21,9 +27,3 @@ export const unsetAllMarks: Commands['unsetAllMarks'] = () => ({ tr, state, disp
return true
}
declare module '@tiptap/core' {
interface Commands {
unsetAllMarks: () => Command,
}
}

View File

@@ -3,9 +3,15 @@ import { Command, Commands } from '../types'
import getMarkType from '../helpers/getMarkType'
import getMarkRange from '../helpers/getMarkRange'
/**
* Remove all marks in the current selection.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Remove all marks in the current selection.
*/
unsetMark: (typeOrName: string | MarkType) => Command,
}
}
export const unsetMark: Commands['unsetMark'] = typeOrName => ({ tr, state, dispatch }) => {
const { selection } = tr
const type = getMarkType(typeOrName, state.schema)
@@ -28,9 +34,3 @@ export const unsetMark: Commands['unsetMark'] = typeOrName => ({ tr, state, disp
return true
}
declare module '@tiptap/core' {
interface Commands {
unsetMark: (typeOrName: string | MarkType) => Command,
}
}

View File

@@ -2,9 +2,15 @@ import { NodeType } from 'prosemirror-model'
import getNodeType from '../helpers/getNodeType'
import { AnyObject, Command, Commands } from '../types'
/**
* Update attributes of a node.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Update attributes of a node.
*/
updateNodeAttributes: (typeOrName: string | NodeType, attributes: AnyObject) => Command,
}
}
export const updateNodeAttributes: Commands['updateNodeAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
const { selection } = tr
@@ -21,9 +27,3 @@ export const updateNodeAttributes: Commands['updateNodeAttributes'] = (typeOrNam
return true
}
declare module '@tiptap/core' {
interface Commands {
updateNodeAttributes: (typeOrName: string | NodeType, attributes: AnyObject) => Command,
}
}

View File

@@ -4,9 +4,15 @@ import { AnyObject, Command, Commands } from '../types'
import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType'
/**
* Wraps nodes in another node.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Wraps nodes in another node.
*/
wrapIn: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
export const wrapIn: Commands['wrapIn'] = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
const isActive = isNodeActive(state, type, attributes)
@@ -17,9 +23,3 @@ export const wrapIn: Commands['wrapIn'] = (typeOrName, attributes = {}) => ({ st
return originalWrapIn(type, attributes)(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
wrapIn: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}

View File

@@ -3,17 +3,17 @@ import { NodeType } from 'prosemirror-model'
import { AnyObject, Command, Commands } from '../types'
import getNodeType from '../helpers/getNodeType'
/**
* Wrap a node in a list.
*/
declare module '@tiptap/core' {
interface Commands {
/**
* Wrap a node in a list.
*/
wrapInList: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
export const wrapInList: Commands['wrapInList'] = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
return originalWrapInList(type, attributes)(state, dispatch)
}
declare module '@tiptap/core' {
interface Commands {
wrapInList: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}