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

@ -9,6 +9,9 @@ export interface IframeOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Add an iframe
*/
setIframe: (options: { src: string }) => Command,
}
}
@ -58,9 +61,6 @@ export default Node.create({
addCommands() {
return {
/**
* Add an iframe
*/
setIframe: (options: { src: string }) => ({ tr, dispatch }) => {
const { selection } = tr
const node = this.type.create(options)

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,
}
}

View File

@ -9,8 +9,17 @@ export interface BlockquoteOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set a blockquote node
*/
setBlockquote: () => Command,
/**
* Toggle a blockquote node
*/
toggleBlockquote: () => Command,
/**
* Unset a blockquote node
*/
unsetBlockquote: () => Command,
}
}
@ -43,21 +52,12 @@ export const Blockquote = Node.create({
addCommands() {
return {
/**
* Set a blockquote node
*/
setBlockquote: () => ({ commands }) => {
return commands.wrapIn('blockquote')
},
/**
* Toggle a blockquote node
*/
toggleBlockquote: () => ({ commands }) => {
return commands.toggleWrap('blockquote')
},
/**
* Unset a blockquote node
*/
unsetBlockquote: () => ({ commands }) => {
return commands.lift('blockquote')
},

View File

@ -14,8 +14,17 @@ export interface BoldOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set a bold mark
*/
setBold: () => Command,
/**
* Toggle a bold mark
*/
toggleBold: () => Command,
/**
* Unset a bold mark
*/
unsetBold: () => Command,
}
}
@ -54,21 +63,12 @@ export const Bold = Mark.create({
addCommands() {
return {
/**
* Set a bold mark
*/
setBold: () => ({ commands }) => {
return commands.setMark('bold')
},
/**
* Toggle a bold mark
*/
toggleBold: () => ({ commands }) => {
return commands.toggleMark('bold')
},
/**
* Unset a bold mark
*/
unsetBold: () => ({ commands }) => {
return commands.unsetMark('bold')
},

View File

@ -9,6 +9,9 @@ export interface BulletListOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle a bullet list
*/
toggleBulletList: () => Command,
}
}
@ -38,9 +41,6 @@ export const BulletList = Node.create({
addCommands() {
return {
/**
* Toggle a bullet list
*/
toggleBulletList: () => ({ commands }) => {
return commands.toggleList('bulletList', 'listItem')
},

View File

@ -10,7 +10,13 @@ export interface CodeBlockOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set a code block
*/
setCodeBlock: (attributes?: { language: string }) => Command,
/**
* Toggle a code block
*/
toggleCodeBlock: (attributes?: { language: string }) => Command,
}
}
@ -81,15 +87,9 @@ export const CodeBlock = Node.create({
addCommands() {
return {
/**
* Set a code block
*/
setCodeBlock: attributes => ({ commands }) => {
return commands.setNode('codeBlock', attributes)
},
/**
* Toggle a code block
*/
toggleCodeBlock: attributes => ({ commands }) => {
return commands.toggleNode('codeBlock', 'paragraph', attributes)
},

View File

@ -14,8 +14,17 @@ export interface CodeOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set a code mark
*/
setCode: () => Command,
/**
* Toggle inline code
*/
toggleCode: () => Command,
/**
* Unset a code mark
*/
unsetCode: () => Command,
}
}
@ -44,21 +53,12 @@ export const Code = Mark.create({
addCommands() {
return {
/**
* Set a code mark
*/
setCode: () => ({ commands }) => {
return commands.setMark('code')
},
/**
* Toggle inline code
*/
toggleCode: () => ({ commands }) => {
return commands.toggleMark('code')
},
/**
* Unset a code mark
*/
unsetCode: () => ({ commands }) => {
return commands.unsetMark('code')
},

View File

@ -10,6 +10,9 @@ export interface CollaborationCursorOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Update details of the current user
*/
user: (attributes: AnyObject) => Command,
}
}
@ -50,9 +53,6 @@ export const CollaborationCursor = Extension.create({
addCommands() {
return {
/**
* Update details of the current user
*/
user: attributes => () => {
this.options.user = attributes

View File

@ -8,7 +8,13 @@ import {
declare module '@tiptap/core' {
interface Commands {
/**
* Undo recent changes
*/
undo: () => Command,
/**
* Reapply reverted changes
*/
redo: () => Command,
}
}
@ -39,17 +45,11 @@ export const Collaboration = Extension.create({
addCommands() {
return {
/**
* Undo recent changes
*/
undo: () => ({ tr, state }) => {
tr.setMeta('preventDispatch', true)
return undo(state)
},
/**
* Reapply reverted changes
*/
redo: () => ({ tr, state }) => {
tr.setMeta('preventDispatch', true)

View File

@ -7,7 +7,13 @@ type FontFamilyOptions = {
declare module '@tiptap/core' {
interface Commands {
/**
* Set the font family
*/
setFontFamily: (fontFamily: string) => Command,
/**
* Unset the font family
*/
unsetFontFamily: () => Command,
}
}
@ -46,17 +52,11 @@ export const FontFamily = Extension.create({
addCommands() {
return {
/**
* Set the font family
*/
setFontFamily: fontFamily => ({ chain }) => {
return chain()
.setMark('textStyle', { fontFamily })
.run()
},
/**
* Unset the font family
*/
unsetFontFamily: () => ({ chain }) => {
return chain()
.setMark('textStyle', { fontFamily: null })

View File

@ -9,6 +9,9 @@ export interface HardBreakOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Add a hard break
*/
setHardBreak: () => Command,
}
}
@ -39,9 +42,6 @@ export const HardBreak = Node.create({
addCommands() {
return {
/**
* Add a hard break
*/
setHardBreak: () => ({ commands, state, dispatch }) => {
return commands.first([
() => exitCode(state, dispatch),

View File

@ -12,7 +12,13 @@ export interface HeadingOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set a heading node
*/
setHeading: (attributes: { level: Level }) => Command,
/**
* Toggle a heading node
*/
toggleHeading: (attributes: { level: Level }) => Command,
}
}
@ -59,9 +65,6 @@ export const Heading = Node.create({
addCommands() {
return {
/**
* Set a heading node
*/
setHeading: attributes => ({ commands }) => {
if (!this.options.levels.includes(attributes.level)) {
return false
@ -69,9 +72,6 @@ export const Heading = Node.create({
return commands.setNode('heading', attributes)
},
/**
* Toggle a heading node
*/
toggleHeading: attributes => ({ commands }) => {
if (!this.options.levels.includes(attributes.level)) {
return false

View File

@ -15,8 +15,17 @@ export interface HighlightOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set a highlight mark
*/
setHighlight: (attributes?: { color: string }) => Command,
/**
* Toggle a highlight mark
*/
toggleHighlight: (attributes?: { color: string }) => Command,
/**
* Unset a highlight mark
*/
unsetHighlight: () => Command,
}
}
@ -73,21 +82,12 @@ export const Highlight = Mark.create({
addCommands() {
return {
/**
* Set a highlight mark
*/
setHighlight: attributes => ({ commands }) => {
return commands.setMark('highlight', attributes)
},
/**
* Toggle a highlight mark
*/
toggleHighlight: attributes => ({ commands }) => {
return commands.toggleMark('highlight', attributes)
},
/**
* Unset a highlight mark
*/
unsetHighlight: () => ({ commands }) => {
return commands.unsetMark('highlight')
},

View File

@ -8,7 +8,13 @@ export interface HistoryOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Undo recent changes
*/
undo: () => Command,
/**
* Reapply reverted changes
*/
redo: () => Command,
}
}
@ -23,15 +29,9 @@ export const History = Extension.create({
addCommands() {
return {
/**
* Undo recent changes
*/
undo: () => ({ state, dispatch }) => {
return undo(state, dispatch)
},
/**
* Reapply reverted changes
*/
redo: () => ({ state, dispatch }) => {
return redo(state, dispatch)
},

View File

@ -13,6 +13,9 @@ export interface HorizontalRuleOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Add a horizontal rule
*/
setHorizontalRule: () => Command,
}
}
@ -38,9 +41,6 @@ export const HorizontalRule = Node.create({
addCommands() {
return {
/**
* Add a horizontal rule
*/
setHorizontalRule: () => ({ tr, dispatch }) => {
if (dispatch) {
tr.replaceSelectionWith(this.type.create())

View File

@ -14,6 +14,9 @@ export interface ImageOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Add an image
*/
setImage: (options: { src: string, alt?: string, title?: string }) => Command,
}
}
@ -66,9 +69,6 @@ export const Image = Node.create({
addCommands() {
return {
/**
* Add an image
*/
setImage: options => ({ tr, dispatch }) => {
const { selection } = tr
const node = this.type.create(options)

View File

@ -14,8 +14,17 @@ export interface ItalicOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set an italic mark
*/
setItalic: () => Command,
/**
* Toggle an italic mark
*/
toggleItalic: () => Command,
/**
* Unset an italic mark
*/
unsetItalic: () => Command,
}
}
@ -53,21 +62,12 @@ export const Italic = Mark.create({
addCommands() {
return {
/**
* Set an italic mark
*/
setItalic: () => ({ commands }) => {
return commands.setMark('italic')
},
/**
* Toggle an italic mark
*/
toggleItalic: () => ({ commands }) => {
return commands.toggleMark('italic')
},
/**
* Unset an italic mark
*/
unsetItalic: () => ({ commands }) => {
return commands.unsetMark('italic')
},

View File

@ -15,8 +15,17 @@ export interface LinkOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set a link mark
*/
setLink: (attributes: { href: string, target?: string }) => Command,
/**
* Toggle a link mark
*/
toggleLink: (attributes: { href: string, target?: string }) => Command,
/**
* Unset a link mark
*/
unsetLink: () => Command,
}
}
@ -60,21 +69,12 @@ export const Link = Mark.create({
addCommands() {
return {
/**
* Set a link mark
*/
setLink: attributes => ({ commands }) => {
return commands.setMark('link', attributes)
},
/**
* Toggle a link mark
*/
toggleLink: attributes => ({ commands }) => {
return commands.toggleMark('link', attributes)
},
/**
* Unset a link mark
*/
unsetLink: () => ({ commands }) => {
return commands.unsetMark('link')
},

View File

@ -9,6 +9,9 @@ export interface OrderedListOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle an ordered list
*/
toggleOrderedList: () => Command,
}
}
@ -57,9 +60,6 @@ export const OrderedList = Node.create({
addCommands() {
return {
/**
* Toggle an ordered list
*/
toggleOrderedList: () => ({ commands }) => {
return commands.toggleList('orderedList', 'listItem')
},

View File

@ -8,6 +8,9 @@ export interface ParagraphOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle a paragraph
*/
setParagraph: () => Command,
}
}
@ -35,9 +38,6 @@ export const Paragraph = Node.create({
addCommands() {
return {
/**
* Toggle a paragraph
*/
setParagraph: () => ({ commands }) => {
return commands.toggleNode('paragraph', 'paragraph')
},

View File

@ -14,8 +14,17 @@ export interface StrikeOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set a strike mark
*/
setStrike: () => Command,
/**
* Toggle a strike mark
*/
toggleStrike: () => Command,
/**
* Unset a strike mark
*/
unsetStrike: () => Command,
}
}
@ -53,21 +62,12 @@ export const Strike = Mark.create({
addCommands() {
return {
/**
* Set a strike mark
*/
setStrike: () => ({ commands }) => {
return commands.setMark('strike')
},
/**
* Toggle a strike mark
*/
toggleStrike: () => ({ commands }) => {
return commands.toggleMark('strike')
},
/**
* Unset a strike mark
*/
unsetStrike: () => ({ commands }) => {
return commands.unsetMark('strike')
},

View File

@ -8,6 +8,9 @@ export interface TaskListOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle a task list
*/
toggleTaskList: () => Command,
}
}
@ -38,9 +41,6 @@ export const TaskList = Node.create({
addCommands() {
return {
/**
* Toggle a task list
*/
toggleTaskList: () => ({ commands }) => {
return commands.toggleList('taskList', 'taskItem')
},

View File

@ -8,7 +8,13 @@ type TextAlignOptions = {
declare module '@tiptap/core' {
interface Commands {
/**
* Set the text align attribute
*/
setTextAlign: (alignment: string) => Command,
/**
* Unset the text align attribute
*/
unsetTextAlign: () => Command,
}
}
@ -43,9 +49,6 @@ export const TextAlign = Extension.create({
addCommands() {
return {
/**
* Set the text align attribute
*/
setTextAlign: (alignment: string) => ({ commands }) => {
if (!this.options.alignments.includes(alignment)) {
return false
@ -53,9 +56,6 @@ export const TextAlign = Extension.create({
return this.options.types.every(type => commands.updateNodeAttributes(type, { textAlign: alignment }))
},
/**
* Unset the text align attribute
*/
unsetTextAlign: () => ({ commands }) => {
return this.options.types.every(type => commands.resetNodeAttributes(type, 'textAlign'))
},

View File

@ -13,6 +13,9 @@ export interface TextStyleOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Remove spans without inline style attributes.
*/
removeEmptyTextStyle: () => Command,
}
}
@ -47,9 +50,6 @@ export const TextStyle = Mark.create({
addCommands() {
return {
/**
* Remove spans without inline style attributes.
*/
removeEmptyTextStyle: () => ({ state, commands }) => {
const attributes = getMarkAttributes(state, this.type)
const hasStyles = Object.entries(attributes).every(([, value]) => !!value)

View File

@ -8,8 +8,17 @@ export interface UnderlineOptions {
declare module '@tiptap/core' {
interface Commands {
/**
* Set an underline mark
*/
setUnderline: () => Command,
/**
* Toggle an underline mark
*/
toggleUnderline: () => Command,
/**
* Unset an underline mark
*/
unsetUnderline: () => Command,
}
}
@ -38,21 +47,12 @@ export const Underline = Mark.create({
addCommands() {
return {
/**
* Set an underline mark
*/
setUnderline: () => ({ commands }) => {
return commands.setMark('underline')
},
/**
* Toggle an underline mark
*/
toggleUnderline: () => ({ commands }) => {
return commands.toggleMark('underline')
},
/**
* Unset an underline mark
*/
unsetUnderline: () => ({ commands }) => {
return commands.unsetMark('underline')
},