Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
return undefined;
}
}
return State.load(data);
return State.fromData(data);
}

let dontSave = $state(false);
Expand Down Expand Up @@ -309,7 +309,7 @@
return;
}
dontSave = true;
globals = Object.assign(State.load(e.state), {
globals = Object.assign(State.fromData(e.state), {
currentSheetIndex: globals.currentSheetIndex,
mode: globals.mode,
helpOpen: globals.helpOpen,
Expand Down
3 changes: 2 additions & 1 deletion src/SaveLoad.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
const data = reader.result.match(/,(.*)/)[1];
decompressText(data)
.then((result) => {
globals = State.load(JSON.parse(result));
globals.load(JSON.parse(result));
globals.imageOpen = false;
Copy link
Collaborator Author

@lsnow99 lsnow99 Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now have to explicitly close the modal since we aren't just relying on the state being wiped. This is probably better to be controlled explicitly imo - you could also handle errors better/show confirmation if you wanted to for example

})
.catch((e) => {
console.error(e);
Expand Down
72 changes: 46 additions & 26 deletions src/classes.svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,7 @@ function minmax(min, x, max) {
return Math.min(Math.max(x, min), max);
}

export class State {
sheets = $state([]);
currentSheetIndex = $state(0);
currentSheet = $derived(this.sheets[this.currentSheetIndex]);
selected = $state(new Selection());
mode = $state("normal");
keyQueue = $state([]);
pasteBuffer = $state(new Register());
elements = $state({});
helpOpen = $state(false);
editorOpen = $state(false);
imageOpen = $state(false);
llmOpen = $state(false);
formulaCode = $state(`// Examples of user-defined formula functions
const sampleFormulaCode = () => `// Examples of user-defined formula functions

functions.factorial = (n) => {
if (n == 0) return 1;
Expand All @@ -50,14 +37,49 @@ functions.crypto = async (ticker) => {
),
);
};
`);
`;

class LoadableState {
sheets = $state([]);
formulaCode = $state(sampleFormulaCode());

constructor(sheets, formulaCode) {
this.sheets = sheets;
this.sheets.forEach((sheet) => (sheet.globals = this));
if (formulaCode != null) {
this.formulaCode = formulaCode;
evalCode(this.formulaCode);
}
}
}

export class State {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff got kind of butchered here since it prioritized my move of the sampleFormulaCode

sheets = $state([]);
currentSheetIndex = $state(0);
currentSheet = $derived(this.sheets[this.currentSheetIndex]);
selected = $state(new Selection());
mode = $state("normal");
keyQueue = $state([]);
pasteBuffer = $state(new Register());
elements = $state({});
helpOpen = $state(false);
editorOpen = $state(false);
imageOpen = $state(false);
llmOpen = $state(false);
settings = $state({
mobileZoom: 100,
});
formulaCode = $state(sampleFormulaCode());
forceSave = $state(0);

static load(data) {
let result = new State(
static fromData(data) {
const state = new State([]);
state.load(data);
return state;
}

load(data) {
let result = new LoadableState(
data.sheets.map((sheet) => {
let s = new Sheet(
sheet.name,
Expand All @@ -72,18 +94,16 @@ functions.crypto = async (ticker) => {
}),
data.formulaCode,
);
return result;
this._reload(result);
}

_reload(loadableState) {
this.sheets = loadableState.sheets;
this.formulaCode = loadableState.formulaCode;
}

constructor(sheets, formulaCode) {
this.sheets = sheets;
this.sheets.forEach((sheet) => {
sheet.globals = this;
});
if (formulaCode != null) {
this.formulaCode = formulaCode;
evalCode(this.formulaCode);
}
this._reload(new LoadableState(sheets, formulaCode));
}

getSelectedCells() {
Expand Down
26 changes: 24 additions & 2 deletions test/sheet-and-formula.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Sheet, State } from "../src/classes.svelte.js";
import { State } from "../src/classes.svelte.js";
import { test, expect, beforeEach } from "vitest";
import { evalCode, functions } from "../src/formula-functions.svelte.js";

function createSheet(cells, formulaCode = "") {
return State.load({
return State.fromData({
sheets: [
{
name: "Sheet 1",
Expand Down Expand Up @@ -690,3 +690,25 @@ test("Binary literals", async () => {
[8, -8, -9, 7, 9],
]);
});

test("State.load preserves elements", async () => {
const state = createSheet([["1", "2", "3"]]);
await expectSheet(state.currentSheet, [[1, 2, 3]]);
state.elements.formulaBar = document.createElement("textarea");
state.elements.formulaBar.setAttribute("data-testid", "shouldPreserve");

const savedState = createSheet([["4", "5", "6"]]);
await expectSheet(savedState.currentSheet, [[4, 5, 6]]);

const testFormulaCode = "functions.testFormulaCode = () => {}";
state.load({
sheets: savedState.sheets,
formulaCode: testFormulaCode,
});

await expectSheet(state.currentSheet, [[4, 5, 6]]);
expect(state.formulaCode).toBe(testFormulaCode);
expect(state.elements.formulaBar.getAttribute("data-testid")).toBe(
"shouldPreserve",
);
});