Skip to content

Commit

Permalink
Merge pull request #165 from XpressAI/adry/save-points-on-link
Browse files Browse the repository at this point in the history
✨Able save points on link and add listener when moving node
  • Loading branch information
MFA-X-AI authored Jun 16, 2022
2 parents e870597 + 6ae9f9d commit 09283ff
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/components/XircuitsApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ILabShell, JupyterFrontEnd } from '@jupyterlab/application';
import { CustomDiagramState } from './state/CustomDiagramState'
import { CustomLinkModel, TriangleLinkModel } from './link/CustomLinkModel';
import { CustomLinkFactory, TriangleLinkFactory } from './link/CustomLinkFactory';
import { PointModel } from '@projectstorm/react-diagrams';
import { Point } from '@projectstorm/geometry';

export class XircuitsApplication {

Expand Down Expand Up @@ -90,6 +92,8 @@ export class XircuitsApplication {
const newTriangleLink = new TriangleLinkModel();
const sourceNode = tempModel.getNode(link.source);
const targetNode = tempModel.getNode(link.target);
const linkPoints = link.points;
const points = [];

const sourcePort = sourceNode.getPortFromID(link.sourcePort);
const sourcePortName = sourcePort.getOptions()['name'];
Expand All @@ -113,6 +117,11 @@ export class XircuitsApplication {
}
newLink.setTargetPort(targetPort);

// Set points on link if exist
linkPoints.map((point)=> {
points.push(new PointModel({ link: link, position: new Point(point.x, point.y) }));
})
newLink.setPoints(points);
tempModel.addAll(newLink);
diagramEngine.setModel(tempModel);
}
Expand Down
34 changes: 19 additions & 15 deletions src/components/state/DragDiagramItemsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ export class DragDiagramItemsState extends MoveItemsState<DiagramEngine> {
new Action({
type: InputType.MOUSE_UP,
fire: event => {
const item = this.engine.getMouseElement(event.event as MouseEvent<Element, globalThis.MouseEvent>);
if (item instanceof PortModel) {
_.forEach(this.initialPositions, position => {
if (position.item instanceof PointModel) {
const link = position.item.getParent() as LinkModel;
try {
const item = this.engine.getMouseElement(event.event as MouseEvent<Element, globalThis.MouseEvent>);
if (item instanceof PortModel) {
_.forEach(this.initialPositions, position => {
if (position.item instanceof PointModel) {
const link = position.item.getParent() as LinkModel;

// only care about the last links
if (link.getLastPoint() !== position.item) {
return;
// only care about the last links
if (link.getLastPoint() !== position.item) {
return;
}
if (link.getSourcePort().canLinkToPort(item)) {
link.setTargetPort(item);
item.reportPosition();
this.engine.repaintCanvas();
}
}
if (link.getSourcePort().canLinkToPort(item)) {
link.setTargetPort(item);
item.reportPosition();
this.engine.repaintCanvas();
}
}
});
});
}
} catch (e) {
// No-op
}
}
})
Expand Down
24 changes: 24 additions & 0 deletions src/components/state/MoveItemsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class MoveItemsState<E extends CanvasEngine = CanvasEngine> extends Abstr
item: BaseModel;
};
} = {};
initialPosition: Point;
finalPosition: Point;

constructor() {
super({
Expand Down Expand Up @@ -51,11 +53,32 @@ export class MoveItemsState<E extends CanvasEngine = CanvasEngine> extends Abstr
}
element.setSelected(true);
this.engine.repaintCanvas();
this.initialPosition = element['position'];
}
})
);

this.registerAction(
new Action({
type: InputType.MOUSE_UP,
fire: () => {
// When node in the same position, just return
if (
this.initialPosition.x === this.finalPosition.x &&
this.initialPosition.y === this.finalPosition.y
) {
return;
}
this.fireEvent();
}
})
);
}

fireEvent = () => {
this.engine.fireEvent({}, 'onChange');
};

activated(previous: State) {
super.activated(previous);
this.initialPositions = {};
Expand All @@ -78,6 +101,7 @@ export class MoveItemsState<E extends CanvasEngine = CanvasEngine> extends Abstr

const pos = this.initialPositions[item.getID()].point;
item.setPosition(model.getGridPosition(pos.x + event.virtualDisplacementX), model.getGridPosition(pos.y + event.virtualDisplacementY));
this.finalPosition = item.getPosition();
}
}
this.engine.repaintCanvas();
Expand Down

0 comments on commit 09283ff

Please sign in to comment.