Skip to content

Commit fe1d801

Browse files
committed
feat(core): add updateNode & updateNodeData actions to store
1 parent c855dfe commit fe1d801

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

packages/core/src/store/actions.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,34 @@ export function useActions(
600600
return changedEdges
601601
}
602602

603+
// todo: maybe we should use a more immutable approach, this is a bit too much mutation and hard to maintain
604+
const updateNode: Actions['updateNode'] = (id, nodeUpdate, options = { replace: true }) => {
605+
const node = findNode(id)
606+
607+
if (!node) {
608+
return
609+
}
610+
611+
const nextNode = typeof nodeUpdate === 'function' ? nodeUpdate(node) : nodeUpdate
612+
613+
if (options.replace) {
614+
state.nodes.splice(state.nodes.indexOf(node), 1, nextNode as GraphNode)
615+
} else {
616+
Object.assign(node, nextNode)
617+
}
618+
}
619+
620+
const updateNodeData: Actions['updateNodeData'] = (id, dataUpdate, options = { replace: false }) => {
621+
updateNode(
622+
id,
623+
(node) => {
624+
const nextData = typeof dataUpdate === 'function' ? dataUpdate(node) : dataUpdate
625+
return options.replace ? { ...node, data: nextData } : { ...node, data: { ...node.data, ...nextData } }
626+
},
627+
options,
628+
)
629+
}
630+
603631
const startConnection: Actions['startConnection'] = (startHandle, position, event, isClick = false) => {
604632
if (isClick) {
605633
state.connectionClickStartHandle = startHandle
@@ -902,6 +930,8 @@ export function useActions(
902930
findNode,
903931
findEdge,
904932
updateEdge,
933+
updateNode,
934+
updateNodeData,
905935
applyEdgeChanges,
906936
applyNodeChanges,
907937
addSelectedElements,

packages/core/src/types/store.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ export type GetIntersectingNodes = (
204204
nodes?: GraphNode[],
205205
) => GraphNode[]
206206

207+
export type UpdateNode = <Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(
208+
id: string,
209+
nodeUpdate: Partial<Node<Data, CustomEvents>> | ((node: GraphNode<Data, CustomEvents>) => Partial<Node<Data, CustomEvents>>),
210+
options?: { replace: boolean },
211+
) => void
212+
213+
export type UpdateNodeData = <Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(
214+
id: string,
215+
dataUpdate: Partial<Data> | ((node: GraphNode<Data, CustomEvents>) => Partial<Data>),
216+
options?: { replace: boolean },
217+
) => void
218+
207219
export type IsNodeIntersecting = (node: (Partial<Node> & { id: Node['id'] }) | Rect, area: Rect, partially?: boolean) => boolean
208220

209221
export interface Actions extends ViewportFunctions {
@@ -227,6 +239,10 @@ export interface Actions extends ViewportFunctions {
227239
findEdge: FindEdge
228240
/** updates an edge */
229241
updateEdge: UpdateEdge
242+
/** updates a node */
243+
updateNode: UpdateNode
244+
/** updates the data of a node */
245+
updateNodeData: UpdateNodeData
230246
/** applies default edge change handler */
231247
applyEdgeChanges: (changes: EdgeChange[]) => GraphEdge[]
232248
/** applies default node change handler */

0 commit comments

Comments
 (0)