From d91d6a247243a58ba9102c556e59121c4681431c Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Mon, 3 Jan 2022 08:27:00 -0800 Subject: [PATCH] Migrate serializer to typescript --- .../src/components/model/LinkModel.ts | 5 --- .../src/components/model/NodeModel.ts | 2 - .../src/components/model/NoteModel.ts | 4 +- .../src/components/model/RelationshipModel.ts | 27 ++++---------- ...a2PelaMigrator.js => Beta2PelaMigrator.ts} | 21 +++++++---- .../{ModelCodeName.js => ModelCodeName.ts} | 4 -- ...TangoMigrator.js => Pela2TangoMigrator.ts} | 37 +++++++++++-------- .../persistence/XMLMindmapSerializer.ts | 26 +++++++++++++ .../persistence/XMLSerializerBeta.ts | 15 ++++---- ...izerFactory.js => XMLSerializerFactory.ts} | 9 +++-- .../persistence/XMLSerializerPela.ts | 22 ++++++----- .../src/components/persistence/index.js | 17 --------- .../test/unit/export/expected/complex.wxml | 4 +- 13 files changed, 97 insertions(+), 96 deletions(-) rename packages/mindplot/src/components/persistence/{Beta2PelaMigrator.js => Beta2PelaMigrator.ts} (71%) rename packages/mindplot/src/components/persistence/{ModelCodeName.js => ModelCodeName.ts} (93%) rename packages/mindplot/src/components/persistence/{Pela2TangoMigrator.js => Pela2TangoMigrator.ts} (70%) create mode 100644 packages/mindplot/src/components/persistence/XMLMindmapSerializer.ts rename packages/mindplot/src/components/persistence/{XMLSerializerFactory.js => XMLSerializerFactory.ts} (90%) delete mode 100644 packages/mindplot/src/components/persistence/index.js diff --git a/packages/mindplot/src/components/model/LinkModel.ts b/packages/mindplot/src/components/model/LinkModel.ts index 346f871f..6b8a293d 100644 --- a/packages/mindplot/src/components/model/LinkModel.ts +++ b/packages/mindplot/src/components/model/LinkModel.ts @@ -24,15 +24,10 @@ class LinkModel extends FeatureModel { this.setUrl(attributes.url); } - /** @return {String} the url attribute value */ getUrl():string { return this.getAttribute('url'); } - /** - * @param {String} url a URL provided by the user to set the link to - * @throws will throw an error if url is null or undefined - */ setUrl(url: string): void { $assert(url, 'url can not be null'); diff --git a/packages/mindplot/src/components/model/NodeModel.ts b/packages/mindplot/src/components/model/NodeModel.ts index b4a8cd07..e791fd64 100644 --- a/packages/mindplot/src/components/model/NodeModel.ts +++ b/packages/mindplot/src/components/model/NodeModel.ts @@ -167,7 +167,6 @@ class NodeModel extends INodeModel { append(child: NodeModel) { $assert(child && child.isNodeModel(), 'Only NodeModel can be appended to Mindmap object'); this._children.push(child); - // eslint-disable-next-line no-param-reassign child._parent = this; } @@ -178,7 +177,6 @@ class NodeModel extends INodeModel { removeChild(child: NodeModel): void { $assert(child && child.isNodeModel(), 'Only NodeModel can be appended to Mindmap object.'); this._children = this._children.filter((c) => c !== child); - // eslint-disable-next-line no-param-reassign child._parent = null; } diff --git a/packages/mindplot/src/components/model/NoteModel.ts b/packages/mindplot/src/components/model/NoteModel.ts index 8d289931..2080aaff 100644 --- a/packages/mindplot/src/components/model/NoteModel.ts +++ b/packages/mindplot/src/components/model/NoteModel.ts @@ -26,12 +26,12 @@ class NoteModel extends FeatureModel { } /** */ - getText() { + getText():string { return this.getAttribute('text'); } /** */ - setText(text) { + setText(text:string) { $assert(text, 'text can not be null'); this.setAttribute('text', text); } diff --git a/packages/mindplot/src/components/model/RelationshipModel.ts b/packages/mindplot/src/components/model/RelationshipModel.ts index 71c3441b..fb4213c6 100644 --- a/packages/mindplot/src/components/model/RelationshipModel.ts +++ b/packages/mindplot/src/components/model/RelationshipModel.ts @@ -46,69 +46,56 @@ class RelationshipModel { this._startArrow = false; } - /** */ - getFromNode() { + getFromNode(): number { return this._sourceTargetId; } - /** */ - getToNode() { + getToNode(): number { return this._targetTopicId; } - /** */ - getId():number { + getId(): number { $assert(this._id, 'id is null'); return this._id; } - /** */ getLineType() { return this._lineType; } - /** */ setLineType(lineType: number) { this._lineType = lineType; } - /** */ getSrcCtrlPoint(): Point { return this._srcCtrlPoint; } - /** */ setSrcCtrlPoint(srcCtrlPoint: Point) { this._srcCtrlPoint = srcCtrlPoint; } - /** */ getDestCtrlPoint(): Point { return this._destCtrlPoint; } - /** */ - setDestCtrlPoint(destCtrlPoint: Point) { + setDestCtrlPoint(destCtrlPoint: Point): void { this._destCtrlPoint = destCtrlPoint; } - /** */ - getEndArrow() { + getEndArrow(): boolean { return this._endArrow; } - /** */ setEndArrow(endArrow: boolean) { this._endArrow = endArrow; } - /** */ - getStartArrow() { + getStartArrow(): boolean { return this._startArrow; } - /** */ - setStartArrow(startArrow: boolean) { + setStartArrow(startArrow: boolean): void { this._startArrow = startArrow; } diff --git a/packages/mindplot/src/components/persistence/Beta2PelaMigrator.js b/packages/mindplot/src/components/persistence/Beta2PelaMigrator.ts similarity index 71% rename from packages/mindplot/src/components/persistence/Beta2PelaMigrator.js rename to packages/mindplot/src/components/persistence/Beta2PelaMigrator.ts index d85fe576..93fbdb00 100644 --- a/packages/mindplot/src/components/persistence/Beta2PelaMigrator.js +++ b/packages/mindplot/src/components/persistence/Beta2PelaMigrator.ts @@ -16,20 +16,25 @@ * limitations under the License. */ import { $assert, $defined } from '@wisemapping/core-js'; +import { Mindmap } from '../..'; +import NodeModel from '../model/NodeModel'; import ModelCodeName from './ModelCodeName'; -import XMLSerializer from './XMLSerializerPela'; +import XMLMindmapSerializer from './XMLMindmapSerializer'; +import XMLSerializerPela from './XMLSerializerPela'; -class Beta2PelaMigrator { - constructor(betaSerializer) { +class Beta2PelaMigrator implements XMLMindmapSerializer { + private _betaSerializer: XMLMindmapSerializer; + private _pelaSerializer: XMLSerializerPela; + constructor(betaSerializer: XMLSerializerPela) { this._betaSerializer = betaSerializer; - this._pelaSerializer = new XMLSerializer(); + this._pelaSerializer = new XMLSerializerPela(); } - toXML(mindmap) { + toXML(mindmap: Mindmap) { return this._pelaSerializer.toXML(mindmap); } - loadFromDom(dom, mapId) { + loadFromDom(dom: Document, mapId: string): Mindmap { $assert($defined(mapId), 'mapId can not be null'); const mindmap = this._betaSerializer.loadFromDom(dom, mapId); mindmap.setVersion(ModelCodeName.PELA); @@ -44,13 +49,13 @@ class Beta2PelaMigrator { return mindmap; } - _fixPosition(parentModel) { + private _fixPosition(parentModel: NodeModel) { const parentPos = parentModel.getPosition(); const isRight = parentPos.x > 0; const me = this; parentModel.getChildren().forEach((child) => { if (!child.getPosition()) { - child.setPosition(parentPos.x + (50 * isRight ? 1 : -1), parentPos.y); + child.setPosition(parentPos.x + (isRight ? 1 : -1), parentPos.y); } me._fixPosition(child); }); diff --git a/packages/mindplot/src/components/persistence/ModelCodeName.js b/packages/mindplot/src/components/persistence/ModelCodeName.ts similarity index 93% rename from packages/mindplot/src/components/persistence/ModelCodeName.js rename to packages/mindplot/src/components/persistence/ModelCodeName.ts index 525eee63..ae8bb037 100644 --- a/packages/mindplot/src/components/persistence/ModelCodeName.js +++ b/packages/mindplot/src/components/persistence/ModelCodeName.ts @@ -16,10 +16,6 @@ * limitations under the License. */ -/** - * Enum for wisemap version names - * @enum {String} - */ const ModelCodeName = { BETA: 'beta', PELA: 'pela', diff --git a/packages/mindplot/src/components/persistence/Pela2TangoMigrator.js b/packages/mindplot/src/components/persistence/Pela2TangoMigrator.ts similarity index 70% rename from packages/mindplot/src/components/persistence/Pela2TangoMigrator.js rename to packages/mindplot/src/components/persistence/Pela2TangoMigrator.ts index 7cd348de..abbab104 100644 --- a/packages/mindplot/src/components/persistence/Pela2TangoMigrator.js +++ b/packages/mindplot/src/components/persistence/Pela2TangoMigrator.ts @@ -18,18 +18,23 @@ import { $assert, $defined } from '@wisemapping/core-js'; import XMLSerializer from './XMLSerializerTango'; import ModelCodeName from './ModelCodeName'; +import XMLMindmapSerializer from './XMLMindmapSerializer'; +import Mindmap from '../model/Mindmap'; +import NodeModel from '../model/NodeModel'; -class Pela2TangoMigrator { - constructor(pelaSerializer) { +class Pela2TangoMigrator implements XMLMindmapSerializer { + private _pelaSerializer: XMLMindmapSerializer; + private _tangoSerializer: XMLSerializer; + constructor(pelaSerializer: XMLMindmapSerializer) { this._pelaSerializer = pelaSerializer; this._tangoSerializer = new XMLSerializer(); } - toXML(mindmap) { + toXML(mindmap: Mindmap): Document { return this._tangoSerializer.toXML(mindmap); } - loadFromDom(dom, mapId) { + loadFromDom(dom: Document, mapId: string): Mindmap { $assert($defined(mapId), 'mapId can not be null'); const mindmap = this._pelaSerializer.loadFromDom(dom, mapId); mindmap.setVersion(ModelCodeName.TANGO); @@ -38,23 +43,23 @@ class Pela2TangoMigrator { return mindmap; } - _fixOrder(mindmap) { + private _fixOrder(mindmap: Mindmap) { // First level node policies has been changed. - const centralNode = mindmap.getBranches()[0]; - const children = centralNode.getChildren(); - const leftNodes = []; - const rightNodes = []; - for (let i = 0; i < children.length; i++) { - const child = children[i]; + const centralNode: NodeModel = mindmap.getBranches()[0]; + const children: NodeModel[] = centralNode.getChildren(); + const leftNodes: NodeModel[] = []; + const rightNodes: NodeModel[] = []; + + children.forEach((child) => { const position = child.getPosition(); if (position.x < 0) { leftNodes.push(child); } else { rightNodes.push(child); } - } - rightNodes.sort((a, b) => a.getOrder() > b.getOrder()); - leftNodes.sort((a, b) => a.getOrder() > b.getOrder()); + rightNodes.sort((a, b) => a.getOrder() - b.getOrder()); + leftNodes.sort((a, b) => a.getOrder() - b.getOrder()); + }); for (let i = 0; i < rightNodes.length; i++) { rightNodes[i].setOrder(i * 2); @@ -65,7 +70,7 @@ class Pela2TangoMigrator { } } - _fixPosition(mindmap) { + private _fixPosition(mindmap: Mindmap) { // Position was not required in previous versions. Try to synthesize one . const centralNode = mindmap.getBranches()[0]; const children = centralNode.getChildren(); @@ -76,7 +81,7 @@ class Pela2TangoMigrator { } } - _fixNodePosition(node, parentPosition) { + _fixNodePosition(node: { getPosition: () => any; setPosition: (arg0: any, arg1: any) => void; getChildren: () => any; }, parentPosition: { x: number; y: any; }) { // Position was not required in previous versions. Try to synthesize one . let position = node.getPosition(); if (!position) { diff --git a/packages/mindplot/src/components/persistence/XMLMindmapSerializer.ts b/packages/mindplot/src/components/persistence/XMLMindmapSerializer.ts new file mode 100644 index 00000000..8ab32280 --- /dev/null +++ b/packages/mindplot/src/components/persistence/XMLMindmapSerializer.ts @@ -0,0 +1,26 @@ +/* + * Copyright [2021] [wisemapping] + * + * Licensed under WiseMapping Public License, Version 1.0 (the "License"). + * It is basically the Apache License, Version 2.0 (the "License") plus the + * "powered by wisemapping" text requirement on every single page; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the license at + * + * http://www.wisemapping.org/license + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Mindmap from "../model/Mindmap"; + + +interface XMLMindmapSerializer { + toXML(mindmap: Mindmap): Document; + loadFromDom(dom: Document, mapId: string): Mindmap; +} +export default XMLMindmapSerializer; \ No newline at end of file diff --git a/packages/mindplot/src/components/persistence/XMLSerializerBeta.ts b/packages/mindplot/src/components/persistence/XMLSerializerBeta.ts index 9afa8886..d9f349a2 100644 --- a/packages/mindplot/src/components/persistence/XMLSerializerBeta.ts +++ b/packages/mindplot/src/components/persistence/XMLSerializerBeta.ts @@ -21,9 +21,10 @@ import ModelCodeName from './ModelCodeName'; import Mindmap from '../model/Mindmap'; import FeatureModelFactory from '../model/FeatureModelFactory'; import NodeModel from '../model/NodeModel'; +import XMLMindmapSerializer from './XMLMindmapSerializer'; -class XMLSerializerBeta { - private static MAP_ROOT_NODE = 'map'; +class XMLSerializerBeta implements XMLMindmapSerializer { + private static MAP_ROOT_NODE = 'map'; toXML(mindmap: Mindmap) { $assert(mindmap, 'Can not save a null mindmap'); @@ -119,27 +120,27 @@ class XMLSerializerBeta { // ICONS const icons = topic.findFeatureByType('icons'); - icons.forEach((icon)=>{ + icons.forEach((icon) => { const iconDom = this._iconToXML(document, icon); parentTopic.append(iconDom); }); - + // LINKS const links = topic.findFeatureByType('links'); - icons.forEach((link)=>{ + icons.forEach((link) => { const linkDom = this._linkToXML(document, link); parentTopic.append(linkDom); }); const notes = topic.findFeatureByType('note'); - notes.forEach((note)=>{ + notes.forEach((note) => { const noteDom = this._noteToXML(document, note); parentTopic.append(noteDom); }); // CHILDREN TOPICS const childTopics = topic.getChildren(); - childTopics.forEach((childTopic)=>{ + childTopics.forEach((childTopic) => { const childDom = this._topicToXML(document, childTopic); parentTopic.append(childDom); }); diff --git a/packages/mindplot/src/components/persistence/XMLSerializerFactory.js b/packages/mindplot/src/components/persistence/XMLSerializerFactory.ts similarity index 90% rename from packages/mindplot/src/components/persistence/XMLSerializerFactory.js rename to packages/mindplot/src/components/persistence/XMLSerializerFactory.ts index 7d4a922f..16eaa1b0 100644 --- a/packages/mindplot/src/components/persistence/XMLSerializerFactory.js +++ b/packages/mindplot/src/components/persistence/XMLSerializerFactory.ts @@ -22,6 +22,8 @@ import Pela2TangoMigrator from './Pela2TangoMigrator'; import XMLSerializerBeta from './XMLSerializerBeta'; import XMLSerializerPela from './XMLSerializerPela'; import XMLSerializerTango from './XMLSerializerTango'; +import { Mindmap } from '../..'; +import XMLMindmapSerializer from './XMLMindmapSerializer'; const codeToSerializer = [ { @@ -48,7 +50,7 @@ class XMLSerializerFactory { * @return {mindplot.persistence.XMLSerializer_Beta|mindplot.persistence.XMLSerializer_Pela| * mindplot.persistence.XMLSerializer_Tango} serializer corresponding to the mindmap's version */ - static createInstanceFromMindmap(mindmap) { + static createInstanceFromMindmap(mindmap: Mindmap) { return XMLSerializerFactory .getSerializer(mindmap.getVersion()); } @@ -57,7 +59,7 @@ class XMLSerializerFactory { * @param domDocument * @return serializer corresponding to the mindmap's version */ - static createInstanceFromDocument(domDocument) { + static createInstanceFromDocument(domDocument: Document) { const rootElem = domDocument.documentElement; // Legacy version don't have version defined. @@ -74,7 +76,7 @@ class XMLSerializerFactory { * @param {String} version the version name * @return serializer */ - static getSerializer(version = ModelCodeName.TANGO) { + static getSerializer(version = ModelCodeName.TANGO): XMLMindmapSerializer { let found = false; let result = null; for (let i = 0; i < codeToSerializer.length; i++) { @@ -84,6 +86,7 @@ class XMLSerializerFactory { if (found) result = new (codeToSerializer[i].serializer)(); } else { const { migrator: Migrator } = codeToSerializer[i]; + // @ts-ignore result = new Migrator(result); } } diff --git a/packages/mindplot/src/components/persistence/XMLSerializerPela.ts b/packages/mindplot/src/components/persistence/XMLSerializerPela.ts index 838bb951..6c356bb8 100644 --- a/packages/mindplot/src/components/persistence/XMLSerializerPela.ts +++ b/packages/mindplot/src/components/persistence/XMLSerializerPela.ts @@ -23,12 +23,14 @@ import ConnectionLine from '../ConnectionLine'; import FeatureModelFactory from '../model/FeatureModelFactory'; import NodeModel from '../model/NodeModel'; import { FeatureType } from '../model/FeatureModel'; +import RelationshipModel from '../model/RelationshipModel'; +import XMLMindmapSerializer from './XMLMindmapSerializer'; -class XMLSerializerPela { +class XMLSerializerPela implements XMLMindmapSerializer { private static MAP_ROOT_NODE = 'map'; private _idsMap: {}; - toXML(mindmap: Mindmap) { + toXML(mindmap: Mindmap): Document { $assert(mindmap, 'Can not save a null mindmap'); const document = createDocument(); @@ -183,7 +185,7 @@ class XMLSerializerPela { return parentTopic; } - _noteTextToXML(document, elem, text) { + _noteTextToXML(document: Document, elem: Element, text: string) { if (text.indexOf('\n') === -1) { elem.setAttribute('text', this.rmXmlInv(text)); } else { @@ -194,13 +196,13 @@ class XMLSerializerPela { } } - static _relationshipToXML(document, relationship) { + static _relationshipToXML(document: Document, relationship: RelationshipModel) { const result = document.createElement('relationship'); - result.setAttribute('srcTopicId', relationship.getFromNode()); - result.setAttribute('destTopicId', relationship.getToNode()); + result.setAttribute('srcTopicId', relationship.getFromNode().toString()); + result.setAttribute('destTopicId', relationship.getToNode().toString()); const lineType = relationship.getLineType(); - result.setAttribute('lineType', lineType); + result.setAttribute('lineType', lineType.toString()); if (lineType === ConnectionLine.CURVED || lineType === ConnectionLine.SIMPLE_CURVED) { if ($defined(relationship.getSrcCtrlPoint())) { const srcPoint = relationship.getSrcCtrlPoint(); @@ -217,8 +219,8 @@ class XMLSerializerPela { ); } } - result.setAttribute('endArrow', relationship.getEndArrow()); - result.setAttribute('startArrow', relationship.getStartArrow()); + result.setAttribute('endArrow', String(relationship.getEndArrow())); + result.setAttribute('startArrow', String(relationship.getStartArrow())); return result; } @@ -230,7 +232,7 @@ class XMLSerializerPela { * @throws will throw an error if the document element is not consistent with a wisemap's root * element */ - loadFromDom(dom, mapId) { + loadFromDom(dom: Document, mapId: string) { $assert(dom, 'dom can not be null'); $assert(mapId, 'mapId can not be null'); diff --git a/packages/mindplot/src/components/persistence/index.js b/packages/mindplot/src/components/persistence/index.js deleted file mode 100644 index 29683e29..00000000 --- a/packages/mindplot/src/components/persistence/index.js +++ /dev/null @@ -1,17 +0,0 @@ -import beta2PelaMigrator from './Beta2PelaMigrator'; -import modelCodeName from './ModelCodeName'; -import pela2TangoMigrator from './Pela2TangoMigrator'; -import xmlSerializerBeta from './XMLSerializerBeta'; -import xmlSerializerPela from './XMLSerializerPela'; -import xmlSerializerTango from './XMLSerializerTango'; -import xmlSerializerFactory from './XMLSerializerFactory'; - -export default { - Beta2PelaMigrator: beta2PelaMigrator, - ModelCodeName: modelCodeName, - Pela2TangoMigrator: pela2TangoMigrator, - XMLSerializer_Beta: xmlSerializerBeta, - XMLSerializer_Pela: xmlSerializerPela, - XMLSerializer_Tango: xmlSerializerTango, - XMLSerializerFactory: xmlSerializerFactory, -}; diff --git a/packages/mindplot/test/unit/export/expected/complex.wxml b/packages/mindplot/test/unit/export/expected/complex.wxml index 5f78e133..91268870 100644 --- a/packages/mindplot/test/unit/export/expected/complex.wxml +++ b/packages/mindplot/test/unit/export/expected/complex.wxml @@ -1,5 +1,5 @@ -