restore highlighting on tab switch

This commit is contained in:
Eric Husa 2019-11-24 04:18:51 -06:00
parent 610d46a1a4
commit 547f1fc426
4 changed files with 74 additions and 4 deletions

View file

@ -302,6 +302,13 @@ class HighlighterWaiter {
} }
} }
/**
* Resets mouse variable to up position.
* Used on tab change.
*/
mouseUp(){
this.mouseButtonDown = false;
}
/** /**
* Given start and end offsets, writes the HTML for the selection info element with the correct * Given start and end offsets, writes the HTML for the selection info element with the correct
@ -376,9 +383,10 @@ class HighlighterWaiter {
* @param {string} direction * @param {string} direction
*/ */
displayHighlights(pos, direction) { displayHighlights(pos, direction) {
if (!pos) return; if (!pos || pos.length === 0) return;
if (this.manager.tabs.getActiveInputTab() !== this.manager.tabs.getActiveOutputTab()) return; const inputNum = this.manager.tabs.getActiveInputTab();
if (inputNum !== this.manager.tabs.getActiveOutputTab()) return;
const io = direction === "forward" ? "output" : "input"; const io = direction === "forward" ? "output" : "input";
@ -387,6 +395,13 @@ class HighlighterWaiter {
document.getElementById(io + "-text"), document.getElementById(io + "-text"),
document.getElementById(io + "-highlighter"), document.getElementById(io + "-highlighter"),
pos); pos);
if (direction === "forward"){
this.highlightInput(pos);
}
else{
this.manager.input.updateInputHighlight(inputNum, pos)
}
} }

View file

@ -328,6 +328,7 @@ class InputWaiter {
* @param {object} inputData - Object containing the input and its metadata * @param {object} inputData - Object containing the input and its metadata
* @param {number} inputData.inputNum - The unique inputNum for the selected input * @param {number} inputData.inputNum - The unique inputNum for the selected input
* @param {string | object} inputData.input - The actual input data * @param {string | object} inputData.input - The actual input data
* @param {object} inputData.pos - The highlight object for the selected tab
* @param {string} inputData.name - The name of the input file * @param {string} inputData.name - The name of the input file
* @param {number} inputData.size - The size in bytes of the input file * @param {number} inputData.size - The size in bytes of the input file
* @param {string} inputData.type - The MIME type of the input file * @param {string} inputData.type - The MIME type of the input file
@ -372,6 +373,10 @@ class InputWaiter {
}); });
} }
//Restore highlighting
this.updateInputHighlight(inputData.inputNum, inputData.pos);
this.restoreHighlighting();
if (!silent) window.dispatchEvent(this.manager.statechange); if (!silent) window.dispatchEvent(this.manager.statechange);
} else { } else {
this.setFile(inputData, silent); this.setFile(inputData, silent);
@ -554,6 +559,23 @@ class InputWaiter {
}, transferable); }, transferable);
} }
/**
* Updates the saved highlight property for the specified inputNum
* Used for restoring highlight on tab switch
*
* @param {number} inputNum
* @param {Object} pos - The position object for the highlight.
*/
updateInputHighlight(inputNum, pos) {
this.inputWorker.postMessage({
action: "updateInputHighlight",
data: {
inputNum: inputNum,
pos: pos
}
});
}
/** /**
* Updates the .data property for the input of the specified inputNum. * Updates the .data property for the input of the specified inputNum.
* Used for switching the output into the input * Used for switching the output into the input
@ -845,6 +867,16 @@ class InputWaiter {
} }
} }
/**
* Restore the highlighting of the active tabs from the saved highlight data
*/
async restoreHighlighting() {
const activeTab = this.manager.tabs.getActiveInputTab();
const tabObj = await this.getInputObj(activeTab);
this.manager.highlighter.highlightOutput(tabObj.highlight);
}
/** /**
* Checks if an input contains carriage returns. * Checks if an input contains carriage returns.
* If a CR is detected, checks if the preserve CR option has been set, * If a CR is detected, checks if the preserve CR option has been set,
@ -1013,6 +1045,7 @@ class InputWaiter {
silent: true silent: true
} }
}); });
this.manager.highlighter.mouseUp();
} else { } else {
const minNum = Math.min(...this.manager.tabs.getInputTabList()); const minNum = Math.min(...this.manager.tabs.getInputTabList());
let direction = "right"; let direction = "right";

View file

@ -1292,6 +1292,7 @@ class OutputWaiter {
inputSwitch(switchData) { inputSwitch(switchData) {
this.switchOrigData = switchData; this.switchOrigData = switchData;
document.getElementById("undo-switch").disabled = false; document.getElementById("undo-switch").disabled = false;
this.manager.highlighter.removeHighlights();
this.resetSwitchButton(); this.resetSwitchButton();

View file

@ -53,6 +53,9 @@ self.addEventListener("message", function(e) {
case "updateInputValue": case "updateInputValue":
self.updateInputValue(r.data); self.updateInputValue(r.data);
break; break;
case "updateInputHighlight":
self.updateInputHighlight(r.data);
break;
case "updateInputObj": case "updateInputObj":
self.updateInputObj(r.data); self.updateInputObj(r.data);
break; break;
@ -451,9 +454,11 @@ self.setInput = function(inputData) {
if (input === undefined || input === null) return; if (input === undefined || input === null) return;
let inputVal = input.data; let inputVal = input.data;
let highlight = input.highlight;
const inputObj = { const inputObj = {
inputNum: inputNum, inputNum: inputNum,
input: inputVal input: inputVal,
pos: highlight
}; };
if (typeof inputVal !== "string") { if (typeof inputVal !== "string") {
inputObj.name = inputVal.name; inputObj.name = inputVal.name;
@ -553,7 +558,7 @@ self.updateInputValue = function(inputData) {
const inputNum = inputData.inputNum; const inputNum = inputData.inputNum;
if (inputNum < 1) return; if (inputNum < 1) return;
if (Object.prototype.hasOwnProperty.call(self.inputs[inputNum].data, "fileBuffer") && if (Object.prototype.hasOwnProperty.call(self.inputs[inputNum].data, "fileBuffer") &&
typeof inputData.value === "string" && !inputData.force) return; typeof inputData.value === "string" && !inputData.force) return;
const value = inputData.value; const value = inputData.value;
if (self.inputs[inputNum] !== undefined) { if (self.inputs[inputNum] !== undefined) {
if (typeof value === "string") { if (typeof value === "string") {
@ -579,6 +584,21 @@ self.updateInputValue = function(inputData) {
} }
}; };
/**
* Update the stored highlight object of an input.
*
* @param {object} inputData
* @param {number} inputData.inputNum - The input that's having its value updated
* @param {Object} inputData.pos - The position object for the highlight.
*/
self.updateInputHighlight = function(inputData) {
const inputNum = inputData.inputNum;
const pos = inputData.pos;
if (inputNum < 1) return;
self.inputs[inputNum].highlight = pos;
}
/** /**
* Update the stored data object for an input. * Update the stored data object for an input.
* Used if we need to change a string to an ArrayBuffer * Used if we need to change a string to an ArrayBuffer
@ -825,6 +845,7 @@ self.addInput = function(
newInputObj.data = ""; newInputObj.data = "";
newInputObj.status = "loaded"; newInputObj.status = "loaded";
newInputObj.progress = 100; newInputObj.progress = 100;
newInputObj.highlight = [{"start":0,"end":0}];
break; break;
case "file": case "file":
newInputObj.data = { newInputObj.data = {