mapbox / mapbox-gl-draw

Draw tools for mapbox-gl-js
https://www.mapbox.com/mapbox-gl-js/example/mapbox-gl-draw/
ISC License
952 stars 593 forks source link

Expose APIs for writing custom drawing modes #1056

Open ThisIsMissEm opened 3 years ago

ThisIsMissEm commented 3 years ago

mapbox-gl-js version: n/a mapbox-gl-draw version: latest

Currently when writing a custom drawing mode for @mapbox/mapbox-draw-gl a whole heap of internal modules are required to be imported, for instance: https://github.com/bemky/mapbox-gl-draw-freehand-mode/blob/master/src/index.js#L1-L3

import DrawPolygon from '@mapbox/mapbox-gl-draw/src/modes/draw_polygon';
import {geojsonTypes, cursors, types, updateActions, modes, events} from '@mapbox/mapbox-gl-draw/src/constants';
import doubleClickZoom from '@mapbox/mapbox-gl-draw/src/lib/double_click_zoom';

This is further a problem for people using typescript, where you need to declare each of the internal modules that your drawing mode uses:

declare module '@mapbox/mapbox-gl-draw';
declare module '@mapbox/mapbox-gl-draw/src/lib/theme';
declare module '@mapbox/mapbox-gl-draw/src/constants';
declare module '@mapbox/mapbox-gl-draw-static-mode';
declare module '@mapbox/mapbox-gl-draw/src/lib/create_supplementary_points';
declare module '@mapbox/mapbox-gl-draw/src/lib/common_selectors';
declare module '@mapbox/mapbox-gl-draw/src/modes/draw_polygon';
declare module '@mapbox/mapbox-gl-draw/src/lib/is_event_at_coordinates';
declare module '@mapbox/mapbox-gl-draw/src/modes/direct_select';
declare module '@mapbox/mapbox-gl-draw/src/lib/double_click_zoom';

(the above are what we required for the custom drawing modes that we have in the Yara AtFarm+ application)

I think the solution would be to have /index.js re-export all of those modules as a public API, rather than having them as internal modules.

ThisIsMissEm commented 3 years ago

Admittedly we can already access both @mapbox/mapbox-gl-draw/src/modes/draw_polygon and @mapbox/mapbox-gl-draw/src/modes/direct_select via:

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

const DrawPolygon = MapboxDraw.modes.draw_polygon;

If I'm understanding the code correctly.