Skip to content

Commit 3dab319

Browse files
committed
feat(graph): enhance event handling for mousedown to allow default prevention and improve drag functionality
1 parent 852df6e commit 3dab319

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/graph.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { CursorLayer, CursorLayerCursorTypes } from "./components/canvas/layers/
99
import { GraphLayer } from "./components/canvas/layers/graphLayer/GraphLayer";
1010
import { SelectionLayer } from "./components/canvas/layers/selectionLayer/SelectionLayer";
1111
import { TGraphColors, TGraphConstants, initGraphColors, initGraphConstants } from "./graphConfig";
12-
import { GraphEvent, GraphEventParams, GraphEventsDefinitions } from "./graphEvents";
12+
import { GraphEvent, GraphEventParams, GraphEventsDefinitions, isGraphEvent } from "./graphEvents";
1313
import { scheduler } from "./lib/Scheduler";
1414
import { HitTest } from "./services/HitTest";
1515
import { Layer, LayerPublicProps } from "./services/Layer";
@@ -335,6 +335,25 @@ export class Graph {
335335
cancelable: true,
336336
});
337337
this.eventEmitter.dispatchEvent(event);
338+
339+
/*
340+
* !!!IMPORTANT!!!
341+
* Users or layers must be able to prevent the default drag action.
342+
* Simply subscribing to the "mousedown" event does not allow handling the event at the very end of the chain,
343+
* so manual handling of the event is required here.
344+
*
345+
* In example - NewBlockLayer prevent default drag action to allow duplicate blocks with Alt key.
346+
*
347+
* @example
348+
* ```typescript
349+
* graph.on("mousedown", (event) => {
350+
* event.preventDefault(); // Prevent drag
351+
* });
352+
* ```
353+
* */
354+
if (eventName === "mousedown" && isGraphEvent(event) && !event.isDefaultPrevented()) {
355+
this.dragService.handleMouseDown(event);
356+
}
338357
return event;
339358
}
340359

src/graphEvents.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export class GraphEvent<T = unknown> extends CustomEvent<T> {
7272
public stopGraphEventPropagation() {
7373
this.graphEventPropagationStopped = true;
7474
}
75+
76+
public isDefaultPrevented() {
77+
return this.graphEventDefaultPrevented || this.defaultPrevented;
78+
}
7579
}
7680

7781
export function isGraphEvent<T = unknown>(event: CustomEvent<T>): event is GraphEvent<T> {

src/services/drag/DragService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export class DragService {
5252
public readonly $state = signal<DragState>(this.createIdleState());
5353

5454
constructor(private graph: Graph) {
55-
this.unsubscribeMouseDown = graph.on("mousedown", this.handleMouseDown, {
56-
capture: true,
57-
});
55+
// this.unsubscribeMouseDown = graph.on("mousedown", this.handleMouseDown, {
56+
// capture: true,
57+
// });
5858
}
5959

6060
/**
@@ -98,7 +98,7 @@ export class DragService {
9898
/**
9999
* Handle mousedown on graph - determine if drag should start
100100
*/
101-
private handleMouseDown = (event: GraphMouseEvent): void => {
101+
public handleMouseDown = (event: GraphMouseEvent): void => {
102102
// Prevent initiating new drag while one is already in progress
103103
// Check actual drag state, not just emitter presence (emitter may exist but drag not started yet)
104104
if (this.currentDragEmitter && this.$state.value.isDragging) {

0 commit comments

Comments
 (0)