Skip to content

Commit

Permalink
save and merge editor features in map config (#2071)
Browse files Browse the repository at this point in the history
Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com>
Co-authored-by: Shan He <heshan0131@gmail.com>
  • Loading branch information
igorDykhta and heshan0131 committed Jan 5, 2023
1 parent 217b89e commit 9416be4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
20 changes: 18 additions & 2 deletions src/reducers/src/vis-state-merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
ParsedConfig
} from '@kepler.gl/schemas';

import {ParsedLayer, SavedInteractionConfig, TooltipInfo} from '@kepler.gl/types';
import {ParsedLayer, SavedInteractionConfig, TooltipInfo, SavedEditor} from '@kepler.gl/types';
import {KeplerTable, Datasets, assignGpuChannels, resetFilterGpuMode} from '@kepler.gl/table';

/**
Expand Down Expand Up @@ -600,6 +600,21 @@ export function validateLayerWithData(
return newLayer;
}

export function mergeEditor<S extends VisState>(state: S, savedEditor: SavedEditor) {
if (!savedEditor) {
return state;
}
return {
...state,
editor: {
...state.editor,
features: [...state.editor.features, ...(savedEditor.features || [])],
// if savedEditor.visible is undefined keep state.editor.visible
visible: savedEditor.visible ?? state.editor.visible
}
};
}

export function isValidMerger(merger: Merger): boolean {
return isObject(merger) && typeof merger.merge === 'function' && typeof merger.prop === 'string';
}
Expand All @@ -610,5 +625,6 @@ export const VIS_STATE_MERGERS: VisStateMergers = [
{merge: mergeInteractions, prop: 'interactionConfig', toMergeProp: 'interactionToBeMerged'},
{merge: mergeLayerBlending, prop: 'layerBlending'},
{merge: mergeSplitMaps, prop: 'splitMaps', toMergeProp: 'splitMapsToBeMerged'},
{merge: mergeAnimationConfig, prop: 'animationConfig'}
{merge: mergeAnimationConfig, prop: 'animationConfig'},
{merge: mergeEditor, prop: 'editor'}
];
8 changes: 8 additions & 0 deletions src/schemas/src/vis-state-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,14 @@ export const propertiesV1 = {
speed: null
},
key: 'animationConfig'
}),
editor: new Schema({
version: VERSIONS.v1,
properties: {
features: null,
visible: null
},
key: 'editor'
})
};

Expand Down
15 changes: 14 additions & 1 deletion src/types/schemas.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import {RGBColor, Merge, RGBAColor} from './types';

import {Filter, TooltipInfo, InteractionConfig, AnimationConfig, SplitMap} from './reducers';
import {
Filter,
TooltipInfo,
InteractionConfig,
AnimationConfig,
SplitMap,
Feature
} from './reducers';

import {LayerTextLabel} from './layers';

Expand Down Expand Up @@ -74,13 +81,19 @@ export type SavedAnimationConfig = {
speed: AnimationConfig['speed'];
};

export type SavedEditor = {
features: Feature[];
visible: boolean;
};

export type SavedVisState = {
filters: SavedFilter[];
layers: SavedLayer[];
interactionConfig: SavedInteractionConfig;
layerBlending: string;
splitMaps: SplitMap[];
animationConfig: SavedAnimationConfig;
editor?: SavedEditor;
};

export type ParsedVisState = {
Expand Down
22 changes: 16 additions & 6 deletions test/node/schemas/vis-state-schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ import {
import {keplerGlReducerCore as keplerGlReducer} from '@kepler.gl/reducers';
import {VisStateActions} from '@kepler.gl/actions';

const expectedVisStateEntries = [
'filters',
'layers',
'interactionConfig',
'layerBlending',
'splitMaps',
'animationConfig',
'editor'
];

test('#visStateSchema -> v1 -> save layers', t => {
const initialState = cloneDeep(StateWFilesFiltersLayerColor);

Expand All @@ -48,8 +58,8 @@ test('#visStateSchema -> v1 -> save layers', t => {

t.deepEqual(
Object.keys(vsToSave),
['filters', 'layers', 'interactionConfig', 'layerBlending', 'splitMaps', 'animationConfig'],
'visState should have all 5 entries'
expectedVisStateEntries,
`visState should have all ${expectedVisStateEntries.length} entries`
);

const exptectedSavedLayers = [expectedSavedLayer0, expectedSavedLayer1, expectedSavedLayer2];
Expand All @@ -69,8 +79,8 @@ test('#visStateSchema -> v1 -> load layers', t => {

t.deepEqual(
Object.keys(vsLoaded),
['filters', 'layers', 'interactionConfig', 'layerBlending', 'splitMaps', 'animationConfig'],
'visState should have all 5 entries'
expectedVisStateEntries,
`visState should have all ${expectedVisStateEntries.length} entries`
);

const loadedLayers = vsLoaded.layers;
Expand Down Expand Up @@ -300,8 +310,8 @@ test('#visStateSchema -> v1 -> save animation', t => {

t.deepEqual(
Object.keys(vsToSave),
['filters', 'layers', 'interactionConfig', 'layerBlending', 'splitMaps', 'animationConfig'],
'visState should have all 5 entries'
expectedVisStateEntries,
`visState should have all ${expectedVisStateEntries.length} entries`
);

const expectedSavedLayers = [expectedSavedTripLayer];
Expand Down

0 comments on commit 9416be4

Please sign in to comment.