-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtoggleWrap.ts
30 lines (25 loc) · 979 Bytes
/
toggleWrap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { NodeType } from '@tiptap/pm/model'
import { getNodeType } from '../helpers/getNodeType.js'
import { isNodeActive } from '../helpers/isNodeActive.js'
import { RawCommands } from '../types.js'
declare module '@tiptap/core' {
interface Commands<ReturnType> {
toggleWrap: {
/**
* Wraps nodes in another node, or removes an existing wrap.
* @param typeOrName The type or name of the node.
* @param attributes The attributes of the node.
* @example editor.commands.toggleWrap('blockquote')
*/
toggleWrap: (typeOrName: string | NodeType, attributes?: Record<string, any>) => ReturnType
}
}
}
export const toggleWrap: RawCommands['toggleWrap'] = (typeOrName, attributes = {}) => ({ state, commands }) => {
const type = getNodeType(typeOrName, state.schema)
const isActive = isNodeActive(state, type, attributes)
if (isActive) {
return commands.lift(type)
}
return commands.wrapIn(type, attributes)
}