From 5dffa99835201bb28d58145036a3755be35a4a41 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Thu, 24 Nov 2022 10:40:18 -0800 Subject: [PATCH] Clean up code. --- packages/mindplot/.eslintrc.json | 4 +-- packages/mindplot/src/@types/custom.d.ts | 1 + .../mindplot/src/components/ControlPoint.ts | 24 +++++++++----- .../mindplot/src/components/EmojiCharIcon.ts | 2 +- packages/mindplot/src/components/IconGroup.ts | 2 +- packages/mindplot/src/components/Workspace.ts | 4 +-- .../commands/AddFeatureToTopicCommand.ts | 4 +-- .../components/commands/AddTopicCommand.ts | 2 +- .../commands/MoveControlPointCommand.ts | 4 +-- .../src/components/export/SVGExporter.ts | 33 ++++++++++--------- .../src/components/import/FreemindImporter.ts | 6 ++-- .../layout/ChildrenSorterStrategy.ts | 7 +++- .../mindplot/src/components/layout/Node.ts | 2 +- .../src/components/layout/RootedTreeSet.ts | 16 ++++----- .../mindplot/src/components/model/IMindmap.ts | 2 +- .../src/components/model/INodeModel.ts | 2 +- .../persistence/XMLSerializerFactory.ts | 5 +-- packages/mindplot/src/index.ts | 1 + 18 files changed, 69 insertions(+), 52 deletions(-) diff --git a/packages/mindplot/.eslintrc.json b/packages/mindplot/.eslintrc.json index 3fae3a03..b9606180 100644 --- a/packages/mindplot/.eslintrc.json +++ b/packages/mindplot/.eslintrc.json @@ -39,9 +39,11 @@ } ], "object-curly-newline": "off", + "@typescript-eslint/no-unused-vars": "error", "no-underscore-dangle": "off", "no-plusplus": "off", "no-param-reassign": "off", + "@typescript-eslint/no-non-null-assertion": "off", "max-len": [ 1, 300 @@ -50,8 +52,6 @@ "no-console": "off", // codebase contains many this aliases, fix in the future? "@typescript-eslint/no-this-alias": "off", - // Remove once migration is completed ... - "@typescript-eslint/no-explicit-any": "warn", "import/extensions": [ "error", "ignorePackages", diff --git a/packages/mindplot/src/@types/custom.d.ts b/packages/mindplot/src/@types/custom.d.ts index 2ff57beb..957d9d84 100644 --- a/packages/mindplot/src/@types/custom.d.ts +++ b/packages/mindplot/src/@types/custom.d.ts @@ -1,4 +1,5 @@ declare module '*.svg' { + // eslint-disable-next-line @typescript-eslint/no-explicit-any const content: any; export default content; } diff --git a/packages/mindplot/src/components/ControlPoint.ts b/packages/mindplot/src/components/ControlPoint.ts index b9aab58b..1b2fada2 100644 --- a/packages/mindplot/src/components/ControlPoint.ts +++ b/packages/mindplot/src/components/ControlPoint.ts @@ -21,6 +21,7 @@ import { $defined } from '@wisemapping/core-js'; import Shape from './util/Shape'; import ActionDispatcher from './ActionDispatcher'; import Workspace from './Workspace'; +import PositionType from './PositionType'; class ControlPoint { private control1: Elipse; @@ -37,11 +38,13 @@ class ControlPoint { private _workspace: Workspace; - private _endPoint: any[]; + // eslint-disable-next-line no-use-before-define + private _endPoint: PositionType[]; - private _orignalCtrlPoint: any; + // eslint-disable-next-line no-use-before-define + private _orignalCtrlPoint: PositionType[]; - private _controls: any; + private _controls: number; private _mouseMoveFunction: (e: Event) => void; @@ -125,17 +128,20 @@ class ControlPoint { this._controls[ControlPoint.FROM].x + pos.x, this._controls[ControlPoint.FROM].y + pos.y - 3, ); + this._controlLines[0].setFrom(pos.x, pos.y); this._controlLines[0].setTo( this._controls[ControlPoint.FROM].x + pos.x + 3, this._controls[ControlPoint.FROM].y + pos.y, ); + pos = this._line.getLine().getTo(); this._controlLines[1].setFrom(pos.x, pos.y); this._controlLines[1].setTo( this._controls[ControlPoint.TO].x + pos.x + 3, this._controls[ControlPoint.TO].y + pos.y, ); + this._controlPointsController[1].setPosition( this._controls[ControlPoint.TO].x + pos.x, this._controls[ControlPoint.TO].y + pos.y - 3, @@ -223,8 +229,8 @@ class ControlPoint { workspace.append(this._controlLines[1]); } - removeFromWorkspace(workspace: Workspace) { - this._workspace = null; + removeFromWorkspace(workspace: Workspace): void { + this._workspace!; workspace.removeChild(this._controlPointsController[0]); workspace.removeChild(this._controlPointsController[1]); workspace.removeChild(this._controlLines[0]); @@ -235,17 +241,17 @@ class ControlPoint { return this._controls[index]; } - getOriginalEndPoint(index: number) { + getOriginalEndPoint(index: number): PositionType { return this._endPoint[index]; } - getOriginalCtrlPoint(index: number): ControlPoint { + getOriginalCtrlPoint(index: number): PositionType { return this._orignalCtrlPoint[index]; } - static FROM = 0; + private static FROM = 0; - static TO = 1; + private static TO = 1; } export default ControlPoint; diff --git a/packages/mindplot/src/components/EmojiCharIcon.ts b/packages/mindplot/src/components/EmojiCharIcon.ts index 990c4a16..801a2f7d 100644 --- a/packages/mindplot/src/components/EmojiCharIcon.ts +++ b/packages/mindplot/src/components/EmojiCharIcon.ts @@ -81,7 +81,7 @@ class EmojiCharIcon implements Icon { return this.group.getPosition(); } - addEvent(type: string, fnc: any): void { + addEvent(type: string, fnc: () => void): void { this.element.addEvent(type, fnc); } diff --git a/packages/mindplot/src/components/IconGroup.ts b/packages/mindplot/src/components/IconGroup.ts index 6a2efc6d..7a247c89 100644 --- a/packages/mindplot/src/components/IconGroup.ts +++ b/packages/mindplot/src/components/IconGroup.ts @@ -31,7 +31,7 @@ ORDER_BY_TYPE.set('link', 2); class IconGroup { private _icons: ImageIcon[]; - private _group: any; + private _group: Group; private _removeTip: IconGroupRemoveTip; diff --git a/packages/mindplot/src/components/Workspace.ts b/packages/mindplot/src/components/Workspace.ts index b4b7c29c..2bb48914 100644 --- a/packages/mindplot/src/components/Workspace.ts +++ b/packages/mindplot/src/components/Workspace.ts @@ -182,8 +182,8 @@ class Workspace { const workspace = this._workspace; const divContainer = this._screenManager.getContainer(); - const containerWidth = divContainer.width(); - const containerHeight = divContainer.height(); + const containerWidth = divContainer.width()!; + const containerHeight = divContainer.height()!; const newVisibleAreaSize = { width: containerWidth, height: containerHeight }; // - svg must fit container size diff --git a/packages/mindplot/src/components/commands/AddFeatureToTopicCommand.ts b/packages/mindplot/src/components/commands/AddFeatureToTopicCommand.ts index 8aea8d7b..245351d1 100644 --- a/packages/mindplot/src/components/commands/AddFeatureToTopicCommand.ts +++ b/packages/mindplot/src/components/commands/AddFeatureToTopicCommand.ts @@ -28,7 +28,7 @@ class AddFeatureToTopicCommand extends Command { private _attributes: object; - private _featureModel: FeatureModel; + private _featureModel: FeatureModel | null; /* * @classdesc This command class handles do/undo of adding features to topics, e.g. an @@ -65,7 +65,7 @@ class AddFeatureToTopicCommand extends Command { undoExecute(commandContext: CommandContext) { const topic = commandContext.findTopics([this._topicId])[0]; - topic.removeFeature(this._featureModel); + topic.removeFeature(this._featureModel!); } } diff --git a/packages/mindplot/src/components/commands/AddTopicCommand.ts b/packages/mindplot/src/components/commands/AddTopicCommand.ts index 0e405a09..faf39c44 100644 --- a/packages/mindplot/src/components/commands/AddTopicCommand.ts +++ b/packages/mindplot/src/components/commands/AddTopicCommand.ts @@ -70,7 +70,7 @@ class AddTopicCommand extends Command { undoExecute(commandContext: CommandContext) { // Delete disconnected the nodes. Create a copy of the topics ... - const clonedModel = []; + const clonedModel: NodeModel[] = []; this._models.forEach((model) => { clonedModel.push(model.clone()); }); diff --git a/packages/mindplot/src/components/commands/MoveControlPointCommand.ts b/packages/mindplot/src/components/commands/MoveControlPointCommand.ts index 33523cf9..7529afa6 100644 --- a/packages/mindplot/src/components/commands/MoveControlPointCommand.ts +++ b/packages/mindplot/src/components/commands/MoveControlPointCommand.ts @@ -34,7 +34,7 @@ class MoveControlPointCommand extends Command { private _wasCustom: boolean; - private _endPoint: any; + private _endPoint: PositionType; private _point: number; @@ -52,7 +52,7 @@ class MoveControlPointCommand extends Command { this._ctrlPointControler = ctrlPointController; this._line = ctrlPointController._line; this._controlPoint = { ...this._ctrlPointControler.getControlPoint(point) }; - this._oldControlPoint = { ...this._ctrlPointControler.getOriginalCtrlPoint(point) }; + this._oldControlPoint = this._ctrlPointControler.getOriginalCtrlPoint(point); this._originalEndPoint = this._ctrlPointControler.getOriginalEndPoint(point); switch (point) { case 0: diff --git a/packages/mindplot/src/components/export/SVGExporter.ts b/packages/mindplot/src/components/export/SVGExporter.ts index f72de06c..4f12a756 100644 --- a/packages/mindplot/src/components/export/SVGExporter.ts +++ b/packages/mindplot/src/components/export/SVGExporter.ts @@ -66,25 +66,28 @@ class SVGExporter extends Exporter { private _calcualteDimensions(): { minX: number; maxX: number; minY: number; maxY: number } { // Collect all group elements ... const rectElems = Array.from(this.svgElement.querySelectorAll('g>rect')); + let result: SizeType = { width: 0, height: 0 }; const translates: SizeType[] = rectElems.map((rect: Element) => { const g = rect.parentElement; - const transformStr = g.getAttribute('transform'); + const transformStr = g?.getAttribute('transform'); + if (transformStr) { + // Looking to parse translate(220.00000,279.00000) scale(1.00000,1.00000) + const match = transformStr.match(SVGExporter.regexpTranslate); + if (match !== null) { + result = { width: Number.parseFloat(match[1]), height: Number.parseFloat(match[2]) }; - // Looking to parse translate(220.00000,279.00000) scale(1.00000,1.00000) - const match = transformStr.match(SVGExporter.regexpTranslate); - let result: SizeType = { width: 0, height: 0 }; - if (match !== null) { - result = { width: Number.parseFloat(match[1]), height: Number.parseFloat(match[2]) }; + // Add rect size ... + const width = rect.getAttribute('width'); + if (result.width > 0 && width) { + const rectWidth = Number.parseFloat(width); + result.width += rectWidth; + } - // Add rect size ... - if (result.width > 0) { - const rectWidth = Number.parseFloat(rect.getAttribute('width')); - result.width += rectWidth; - } - - if (result.height > 0) { - const rectHeight = Number.parseFloat(rect.getAttribute('height')); - result.height += rectHeight; + const height = rect.getAttribute('height'); + if (result.height > 0 && height) { + const rectHeight = Number.parseFloat(height); + result.height += rectHeight; + } } } return result; diff --git a/packages/mindplot/src/components/import/FreemindImporter.ts b/packages/mindplot/src/components/import/FreemindImporter.ts index 50aa621a..999920fc 100644 --- a/packages/mindplot/src/components/import/FreemindImporter.ts +++ b/packages/mindplot/src/components/import/FreemindImporter.ts @@ -169,7 +169,7 @@ export default class FreemindImporter extends Importer { } // Check for style... - const fontStyle = this.generateFontStyle(freeNode, null); + const fontStyle = this.generateFontStyle(freeNode); if (fontStyle && fontStyle !== ';;;;') wiseTopic.setFontStyle(fontStyle); // Is there any link... @@ -393,7 +393,7 @@ export default class FreemindImporter extends Importer { return result; } - private generateFontStyle(node: FreemindNode, font: FreemindFont | undefined): string { + private generateFontStyle(node: FreemindNode, font?: FreemindFont): string { const fontStyle: Array = []; // Font family @@ -485,6 +485,6 @@ export default class FreemindImporter extends Importer { private html2Text(content: string): string { const temporalDivElement = document.createElement('div'); temporalDivElement.innerHTML = content; - return temporalDivElement.textContent.trim() || temporalDivElement.innerText.trim() || ''; + return temporalDivElement.textContent?.trim() || temporalDivElement.innerText.trim() || ''; } } diff --git a/packages/mindplot/src/components/layout/ChildrenSorterStrategy.ts b/packages/mindplot/src/components/layout/ChildrenSorterStrategy.ts index e07fd91c..ed701823 100644 --- a/packages/mindplot/src/components/layout/ChildrenSorterStrategy.ts +++ b/packages/mindplot/src/components/layout/ChildrenSorterStrategy.ts @@ -28,7 +28,12 @@ abstract class ChildrenSorterStrategy { abstract detach(treeSet: RootedTreeSet, node: Node): void; - abstract predict(treeSet: RootedTreeSet, parent, node: Node | null, position: PositionType); + abstract predict( + treeSet: RootedTreeSet, + parent: Node, + node: Node | null, + position: PositionType | null, + ); abstract verify(treeSet: RootedTreeSet, node: Node); diff --git a/packages/mindplot/src/components/layout/Node.ts b/packages/mindplot/src/components/layout/Node.ts index 7eb364cd..da366b51 100644 --- a/packages/mindplot/src/components/layout/Node.ts +++ b/packages/mindplot/src/components/layout/Node.ts @@ -24,7 +24,7 @@ class Node { private _id: number; // eslint-disable-next-line no-use-before-define - _parent: Node; + _parent: Node | null; private _sorter: ChildrenSorterStrategy; diff --git a/packages/mindplot/src/components/layout/RootedTreeSet.ts b/packages/mindplot/src/components/layout/RootedTreeSet.ts index efc7c3fb..c8f061c5 100644 --- a/packages/mindplot/src/components/layout/RootedTreeSet.ts +++ b/packages/mindplot/src/components/layout/RootedTreeSet.ts @@ -89,7 +89,7 @@ class RootedTreeSet { * @throws will throw an error if nodeId is null or undefined * @throws will throw an error if node is not connected */ - disconnect(nodeId: number) { + disconnect(nodeId: number): void { $assert($defined(nodeId), 'nodeId can not be null'); const node = this.find(nodeId); $assert(node._parent, 'Node is not connected'); @@ -106,11 +106,11 @@ class RootedTreeSet { * @throws will throw an error if node cannot be found * @return node */ - find(id: number, validate = true): Node { + find(id: number, validate = true): Node | null { $assert($defined(id), 'id can not be null'); const graphs = this._rootNodes; - let result = null; + let result: Node | null = null; for (let i = 0; i < graphs.length; i++) { const node = graphs[i]; result = this._find(id, node); @@ -126,12 +126,12 @@ class RootedTreeSet { return result; } - private _find(id: number, parent: Node): Node { + private _find(id: number, parent: Node): Node | null { if (parent.getId() === id) { return parent; } - let result = null; + let result: Node | null = null; const children = parent._children; for (let i = 0; i < children.length; i++) { const child = children[i]; @@ -192,10 +192,10 @@ class RootedTreeSet { */ getSiblings(node: Node): Node[] { $assert(node, 'node cannot be null'); - if (!$defined(node._parent)) { + if (!node._parent) { return []; } - const siblings = node._parent._children.filter((child) => child !== node); + const siblings = node._parent?._children.filter((child) => child !== node); return siblings; } @@ -241,7 +241,7 @@ class RootedTreeSet { * @throws will throw an error if node is null or undefined * @return parent */ - getParent(node: Node): Node { + getParent(node: Node): Node | null { $assert(node, 'node cannot be null'); return node._parent; } diff --git a/packages/mindplot/src/components/model/IMindmap.ts b/packages/mindplot/src/components/model/IMindmap.ts index 95a15e59..c36569ea 100644 --- a/packages/mindplot/src/components/model/IMindmap.ts +++ b/packages/mindplot/src/components/model/IMindmap.ts @@ -67,7 +67,7 @@ abstract class IMindmap { $assert(child, 'Child can not be null.'); $assert(parent, 'Child model seems to be already connected'); - parent.removeChild(child); + parent?.removeChild(child); this.addBranch(child); } diff --git a/packages/mindplot/src/components/model/INodeModel.ts b/packages/mindplot/src/components/model/INodeModel.ts index 44c7b405..1f8f9249 100644 --- a/packages/mindplot/src/components/model/INodeModel.ts +++ b/packages/mindplot/src/components/model/INodeModel.ts @@ -46,7 +46,7 @@ abstract class INodeModel { abstract getFeatures(): FeatureModel[]; setId(id?: number): void { - if (!$defined(id)) { + if (id === null || id === undefined) { const newId = INodeModel._nextUUID(); this.putProperty('id', newId); } else { diff --git a/packages/mindplot/src/components/persistence/XMLSerializerFactory.ts b/packages/mindplot/src/components/persistence/XMLSerializerFactory.ts index a6c43b55..5e6b8b84 100644 --- a/packages/mindplot/src/components/persistence/XMLSerializerFactory.ts +++ b/packages/mindplot/src/components/persistence/XMLSerializerFactory.ts @@ -15,7 +15,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { $assert } from '@wisemapping/core-js'; import ModelCodeName from './ModelCodeName'; import Beta2PelaMigrator from './Beta2PelaMigrator'; import Pela2TangoMigrator from './Pela2TangoMigrator'; @@ -89,7 +88,9 @@ class XMLSerializerFactory { result = new migrator(result); } } - $assert(result, `Cound not find serialized for ${version}`); + if (!result) { + throw new Error(`Cound not find serialized for ${version}`); + } return result; } } diff --git a/packages/mindplot/src/index.ts b/packages/mindplot/src/index.ts index a7454486..3ccf87af 100644 --- a/packages/mindplot/src/index.ts +++ b/packages/mindplot/src/index.ts @@ -58,6 +58,7 @@ declare global { var designer: Designer; } +// eslint-disable-next-line @typescript-eslint/no-explicit-any const globalAny: any = global; globalAny.jQuery = jquery; // WebComponent registration