Skip to content

Commit

Permalink
#9 chore: Add group movement
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsobspinto committed Aug 1, 2022
1 parent a12df2a commit a69c86a
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 79 deletions.
106 changes: 71 additions & 35 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.

106 changes: 71 additions & 35 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.

3 changes: 3 additions & 0 deletions dist/react-diagrams/MetaNodeModel.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { NodeModel } from '@projectstorm/react-diagrams';
import { Position } from "../models/Position";
export declare class MetaNodeModel extends NodeModel {
constructor(options?: {});
getLocalPosition(nodes: MetaNodeModel[]): Position;
updateLocalPosition(nodes: MetaNodeModel[]): void;
}
4 changes: 2 additions & 2 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const useStyles = makeStyles(_ => ({
const App = () => {
const classes = useStyles();

const grandparent = new MetaNode('group2', 'grandparent', 'default', new Position(0, 0),
const grandparent = new MetaNode('group2', 'grandparent', 'default', new Position(130, 105),
null, new Map(Object.entries({color: 'rgb(0,255,0)'})))

const parent = new MetaNode('group', 'parent', 'default', new Position(130, 105),
Expand All @@ -43,7 +43,7 @@ const App = () => {

return (
<div className={classes.main}>
<MetaDiagram metaNodes={[node1, node2, parent, grandparent]} metaLinks={[link3]} componentsMap={componentsMap}
<MetaDiagram metaNodes={[node1, node2, parent, grandparent]} metaLinks={[]} componentsMap={componentsMap}
metaTheme={{
customThemeVariables: {},
canvasClassName: classes.canvasBG,
Expand Down
16 changes: 16 additions & 0 deletions src/helpers/engineHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {MetaNodeModel} from "../react-diagrams/MetaNodeModel";

export function updateChildrenPosition(nodes: MetaNodeModel[], parent: MetaNodeModel): void {
// @ts-ignore
const children = nodes.filter(n => n.options['parentId'] == parent.options['id']);
children.forEach(n => {
// @ts-ignore
n.setPosition(parent.getX() + n.options['position'].x, parent.getY() + n.options['position'].y)
// TODO: Fix nested position update
// updateChildrenPosition(nodes, n)
})
}

export function updateNodeLocalPosition(nodes: MetaNodeModel[], node: MetaNodeModel): void {
node.updateLocalPosition(nodes)
}
3 changes: 1 addition & 2 deletions src/helpers/nodesHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ export function getNode(
return nodes.find(n => n.getOptions().id === id);
}

// @ts-ignore
export function processNodes(metaNodes: MetaNode[], callback: { (event: any): void; (arg0: BaseEntityEvent<NodeModel<NodeModelGenerics>>): void; }) : MetaNodeModel[] {
const metaNodeModels = []
for(const mn of metaNodes){
const metaNodeModel = mn.toModel()
const position = mn.getWorldPosition()
metaNodeModel.setPosition(position.x, position.y)
// @ts-ignore
//metaNodeModel.registerListener({positionChanged: (event => callback(event))})
metaNodeModel.registerListener({positionChanged: (event => callback(event))})
metaNodeModels.push(metaNodeModel)
}
return metaNodeModels
Expand Down
15 changes: 14 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import CssBaseline from '@mui/material/CssBaseline';
import theme from './theme';
import { Box } from '@mui/material';
import {processNodes} from "./helpers/nodesHelper";
import {updateChildrenPosition} from "./helpers/engineHelper";

const useStyles = makeStyles(_ => ({
container: {
Expand Down Expand Up @@ -62,10 +63,22 @@ const MetaDiagram = ({
// @ts-ignore
.registerFactory(new MetaLinkFactory(componentsMap.links));

// @ts-ignore
const repaintCanvas = (event: any) => {
let model = engine.getModel();
const node = event.entity
const nodes = model.getNodes()
// @ts-ignore
updateChildrenPosition(nodes, node)
// @ts-ignore
// updateNodeLocalPosition(nodes, node)
engine.repaintCanvas();
}

// set up the diagram model

const model = new DiagramModel();
const nodes = processNodes(metaNodes, () => console.log("repaint canvas"))
const nodes = processNodes(metaNodes, repaintCanvas)

const links = metaLinks
.map(ml => getLinkModel(ml, nodes))
Expand Down
Loading

0 comments on commit a69c86a

Please sign in to comment.