From 63a8e76e8f91add2165b3f1c742ba62664805498 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Mon, 31 Jan 2022 19:24:38 -0800 Subject: [PATCH] Move mindmap loader as part of Client. --- .../src/components/LocalStorageManager.ts | 2 +- .../src/components/model/FeatureModel.ts | 2 +- .../persistence/XMLSerializerPela.ts | 39 +++++++++---------- .../client/cache-decorator-client/index.ts | 6 ++- packages/webapp/src/classes/client/index.ts | 4 ++ .../src/classes/client/mock-client/index.ts | 37 ++++++++++++------ .../src/classes/client/rest-client/index.ts | 11 ++++++ .../action-dispatcher/export-dialog/index.tsx | 14 +++---- 8 files changed, 73 insertions(+), 42 deletions(-) diff --git a/packages/mindplot/src/components/LocalStorageManager.ts b/packages/mindplot/src/components/LocalStorageManager.ts index 66d11890..67087661 100644 --- a/packages/mindplot/src/components/LocalStorageManager.ts +++ b/packages/mindplot/src/components/LocalStorageManager.ts @@ -24,7 +24,7 @@ class LocalStorageManager extends PersistenceManager { private forceLoad: boolean; - constructor(documentUrl, forceLoad) { + constructor(documentUrl: string, forceLoad: boolean) { super(); this.documentUrl = documentUrl; this.forceLoad = forceLoad; diff --git a/packages/mindplot/src/components/model/FeatureModel.ts b/packages/mindplot/src/components/model/FeatureModel.ts index 66156b4c..2b73621b 100644 --- a/packages/mindplot/src/components/model/FeatureModel.ts +++ b/packages/mindplot/src/components/model/FeatureModel.ts @@ -26,7 +26,7 @@ class FeatureModel { private _type: FeatureType; - private _attributes: Record; + private _attributes; /** * @constructs diff --git a/packages/mindplot/src/components/persistence/XMLSerializerPela.ts b/packages/mindplot/src/components/persistence/XMLSerializerPela.ts index 01dfc796..64ed7378 100644 --- a/packages/mindplot/src/components/persistence/XMLSerializerPela.ts +++ b/packages/mindplot/src/components/persistence/XMLSerializerPela.ts @@ -40,7 +40,7 @@ class XMLSerializerPela implements XMLMindmapSerializer { const mapElem = document.createElement('map'); const name = mindmap.getId(); if ($defined(name)) { - mapElem.setAttribute('name', this.rmXmlInv(name)); + mapElem.setAttribute('name', this._rmXmlInv(name)); } const version = mindmap.getVersion(); if ($defined(version)) { @@ -61,7 +61,7 @@ class XMLSerializerPela implements XMLMindmapSerializer { relationships.forEach((relationship) => { if ( mindmap.findNodeById(relationship.getFromNode()) !== null - && mindmap.findNodeById(relationship.getToNode()) !== null + && mindmap.findNodeById(relationship.getToNode()) !== null ) { // Isolated relationships are not persisted .... const relationDom = XMLSerializerPela._relationshipToXML(document, relationship); @@ -72,7 +72,7 @@ class XMLSerializerPela implements XMLMindmapSerializer { return document; } - _topicToXML(document: Document, topic: NodeModel) { + protected _topicToXML(document: Document, topic: NodeModel) { const parentTopic = document.createElement('topic'); // Set topic attributes... @@ -133,10 +133,10 @@ class XMLSerializerPela implements XMLMindmapSerializer { if ( $defined(fontFamily) - || $defined(fontSize) - || $defined(fontColor) - || $defined(fontWeight) - || $defined(fontStyle) + || $defined(fontSize) + || $defined(fontColor) + || $defined(fontWeight) + || $defined(fontStyle) ) { parentTopic.setAttribute('fontStyle', font); } @@ -168,10 +168,10 @@ class XMLSerializerPela implements XMLMindmapSerializer { const key = attributesKeys[attrIndex]; const value = attributes[key]; if (key === 'text') { - const cdata = document.createCDATASection(this.rmXmlInv(value)); + const cdata = document.createCDATASection(this._rmXmlInv(value)); featureDom.appendChild(cdata); } else { - featureDom.setAttribute(key, this.rmXmlInv(value)); + featureDom.setAttribute(key, this._rmXmlInv(value)); } } parentTopic.appendChild(featureDom); @@ -187,12 +187,12 @@ class XMLSerializerPela implements XMLMindmapSerializer { return parentTopic; } - _noteTextToXML(document: Document, elem: Element, text: string) { + protected _noteTextToXML(document: Document, elem: Element, text: string) { if (text.indexOf('\n') === -1) { - elem.setAttribute('text', this.rmXmlInv(text)); + elem.setAttribute('text', this._rmXmlInv(text)); } else { const textDom = document.createElement('text'); - const cdata = document.createCDATASection(this.rmXmlInv(text)); + const cdata = document.createCDATASection(this._rmXmlInv(text)); textDom.appendChild(cdata); elem.appendChild(textDom); } @@ -284,7 +284,7 @@ class XMLSerializerPela implements XMLMindmapSerializer { return mindmap; } - _deserializeNode(domElem: Element, mindmap: Mindmap) { + protected _deserializeNode(domElem: Element, mindmap: Mindmap) { const type = domElem.getAttribute('central') != null ? 'CentralTopic' : 'MainTopic'; // Load attributes... @@ -493,8 +493,7 @@ class XMLSerializerPela implements XMLMindmapSerializer { * @param in The String whose non-valid characters we want to remove. * @return The in String, stripped of non-valid characters. */ - // eslint-disable-next-line class-methods-use-this - rmXmlInv(str) { + protected _rmXmlInv(str: string) { if (str == null || str === undefined) return null; let result = ''; @@ -502,11 +501,11 @@ class XMLSerializerPela implements XMLMindmapSerializer { const c = str.charCodeAt(i); if ( c === 0x9 - || c === 0xa - || c === 0xd - || (c >= 0x20 && c <= 0xd7ff) - || (c >= 0xe000 && c <= 0xfffd) - || (c >= 0x10000 && c <= 0x10ffff) + || c === 0xa + || c === 0xd + || (c >= 0x20 && c <= 0xd7ff) + || (c >= 0xe000 && c <= 0xfffd) + || (c >= 0x10000 && c <= 0x10ffff) ) { result += str.charAt(i); } diff --git a/packages/webapp/src/classes/client/cache-decorator-client/index.ts b/packages/webapp/src/classes/client/cache-decorator-client/index.ts index 5a584dcd..a844904f 100644 --- a/packages/webapp/src/classes/client/cache-decorator-client/index.ts +++ b/packages/webapp/src/classes/client/cache-decorator-client/index.ts @@ -1,3 +1,4 @@ +import { Mindmap } from '@wisemapping/mindplot'; import Client, { AccountInfo, BasicMapInfo, @@ -16,6 +17,9 @@ class CacheDecoratorClient implements Client { constructor(client: Client) { this.client = client; } + fetchMindmap(id: number): Mindmap { + return this.client.fetchMindmap(id); + } deleteAccount(): Promise { return this.client.deleteAccount(); @@ -106,7 +110,7 @@ class CacheDecoratorClient implements Client { } revertHistory(id: number, cid: number): Promise { - return this.client.revertHistory(id,cid); + return this.client.revertHistory(id, cid); } } diff --git a/packages/webapp/src/classes/client/index.ts b/packages/webapp/src/classes/client/index.ts index c5df5ebf..6a0a3971 100644 --- a/packages/webapp/src/classes/client/index.ts +++ b/packages/webapp/src/classes/client/index.ts @@ -1,3 +1,4 @@ +import { Mindmap } from '@wisemapping/mindplot'; import { Locale, LocaleCode } from '../app-i18n'; export type NewUser = { @@ -102,6 +103,9 @@ interface Client { fetchHistory(id: number): Promise; revertHistory(id: number, cid: number): Promise; + + fetchMindmap(id:number): Mindmap; + } export default Client; diff --git a/packages/webapp/src/classes/client/mock-client/index.ts b/packages/webapp/src/classes/client/mock-client/index.ts index 3b0c2368..07a1fa21 100644 --- a/packages/webapp/src/classes/client/mock-client/index.ts +++ b/packages/webapp/src/classes/client/mock-client/index.ts @@ -1,3 +1,5 @@ +import { Mindmap } from '@wisemapping/mindplot'; +import XMLSerializerTango from '@wisemapping/mindplot/src/components/persistence/XMLSerializerTango'; import Client, { AccountInfo, BasicMapInfo, @@ -11,21 +13,21 @@ import Client, { import { LocaleCode, localeFromStr } from '../../app-i18n'; const label1: Label = { - id: 1, - title: 'label 1', - color: 'black', + id: 1, + title: 'label 1', + color: 'black', } const label2: Label = { - id: 2, - title: 'label 2', - color: 'green', + id: 2, + title: 'label 2', + color: 'green', } const label3: Label = { - id: 3, - title: 'label 3', - color: 'red', + id: 3, + title: 'label 3', + color: 'red', } class MockClient implements Client { @@ -81,7 +83,7 @@ class MockClient implements Client { 11, false, 'El Mapa3', - [label1, label2], + [label1, label2], 'Paulo3', '2008-06-02T00:00:00Z', 'Berna', @@ -107,6 +109,17 @@ class MockClient implements Client { this.labels = [label1, label2, label3]; } + fetchMindmap(id: number): Mindmap { + const parser = new DOMParser(); + const xmlDoc = parser.parseFromString(` + + + + `, 'text/xml'); + + const serializer = new XMLSerializerTango(); + return serializer.loadFromDom(xmlDoc, String(id)); + } deleteMapPermission(id: number, email: string): Promise { let perm = this.permissionsByMap.get(id) || []; perm = perm.filter((p) => p.email != email); @@ -325,8 +338,8 @@ class MockClient implements Client { registerNewUser(user: NewUser): Promise { console.log('user:' + user); - if(user.email=="error@example.com"){ - return Promise.reject({msg:"Unexpected error"}); + if (user.email == "error@example.com") { + return Promise.reject({ msg: "Unexpected error" }); } return Promise.resolve(); } diff --git a/packages/webapp/src/classes/client/rest-client/index.ts b/packages/webapp/src/classes/client/rest-client/index.ts index 9b79a148..37ab6c85 100644 --- a/packages/webapp/src/classes/client/rest-client/index.ts +++ b/packages/webapp/src/classes/client/rest-client/index.ts @@ -1,3 +1,4 @@ +import { LocalStorageManager, Mindmap } from '@wisemapping/mindplot'; import axios from 'axios'; import Client, { ErrorInfo, @@ -21,6 +22,16 @@ export default class RestClient implements Client { this.sessionExpired = sessionExpired; } + fetchMindmap(id: number): Mindmap { + // Load mindmap ... + const persistence = new LocalStorageManager( + `/c/restful/maps/{id}/document/xml`, + true + ); + const mindmap = persistence.load(String(id)); + return mindmap; + } + deleteMapPermission(id: number, email: string): Promise { const handler = (success: () => void, reject: (error: ErrorInfo) => void) => { axios diff --git a/packages/webapp/src/components/maps-page/action-dispatcher/export-dialog/index.tsx b/packages/webapp/src/components/maps-page/action-dispatcher/export-dialog/index.tsx index e5595441..44b7d789 100644 --- a/packages/webapp/src/components/maps-page/action-dispatcher/export-dialog/index.tsx +++ b/packages/webapp/src/components/maps-page/action-dispatcher/export-dialog/index.tsx @@ -10,7 +10,11 @@ import FormControlLabel from '@material-ui/core/FormControlLabel'; import Radio from '@material-ui/core/Radio'; import Select from '@material-ui/core/Select'; import MenuItem from '@material-ui/core/MenuItem'; -import { Designer, TextExporterFactory, ImageExporterFactory, Exporter, Mindmap, LocalStorageManager } from '@wisemapping/mindplot'; +import { Designer, TextExporterFactory, ImageExporterFactory, Exporter, Mindmap } from '@wisemapping/mindplot'; +import Client from '../../../../classes/client'; +import { activeInstance } from '../../../../redux/clientSlice'; + +import { useSelector } from 'react-redux'; type ExportFormat = 'svg' | 'jpg' | 'png' | 'txt' | 'mm' | 'wxml' | 'xls' | 'md'; type ExportGroup = 'image' | 'document' | 'mindmap-tool'; @@ -30,6 +34,7 @@ const ExportDialog = ({ const intl = useIntl(); const [submit, setSubmit] = React.useState(false); const { map } = fetchMapById(mapId); + const client: Client = useSelector(activeInstance); const [exportGroup, setExportGroup] = React.useState( enableImgExport ? 'image' : 'document' @@ -83,12 +88,7 @@ const ExportDialog = ({ size = workspace.getSize(); mindmap = designer.getMindmap(); } else { - // Load mindmap ... - const persistence = new LocalStorageManager( - `/c/restful/maps/{id}/document/xml`, - true - ); - mindmap = persistence.load(mapId.toString()); + mindmap = client.fetchMindmap(mapId); } let exporter: Exporter;