2022-01-19 18:54:49 +01:00
|
|
|
import { Block, Source, Step, Table, Target } from 'models'
|
2021-12-16 10:43:49 +01:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
Dispatch,
|
2022-01-15 17:30:20 +01:00
|
|
|
MutableRefObject,
|
2021-12-16 10:43:49 +01:00
|
|
|
ReactNode,
|
|
|
|
SetStateAction,
|
|
|
|
useContext,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
|
|
|
|
|
|
|
export const stubLength = 20
|
|
|
|
export const blockWidth = 300
|
|
|
|
export const blockAnchorsOffset = {
|
|
|
|
left: {
|
|
|
|
x: 0,
|
|
|
|
y: 20,
|
|
|
|
},
|
|
|
|
top: {
|
|
|
|
x: blockWidth / 2,
|
|
|
|
y: 0,
|
|
|
|
},
|
|
|
|
right: {
|
|
|
|
x: blockWidth,
|
|
|
|
y: 20,
|
|
|
|
},
|
|
|
|
}
|
2022-01-12 09:10:59 +01:00
|
|
|
|
2021-12-16 10:43:49 +01:00
|
|
|
export type Coordinates = { x: number; y: number }
|
|
|
|
|
|
|
|
type Position = Coordinates & { scale: number }
|
|
|
|
|
|
|
|
export type Anchor = {
|
|
|
|
coordinates: Coordinates
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Node = Omit<Block, 'steps'> & {
|
|
|
|
steps: (Step & {
|
|
|
|
sourceAnchorsPosition: { left: Coordinates; right: Coordinates }
|
|
|
|
})[]
|
|
|
|
}
|
|
|
|
|
|
|
|
const graphPositionDefaultValue = { x: 400, y: 100, scale: 1 }
|
|
|
|
|
2022-01-12 09:10:59 +01:00
|
|
|
export type ConnectingIds = {
|
2022-01-19 18:54:49 +01:00
|
|
|
source: Source
|
2022-01-06 09:40:56 +01:00
|
|
|
target?: Target
|
2022-01-12 09:10:59 +01:00
|
|
|
}
|
|
|
|
|
2022-01-15 17:30:20 +01:00
|
|
|
type StepId = string
|
|
|
|
type NodeId = string
|
|
|
|
export type Endpoint = {
|
|
|
|
id: StepId | NodeId
|
|
|
|
ref: MutableRefObject<HTMLDivElement | null>
|
|
|
|
}
|
|
|
|
|
2021-12-16 10:43:49 +01:00
|
|
|
const graphContext = createContext<{
|
|
|
|
graphPosition: Position
|
|
|
|
setGraphPosition: Dispatch<SetStateAction<Position>>
|
2022-01-12 09:10:59 +01:00
|
|
|
connectingIds: ConnectingIds | null
|
|
|
|
setConnectingIds: Dispatch<SetStateAction<ConnectingIds | null>>
|
2022-01-19 18:54:49 +01:00
|
|
|
previewingEdgeId?: string
|
|
|
|
setPreviewingEdgeId: Dispatch<SetStateAction<string | undefined>>
|
2022-01-15 17:30:20 +01:00
|
|
|
sourceEndpoints: Table<Endpoint>
|
|
|
|
addSourceEndpoint: (endpoint: Endpoint) => void
|
|
|
|
targetEndpoints: Table<Endpoint>
|
|
|
|
addTargetEndpoint: (endpoint: Endpoint) => void
|
2022-01-28 09:42:31 +01:00
|
|
|
openedStepId?: string
|
|
|
|
setOpenedStepId: Dispatch<SetStateAction<string | undefined>>
|
2021-12-22 14:59:07 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
//@ts-ignore
|
2021-12-16 10:43:49 +01:00
|
|
|
}>({
|
|
|
|
graphPosition: graphPositionDefaultValue,
|
|
|
|
connectingIds: null,
|
|
|
|
})
|
|
|
|
|
|
|
|
export const GraphProvider = ({ children }: { children: ReactNode }) => {
|
|
|
|
const [graphPosition, setGraphPosition] = useState(graphPositionDefaultValue)
|
2022-01-12 09:10:59 +01:00
|
|
|
const [connectingIds, setConnectingIds] = useState<ConnectingIds | null>(null)
|
2022-01-19 18:54:49 +01:00
|
|
|
const [previewingEdgeId, setPreviewingEdgeId] = useState<string>()
|
2022-01-15 17:30:20 +01:00
|
|
|
const [sourceEndpoints, setSourceEndpoints] = useState<Table<Endpoint>>({
|
|
|
|
byId: {},
|
|
|
|
allIds: [],
|
|
|
|
})
|
|
|
|
const [targetEndpoints, setTargetEndpoints] = useState<Table<Endpoint>>({
|
|
|
|
byId: {},
|
|
|
|
allIds: [],
|
|
|
|
})
|
2022-01-28 09:42:31 +01:00
|
|
|
const [openedStepId, setOpenedStepId] = useState<string>()
|
2022-01-15 17:30:20 +01:00
|
|
|
|
|
|
|
const addSourceEndpoint = (endpoint: Endpoint) => {
|
|
|
|
setSourceEndpoints((endpoints) => ({
|
|
|
|
byId: { ...endpoints.byId, [endpoint.id]: endpoint },
|
|
|
|
allIds: [...endpoints.allIds, endpoint.id],
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
const addTargetEndpoint = (endpoint: Endpoint) => {
|
|
|
|
setTargetEndpoints((endpoints) => ({
|
|
|
|
byId: { ...endpoints.byId, [endpoint.id]: endpoint },
|
|
|
|
allIds: [...endpoints.allIds, endpoint.id],
|
|
|
|
}))
|
|
|
|
}
|
2021-12-16 10:43:49 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<graphContext.Provider
|
|
|
|
value={{
|
|
|
|
graphPosition,
|
|
|
|
setGraphPosition,
|
|
|
|
connectingIds,
|
|
|
|
setConnectingIds,
|
2022-01-19 18:54:49 +01:00
|
|
|
previewingEdgeId,
|
|
|
|
setPreviewingEdgeId,
|
2022-01-15 17:30:20 +01:00
|
|
|
sourceEndpoints,
|
|
|
|
targetEndpoints,
|
|
|
|
addSourceEndpoint,
|
|
|
|
addTargetEndpoint,
|
2022-01-28 09:42:31 +01:00
|
|
|
openedStepId,
|
|
|
|
setOpenedStepId,
|
2021-12-16 10:43:49 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</graphContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useGraph = () => useContext(graphContext)
|