Skip to content

Commit

Permalink
fix: copy lazy layer on plugin on reearth/core (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 committed Mar 6, 2023
1 parent 81d291a commit 046a3d5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/core/Crust/Plugins/api.ts
@@ -1,5 +1,14 @@
import type { Tag } from "@reearth/core/mantle/compat";
import type { Events, Layer, LayerSelectionReason, LayersRef, NaiveLayer } from "@reearth/core/Map";
import {
copyLazyLayer,
Events,
Layer,
LayerSelectionReason,
LayersRef,
NaiveLayer,
LazyLayer,
copyLazyLayers,
} from "@reearth/core/Map";
import { merge } from "@reearth/util/object";

import type { Block } from "../Infobox";
Expand Down Expand Up @@ -475,7 +484,7 @@ export function commonReearth({
return !!layers()?.isLayer;
},
get layers() {
return layers()?.layers() ?? [];
return copyLazyLayers(layers()?.layers()) ?? [];
},
get tags() {
return tags();
Expand All @@ -496,22 +505,29 @@ export function commonReearth({
return selectedFeature();
},
get findById() {
return layers()?.findById;
return (id: string) => copyLazyLayer(layers()?.findById(id));
},
get findByIds() {
return layers()?.findByIds;
return (...args: string[]) =>
copyLazyLayers(
layers()
?.findByIds(...args)
?.filter((l): l is LazyLayer => !!l),
);
},
get findByTags() {
return layers()?.findByTags;
return (...args: string[]) => copyLazyLayers(layers()?.findByTags(...args));
},
get findByTagLabels() {
return layers()?.findByTagLabels;
return (...args: string[]) => copyLazyLayers(layers()?.findByTagLabels(...args));
},
get find() {
return layers()?.find;
return (cb: (layer: LazyLayer, index: number, parents: LazyLayer[]) => boolean) =>
copyLazyLayer(layers()?.find(cb));
},
get findAll() {
return layers()?.findAll;
return (cb: (layer: LazyLayer, index: number, parents: LazyLayer[]) => boolean) =>
copyLazyLayers(layers()?.findAll(cb));
},
get override() {
return layers()?.override;
Expand Down
1 change: 1 addition & 0 deletions src/core/Map/Layers/index.tsx
Expand Up @@ -27,6 +27,7 @@ export type {
ClusterProperty,
Cluster,
} from "../ClusteredLayers";
export { copyLazyLayers, copyLazyLayer } from "./utils";

export type Props = Omit<ClusteredLayerProps, "atomMap" | "isHidden"> & {
hiddenLayers?: string[];
Expand Down
23 changes: 23 additions & 0 deletions src/core/Map/Layers/utils.ts
@@ -1,5 +1,8 @@
import { isArray, isObject } from "lodash-es";

import { LazyLayer } from "./hooks";
import { layerKeys, computedLayerKeys } from "./keys";

export const deepAssign = <O extends Record<string, any>>(obj: O, src: O) => {
return Object.fromEntries(
Object.entries({ ...src, ...obj })
Expand Down Expand Up @@ -35,3 +38,23 @@ export const deepAssign = <O extends Record<string, any>>(obj: O, src: O) => {
.filter((v): v is [string, any] => !!v),
) as O;
};

export const copyLazyLayers = (layers: LazyLayer[] | undefined) => {
return layers?.map(l => {
return copyLazyLayer(l);
});
};

export const copyLazyLayer = (l: LazyLayer | undefined) => {
return layerKeys.reduce((res, key) => {
if (key === "computed") {
res[key] = computedLayerKeys.reduce((computedRes, computedKey) => {
computedRes[computedKey] = l?.[key as keyof LazyLayer][computedKey];
return computedRes;
}, {} as Record<string, any>);
return res;
}
res[key] = l?.[key as keyof LazyLayer];
return res;
}, {} as Record<string, any>) as LazyLayer;
};
1 change: 1 addition & 0 deletions src/core/Map/index.tsx
Expand Up @@ -6,6 +6,7 @@ import type { Engine, EngineProps } from "./types";

export * from "./types";
export { useGet, type WrappedRef, type Undefinable, useOverriddenProperty } from "./utils";
export { copyLazyLayers, copyLazyLayer } from "./Layers";

export type {
NaiveLayer,
Expand Down

0 comments on commit 046a3d5

Please sign in to comment.