It works. However, I would also like to decorate these inserted text. Here is what I do:
const effects = changes
.map(({ from, to }) => addAdd.of({ from, to }))
view.dispatch({ effects })
However, it doesn’t work. I guess it is because the { from, to} is the position of the old doc (before inserting the content). They can be used to insert the text but cannot be used to set the range of the decoration. am I correct? how should I fix this?
Thanks!
The fact that const changes = changes.map(...) isn’t valid JavaScript makes it a bit hard to see what you are really doing. But I assume in your code those are two different variables, and the one you use in the computation of effects isn’t the one you define above.
If you use view.state.changes to create a ChangeSet object for your changes (which you can also dispatch), you can use that to map positions from the document space before the change to after. Also probably makes sense to dispatch both the changes and the decorations in a single transaction.