Files
tiptap/packages/core/src/extensions/resetNodeAttributes.ts
Philipp Kühn 389937c32f refactoring
2020-11-02 16:23:43 +01:00

37 lines
995 B
TypeScript

import { Command } from '../Editor'
import { createExtension } from '../Extension'
export const ResetNodeAttributes = createExtension({
addCommands() {
return {
resetNodeAttributes: (attributeNames: string[] = []): Command => ({ tr, state, dispatch }) => {
const { selection } = tr
const { from, to } = selection
state.doc.nodesBetween(from, to, (node, pos) => {
if (!node.type.isText) {
attributeNames.forEach(name => {
const attribute = node.type.spec.attrs?.[name]
const defaultValue = attribute?.default
if (attribute && defaultValue !== undefined && dispatch) {
tr.setNodeMarkup(pos, undefined, {
[name]: defaultValue,
})
}
})
}
})
return true
},
}
},
})
declare module '../Editor' {
interface AllExtensions {
ResetNodeAttributes: typeof ResetNodeAttributes,
}
}