@fieldnotes/core 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -22,6 +22,7 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  AddElementCommand: () => AddElementCommand,
24
24
  ArrowTool: () => ArrowTool,
25
+ AutoSave: () => AutoSave,
25
26
  Background: () => Background,
26
27
  BatchCommand: () => BatchCommand,
27
28
  Camera: () => Camera,
@@ -153,6 +154,65 @@ function migrateElement(obj) {
153
154
  }
154
155
  }
155
156
 
157
+ // src/core/auto-save.ts
158
+ var DEFAULT_KEY = "fieldnotes-autosave";
159
+ var DEFAULT_DEBOUNCE_MS = 1e3;
160
+ var AutoSave = class {
161
+ constructor(store, camera, options = {}) {
162
+ this.store = store;
163
+ this.camera = camera;
164
+ this.key = options.key ?? DEFAULT_KEY;
165
+ this.debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;
166
+ }
167
+ key;
168
+ debounceMs;
169
+ timerId = null;
170
+ unsubscribers = [];
171
+ start() {
172
+ const schedule = () => this.scheduleSave();
173
+ this.unsubscribers = [
174
+ this.store.on("add", schedule),
175
+ this.store.on("remove", schedule),
176
+ this.store.on("update", schedule),
177
+ this.camera.onChange(schedule)
178
+ ];
179
+ }
180
+ stop() {
181
+ this.cancelPending();
182
+ this.unsubscribers.forEach((fn) => fn());
183
+ this.unsubscribers = [];
184
+ }
185
+ load() {
186
+ if (typeof localStorage === "undefined") return null;
187
+ const json = localStorage.getItem(this.key);
188
+ if (!json) return null;
189
+ try {
190
+ return parseState(json);
191
+ } catch {
192
+ return null;
193
+ }
194
+ }
195
+ clear() {
196
+ if (typeof localStorage === "undefined") return;
197
+ localStorage.removeItem(this.key);
198
+ }
199
+ scheduleSave() {
200
+ this.cancelPending();
201
+ this.timerId = setTimeout(() => this.save(), this.debounceMs);
202
+ }
203
+ cancelPending() {
204
+ if (this.timerId !== null) {
205
+ clearTimeout(this.timerId);
206
+ this.timerId = null;
207
+ }
208
+ }
209
+ save() {
210
+ if (typeof localStorage === "undefined") return;
211
+ const state = exportState(this.store.snapshot(), this.camera);
212
+ localStorage.setItem(this.key, JSON.stringify(state));
213
+ }
214
+ };
215
+
156
216
  // src/canvas/camera.ts
157
217
  var DEFAULT_MIN_ZOOM = 0.1;
158
218
  var DEFAULT_MAX_ZOOM = 10;
@@ -2225,6 +2285,7 @@ var VERSION = "0.1.2";
2225
2285
  0 && (module.exports = {
2226
2286
  AddElementCommand,
2227
2287
  ArrowTool,
2288
+ AutoSave,
2228
2289
  Background,
2229
2290
  BatchCommand,
2230
2291
  Camera,