Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[Bug] Fix missing default map styles after loading custom map style from saved json (#490)
Shan He335987994294
diff --git a/package.json b/package.jsonindex 90088be54..e6043f9c5 100644--- a/package.json+++ b/package.json@@ -88,7 +88,6 @@"fuzzy": "^0.1.3","global": "^4.3.0","h3-js": "^3.1.0",- "immutable": "^4.0.0-rc.12","keymirror": "^0.1.1","lodash.clonedeep": "^4.0.1","lodash.curry": "^4.1.1",@@ -171,6 +170,7 @@"eslint-plugin-prettier": "^3.0.1","eslint-plugin-react": "~7.12.4","gl": "^4.2.2",+ "immutable": "^4.0.0-rc.12","jsdom": "^11.1.0","json-loader": "^0.5.4","nyc": "^11.4.1",diff --git a/src/reducers/map-style-updaters.js b/src/reducers/map-style-updaters.jsindex 2e9142959..0880396a1 100644--- a/src/reducers/map-style-updaters.js+++ b/src/reducers/map-style-updaters.js@@ -18,7 +18,6 @@// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN// THE SOFTWARE.-import Immutable from 'immutable';import Task, {withTask} from 'react-palm/tasks';import cloneDeep from 'lodash.clonedeep';@@ -129,7 +128,7 @@ export const INITIAL_MAP_STYLE = getDefaultState();* @param {Object} mapStyles - a dictionary of all map styles* @returns {Object} bottomMapStyle | topMapStyle | isRaster*/-function getMapStyles({+export function getMapStyles({styleType,visibleLayerGroups,topLayerGroups,@@ -145,7 +144,7 @@ function getMapStyles({const editable = Object.keys(visibleLayerGroups).length;const bottomMapStyle = !editable- ? Immutable.fromJS(mapStyle.style)+ ? mapStyle.style: editBottomMapStyle({id: styleType,mapStyle,@@ -339,7 +338,16 @@ export const receiveMapConfigUpdater = (state, {payload: {mapStyle}}) => {? getLoadMapStyleTasks(mapStyle.mapStyles, state.mapboxApiAccessToken): null;- const newState = mapConfigChangeUpdater(state, {payload: mapStyle});+ // merge default mapStyles+ const merged = mapStyle.mapStyles ? {+ ...mapStyle,+ mapStyles: {+ ...mapStyle.mapStyles,+ ...state.mapStyles+ }+ } : mapStyle;++ const newState = mapConfigChangeUpdater(state, {payload: merged});return loadMapStyleTasks ? withTask(newState, loadMapStyleTasks) : newState;};diff --git a/src/utils/map-style-utils/mapbox-gl-style-editor.js b/src/utils/map-style-utils/mapbox-gl-style-editor.jsindex 56e2f8d56..dd0e5c8ac 100644--- a/src/utils/map-style-utils/mapbox-gl-style-editor.js+++ b/src/utils/map-style-utils/mapbox-gl-style-editor.js@@ -18,8 +18,8 @@// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN// THE SOFTWARE.-import Immutable from 'immutable';import memoize from 'lodash.memoize';+import clondDeep from 'lodash.clonedeep';import {DEFAULT_LAYER_GROUPS,RESOLUTIONS,@@ -47,7 +47,7 @@ const resolver = ({id, mapStyle, visibleLayerGroups = {}}) =>** @param {object} mapStyle - preset map style* @param {object} visibleLayerGroups - visible layers of top map- * @returns {Immutable.Map} top map style+ * @returns {object} top map style*/export const editTopMapStyle = memoize(({id, mapStyle, visibleLayerGroups}) => {const visibleFilters = (mapStyle.layerGroups || [])@@ -60,10 +60,10 @@ export const editTopMapStyle = memoize(({id, mapStyle, visibleLayerGroups}) => {visibleFilters.some(match => match(layer)));- return Immutable.fromJS({+ return {...mapStyle.style,layers: filteredLayers- });+ };}, resolver);/**@@ -71,7 +71,7 @@ export const editTopMapStyle = memoize(({id, mapStyle, visibleLayerGroups}) => {** @param {object} mapStyle - preset map style* @param {object} visibleLayerGroups - visible layers of bottom map- * @returns {Immutable.Map} bottom map style+ * @returns {object} bottom map style*/export const editBottomMapStyle = memoize(({id, mapStyle, visibleLayerGroups}) => {@@ -86,10 +86,10 @@ export const editBottomMapStyle = memoize();// console.log(filteredLayers)- return Immutable.fromJS({+ return {...mapStyle.style,layers: filteredLayers- });+ };},resolver);@@ -130,7 +130,7 @@ export function scaleMapStyleByResolution(mapboxStyle, resolution) {const {scale, zoomOffset} = RESOLUTION_OPTIONS.find(r => r.id === resolution);- const copyStyle = mapboxStyle.toJS();+ const copyStyle = clondDeep(mapboxStyle);(copyStyle.layers || []).forEach(d => {// edit minzoom and maxzoomif (d.maxzoom) {@@ -158,7 +158,7 @@ export function scaleMapStyleByResolution(mapboxStyle, resolution) {}});- return Immutable.fromJS(copyStyle);+ return copyStyle;}return mapboxStyle;diff --git a/test/node/reducers/map-style-test.js b/test/node/reducers/map-style-test.jsindex f29334f69..951826f97 100644--- a/test/node/reducers/map-style-test.js+++ b/test/node/reducers/map-style-test.js@@ -22,9 +22,10 @@ import test from 'tape';import {drainTasksForTesting, succeedTaskWithValues} from 'react-palm/tasks';import reducer from 'reducers/map-style';-import {INITIAL_MAP_STYLE} from 'reducers/map-style-updaters';+import {INITIAL_MAP_STYLE, getMapStyles} from 'reducers/map-style-updaters';import {keplerGlInit, receiveMapConfig} from 'actions/actions';import SchemaManager from 'schemas';+import {DEFAULT_MAP_STYLES} from 'constants/default-settings';// helpersimport {StateWCustomMapStyle} from 'test/helpers/mock-state';@@ -83,6 +84,14 @@ test('#mapStyleReducer -> RECEIVE_MAP_CONFIG', t => {receiveMapConfig(stateLoaded));+ const defaultMapStyles = DEFAULT_MAP_STYLES.reduce(+ (accu, st) => ({+ ...accu,+ [st.id]: st+ }),+ {}+ );+const expectedStateWithConfig = {styleType: 'smoothie_the_cat',visibleLayerGroups: {label: true, road: true},@@ -95,7 +104,8 @@ test('#mapStyleReducer -> RECEIVE_MAP_CONFIG', t => {id: 'smoothie_the_cat',label: 'Smoothie the Cat',url: 'mapbox://styles/shanhe/smoothie.the.cat'- }+ },+ ...defaultMapStyles},mapboxApiAccessToken: 'smoothies_secret_token',inputStyle: {@@ -152,16 +162,51 @@ test('#mapStyleReducer -> RECEIVE_MAP_CONFIG', t => {id: 'smoothie_the_cat',label: 'Smoothie the Cat',url: 'mapbox://styles/shanhe/smoothie.the.cat',- style: {layers: [], name: 'smoothie_the_cat'},+ style: {layers: [], name: 'smoothie_the_cat'},layerGroups: []- }+ },+ ...defaultMapStyles+ };++ const expectedMapStyleState = {+ styleType: 'smoothie_the_cat',+ visibleLayerGroups: {},+ topLayerGroups: {},+ mapStyles: expectedMapStyles,+ mapboxApiAccessToken: 'smoothies_secret_token',+ inputStyle: {+ accessToken: null,+ error: false,+ isValid: false,+ label: null,+ style: null,+ url: null,+ custom: true+ },+ threeDBuildingColor: [+ 194.6103322548211,+ 191.81688250953655,+ 185.2988331038727+ ],+ initialState: {},+ bottomMapStyle: {layers: [], name: 'smoothie_the_cat'},+ topMapStyle: null,+ editable: 0};t.deepEqual(- resultState1.mapStyles,- expectedMapStyles,- 'should update state with loaded map styles'+ Object.keys(resultState1).sort(),+ Object.keys(expectedMapStyleState).sort(),+ 'mapStyle state should have same keys');+ Object.keys(resultState1).forEach(key => {+ t.deepEqual(+ resultState1[key],+ expectedMapStyleState[key],+ `should update state,${key} with loaded map styles`+ );+ });+t.end();});