Skip to content

Commit ebb9229

Browse files
authored
Merge pull request #353 from avinxshKD/fix/undo-history-btoa-quota
fix: cap undo history, fix btoa unicode crash, add quota fallback (#352)
2 parents 5361351 + 36e8fd4 commit ebb9229

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/graph-builder/graph-core/4-undo-redo.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ class GraphUndoRedo extends GraphComponent {
9999
),
100100
});
101101
this.curActionIndex += 1;
102+
103+
if (this.actionArr.length > 100) {
104+
const drop = this.actionArr.length - 100;
105+
this.actionArr.splice(0, drop);
106+
this.curActionIndex -= drop;
107+
}
108+
102109
this.informUI();
103110
}
104111

src/graph-builder/graph-core/5-load-save.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ class GraphLoadSave extends GraphUndoRedo {
4848
}
4949

5050
static stringifyAction({ actionName, parameters }) {
51-
return { actionName, parameters: window.btoa(JSON.stringify(parameters)) };
51+
return { actionName, parameters: JSON.stringify(parameters) };
5252
}
5353

5454
static parseAction({ actionName, parameters }) {
55-
return { actionName, parameters: JSON.parse(window.atob(parameters)) };
55+
try {
56+
return { actionName, parameters: JSON.parse(parameters) };
57+
} catch {
58+
return { actionName, parameters: JSON.parse(window.atob(parameters)) };
59+
}
5660
}
5761

5862
jsonifyGraph() {

src/graph-builder/local-storage-manager.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ const localStorageGet = (key) => {
3535
const localStorageSet = (key, value) => {
3636
try {
3737
window.localStorage.setItem(key, value);
38+
return true;
3839
} catch (e) {
3940
toast.error(e.message);
41+
return false;
4042
}
4143
};
4244

@@ -82,16 +84,20 @@ const localStorageManager = {
8284
const raw = localStorageGet(id);
8385
if (raw === null) return null;
8486
const parsed = parseStoredJson(raw);
85-
if (parsed === null) {
86-
localStorageRemove(id);
87+
if (parsed !== null) return parsed;
88+
// fallback for legacy plain JSON data saved before base64 encoding was introduced
89+
try {
90+
return JSON.parse(raw);
91+
} catch {
8792
return null;
8893
}
89-
return parsed;
9094
},
9195
save(id, graphContent) {
9296
this.addGraph(id);
93-
const serializedJson = JSON.stringify(graphContent);
94-
localStorageSet(id, encodeBase64(serializedJson));
97+
if (!localStorageSet(id, encodeBase64(JSON.stringify(graphContent)))) {
98+
const stripped = { ...graphContent, actionHistory: [] };
99+
localStorageSet(id, encodeBase64(JSON.stringify(stripped)));
100+
}
95101
},
96102
remove(id) {
97103
if (this.allgs.delete(id)) this.saveAllgs();

0 commit comments

Comments
 (0)