feat: Extend options to pass custom nodes / edges
* Accept either a Record<string, Component | boolean> or string[] as nodetypes option * If bool / string is used a slot will be expected, otherwise no node/edge will be rendered Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
@import "../dist/theme-default.css";
|
@import "../src/theme-default.css";
|
||||||
|
|
||||||
body {
|
body {
|
||||||
color: #111;
|
color: #111;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import EdgeAnchor from './EdgeAnchor.vue'
|
import EdgeAnchor from './EdgeAnchor.vue'
|
||||||
import { getEdgePositions, getHandle, getSourceTargetNodes, isEdgeVisible } from '~/container/EdgeRenderer/utils'
|
import { getEdgePositions, getHandle, getSourceTargetNodes, isEdgeVisible } from '~/container/EdgeRenderer/utils'
|
||||||
import { isEdge } from '~/utils'
|
import { isEdge } from '~/utils'
|
||||||
import { ConnectionMode, Edge, EdgeType, Position } from '~/types'
|
import { ConnectionMode, Edge, EdgePositions, EdgeType, Position } from '~/types'
|
||||||
import { useHandle, useHooks, useStore } from '~/composables'
|
import { useHandle, useHooks, useStore } from '~/composables'
|
||||||
|
|
||||||
interface EdgeProps {
|
interface EdgeProps {
|
||||||
@@ -64,7 +64,7 @@ const onEdgeUpdaterMouseEnter = () => (updating.value = true)
|
|||||||
|
|
||||||
const onEdgeUpdaterMouseOut = () => (updating.value = false)
|
const onEdgeUpdaterMouseOut = () => (updating.value = false)
|
||||||
|
|
||||||
const isVisible = ({ sourceX, sourceY, targetX, targetY }: ReturnType<typeof getEdgePositions>) => {
|
const isVisible = ({ sourceX, sourceY, targetX, targetY }: EdgePositions) => {
|
||||||
return store.onlyRenderVisibleElements
|
return store.onlyRenderVisibleElements
|
||||||
? isEdgeVisible({
|
? isEdgeVisible({
|
||||||
sourcePos: { x: sourceX, y: sourceY },
|
sourcePos: { x: sourceX, y: sourceY },
|
||||||
|
|||||||
@@ -1,29 +1,5 @@
|
|||||||
import { BezierEdge, SmoothStepEdge, StepEdge, StraightEdge } from '~/components/Edges'
|
|
||||||
import { rectToBox } from '~/utils'
|
import { rectToBox } from '~/utils'
|
||||||
import { Edge, EdgePositions, EdgeType, ElementId, HandleElement, Node, Position, Transform, XYPosition } from '~/types'
|
import { Edge, EdgePositions, ElementId, HandleElement, Node, Position, Transform, XYPosition } from '~/types'
|
||||||
|
|
||||||
export function createEdgeTypes(edgeTypes: Record<string, EdgeType>): Record<string, EdgeType> {
|
|
||||||
const standardTypes: Record<string, EdgeType> = {
|
|
||||||
default: edgeTypes.default || BezierEdge,
|
|
||||||
straight: edgeTypes.bezier || StraightEdge,
|
|
||||||
step: edgeTypes.step || StepEdge,
|
|
||||||
smoothstep: edgeTypes.step || SmoothStepEdge,
|
|
||||||
}
|
|
||||||
|
|
||||||
const wrappedTypes = {} as Record<string, EdgeType>
|
|
||||||
const specialTypes: Record<string, EdgeType> = Object.keys(edgeTypes)
|
|
||||||
.filter((k) => !['default', 'bezier'].includes(k))
|
|
||||||
.reduce((res, key) => {
|
|
||||||
res[key] = edgeTypes[key] || BezierEdge
|
|
||||||
|
|
||||||
return res
|
|
||||||
}, wrappedTypes)
|
|
||||||
|
|
||||||
return {
|
|
||||||
...standardTypes,
|
|
||||||
...specialTypes,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getHandlePosition(position: Position, node: Node, handle: any | null = null): XYPosition {
|
export function getHandlePosition(position: Position, node: Node, handle: any | null = null): XYPosition {
|
||||||
const x = (handle?.x || 0) + node.__rf?.position?.x
|
const x = (handle?.x || 0) + node.__rf?.position?.x
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
import { DefaultNode, InputNode, OutputNode } from '~/components/Nodes'
|
|
||||||
import { NodeType } from '~/types'
|
|
||||||
|
|
||||||
export function createNodeTypes(nodeTypes: Record<string, NodeType>): Record<string, NodeType> {
|
|
||||||
const standardTypes: Record<string, NodeType> = {
|
|
||||||
input: nodeTypes.input || InputNode,
|
|
||||||
default: nodeTypes.default || DefaultNode,
|
|
||||||
output: nodeTypes.output || OutputNode,
|
|
||||||
}
|
|
||||||
|
|
||||||
const wrappedTypes = {} as NodeType
|
|
||||||
const specialTypes: NodeType = Object.keys(nodeTypes)
|
|
||||||
.filter((k) => !['input', 'default', 'output'].includes(k))
|
|
||||||
.reduce((res, key) => {
|
|
||||||
res[key] = nodeTypes[key] || DefaultNode
|
|
||||||
|
|
||||||
return res
|
|
||||||
}, wrappedTypes)
|
|
||||||
|
|
||||||
return {
|
|
||||||
...standardTypes,
|
|
||||||
...specialTypes,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { CSSProperties, onBeforeUnmount } from 'vue'
|
import { CSSProperties, onBeforeUnmount } from 'vue'
|
||||||
import { createEdgeTypes } from '../EdgeRenderer/utils'
|
|
||||||
import { createNodeTypes } from '../NodeRenderer/utils'
|
|
||||||
import ZoomPane from '~/container/ZoomPane/ZoomPane.vue'
|
import ZoomPane from '~/container/ZoomPane/ZoomPane.vue'
|
||||||
import SelectionPane from '~/container/SelectionPane/SelectionPane.vue'
|
import SelectionPane from '~/container/SelectionPane/SelectionPane.vue'
|
||||||
import NodeRenderer from '~/container/NodeRenderer/NodeRenderer.vue'
|
import NodeRenderer from '~/container/NodeRenderer/NodeRenderer.vue'
|
||||||
@@ -24,8 +22,8 @@ import { useHooks, useStore, createHooks } from '~/composables'
|
|||||||
|
|
||||||
export interface FlowProps extends FlowOptions {
|
export interface FlowProps extends FlowOptions {
|
||||||
elements: Elements
|
elements: Elements
|
||||||
nodeTypes?: Record<string, NodeType>
|
nodeTypes?: Record<string, NodeType> | string[]
|
||||||
edgeTypes?: Record<string, EdgeType>
|
edgeTypes?: Record<string, EdgeType> | string[]
|
||||||
connectionMode?: ConnectionMode
|
connectionMode?: ConnectionMode
|
||||||
connectionLineType?: ConnectionLineType
|
connectionLineType?: ConnectionLineType
|
||||||
connectionLineStyle?: CSSProperties
|
connectionLineStyle?: CSSProperties
|
||||||
@@ -134,14 +132,18 @@ watch(
|
|||||||
)
|
)
|
||||||
init(props)
|
init(props)
|
||||||
|
|
||||||
const nodeTypes = controlledComputed(
|
const nodeTypes = computed(() => {
|
||||||
() => props.nodeTypesId,
|
let types = defaultNodeTypes
|
||||||
() => createNodeTypes({ ...defaultNodeTypes, ...props.nodeTypes }),
|
if (Array.isArray(props.nodeTypes)) props.nodeTypes.forEach((type) => (types[type] = true))
|
||||||
)
|
else types = { ...types, ...props.nodeTypes }
|
||||||
const edgeTypes = controlledComputed(
|
return types
|
||||||
() => props.edgeTypesId,
|
})
|
||||||
() => createEdgeTypes({ ...defaultEdgeTypes, ...props.edgeTypes }),
|
const edgeTypes = computed(() => {
|
||||||
)
|
let types = defaultEdgeTypes
|
||||||
|
if (Array.isArray(props.edgeTypes)) props.edgeTypes.forEach((type) => (types[type] = true))
|
||||||
|
else types = { ...types, ...props.edgeTypes }
|
||||||
|
return types
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<div class="vue-flow">
|
<div class="vue-flow">
|
||||||
|
|||||||
+1
-1
@@ -63,7 +63,7 @@ export interface EdgeSmoothStepProps<T = any> extends EdgeProps<T> {
|
|||||||
borderRadius?: number
|
borderRadius?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type EdgeType = DefineComponent<EdgeSmoothStepProps, any, any, any, any, any>
|
export type EdgeType = DefineComponent<EdgeSmoothStepProps, any, any, any, any, any> | boolean
|
||||||
|
|
||||||
export type OnEdgeUpdateFunc<T = any> = (oldEdge: Edge<T>, newConnection: Connection) => void
|
export type OnEdgeUpdateFunc<T = any> = (oldEdge: Edge<T>, newConnection: Connection) => void
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -58,4 +58,4 @@ export interface NodeProps<T = any> {
|
|||||||
dragging?: boolean
|
dragging?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NodeType = DefineComponent<NodeProps, any, any, any, any>
|
export type NodeType = DefineComponent<NodeProps, any, any, any, any> | boolean
|
||||||
|
|||||||
+2
-2
@@ -93,8 +93,8 @@ export type OnLoadFunc<T = any> = (params: FlowInstance<T>) => void
|
|||||||
|
|
||||||
export interface FlowOptions extends Omit<HTMLAttributes, 'onLoad'> {
|
export interface FlowOptions extends Omit<HTMLAttributes, 'onLoad'> {
|
||||||
elements: Elements
|
elements: Elements
|
||||||
nodeTypes?: Record<string, NodeType>
|
nodeTypes?: Record<string, NodeType> | string[]
|
||||||
edgeTypes?: Record<string, EdgeType>
|
edgeTypes?: Record<string, EdgeType> | string[]
|
||||||
connectionMode?: ConnectionMode
|
connectionMode?: ConnectionMode
|
||||||
connectionLineType?: ConnectionLineType
|
connectionLineType?: ConnectionLineType
|
||||||
connectionLineStyle?: CSSProperties
|
connectionLineStyle?: CSSProperties
|
||||||
|
|||||||
Reference in New Issue
Block a user