mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-01 06:37:51 +08:00
32 lines
869 B
TypeScript
32 lines
869 B
TypeScript
import { NodeType } from 'prosemirror-model'
|
|
import getNodeType from '../helpers/getNodeType'
|
|
import { AnyObject, Command, Commands } from '../types'
|
|
|
|
declare module '@tiptap/core' {
|
|
interface AllCommands {
|
|
updateNodeAttributes: {
|
|
/**
|
|
* 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
|
|
const { from, to } = selection
|
|
|
|
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
if (node.type === type && dispatch) {
|
|
tr.setNodeMarkup(pos, undefined, {
|
|
...node.attrs,
|
|
...attributes,
|
|
})
|
|
}
|
|
})
|
|
|
|
return true
|
|
}
|