# Appending one block to another, preserving cursor position

**URL:** <https://discuss.prosemirror.net/t/appending-one-block-to-another-preserving-cursor-position/942>\
**Category:** Uncategorized\
**Created:** [August 29, 2017, 5:57pm UTC](https://discuss.prosemirror.net/t/appending-one-block-to-another-preserving-cursor-position/942 "2017-08-29T17:57:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhuebert](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/mhuebert/32/310_2.png) [@mhuebert](https://discuss.prosemirror.net/u/mhuebert)\
**Post date:** [August 29, 2017, 5:57pm UTC](https://discuss.prosemirror.net/t/appending-one-block-to-another-preserving-cursor-position/942/1 "2017-08-29T17:57:50Z")

</div>

I am trying to join 2 ProseMirror docs, one of which is focused and contains a cursor:

> \<doc-1\> Hello… \</doc-1\>
> 
> \<doc-2\> Ther\<CURSOR\>e… \</doc-2\>

(we can assume that we already know which doc is focused)

I would like to append doc-2 to doc-1, and have the cursor remain exactly where it is. Is there a straightforward way to think about doing this? I haven’t yet wrapped my head around all the different kinds of selections and positions.

I’ve tried reading the cursor’s coordinates on the screen, and then mapping that back to the newly-joined doc, but was getting inconsistent results, and this also wouldn’t work if the two docs were not exactly next to each other. I would rather have some way of using the offset/position precisely, just not sure how to do that when joining two documents.

---

<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:** [August 29, 2017, 7:21pm UTC](https://discuss.prosemirror.net/t/appending-one-block-to-another-preserving-cursor-position/942/2 "2017-08-29T19:21:42Z")

</div>

If the docs can be cleanly joined, without introducing intermediate wrapper nodes, you can just add the size of the first doc to the cursor position and create a new selection from that, i.e. something like `TextSelection.create(joinedDoc, oldSelection.anchor + doc1.content.size)`. A cleaner and more general way is to create a mapping that moves all positions forward, and map the selection through that — `oldSelection.map(joineDoc, StepMap.offset(doc1.content.size))` (though `StepMap.offset` isn’t in the latest release yet, where you’d do something like `new StepMap([0, 0, doc1.content.size])` instead.

---

<div class="post-metadata">

**Author:** ![mhuebert](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/mhuebert/32/310_2.png) [@mhuebert](https://discuss.prosemirror.net/u/mhuebert)\
**Post date:** [August 30, 2017, 10:08am UTC](https://discuss.prosemirror.net/t/appending-one-block-to-another-preserving-cursor-position/942/3 "2017-08-30T10:08:51Z")

</div>

Thanks, that’s very helpful.
