# Cursor isn't behaving when saving and reloading states

**URL:** https://discuss.codemirror.net/t/cursor-isnt-behaving-when-saving-and-reloading-states/3913
**Category:** v6
**Created:** [January 18, 2022, 1:37am UTC](https://discuss.codemirror.net/t/cursor-isnt-behaving-when-saving-and-reloading-states/3913 "2022-01-18T01:37:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![dusty](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@dusty](https://discuss.codemirror.net/u/dusty)
#### Post date: [January 18, 2022, 1:37am UTC](https://discuss.codemirror.net/t/cursor-isnt-behaving-when-saving-and-reloading-states/3913/1 "2022-01-18T01:37:40Z")

</div>

Before loading a new document into the editor, I save the current state so that it can be restored using `view.setState()`.

This works fine if I just switch back and forth between states. But if I interact with the document, even just to give it focus by placing a cursor inside the editor, then, after I switch to another state using `view.setState()`, a cursor is placed at the bottom of the editor which scrolls the editor down to the bottom and disables any selection that was in the restored state.

I am getting this issue with even the most minimalist set up with no extensions. Any idea might be happening?

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [January 18, 2022, 9:50am UTC](https://discuss.codemirror.net/t/cursor-isnt-behaving-when-saving-and-reloading-states/3913/2 "2022-01-18T09:50:28Z")

</div>

I’m not able to reproduce. Could you show a minimal script + precise instructions on how to get it to happen?

---

<div class="post-metadata">

### Author: ![dusty](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@dusty](https://discuss.codemirror.net/u/dusty)
#### Post date: [January 18, 2022, 10:48pm UTC](https://discuss.codemirror.net/t/cursor-isnt-behaving-when-saving-and-reloading-states/3913/3 "2022-01-18T22:48:02Z")

</div>

Thank you for taking a look.

I’ve dug into this some more and I’m not sure that is related to CodeMirror so I don’t want to take up too much of your time.

I’ve found that the issue only occurs when navigating between states using a key command. When the editor is focused there doesn’t seem to be any way to prevent the keystroke from landing in the editor even if the event is prevented from propagating.

Here’s a quick demo. `Mod-/` toggles between states.

```auto
const view = new EditorView({ parent: document.body, })
const ext = [EditorState.allowMultipleSelections.of(true), drawSelection()]

const store = new Map()
store.set(1, EditorState.create({ doc: 'Doc 1\nLorem Ipsum', extensions: ext }))
store.set(2, EditorState.create({ doc: 'Doc 2\nLorem Ipsum', extensions: ext }))

let current = 1
view.setState(store.get(current))
view.dispatch(view.state.update({selection: {anchor: 1, head: 3}}))

window.addEventListener('keydown', handler, true)

function handler (e) {
  if (e.key === '/' && e.metaKey) {
    toggleState()
    e.stopPropagation()
  }
}

function toggleState () {
  const next = current === 1 ? 2 : 1
  store.set(current, view.state)
  view.setState(store.get(next))
  current = next
}

```

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [January 19, 2022, 6:55am UTC](https://discuss.codemirror.net/t/cursor-isnt-behaving-when-saving-and-reloading-states/3913/4 "2022-01-19T06:55:29Z")

</div>

Doing `preventDefault` will cause the editor to not further handle the event. It doesn’t look at `stopPropagation`.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [January 19, 2022, 2:49pm UTC](https://discuss.codemirror.net/t/cursor-isnt-behaving-when-saving-and-reloading-states/3913/6 "2022-01-19T14:49:21Z")

</div>

[This patch](https://github.com/codemirror/view/commit/9bde074787d8b8e243d1cc8a39b268108c1ecd86) should help with this.

---

<div class="post-metadata">

### Author: ![dusty](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@dusty](https://discuss.codemirror.net/u/dusty)
#### Post date: [January 19, 2022, 5:41pm UTC](https://discuss.codemirror.net/t/cursor-isnt-behaving-when-saving-and-reloading-states/3913/7 "2022-01-19T17:41:34Z")

</div>

That patch seems to work perfectly. Thank you.

---

<div class="post-metadata">

### Author: ![dusty](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@dusty](https://discuss.codemirror.net/u/dusty)
#### Post date: [January 19, 2022, 5:48pm UTC](https://discuss.codemirror.net/t/cursor-isnt-behaving-when-saving-and-reloading-states/3913/8 "2022-01-19T17:48:46Z")

</div>

_(My last post before marijn committed the patch was accidentally deleted. Here it is again to avoid having to dig through the edit history to see why the patch was needed.)_

Unfortunately `preventDefault()` doesn’t solve this even though it does prevent the keystroke from landing.

Here’s a simpler example. The selection is there in the state after the update but it isn’t visible and the cursor is placed at the end of the text. If you `contentDOM.blur()` before `setState()` the problem disappears but then you lose the selection when you click back into the editor.

```auto
window.onkeydown = (e) => {
  if (e.key === '/') { 
    e.preventDefault()
    const state = EditorState.create({
      extensions: ext,
      doc: 'Lorem Ipsum',
      selection: {anchor: 1, head: 3},
    })
    view.setState(state)
    console.log(view.state.selection)
  }
}

```
