refactor(nodes): Reduce node props

* Reduced node props down to essential and necessary props for custom nodes
This commit is contained in:
Braks
2022-04-04 21:42:48 +02:00
parent cd6b03d4ae
commit 35f3d89240
5 changed files with 50 additions and 56 deletions
+2 -5
View File
@@ -13,7 +13,6 @@ const props = withDefaults(defineProps<NodeProps>(), {
<script lang="ts"> <script lang="ts">
export default { export default {
name: 'DefaultNode', name: 'DefaultNode',
inheritAttrs: false,
} }
</script> </script>
<template> <template>
@@ -23,10 +22,8 @@ export default {
:is-connectable="props.connectable" :is-connectable="props.connectable"
:is-valid-connection="props.isValidTargetPos" :is-valid-connection="props.isValidTargetPos"
/> />
<slot v-bind="props"> <component :is="props.label.component" v-bind="props.label.props" v-if="typeof props.label !== 'string'" />
<component :is="props.label" v-if="typeof props.label !== 'string'" /> <span v-else v-html="props.label" />
<span v-else v-html="props.label"></span>
</slot>
<Handle <Handle
type="source" type="source"
:position="props.sourcePosition" :position="props.sourcePosition"
+2 -5
View File
@@ -12,14 +12,11 @@ const props = withDefaults(defineProps<NodeProps>(), {
<script lang="ts"> <script lang="ts">
export default { export default {
name: 'InputNode', name: 'InputNode',
inheritAttrs: false,
} }
</script> </script>
<template> <template>
<slot v-bind="props"> <component :is="props.label.component" v-bind="props.label.props" v-if="typeof props.label !== 'string'" />
<component :is="props.label" v-if="typeof props.label !== 'string'" /> <span v-else v-html="props.label" />
<span v-else v-html="props.label"></span>
</slot>
<Handle <Handle
type="source" type="source"
:position="props.sourcePosition" :position="props.sourcePosition"
+35 -23
View File
@@ -16,7 +16,7 @@ interface NodeWrapperProps {
const props = defineProps<NodeWrapperProps>() const props = defineProps<NodeWrapperProps>()
const node = useVModel(props, 'node') const node = useVModel(props, 'node')
const { id, store } = useVueFlow() const { store } = useVueFlow()
provide(NodeId, props.node.id) provide(NodeId, props.node.id)
const nodeElement = templateRef<HTMLDivElement>('node-element', null) const nodeElement = templateRef<HTMLDivElement>('node-element', null)
@@ -93,8 +93,6 @@ const onDragStop: DraggableEventListener = ({ event, data: { deltaX, deltaY } })
store.hooks.nodeDragStop.trigger({ event, node: node.value }) store.hooks.nodeDragStop.trigger({ event, node: node.value })
} }
const removeEmpty = (obj: Record<string, any>) => Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null))
store.updateNodePosition({ id: node.value.id, diff: { x: 0, y: 0 } }) store.updateNodePosition({ id: node.value.id, diff: { x: 0, y: 0 } })
onMounted(() => { onMounted(() => {
useResizeObserver(nodeElement, () => store.updateNodeDimensions([{ id: node.value.id, nodeElement: nodeElement.value, forceUpdate: true }])) useResizeObserver(nodeElement, () => store.updateNodeDimensions([{ id: node.value.id, nodeElement: nodeElement.value, forceUpdate: true }]))
@@ -150,31 +148,45 @@ export default {
> >
<slot <slot
v-bind="{ v-bind="{
...node, nodeElement,
nodeElement, id: node.id,
connectable: props.connectable, type: node.type,
draggable: props.draggable, data: node.data,
selectable: props.selectable, selected: !!node.selected,
zIndex: node.dragging || node.selected ? 1000 : node.computedPosition.z, isConnectable: props.connectable,
selected: !!node.selected, position: node.position,
hidden: !!node.hidden, computedPosition: node.computedPosition,
dragging: !!node.dragging, isValidTargetPos: node.isValidTargetPos,
parentNode: node.parentNode ? node.parentNode.id : undefined, isValidSourcePos: node.isValidSourcePos,
parentNode: node.parentNode ? node.parentNode.id : undefined,
dragging: !!node.dragging,
zIndex: node.dragging || node.selected ? 1000 : node.computedPosition.z,
targetPosition: node.targetPosition,
sourcePosition: node.sourcePosition,
label: node.label,
dragHandle: node.dragHandle,
}" }"
> >
<component <component
:is="props.type ?? node.type" :is="props.type ?? node.type"
v-bind="{ v-bind="{
...node, nodeElement,
nodeElement, id: node.id,
connectable: props.connectable, type: node.type,
draggable: props.draggable, data: node.data,
selectable: props.selectable, selected: !!node.selected,
zIndex: node.dragging || node.selected ? 1000 : node.computedPosition.z, connectable: props.connectable,
selected: !!node.selected, position: node.position,
hidden: !!node.hidden, computedPosition: node.computedPosition,
dragging: !!node.dragging, isValidTargetPos: node.isValidTargetPos,
parentNode: node.parentNode ? node.parentNode.id : undefined, isValidSourcePos: node.isValidSourcePos,
parentNode: node.parentNode ? node.parentNode.id : undefined,
dragging: !!node.dragging,
zIndex: node.dragging || node.selected ? 1000 : node.computedPosition.z,
targetPosition: node.targetPosition,
sourcePosition: node.sourcePosition,
label: node.label,
dragHandle: node.dragHandle,
}" }"
/> />
</slot> </slot>
+2 -5
View File
@@ -12,14 +12,11 @@ const props = withDefaults(defineProps<NodeProps>(), {
<script lang="ts"> <script lang="ts">
export default { export default {
name: 'OutputNode', name: 'OutputNode',
inheritAttrs: false,
} }
</script> </script>
<template> <template>
<slot v-bind="props"> <component :is="props.label.component" v-bind="props.label.props" v-if="typeof props.label !== 'string'" />
<component :is="props.label" v-if="typeof props.label !== 'string'" /> <span v-else v-html="props.label" />
<span v-else v-html="props.label"></span>
</slot>
<Handle <Handle
type="source" type="source"
:position="props.targetPosition" :position="props.targetPosition"
+9 -18
View File
@@ -39,36 +39,27 @@ export interface GraphNode<T = any, P = any> extends Node<T> {
export interface NodeProps<T = any> { export interface NodeProps<T = any> {
id: string id: string
dimensions: Dimensions nodeElement: HTMLDivElement
handleBounds: NodeHandleBounds type: string
data: T
selected: boolean
connectable: boolean
/** computed position is the position relative to the node's extent (i.e. parent or CoordinateExtent) */ /** computed position is the position relative to the node's extent (i.e. parent or CoordinateExtent) */
computedPosition: XYZPosition computedPosition: XYZPosition
/** x and y position on the graph */ /** x and y position on the graph */
position: XYPosition position: XYPosition
draggable: boolean
selectable: boolean
connectable: boolean
selected: boolean
dragging: boolean
hidden: boolean
nodeElement: HTMLDivElement
zIndex: number
label?: label?:
| string | string
| { | {
props?: any props?: any
component: any component: any
} }
class?: string
style?: CSSProperties
type?: string
data?: T
targetPosition?: Position
sourcePosition?: Position
isValidTargetPos?: ValidConnectionFunc isValidTargetPos?: ValidConnectionFunc
isValidSourcePos?: ValidConnectionFunc isValidSourcePos?: ValidConnectionFunc
parentNode?: string parentNode?: string
isParent?: boolean dragging: boolean
children?: Node<T>[] zIndex: number
targetPosition?: Position
sourcePosition?: Position
dragHandle?: string dragHandle?: string
} }