Input and Output encodings are now saved per tab

This commit is contained in:
n1474335 2022-10-21 18:29:52 +01:00
parent 5efd125d9b
commit bdb8c02d5a
9 changed files with 87 additions and 33 deletions

View file

@ -138,8 +138,8 @@ class ControlsWaiter {
}
}
const inputChrEnc = this.manager.input.inputChrEnc;
const outputChrEnc = this.manager.output.outputChrEnc;
const inputChrEnc = this.manager.input.getChrEnc();
const outputChrEnc = this.manager.output.getChrEnc();
const params = [
includeRecipe ? ["recipe", recipeStr] : undefined,

View file

@ -51,8 +51,8 @@ class HighlighterWaiter {
const selectionRanges = e.state.selection.ranges;
// Adjust offsets based on the width of the character set
const inputCharacterWidth = chrEncWidth(this.manager.input.inputChrEnc);
const outputCharacterWidth = chrEncWidth(this.manager.output.outputChrEnc);
const inputCharacterWidth = chrEncWidth(this.manager.input.getChrEnc());
const outputCharacterWidth = chrEncWidth(this.manager.output.getChrEnc());
let ratio = 1;
if (inputCharacterWidth !== outputCharacterWidth &&
inputCharacterWidth !== 0 && outputCharacterWidth !== 0) {

View file

@ -469,6 +469,7 @@ class InputWaiter {
* @param {string} file.type
* @param {string} status
* @param {number} progress
* @param {number} encoding
* @param {boolean} [silent=false] - If false, fires the manager statechange event
*/
async set(inputNum, inputData, silent=false) {
@ -476,13 +477,14 @@ class InputWaiter {
const activeTab = this.manager.tabs.getActiveTab("input");
if (inputNum !== activeTab) return;
this.inputChrEnc = inputData.encoding;
if (inputData.file) {
this.setFile(inputNum, inputData);
} else {
this.clearFile(inputNum);
}
// TODO Per-tab encodings?
let inputVal;
if (this.inputChrEnc > 0) {
inputVal = cptable.utils.decode(this.inputChrEnc, new Uint8Array(inputData.buffer));
@ -609,8 +611,8 @@ class InputWaiter {
// If value is a string, interpret it using the specified character encoding
if (typeof value === "string") {
stringSample = value.slice(0, 4096);
if (this.inputChrEnc > 0) {
buffer = cptable.utils.encode(this.inputChrEnc, value);
if (this.getChrEnc() > 0) {
buffer = cptable.utils.encode(this.getChrEnc(), value);
buffer = new Uint8Array(buffer).buffer;
} else {
buffer = Utils.strToArrayBuffer(value);
@ -631,7 +633,8 @@ class InputWaiter {
data: {
inputNum: inputNum,
buffer: buffer,
stringSample: stringSample
stringSample: stringSample,
encoding: this.getChrEnc()
}
}, transferable);
}
@ -924,7 +927,7 @@ class InputWaiter {
* @param {number} inputNum - The inputNum of the tab to change to
* @param {boolean} [changeOutput=false] - If true, also changes the output
*/
changeTab(inputNum, changeOutput) {
changeTab(inputNum, changeOutput=false) {
if (this.manager.tabs.getTabItem(inputNum, "input") !== null) {
this.manager.tabs.changeTab(inputNum, "input");
this.inputWorker.postMessage({

View file

@ -51,7 +51,6 @@ class OutputWaiter {
};
// Hold a copy of the currently displayed output so that we don't have to update it unnecessarily
this.currentOutputCache = null;
this.outputChrEnc = 0;
this.initEditor();
this.outputs = {};
@ -146,7 +145,14 @@ class OutputWaiter {
*/
chrEncChange(chrEncVal) {
if (typeof chrEncVal !== "number") return;
this.outputChrEnc = chrEncVal;
const currentTabNum = this.manager.tabs.getActiveTab("output");
if (currentTabNum >= 0) {
this.outputs[currentTabNum].encoding = chrEncVal;
} else {
throw new Error("Cannot change output chrEnc to " + chrEncVal);
}
// Reset the output, forcing it to re-decode the data with the new character encoding
this.setOutput(this.currentOutputCache, true);
// Update the URL manually since we aren't firing a statechange event
@ -154,11 +160,15 @@ class OutputWaiter {
}
/**
* Getter for the input character encoding
* Getter for the output character encoding
* @returns {number}
*/
getChrEnc() {
return this.outputChrEnc;
const currentTabNum = this.manager.tabs.getActiveTab("output");
if (currentTabNum < 0) {
return 0;
}
return this.outputs[currentTabNum].encoding;
}
/**
@ -195,11 +205,12 @@ class OutputWaiter {
// If data is an ArrayBuffer, convert to a string in the correct character encoding
if (data instanceof ArrayBuffer) {
if (this.outputChrEnc === 0) {
const encoding = this.getChrEnc();
if (encoding === 0) {
data = Utils.arrayBufferToStr(data);
} else {
try {
data = cptable.utils.decode(this.outputChrEnc, new Uint8Array(data));
data = cptable.utils.decode(encoding, new Uint8Array(data));
} catch (err) {
data = err;
}
@ -324,7 +335,8 @@ class OutputWaiter {
error: null,
status: "inactive",
bakeId: -1,
progress: false
progress: false,
encoding: 0
};
this.outputs[inputNum] = newOutput;
@ -851,7 +863,7 @@ class OutputWaiter {
if (!this.manager.tabs.getTabItem(inputNum, "output") && numTabs < this.maxTabs) {
// Create a new tab element
const newTab = this.manager.tabs.reateTabElement(inputNum, changeTab, "output");
const newTab = this.manager.tabs.createTabElement(inputNum, changeTab, "output");
tabsWrapper.appendChild(newTab);
} else if (numTabs === this.maxTabs) {
// Can't create a new tab

View file

@ -217,7 +217,7 @@ class WorkerWaiter {
break;
case "workerLoaded":
this.app.workerLoaded = true;
log.debug("ChefWorker loaded.");
log.debug("ChefWorker loaded");
if (!this.loaded) {
this.app.loaded();
this.loaded = true;