Skip to content

Commit

Permalink
chore(web): Basic layer features API (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 authored Sep 12, 2023
1 parent 3a35cbc commit ba4409c
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 56 deletions.
6 changes: 6 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ export function commonReearth({
moveRight,
moveOverTerrain,
flyToGround,
findFeatureById,
findFeaturesByIds,
}: {
engineName?: string;
events: Events<ReearthEventType>;
Expand Down Expand Up @@ -390,6 +392,8 @@ export function commonReearth({
moveRight: GlobalThis["reearth"]["camera"]["moveRight"];
moveOverTerrain: GlobalThis["reearth"]["camera"]["moveOverTerrain"];
flyToGround: GlobalThis["reearth"]["camera"]["flyToGround"];
findFeatureById: GlobalThis["reearth"]["layers"]["findFeatureById"];
findFeaturesByIds: GlobalThis["reearth"]["layers"]["findFeaturesByIds"];
}): CommonReearth {
return {
version: window.REEARTH_CONFIG?.version || "",
Expand Down Expand Up @@ -572,6 +576,8 @@ export function commonReearth({
get add() {
return addLayer;
},
findFeatureById,
findFeaturesByIds,
},
plugins: {
get instances() {
Expand Down
18 changes: 18 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ export default function ({
[engineRef],
);

const findFeatureById = useCallback(
(layerId: string, featureId: string) => {
return engineRef?.findFeatureById(layerId, featureId);
},
[engineRef],
);

const findFeaturesByIds = useCallback(
(layerId: string, featureIds: string[]) => {
return engineRef?.findFeaturesByIds(layerId, featureIds);
},
[engineRef],
);

const addLayer = useCallback(
(layer: NaiveLayer) => {
return layersRef?.add(layer)?.id;
Expand Down Expand Up @@ -333,6 +347,8 @@ export default function ({
moveRight,
moveOverTerrain,
flyToGround,
findFeatureById,
findFeaturesByIds,
}),
overrideSceneProperty,
pluginInstances,
Expand Down Expand Up @@ -388,6 +404,8 @@ export default function ({
pluginInstances,
clientStorage,
useExperimentalSandbox,
findFeatureById,
findFeaturesByIds,
],
);

Expand Down
3 changes: 3 additions & 0 deletions web/src/beta/lib/core/Crust/Plugins/plugin_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
OverriddenLayer,
Undefinable,
WrappedRef,
Feature,
} from "@reearth/beta/lib/core/Map";

import { Block } from "../Infobox";
Expand Down Expand Up @@ -82,6 +83,8 @@ export type Reearth = {
layerId: string | undefined,
reason?: LayerSelectionReason | undefined,
) => void;
findFeatureById?: (layerId: string, featureId: string) => Feature | undefined;
findFeaturesByIds?: (layerId: string, featureId: string[]) => Feature[] | undefined;
selectionReason?: LayerSelectionReason;
// For compat
overriddenInfobox?: LayerSelectionReason["defaultInfobox"];
Expand Down
15 changes: 14 additions & 1 deletion web/src/beta/lib/core/Map/Layer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import {
evalFeature,
type Data,
} from "../../mantle";
import type { Atom, DataRange, Layer, ComputedLayer, ComputedFeature, Feature } from "../types";
import type {
Atom,
DataRange,
Layer,
ComputedLayer,
ComputedFeature,
Feature,
LayerSimple,
} from "../types";

export type { Atom as Atom } from "../types";

Expand All @@ -35,6 +43,10 @@ export default function useHooks({
selectedFeatureId?: string;
}) {
const [computedLayer, set] = useAtom(useMemo(() => atom ?? createAtom(), [atom]));
const writeLayer = useCallback(
(value: Partial<Pick<LayerSimple, "properties">>) => set({ type: "writeLayer", value }),
[set],
);
const writeFeatures = useCallback(
(features: Feature[]) => set({ type: "writeFeatures", features }),
[set],
Expand Down Expand Up @@ -131,6 +143,7 @@ export default function useHooks({
return {
computedLayer,
handleFeatureRequest: requestFetch,
handleLayerFetch: writeLayer,
handleFeatureFetch: writeFeatures,
handleComputedFeatureFetch: writeComputedFeatures,
handleFeatureDelete: deleteFeatures,
Expand Down
4 changes: 4 additions & 0 deletions web/src/beta/lib/core/Map/Layer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
Layer,
DataType,
ComputedFeature,
LayerSimple,
} from "../../mantle";
import { SceneProperty } from "../types";

Expand All @@ -30,6 +31,7 @@ export type FeatureComponentProps = {
layer: ComputedLayer;
sceneProperty?: SceneProperty;
onFeatureRequest?: (range: DataRange) => void;
onLayerFetch?: (value: Partial<Pick<LayerSimple, "properties">>) => void;
onFeatureFetch?: (features: Feature[]) => void;
onComputedFeatureFetch?: (feature: Feature[], computed: ComputedFeature[]) => void;
onFeatureDelete?: (features: string[]) => void;
Expand Down Expand Up @@ -59,6 +61,7 @@ export default function LayerComponent({
const {
computedLayer,
handleFeatureDelete,
handleLayerFetch,
handleComputedFeatureDelete,
handleFeatureFetch,
handleComputedFeatureFetch,
Expand All @@ -78,6 +81,7 @@ export default function LayerComponent({
layer={computedLayer}
onFeatureDelete={handleFeatureDelete}
onComputedFeatureDelete={handleComputedFeatureDelete}
onLayerFetch={handleLayerFetch}
onFeatureFetch={handleFeatureFetch}
onComputedFeatureFetch={handleComputedFeatureFetch}
onFeatureRequest={handleFeatureRequest}
Expand Down
2 changes: 2 additions & 0 deletions web/src/beta/lib/core/Map/Layers/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ export default function useHooks({
// compat
if (key === "pluginId") return layer.compat?.extensionId ? "reearth" : undefined;
else if (key === "extensionId") return layer.compat?.extensionId;
// TODO: Support normal layer's properties
else if (key === "properties") return layer.type === "simple" ? layer.properties : undefined;
else if (key === "property") return layer.compat?.property;
else if (key === "propertyId") return layer.compat?.propertyId;
else if (key === "isVisible") return layer.visible;
Expand Down
2 changes: 2 additions & 0 deletions web/src/beta/lib/core/Map/Layers/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test("simple", () => {
},
onFeatureDelete: expect.any(Function),
onFeatureFetch: expect.any(Function),
onLayerFetch: expect.any(Function),
onComputedFeatureFetch: expect.any(Function),
onComputedFeatureDelete: expect.any(Function),
onFeatureRequest: expect.any(Function),
Expand All @@ -45,6 +46,7 @@ test("simple", () => {
},
onFeatureDelete: expect.any(Function),
onFeatureFetch: expect.any(Function),
onLayerFetch: expect.any(Function),
onComputedFeatureFetch: expect.any(Function),
onComputedFeatureDelete: expect.any(Function),
onFeatureRequest: expect.any(Function),
Expand Down
2 changes: 2 additions & 0 deletions web/src/beta/lib/core/Map/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const engineRefKeys: FunctionKeys<EngineRef> = {
inViewport: 1,
onTick: 1,
removeTickEventListener: 1,
findFeaturesByIds: 1,
findFeatureById: 1,
};

const layersRefKeys: FunctionKeys<LayersRef> = {
Expand Down
3 changes: 3 additions & 0 deletions web/src/beta/lib/core/Map/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
LatLng,
DataType,
SelectedFeatureInfo,
Feature,
} from "../../mantle";
import type {
FeatureComponentType,
Expand Down Expand Up @@ -96,6 +97,8 @@ export type EngineRef = {
onTick: TickEvent;
tickEventCallback?: RefObject<TickEventCallback[]>;
removeTickEventListener: TickEvent;
findFeatureById: (layerId: string, featureId: string) => Feature | undefined;
findFeaturesByIds: (layerId: string, featureId: string[]) => Feature[] | undefined;
};

export type EngineProps = {
Expand Down
Loading

0 comments on commit ba4409c

Please sign in to comment.