From 4ae3e07537c88e559d4ac04350171b4c5d4be40b Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sat, 10 Dec 2022 11:15:36 -0800 Subject: [PATCH] Enable nullable check. --- .../src/components/MindplotWebComponent.ts | 24 +++- .../import/FreemindIconConverter.ts | 2 +- .../src/components/import/FreemindImporter.ts | 2 +- .../src/components/layout/BalancedSorter.ts | 18 +-- .../mindplot/src/components/layout/Node.ts | 2 +- .../src/components/layout/RootedTreeSet.ts | 12 +- .../playground/layout/BalancedTestSuite.js | 129 +++++++++--------- packages/mindplot/tsconfig.json | 2 +- 8 files changed, 107 insertions(+), 84 deletions(-) diff --git a/packages/mindplot/src/components/MindplotWebComponent.ts b/packages/mindplot/src/components/MindplotWebComponent.ts index 08360481..22dbd926 100644 --- a/packages/mindplot/src/components/MindplotWebComponent.ts +++ b/packages/mindplot/src/components/MindplotWebComponent.ts @@ -1,3 +1,21 @@ +/* + * 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 { $assert } from '@wisemapping/core-js'; import Designer from './Designer'; import buildDesigner from './DesignerBuilder'; import DesignerOptionsBuilder from './DesignerOptionsBuilder'; @@ -65,11 +83,15 @@ class MindplotWebComponent extends HTMLElement { const persistenceManager = persistence || new LocalStorageManager('map.xml', false, false); const mode = editorRenderMode || 'viewonly'; + + const mindplodElem = this._shadowRoot.getElementById('mindplot'); + $assert(mindplodElem, 'Root mindplot element could not be loaded'); + const options = DesignerOptionsBuilder.buildOptions({ persistenceManager, mode, widgetManager, - divContainer: this._shadowRoot.getElementById('mindplot'), + divContainer: mindplodElem!, container: 'mindplot', zoom: zoom ? Number.parseFloat(zoom) : 1, locale: locale || 'en', diff --git a/packages/mindplot/src/components/import/FreemindIconConverter.ts b/packages/mindplot/src/components/import/FreemindIconConverter.ts index e8761395..974035f7 100644 --- a/packages/mindplot/src/components/import/FreemindIconConverter.ts +++ b/packages/mindplot/src/components/import/FreemindIconConverter.ts @@ -4,7 +4,7 @@ export default class FreemindIconConverter { private static freeIdToIcon: Map = new Map(); public static toWiseId(iconId: string): number | null { - const result: SvgIconModel = this.freeIdToIcon.get(iconId); + const result: SvgIconModel = this.freeIdToIcon.get(iconId)!; return result ? result.getId() : null; } } diff --git a/packages/mindplot/src/components/import/FreemindImporter.ts b/packages/mindplot/src/components/import/FreemindImporter.ts index 6967dc45..cecd74d3 100644 --- a/packages/mindplot/src/components/import/FreemindImporter.ts +++ b/packages/mindplot/src/components/import/FreemindImporter.ts @@ -70,7 +70,7 @@ export default class FreemindImporter extends Importer { this.convertNodeProperties(freeNode, wiseTopic, true); - this.nodesmap.set(freeNode.getId(), wiseTopic); + this.nodesmap.set(freeNode.getId()!, wiseTopic); this.convertChildNodes(freeNode, wiseTopic, this.mindmap, 1); this.addRelationship(this.mindmap); diff --git a/packages/mindplot/src/components/layout/BalancedSorter.ts b/packages/mindplot/src/components/layout/BalancedSorter.ts index a3af023d..f2ec375d 100644 --- a/packages/mindplot/src/components/layout/BalancedSorter.ts +++ b/packages/mindplot/src/components/layout/BalancedSorter.ts @@ -135,15 +135,17 @@ class BalancedSorter extends AbstractBasicSorter { detach(treeSet: RootedTreeSet, node: Node): void { const parent = treeSet.getParent(node); - // Filter nodes on one side.. - const children = this._getChildrenForOrder(parent, treeSet, node.getOrder()); + if (parent) { + // Filter nodes on one side.. + const children = this._getChildrenForOrder(parent, treeSet, node.getOrder()); - children.forEach((child) => { - if (child.getOrder() > node.getOrder()) { - child.setOrder(child.getOrder() - 2); - } - }); - node.setOrder(node.getOrder() % 2 === 0 ? 0 : 1); + children.forEach((child) => { + if (child.getOrder() > node.getOrder()) { + child.setOrder(child.getOrder() - 2); + } + }); + node.setOrder(node.getOrder() % 2 === 0 ? 0 : 1); + } } computeOffsets(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 1919e203..49a32437 100644 --- a/packages/mindplot/src/components/layout/RootedTreeSet.ts +++ b/packages/mindplot/src/components/layout/RootedTreeSet.ts @@ -94,7 +94,7 @@ class RootedTreeSet { const node = this.find(nodeId); $assert(node._parent, 'Node is not connected'); - node._parent._children = node._parent._children.filter((n) => node !== n); + node._parent!._children = node._parent!._children.filter((n) => node !== n); this._rootNodes.push(node); node._parent = null; } @@ -160,7 +160,7 @@ class RootedTreeSet { getRootNode(node: Node) { $assert(node, 'node cannot be null'); const parent = this.getParent(node); - if ($defined(parent)) { + if (parent) { return this.getRootNode(parent); } @@ -176,7 +176,7 @@ class RootedTreeSet { return this._getAncestors(this.getParent(node), []); } - _getAncestors(node: Node, ancestors: Node[]) { + private _getAncestors(node: Node | null, ancestors: Node[]) { const result = ancestors; if (node) { result.push(node); @@ -192,7 +192,7 @@ 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); @@ -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; } @@ -373,7 +373,7 @@ class RootedTreeSet { getSiblingsInVerticalDirection(node: Node, yOffset: number): Node[] { // siblings with lower or higher order // (depending on the direction of the offset and on the same side as their parent) - const parent = this.getParent(node); + const parent = this.getParent(node)!; const siblings = this.getSiblings(node).filter((sibling) => { const sameSide = node.getPosition().x > parent.getPosition().x diff --git a/packages/mindplot/test/playground/layout/BalancedTestSuite.js b/packages/mindplot/test/playground/layout/BalancedTestSuite.js index cc75c362..8acde71b 100644 --- a/packages/mindplot/test/playground/layout/BalancedTestSuite.js +++ b/packages/mindplot/test/playground/layout/BalancedTestSuite.js @@ -1,4 +1,3 @@ -/* eslint-disable no-console */ /* * Copyright [2021] [wisemapping] * @@ -90,7 +89,7 @@ class BalancedTestSuite extends TestSuite { manager.plot('testBalanced9', { width: 1000, height: 400 }); // Check orders have shifted accordingly - $assert(manager.find(5).getOrder() == 6, 'Node 5 should have order 6'); + $assert(manager.find(5).getOrder() === 6, 'Node 5 should have order 6'); manager.addNode(14, TestSuite.NODE_SIZE, position); manager.connectNode(0, 14, 5); @@ -98,7 +97,7 @@ class BalancedTestSuite extends TestSuite { manager.plot('testBalanced10', { width: 1000, height: 400 }); // Check orders have shifted accordingly - $assert(manager.find(6).getOrder() == 7, 'Node 6 should have order 7'); + $assert(manager.find(6).getOrder() === 7, 'Node 6 should have order 7'); manager.addNode(15, TestSuite.NODE_SIZE, position); manager.connectNode(0, 15, 4); @@ -106,8 +105,8 @@ class BalancedTestSuite extends TestSuite { manager.plot('testBalanced11', { width: 1000, height: 400 }); // Check orders have shifted accordingly - $assert(manager.find(13).getOrder() == 6, 'Node 13 should have order 6'); - $assert(manager.find(5).getOrder() == 8, 'Node 5 should have order 8'); + $assert(manager.find(13).getOrder() === 6, 'Node 13 should have order 6'); + $assert(manager.find(5).getOrder() === 8, 'Node 5 should have order 8'); manager.addNode(16, TestSuite.NODE_SIZE, position); manager.connectNode(0, 16, 25); @@ -115,7 +114,7 @@ class BalancedTestSuite extends TestSuite { manager.plot('testBalanced12', { width: 1000, height: 400 }); // Check orders have shifted accordingly - $assert(manager.find(16).getOrder() == 9, 'Node 16 should have order 9'); + $assert(manager.find(16).getOrder() === 9, 'Node 16 should have order 9'); manager.addNode(17, TestSuite.NODE_SIZE, position); manager.addNode(18, TestSuite.NODE_SIZE, position); @@ -216,42 +215,42 @@ class BalancedTestSuite extends TestSuite { this._plotPrediction(graph1, prediction1a); $assert( prediction1a.position.y < manager.find(1).getPosition().y && - prediction1a.position.x == manager.find(1).getPosition().x, + prediction1a.position.x === manager.find(1).getPosition().x, 'Prediction is incorrectly positioned', ); - $assert(prediction1a.order == 0, 'Prediction order should be 0'); + $assert(prediction1a.order === 0, 'Prediction order should be 0'); console.log('\tAdded as child of node 0 and dropped at (165, -10):'); const prediction1b = manager.predict(0, null, { x: 165, y: -10 }); this._plotPrediction(graph1, prediction1b); $assert( prediction1b.position.y > manager.find(1).getPosition().y && - prediction1b.position.y < manager.find(3).getPosition().y && - prediction1b.position.x == manager.find(1).getPosition().x, + prediction1b.position.y < manager.find(3).getPosition().y && + prediction1b.position.x === manager.find(1).getPosition().x, 'Prediction is incorrectly positioned', ); - $assert(prediction1b.order == 2, 'Prediction order should be 2'); + $assert(prediction1b.order === 2, 'Prediction order should be 2'); console.log('\tAdded as child of node 0 and dropped at (145, 15):'); const prediction1c = manager.predict(0, null, { x: 145, y: 15 }); this._plotPrediction(graph1, prediction1c); $assert( prediction1c.position.y > manager.find(3).getPosition().y && - prediction1c.position.y < manager.find(5).getPosition().y && - prediction1c.position.x == manager.find(3).getPosition().x, + prediction1c.position.y < manager.find(5).getPosition().y && + prediction1c.position.x === manager.find(3).getPosition().x, 'Prediction is incorrectly positioned', ); - $assert(prediction1c.order == 4, 'Prediction order should be 4'); + $assert(prediction1c.order === 4, 'Prediction order should be 4'); console.log('\tAdded as child of node 0 and dropped at (145, 70):'); const prediction1d = manager.predict(0, null, { x: 145, y: 70 }); this._plotPrediction(graph1, prediction1d); $assert( prediction1d.position.y > manager.find(5).getPosition().y && - prediction1d.position.x == manager.find(5).getPosition().x, + prediction1d.position.x === manager.find(5).getPosition().x, 'Prediction is incorrectly positioned', ); - $assert(prediction1d.order == 6, 'Prediction order should be 6'); + $assert(prediction1d.order === 6, 'Prediction order should be 6'); // Graph 2 const graph2 = manager.plot('testBalancedPredict2', { width: 1000, height: 400 }); @@ -261,31 +260,31 @@ class BalancedTestSuite extends TestSuite { this._plotPrediction(graph2, prediction2a); $assert( prediction2a.position.y < manager.find(2).getPosition().y && - prediction2a.position.x == manager.find(2).getPosition().x, + prediction2a.position.x === manager.find(2).getPosition().x, 'Prediction is incorrectly positioned', ); - $assert(prediction2a.order == 1, 'Prediction order should be 1'); + $assert(prediction2a.order === 1, 'Prediction order should be 1'); console.log('\tAdded as child of node 0 and dropped at (-145, -10):'); const prediction2b = manager.predict(0, null, { x: -145, y: -10 }); this._plotPrediction(graph2, prediction2b); $assert( prediction2b.position.y > manager.find(2).getPosition().y && - prediction2b.position.y < manager.find(4).getPosition().y && - prediction2b.position.x == manager.find(2).getPosition().x, + prediction2b.position.y < manager.find(4).getPosition().y && + prediction2b.position.x === manager.find(2).getPosition().x, 'Prediction is incorrectly positioned', ); - $assert(prediction2b.order == 3, 'Prediction order should be 3'); + $assert(prediction2b.order === 3, 'Prediction order should be 3'); console.log('\tAdded as child of node 0 and dropped at (-145, 40):'); const prediction2c = manager.predict(0, null, { x: -145, y: 400 }); this._plotPrediction(graph2, prediction2c); $assert( prediction2c.position.y > manager.find(4).getPosition().y && - prediction2c.position.x == manager.find(4).getPosition().x, + prediction2c.position.x === manager.find(4).getPosition().x, 'Prediction is incorrectly positioned', ); - $assert(prediction2c.order == 5, 'Prediction order should be 5'); + $assert(prediction2c.order === 5, 'Prediction order should be 5'); // Graph 3 console.log('\tPredict nodes added with no position:'); @@ -294,10 +293,10 @@ class BalancedTestSuite extends TestSuite { this._plotPrediction(graph3, prediction3); $assert( prediction3.position.y > manager.find(4).getPosition().y && - prediction3.position.x == manager.find(4).getPosition().x, + prediction3.position.x === manager.find(4).getPosition().x, 'Prediction is incorrectly positioned', ); - $assert(prediction3.order == 5, 'Prediction order should be 5'); + $assert(prediction3.order === 5, 'Prediction order should be 5'); console.log('\tPredict nodes added with no position:'); manager.addNode(6, TestSuite.NODE_SIZE, prediction3.position); @@ -308,10 +307,10 @@ class BalancedTestSuite extends TestSuite { this._plotPrediction(graph4, prediction4); $assert( prediction4.position.y > manager.find(5).getPosition().y && - prediction4.position.x == manager.find(5).getPosition().x, + prediction4.position.x === manager.find(5).getPosition().x, 'Prediction is incorrectly positioned', ); - $assert(prediction4.order == 6, 'Prediction order should be 6'); + $assert(prediction4.order === 6, 'Prediction order should be 6'); console.log('\tPredict nodes added only a root node:'); manager.removeNode(1).removeNode(2).removeNode(3).removeNode(4).removeNode(5); @@ -323,16 +322,16 @@ class BalancedTestSuite extends TestSuite { this._plotPrediction(graph5, prediction5b); $assert( prediction5a.position.x > manager.find(0).getPosition().x && - prediction5a.position.y == manager.find(0).getPosition().y, + prediction5a.position.y === manager.find(0).getPosition().y, 'Prediction is incorrectly positioned', ); - $assert(prediction5a.order == 0, 'Prediction order should be 0'); + $assert(prediction5a.order === 0, 'Prediction order should be 0'); $assert( - prediction5a.position.x == prediction5b.position.x && - prediction5a.position.y == prediction5b.position.y, + prediction5a.position.x === prediction5b.position.x && + prediction5a.position.y === prediction5b.position.y, 'Both predictions should be the same', ); - $assert(prediction5a.order == prediction5b.order, 'Both predictions should be the same'); + $assert(prediction5a.order === prediction5b.order, 'Both predictions should be the same'); console.log('OK!\n\n'); } @@ -350,24 +349,24 @@ class BalancedTestSuite extends TestSuite { const prediction1a = manager.predict(0, 1, { x: 50, y: 50 }); this._plotPrediction(graph1, prediction1a); $assert( - prediction1a.position.x == manager.find(1).getPosition().x && - prediction1a.position.y == manager.find(1).getPosition().y, + prediction1a.position.x === manager.find(1).getPosition().x && + prediction1a.position.y === manager.find(1).getPosition().y, 'Prediction position should be the same as node 1', ); $assert( - prediction1a.order == manager.find(1).getOrder(), + prediction1a.order === manager.find(1).getOrder(), 'Prediction order should be the same as node 1', ); const prediction1b = manager.predict(0, 1, { x: 50, y: -50 }); this._plotPrediction(graph1, prediction1b); $assert( - prediction1b.position.x == manager.find(1).getPosition().x && - prediction1b.position.y == manager.find(1).getPosition().y, + prediction1b.position.x === manager.find(1).getPosition().x && + prediction1b.position.y === manager.find(1).getPosition().y, 'Prediction position should be the same as node 1', ); $assert( - prediction1b.order == manager.find(1).getOrder(), + prediction1b.order === manager.find(1).getOrder(), 'Prediction order should be the same as node 1', ); @@ -375,19 +374,19 @@ class BalancedTestSuite extends TestSuite { this._plotPrediction(graph1, prediction1c); $assert( prediction1c.position.x < manager.find(0).getPosition().x && - prediction1c.position.y == manager.find(0).getPosition().y, + prediction1c.position.y === manager.find(0).getPosition().y, 'Prediction is incorrectly positioned', ); - $assert(prediction1c.order == 1, 'Prediction order should be the same as node 1'); + $assert(prediction1c.order === 1, 'Prediction order should be the same as node 1'); const prediction1d = manager.predict(0, 1, { x: -50, y: -50 }); this._plotPrediction(graph1, prediction1d); $assert( prediction1d.position.x < manager.find(0).getPosition().x && - prediction1d.position.y == manager.find(0).getPosition().y, + prediction1d.position.y === manager.find(0).getPosition().y, 'Prediction is incorrectly positioned', ); - $assert(prediction1d.order == 1, 'Prediction order should be the same as node 1'); + $assert(prediction1d.order === 1, 'Prediction order should be the same as node 1'); // Graph 2 manager.disconnectNode(1); @@ -399,41 +398,41 @@ class BalancedTestSuite extends TestSuite { this._plotPrediction(graph2, prediction2a); $assert( prediction2a.position.x > manager.find(0).getPosition().x && - prediction2a.position.y == manager.find(0).getPosition().y, + prediction2a.position.y === manager.find(0).getPosition().y, 'Prediction is positioned incorrectly', ); - $assert(prediction2a.order == 0, 'Prediction order should be 0'); + $assert(prediction2a.order === 0, 'Prediction order should be 0'); const prediction2b = manager.predict(0, 1, { x: 50, y: -50 }); this._plotPrediction(graph2, prediction2b); $assert( prediction2b.position.x > manager.find(0).getPosition().x && - prediction2b.position.y == manager.find(0).getPosition().y, + prediction2b.position.y === manager.find(0).getPosition().y, 'Prediction is positioned incorrectly', ); - $assert(prediction2b.order == 0, 'Prediction order should be 0'); + $assert(prediction2b.order === 0, 'Prediction order should be 0'); const prediction2c = manager.predict(0, 1, { x: -50, y: 50 }); this._plotPrediction(graph2, prediction2c); $assert( - prediction2c.position.x == manager.find(1).getPosition().x && - prediction2c.position.y == manager.find(1).getPosition().y, + prediction2c.position.x === manager.find(1).getPosition().x && + prediction2c.position.y === manager.find(1).getPosition().y, 'Prediction position should be the same as node 1', ); $assert( - prediction2c.order == manager.find(1).getOrder(), + prediction2c.order === manager.find(1).getOrder(), 'Prediction order should be the same as node 1', ); const prediction2d = manager.predict(0, 1, { x: -50, y: -50 }); this._plotPrediction(graph2, prediction2d); $assert( - prediction2d.position.x == manager.find(1).getPosition().x && - prediction2d.position.y == manager.find(1).getPosition().y, + prediction2d.position.x === manager.find(1).getPosition().x && + prediction2d.position.y === manager.find(1).getPosition().y, 'Prediction position should be the same as node 1', ); $assert( - prediction2d.order == manager.find(1).getOrder(), + prediction2d.order === manager.find(1).getOrder(), 'Prediction order should be the same as node 1', ); @@ -447,18 +446,18 @@ class BalancedTestSuite extends TestSuite { const prediction3a = manager.predict(0, 1, { x: 50, y: 50 }); this._plotPrediction(graph3, prediction3a); $assert( - prediction3a.position.x == manager.find(2).getPosition().x && - prediction3a.position.y > manager.find(2).getPosition().y, + prediction3a.position.x === manager.find(2).getPosition().x && + prediction3a.position.y > manager.find(2).getPosition().y, 'Prediction is incorrectly positioned', ); - $assert(prediction3a.order == 4, 'Prediction order should be 4'); + $assert(prediction3a.order === 4, 'Prediction order should be 4'); const prediction3b = manager.predict(0, 1, { x: 50, y: -50 }); this._plotPrediction(graph3, prediction3b); $assert( - prediction3b.position.x == manager.find(1).getPosition().x && - prediction3b.position.y == manager.find(1).getPosition().y && - prediction3b.order == manager.find(1).getOrder(), + prediction3b.position.x === manager.find(1).getPosition().x && + prediction3b.position.y === manager.find(1).getPosition().y && + prediction3b.order === manager.find(1).getOrder(), 'Prediction should be the exact same as dragged node', ); @@ -466,29 +465,29 @@ class BalancedTestSuite extends TestSuite { this._plotPrediction(graph3, prediction3c); $assert( prediction3c.position.x < manager.find(0).getPosition().x && - prediction3c.position.y == manager.find(0).getPosition().y, + prediction3c.position.y === manager.find(0).getPosition().y, 'Prediction is incorrectly positioned', ); - $assert(prediction3c.order == 1, 'Prediction order should be 1'); + $assert(prediction3c.order === 1, 'Prediction order should be 1'); const prediction3d = manager.predict(0, 1, { x: -50, y: -50 }); this._plotPrediction(graph3, prediction3d); $assert( prediction3d.position.x < manager.find(0).getPosition().x && - prediction3d.position.y == manager.find(0).getPosition().y, + prediction3d.position.y === manager.find(0).getPosition().y, 'Prediction is incorrectly positioned', ); - $assert(prediction3d.order == 1, 'Prediction order should be 1'); + $assert(prediction3d.order === 1, 'Prediction order should be 1'); const prediction3e = manager.predict(0, 1, { x: 50, y: 0 }); this._plotPrediction(graph3, prediction3e); $assert( - prediction3e.position.x == manager.find(1).getPosition().x && - prediction3e.position.y == manager.find(1).getPosition().y, + prediction3e.position.x === manager.find(1).getPosition().x && + prediction3e.position.y === manager.find(1).getPosition().y, 'Prediction position should be the same as node 1', ); $assert( - prediction3e.order == manager.find(1).getOrder(), + prediction3e.order === manager.find(1).getOrder(), 'Prediction order should be the same as node 1', ); diff --git a/packages/mindplot/tsconfig.json b/packages/mindplot/tsconfig.json index b2a5de29..ed87437f 100644 --- a/packages/mindplot/tsconfig.json +++ b/packages/mindplot/tsconfig.json @@ -10,7 +10,7 @@ "esModuleInterop": true, "resolveJsonModule": true, "declaration": true, - "strictNullChecks": false, + "strictNullChecks": true, "rootDirs": [ "src", ]