Skip to content

Commit 12e39d4

Browse files
committed
fix(cursor): improve cursor positioning logic for line breaks and diagnostics
- Implement special handling for BR elements in moveCursor - Replace diagnostic ::before spacer with margin-left for better layout consistency - Filter out diagnostic elements when calculating child index for cursor placement
1 parent 2539e37 commit 12e39d4

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

anycode-base/src/cursor.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,32 @@ export function moveCursor(
5050
return
5151
}
5252

53-
const ch = chunk.firstChild || chunk;
53+
// Special handling for BR elements: set cursor relative to parent, not inside BR
54+
let ch: Node;
55+
let chunkOffset: number;
56+
57+
if (chunk.tagName === 'BR') {
58+
// Find the index of the BR element within its parent
59+
const parent = chunk.parentElement;
60+
if (!parent) return;
61+
62+
const childIndex = Array.from(parent.children)
63+
.filter(child => !isDiagnosticElement(child))
64+
.indexOf(chunk as Element);
65+
66+
if (childIndex === -1) return;
67+
68+
ch = parent;
69+
chunkOffset = childIndex;
70+
} else {
71+
ch = chunk.firstChild || chunk;
72+
chunkOffset = chunkCharacter;
73+
}
5474

5575
// Ensure we're working with the correct document context
5676
const doc = ch.ownerDocument || document;
5777
const range = doc.createRange();
58-
range.setStart(ch, chunkCharacter);
78+
range.setStart(ch, chunkOffset);
5979
range.collapse(true);
6080

6181
// Check if the range is already the same as the current selection
@@ -86,8 +106,8 @@ export function moveCursor(
86106
const codePaddingLeft = codeElement ?
87107
parseFloat(getComputedStyle(codeElement).paddingLeft) : 0;
88108

89-
const cursorNode = ch.firstChild || ch;
90-
const cursorOffset = chunkCharacter;
109+
const cursorNode = ch;
110+
const cursorOffset = chunkOffset;
91111

92112
scrollCursorIntoViewHorizontally(
93113
scrollable!, cursorNode, cursorOffset,

anycode-base/src/styles.css

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
color: #ff6b6b;
6363
border-radius: 4px;
6464
padding: 0 4px;
65+
margin-left: 3ch;
6566
vertical-align: middle;
6667
line-height: 1;
6768
display: inline-block;
@@ -72,13 +73,6 @@
7273
cursor: text;
7374
}
7475

75-
.diagnostic::before {
76-
content: "";
77-
display: inline-block;
78-
width: 3ch;
79-
pointer-events: none;
80-
}
81-
8276
.code.selecting .diagnostic {
8377
user-select: none;
8478
-webkit-user-select: none;

0 commit comments

Comments
 (0)