Decoration plugin updating decorations in node's children

I have a decoration widget plugin that updates the decoration set, removing some when they need to be updated with augmented html (as where they are put and their html contents can change as typing occurs). However, I am filtering the doc.descendents loop to only look at the nodes of a particular type (my main paragraph nodes, not their text node children).

So, in my code I create new widget decorations to replace those that need updating, which works great when the decorations are at the node level being looked at (i.e. my paragraph nodes).

Often, however, I need to add a decoration in the middle of the node’s text contents (in effect breaking the text mid-node up with some html), so my widget creation call (Decoration.widget(newpos, thediv,{key: pagenum,side: 0, pos: pos})) might have a newpos parameter that is within a child node.

When I loop through the first time (there being no decorations at all yet), the new widgets with a position that falls in a child node (i.e. the text node containing the paragraph’s contents) get created just fine. However, a typing occurs, trying to update them (removing and replacing with the same global position) does then not work. It does not create a widget at all. I need to do that because even those decorations might need updated html, but having the same document position of the old decoration. As there is not a decoration.update method, creating new decorations seems the way to go.

I get that my loop is skipping the text nodes (I never want to process them directly but only in the context of their parent paragraph where all my positioning calculations are done), which might place the new decoration in the middle of that text block somewhere, but I am confused as to how to get this work. That is, to find old decoration inside the paren’t children, delete it, and then to have the new Decoration.widget function create a new decoration at the same spot to replace the old one, with new html to be inserted there. As mentioned, updating via the remove and add works great with decorations at the level of the node being inspected (the paragraph parent), just not the ones with positions inside the text child, when a decoration is already there at that position.

SO…

In my loop, what can I do so that attempting to create a decoration in the text child will cause a new decoration to be created when the position is the same? Note that I tested adding a small random number to the global newpos variable each time throught the loop and the new decoration is being created and updated perfectly when the random number is not the same as the decorations’ from variable was previously (thus having a newpos that does not correspond to an existing decoration).

When the view finds a widget in a place that already has a widget with the same key or whose creation function is the exact same function, it assumes that it doesn’t need to update it, because it’s already there. I think your use of key might be the issue here—if you want the widget to actually be redrawn, you shouldn’t pass in the same key. Probably you can just avoid passing in any key at all.

Thanks so much, Marjin, that solved the issue. I move the previous key to a new spec variable and it now works perfectly. I will post a URL link soon (when it works perfectly in all cases) so you can check out real, effective and very fast pgaination using ProseMirror, even with 100 page documents. At least for text based documents like screenplays (my use case) that has widow/orphan control, paragraph splitting based on sentences (no sentences orphaned mid sentence) aws well as splitting up dialogue when it spans two pages. I could add forced page breaks in a trivial manner as well in my editor by just inserting a new decoration, a no brainer.