Skip to content

Commit

Permalink
fix: overridden layers api on reearth/core (#477)
Browse files Browse the repository at this point in the history
* fix: overridden layers api on reearth/core

* test: fix
  • Loading branch information
keiya01 committed Feb 21, 2023
1 parent 1058abd commit abaade1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
15 changes: 12 additions & 3 deletions src/core/Crust/Plugins/api.ts
Expand Up @@ -293,7 +293,6 @@ export function commonReearth({
selectedLayer,
selectedFeature,
layerSelectionReason,
layerOverriddenProperties,
selectLayer,
showLayer,
hideLayer,
Expand Down Expand Up @@ -334,7 +333,6 @@ export function commonReearth({
selectedLayer: () => GlobalThis["reearth"]["layers"]["selected"];
selectedFeature: () => GlobalThis["reearth"]["layers"]["selectedFeature"];
layerSelectionReason: () => GlobalThis["reearth"]["layers"]["selectionReason"];
layerOverriddenProperties?: () => GlobalThis["reearth"]["layers"]["overriddenProperties"];
selectLayer: LayersRef["select"];
layersInViewport: GlobalThis["reearth"]["layers"]["layersInViewport"];
showLayer: GlobalThis["reearth"]["layers"]["show"];
Expand Down Expand Up @@ -459,8 +457,16 @@ export function commonReearth({
get hide() {
return hideLayer;
},
// For compat
get overriddenProperties() {
return layerOverriddenProperties?.();
return layers()
?.overriddenLayers?.()
?.reduce((res, v) => {
return {
...res,
[v.id]: v.compat?.property,
};
}, {} as { [id: string]: any });
},
get overrideProperty() {
return overrideLayerProperty;
Expand Down Expand Up @@ -510,6 +516,9 @@ export function commonReearth({
get override() {
return layers()?.override;
},
get overridden() {
return layers()?.overriddenLayers?.();
},
get replace() {
return layers()?.replace;
},
Expand Down
6 changes: 0 additions & 6 deletions src/core/Crust/Plugins/hooks.ts
Expand Up @@ -268,10 +268,6 @@ export default function ({
[layersRef],
);

const layerOverriddenProperties = useCallback(() => {
return layersRef?.overriddenLayers();
}, [layersRef]);

const value = useMemo<Context>(
() => ({
reearth: commonReearth({
Expand All @@ -288,7 +284,6 @@ export default function ({
selectedLayer: getSelectedLayer,
selectedFeature: getSelectedFeature,
layerSelectionReason: getLayerSelectionReason,
layerOverriddenProperties,
showLayer,
hideLayer,
addLayer,
Expand Down Expand Up @@ -367,7 +362,6 @@ export default function ({
showLayer,
zoomIn,
zoomOut,
layerOverriddenProperties,
],
);

Expand Down
3 changes: 2 additions & 1 deletion src/core/Crust/Plugins/plugin_types.ts
Expand Up @@ -71,8 +71,9 @@ export type Reearth = {
| "overriddenLayers"
> & {
readonly layersInViewport?: () => LazyLayer[] | undefined;
readonly overriddenProperties?: OverriddenLayer[];
readonly overriddenProperties?: { [id: string]: any };
readonly overrideProperty?: (properties: LayerSimple["properties"] | undefined) => void;
readonly overridden?: OverriddenLayer[];
readonly add?: (layer: NaiveLayer) => string | undefined;
readonly delete?: WrappedRef<LayersRef>["deleteLayer"];
readonly select?: (
Expand Down
1 change: 1 addition & 0 deletions src/core/Map/Layers/hooks.test.ts
Expand Up @@ -654,6 +654,7 @@ test("compat", () => {
default: {
location: { lat: 1, lng: 2 },
pointSize: 10,
pointColor: "yellow",
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/core/Map/Layers/hooks.ts
Expand Up @@ -327,7 +327,7 @@ export default function useHooks({
rawLayer.data.value.id = id;
}

const layer2 = { id, ...omit(rawLayer, "id", "type", "children", "compat") };
const layer2 = { id, ...omit(rawLayer, "id", "type", "children") };
setOverridenLayers(layers => {
const i = layers.findIndex(l => l.id === id);
if (i < 0) return [...layers, layer2];
Expand Down

0 comments on commit abaade1

Please sign in to comment.