Skip to content

Commit

Permalink
#9 chore: Update local position option on node movement
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsobspinto committed Jul 25, 2022
1 parent c5cd9ff commit 28f0f30
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 38 deletions.
2 changes: 2 additions & 0 deletions dist/helpers/nodesHelper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { MetaNodeModel } from '../react-diagrams/MetaNodeModel';
import { MetaNode } from "../models/MetaNode";
import { BaseEntityEvent } from '@projectstorm/react-canvas-core';
import { NodeModel, NodeModelGenerics } from '@projectstorm/react-diagrams';
import { Position } from "../models/Position";
export declare function getNode(id: string, nodes: MetaNodeModel[]): MetaNodeModel | undefined;
export declare function processNodes(metaNodes: MetaNode[], callback: {
(event: any): void;
(arg0: BaseEntityEvent<NodeModel<NodeModelGenerics>>): void;
}): MetaNodeModel[];
export declare function getLocalPosition(nodes: MetaNodeModel[], metaNodeModel: MetaNodeModel): Position;
43 changes: 28 additions & 15 deletions dist/meta-diagram.cjs.development.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/meta-diagram.cjs.development.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/meta-diagram.cjs.production.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/meta-diagram.cjs.production.min.js.map

Large diffs are not rendered by default.

43 changes: 28 additions & 15 deletions dist/meta-diagram.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/meta-diagram.esm.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/models/Position.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export declare class Position {
y: number;
constructor(x: number, y: number);
add(otherPosition: Position): Position;
sub(otherPosition: Position): Position;
}
13 changes: 12 additions & 1 deletion src/helpers/nodesHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MetaNodeModel } from '../react-diagrams/MetaNodeModel';
import {MetaNode} from "../models/MetaNode";
import { BaseEntityEvent } from '@projectstorm/react-canvas-core';
import { NodeModel, NodeModelGenerics } from '@projectstorm/react-diagrams';
import {Position} from "../models/Position";

export function getNode(
id: string,
Expand All @@ -21,4 +22,14 @@ export function processNodes(metaNodes: MetaNode[], callback: { (event: any): vo
metaNodeModels.push(metaNodeModel)
}
return metaNodeModels
}
}

export function getLocalPosition(nodes: MetaNodeModel[], metaNodeModel: MetaNodeModel) : Position {
const worldPosition = new Position(metaNodeModel.getX(), metaNodeModel.getY())
// @ts-ignore
const parentId = metaNodeModel.options['parentId']
const parent = getNode(parentId, nodes)
return parent ? worldPosition.sub(getLocalPosition(nodes, parent)) : worldPosition
}


8 changes: 5 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { MetaNodeModel } from './react-diagrams/MetaNodeModel';
import { getLinkModel } from './helpers/linksHelper';
import { makeStyles } from '@material-ui/core';
import Sidebar from './components/Sidebar';
import {processNodes} from "./helpers/nodesHelper";
import {getLocalPosition, processNodes} from "./helpers/nodesHelper";

const useStyles = makeStyles(_ => ({
container: {
Expand Down Expand Up @@ -58,10 +58,12 @@ const MetaDiagram = ({
const repaintCanvas = (event: any) => {
let model = engine.getModel();
const node = event.entity
const nodes = model.getNodes()
// @ts-ignore
const nodes = model.getNodes().filter(n => n.options['parentId'] == node.options['id']);
const children = nodes.filter(n => n.options['parentId'] == node.options['id']);
// @ts-ignore
nodes.forEach(n => n.setPosition(node.getX() + n.options['position'].x, node.getY() + n.options['position'].y))
children.forEach(n => n.setPosition(node.getX() + n.options['position'].x, node.getY() + n.options['position'].y))
node.options['position'] = getLocalPosition(nodes, node)
engine.repaintCanvas();
}

Expand Down
4 changes: 4 additions & 0 deletions src/models/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export class Position {
add(otherPosition: Position) : Position {
return new Position(this.x+ otherPosition.x, this.y + otherPosition.y)
}

sub(otherPosition: Position) : Position {
return new Position(this.x - otherPosition.x, this.y - otherPosition.y)
}
}

0 comments on commit 28f0f30

Please sign in to comment.