@fieldnotes/core 0.1.1 → 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;
@@ -220,6 +280,7 @@ var Camera = class {
220
280
  };
221
281
 
222
282
  // src/canvas/background.ts
283
+ var MIN_PATTERN_SPACING = 16;
223
284
  var DEFAULTS = {
224
285
  pattern: "dots",
225
286
  spacing: 24,
@@ -255,8 +316,15 @@ var Background = class {
255
316
  }
256
317
  ctx.restore();
257
318
  }
319
+ adaptSpacing(baseSpacing, zoom) {
320
+ let spacing = baseSpacing * zoom;
321
+ while (spacing < MIN_PATTERN_SPACING) {
322
+ spacing *= 2;
323
+ }
324
+ return spacing;
325
+ }
258
326
  renderDots(ctx, camera, width, height) {
259
- const spacing = this.spacing * camera.zoom;
327
+ const spacing = this.adaptSpacing(this.spacing, camera.zoom);
260
328
  const offsetX = camera.position.x % spacing;
261
329
  const offsetY = camera.position.y % spacing;
262
330
  const radius = this.dotRadius * Math.min(camera.zoom, 2);
@@ -271,7 +339,7 @@ var Background = class {
271
339
  ctx.fill();
272
340
  }
273
341
  renderGrid(ctx, camera, width, height) {
274
- const spacing = this.spacing * camera.zoom;
342
+ const spacing = this.adaptSpacing(this.spacing, camera.zoom);
275
343
  const offsetX = camera.position.x % spacing;
276
344
  const offsetY = camera.position.y % spacing;
277
345
  const lineW = this.lineWidth * Math.min(camera.zoom, 2);
@@ -2212,11 +2280,12 @@ var ImageTool = class {
2212
2280
  };
2213
2281
 
2214
2282
  // src/index.ts
2215
- var VERSION = "0.1.0";
2283
+ var VERSION = "0.1.2";
2216
2284
  // Annotate the CommonJS export names for ESM import in node:
2217
2285
  0 && (module.exports = {
2218
2286
  AddElementCommand,
2219
2287
  ArrowTool,
2288
+ AutoSave,
2220
2289
  Background,
2221
2290
  BatchCommand,
2222
2291
  Camera,