# Tr.mapping.map(pos) in Plugin.apply returns the end of the document on change

**URL:** https://discuss.prosemirror.net/t/tr-mapping-map-pos-in-plugin-apply-returns-the-end-of-the-document-on-change/3940
**Category:** Uncategorized
**Created:** [July 30, 2021, 9:53pm UTC](https://discuss.prosemirror.net/t/tr-mapping-map-pos-in-plugin-apply-returns-the-end-of-the-document-on-change/3940 "2021-07-30T21:53:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jamis0n](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jamis0n/32/2272_2.png) [@jamis0n](https://discuss.prosemirror.net/u/jamis0n)
#### Post date: [July 30, 2021, 9:53pm UTC](https://discuss.prosemirror.net/t/tr-mapping-map-pos-in-plugin-apply-returns-the-end-of-the-document-on-change/3940/1 "2021-07-30T21:53:49Z")

</div>

I’m trying to keep track of the position for a decoration as the document changes.

```auto
apply: (tr, prevState, oldEditorState, newEditorState) => {
  const newPos = tr.mapping.map(prevState.pos)

```

For some reason, if another user types a single character into the document, the `newPos` value is always the end of the document instead of simply +1 (if the change was before it) or the same (if the change was after it).

Am I misunderstanding how [position mapping](https://prosemirror.net/docs/ref/#transform.Position_Mapping) works?

Thanks! Were a new sponsor FYI (gamma.app) 🙂 Loving Prosemirror so far!

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [July 31, 2021, 5:46am UTC](https://discuss.prosemirror.net/t/tr-mapping-map-pos-in-plugin-apply-returns-the-end-of-the-document-on-change/3940/2 "2021-07-31T05:46:07Z")

</div>

This would happen if you replace the entire document with a new document. How does your collaborative editing work, and what kind of steps are present in the transaction (`tr.steps.toJSON()`) when this occurs?

---

<div class="post-metadata">

### Author: ![philippkuehn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/philippkuehn/32/1820_2.png) [@philippkuehn](https://discuss.prosemirror.net/u/philippkuehn)
#### Post date: [July 31, 2021, 9:25am UTC](https://discuss.prosemirror.net/t/tr-mapping-map-pos-in-plugin-apply-returns-the-end-of-the-document-on-change/3940/3 "2021-07-31T09:25:52Z")

</div>

@jamis0n This is probably because you are using y.js which is replacing the doc on every incoming change. Some helper methods to map decorations are possible but it’s not done yet (in y.js or tiptap).

---

<div class="post-metadata">

### Author: ![jamis0n](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jamis0n/32/2272_2.png) [@jamis0n](https://discuss.prosemirror.net/u/jamis0n)
#### Post date: [July 31, 2021, 3:38pm UTC](https://discuss.prosemirror.net/t/tr-mapping-map-pos-in-plugin-apply-returns-the-end-of-the-document-on-change/3940/4 "2021-07-31T15:38:21Z")

</div>

Ah, thanks for pointing that out @philippkuehn ! I suspect you’re exactly right.

Any ideas for workarounds at the moment? This plugin is just adding decorations with a class for a particular node.

If the original pointer was position `10`, we just need to know if a change was made that should update that pointer to `11` (or whatever the corresponding new location is)

OH I should also mention were storing the prosemirror `node` in the plugin state as well, so another approach could be to “find this same node, if it still exists”

---

<div class="post-metadata">

### Author: ![bhl](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bhl/32/4074_2.png) [@bhl](https://discuss.prosemirror.net/u/bhl)
#### Post date: [August 1, 2021, 7:33pm UTC](https://discuss.prosemirror.net/t/tr-mapping-map-pos-in-plugin-apply-returns-the-end-of-the-document-on-change/3940/5 "2021-08-01T19:33:58Z")

</div>

> [@jamis0n](#):
>
> If the original pointer was position `10`, we just need to know if a change was made that should update that pointer to `11` (or whatever the corresponding new location is)

Does the [`DecorationSet.map`](https://prosemirror.net/docs/ref/#view.DecorationSet.map) not suffice for this usecase? What’s the reason for needing to keep a state of decoration positions outside of `DecorationSet` (e.g. asynchronous decorations, decoration cache-ing)?
