From b3935ac3f30592b12d3c3df908b2b71c0454e824 Mon Sep 17 00:00:00 2001 From: pgallino Date: Thu, 14 Nov 2024 00:12:40 -0300 Subject: [PATCH 1/3] fix: re-subscribe to the graph on 'new' click to ensure auto-save works correctly --- src/index.ts | 41 ++++++++++++++++++++++++++--------------- src/style.css | 3 ++- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index c7c18fbc..3aef2109 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,17 +34,18 @@ export class GlobalContext { private viewport: Viewport = null; private datagraph: DataGraph; private viewgraph: ViewGraph; + private saveIntervalId: NodeJS.Timeout | null = null; initialize(viewport: Viewport) { this.viewport = viewport; - this.datagraph = new DataGraph(); - this.viewgraph = new ViewGraph(this.datagraph, this.viewport); + loadFromLocalStorage(this); } load(datagraph: DataGraph) { this.datagraph = datagraph; this.viewport.clear(); this.viewgraph = new ViewGraph(this.datagraph, this.viewport); + this.setupAutoSave(); } getViewport() { @@ -58,6 +59,29 @@ export class GlobalContext { getDataGraph() { return this.datagraph; } + + private setupAutoSave() { + + this.clearAutoSave(); + + this.datagraph.subscribeChanges(() => { + if (this.saveIntervalId) { + clearInterval(this.saveIntervalId); + } + this.saveIntervalId = setInterval(() => { + saveToLocalStorage(this); + clearInterval(this.saveIntervalId); + }, 100); + }); + } + + private clearAutoSave() { + // Limpia el intervalo y evita duplicados + if (this.saveIntervalId) { + clearInterval(this.saveIntervalId); + this.saveIntervalId = null; + } + } } // > graphics.ts @@ -387,18 +411,5 @@ export class RightBar { // TODO: load from local storage directly, without first generating a context loadFromLocalStorage(ctx); - let saveIntervalId: NodeJS.Timeout | null = null; - - ctx.getDataGraph().subscribeChanges(() => { - // Wait a bit after the last change to save - if (saveIntervalId) { - clearInterval(saveIntervalId); - } - saveIntervalId = setInterval(() => { - saveToLocalStorage(ctx); - clearInterval(saveIntervalId); - }, 100); - }); - console.log("initialized!"); })(); diff --git a/src/style.css b/src/style.css index 1cc1e545..fe58f3a1 100644 --- a/src/style.css +++ b/src/style.css @@ -118,7 +118,8 @@ canvas { /* Hover effect for save and load buttons */ .save-button:hover, -.load-button:hover { +.load-button:hover, +.new-button:hover { background-color: #0056b3; /* Darker background on hover */ } From 9783c4cfce3cdc3bfbcdeb510ed17c3da4da0caf Mon Sep 17 00:00:00 2001 From: pgallino Date: Thu, 14 Nov 2024 00:21:27 -0300 Subject: [PATCH 2/3] fix: ensure localStorage updates immediately when a new graph is loaded --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 3aef2109..4871ab0f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,6 +46,7 @@ export class GlobalContext { this.viewport.clear(); this.viewgraph = new ViewGraph(this.datagraph, this.viewport); this.setupAutoSave(); + saveToLocalStorage(this); } getViewport() { From 0f641e18a728a2736a9d44d77658633cd373206d Mon Sep 17 00:00:00 2001 From: pgallino Date: Thu, 14 Nov 2024 00:22:41 -0300 Subject: [PATCH 3/3] fix: lint errors --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 4871ab0f..05704c11 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,7 +62,6 @@ export class GlobalContext { } private setupAutoSave() { - this.clearAutoSave(); this.datagraph.subscribeChanges(() => {