Skip to content

Commit

Permalink
feat: add updateAttributes() command, deprecate updateNodeAttributes(),
Browse files Browse the repository at this point in the history
fix #254
  • Loading branch information
philippkuehn committed Apr 7, 2021
1 parent e61b2a5 commit aac32b4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/src/docPages/api/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Have a look at all of the core commands listed below. They should give you a goo
| .undoInputRule() | Undo an input rule. |
| .unsetAllMarks() | Remove all marks in the current selection. |
| .unsetMark() | Remove a mark in the current selection. |
| .updateNodeAttributes() | Update attributes of a node. |
| .updateAttributes() | Update attributes of a node or mark. |

### Lists
| Command | Description |
Expand Down
66 changes: 66 additions & 0 deletions packages/core/src/commands/updateAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { NodeType, MarkType } from 'prosemirror-model'
import getNodeType from '../helpers/getNodeType'
import getMarkType from '../helpers/getMarkType'
import getSchemaTypeNameByName from '../helpers/getSchemaTypeNameByName'
import { AnyObject, Command, RawCommands } from '../types'

declare module '@tiptap/core' {
interface Commands {
updateAttributes: {
/**
* Update attributes of a node or mark.
*/
updateAttributes: (typeOrName: string | NodeType | MarkType, attributes: AnyObject) => Command,
}
}
}

export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
let nodeType: NodeType | null = null
let markType: MarkType | null = null

const schemaType = getSchemaTypeNameByName(
typeof typeOrName === 'string'
? typeOrName
: typeOrName.name,
state.schema,
)

if (!schemaType) {
return false
}

if (schemaType === 'node') {
nodeType = getNodeType(typeOrName as NodeType, state.schema)
}

if (schemaType === 'mark') {
markType = getMarkType(typeOrName as MarkType, state.schema)
}

if (dispatch) {
tr.selection.ranges.forEach(range => {
state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
if (nodeType && nodeType === node.type) {
tr.setNodeMarkup(pos, undefined, {
...node.attrs,
...attributes,
})
}

if (markType && node.marks.length) {
node.marks.forEach(mark => {
if (markType === mark.type) {
tr.addMark(pos, pos + node.nodeSize, markType.create({
...mark.attrs,
...attributes,
}))
}
})
}
})
})
}

return true
}
2 changes: 2 additions & 0 deletions packages/core/src/commands/updateNodeAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ declare module '@tiptap/core' {
}

export const updateNodeAttributes: RawCommands['updateNodeAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
console.warn('[tiptap warn]: updateNodeAttributes() is deprecated. please use updateAttributes() instead.')

const type = getNodeType(typeOrName, state.schema)
const { selection } = tr
const { ranges } = selection
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/extensions/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import * as toggleWrap from '../commands/toggleWrap'
import * as undoInputRule from '../commands/undoInputRule'
import * as unsetAllMarks from '../commands/unsetAllMarks'
import * as unsetMark from '../commands/unsetMark'
import * as updateAttributes from '../commands/updateAttributes'
import * as updateNodeAttributes from '../commands/updateNodeAttributes'
import * as wrapIn from '../commands/wrapIn'
import * as wrapInList from '../commands/wrapInList'
Expand Down Expand Up @@ -91,6 +92,7 @@ export { toggleWrap }
export { undoInputRule }
export { unsetAllMarks }
export { unsetMark }
export { updateAttributes }
export { updateNodeAttributes }
export { wrapIn }
export { wrapInList }
Expand Down Expand Up @@ -144,6 +146,7 @@ export const Commands = Extension.create({
...undoInputRule,
...unsetAllMarks,
...unsetMark,
...updateAttributes,
...updateNodeAttributes,
...wrapIn,
...wrapInList,
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-text-align/src/text-align.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const TextAlign = Extension.create<TextAlignOptions>({
return false
}

return this.options.types.every(type => commands.updateNodeAttributes(type, { textAlign: alignment }))
return this.options.types.every(type => commands.updateAttributes(type, { textAlign: alignment }))
},
unsetTextAlign: () => ({ commands }) => {
return this.options.types.every(type => commands.resetNodeAttributes(type, 'textAlign'))
Expand Down

0 comments on commit aac32b4

Please sign in to comment.