Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Able save points on link and add listener when moving node #165

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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