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

Add mapbox-gl draw mode #347

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
* Update tooltip behavior change ([#317](https://github.com/opensearch-project/dashboards-maps/pull/317))
* Update max supported layer count ([#332](https://github.com/opensearch-project/dashboards-maps/pull/332))
* BWC for document layer label textType ([#340](https://github.com/opensearch-project/dashboards-maps/pull/340))
* Add mapbox-gl draw mode ([#347](https://github.com/opensearch-project/dashboards-maps/pull/347))

### Bug Fixes
* Fix property value undefined check ([#276](https://github.com/opensearch-project/dashboards-maps/pull/276))
Expand Down
2 changes: 1 addition & 1 deletion common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ export interface DrawFilterProperties {
export const DRAW_FILTER_SHAPE_TITLE = 'DRAW SHAPE';
export const DRAW_FILTER_POLYGON_DEFAULT_LABEL = 'polygon';
export const DRAW_FILTER_POLYGON = 'Draw Polygon';
export const DRAW_FILTER_POLYGON_RELATIONS = ['intersects', 'disjoint', 'within'];
export const DRAW_FILTER_SPATIAL_RELATIONS = ['intersects', 'disjoint', 'within'];
8 changes: 8 additions & 0 deletions public/components/map_container/map_container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { LayerControlPanel } from '../layer_control_panel';
import './map_container.scss';
import { DrawFilterProperties, FILTER_DRAW_MODE, MAP_INITIAL_STATE } from '../../../common';
import { MapLayerSpecification } from '../../model/mapLayerType';
import { DrawFilterShape } from '../toolbar/spatial_filter/draw_filter_shape';
import {
Filter,
IndexPattern,
Expand Down Expand Up @@ -232,6 +233,13 @@ export const MapContainer = ({
{mounted && Boolean(maplibreRef.current) && (
<DrawTooltip map={maplibreRef.current!} mode={filterProperties.mode} />
)}
{mounted && maplibreRef.current && tooltipState === TOOLTIP_STATE.FILTER_DRAW_SHAPE && (
<DrawFilterShape
map={maplibreRef.current}
filterProperties={filterProperties}
updateFilterProperties={setFilterProperties}
/>
)}
<div className="SpatialFilterToolbar-container">
{mounted && (
<SpatialFilterToolbar
Expand Down
57 changes: 57 additions & 0 deletions public/components/toolbar/spatial_filter/draw_filter_shape.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React, { Fragment, useEffect, useRef } from 'react';
import { IControl, Map as Maplibre } from 'maplibre-gl';
import MapboxDraw from '@mapbox/mapbox-gl-draw';
import { Feature } from 'geojson';
import { DrawFilterProperties, FILTER_DRAW_MODE} from '../../../../common';
VijayanB marked this conversation as resolved.
Show resolved Hide resolved

interface DrawFilterShapeProps {
filterProperties: DrawFilterProperties;
map: Maplibre;
updateFilterProperties: (properties: DrawFilterProperties) => void;
}
export const DrawFilterShape = ({
filterProperties,
map,
updateFilterProperties,
}: DrawFilterShapeProps) => {
const onDraw = (event: { features: Feature[] }) => {
updateFilterProperties({
mode: FILTER_DRAW_MODE.NONE,
});
};
const mapboxDrawRef = useRef<MapboxDraw>(
new MapboxDraw({
displayControlsDefault: false,
})
);

useEffect(() => {
if (map) {
map.addControl((mapboxDrawRef.current as unknown) as IControl);
VijayanB marked this conversation as resolved.
Show resolved Hide resolved
map.on('draw.create', onDraw);
}
return () => {
if (map) {
map.off('draw.create', onDraw);
// eslint-disable-next-line react-hooks/exhaustive-deps
map.removeControl((mapboxDrawRef.current as unknown) as IControl);
}
};
}, []);

useEffect(() => {
if (filterProperties.mode === FILTER_DRAW_MODE.POLYGON) {
mapboxDrawRef.current.changeMode('draw_polygon');
} else {
// default mode
mapboxDrawRef.current.changeMode('simple_select');
VijayanB marked this conversation as resolved.
Show resolved Hide resolved
}
}, [filterProperties.mode]);

return <Fragment />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
DrawFilterProperties,
DRAW_FILTER_POLYGON,
DRAW_FILTER_POLYGON_DEFAULT_LABEL,
DRAW_FILTER_POLYGON_RELATIONS,
DRAW_FILTER_SPATIAL_RELATIONS,
DRAW_FILTER_SHAPE_TITLE,
} from '../../../../common';
import { FILTER_DRAW_MODE } from '../../../../common';
Expand Down Expand Up @@ -52,7 +52,7 @@ export const FilterByPolygon = ({
<FilterInputPanel
drawLabel={DRAW_FILTER_POLYGON}
defaultFilterLabel={DRAW_FILTER_POLYGON_DEFAULT_LABEL}
relations={DRAW_FILTER_POLYGON_RELATIONS}
relations={DRAW_FILTER_SPATIAL_RELATIONS}
onSubmit={onSubmit}
mode={FILTER_DRAW_MODE.POLYGON}
/>
Expand Down