Leaving the Editor View with an Arrow key handler


I’m working out how to determine whether the user intends to leave the Editor View when pressing the ArrowDown key at the end of the document (or the ArrowUp key at the top of the document) but am uncertain about how to approach this. Setting up the key handler is easy, now I just have to determine the logic.

Generally, I’d assume you’d want to know what line the user is in and if pressing the up or down key would lead out of the document. Reading the docs, there’s no way to determine what line the user is in. One possible solution that I came up with is using EditorView.posAtCoords() to determine if some Coordinate in the direction the user is moving in would return null and if so, exit the EditorView and do something.

My solution sounds a little shoddy to me. For one, I’d probably have to guess some arbitrary coordinate offset in the direction of the arrow key which may or may not return null. I can’t help but feel there’s a better way. Thoughts?

Edit I attempted to use my solution described above and it pretty much falls victim to what I thought would happen. It works when there is only text in the top or bottom line of the document but if you have anything taller than a regular text node, or mixed content in the line, results are unpredicatble


You can probably make this work using the endOfTextblock method, which provides a (bidi-aware) way of checking whether a given motion would take the cursor out of its current text block. If the selection is in the first or last textblock of the document, and that method returns true, that should be the case you’re looking for. (Of course, having selectable non-textblock block nodes might complicate this a little.)