Skip to content

Commit

Permalink
fix: 3D Tiles infobox on reearth/core (#433)
Browse files Browse the repository at this point in the history
* fix: 3dtiles infobox on reearth/core

* fix: unnecessary render

* Update src/components/organisms/EarthEditor/core/CanvasArea/hooks.ts

Co-authored-by: rot1024 <[email protected]>

* Update src/core/Visualizer/hooks.ts

Co-authored-by: rot1024 <[email protected]>

---------

Co-authored-by: rot1024 <[email protected]>
  • Loading branch information
keiya01 and rot1024 authored Feb 7, 2023
1 parent e6e805c commit b4afd7e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
21 changes: 17 additions & 4 deletions src/components/organisms/EarthEditor/core/CanvasArea/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useMemo, useEffect, useCallback } from "react";

import { config } from "@reearth/config";
import type { Alignment, Location } from "@reearth/core/Crust";
import { convertLegacyLayer } from "@reearth/core/mantle";
import type { Cluster, Layer } from "@reearth/core/Map";
import { type ComputedLayer, convertLegacyLayer } from "@reearth/core/mantle";
import type { Cluster, Layer, LayerSelectionReason } from "@reearth/core/Map";
import {
useGetLayersQuery,
useGetEarthWidgetsQuery,
Expand Down Expand Up @@ -102,7 +102,14 @@ export default (isBuilt?: boolean) => {

// convert data
const selectedLayerId = useMemo(
() => (selected?.type === "layer" ? { layerId: selected.layerId } : undefined),
() =>
selected?.type === "layer"
? { layerId: selected.layerId, featureId: selected.featureId }
: undefined,
[selected],
);
const layerSelectionReason = useMemo(
() => (selected?.type === "layer" ? selected.layerSelectionReason : undefined),
[selected],
);

Expand Down Expand Up @@ -136,7 +143,12 @@ export default (isBuilt?: boolean) => {
);

const selectLayer = useCallback(
(id?: string) => select(id ? { layerId: id, type: "layer" } : undefined),
(
id?: string,
featureId?: string,
_layer?: () => Promise<ComputedLayer | undefined>,
layerSelectionReason?: LayerSelectionReason,
) => select(id ? { layerId: id, featureId, layerSelectionReason, type: "layer" } : undefined),
[select],
);

Expand Down Expand Up @@ -300,6 +312,7 @@ export default (isBuilt?: boolean) => {
selectedWidgetArea,
widgetAlignEditorActivated,
engineMeta,
layerSelectionReason,
selectLayer,
selectBlock,
onBlockChange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const CanvasArea: React.FC<Props> = ({ isBuilt, inEditor }) => {
selectedWidgetArea,
widgetAlignEditorActivated,
engineMeta,
layerSelectionReason,
selectLayer,
selectBlock,
onBlockChange,
Expand Down Expand Up @@ -80,6 +81,7 @@ const CanvasArea: React.FC<Props> = ({ isBuilt, inEditor }) => {
pluginBaseUrl={config()?.plugins}
widgetAlignSystemEditing={widgetAlignEditorActivated}
meta={engineMeta}
layerSelectionReason={layerSelectionReason}
onLayerSelect={selectLayer}
onCameraChange={onCameraChange}
onWidgetLayoutUpdate={onWidgetUpdate}
Expand Down
41 changes: 41 additions & 0 deletions src/core/Visualizer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useWindowSize } from "react-use";

import { type DropOptions, useDrop } from "@reearth/util/use-dnd";

import type { Block } from "../Crust";
import type { ComputedFeature, Feature } from "../mantle";
import type {
Ref as MapRef,
Expand All @@ -11,6 +12,7 @@ import type {
ComputedLayer,
SceneProperty,
LayerEditEvent,
OverriddenInfobox,
} from "../Map";
import { useOverriddenProperty } from "../Map";

Expand Down Expand Up @@ -106,6 +108,22 @@ export default function useHooks({
[],
);

// Infobox
const overriddenInfobox = selectedLayer.reason?.overriddenInfobox;
const infobox = useMemo(
() =>
selectedLayer
? {
title: overriddenInfobox?.title || selectedLayer.layer?.layer?.title,
isEditable: !overriddenInfobox && !!selectedLayer.layer?.layer?.infobox,
visible: !!selectedLayer.layer?.layer?.infobox,
property: selectedLayer.layer?.layer.infobox?.property?.default,
blocks: overridenInfoboxBlocks(overriddenInfobox),
}
: undefined,
[selectedLayer, overriddenInfobox],
);

// scene
const [overriddenSceneProperty, overrideSceneProperty] = useOverriddenProperty(sceneProperty);

Expand Down Expand Up @@ -148,6 +166,7 @@ export default function useHooks({
isMobile,
overriddenSceneProperty,
isDroppable,
infobox,
handleLayerSelect,
handleBlockSelect: selectBlock,
handleCameraChange: changeCamera,
Expand Down Expand Up @@ -179,3 +198,25 @@ function useValue<T>(

return [state, handleOnChange];
}

function overridenInfoboxBlocks(
overriddenInfobox: OverriddenInfobox | undefined,
): Block[] | undefined {
return overriddenInfobox && Array.isArray(overriddenInfobox?.content)
? [
{
id: "content",
pluginId: "reearth",
extensionId: "dlblock",
property: {
items: overriddenInfobox.content.map((c, i) => ({
id: i,
item_title: c.key,
item_datastr: String(c.value),
item_datatype: "string",
})),
},
},
]
: undefined;
}
14 changes: 9 additions & 5 deletions src/core/Visualizer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type Props = {
layerId?: string;
featureId?: string;
};
layerSelectionReason?: LayerSelectionReason;
selectedWidgetArea?: WidgetAreaType;
hiddenLayers?: string[];
zoomedLayerId?: string;
Expand Down Expand Up @@ -132,6 +133,7 @@ export default function Visualizer({
pluginBaseUrl,
pluginProperty,
zoomedLayerId,
layerSelectionReason,
onLayerDrag,
onLayerDrop,
onLayerSelect,
Expand Down Expand Up @@ -160,6 +162,7 @@ export default function Visualizer({
isMobile,
overriddenSceneProperty,
isDroppable,
infobox,
handleLayerSelect,
handleBlockSelect,
handleCameraChange,
Expand Down Expand Up @@ -187,21 +190,21 @@ export default function Visualizer({
tags={tags}
viewport={viewport}
isBuilt={isBuilt}
isEditable={isEditable}
isEditable={isEditable && infobox?.isEditable}
inEditor={inEditor}
sceneProperty={overriddenSceneProperty}
overrideSceneProperty={overrideSceneProperty}
blocks={selectedLayer?.layer?.layer.infobox?.blocks}
blocks={infobox?.blocks}
camera={camera}
isMobile={isMobile}
selectedWidgetArea={selectedWidgetArea}
selectedComputedLayer={selectedLayer?.layer}
selectedFeature={selectedFeature}
selectedComputedFeature={selectedComputedFeature}
selectedReason={selectedLayer.reason}
infoboxProperty={selectedLayer?.layer?.layer.infobox?.property?.default}
infoboxTitle={selectedLayer?.layer?.layer.title}
infoboxVisible={!!selectedLayer?.layer?.layer.infobox}
infoboxProperty={infobox?.property}
infoboxTitle={infobox?.title}
infoboxVisible={!!infobox?.visible}
selectedBlockId={selectedBlock}
selectedLayerId={{ layerId: selectedLayer.layerId, featureId: selectedLayer.featureId }}
widgetAlignSystem={widgetAlignSystem}
Expand Down Expand Up @@ -239,6 +242,7 @@ export default function Visualizer({
// overrides={overrides} // not used for now
property={overriddenSceneProperty}
selectedLayerId={selectedLayerId}
layerSelectionReason={layerSelectionReason}
small={small}
ready={ready}
onCameraChange={handleCameraChange}
Expand Down
8 changes: 7 additions & 1 deletion src/state/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { atom, useAtom } from "jotai";

import { Clock } from "@reearth/components/molecules/Visualizer/Plugin/types";
import { LayerSelectionReason } from "@reearth/core/Map";
import { Camera } from "@reearth/util/value";

// useError is needed for Apollo provider error only. Handle other errors with useNotification directly.
Expand Down Expand Up @@ -36,7 +37,12 @@ export const useSelectedWidgetArea = () => useAtom(selectedWidgetArea);

export type Selected =
| { type: "scene" }
| { type: "layer"; layerId: string }
| {
type: "layer";
layerId: string;
featureId?: string;
layerSelectionReason?: LayerSelectionReason;
}
| { type: "widgets" }
| { type: "cluster"; clusterId: string }
| { type: "widget"; widgetId?: string; pluginId: string; extensionId: string }
Expand Down

0 comments on commit b4afd7e

Please sign in to comment.