2020-11-05 05:38:52 +08:00
|
|
|
import { NodeType } from 'prosemirror-model'
|
2021-02-17 01:36:37 +08:00
|
|
|
import { AnyObject, Command, RawCommands } from '../types'
|
2020-12-01 16:11:58 +08:00
|
|
|
import isNodeActive from '../helpers/isNodeActive'
|
2020-11-30 16:42:53 +08:00
|
|
|
import getNodeType from '../helpers/getNodeType'
|
2020-11-05 05:38:52 +08:00
|
|
|
|
2021-02-11 01:05:02 +08:00
|
|
|
declare module '@tiptap/core' {
|
2021-02-17 01:39:37 +08:00
|
|
|
interface Commands {
|
2021-02-16 18:27:58 +08:00
|
|
|
toggleNode: {
|
|
|
|
/**
|
|
|
|
* Toggle a node with another node.
|
|
|
|
*/
|
|
|
|
toggleNode: (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attributes?: AnyObject) => Command,
|
|
|
|
}
|
2021-02-11 01:05:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 01:36:37 +08:00
|
|
|
export const toggleNode: RawCommands['toggleNode'] = (typeOrName, toggleTypeOrName, attributes = {}) => ({ state, commands }) => {
|
2020-11-05 05:38:52 +08:00
|
|
|
const type = getNodeType(typeOrName, state.schema)
|
|
|
|
const toggleType = getNodeType(toggleTypeOrName, state.schema)
|
2021-02-10 16:59:35 +08:00
|
|
|
const isActive = isNodeActive(state, type, attributes)
|
2020-11-05 05:38:52 +08:00
|
|
|
|
|
|
|
if (isActive) {
|
2020-11-21 06:56:41 +08:00
|
|
|
return commands.setNode(toggleType)
|
2020-11-05 05:38:52 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
return commands.setNode(type, attributes)
|
|
|
|
}
|