Skip to content

Commit 55b33a5

Browse files
committed
Fix issue with incorrect position when selecting end of line in focusHelpers
1 parent 69eeeb7 commit 55b33a5

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

web/src/lib/helpers/focusHelpers.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,29 @@ const selectEnd = (element: HTMLElement, position: number) => {
150150
if (position === -1) {
151151
range.setStart(textNodes.slice(-1)[0], Math.min(position, element.textContent?.length || 0));
152152
} else {
153+
const lastTextNode = textNodes.findLast((node) => node.nodeName === '#text');
154+
153155
textNodes.forEach((node) => {
154156
if (position < 0) return;
155157

156158
const length = node.nodeName === '#text'
157-
? node.textContent?.length || 0
158-
: 1;
159-
160-
if (node.nodeName === 'BR') {
161-
162-
}
159+
? node.textContent?.length || 0
160+
: 1;
163161

164162
position -= length;
165163

166-
if (position <= 0) {
164+
if (position <= 0 || node === lastTextNode) {
165+
console.log(node);
166+
167167
const index = Math.max(Math.min(position + length, length), 0);
168168

169-
range.setStart(node, index);
169+
if (node.textContent?.at(index - 1) === '\n') {
170+
range.setStart(node, Math.max(index - 1, 0));
171+
} else {
172+
range.setStart(node, index);
173+
}
174+
175+
position = -1;
170176
}
171177
});
172178
}

0 commit comments

Comments
 (0)