Skip to content

Commit e836e48

Browse files
committed
fix: cap undo history, fix btoa unicode crash, add quota fallback (#352)
1 parent 5b79fb7 commit e836e48

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const localStorageGet = (key) => {
1212
const localStorageSet = (key, value) => {
1313
try {
1414
window.localStorage.setItem(key, value);
15+
return true;
1516
} catch (e) {
1617
toast.error(e.message);
18+
return false;
1719
}
1820
};
1921

@@ -53,12 +55,18 @@ const localStorageManager = {
5355
get(id) {
5456
const raw = localStorageGet(id);
5557
if (raw === null) return null;
56-
return JSON.parse(window.atob(raw));
58+
try {
59+
return JSON.parse(raw);
60+
} catch {
61+
return JSON.parse(window.atob(raw));
62+
}
5763
},
5864
save(id, graphContent) {
5965
this.addGraph(id);
60-
const serializedJson = JSON.stringify(graphContent);
61-
localStorageSet(id, window.btoa(serializedJson));
66+
if (!localStorageSet(id, JSON.stringify(graphContent))) {
67+
const stripped = { ...graphContent, actionHistory: [] };
68+
localStorageSet(id, JSON.stringify(stripped));
69+
}
6270
},
6371
remove(id) {
6472
if (this.allgs.delete(id)) this.saveAllgs();

0 commit comments

Comments
 (0)