From 7905a32c0537f9cfe45defb9c4118baa0e8866b3 Mon Sep 17 00:00:00 2001 From: Chrissi2812 Date: Wed, 6 Mar 2019 13:42:31 +0100 Subject: [PATCH 1/2] fix wrong position if selection end is at end of line --- src/domcoords.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/domcoords.js b/src/domcoords.js index a57d39ae..7ef2d5ee 100644 --- a/src/domcoords.js +++ b/src/domcoords.js @@ -234,18 +234,17 @@ function singleRect(object, bias) { // : (EditorView, number) → {left: number, top: number, right: number, bottom: number} // Given a position in the document model, get a bounding box of the // character at that position, relative to the window. -export function coordsAtPos(view, pos) { +export function coordsAtPos(view, pos, end = false) { let {node, offset} = view.docView.domFromPos(pos) let side, rect if (node.nodeType == 3) { - if (offset < node.nodeValue.length) { + if (end && offset < node.nodeValue.length) { + rect = singleRect(textRange(node, offset - 1, offset), -1) + side = "right" + } else if (offset < node.nodeValue.length) { rect = singleRect(textRange(node, offset, offset + 1), -1) side = "left" } - if ((!rect || rect.left == rect.right) && offset) { - rect = singleRect(textRange(node, offset - 1, offset), 1) - side = "right" - } } else if (node.firstChild) { if (offset < node.childNodes.length) { let child = node.childNodes[offset] From 7c0c1d7105a60714bbc1837ba71848fce4cc3944 Mon Sep 17 00:00:00 2001 From: Chrissi2812 Date: Wed, 6 Mar 2019 17:13:12 +0100 Subject: [PATCH 2/2] add end parameter to EditorView --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 55cb0861..92852867 100644 --- a/src/index.js +++ b/src/index.js @@ -259,10 +259,10 @@ export class EditorView { // Returns the viewport rectangle at a given document position. `left` // and `right` will be the same number, as this returns a flat // cursor-ish rectangle. - coordsAtPos(pos) { + coordsAtPos(pos, end = false) { if (this.inDOMChange) pos = this.inDOMChange.mapping.invert().map(pos) - return coordsAtPos(this, pos) + return coordsAtPos(this, pos, end) } // :: (number) → {node: dom.Node, offset: number}