mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-28 07:40:13 +08:00
improve resetNodeAttributes command
This commit is contained in:
parent
4a78318a74
commit
e9602626b7
@ -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" />
|
||||
|
18
packages/core/src/commands/removeNodeAttributes.ts
Normal file
18
packages/core/src/commands/removeNodeAttributes.ts
Normal 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
|
||||
}
|
@ -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))
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -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,
|
||||
/**
|
||||
|
20
packages/core/src/utils/deleteProps.ts
Normal file
20
packages/core/src/utils/deleteProps.ts
Normal 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
|
||||
}, {})
|
||||
}
|
@ -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'))
|
||||
},
|
||||
}
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user