|
|
@@ -1,9 +1,10 @@
|
|
1
|
1
|
import { Data } from "types"
|
|
2
|
2
|
import { BaseCommand } from "./commands/command"
|
|
|
3
|
+import state from "./state"
|
|
3
|
4
|
|
|
4
|
5
|
// A singleton to manage history changes.
|
|
5
|
6
|
|
|
6
|
|
-class History<T> {
|
|
|
7
|
+class BaseHistory<T> {
|
|
7
|
8
|
private stack: BaseCommand<T>[] = []
|
|
8
|
9
|
private pointer = -1
|
|
9
|
10
|
private maxLength = 100
|
|
|
@@ -42,11 +43,22 @@ class History<T> {
|
|
42
|
43
|
this.save(data)
|
|
43
|
44
|
}
|
|
44
|
45
|
|
|
45
|
|
- save = (data: T) => {
|
|
|
46
|
+ load(data: T, id = "code_slate_0.0.1") {
|
|
46
|
47
|
if (typeof window === "undefined") return
|
|
47
|
48
|
if (typeof localStorage === "undefined") return
|
|
48
|
49
|
|
|
49
|
|
- localStorage.setItem("code_slate_0.0.1", JSON.stringify(data))
|
|
|
50
|
+ const savedData = localStorage.getItem(id)
|
|
|
51
|
+
|
|
|
52
|
+ if (savedData !== null) {
|
|
|
53
|
+ Object.assign(data, this.restoreSavedData(JSON.parse(savedData)))
|
|
|
54
|
+ }
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ save = (data: T, id = "code_slate_0.0.1") => {
|
|
|
58
|
+ if (typeof window === "undefined") return
|
|
|
59
|
+ if (typeof localStorage === "undefined") return
|
|
|
60
|
+
|
|
|
61
|
+ localStorage.setItem(id, JSON.stringify(this.prepareDataForSave(data)))
|
|
50
|
62
|
}
|
|
51
|
63
|
|
|
52
|
64
|
disable = () => {
|
|
|
@@ -57,9 +69,54 @@ class History<T> {
|
|
57
|
69
|
this._enabled = true
|
|
58
|
70
|
}
|
|
59
|
71
|
|
|
|
72
|
+ prepareDataForSave(data: T): any {
|
|
|
73
|
+ return { ...data }
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ restoreSavedData(data: any): T {
|
|
|
77
|
+ return { ...data }
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
60
|
80
|
get disabled() {
|
|
61
|
81
|
return !this._enabled
|
|
62
|
82
|
}
|
|
63
|
83
|
}
|
|
64
|
84
|
|
|
65
|
|
-export default new History<Data>()
|
|
|
85
|
+// App-specific
|
|
|
86
|
+
|
|
|
87
|
+class History extends BaseHistory<Data> {
|
|
|
88
|
+ constructor() {
|
|
|
89
|
+ super()
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ prepareDataForSave(data: Data): any {
|
|
|
93
|
+ const dataToSave: any = { ...data }
|
|
|
94
|
+
|
|
|
95
|
+ dataToSave.selectedIds = Array.from(data.selectedIds.values())
|
|
|
96
|
+
|
|
|
97
|
+ return dataToSave
|
|
|
98
|
+ }
|
|
|
99
|
+
|
|
|
100
|
+ restoreSavedData(data: any): Data {
|
|
|
101
|
+ const restoredData = { ...data }
|
|
|
102
|
+
|
|
|
103
|
+ restoredData.selectedIds = new Set(restoredData.selectedIds)
|
|
|
104
|
+
|
|
|
105
|
+ // Also restore camera position, which is saved separately in this app
|
|
|
106
|
+ const cameraInfo = localStorage.getItem("code_slate_camera")
|
|
|
107
|
+
|
|
|
108
|
+ if (cameraInfo !== null) {
|
|
|
109
|
+ Object.assign(data.camera, JSON.parse(cameraInfo))
|
|
|
110
|
+
|
|
|
111
|
+ // And update the CSS property
|
|
|
112
|
+ document.documentElement.style.setProperty(
|
|
|
113
|
+ "--camera-zoom",
|
|
|
114
|
+ data.camera.zoom.toString()
|
|
|
115
|
+ )
|
|
|
116
|
+ }
|
|
|
117
|
+
|
|
|
118
|
+ return restoredData
|
|
|
119
|
+ }
|
|
|
120
|
+}
|
|
|
121
|
+
|
|
|
122
|
+export default new History()
|