Skip to content

Commit

Permalink
web: maintain active selection position when programmatically updatin…
Browse files Browse the repository at this point in the history
…g textbox content
  • Loading branch information
zackbrown committed Mar 11, 2024
1 parent 5976b11 commit 20bc873
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions pax-chassis-web/interface/src/classes/native-element-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ export class NativeElementPool {
}
}
this.chassis!.interrupt(JSON.stringify(message), undefined);
// @ts-ignore
textbox.value = textbox.fixed_text_value;
});

textbox.addEventListener("change", (_event) => {
Expand All @@ -207,8 +205,6 @@ export class NativeElementPool {
}
}
this.chassis!.interrupt(JSON.stringify(message), undefined);
// @ts-ignore
textbox.value = textbox.fixed_text_value;
});

let runningChain: HTMLDivElement = this.objectManager.getFromPool(DIV);
Expand Down Expand Up @@ -261,9 +257,26 @@ export class NativeElementPool {

// Apply the content
if (patch.text != null) {
textbox.value = patch.text;
// @ts-ignore
textbox.fixed_text_value = textbox.value;

// Check if the input element is focused — we want to maintain the user's cursor position if so
if (document.activeElement === textbox) {
// Get the current selection range
const selectionStart = textbox.selectionStart || 0;

// Update the content of the input
textbox.value = patch.text;

// Calculate the new cursor position, clamped to the new length of the input value
const newCursorPosition = Math.min(selectionStart, patch.text.length);

// Set the cursor position to the beginning of the former selection range
textbox.setSelectionRange(newCursorPosition, newCursorPosition);
} else {
// If the textbox isn't selected, just update its content
textbox.value = patch.text;
}


}

// Handle size_x and size_y
Expand Down

0 comments on commit 20bc873

Please sign in to comment.