Move mindmap loader as part of Client.

This commit is contained in:
Paulo Gustavo Veiga 2022-01-31 19:24:38 -08:00
parent bea6a15282
commit 63a8e76e8f
8 changed files with 73 additions and 42 deletions

View File

@ -24,7 +24,7 @@ class LocalStorageManager extends PersistenceManager {
private forceLoad: boolean; private forceLoad: boolean;
constructor(documentUrl, forceLoad) { constructor(documentUrl: string, forceLoad: boolean) {
super(); super();
this.documentUrl = documentUrl; this.documentUrl = documentUrl;
this.forceLoad = forceLoad; this.forceLoad = forceLoad;

View File

@ -26,7 +26,7 @@ class FeatureModel {
private _type: FeatureType; private _type: FeatureType;
private _attributes: Record<string, unknown>; private _attributes;
/** /**
* @constructs * @constructs

View File

@ -40,7 +40,7 @@ class XMLSerializerPela implements XMLMindmapSerializer {
const mapElem = document.createElement('map'); const mapElem = document.createElement('map');
const name = mindmap.getId(); const name = mindmap.getId();
if ($defined(name)) { if ($defined(name)) {
mapElem.setAttribute('name', this.rmXmlInv(name)); mapElem.setAttribute('name', this._rmXmlInv(name));
} }
const version = mindmap.getVersion(); const version = mindmap.getVersion();
if ($defined(version)) { if ($defined(version)) {
@ -61,7 +61,7 @@ class XMLSerializerPela implements XMLMindmapSerializer {
relationships.forEach((relationship) => { relationships.forEach((relationship) => {
if ( if (
mindmap.findNodeById(relationship.getFromNode()) !== null mindmap.findNodeById(relationship.getFromNode()) !== null
&& mindmap.findNodeById(relationship.getToNode()) !== null && mindmap.findNodeById(relationship.getToNode()) !== null
) { ) {
// Isolated relationships are not persisted .... // Isolated relationships are not persisted ....
const relationDom = XMLSerializerPela._relationshipToXML(document, relationship); const relationDom = XMLSerializerPela._relationshipToXML(document, relationship);
@ -72,7 +72,7 @@ class XMLSerializerPela implements XMLMindmapSerializer {
return document; return document;
} }
_topicToXML(document: Document, topic: NodeModel) { protected _topicToXML(document: Document, topic: NodeModel) {
const parentTopic = document.createElement('topic'); const parentTopic = document.createElement('topic');
// Set topic attributes... // Set topic attributes...
@ -133,10 +133,10 @@ class XMLSerializerPela implements XMLMindmapSerializer {
if ( if (
$defined(fontFamily) $defined(fontFamily)
|| $defined(fontSize) || $defined(fontSize)
|| $defined(fontColor) || $defined(fontColor)
|| $defined(fontWeight) || $defined(fontWeight)
|| $defined(fontStyle) || $defined(fontStyle)
) { ) {
parentTopic.setAttribute('fontStyle', font); parentTopic.setAttribute('fontStyle', font);
} }
@ -168,10 +168,10 @@ class XMLSerializerPela implements XMLMindmapSerializer {
const key = attributesKeys[attrIndex]; const key = attributesKeys[attrIndex];
const value = attributes[key]; const value = attributes[key];
if (key === 'text') { if (key === 'text') {
const cdata = document.createCDATASection(this.rmXmlInv(value)); const cdata = document.createCDATASection(this._rmXmlInv(value));
featureDom.appendChild(cdata); featureDom.appendChild(cdata);
} else { } else {
featureDom.setAttribute(key, this.rmXmlInv(value)); featureDom.setAttribute(key, this._rmXmlInv(value));
} }
} }
parentTopic.appendChild(featureDom); parentTopic.appendChild(featureDom);
@ -187,12 +187,12 @@ class XMLSerializerPela implements XMLMindmapSerializer {
return parentTopic; return parentTopic;
} }
_noteTextToXML(document: Document, elem: Element, text: string) { protected _noteTextToXML(document: Document, elem: Element, text: string) {
if (text.indexOf('\n') === -1) { if (text.indexOf('\n') === -1) {
elem.setAttribute('text', this.rmXmlInv(text)); elem.setAttribute('text', this._rmXmlInv(text));
} else { } else {
const textDom = document.createElement('text'); const textDom = document.createElement('text');
const cdata = document.createCDATASection(this.rmXmlInv(text)); const cdata = document.createCDATASection(this._rmXmlInv(text));
textDom.appendChild(cdata); textDom.appendChild(cdata);
elem.appendChild(textDom); elem.appendChild(textDom);
} }
@ -284,7 +284,7 @@ class XMLSerializerPela implements XMLMindmapSerializer {
return mindmap; return mindmap;
} }
_deserializeNode(domElem: Element, mindmap: Mindmap) { protected _deserializeNode(domElem: Element, mindmap: Mindmap) {
const type = domElem.getAttribute('central') != null ? 'CentralTopic' : 'MainTopic'; const type = domElem.getAttribute('central') != null ? 'CentralTopic' : 'MainTopic';
// Load attributes... // Load attributes...
@ -493,8 +493,7 @@ class XMLSerializerPela implements XMLMindmapSerializer {
* @param in The String whose non-valid characters we want to remove. * @param in The String whose non-valid characters we want to remove.
* @return The in String, stripped of non-valid characters. * @return The in String, stripped of non-valid characters.
*/ */
// eslint-disable-next-line class-methods-use-this protected _rmXmlInv(str: string) {
rmXmlInv(str) {
if (str == null || str === undefined) return null; if (str == null || str === undefined) return null;
let result = ''; let result = '';
@ -502,11 +501,11 @@ class XMLSerializerPela implements XMLMindmapSerializer {
const c = str.charCodeAt(i); const c = str.charCodeAt(i);
if ( if (
c === 0x9 c === 0x9
|| c === 0xa || c === 0xa
|| c === 0xd || c === 0xd
|| (c >= 0x20 && c <= 0xd7ff) || (c >= 0x20 && c <= 0xd7ff)
|| (c >= 0xe000 && c <= 0xfffd) || (c >= 0xe000 && c <= 0xfffd)
|| (c >= 0x10000 && c <= 0x10ffff) || (c >= 0x10000 && c <= 0x10ffff)
) { ) {
result += str.charAt(i); result += str.charAt(i);
} }

View File

@ -1,3 +1,4 @@
import { Mindmap } from '@wisemapping/mindplot';
import Client, { import Client, {
AccountInfo, AccountInfo,
BasicMapInfo, BasicMapInfo,
@ -16,6 +17,9 @@ class CacheDecoratorClient implements Client {
constructor(client: Client) { constructor(client: Client) {
this.client = client; this.client = client;
} }
fetchMindmap(id: number): Mindmap {
return this.client.fetchMindmap(id);
}
deleteAccount(): Promise<void> { deleteAccount(): Promise<void> {
return this.client.deleteAccount(); return this.client.deleteAccount();
@ -106,7 +110,7 @@ class CacheDecoratorClient implements Client {
} }
revertHistory(id: number, cid: number): Promise<void> { revertHistory(id: number, cid: number): Promise<void> {
return this.client.revertHistory(id,cid); return this.client.revertHistory(id, cid);
} }
} }

View File

@ -1,3 +1,4 @@
import { Mindmap } from '@wisemapping/mindplot';
import { Locale, LocaleCode } from '../app-i18n'; import { Locale, LocaleCode } from '../app-i18n';
export type NewUser = { export type NewUser = {
@ -102,6 +103,9 @@ interface Client {
fetchHistory(id: number): Promise<ChangeHistory[]>; fetchHistory(id: number): Promise<ChangeHistory[]>;
revertHistory(id: number, cid: number): Promise<void>; revertHistory(id: number, cid: number): Promise<void>;
fetchMindmap(id:number): Mindmap;
} }
export default Client; export default Client;

View File

@ -1,3 +1,5 @@
import { Mindmap } from '@wisemapping/mindplot';
import XMLSerializerTango from '@wisemapping/mindplot/src/components/persistence/XMLSerializerTango';
import Client, { import Client, {
AccountInfo, AccountInfo,
BasicMapInfo, BasicMapInfo,
@ -11,21 +13,21 @@ import Client, {
import { LocaleCode, localeFromStr } from '../../app-i18n'; import { LocaleCode, localeFromStr } from '../../app-i18n';
const label1: Label = { const label1: Label = {
id: 1, id: 1,
title: 'label 1', title: 'label 1',
color: 'black', color: 'black',
} }
const label2: Label = { const label2: Label = {
id: 2, id: 2,
title: 'label 2', title: 'label 2',
color: 'green', color: 'green',
} }
const label3: Label = { const label3: Label = {
id: 3, id: 3,
title: 'label 3', title: 'label 3',
color: 'red', color: 'red',
} }
class MockClient implements Client { class MockClient implements Client {
@ -81,7 +83,7 @@ class MockClient implements Client {
11, 11,
false, false,
'El Mapa3', 'El Mapa3',
[label1, label2], [label1, label2],
'Paulo3', 'Paulo3',
'2008-06-02T00:00:00Z', '2008-06-02T00:00:00Z',
'Berna', 'Berna',
@ -107,6 +109,17 @@ class MockClient implements Client {
this.labels = [label1, label2, label3]; this.labels = [label1, label2, label3];
} }
fetchMindmap(id: number): Mindmap {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(`
<map name="${id}" version="tango">
<topic central="true" text="This is the map ${id}" id="1" fontStyle=";;#ffffff;;;"></topic>
</map>
`, 'text/xml');
const serializer = new XMLSerializerTango();
return serializer.loadFromDom(xmlDoc, String(id));
}
deleteMapPermission(id: number, email: string): Promise<void> { deleteMapPermission(id: number, email: string): Promise<void> {
let perm = this.permissionsByMap.get(id) || []; let perm = this.permissionsByMap.get(id) || [];
perm = perm.filter((p) => p.email != email); perm = perm.filter((p) => p.email != email);
@ -325,8 +338,8 @@ class MockClient implements Client {
registerNewUser(user: NewUser): Promise<void> { registerNewUser(user: NewUser): Promise<void> {
console.log('user:' + user); console.log('user:' + user);
if(user.email=="error@example.com"){ if (user.email == "error@example.com") {
return Promise.reject({msg:"Unexpected error"}); return Promise.reject({ msg: "Unexpected error" });
} }
return Promise.resolve(); return Promise.resolve();
} }

View File

@ -1,3 +1,4 @@
import { LocalStorageManager, Mindmap } from '@wisemapping/mindplot';
import axios from 'axios'; import axios from 'axios';
import Client, { import Client, {
ErrorInfo, ErrorInfo,
@ -21,6 +22,16 @@ export default class RestClient implements Client {
this.sessionExpired = sessionExpired; 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<void> { deleteMapPermission(id: number, email: string): Promise<void> {
const handler = (success: () => void, reject: (error: ErrorInfo) => void) => { const handler = (success: () => void, reject: (error: ErrorInfo) => void) => {
axios axios

View File

@ -10,7 +10,11 @@ import FormControlLabel from '@material-ui/core/FormControlLabel';
import Radio from '@material-ui/core/Radio'; import Radio from '@material-ui/core/Radio';
import Select from '@material-ui/core/Select'; import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem'; 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 ExportFormat = 'svg' | 'jpg' | 'png' | 'txt' | 'mm' | 'wxml' | 'xls' | 'md';
type ExportGroup = 'image' | 'document' | 'mindmap-tool'; type ExportGroup = 'image' | 'document' | 'mindmap-tool';
@ -30,6 +34,7 @@ const ExportDialog = ({
const intl = useIntl(); const intl = useIntl();
const [submit, setSubmit] = React.useState<boolean>(false); const [submit, setSubmit] = React.useState<boolean>(false);
const { map } = fetchMapById(mapId); const { map } = fetchMapById(mapId);
const client: Client = useSelector(activeInstance);
const [exportGroup, setExportGroup] = React.useState<ExportGroup>( const [exportGroup, setExportGroup] = React.useState<ExportGroup>(
enableImgExport ? 'image' : 'document' enableImgExport ? 'image' : 'document'
@ -83,12 +88,7 @@ const ExportDialog = ({
size = workspace.getSize(); size = workspace.getSize();
mindmap = designer.getMindmap(); mindmap = designer.getMindmap();
} else { } else {
// Load mindmap ... mindmap = client.fetchMindmap(mapId);
const persistence = new LocalStorageManager(
`/c/restful/maps/{id}/document/xml`,
true
);
mindmap = persistence.load(mapId.toString());
} }
let exporter: Exporter; let exporter: Exporter;