From adccf1b38ede40fcb15a88915cc1ddac1b8fb8bb Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Mon, 3 Apr 2023 08:54:32 +0200 Subject: [PATCH] chore(examples): add easy connect example Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com> --- examples/vite/router.ts | 4 + .../src/EasyConnect/CustomConnectionLine.vue | 27 ++++++ examples/vite/src/EasyConnect/CustomNode.vue | 83 +++++++++++++++++++ examples/vite/src/EasyConnect/EasyConnect.vue | 83 +++++++++++++++++++ .../vite/src/EasyConnect/FloatingEdge.vue | 24 ++++++ examples/vite/src/EasyConnect/utils.ts | 73 ++++++++++++++++ 6 files changed, 294 insertions(+) create mode 100644 examples/vite/src/EasyConnect/CustomConnectionLine.vue create mode 100644 examples/vite/src/EasyConnect/CustomNode.vue create mode 100644 examples/vite/src/EasyConnect/EasyConnect.vue create mode 100644 examples/vite/src/EasyConnect/FloatingEdge.vue create mode 100644 examples/vite/src/EasyConnect/utils.ts diff --git a/examples/vite/router.ts b/examples/vite/router.ts index 6c15cd71..ae720fe9 100644 --- a/examples/vite/router.ts +++ b/examples/vite/router.ts @@ -118,6 +118,10 @@ export const routes: RouterOptions['routes'] = [ path: '/rgb', component: () => import('./src/RGBFlow/RGBFlow.vue'), }, + { + path: '/easy-connect', + component: () => import('./src/EasyConnect/EasyConnect.vue'), + }, ] export const router = createRouter({ diff --git a/examples/vite/src/EasyConnect/CustomConnectionLine.vue b/examples/vite/src/EasyConnect/CustomConnectionLine.vue new file mode 100644 index 00000000..7af7f93c --- /dev/null +++ b/examples/vite/src/EasyConnect/CustomConnectionLine.vue @@ -0,0 +1,27 @@ + + + diff --git a/examples/vite/src/EasyConnect/CustomNode.vue b/examples/vite/src/EasyConnect/CustomNode.vue new file mode 100644 index 00000000..55d59781 --- /dev/null +++ b/examples/vite/src/EasyConnect/CustomNode.vue @@ -0,0 +1,83 @@ + + + + + diff --git a/examples/vite/src/EasyConnect/EasyConnect.vue b/examples/vite/src/EasyConnect/EasyConnect.vue new file mode 100644 index 00000000..4ed6aa9d --- /dev/null +++ b/examples/vite/src/EasyConnect/EasyConnect.vue @@ -0,0 +1,83 @@ + + + diff --git a/examples/vite/src/EasyConnect/FloatingEdge.vue b/examples/vite/src/EasyConnect/FloatingEdge.vue new file mode 100644 index 00000000..8e1aef8a --- /dev/null +++ b/examples/vite/src/EasyConnect/FloatingEdge.vue @@ -0,0 +1,24 @@ + + + diff --git a/examples/vite/src/EasyConnect/utils.ts b/examples/vite/src/EasyConnect/utils.ts new file mode 100644 index 00000000..8da3a7eb --- /dev/null +++ b/examples/vite/src/EasyConnect/utils.ts @@ -0,0 +1,73 @@ +import type { GraphNode, XYPosition } from '@vue-flow/core' +import { Position } from '@vue-flow/core' + +// this helper function returns the intersection point +// of the line between the center of the intersectionNode and the target node +function getNodeIntersection(intersectionNode: GraphNode, targetNode: GraphNode) { + // https://math.stackexchange.com/questions/1724792/an-algorithm-for-finding-the-intersection-point-between-a-center-of-vision-and-a + const { + dimensions: { width: intersectionNodeWidth, height: intersectionNodeHeight }, + computedPosition: intersectionNodePosition, + } = intersectionNode + const targetPosition = targetNode.computedPosition + + const w = intersectionNodeWidth / 2 + const h = intersectionNodeHeight / 2 + + const x2 = intersectionNodePosition.x + w + const y2 = intersectionNodePosition.y + h + const x1 = targetPosition.x + w + const y1 = targetPosition.y + h + + const xx1 = (x1 - x2) / (2 * w) - (y1 - y2) / (2 * h) + const yy1 = (x1 - x2) / (2 * w) + (y1 - y2) / (2 * h) + const a = 1 / (Math.abs(xx1) + Math.abs(yy1)) + const xx3 = a * xx1 + const yy3 = a * yy1 + const x = w * (xx3 + yy3) + x2 + const y = h * (-xx3 + yy3) + y2 + + return { x, y } +} + +// returns the position (top,right,bottom or right) passed node compared to the intersection point +function getEdgePosition(node: GraphNode, intersectionPoint: XYPosition) { + const n = { ...node.computedPosition, ...node } + const nx = Math.round(n.x) + const ny = Math.round(n.y) + const px = Math.round(intersectionPoint.x) + const py = Math.round(intersectionPoint.y) + + if (px <= nx + 1) { + return Position.Left + } + if (px >= nx + n.dimensions.width - 1) { + return Position.Right + } + if (py <= ny + 1) { + return Position.Top + } + if (py >= n.y + n.dimensions.height - 1) { + return Position.Bottom + } + + return Position.Top +} + +// returns the parameters (sx, sy, tx, ty, sourcePos, targetPos) you need to create an edge +export function getEdgeParams(source: GraphNode, target: GraphNode) { + const sourceIntersectionPoint = getNodeIntersection(source, target) + const targetIntersectionPoint = getNodeIntersection(target, source) + + const sourcePos = getEdgePosition(source, sourceIntersectionPoint) + const targetPos = getEdgePosition(target, targetIntersectionPoint) + + return { + sx: sourceIntersectionPoint.x, + sy: sourceIntersectionPoint.y, + tx: targetIntersectionPoint.x, + ty: targetIntersectionPoint.y, + sourcePos, + targetPos, + } +}