What's the best way to restore marks?

I want to restore marks after calling transaction.setNodeAttribute(). Consider this code:

new Plugin({
  key: new PluginKey("textDirection"),
  appendTransaction: (transactions, oldState, newState) => {
    let modified = false;
    const tr = newState.tr;

    newState.doc.descendants((node, pos) => {
      if (node.type.name === "paragraph") {
        // Resets marks
        tr.setNodeAttribute(pos, "dir", "rtl");

        // I need to restore marks here

        modified = true;

    return modified ? tr : null;

There are multiple methods that apparently can do this:

Which one should I use in this situation?

For some reason, setStoredMarks and ensureMarks restore the marks just for the next character, and the second character I type won’t have any marks. I don’t understand why. The only thing that has worked for me is this:

const marks = tr.storedMarks || [];
tr.setNodeAttribute(pos, "dir", "rtl");
for (const mark of marks) {

Please let me know if there are better ways.