From a41eb5f46b785d129d45122961e86727f5216240 Mon Sep 17 00:00:00 2001 From: qj <785065948@qq.com> Date: Sun, 21 Apr 2024 19:59:27 +0800 Subject: [PATCH] Add hotSourceOptions and coldSourceOptions Add hotSourceOptions and coldSourceOptions to the options to pass to the drawing and drawn layers. This could fix issue [#858](https://github.com/mapbox/mapbox-gl-draw/issues/858) and [#1143](https://github.com/mapbox/mapbox-gl-draw/issues/1143). --- docs/API.md | 2 ++ src/options.js | 4 +++- src/setup.js | 2 ++ test/options.test.js | 24 ++++++++++++++++++------ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/docs/API.md b/docs/API.md index 79695e808..4cde1fd8d 100644 --- a/docs/API.md +++ b/docs/API.md @@ -36,6 +36,8 @@ All of the following options are optional. - `modes`, Object: over ride the default modes with your own. `MapboxDraw.modes` can be used to see the default values. More information on custom modes [can be found here](https://github.com/mapbox/mapbox-gl-draw/blob/main/docs/MODES.md). - `defaultMode`, String (default: `'simple_select'`): the mode (from `modes`) that user will first land in. - `userProperties`, boolean (default: `false`): properties of a feature will also be available for styling and prefixed with `user_`, e.g., `['==', 'user_custom_label', 'Example']` +- `hotSourceOptions`, Object (default {}): same as [GeoJSONSource](https://docs.mapbox.com/style-spec/reference/sources#geojson) in mapbox,can control hot features style. +- `coldSourceOptions`, Object (default {}): same as [GeoJSONSource](https://docs.mapbox.com/style-spec/reference/sources#geojson) in mapbox,can control drawn features style. ## Modes diff --git a/src/options.js b/src/options.js index 720aa351a..9a5a010bb 100644 --- a/src/options.js +++ b/src/options.js @@ -15,7 +15,9 @@ const defaultOptions = { styles, modes, controls: {}, - userProperties: false + userProperties: false, + coldSourceOptions: {}, + hotSourceOptions: {} }; const showControls = { diff --git a/src/setup.js b/src/setup.js index be977b8be..1bcbfcebd 100644 --- a/src/setup.js +++ b/src/setup.js @@ -85,6 +85,7 @@ export default function(ctx) { addLayers() { // drawn features style ctx.map.addSource(Constants.sources.COLD, { + ...ctx.options.coldSourceOptions, data: { type: Constants.geojsonTypes.FEATURE_COLLECTION, features: [] @@ -94,6 +95,7 @@ export default function(ctx) { // hot features style ctx.map.addSource(Constants.sources.HOT, { + ...ctx.options.hotSourceOptions, data: { type: Constants.geojsonTypes.FEATURE_COLLECTION, features: [] diff --git a/test/options.test.js b/test/options.test.js index 42e94fcaa..3fe213379 100644 --- a/test/options.test.js +++ b/test/options.test.js @@ -25,7 +25,9 @@ test('Options test', (t) => { trash: true, combine_features: true, uncombine_features: true - } + }, + coldSourceOptions: {}, + hotSourceOptions: {} }; t.deepEquals(defaultOptions, Draw.options); t.deepEquals(styleWithSourcesFixture, Draw.options.styles); @@ -52,7 +54,9 @@ test('Options test', (t) => { trash: true, combine_features: true, uncombine_features: true - } + }, + coldSourceOptions: {}, + hotSourceOptions: {} }; t.deepEquals(defaultOptions, Draw.options); @@ -79,7 +83,9 @@ test('Options test', (t) => { trash: false, combine_features: false, uncombine_features: false - } + }, + coldSourceOptions: {}, + hotSourceOptions: {} }; t.deepEquals(defaultOptions, Draw.options); t.end(); @@ -105,7 +111,9 @@ test('Options test', (t) => { trash: false, combine_features: false, uncombine_features: false - } + }, + coldSourceOptions: {}, + hotSourceOptions: {} }; t.deepEquals(defaultOptions, Draw.options); @@ -132,7 +140,9 @@ test('Options test', (t) => { trash: true, combine_features: true, uncombine_features: true - } + }, + coldSourceOptions: {}, + hotSourceOptions: {} }; t.deepEquals(defaultOptions, Draw.options); @@ -159,7 +169,9 @@ test('Options test', (t) => { trash: true, combine_features: true, uncombine_features: true - } + }, + coldSourceOptions: {}, + hotSourceOptions: {} }; t.deepEquals(defaultOptions, Draw.options); t.deepEquals(styleWithSourcesFixture, Draw.options.styles);