Skip to content

Commit 450475b

Browse files
authored
fix: Delegate pointer-events from the BlockGroupLayer to the Camera on interact with groups (#57)
1 parent 8b20838 commit 450475b

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

src/components/canvas/GraphComponent/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class GraphComponent<
3333
onDragStart,
3434
onDragUpdate,
3535
onDrop,
36+
isDraggable,
3637
}: {
3738
onDragStart?: (_event: MouseEvent) => void | boolean;
3839
onDragUpdate?: (
@@ -45,9 +46,13 @@ export class GraphComponent<
4546
_event: MouseEvent
4647
) => void;
4748
onDrop?: (_event: MouseEvent) => void;
49+
isDraggable?: (event: MouseEvent) => boolean;
4850
}) {
4951
let startDragCoords: [number, number];
5052
return this.addEventListener("mousedown", (event: MouseEvent) => {
53+
if (!isDraggable?.(event)) {
54+
return;
55+
}
5156
event.stopPropagation();
5257
dragListener(this.context.ownerDocument)
5358
.on(EVENTS.DRAG_START, (event: MouseEvent) => {

src/components/canvas/groups/BlockGroups.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Signal, computed } from "@preact/signals-core";
22

33
import { Graph } from "../../../graph";
4+
import { CoreComponent } from "../../../lib";
45
import { TComponentState } from "../../../lib/Component";
56
import { Layer, LayerContext, LayerProps } from "../../../services/Layer";
67
import { BlockState } from "../../../store/block/Block";
@@ -104,6 +105,14 @@ export class BlockGroups<P extends BlockGroupsProps = BlockGroupsProps> extends
104105
);
105106
}
106107

108+
public getParent(): CoreComponent | undefined {
109+
/*
110+
* Override parent to delegate click events to camera.
111+
* This allows camera movement when mouse button is held down.
112+
*/
113+
return this.props.graph.getGraphLayer().$.camera;
114+
}
115+
107116
public updateBlocks = (groupId: TGroupId, { diffX, diffY }: { diffX: number; diffY: number }) => {
108117
if ((this.props as BlockGroupsProps & { updateBlocksOnDrag?: boolean }).updateBlocksOnDrag) {
109118
const blocks = this.$groupsBlocksMap.value[groupId];

src/components/canvas/groups/Group.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,26 @@ export class Group<T extends TGroup = TGroup> extends GraphComponent<TGroupProps
101101
this.subscribeToGroup();
102102

103103
this.addEventListener("click", (event: MouseEvent) => {
104-
event.stopPropagation();
105104
this.groupState.setSelection(
106105
true,
107106
!isMetaKeyEvent(event) ? ESelectionStrategy.REPLACE : ESelectionStrategy.APPEND
108107
);
109108
});
110109

111110
this.onDrag({
111+
isDraggable: () => this.isDraggable(),
112112
onDragUpdate: ({ diffX, diffY }) => {
113-
if (this.isDraggable()) {
114-
const rect = {
115-
x: this.state.rect.x - diffX,
116-
y: this.state.rect.y - diffY,
117-
width: this.state.rect.width,
118-
height: this.state.rect.height,
119-
};
120-
this.setState({
121-
rect,
122-
});
123-
this.updateHitBox(rect);
124-
this.props.onDragUpdate(this.props.id, { diffX, diffY });
125-
}
113+
const rect = {
114+
x: this.state.rect.x - diffX,
115+
y: this.state.rect.y - diffY,
116+
width: this.state.rect.width,
117+
height: this.state.rect.height,
118+
};
119+
this.setState({
120+
rect,
121+
});
122+
this.updateHitBox(rect);
123+
this.props.onDragUpdate(this.props.id, { diffX, diffY });
126124
},
127125
});
128126
}

src/components/canvas/layers/graphLayer/GraphLayer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ export class GraphLayer extends Layer<TGraphLayerProps, TGraphLayerContext> {
332332

333333
public updateChildren() {
334334
const cameraProps: TCameraProps = {
335+
/* Blocks must be initialized before connections as connections need Block instances to access their geometry */
335336
children: [DrawOver.create(), Blocks.create(), DrawBelow.create(), BlockConnections.create()],
336337
root: this.root,
337338
};

src/graph.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ export class Graph {
121121
this.setupGraph(config);
122122
}
123123

124+
public getGraphLayer() {
125+
return this.graphLayer;
126+
}
127+
124128
public setColors(colors: RecursivePartial<TGraphColors>) {
125129
this.$graphColors.value = merge({}, initGraphColors, colors);
126130
this.emit("colors-changed", { colors: this.graphColors });

0 commit comments

Comments
 (0)