Skip to content

Commit ed77f5f

Browse files
committed
clean up
1 parent 26d947c commit ed77f5f

1 file changed

Lines changed: 12 additions & 21 deletions

File tree

src/layers/ContextMenu.tsx

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,22 @@ export default function ContextMenu({ map, route, queryPoints }: ContextMenuProp
8787
const longTouchHandler = new LongTouchHandler(e => openContextMenu(e))
8888

8989
function onMapTargetChange() {
90-
const targetElement = map.getTargetElement()
91-
if (!targetElement) {
92-
console.warn('Map target element is null. Delaying event listeners setup.')
93-
return
94-
}
90+
// it is important to setup new listeners whenever the map target changes, like when we switch between the
91+
// small and large screen layout, see #203
9592

96-
targetElement.addEventListener('contextmenu', openContextMenu)
97-
targetElement.addEventListener('touchstart', e => longTouchHandler.onTouchStart(e))
98-
targetElement.addEventListener('touchmove', () => longTouchHandler.onTouchEnd())
99-
targetElement.addEventListener('touchend', () => longTouchHandler.onTouchEnd())
93+
// we cannot listen to right-click simply using map.on('contextmenu') and need to add the listener to
94+
// the map container instead
95+
// https://github.com/openlayers/openlayers/issues/12512#issuecomment-879403189
96+
map.getTargetElement().addEventListener('contextmenu', openContextMenu)
10097

101-
map.on('singleclick', handleClick)
98+
map.getTargetElement().addEventListener('touchstart', e => longTouchHandler.onTouchStart(e))
99+
map.getTargetElement().addEventListener('touchmove', () => longTouchHandler.onTouchEnd())
100+
map.getTargetElement().addEventListener('touchend', () => longTouchHandler.onTouchEnd())
102101
}
102+
map.on('singleclick', handleClick)
103+
map.on('change:target', onMapTargetChange)
103104

104-
const cleanupMapTarget = () => {
105+
return () => {
105106
const targetElement = map.getTargetElement()
106107
if (targetElement) {
107108
targetElement.removeEventListener('contextmenu', openContextMenu)
@@ -110,16 +111,6 @@ export default function ContextMenu({ map, route, queryPoints }: ContextMenuProp
110111
targetElement.removeEventListener('touchend', () => longTouchHandler.onTouchEnd())
111112
}
112113
map.un('singleclick', handleClick)
113-
}
114-
115-
// Set up initial listeners
116-
onMapTargetChange()
117-
118-
// Listen for target changes
119-
map.on('change:target', onMapTargetChange)
120-
121-
return () => {
122-
cleanupMapTarget()
123114
map.removeOverlay(overlay)
124115
map.un('change:target', onMapTargetChange)
125116
}

0 commit comments

Comments
 (0)