From b9de72905304c9b550ad9d229350601950eabfe7 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Wed, 23 Nov 2022 21:32:15 -0800 Subject: [PATCH] Add not null control for mindplot initialization --- packages/editor/src/components/index.tsx | 43 +++++++++++-------- .../src/components/MindplotWebComponent.ts | 39 ++++------------- 2 files changed, 35 insertions(+), 47 deletions(-) diff --git a/packages/editor/src/components/index.tsx b/packages/editor/src/components/index.tsx index 2db25438..fe4ee8e9 100644 --- a/packages/editor/src/components/index.tsx +++ b/packages/editor/src/components/index.tsx @@ -78,24 +78,33 @@ const Editor = ({ const [popoverOpen, popoverTarget, widgetManager] = DefaultWidgetManager.useCreate(); const capability = new Capability(options.mode, mapInfo.isLocked()); - const mindplotRef = useCallback((component: MindplotWebComponent) => { - const model = new Model(component); + const mindplotRef: (component: MindplotWebComponent) => void = useCallback( + (component: MindplotWebComponent) => { + if (!component) { + const error = new Error(`Unexpected error during initialization. ${component}`); + window.newrelic?.noticeError(error); + throw error; + } - // Force refresh after map load ... - model - .loadMindmap(mapInfo.getId(), persistenceManager, widgetManager) - .then(() => { - setCanvasUpdate(Date.now()); - model.registerEvents(setCanvasUpdate, capability); - }) - .catch((e) => { - console.error(e); - window.newrelic?.noticeError( - new Error(`Unexpected error loading map ${mapInfo.getId()} = ${JSON.stringify(e)}`), - ); - }); - setModel(model); - }, []); + const model = new Model(component); + + // Force refresh after map load ... + model + .loadMindmap(mapInfo.getId(), persistenceManager, widgetManager) + .then(() => { + setCanvasUpdate(Date.now()); + model.registerEvents(setCanvasUpdate, capability); + }) + .catch((e) => { + console.error(e); + window.newrelic?.noticeError( + new Error(`Unexpected error loading map ${mapInfo.getId()} = ${JSON.stringify(e)}`), + ); + }); + setModel(model); + }, + [], + ); useEffect(() => { if (options.enableKeyboardEvents) { diff --git a/packages/mindplot/src/components/MindplotWebComponent.ts b/packages/mindplot/src/components/MindplotWebComponent.ts index 27b74041..b0b0675b 100644 --- a/packages/mindplot/src/components/MindplotWebComponent.ts +++ b/packages/mindplot/src/components/MindplotWebComponent.ts @@ -2,20 +2,18 @@ import Designer from './Designer'; import buildDesigner from './DesignerBuilder'; import DesignerOptionsBuilder from './DesignerOptionsBuilder'; import EditorRenderMode from './EditorRenderMode'; -import LocalStorageManager from './LocalStorageManager'; import PersistenceManager from './PersistenceManager'; import WidgetManager from './WidgetManager'; import mindplotStyles from './styles/mindplot-styles'; import { $notify } from './widget/ToolbarNotifier'; import { $msg } from './Messages'; import DesignerKeyboard from './DesignerKeyboard'; - -const defaultPersistenceManager = () => new LocalStorageManager('map.xml', false, false); +import LocalStorageManager from './LocalStorageManager'; export type MindplotWebComponentInterface = { id: string; mode: string; - ref: any; + ref: (component: object) => void; locale?: string; }; /** @@ -35,12 +33,15 @@ class MindplotWebComponent extends HTMLElement { constructor() { super(); this._shadowRoot = this.attachShadow({ mode: 'open' }); + const mindplotStylesElement = document.createElement('style'); mindplotStylesElement.innerHTML = mindplotStyles; this._shadowRoot.appendChild(mindplotStylesElement); + const wrapper = document.createElement('div'); wrapper.setAttribute('class', 'wise-editor'); wrapper.setAttribute('id', 'mindplot'); + this._shadowRoot.appendChild(wrapper); } @@ -59,7 +60,8 @@ class MindplotWebComponent extends HTMLElement { buildDesigner(persistence?: PersistenceManager, widgetManager?: WidgetManager) { const editorRenderMode = this.getAttribute('mode') as EditorRenderMode; const locale = this.getAttribute('locale'); - const persistenceManager = persistence || defaultPersistenceManager(); + + const persistenceManager = persistence || new LocalStorageManager('map.xml', false, false); const mode = editorRenderMode || 'viewonly'; const options = DesignerOptionsBuilder.buildOptions({ persistenceManager, @@ -97,26 +99,19 @@ class MindplotWebComponent extends HTMLElement { } } - setSaveRequired(arg0: boolean) { - this.saveRequired = arg0; + setSaveRequired(value: boolean) { + this.saveRequired = value; } getSaveRequired() { return this.saveRequired; } - /** - * Load map in designer throught persistence manager instance - * @param id the map id to be loaded. - */ loadMap(id: string): Promise { const instance = PersistenceManager.getInstance(); return instance.load(id).then((mindmap) => this._designer.loadMap(mindmap)); } - /** - * save the map - */ save(saveHistory: boolean) { if (!saveHistory && !this.getSaveRequired()) return; console.log('Saving...'); @@ -146,22 +141,6 @@ class MindplotWebComponent extends HTMLElement { this.setSaveRequired(false); } - discardChanges() { - // Avoid autosave before leaving the page .... - // this.setRequireChange(false); - - // Finally call discard function ... - const persistenceManager = PersistenceManager.getInstance(); - const mindmap = this._designer.getMindmap(); - persistenceManager.discardChanges(mindmap.getId()); - - // Unlock map ... - this.unlockMap(); - - // Reload the page ... - window.location.reload(); - } - unlockMap() { const mindmap = this._designer.getMindmap(); const persistenceManager = PersistenceManager.getInstance();