diff --git a/packages/core/src/components/ConnectionLine/index.ts b/packages/core/src/components/ConnectionLine/index.ts index 025ec5627..39c5a01c1 100644 --- a/packages/core/src/components/ConnectionLine/index.ts +++ b/packages/core/src/components/ConnectionLine/index.ts @@ -23,19 +23,17 @@ const ConnectionLine = defineComponent({ connectionStartHandle, connectionEndHandle, connectionPosition, - connectionLineType, - connectionLineStyle, connectionLineOptions, connectionStatus, viewport, - findNode, + getNode, } = useVueFlow() const connectionLineComponent = inject(Slots)?.['connection-line'] - const fromNode = computed(() => findNode(connectionStartHandle.value?.nodeId)) + const fromNode = computed(() => getNode(connectionStartHandle.value!.nodeId!)) - const toNode = computed(() => findNode(connectionEndHandle.value?.nodeId) ?? null) + const toNode = computed(() => getNode(connectionEndHandle.value!.nodeId!) ?? null) const toXY = computed(() => { return { @@ -99,7 +97,7 @@ const ConnectionLine = defineComponent({ return null } - const type = connectionLineType.value ?? connectionLineOptions.value.type ?? ConnectionLineType.Bezier + const type = connectionLineOptions.value.type ?? ConnectionLineType.Bezier let dAttr = '' @@ -152,10 +150,7 @@ const ConnectionLine = defineComponent({ : h('path', { 'd': dAttr, 'class': [connectionLineOptions.value.class, connectionStatus, 'vue-flow__connection-path'], - 'style': { - ...connectionLineStyle.value, - ...connectionLineOptions.value.style, - }, + 'style': connectionLineOptions.value.style, 'marker-end': markerEnd.value, 'marker-start': markerStart.value, }), diff --git a/packages/core/src/components/Edges/EdgeWrapper.ts b/packages/core/src/components/Edges/EdgeWrapper.ts index d8c61e9a5..5ba644bbc 100644 --- a/packages/core/src/components/Edges/EdgeWrapper.ts +++ b/packages/core/src/components/Edges/EdgeWrapper.ts @@ -1,7 +1,7 @@ import { computed, defineComponent, getCurrentInstance, h, inject, provide, ref, resolveComponent, toRef } from 'vue' import type { Connection, EdgeComponent, HandleType, MouseTouchEvent } from '../../types' import { ConnectionMode, Position } from '../../types' -import { useEdgeHooks, useHandle, useVueFlow } from '../../composables' +import { useHandle, useVueFlow } from '../../composables' import { EdgeId, EdgeRef, Slots } from '../../context' import { ARIA_EDGE_DESC_KEY, @@ -33,8 +33,8 @@ const EdgeWrapper = defineComponent({ noPanClassName, getEdgeTypes, removeSelectedEdges, - findEdge, - findNode, + getEdge, + getNode, isValidConnection, multiSelectionActive, disableKeyboardA11y, @@ -43,9 +43,7 @@ const EdgeWrapper = defineComponent({ edgesFocusable, } = useVueFlow() - const edge = computed(() => findEdge(props.id)!) - - const hooks = useEdgeHooks(edge.value, emits) + const edge = computed(() => getEdge(props.id)!) const slots = inject(Slots) @@ -74,9 +72,6 @@ const EdgeWrapper = defineComponent({ provide(EdgeId, props.id) provide(EdgeRef, edgeEl) - const edgeClass = computed(() => (edge.value.class instanceof Function ? edge.value.class(edge.value) : edge.value.class)) - const edgeStyle = computed(() => (edge.value.style instanceof Function ? edge.value.style(edge.value) : edge.value.style)) - const edgeCmp = computed(() => { const name = edge.value.type || 'default' @@ -116,8 +111,8 @@ const EdgeWrapper = defineComponent({ }) return () => { - const sourceNode = findNode(edge.value.source) - const targetNode = findNode(edge.value.target) + const sourceNode = getNode(edge.value.source) + const targetNode = getNode(edge.value.target) const pathOptions = 'pathOptions' in edge.value ? edge.value.pathOptions : {} if (!sourceNode && !targetNode) { @@ -183,7 +178,7 @@ const EdgeWrapper = defineComponent({ 'vue-flow__edge', `vue-flow__edge-${edgeCmp.value === false ? 'default' : edge.value.type || 'default'}`, noPanClassName.value, - edgeClass.value, + edge.value.class, { updating: mouseOver.value, selected: edge.value.selected, @@ -226,8 +221,7 @@ const EdgeWrapper = defineComponent({ labelBgPadding: edge.value.labelBgPadding, labelBgBorderRadius: edge.value.labelBgBorderRadius, data: edge.value.data, - events: { ...edge.value.events, ...hooks.on }, - style: edgeStyle.value, + style: edge.value.style, markerStart: `url('#${getMarkerId(edge.value.markerStart, vueFlowId)}')`, markerEnd: `url('#${getMarkerId(edge.value.markerEnd, vueFlowId)}')`, sourcePosition, @@ -296,11 +290,11 @@ const EdgeWrapper = defineComponent({ } function onEdgeUpdate(event: MouseTouchEvent, connection: Connection) { - hooks.emit.update({ event, edge: edge.value, connection }) + emits.edgeUpdate({ event, edge: edge.value, connection }) } function onEdgeUpdateEnd(event: MouseTouchEvent) { - hooks.emit.updateEnd({ event, edge: edge.value }) + emits.edgeUpdateEnd({ event, edge: edge.value }) updating.value = false } @@ -316,7 +310,7 @@ const EdgeWrapper = defineComponent({ edgeUpdaterType.value = isSourceHandle ? 'target' : 'source' - hooks.emit.updateStart({ event, edge: edge.value }) + emits.edgeUpdateStart({ event, edge: edge.value }) handlePointerDown(event) } @@ -336,27 +330,27 @@ const EdgeWrapper = defineComponent({ } } - hooks.emit.click(data) + emits.edgeClick(data) } function onEdgeContextMenu(event: MouseEvent) { - hooks.emit.contextMenu({ event, edge: edge.value }) + emits.edgeContextMenu({ event, edge: edge.value }) } function onDoubleClick(event: MouseEvent) { - hooks.emit.doubleClick({ event, edge: edge.value }) + emits.edgeDoubleClick({ event, edge: edge.value }) } function onEdgeMouseEnter(event: MouseEvent) { - hooks.emit.mouseEnter({ event, edge: edge.value }) + emits.edgeMouseEnter({ event, edge: edge.value }) } function onEdgeMouseMove(event: MouseEvent) { - hooks.emit.mouseMove({ event, edge: edge.value }) + emits.edgeMouseMove({ event, edge: edge.value }) } function onEdgeMouseLeave(event: MouseEvent) { - hooks.emit.mouseLeave({ event, edge: edge.value }) + emits.edgeMouseLeave({ event, edge: edge.value }) } function onEdgeUpdaterSourceMouseDown(event: MouseEvent) { @@ -374,9 +368,9 @@ const EdgeWrapper = defineComponent({ if (unselect) { edgeEl.value?.blur() - removeSelectedEdges([findEdge(props.id)!]) + removeSelectedEdges([getEdge(props.id)!]) } else { - addSelectedEdges([findEdge(props.id)!]) + addSelectedEdges([getEdge(props.id)!]) } } } diff --git a/packages/core/src/components/Nodes/DefaultNode.ts b/packages/core/src/components/Nodes/DefaultNode.ts index 839e7a7f7..334d21dc0 100644 --- a/packages/core/src/components/Nodes/DefaultNode.ts +++ b/packages/core/src/components/Nodes/DefaultNode.ts @@ -1,4 +1,4 @@ -import type { Component, FunctionalComponent } from 'vue' +import type { FunctionalComponent } from 'vue' import { h } from 'vue' import Handle from '../Handle/Handle.vue' import type { NodeProps } from '../../types' @@ -7,22 +7,17 @@ import { Position } from '../../types' const DefaultNode: FunctionalComponent> = function ({ sourcePosition = Position.Bottom, targetPosition = Position.Top, - label: _label, connectable = true, - isValidTargetPos, - isValidSourcePos, data, }) { - const label = data.label || _label - return [ - h(Handle as Component, { type: 'target', position: targetPosition, connectable, isValidConnection: isValidTargetPos }), - typeof label !== 'string' && label ? h(label) : h('div', { innerHTML: label }), - h(Handle as Component, { type: 'source', position: sourcePosition, connectable, isValidConnection: isValidSourcePos }), + h(Handle, { type: 'target', position: targetPosition, connectable }), + typeof data.label !== 'string' && data.label ? h(data.label) : h('div', { innerHTML: data.label }), + h(Handle, { type: 'source', position: sourcePosition, connectable }), ] } -DefaultNode.props = ['sourcePosition', 'targetPosition', 'label', 'isValidTargetPos', 'isValidSourcePos', 'connectable', 'data'] +DefaultNode.props = ['sourcePosition', 'targetPosition', 'connectable', 'data'] DefaultNode.inheritAttrs = false DefaultNode.compatConfig = { MODE: 3 } diff --git a/packages/core/src/components/Nodes/InputNode.ts b/packages/core/src/components/Nodes/InputNode.ts index c963faaf0..520035b9b 100644 --- a/packages/core/src/components/Nodes/InputNode.ts +++ b/packages/core/src/components/Nodes/InputNode.ts @@ -1,4 +1,4 @@ -import type { Component, FunctionalComponent } from 'vue' +import type { FunctionalComponent } from 'vue' import { h } from 'vue' import Handle from '../Handle/Handle.vue' import type { NodeProps } from '../../types' @@ -6,20 +6,16 @@ import { Position } from '../../types' const InputNode: FunctionalComponent> = function ({ sourcePosition = Position.Bottom, - label: _label, connectable = true, - isValidSourcePos, data, }) { - const label = data.label || _label - return [ - typeof label !== 'string' && label ? h(label) : h('div', { innerHTML: label }), - h(Handle as Component, { type: 'source', position: sourcePosition, connectable, isValidConnection: isValidSourcePos }), + typeof data.label !== 'string' && data.label ? h(data.label) : h('div', { innerHTML: data.label }), + h(Handle, { type: 'source', position: sourcePosition, connectable }), ] } -InputNode.props = ['sourcePosition', 'label', 'isValidSourcePos', 'connectable', 'data'] +InputNode.props = ['sourcePosition', 'connectable', 'data'] InputNode.inheritAttrs = false InputNode.compatConfig = { MODE: 3 } diff --git a/packages/core/src/components/Nodes/NodeWrapper.ts b/packages/core/src/components/Nodes/NodeWrapper.ts index 9c443ea49..ee5435e32 100644 --- a/packages/core/src/components/Nodes/NodeWrapper.ts +++ b/packages/core/src/components/Nodes/NodeWrapper.ts @@ -24,7 +24,7 @@ import { handleNodeClick, } from '../../utils' import { NodeId, NodeRef, Slots } from '../../context' -import { isInputDOMNode, useDrag, useNode, useNodeHooks, useUpdateNodePositions, useVueFlow } from '../../composables' +import { isInputDOMNode, useDrag, useNode, useUpdateNodePositions, useVueFlow } from '../../composables' import type { NodeComponent } from '../../types' interface Props { @@ -92,7 +92,7 @@ const NodeWrapper = defineComponent({ return slot } - let nodeType = node.template || getNodeTypes.value[name] + let nodeType = getNodeTypes.value[name] if (typeof nodeType === 'string') { if (instance) { @@ -112,8 +112,6 @@ const NodeWrapper = defineComponent({ return false }) - const { emit, on } = useNodeHooks(node, emits) - const dragging = useDrag({ id: props.id, el: nodeElement, @@ -121,33 +119,28 @@ const NodeWrapper = defineComponent({ selectable: isSelectable, dragHandle: () => node.dragHandle, onStart(event) { - emit.dragStart(event) + emits.nodeDragStart(event) }, onDrag(event) { - emit.drag(event) + emits.nodeDrag(event) }, onStop(event) { - emit.dragStop(event) + emits.nodeDragStop(event) }, onClick(event) { onSelectNode(event) }, }) - const getClass = computed(() => (node.class instanceof Function ? node.class(node) : node.class)) - const getStyle = computed(() => { - const styles = (node.style instanceof Function ? node.style(node) : node.style) || {} - - const width = node.width instanceof Function ? node.width(node) : node.width - const height = node.height instanceof Function ? node.height(node) : node.height + const styles = node.style || {} - if (width) { - styles.width = typeof width === 'string' ? width : `${width}px` + if (node.width) { + styles.width = typeof node.width === 'string' ? node.width : `${node.width}px` } - if (height) { - styles.height = typeof height === 'string' ? height : `${height}px` + if (node.height) { + styles.height = typeof node.height === 'string' ? node.height : `${node.height}px` } return styles @@ -260,7 +253,7 @@ const NodeWrapper = defineComponent({ selectable: isSelectable.value, parent: node.isParent, }, - getClass.value, + node.class, ], 'style': { visibility: isInit.value ? 'visible' : 'hidden', @@ -282,25 +275,20 @@ const NodeWrapper = defineComponent({ 'onKeydown': onKeyDown, }, [ - h(nodeCmp.value === false ? getNodeTypes.value.default : (nodeCmp.value as any), { + h(nodeCmp.value === false ? getNodeTypes.value.default : nodeCmp.value, { id: node.id, type: node.type, data: node.data, - events: { ...node.events, ...on }, selected: node.selected, resizing: node.resizing, dragging: dragging.value, connectable: isConnectable.value, - position: node.computedPosition, dimensions: node.dimensions, - isValidTargetPos: node.isValidTargetPos, - isValidSourcePos: node.isValidSourcePos, parent: node.parentNode, parentNodeId: node.parentNode, zIndex: node.computedPosition.z ?? zIndex.value, targetPosition: node.targetPosition, sourcePosition: node.sourcePosition, - label: node.label, dragHandle: node.dragHandle, onUpdateNodeInternals: updateInternals, }), @@ -336,28 +324,28 @@ const NodeWrapper = defineComponent({ function onMouseEnter(event: MouseEvent) { if (!dragging?.value) { - emit.mouseEnter({ event, node }) + emits.nodeMouseEnter({ event, node }) } } function onMouseMove(event: MouseEvent) { if (!dragging?.value) { - emit.mouseMove({ event, node }) + emits.nodeMouseMove({ event, node }) } } function onMouseLeave(event: MouseEvent) { if (!dragging?.value) { - emit.mouseLeave({ event, node }) + emits.nodeMouseLeave({ event, node }) } } function onContextMenu(event: MouseEvent) { - return emit.contextMenu({ event, node }) + return emits.nodeContextMenu({ event, node }) } function onDoubleClick(event: MouseEvent) { - return emit.doubleClick({ event, node }) + return emits.nodeDoubleClick({ event, node }) } function onSelectNode(event: MouseEvent) { @@ -373,7 +361,7 @@ const NodeWrapper = defineComponent({ ) } - emit.click({ event, node }) + emits.nodeClick({ event, node }) } function onKeyDown(event: KeyboardEvent) { diff --git a/packages/core/src/components/Nodes/OutputNode.ts b/packages/core/src/components/Nodes/OutputNode.ts index 02fb7933b..3c17a8fb4 100644 --- a/packages/core/src/components/Nodes/OutputNode.ts +++ b/packages/core/src/components/Nodes/OutputNode.ts @@ -1,4 +1,4 @@ -import type { Component, FunctionalComponent } from 'vue' +import type { FunctionalComponent } from 'vue' import { h } from 'vue' import Handle from '../Handle/Handle.vue' import type { NodeProps } from '../../types' @@ -6,20 +6,16 @@ import { Position } from '../../types' const OutputNode: FunctionalComponent> = function ({ targetPosition = Position.Top, - label: _label, connectable = true, - isValidTargetPos, data, }) { - const label = data.label || _label - return [ - h(Handle as Component, { type: 'target', position: targetPosition, connectable, isValidConnection: isValidTargetPos }), - typeof label !== 'string' && label ? h(label) : h('div', { innerHTML: label }), + h(Handle, { type: 'target', position: targetPosition, connectable }), + typeof data.label !== 'string' && data.label ? h(data.label) : h('div', { innerHTML: data.label }), ] } -OutputNode.props = ['targetPosition', 'label', 'isValidTargetPos', 'connectable', 'data'] +OutputNode.props = ['targetPosition', 'connectable', 'data'] OutputNode.inheritAttrs = false OutputNode.compatConfig = { MODE: 3 } diff --git a/packages/core/src/composables/index.ts b/packages/core/src/composables/index.ts index e7f6c2eff..9860211c8 100644 --- a/packages/core/src/composables/index.ts +++ b/packages/core/src/composables/index.ts @@ -1,11 +1,9 @@ export * from './useDrag' export * from './useEdge' -export * from './useEdgeHooks' export * from './useGetPointerPosition' export * from './useHandle' export * from './useKeyPress' export * from './useNode' -export * from './useNodeHooks' export * from './useUpdateNodePositions' export * from './useViewportHelper' export * from './useVueFlow' diff --git a/packages/core/src/composables/useDrag.ts b/packages/core/src/composables/useDrag.ts index c84f8f5ef..71401b35f 100644 --- a/packages/core/src/composables/useDrag.ts +++ b/packages/core/src/composables/useDrag.ts @@ -49,11 +49,11 @@ export function useDrag(params: UseDragParams) { autoPanSpeed, nodesDraggable, panBy, - findNode, + getNode, multiSelectionActive, nodesSelectionActive, selectNodesOnDrag, - removeSelectedElements, + removeSelectedNodes, addSelectedNodes, updateNodePositions, emits, @@ -97,7 +97,7 @@ export function useDrag(params: UseDragParams) { nextPosition, emits.error, nodeExtent.value, - n.parentNode ? findNode(n.parentNode) : undefined, + n.parentNode ? getNode(n.parentNode) : undefined, ) // we want to make sure that we only fire a change event when there is a change @@ -120,7 +120,7 @@ export function useDrag(params: UseDragParams) { const [currentNode, nodes] = getEventHandlerParams({ id, dragItems, - findNode, + getNode, }) onDrag({ event: dragEvent, node: currentNode, nodes }) @@ -151,11 +151,11 @@ export function useDrag(params: UseDragParams) { const startDrag = (event: UseDragEvent, nodeEl: Element) => { dragStarted = true - const node = findNode(id) + const node = getNode(id!) if (!selectNodesOnDrag.value && !multiSelectionActive.value && node) { if (!node.selected) { // we need to reset selected nodes when selectNodesOnDrag=false - removeSelectedElements() + removeSelectedNodes() } } @@ -164,7 +164,7 @@ export function useDrag(params: UseDragParams) { node, multiSelectionActive.value, addSelectedNodes, - removeSelectedElements, + removeSelectedNodes, nodesSelectionActive, false, nodeEl as HTMLDivElement, @@ -173,13 +173,13 @@ export function useDrag(params: UseDragParams) { const pointerPos = getPointerPosition(event) lastPos = pointerPos - dragItems = getDragItems(nodes.value, nodesDraggable.value, pointerPos, findNode, id) + dragItems = getDragItems(nodes.value, nodesDraggable.value, pointerPos, getNode, id) if (dragItems.length) { const [currentNode, nodes] = getEventHandlerParams({ id, dragItems, - findNode, + getNode, }) onStart({ event: event.sourceEvent, node: currentNode, nodes }) @@ -256,7 +256,7 @@ export function useDrag(params: UseDragParams) { const [currentNode, nodes] = getEventHandlerParams({ id, dragItems, - findNode, + getNode, }) onStop({ event: event.sourceEvent, node: currentNode, nodes }) diff --git a/packages/core/src/composables/useEdge.ts b/packages/core/src/composables/useEdge.ts index 8a1d92871..567073347 100644 --- a/packages/core/src/composables/useEdge.ts +++ b/packages/core/src/composables/useEdge.ts @@ -1,5 +1,5 @@ import { inject, ref } from 'vue' -import type { CustomEvent, ElementData } from '../types' +import type { ElementData } from '../types' import { ErrorCode, VueFlowError } from '../utils' import { EdgeId, EdgeRef } from '../context' import { useVueFlow } from './useVueFlow' @@ -15,13 +15,13 @@ import { useVueFlow } from './useVueFlow' * @param id - The id of the edge to access * @returns the edge id, the edge and the edge dom element */ -export function useEdge = any>(id?: string) { +export function useEdge(id?: string) { const edgeId = id ?? inject(EdgeId, '') const edgeEl = inject(EdgeRef, ref(null)) - const { findEdge, emits } = useVueFlow() + const { getEdge, emits } = useVueFlow() - const edge = findEdge(edgeId)! + const edge = getEdge(edgeId)! if (!edge) { emits.error(new VueFlowError(ErrorCode.EDGE_NOT_FOUND, edgeId)) diff --git a/packages/core/src/composables/useEdgeHooks.ts b/packages/core/src/composables/useEdgeHooks.ts deleted file mode 100644 index cd6dd8144..000000000 --- a/packages/core/src/composables/useEdgeHooks.ts +++ /dev/null @@ -1,80 +0,0 @@ -import type { EdgeEventsEmit, EdgeEventsOn, GraphEdge, VueFlowStore } from '../types' -import { createExtendedEventHook } from '../utils' - -function createEdgeHooks() { - return { - doubleClick: createExtendedEventHook(), - click: createExtendedEventHook(), - mouseEnter: createExtendedEventHook(), - mouseMove: createExtendedEventHook(), - mouseLeave: createExtendedEventHook(), - contextMenu: createExtendedEventHook(), - updateStart: createExtendedEventHook(), - update: createExtendedEventHook(), - updateEnd: createExtendedEventHook(), - } -} - -/** - * Composable for handling edge events - * - * @internal - */ -export function useEdgeHooks(edge: GraphEdge, emits: VueFlowStore['emits']): { emit: EdgeEventsEmit; on: EdgeEventsOn } { - const edgeHooks = createEdgeHooks() - - edgeHooks.doubleClick.on((event) => { - emits.edgeDoubleClick(event) - edge.events?.doubleClick?.(event) - }) - - edgeHooks.click.on((event) => { - emits.edgeClick(event) - edge.events?.click?.(event) - }) - - edgeHooks.mouseEnter.on((event) => { - emits.edgeMouseEnter(event) - edge.events?.mouseEnter?.(event) - }) - - edgeHooks.mouseMove.on((event) => { - emits.edgeMouseMove(event) - edge.events?.mouseMove?.(event) - }) - - edgeHooks.mouseLeave.on((event) => { - emits.edgeMouseLeave(event) - edge.events?.mouseLeave?.(event) - }) - - edgeHooks.contextMenu.on((event) => { - emits.edgeContextMenu(event) - edge.events?.contextMenu?.(event) - }) - - edgeHooks.updateStart.on((event) => { - emits.edgeUpdateStart(event) - edge.events?.updateStart?.(event) - }) - - edgeHooks.update.on((event) => { - emits.edgeUpdate(event) - edge.events?.update?.(event) - }) - - edgeHooks.updateEnd.on((event) => { - emits.edgeUpdateEnd(event) - edge.events?.updateEnd?.(event) - }) - - return Object.entries(edgeHooks).reduce( - (hooks, [key, value]) => { - hooks.emit[key as keyof EdgeEventsEmit] = value.trigger - hooks.on[key as keyof EdgeEventsOn] = value.on - - return hooks - }, - { emit: {} as EdgeEventsEmit, on: {} as EdgeEventsOn }, - ) -} diff --git a/packages/core/src/composables/useEdgesData.ts b/packages/core/src/composables/useEdgesData.ts index 4ff5f3089..97bdda282 100644 --- a/packages/core/src/composables/useEdgesData.ts +++ b/packages/core/src/composables/useEdgesData.ts @@ -29,14 +29,14 @@ export function useEdgesData( guard: (node: Edge) => node is EdgeType, ): ComputedRef[]> export function useEdgesData(_edgeIds: any): any { - const { findEdge } = useVueFlow() + const { getEdge } = useVueFlow() return computed({ get() { const edgeIds = toValue(_edgeIds) if (!Array.isArray(edgeIds)) { - const edge = findEdge(edgeIds) + const edge = getEdge(edgeIds) if (edge) { return { @@ -52,7 +52,7 @@ export function useEdgesData(_edgeIds: any): any { const data: EdgeData[] = [] for (const edgeId of edgeIds) { - const edge = findEdge(edgeId) + const edge = getEdge(edgeId) if (edge) { data.push({ diff --git a/packages/core/src/composables/useHandle.ts b/packages/core/src/composables/useHandle.ts index e20f1ad44..55b6f2b3c 100644 --- a/packages/core/src/composables/useHandle.ts +++ b/packages/core/src/composables/useHandle.ts @@ -57,7 +57,7 @@ export function useHandle({ nodesConnectable, autoPanOnConnect, autoPanSpeed, - findNode, + getNode, panBy, startConnection, updateConnection, @@ -83,13 +83,7 @@ export function useHandle({ const doc = getHostForElement(event.target as HTMLElement) if ((isMouseTriggered && event.button === 0) || !isMouseTriggered) { - const node = findNode(toValue(nodeId)) - - let isValidConnectionHandler = toValue(isValidConnection) || isValidConnectionProp.value || alwaysValid - - if (!isValidConnectionHandler && node) { - isValidConnectionHandler = (!isTarget ? node.isValidTargetPos : node.isValidSourcePos) || alwaysValid - } + const isValidConnectionHandler = toValue(isValidConnection) || isValidConnectionProp.value || alwaysValid let closestHandle: ConnectionHandle | null @@ -163,7 +157,7 @@ export function useHandle({ doc, edges.value, nodes.value, - findNode, + getNode, ), ) @@ -270,20 +264,14 @@ export function useHandle({ return } - const isTarget = toValue(type) === 'target' - if (!connectionClickStartHandle.value) { emits.clickConnectStart({ event, nodeId: toValue(nodeId), handleId: toValue(handleId) }) startConnection({ nodeId: toValue(nodeId), type: toValue(type), handleId: toValue(handleId) }, undefined, true) } else { - let isValidConnectionHandler = toValue(isValidConnection) || isValidConnectionProp.value || alwaysValid - - const node = findNode(toValue(nodeId)) + const isValidConnectionHandler = toValue(isValidConnection) || isValidConnectionProp.value || alwaysValid - if (!isValidConnectionHandler && node) { - isValidConnectionHandler = (!isTarget ? node.isValidTargetPos : node.isValidSourcePos) || alwaysValid - } + const node = getNode(toValue(nodeId)) if (node && (typeof node.connectable === 'undefined' ? nodesConnectable.value : node.connectable) === false) { return @@ -306,7 +294,7 @@ export function useHandle({ doc, edges.value, nodes.value, - findNode, + getNode, ) const isOwnHandle = connection.source === connection.target diff --git a/packages/core/src/composables/useNode.ts b/packages/core/src/composables/useNode.ts index 2355a98fb..467f4cb83 100644 --- a/packages/core/src/composables/useNode.ts +++ b/packages/core/src/composables/useNode.ts @@ -1,5 +1,5 @@ import { computed, inject, ref } from 'vue' -import type { CustomEvent, ElementData } from '../types' +import type { ElementData } from '../types' import { ErrorCode, VueFlowError, getConnectedEdges } from '../utils' import { NodeRef } from '../context' import { useVueFlow } from './useVueFlow' @@ -16,13 +16,13 @@ import { useNodeId } from './useNodeId' * @param id - The id of the node to access * @returns the node id, the node, the node dom element, it's parent and connected edges */ -export function useNode = any>(id?: string) { +export function useNode(id?: string) { const nodeId = id ?? useNodeId() ?? '' const nodeEl = inject(NodeRef, ref(null)) - const { findNode, edges, emits } = useVueFlow() + const { getNode, edges, emits } = useVueFlow() - const node = findNode(nodeId)! + const node = getNode(nodeId)! if (!node) { emits.error(new VueFlowError(ErrorCode.NODE_NOT_FOUND, nodeId)) @@ -32,7 +32,7 @@ export function useNode findNode(node.parentNode)), + parentNode: computed(() => (node.parentNode ? getNode(node.parentNode) : undefined)), connectedEdges: computed(() => getConnectedEdges([node], edges.value)), } } diff --git a/packages/core/src/composables/useNodeHooks.ts b/packages/core/src/composables/useNodeHooks.ts deleted file mode 100644 index 4ab047624..000000000 --- a/packages/core/src/composables/useNodeHooks.ts +++ /dev/null @@ -1,79 +0,0 @@ -import type { GraphNode, NodeEventsEmit, NodeEventsOn, VueFlowStore } from '../types' -import { createExtendedEventHook } from '../utils' - -function createNodeHooks() { - return { - doubleClick: createExtendedEventHook(), - click: createExtendedEventHook(), - mouseEnter: createExtendedEventHook(), - mouseMove: createExtendedEventHook(), - mouseLeave: createExtendedEventHook(), - contextMenu: createExtendedEventHook(), - dragStart: createExtendedEventHook(), - drag: createExtendedEventHook(), - dragStop: createExtendedEventHook(), - } -} - -/** - * Composable for handling node events - * - * @internal - */ -export function useNodeHooks(node: GraphNode, emits: VueFlowStore['emits']): { emit: NodeEventsEmit; on: NodeEventsOn } { - const nodeHooks = createNodeHooks() - - nodeHooks.doubleClick.on((event) => { - emits.nodeDoubleClick(event) - node.events?.doubleClick?.(event) - }) - - nodeHooks.click.on((event) => { - emits.nodeClick(event) - node.events?.click?.(event) - }) - - nodeHooks.mouseEnter.on((event) => { - emits.nodeMouseEnter(event) - node.events?.mouseEnter?.(event) - }) - - nodeHooks.mouseMove.on((event) => { - emits.nodeMouseMove(event) - node.events?.mouseMove?.(event) - }) - - nodeHooks.mouseLeave.on((event) => { - emits.nodeMouseLeave(event) - node.events?.mouseLeave?.(event) - }) - - nodeHooks.contextMenu.on((event) => { - emits.nodeContextMenu(event) - node.events?.contextMenu?.(event) - }) - - nodeHooks.dragStart.on((event) => { - emits.nodeDragStart(event) - node.events?.dragStart?.(event) - }) - - nodeHooks.drag.on((event) => { - emits.nodeDrag(event) - node.events?.drag?.(event) - }) - - nodeHooks.dragStop.on((event) => { - emits.nodeDragStop(event) - node.events?.dragStop?.(event) - }) - - return Object.entries(nodeHooks).reduce( - (hooks, [key, value]) => { - hooks.emit[key as keyof NodeEventsEmit] = value.trigger - hooks.on[key as keyof NodeEventsOn] = value.on - return hooks - }, - { emit: {} as NodeEventsEmit, on: {} as NodeEventsOn }, - ) -} diff --git a/packages/core/src/composables/useNodesData.ts b/packages/core/src/composables/useNodesData.ts index 9c1e8ca21..48c03312b 100644 --- a/packages/core/src/composables/useNodesData.ts +++ b/packages/core/src/composables/useNodesData.ts @@ -29,14 +29,14 @@ export function useNodesData( guard: (node: Node) => node is NodeType, ): ComputedRef[]> export function useNodesData(_nodeIds: any): any { - const { findNode } = useVueFlow() + const { getNode } = useVueFlow() return computed({ get() { const nodeIds = toValue(_nodeIds) if (!Array.isArray(nodeIds)) { - const node = findNode(nodeIds) + const node = getNode(nodeIds) if (node) { return { @@ -52,7 +52,7 @@ export function useNodesData(_nodeIds: any): any { const data: NodeData[] = [] for (const nodeId of nodeIds) { - const node = findNode(nodeId) + const node = getNode(nodeId) if (node) { data.push({ diff --git a/packages/core/src/composables/useOnInitHandler.ts b/packages/core/src/composables/useOnInitHandler.ts index 142bd2529..5f2a196f4 100644 --- a/packages/core/src/composables/useOnInitHandler.ts +++ b/packages/core/src/composables/useOnInitHandler.ts @@ -1,4 +1,4 @@ -import { watch } from 'vue' +import { onMounted } from 'vue' import { useVueFlow } from './useVueFlow' /** @@ -9,15 +9,9 @@ import { useVueFlow } from './useVueFlow' export function useOnInitHandler() { const vfInstance = useVueFlow() - watch( - () => vfInstance.viewportHelper.value.viewportInitialized, - (isInitialized) => { - if (isInitialized) { - setTimeout(() => { - vfInstance.emits.init(vfInstance) - vfInstance.emits.paneReady(vfInstance) - }, 1) - } - }, - ) + onMounted(() => { + setTimeout(() => { + vfInstance.emits.init(vfInstance) + }, 1) + }) } diff --git a/packages/core/src/composables/useUpdateNodePositions.ts b/packages/core/src/composables/useUpdateNodePositions.ts index 23e3416f0..0ad3c42e2 100644 --- a/packages/core/src/composables/useUpdateNodePositions.ts +++ b/packages/core/src/composables/useUpdateNodePositions.ts @@ -8,8 +8,7 @@ import { useVueFlow } from './useVueFlow' * @internal */ export function useUpdateNodePositions() { - const { getSelectedNodes, nodeExtent, updateNodePositions, findNode, snapGrid, snapToGrid, nodesDraggable, emits } = - useVueFlow() + const { getSelectedNodes, nodeExtent, updateNodePositions, getNode, snapGrid, snapToGrid, nodesDraggable, emits } = useVueFlow() return (positionDiff: XYPosition, isShiftPressed = false) => { // by default a node moves 5px on each key press, or 20px if shift is pressed @@ -31,7 +30,7 @@ export function useUpdateNodePositions() { nextPosition, emits.error, nodeExtent.value, - node.parentNode ? findNode(node.parentNode) : undefined, + node.parentNode ? getNode(node.parentNode) : undefined, ) nodeUpdates.push({ diff --git a/packages/core/src/composables/useViewportHelper.ts b/packages/core/src/composables/useViewportHelper.ts index 586984172..df39c1509 100644 --- a/packages/core/src/composables/useViewportHelper.ts +++ b/packages/core/src/composables/useViewportHelper.ts @@ -28,9 +28,7 @@ const initialViewportHelper: ViewportHelper = { screenToFlowCoordinate: (position) => position, flowToScreenCoordinate: (position) => position, setViewport: noop, - setTransform: noop, getViewport: () => ({ x: 0, y: 0, zoom: 1 }), - getTransform: () => ({ x: 0, y: 0, zoom: 1 }), viewportInitialized: false, } @@ -109,21 +107,11 @@ export function useViewportHelper(state: State) { setViewport: (transform, options) => { return transformViewport(transform.x, transform.y, transform.zoom, options?.duration) }, - setTransform: (transform, options) => { - return transformViewport(transform.x, transform.y, transform.zoom, options?.duration) - }, getViewport: () => ({ x: state.viewport.x, y: state.viewport.y, zoom: state.viewport.zoom, }), - getTransform: () => { - return { - x: state.viewport.x, - y: state.viewport.y, - zoom: state.viewport.zoom, - } - }, fitView: ( options = { padding: DEFAULT_PADDING, diff --git a/packages/core/src/composables/useVueFlow.ts b/packages/core/src/composables/useVueFlow.ts index c4f7427a0..a500402ad 100644 --- a/packages/core/src/composables/useVueFlow.ts +++ b/packages/core/src/composables/useVueFlow.ts @@ -1,8 +1,8 @@ import { tryOnScopeDispose } from '@vueuse/core' import type { EffectScope } from 'vue' -import { effectScope, getCurrentInstance, getCurrentScope, inject, provide, watch } from 'vue' -import type { EdgeChange, FlowOptions, NodeChange, VueFlowStore } from '../types' -import { ErrorCode, VueFlowError, warn } from '../utils' +import { effectScope, getCurrentScope, inject, provide, watch } from 'vue' +import type { EdgeChange, NodeChange, VueFlowStore } from '../types' +import { warn } from '../utils' import { VueFlow } from '../context' import { Storage } from '../utils/storage' @@ -19,20 +19,13 @@ type Scope = (EffectScope & { vueFlowId: string }) | undefined * * @public * @returns a vue flow store instance - * @param idOrOpts - id of the store instance or options to create a new store instance + * @param id - id of the store instance or options to create a new store instance */ -export function useVueFlow(id?: string): VueFlowStore -export function useVueFlow(options?: FlowOptions): VueFlowStore -export function useVueFlow(idOrOpts?: any): VueFlowStore { +export function useVueFlow(id?: string): VueFlowStore { const storage = Storage.getInstance() const scope = getCurrentScope() as Scope - const isOptsObj = typeof idOrOpts === 'object' - - const options = isOptsObj ? idOrOpts : { id: idOrOpts } - - const id = options.id const vueFlowId = scope?.vueFlowId || id let vueFlow: Injection @@ -66,7 +59,7 @@ export function useVueFlow(idOrOpts?: any): VueFlowStore { if (!vueFlow || (vueFlow && id && id !== vueFlow.id)) { const name = id ?? storage.getId() - const state = storage.create(name, options) + const state = storage.create(name) vueFlow = state @@ -118,11 +111,6 @@ export function useVueFlow(idOrOpts?: any): VueFlowStore { } }) }) - } else { - // If options were passed, overwrite state with the options' values - if (isOptsObj) { - vueFlow.setState(options) - } } // Provide a fresh instance into context if we are in a scope @@ -132,14 +120,5 @@ export function useVueFlow(idOrOpts?: any): VueFlowStore { scope.vueFlowId = vueFlow.id } - if (isOptsObj) { - const instance = getCurrentInstance() - - // ignore the warning if we are in a VueFlow component - if (instance?.type.name !== 'VueFlow') { - vueFlow.emits.error(new VueFlowError(ErrorCode.USEVUEFLOW_OPTIONS)) - } - } - return vueFlow } diff --git a/packages/core/src/composables/useWatchProps.ts b/packages/core/src/composables/useWatchProps.ts index 4dd9da6a6..42cb8e01f 100644 --- a/packages/core/src/composables/useWatchProps.ts +++ b/packages/core/src/composables/useWatchProps.ts @@ -2,7 +2,7 @@ import type { ToRefs } from 'vue' import { effectScope, isRef, nextTick, onScopeDispose, toRef, watch } from 'vue' import type { WatchPausableReturn } from '@vueuse/core' import { watchPausable } from '@vueuse/core' -import type { Connection, FlowProps, VueFlowStore } from '../types' +import type { FlowProps, VueFlowStore } from '../types' import { isDef } from '../utils' /** @@ -13,60 +13,10 @@ import { isDef } from '../utils' * @param props * @param store */ -export function useWatchProps( - models: ToRefs>, - props: FlowProps, - store: VueFlowStore, -) { +export function useWatchProps(models: ToRefs>, props: FlowProps, store: VueFlowStore) { const scope = effectScope(true) scope.run(() => { - const watchModelValue = () => { - scope.run(() => { - let pauseModel: WatchPausableReturn - let pauseStore: WatchPausableReturn - - let immediateStore = !!(store.nodes.value.length || store.edges.value.length) - - // eslint-disable-next-line prefer-const - pauseModel = watchPausable([models.modelValue, () => models.modelValue?.value?.length], ([elements]) => { - if (elements && Array.isArray(elements)) { - pauseStore?.pause() - - store.setElements(elements) - - // only trigger store watcher immediately if we actually set any elements to the store - if (!pauseStore && !immediateStore && elements.length) { - immediateStore = true - } else { - pauseStore?.resume() - } - } - }) - - pauseStore = watchPausable( - [store.nodes, store.edges, () => store.edges.value.length, () => store.nodes.value.length], - ([nodes, edges]) => { - if (models.modelValue?.value && Array.isArray(models.modelValue.value)) { - pauseModel?.pause() - - models.modelValue.value = [...nodes, ...edges] - - nextTick(() => { - pauseModel?.resume() - }) - } - }, - { immediate: immediateStore }, - ) - - onScopeDispose(() => { - pauseModel?.stop() - pauseStore?.stop() - }) - }) - } - const watchNodesValue = () => { scope.run(() => { let pauseModel: WatchPausableReturn @@ -221,77 +171,8 @@ export function useWatchProps( }) } - const watchApplyDefault = () => { - scope.run(() => { - watch( - () => props.applyDefault, - () => { - if (isDef(props.applyDefault)) { - store.applyDefault.value = props.applyDefault - } - }, - { - immediate: true, - }, - ) - }) - } - - const watchAutoConnect = () => { - scope.run(() => { - const autoConnector = async (params: Connection) => { - let connection: boolean | Connection = params - - if (typeof props.autoConnect === 'function') { - connection = await props.autoConnect(params) - } - - if (connection !== false) { - store.addEdges([connection]) - } - } - - watch( - () => props.autoConnect, - () => { - if (isDef(props.autoConnect)) { - store.autoConnect.value = props.autoConnect - } - }, - { immediate: true }, - ) - - watch( - store.autoConnect, - (autoConnectEnabled, _, onCleanup) => { - if (autoConnectEnabled) { - store.onConnect(autoConnector) - } else { - store.hooks.value.connect.off(autoConnector) - } - - onCleanup(() => { - store.hooks.value.connect.off(autoConnector) - }) - }, - { immediate: true }, - ) - }) - } - const watchRest = () => { - const skip: (keyof typeof props)[] = [ - 'id', - 'modelValue', - 'translateExtent', - 'nodeExtent', - 'edges', - 'nodes', - 'maxZoom', - 'minZoom', - 'applyDefault', - 'autoConnect', - ] + const skip: (keyof typeof props)[] = ['id', 'translateExtent', 'nodeExtent', 'edges', 'nodes', 'maxZoom', 'minZoom'] for (const key of Object.keys(props)) { const propKey = key as keyof typeof props @@ -318,15 +199,12 @@ export function useWatchProps( } const runAll = () => { - watchModelValue() watchNodesValue() watchEdgesValue() watchMinZoom() watchMaxZoom() watchTranslateExtent() watchNodeExtent() - watchApplyDefault() - watchAutoConnect() watchRest() } diff --git a/packages/core/src/container/EdgeRenderer/EdgeRenderer.vue b/packages/core/src/container/EdgeRenderer/EdgeRenderer.vue index ff7e23b45..cab4a38e0 100644 --- a/packages/core/src/container/EdgeRenderer/EdgeRenderer.vue +++ b/packages/core/src/container/EdgeRenderer/EdgeRenderer.vue @@ -5,7 +5,7 @@ import { useVueFlow } from '../../composables' import { getEdgeZIndex } from '../../utils' import MarkerDefinitions from './MarkerDefinitions.vue' -const { findNode, getEdges, elevateEdgesOnSelect } = useVueFlow() +const { getNode, getEdges, elevateEdgesOnSelect } = useVueFlow()