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 { toggleWrap: { /** * Wraps nodes in another node, or removes an existing wrap. */ toggleWrap: (typeOrName: string | NodeType, attributes?: Record) => 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) }