Fix: Preserve text selection on tab switch for input/output

This commit is contained in:
Henry Harborne 2025-06-05 12:24:11 -04:00
parent c57556f49f
commit d0c1e5525e
2 changed files with 35 additions and 8 deletions

View file

@ -758,6 +758,15 @@ class InputWaiter {
* @param {string | ArrayBuffer} value * @param {string | ArrayBuffer} value
*/ */
updateInputValue(inputNum, value, force=false) { updateInputValue(inputNum, value, force=false) {
// Preserve current selection
const inputElement = document.getElementById("input-text");
let selectionStart = null;
let selectionEnd = null;
if (inputElement) {
selectionStart = inputElement.selectionStart;
selectionEnd = inputElement.selectionEnd;
}
// Prepare the value as a buffer (full value) and a string sample (up to 4096 bytes) // Prepare the value as a buffer (full value) and a string sample (up to 4096 bytes)
let buffer; let buffer;
let stringSample = ""; let stringSample = "";
@ -796,6 +805,14 @@ class InputWaiter {
eolSequence: this.getEOLSeq() eolSequence: this.getEOLSeq()
} }
}, transferable); }, transferable);
// Restore selection if it was set
if (inputElement && selectionStart !== null && selectionEnd !== null) {
requestAnimationFrame(() => {
inputElement.selectionStart = selectionStart;
inputElement.selectionEnd = selectionEnd;
});
}
} }
/** /**

View file

@ -551,17 +551,27 @@ class OutputWaiter {
if (!this.outputExists(inputNum)) { if (!this.outputExists(inputNum)) {
this.addOutput(inputNum); this.addOutput(inputNum);
} }
// Preserve current selection
if (Object.prototype.hasOwnProperty.call(data, "dish")) { const outputElement = document.getElementById("output-text");
data.dish = new Dish(data.dish); let selectionStart = null;
let selectionEnd = null;
if (outputElement) {
selectionStart = outputElement.selectionStart;
selectionEnd = outputElement.selectionEnd;
} }
this.outputs[inputNum].data = data; this.outputs[inputNum].statusMessage = statusMessage;
if (set) {
this.set(inputNum);
const tabItem = this.manager.tabs.getTabItem(inputNum, "output"); // Restore selection if it was set
if (tabItem) tabItem.style.background = ""; if (outputElement && selectionStart !== null && selectionEnd !== null) {
requestAnimationFrame(() => {
if (set) this.set(inputNum); outputElement.selectionStart = selectionStart;
outputElement.selectionEnd = selectionEnd;
});
}
}
} }
/** /**