tiptap/packages/core/src/commands/updateNodeAttributes.ts

24 lines
629 B
TypeScript
Raw Normal View History

import { NodeType } from 'prosemirror-model'
2020-11-30 16:42:53 +08:00
import getNodeType from '../helpers/getNodeType'
2020-11-17 04:42:35 +08:00
import { Command } from '../types'
2020-11-05 05:38:52 +08:00
2020-11-18 23:43:27 +08:00
/**
* Update attributes of a node.
*/
export const updateNodeAttributes = (typeOrName: string | NodeType, attributes: {}): Command => ({ tr, state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
2020-11-05 05:38:52 +08:00
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,
})
2020-11-05 05:38:52 +08:00
}
})
return true
}