Skip to content

Commit c4356a7

Browse files
authored
fix: call setPodPosition on drag stop (#137)
1 parent f7e3c9e commit c4356a7

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

ui/src/components/Canvas.tsx

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,7 @@ export function Canvas() {
897897
nodes.map((n) => n.id)
898898
);
899899

900-
if (!scope || scope.id === commonParent) return;
901-
902-
// check if this position is inside parent scope
903-
904900
// FIXME: a better way to do this: check if the commonParent is the ancestor of the scope
905-
906901
if (commonParent !== undefined && commonParent !== "ROOT") {
907902
const currentParent = nodesMap.get(commonParent);
908903
if (currentParent) {
@@ -925,27 +920,36 @@ export function Canvas() {
925920
nodes.forEach((node) => {
926921
let absX = node.position.x;
927922
let absY = node.position.y;
928-
console.log("dropped into scope:", scope);
929-
930-
// compute the actual position
931-
let [dx, dy] = getAbsPos({ node: scope, nodesMap });
932-
absX = node.positionAbsolute!.x - dx;
933-
absY = node.positionAbsolute!.y - dy;
934-
935-
// auto-align the node to, keep it bound in the scope
936-
// FIXME: it assumes the scope must be larger than the node
937923

938-
absX = Math.max(absX, 0);
939-
absX = Math.min(absX, scope.width! - node.width!);
940-
absY = Math.max(absY, 0);
941-
absY = Math.min(absY, scope.height! - node.height!);
942-
943-
// FIXME: to enable collaborative editing, consider how to sync dropping scope immediately. consider useEffect in each node when data.parent or parent changes.
944-
setPodParent({
924+
if (scope) {
925+
console.log("dropped into scope:", scope);
926+
// compute the actual position
927+
let [dx, dy] = getAbsPos({ node: scope, nodesMap });
928+
absX = node.positionAbsolute!.x - dx;
929+
absY = node.positionAbsolute!.y - dy;
930+
// auto-align the node to, keep it bound in the scope
931+
// FIXME: it assumes the scope must be larger than the node
932+
933+
absX = Math.max(absX, 0);
934+
absX = Math.min(absX, scope.width! - node.width!);
935+
absY = Math.max(absY, 0);
936+
absY = Math.min(absY, scope.height! - node.height!);
937+
}
938+
setPodPosition({
945939
id: node.id,
946-
parent: scope.id,
940+
x: absX,
941+
y: absY,
947942
});
948943

944+
// check if this position is inside parent scope
945+
if (scope && scope.id !== commonParent) {
946+
// FIXME: to enable collaborative editing, consider how to sync dropping scope immediately. consider useEffect in each node when data.parent or parent changes.
947+
setPodParent({
948+
id: node.id,
949+
parent: scope.id,
950+
});
951+
}
952+
949953
const currentNode = nodesMap.get(node.id);
950954
if (currentNode) {
951955
if (scope) {
@@ -960,14 +964,7 @@ export function Canvas() {
960964
});
961965
},
962966
// We need to monitor nodes, so that getScopeAt can have all the nodes.
963-
[
964-
reactFlowInstance,
965-
getScopeAt,
966-
setPodPosition,
967-
nodesMap,
968-
setNodes,
969-
setPodParent,
970-
]
967+
[reactFlowInstance, getScopeAt, setPodPosition, nodesMap, setPodParent]
971968
);
972969

973970
const onNodesDelete = useCallback(

0 commit comments

Comments
 (0)