feat(store): add connection actions to store
This commit is contained in:
@@ -78,11 +78,14 @@ export default () => {
|
|||||||
connectOnClick,
|
connectOnClick,
|
||||||
nodesConnectable,
|
nodesConnectable,
|
||||||
connectionStartHandle,
|
connectionStartHandle,
|
||||||
connectionPosition,
|
|
||||||
connectionMode,
|
connectionMode,
|
||||||
emits,
|
emits,
|
||||||
|
startConnection,
|
||||||
|
updateConnection,
|
||||||
|
endConnection,
|
||||||
setState,
|
setState,
|
||||||
getNode,
|
getNode,
|
||||||
|
vueFlowRef,
|
||||||
} = $(useVueFlow())
|
} = $(useVueFlow())
|
||||||
|
|
||||||
let recentHoveredHandle: Element
|
let recentHoveredHandle: Element
|
||||||
@@ -97,8 +100,6 @@ export default () => {
|
|||||||
onEdgeUpdate?: (connection: Connection) => void,
|
onEdgeUpdate?: (connection: Connection) => void,
|
||||||
onEdgeUpdateEnd?: () => void,
|
onEdgeUpdateEnd?: () => void,
|
||||||
) => {
|
) => {
|
||||||
const flowNode = (event.target as Element).closest('.vue-flow')
|
|
||||||
|
|
||||||
const doc = getHostForElement(event.target as HTMLElement)
|
const doc = getHostForElement(event.target as HTMLElement)
|
||||||
if (!doc) return
|
if (!doc) return
|
||||||
|
|
||||||
@@ -116,13 +117,13 @@ export default () => {
|
|||||||
const elementBelowIsTarget = elementBelow?.classList.contains('target')
|
const elementBelowIsTarget = elementBelow?.classList.contains('target')
|
||||||
const elementBelowIsSource = elementBelow?.classList.contains('source')
|
const elementBelowIsSource = elementBelow?.classList.contains('source')
|
||||||
|
|
||||||
if (!flowNode || (!elementBelowIsTarget && !elementBelowIsSource && !elementEdgeUpdaterType)) return
|
if (!vueFlowRef || (!elementBelowIsTarget && !elementBelowIsSource && !elementEdgeUpdaterType)) return
|
||||||
|
|
||||||
const handleType = elementEdgeUpdaterType ?? (elementBelowIsTarget ? 'target' : 'source')
|
const handleType = elementEdgeUpdaterType ?? (elementBelowIsTarget ? 'target' : 'source')
|
||||||
|
|
||||||
const containerBounds = flowNode.getBoundingClientRect()
|
const containerBounds = vueFlowRef.getBoundingClientRect()
|
||||||
|
|
||||||
setState({
|
startConnection({
|
||||||
connectionPosition: {
|
connectionPosition: {
|
||||||
x: event.clientX - containerBounds.left,
|
x: event.clientX - containerBounds.left,
|
||||||
y: event.clientY - containerBounds.top,
|
y: event.clientY - containerBounds.top,
|
||||||
@@ -135,8 +136,10 @@ export default () => {
|
|||||||
emits.connectStart({ event, nodeId, handleId, handleType })
|
emits.connectStart({ event, nodeId, handleId, handleType })
|
||||||
|
|
||||||
function onMouseMove(event: MouseEvent) {
|
function onMouseMove(event: MouseEvent) {
|
||||||
connectionPosition.x = event.clientX - containerBounds.left
|
updateConnection({
|
||||||
connectionPosition.y = event.clientY - containerBounds.top
|
x: event.clientX - containerBounds.left,
|
||||||
|
y: event.clientY - containerBounds.top,
|
||||||
|
})
|
||||||
|
|
||||||
const { connection, elementBelow, isValid, isHoveringHandle } = checkElementBelowIsValid(
|
const { connection, elementBelow, isValid, isHoveringHandle } = checkElementBelowIsValid(
|
||||||
event,
|
event,
|
||||||
@@ -189,12 +192,7 @@ export default () => {
|
|||||||
|
|
||||||
resetRecentHandle(recentHoveredHandle)
|
resetRecentHandle(recentHoveredHandle)
|
||||||
|
|
||||||
setState({
|
endConnection()
|
||||||
connectionNodeId: null,
|
|
||||||
connectionHandleId: null,
|
|
||||||
connectionHandleType: null,
|
|
||||||
connectionPosition: { x: NaN, y: NaN },
|
|
||||||
})
|
|
||||||
|
|
||||||
doc.removeEventListener('mousemove', onMouseMove as EventListenerOrEventListenerObject)
|
doc.removeEventListener('mousemove', onMouseMove as EventListenerOrEventListenerObject)
|
||||||
doc.removeEventListener('mouseup', onMouseUp as EventListenerOrEventListenerObject)
|
doc.removeEventListener('mouseup', onMouseUp as EventListenerOrEventListenerObject)
|
||||||
|
|||||||
@@ -358,6 +358,24 @@ export default (state: State, getters: ComputedGetters): Actions => {
|
|||||||
|
|
||||||
const applyEdgeChanges: Actions['applyEdgeChanges'] = (changes) => applyChanges(changes, state.edges)
|
const applyEdgeChanges: Actions['applyEdgeChanges'] = (changes) => applyChanges(changes, state.edges)
|
||||||
|
|
||||||
|
const startConnection: Actions['startConnection'] = (params) => {
|
||||||
|
state.connectionPosition = params.connectionPosition
|
||||||
|
state.connectionNodeId = params.connectionNodeId
|
||||||
|
state.connectionHandleId = params.connectionHandleId
|
||||||
|
state.connectionHandleType = params.connectionHandleType
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateConnection: Actions['updateConnection'] = (position) => {
|
||||||
|
state.connectionPosition = position
|
||||||
|
}
|
||||||
|
|
||||||
|
const endConnection: Actions['endConnection'] = () => {
|
||||||
|
state.connectionPosition = { x: NaN, y: NaN }
|
||||||
|
state.connectionNodeId = null
|
||||||
|
state.connectionHandleId = null
|
||||||
|
state.connectionHandleType = null
|
||||||
|
}
|
||||||
|
|
||||||
const setState: Actions['setState'] = (options) => {
|
const setState: Actions['setState'] = (options) => {
|
||||||
const opts = options instanceof Function ? options(state) : options
|
const opts = options instanceof Function ? options(state) : options
|
||||||
const skip: (keyof typeof opts)[] = [
|
const skip: (keyof typeof opts)[] = [
|
||||||
@@ -435,6 +453,9 @@ export default (state: State, getters: ComputedGetters): Actions => {
|
|||||||
removeSelectedElements,
|
removeSelectedElements,
|
||||||
removeSelectedNodes,
|
removeSelectedNodes,
|
||||||
removeSelectedEdges,
|
removeSelectedEdges,
|
||||||
|
startConnection,
|
||||||
|
updateConnection,
|
||||||
|
endConnection,
|
||||||
setInteractive,
|
setInteractive,
|
||||||
setState,
|
setState,
|
||||||
fitView: async (params = { padding: 0.1 }) => {
|
fitView: async (params = { padding: 0.1 }) => {
|
||||||
|
|||||||
@@ -16,6 +16,13 @@ export interface UpdateNodeDimensionsParams {
|
|||||||
forceUpdate?: boolean
|
forceUpdate?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface StartConnectionParams {
|
||||||
|
connectionPosition: XYPosition
|
||||||
|
connectionNodeId: string
|
||||||
|
connectionHandleId: string | null
|
||||||
|
connectionHandleType: HandleType
|
||||||
|
}
|
||||||
|
|
||||||
export interface State extends Omit<FlowOptions, 'id' | 'modelValue'> {
|
export interface State extends Omit<FlowOptions, 'id' | 'modelValue'> {
|
||||||
/** Vue flow element ref */
|
/** Vue flow element ref */
|
||||||
vueFlowRef: HTMLDivElement | null
|
vueFlowRef: HTMLDivElement | null
|
||||||
@@ -60,6 +67,7 @@ export interface State extends Omit<FlowOptions, 'id' | 'modelValue'> {
|
|||||||
multiSelectionKeyCode: KeyFilter
|
multiSelectionKeyCode: KeyFilter
|
||||||
zoomActivationKeyCode: KeyFilter
|
zoomActivationKeyCode: KeyFilter
|
||||||
|
|
||||||
|
// todo: remove these and just use connection start handle
|
||||||
connectionNodeId: string | null
|
connectionNodeId: string | null
|
||||||
connectionHandleId: string | null
|
connectionHandleId: string | null
|
||||||
connectionHandleType: HandleType | null
|
connectionHandleType: HandleType | null
|
||||||
@@ -130,6 +138,12 @@ export type SetState = (
|
|||||||
export type UpdateNodePosition = (dragItems: NodeDragItem[], changed: boolean, dragging: boolean) => void
|
export type UpdateNodePosition = (dragItems: NodeDragItem[], changed: boolean, dragging: boolean) => void
|
||||||
export type UpdateNodeDimensions = (updates: UpdateNodeDimensionsParams[]) => void
|
export type UpdateNodeDimensions = (updates: UpdateNodeDimensionsParams[]) => void
|
||||||
export type UpdateNodeInternals = (nodeIds: string[]) => void
|
export type UpdateNodeInternals = (nodeIds: string[]) => void
|
||||||
|
export type FindNode = <Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(
|
||||||
|
id: string,
|
||||||
|
) => GraphNode<Data, CustomEvents> | undefined
|
||||||
|
export type FindEdge = <Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(
|
||||||
|
id: string,
|
||||||
|
) => GraphEdge<Data, CustomEvents> | undefined
|
||||||
|
|
||||||
export interface Actions extends ViewportFunctions {
|
export interface Actions extends ViewportFunctions {
|
||||||
/** parses elements (nodes + edges) and re-sets the state */
|
/** parses elements (nodes + edges) and re-sets the state */
|
||||||
@@ -147,13 +161,9 @@ export interface Actions extends ViewportFunctions {
|
|||||||
/** remove edges from state */
|
/** remove edges from state */
|
||||||
removeEdges: RemoveEdges
|
removeEdges: RemoveEdges
|
||||||
/** find a node by id */
|
/** find a node by id */
|
||||||
findNode: <Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(
|
findNode: FindNode
|
||||||
id: string,
|
|
||||||
) => GraphNode<Data, CustomEvents> | undefined
|
|
||||||
/** find an edge by id */
|
/** find an edge by id */
|
||||||
findEdge: <Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(
|
findEdge: FindEdge
|
||||||
id: string,
|
|
||||||
) => GraphEdge<Data, CustomEvents> | undefined
|
|
||||||
/** updates an edge */
|
/** updates an edge */
|
||||||
updateEdge: UpdateEdge
|
updateEdge: UpdateEdge
|
||||||
/** applies default edge change handler */
|
/** applies default edge change handler */
|
||||||
@@ -188,6 +198,12 @@ export interface Actions extends ViewportFunctions {
|
|||||||
toObject: () => FlowExportObject
|
toObject: () => FlowExportObject
|
||||||
/** force update node internal data, if handle bounds are incorrect, you might want to use this */
|
/** force update node internal data, if handle bounds are incorrect, you might want to use this */
|
||||||
updateNodeInternals: UpdateNodeInternals
|
updateNodeInternals: UpdateNodeInternals
|
||||||
|
/** start a connection */
|
||||||
|
startConnection: (params: StartConnectionParams) => void
|
||||||
|
/** update connection position */
|
||||||
|
updateConnection: (position: XYPosition) => void
|
||||||
|
/** end (or cancel) a connection */
|
||||||
|
endConnection: () => void
|
||||||
|
|
||||||
/** internal position updater, you probably don't want to use this */
|
/** internal position updater, you probably don't want to use this */
|
||||||
updateNodePositions: UpdateNodePosition
|
updateNodePositions: UpdateNodePosition
|
||||||
|
|||||||
Reference in New Issue
Block a user