Keep original's annotation to an updated transaction

Hello,

I have some questions related to annotation topic:

1. How can I update a transaction keeping its annotation’s origin value?
2. How can I detect the current transaction’s annotation? Is it like this?

tr.annotation(Transaction.userEvent)

Let me give you some context. I am trying to update a ‘delete.selection’ transaction, reducing the selection area of the content that will be deleted. To do this, I am using

EditorState.transactionFilter.of((tr:Transaction) => {
if(tr.isUserEvent('delete.selection')){
...
}
})

and inside of it, I am using

return tr.startState.update({changes:{from:range.from, to: range.to}}

But I realize that the annotation information is missing when I do this. After reading the docs, and this forum’s topic (https://discuss.codemirror.net/t/using-annotations-to-differentiate-origin-of-transaction/3224) I could achieve the following result:

return tr.startState.update(
{  changes:{
     from:range.from, to: range.to
  },
 annotations: Annotation.define<string>().of('delete.selection')
}

But I realize that when I check if tr.isUserEvent('delete') to the transaction updated, it returns false. It doesn’t recognize the userEvent. Probably because I am creating a custom userEvent instead of a real ‘delete.selection’.

I just realized that if I use annotations: Transaction.userEvent.of('delete.selection') it works.

But I would like to have this questions clear in my mind:

How can I update a transaction keeping its annotation’s origin value? Is it the code below right?

return tr.startState.update(
{  changes:{
     from:range.from, to: range.to
  },
 annotations: Transaction.userEvent.of(tr.annotation(Transaction.userEvent))
}

You can’t copy all annotations when replacing a selection, currently. But I think that actually is a reasonable thing, since doing that wouldn’t be safe anyway (for example, the transactions created by undo/redo will include a new undo history object in an annotation, which would not be valid for a transaction that has different changes). You may be able to do what you’re trying to do with a change filter, or just add only userEvent and hope you’re not dropping any important annotations.

1 Like