I’m trying to generate a step which changes all matching nodes with a chang attr. It’s running through Tiptap, but I don’t see how that changes what I’m observing.
import { Step, StepResult } from 'prosemirror-transform'
class SetAllNodeAttr extends Step {
constructor(change, nodeName, stepType = 'SetAllNodeAttr') {
if (!nodeName) throw Error('NoNodeName')
super()
this.stepType = stepType
this.nodeName = nodeName
this.change = change
}
apply(doc) {
this.prevValue = []
let fragment = doc.content
for (const node of fragment.content) {
if (node.type.name === this.nodeName) {
this.prevValue.push(
Object.keys(this.change)
.reduce((acc, key) => {
acc[key] = node.attrs[key]
return acc
}, {})
)
node.attrs = {
...node.attrs,
...this.change
}
let index = fragment.content.indexOf(node)
fragment.replaceChild(index, node)
}
}
doc.content = fragment
return StepResult.ok(doc)
}
invert() {
return this.prevValue.map((change) => {
new SetAllNodeAttr(
change,
this.nodeName,
'revertSetAllNodeAttr'
)
})
}
map() {
return this;
}
toJSON() {
return {
stepType: this.stepType,
change: this.change,
nodeName: this.nodeName
}
}
static jsonID(id, stepClass) {
console.log('jsonID', id, stepClass)
}
static fromJSON(schema, json) {
return new SetAllNodeAttr(
json.change,
json.nodeName,
json.stepType
)
}
}
The step appears to apply after changes, but nothing rerenders. When looking at the Doc and some other comments, is there something else that needs to occur to get the transaction to finish?
Also, in the docs, there’s some language like:
static jsonID(id: string, stepClass: constructor) To be able to serialize steps to JSON, each step needs a string ID to attach to its JSON representation. Use this method to register an ID for your step classes. Try to pick something that’s unlikely to clash with steps from other modules.
I don’t know what registering in this context means. Any support would help.