improve resetNodeAttributes command

This commit is contained in:
Philipp Kühn 2020-11-18 15:18:30 +01:00
parent 4a78318a74
commit e9602626b7
6 changed files with 48 additions and 16 deletions

View File

@ -12,7 +12,7 @@
<button @click="editor.chain().focus().setTextAlign('justify').run()">
justify
</button>
<button @click="editor.chain().focus().resetNodeAttributes(['textAlign']).run()">
<button @click="editor.chain().focus().unsetTextAlign().run()">
set default
</button>
<editor-content :editor="editor" />

View File

@ -0,0 +1,18 @@
import { NodeType } from 'prosemirror-model'
import getNodeType from '../utils/getNodeType'
import deleteProps from '../utils/deleteProps'
import { Command } from '../types'
export default (typeOrName: string | NodeType, attributes: string[]): Command => ({ 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, deleteProps(node.attrs, attributes))
}
})
return true
}

View File

@ -1,22 +1,16 @@
import { NodeType } from 'prosemirror-model'
import getNodeType from '../utils/getNodeType'
import deleteProps from '../utils/deleteProps'
import { Command } from '../types'
export default (attributeNames: string[] = []): Command => ({ tr, state, dispatch }) => {
export default (typeOrName: string | NodeType, attributes: string | string[]): Command => ({ 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.isText) {
attributeNames.forEach(name => {
const attribute = node.type.spec.attrs?.[name]
const defaultValue = attribute?.default
if (attribute && defaultValue !== undefined && dispatch) {
tr.setNodeMarkup(pos, undefined, {
...node.attrs,
[name]: defaultValue,
})
}
})
if (node.type === type && dispatch) {
tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes))
}
})

View File

@ -91,7 +91,7 @@ export const Commands = Extension.create({
*/
removeMarks,
/**
* Resets all node attributes to the default value.
* Resets node attributes to the default value.
*/
resetNodeAttributes,
/**

View File

@ -0,0 +1,20 @@
/**
* Remove a property or an array of properties from an object
* @param obj Object
* @param key Key to remove
*/
export default function deleteProps(obj: { [key: string ]: any }, propOrProps: string | string[]) {
const props = typeof propOrProps === 'string'
? [propOrProps]
: propOrProps
return Object
.keys(obj)
.reduce((newObj: { [key: string ]: any }, prop) => {
if (!props.includes(prop)) {
newObj[prop] = obj[prop]
}
return newObj
}, {})
}

View File

@ -48,7 +48,7 @@ const TextAlign = Extension.create({
* Unset the text align attribute
*/
unsetTextAlign: (): Command => ({ commands }) => {
return this.options.types.every(type => commands.updateNodeAttributes(type, { textAlign: null }))
return this.options.types.every(type => commands.resetNodeAttributes(type, 'textAlign'))
},
}
},