Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapbox-gl-draw no longer works in maplibre v3.0.0 #2601

Closed
erikvullings opened this issue May 26, 2023 · 26 comments
Closed

Mapbox-gl-draw no longer works in maplibre v3.0.0 #2601

erikvullings opened this issue May 26, 2023 · 26 comments

Comments

@erikvullings
Copy link

maplibre-gl-js version: 3.0.0

browser: Edge

Steps to Trigger Behavior

  1. Go to your own example
  2. Note that the drawing tools, which are shown in the example gallery, are no longer there (probably, they have become very small in the top-right).
  3. Even when visible, the draw controls no longer react on mouse click events.

Link to Demonstration

You can use your own jsfiddle to have a demo.

Expected Behavior

I can draw on the map.

Actual Behavior

Nothing happens.

@neodescis
Copy link
Collaborator

neodescis commented May 26, 2023

I believe this is because MapLibre no longer has the Mapbox CSS classes applied. Mapbox Draw is creating a control group with the Mapbox classes, not the MapLibre classes. Also, Mapbox Draw depends on the Mapbox classes on the map itself being there, and not just for CSS. There are checks in the Mapbox Draw code for them.

Do we need to fork Mapbox Draw at this point? I will be unable to update our project to MapLibre 3.x without draw support.

@neodescis
Copy link
Collaborator

I've been able to work around the issue by adding the Mapbox classes back. Here's what I did.

First, add the Mapbox CSS classes to the MapLibre map:

// MapboxDraw requires the canvas's class order to have the class 
// "mapboxgl-canvas" first in the list for the key bindings to work
map.getCanvas().className = 'mapboxgl-canvas maplibregl-canvas';
map.getContainer().classList.add('mapboxgl-map');
const canvasContainer = map.getCanvasContainer();
canvasContainer.classList.add('mapboxgl-canvas-container');
if (canvasContainer.classList.contains('maplibregl-interactive')) {
  canvasContainer.classList.add('mapboxgl-interactive');
}

Also, when the draw controls are created, the MapLibre classes need to be added to what MapboxDraw creates:

const originalOnAdd = draw.onAdd.bind(draw);
draw.onAdd = (map) => {
  const controlContainer = originalOnAdd(map);
  controlContainer.classList.add('maplibregl-ctrl', 'maplibregl-ctrl-group');
  return controlContainer;
};

@HarelM
Copy link
Collaborator

HarelM commented May 26, 2023

I'll close this issue as this is not a msplibre-gl bug.
You can use the css from before version 3 as it has both css classes, it might also have some stuff missing.

@HarelM HarelM closed this as completed May 26, 2023
@neodescis
Copy link
Collaborator

So... by closing this, are you saying that MapLibre no longer wants to support MapboxDraw at all? That seems strange to me, as does leaving a broken demo/example.

@HarelM
Copy link
Collaborator

HarelM commented May 26, 2023

You have it backwards 😀
I can't force mapbox draw to support maplibre 3.0.
I have no control over their codebase.
If someone would like to contribute a fix there it would be great, but I can't track it here, it's not the right place...

@neodescis
Copy link
Collaborator

neodescis commented May 26, 2023

Yeah sorry, I guess I didn't mean to imply that MapLibre should support MapboxDraw the project, but it does seem like the functionality would be important to the MapLibre community. That's why I was asking if we should fork MapboxDraw. It seems unlikely that the current project would want to support MapLibre, but I suppose it doesn't hurt to ask.

@HarelM
Copy link
Collaborator

HarelM commented May 26, 2023

I think that historically mapbox draw did make an effort to support maplibre.
There were talks about forking, but I don't know where it landed.
I agree that mapbox draw is an important capability for the community.

@joekrill
Copy link

You have it backwards 😀
I can't force mapbox draw to support maplibre 3.0.
I have no control over their codebase.

This problem was actually triggered by a change that maplibre made, though - not a mapbox-gl-draw change. The code in question on the mapbox-gl-draw side of things has been that way for 4+ years -- before maplibre even existed. I suppose it could be argued that they shouldn't have hard-coded the class names in their codebase in the first place, but at the time there was no reason to not do that.

So ultimately it was really #1575 that "broke" this, I believe.

I can totally understand this project not wanting to include the mapbox-gl-js classes. But it may be worth reconsidering that decision to maintain as much compatibility as possible and avoid alienating users who might otherwise switch.

If someone would like to contribute a fix there it would be great, but I can't track it here, it's not the right place...

I will try to get a PR up for mapbox-gl-draw and see how it goes over there. But there's likely other plugins that are going to break in similar ways.

In the meantime another option that may work for some folks is adding this code somewhere:

import MapboxDraw from "@mapbox/mapbox-gl-draw";

// @ts-ignore
MapboxDraw.constants.classes.CONTROL_BASE = "maplibregl-ctrl";
// @ts-ignore
MapboxDraw.constants.classes.CONTROL_PREFIX = "maplibregl-ctrl-";
// @ts-ignore
MapboxDraw.constants.classes.CONTROL_GROUP = "maplibregl-ctrl-group";

This worked for me for most functionality, but there's still some CSS rules that rely on the .mapboxgl-map and other mapbox-specific class names.

@neodescis
Copy link
Collaborator

I agree with @HarelM that it would be great for plugins to be updated to work with both, but I'm not sure how feasible that is. MapboxDraw still hasn't published my pull request for a compatibility fix with MapLibre 2.x that got merged three months ago...

@erikvullings
Copy link
Author

@joekrill @neodescis Thanks for your feedback! Since drawing is quite an essential feature for a map library IMHO,, would it make sense to you to merge the MapboxDraw functionality into MapLibre, so this kind of functionality remains under control? Preferably allowing for tree shaking, so it would only be included in the final bundle when required.

@HarelM
Copy link
Collaborator

HarelM commented Jun 22, 2023

Tree shaking is a painful point.
And unfortunately, I don't agree that this component is essential for a map library as it is very opinionated and specific...
If the library is not well maintained and someone would like to maintain it under maplibre organization it would be great, much like other plugins and wrappers.

@neodescis
Copy link
Collaborator

Drawing is essential to many map use cases, but I agree with keeping it separate. I think it would make a lot of sense to fork it though; the plugin would be much improved by moving to TypeScript and a class-based structure, among other things.

@HarelM
Copy link
Collaborator

HarelM commented Jun 27, 2023

@stepankuzmin is there a chance to release a new version of mapbox-gl-draw? I see that the linked PR was merged and should help here, wouldn't it?
There is a new issue reported once a week about mapbox-gl-draw not working with maplibre 3.0 roughly.
I think that in the linked issues here there are good people that might be able to help maintain mapbox-gl-draw.
I really think that forking should be the last resort and I'm not sure we are there yet...

@stepankuzmin
Copy link
Contributor

Hey everyone! Yes, I planned to cut a GL Draw release, but I was caught up with some stuff. I'll find some time to make a release.

@stepankuzmin
Copy link
Contributor

@HarelM Published @mapbox/mapbox-gl-draw v1.4.2 https://github.com/mapbox/mapbox-gl-draw/releases/tag/v1.4.2

@HarelM
Copy link
Collaborator

HarelM commented Jun 28, 2023

THANKS!! I'll update the docs within a few weeks.

@neodescis
Copy link
Collaborator

Yes, thanks a bunch @stepankuzmin! @HarelM, keep in mind my fix was to get MapboxDraw working with MapLibre 2.x. Much of what I posted above is still needed for 3.x. I'm more than willing to work on MapboxDraw to get it working with 3.x, but probably we'll need to discuss an approach. I can file an issue in that repo and mention this one to get the ball rolling.

@tianzaishuo
Copy link

tianzaishuo commented Aug 1, 2023

I have made the following modifications and can run normally. @HarelM
a

@HarelM
Copy link
Collaborator

HarelM commented Aug 1, 2023

@bk201lucifer can you open a PR to update the example here in this repo with these changes?

@nitrag
Copy link

nitrag commented Aug 1, 2023

I can confirm that changing the constants fixes the issue.

MapboxDraw.constants.classes.CONTROL_BASE = "maplibregl-ctrl";
MapboxDraw.constants.classes.CONTROL_PREFIX = "maplibregl-ctrl-";
MapboxDraw.constants.classes.CONTROL_GROUP = "maplibregl-ctrl-group";

@neodescis
Copy link
Collaborator

Well, it fixes most of the problems, but not all. Specifically, keyboard bindings still won't work. @HarelM , I've been holding off on updating the example until we hear back from @stepankuzmin on my PR, but that was a few weeks ago.

@HarelM
Copy link
Collaborator

HarelM commented Aug 3, 2023

We can update the example, and later on update it again.
Better something than nothing I think...

dBitech added a commit to OpenRadioMap/maplibre-gl-js that referenced this issue Nov 20, 2023
HarelM pushed a commit that referenced this issue Nov 20, 2023
* Fix the example as per the discussion in #2601 issue

* Fix a character case issue

* Code Hygiene cleanup as per ci.

* Add bufix to changelog
@kevinjmo
Copy link

kevinjmo commented Dec 5, 2023

Regarding the resolution, is the fix for us to add these three lines in our project before the MapboxDraw instance?

MapboxDraw.constants.classes.CONTROL_BASE  = 'maplibregl-ctrl';
MapboxDraw.constants.classes.CONTROL_PREFIX = 'maplibregl-ctrl-';
MapboxDraw.constants.classes.CONTROL_GROUP = 'maplibregl-ctrl-group';

The changes in #3394 seem to only update the example HTML, and doesn’t add anything to the actual maplibre-gl-js code itself. How come this isn’t set directly in maplibre-gl-js?

cc @dBitech @HarelM

@kevinjmo
Copy link

kevinjmo commented Dec 5, 2023

Also, it seems like when drawing, the cursor is off. It should be using the crosshair cursor, but the default grab cursor is present.

Screen.Recording.2023-12-05.at.15.24.18.mov

@HarelM
Copy link
Collaborator

HarelM commented Dec 6, 2023

There shouldn't be any code changes in maplibre-gl-js as mapbox-gl-draw is a plugin and should facilitate for these issues.
Your best approach would be to open a PR in the mapbox-gl-draw repo to solve these issues.

@NasimAwabdy
Copy link

Also, it seems like when drawing, the cursor is off. It should be using the crosshair cursor, but the default grab cursor is present.

Screen.Recording.2023-12-05.at.15.24.18.mov

@kevinjmo
To solve this issue, you need to rename the classes in the dist/mapbox-gl-draw.css file.
You can check out the changes in my forked repo -> https://github.com/NasimAwabdy/maplibre-gl-draw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants