Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix wrong position at end of line #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/domcoords.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down