refactoring
This commit is contained in:
36
packages/core/src/extensions/resetNodeAttributes.ts
Normal file
36
packages/core/src/extensions/resetNodeAttributes.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user