Can I access "previous content" for a specific node in appendTransaction?

I would like to cache my marks when a user removes the content from a paragraph node. This is so that I can re-apply the marks when they add content again.

I’m thinking that I would do something like:

  • In an appendTransaction handler
  • Iterate over the new state’s doc content
  • For each empty paragraph node
    • Check if it was empty in the old state
    • If it wasn’t, grab the marks from position 0 of the node, and cache them as an attr on the paragraph node

When the user adds content, I would need to do exactly the opposite.

  • Iterate over the new state’s doc content
  • For each non-empty node
    • Check if it was empty in the old state
    • If it was, grab the cached marks and apply them

I’m not sure how to make sure I’m looking at the same node in both documents. I am considering using eq to compare the different nodes, but that feels terrible and like a Big-O nightmare.

If there are any better ideas how to deal with this, I’m open to them!

Thanks

The default behavior when deleting text already does something related to this (if you backspace out a single emphasized character, the stored marks will be set to emphasis).

I’m not sure what the use case would be for removing and then re-inserting the same content.

Document manipulation is usually fast, and unless you’re doing this a lot, being linear to document size is unlikely to be a real problem (the weight of the DOM for a huge document will become an issue before your code’s performance).

Thanks for the response @marijn. I serialize my document into a storable structure, and I need to be able to serialize the marks that empty paragraphs should have.

A user can delete the text from (or never have typed text into) a paragraph, then save the page, then come back to it at a later date and type in that empty paragraph. I need to use the correct marks in this case.

The only way to do this is to serialize the marks.

Do you have a better recommendation for how I should do it? I have to do it one way or the other, and I don’t know how to do it at the moment :frowning:

Any info regarding the original scenario or the a better one is really appreciated. It almost sounds like you are saying I should use eq to match elements in the old state against elements in the new state. Am I understanding that right?

Thanks again!

Ah, you want to store text style for empty paragraphs, to make it persist even when the selection moves away. I’m not sure that’s a great idea in general, but if you want to do something like that storing them in the paragraph’s attributes is probably the way to go. I don’t see any way in which comparing nodes with eq is going to be helpful here.

@marijn thanks again. Yes, that is what I said I am trying to do in my question:

  • If it wasn’t, grab the marks from position 0 of the node, and cache them as an attr on the paragraph node

Maybe I am not asking the correct question here. How would you find the marks that need to be stored?

The only way I know of is to look at the old state and find the marks that were applied to the text in the paragraph. Is there a better way?

Again, I only want to do this if the paragraph that is currently selected, but had text in the previous state.

The reason I had though of eq is, I don’t how how else to make sure I am looking at the correct paragraph in the previous state while in an appendTransaction.

Please help me achieve this, I’m in a time crunch here. (I realize that the time crunch is neither your responsibility nor problem, but you are my lifeline to getting it done and I appreciate your help).

Thank you.

No, that’s how you do it. But don’t scan the entire document, just the part where that paragraph is actually located (using positions from the step maps in the transaction).

Okay, thanks @marijn. Step maps are something I haven’t dug into yet…so maybe it is time to understand them.

Thanks again, you are awesome!