Skip to content
Permalink
Browse files

Make sure y doesn't end up in space above text in posAtCoords

Closes #71
  • Loading branch information...
marijnh committed Mar 13, 2019
1 parent 4ae2532 commit d1fb878741ce6faff0593ceeffcb57f2d4ed96e6
Showing with 3 additions and 2 deletions.
  1. +3 −2 view/src/cursor.ts
@@ -301,16 +301,17 @@ function domPosInText(node: Text, x: number, y: number): {node: Node, offset: nu

export function posAtCoords(view: EditorView, {x, y}: {x: number, y: number}, bias: -1 | 1 = -1): number {
let content = view.contentDOM.getBoundingClientRect(), block
let halfLine = view.defaultLineHeight / 2
for (let bounced = false;;) {
block = view.blockAtHeight(y, content.top)
if (block.top > y || block.bottom < y) {
bias = block.top > y ? -1 : 1
y = Math.min(block.bottom - 1, Math.max(block.top + 1, y))
y = Math.min(block.bottom - halfLine, Math.max(block.top + halfLine, y))
if (bounced) return -1
else bounced = true
}
if (block.type == BlockType.Text) break
y = bias > 0 ? block.bottom + 1 : block.top - 1
y = bias > 0 ? block.bottom + halfLine : block.top - halfLine
}
let lineStart = block.from
// If this is outside of the rendered viewport, we can't determine a position

0 comments on commit d1fb878

Please sign in to comment.
You can’t perform that action at this time.