Skip to content

Commit

Permalink
feat(events): check if event instanceof MouseEvent
Browse files Browse the repository at this point in the history
check if event is instance of MouseEvent in map_event.ts
same for event.ts

Fix 5464
  • Loading branch information
Theo-dev-3D committed Feb 5, 2025
1 parent e89b609 commit 015fb5f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/ui/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ export class MapMouseEvent extends Event implements MapLibreEvent<MouseEvent> {
_defaultPrevented: boolean;

constructor(type: string, map: Map, originalEvent: MouseEvent, data: any = {}) {
originalEvent = originalEvent instanceof MouseEvent ? originalEvent : new MouseEvent(type, originalEvent);
const point = DOM.mousePos(map.getCanvas(), originalEvent);
const lngLat = map.unproject(point);
super(type, extend({point, lngLat, originalEvent}, data));
Expand Down
16 changes: 16 additions & 0 deletions src/ui/handler/map_event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,20 @@ describe('map events', () => {
simulate.contextmenu(map.getCanvas(), {target, button: 2, clientX: 10, clientY: 10}); // triggered only after mouseup
expect(contextmenu).toHaveBeenCalledTimes(0);
});

test('MapMouseEvent constructor does not throw error with Event instance instead of MouseEvent as originalEvent param', () => {
const map = createMap();
const target = map.getCanvasContainer();

target.dispatchEvent(new Event('mousedown'));
target.dispatchEvent(new Event('mouseup'));
target.dispatchEvent(new Event('click'));
target.dispatchEvent(new Event('dblclick'));
target.dispatchEvent(new Event('mousemove'));
target.dispatchEvent(new Event('mouseover'));
target.dispatchEvent(new Event('mouseenter'));
target.dispatchEvent(new Event('mouseleave'));
target.dispatchEvent(new Event('mouseout'));
target.dispatchEvent(new Event('contextmenu'));
});
});
10 changes: 7 additions & 3 deletions src/ui/handler_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,13 @@ export class HandlerManager {
const eventTouches = (e as TouchEvent).touches;

const mapTouches = eventTouches ? this._getMapTouches(eventTouches) : undefined;
const points = mapTouches ?
DOM.touchPos(this._map.getCanvas(), mapTouches) :
DOM.mousePos(this._map.getCanvas(), ((e as MouseEvent)));

let points: Point | Point[] = new Point(0, 0);
if (mapTouches) {
points = DOM.touchPos(this._map.getCanvas(), mapTouches);
} else if (e instanceof MouseEvent) {
points = DOM.mousePos(this._map.getCanvas(), (e));
}

for (const {handlerName, handler, allowed} of this._handlers) {
if (!handler.isEnabled()) continue;
Expand Down

0 comments on commit 015fb5f

Please sign in to comment.