From 2d6bde66d85ad60484faf9f05bd8be34473f4c1a Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sun, 13 Mar 2022 16:36:39 -0300 Subject: [PATCH] Improve save logic. --- packages/editor/src/classes/menu/Menu.ts | 33 +++++++++---------- .../src/components/LocalStorageManager.ts | 5 +-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/editor/src/classes/menu/Menu.ts b/packages/editor/src/classes/menu/Menu.ts index 21f1a556..eb2a4a9b 100644 --- a/packages/editor/src/classes/menu/Menu.ts +++ b/packages/editor/src/classes/menu/Menu.ts @@ -33,8 +33,6 @@ import { $msg } from '@wisemapping/mindplot'; class Menu extends IMenu { constructor(designer: Designer, containerId: string, readOnly = false, baseUrl = '') { super(designer, containerId); - const saveElem = $('#save'); - const widgetsBaseUrl = `${baseUrl}css/widget`; // Create panels ... @@ -274,30 +272,31 @@ class Menu extends IMenu { Menu._registerTooltip('fontItalic', $msg('FONT_ITALIC'), 'meta+I'); - if (saveElem) { + if (!readOnly) { + // Register action on save ... + const saveElem = $('#save'); this._addButton('save', false, false, () => { this.save(saveElem, designer, true); }); Menu._registerTooltip('save', $msg('SAVE'), 'meta+S'); - if (!readOnly) { - window.addEventListener('beforeunload', () => { + // Register unload save ... + window.addEventListener('beforeunload', () => { + if (this.isSaveRequired()) { + this.save(saveElem, designer, false); + } + this.unlockMap(designer); + }); + + // Autosave on a fixed period of time ... + setInterval( + () => { if (this.isSaveRequired()) { this.save(saveElem, designer, false); } - this.unlockMap(designer); - }); - - // Autosave on a fixed period of time ... - setInterval( - () => { - if (this.isSaveRequired()) { - this.save(saveElem, designer, false); - } - }, 10000, - ); - } + }, 10000, + ); } } diff --git a/packages/mindplot/src/components/LocalStorageManager.ts b/packages/mindplot/src/components/LocalStorageManager.ts index ae35abe3..62ec4b5c 100644 --- a/packages/mindplot/src/components/LocalStorageManager.ts +++ b/packages/mindplot/src/components/LocalStorageManager.ts @@ -32,12 +32,13 @@ class LocalStorageManager extends PersistenceManager { this.readOnly = readOnly; } - saveMapXml(mapId: string, mapDoc: Document): void { + saveMapXml(mapId: string, mapDoc: Document, _pref: string, _saveHistory: boolean, events): void { const mapXml = new XMLSerializer().serializeToString(mapDoc); if (!this.readOnly) { localStorage.setItem(`${mapId}-xml`, mapXml); + events.onSuccess(); } - console.log(`Map XML to save => ${this.saveMapXml}`); + console.log(`Map XML to save => ${mapXml}`); } discardChanges(mapId: string) {