ux: FIX cursor updates to prevent cursor from jumping around while working together #21
This commit is contained in:
parent
6f07a180c9
commit
7c5218dba2
1 changed files with 22 additions and 2 deletions
24
app.py
24
app.py
|
|
@ -399,9 +399,10 @@ function connect(){
|
|||
}
|
||||
} else if (msg.type === "update" && msg.ver > ver && msg.clientId !== clientId) {
|
||||
const {selectionStart:s, selectionEnd:e} = ta;
|
||||
const oldText = ta.value;
|
||||
ta.value = msg.text; ver = msg.ver; updateGutter();
|
||||
ta.selectionStart = Math.min(s, ta.value.length);
|
||||
ta.selectionEnd = Math.min(e, ta.value.length);
|
||||
ta.selectionStart = adjustCursor(oldText, msg.text, s);
|
||||
ta.selectionEnd = adjustCursor(oldText, msg.text, e);
|
||||
} else if (msg.type === "peers_changed") {
|
||||
const el = $("#peers");
|
||||
el.textContent = msg.count;
|
||||
|
|
@ -439,6 +440,25 @@ async function copyToClipboard() {
|
|||
}
|
||||
}
|
||||
|
||||
// Adjust cursor position after a remote text update.
|
||||
// Finds the single changed region (common prefix + suffix),
|
||||
// then shifts the cursor accordingly:
|
||||
// - change is after cursor → no movement
|
||||
// - change is before cursor → shift by length delta
|
||||
// - cursor was inside the changed region → place at end of new content
|
||||
function adjustCursor(oldText, newText, pos) {
|
||||
let start = 0;
|
||||
const minLen = Math.min(oldText.length, newText.length);
|
||||
while (start < minLen && oldText[start] === newText[start]) start++;
|
||||
if (pos <= start) return pos; // change is entirely after cursor
|
||||
let oldEnd = oldText.length, newEnd = newText.length;
|
||||
while (oldEnd > start && newEnd > start && oldText[oldEnd - 1] === newText[newEnd - 1]) {
|
||||
oldEnd--; newEnd--;
|
||||
}
|
||||
if (pos >= oldEnd) return pos + (newEnd - oldEnd); // change is before cursor
|
||||
return newEnd; // cursor was inside changed region
|
||||
}
|
||||
|
||||
connect();
|
||||
|
||||
// Handle Tab key to insert 4 spaces instead of navigation
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue