Skip to content

Commit

Permalink
feat: line is hoverable
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX committed Nov 9, 2023
1 parent c5343c6 commit d17f41e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 16 deletions.
16 changes: 12 additions & 4 deletions packages/visual-flow/src/components/socket/MultiInSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class MultiInSocket extends Socket {
);
}

onMouseDown(): void {
getHoveredLine() {
if (this.connectedLines.length > 0) {
const { x: mouseDx, y: mouseDy } = Point.minus(
this.graph.mouseBoardPos,
Expand Down Expand Up @@ -73,10 +73,18 @@ export class MultiInSocket extends Socket {
nearestLine = line;
}
}
return nearestLine;
} else {
return this.connectedLines[0] ?? null;
}
}

this.disconnectTo(nearestLine);
nearestLine.neverLeaves = this;
this.graph.startDraggingLine(nearestLine);
onMouseDown(): void {
if (this.connectedLines.length > 0) {
const hoveredLine = this.getHoveredLine();
this.disconnectTo(hoveredLine);
hoveredLine.neverLeaves = this;
this.graph.startDraggingLine(hoveredLine);
} else {
// do nothing
}
Expand Down
4 changes: 4 additions & 0 deletions packages/visual-flow/src/components/socket/MultiOutSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class MultiOutSocket extends Socket {
return this.connectedLines;
}

getHoveredLine(): Line | null {
return null;
}

onMouseDown(): void {
const line = this.connectToNewLine(new BasicLine());
this.graph.startDraggingLine(line);
Expand Down
4 changes: 4 additions & 0 deletions packages/visual-flow/src/components/socket/SingleInSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export class SingleInSocket extends Socket {
}
}

getHoveredLine(): Line | null {
return this.connectedLine;
}

protected exportData(): any {
return {};
}
Expand Down
4 changes: 4 additions & 0 deletions packages/visual-flow/src/components/socket/SingleOutSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export class SingleOutSocket extends Socket {
return false;
}

getHoveredLine(): Line | null {
return this.connectedLine;
}

onMouseDown(): void {
if (this.connectedLine) {
// do nothing
Expand Down
30 changes: 24 additions & 6 deletions packages/visual-flow/src/model/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export class Graph {
state: State = idelState;
protected mouseDown: boolean = false;
protected scaleEndTimeout: number = NaN;
protected hoveredItem: Block | Socket | null = null;
protected hoveredItem: Block | [Socket, Line | null] | null = null;
mousePagePos: Point;
mouseGraphPos: Point;
mouseBoardPos: Point;
Expand All @@ -271,10 +271,21 @@ export class Graph {
block.selected = true;
}

setHoveredItem(hoveredItem: Block | Socket | null) {
setHoveredItem(hoveredItem: Block | [Socket, Line | null] | null) {
if (this.hoveredItem !== hoveredItem) {
this.hoveredItem?.onUnhover();
hoveredItem?.onHover();
if (Array.isArray(this.hoveredItem)) {
this.hoveredItem[0].onUnhover();
this.hoveredItem[1]?.onUnhover();
} else {
this.hoveredItem?.onUnhover();
}

if (Array.isArray(hoveredItem)) {
hoveredItem[0].onHover();
hoveredItem[1]?.onHover();
} else {
hoveredItem?.onHover();
}
this.hoveredItem = hoveredItem;
}
}
Expand Down Expand Up @@ -626,7 +637,14 @@ export class Graph {
if (this.state.type === GraphStateType.IDLE) {
const draggingSource = this.getDraggingSource(shiftKey);
if (draggingSource) {
this.setHoveredItem(draggingSource[1] ?? draggingSource[0]);
if (draggingSource[1]) {
this.setHoveredItem([
draggingSource[1],
draggingSource[1].getHoveredLine(),
]);
} else {
this.setHoveredItem(draggingSource[0]);
}
} else {
this.setHoveredItem(null);
}
Expand All @@ -639,7 +657,7 @@ export class Graph {
const { line, predictor } = this.state;
const targetSocket = this.getDraggingTarget(line);
if (targetSocket) {
this.setHoveredItem(targetSocket);
this.setHoveredItem([targetSocket, targetSocket.getHoveredLine()]);
line.setBoardPosB(this.mouseBoardPos, targetSocket.direction);
predictor.setBoardPosB(
line.neverLeaves ? this.mouseBoardPos : targetSocket.boardPos,
Expand Down
4 changes: 2 additions & 2 deletions packages/visual-flow/src/model/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ export abstract class Line extends ModelBase {
this.arrowEl?.setAttribute("d", this.arrowPath);
}

hover() {
onHover() {
this.lineEl?.classList.add("hovered");
this.arrowEl?.classList.add("hovered");
}

unhover() {
onUnhover() {
this.lineEl?.classList.remove("hovered");
this.arrowEl?.classList.remove("hovered");
}
Expand Down
2 changes: 2 additions & 0 deletions packages/visual-flow/src/model/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export abstract class Socket extends ModelBase {
return line;
}

abstract getHoveredLine(): Line | null;

protected abstract exportData(): any;
exportRecord(): SocketRecord {
return {
Expand Down
26 changes: 22 additions & 4 deletions packages/visual-flow/src/view/line.styles.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
import { tokens } from "@fluentui/tokens";
import { makeResetStyles, makeStyles, mergeClasses } from "@refina/griffel";

const colors = {
default: tokens.colorBrandForegroundInverted,
dragging: tokens.colorBrandForegroundOnLightPressed,
hovered: tokens.colorBrandForegroundOnLight,
};

const curveClassName = makeResetStyles({
stroke: tokens.colorCompoundBrandStroke,
stroke: colors.default,
fill: "none",
});

const curveStyles = makeStyles({
dragging: {
stroke: tokens.colorCompoundBrandStrokePressed,
stroke: colors.dragging,
strokeWidth: tokens.strokeWidthThickest,
},
predicting: {
opacity: 0.4,
},
hoverable: {
"&.hovered": {
stroke: colors.hovered,
},
},
});

const arrowClassName = makeResetStyles({
fill: tokens.colorCompoundBrandStroke,
fill: colors.default,
});

const arrowStyles = makeStyles({
dragging: {
fill: tokens.colorCompoundBrandStrokePressed,
fill: colors.dragging,
},
predicting: {
opacity: 0.4,
},
hoverable: {
"&.hovered": {
fill: colors.hovered,
},
},
});

export default {
Expand All @@ -35,11 +51,13 @@ export default {
curveClassName,
dragging && curveStyles.dragging,
predicting && curveStyles.predicting,
!(dragging || predicting) && curveStyles.hoverable,
),
arrow: (dragging: boolean, predicting: boolean) =>
mergeClasses(
arrowClassName,
dragging && arrowStyles.dragging,
predicting && arrowStyles.predicting,
!(dragging || predicting) && arrowStyles.hoverable,
),
};

0 comments on commit d17f41e

Please sign in to comment.