Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
feat: support tags in plugin API (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 authored Jan 21, 2022
1 parent 90084f7 commit 1031c51
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 33 deletions.
17 changes: 17 additions & 0 deletions src/components/molecules/Visualizer/Layers/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class LayerStore {
"propertyId",
"title",
"type",
"tags",
],
function (key) {
const id = (this as any).id;
Expand Down Expand Up @@ -77,6 +78,22 @@ export class LayerStore {
return ids.map(id => this.findById(id));
};

findByTags = (...tagIds: string[]): PluginLayer[] => {
return this.findAll(
l =>
!!l.tags?.some(t => tagIds.includes(t.id) || !!t.tags?.some(tt => tagIds.includes(tt.id))),
);
};

findByTagLabels = (...tagLabels: string[]): PluginLayer[] => {
return this.findAll(
l =>
!!l.tags?.some(
t => tagLabels.includes(t.label) || !!t.tags?.some(tt => tagLabels.includes(tt.label)),
),
);
};

find = (
fn: (layer: PluginLayer, index: number, parents: PluginLayer[]) => boolean,
): PluginLayer | undefined => {
Expand Down
22 changes: 21 additions & 1 deletion src/components/molecules/Visualizer/Plugin/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import { merge } from "@reearth/util/object";

import type { LayerStore } from "../Layers";

import type { GlobalThis, Block, Layer, Widget, ReearthEventType, Reearth, Plugin } from "./types";
import type {
GlobalThis,
Block,
Layer,
Widget,
ReearthEventType,
Reearth,
Plugin,
Tag,
} from "./types";

export type CommonReearth = Omit<Reearth, "plugin" | "ui" | "block" | "layer" | "widget">;

Expand Down Expand Up @@ -100,6 +109,7 @@ export function commonReearth({
events,
layers,
sceneProperty,
tags,
camera,
selectedLayer,
layerSelectionReason,
Expand All @@ -118,6 +128,7 @@ export function commonReearth({
events: Events<ReearthEventType>;
layers: () => LayerStore;
sceneProperty: () => any;
tags: () => Tag[];
camera: () => GlobalThis["reearth"]["visualizer"]["camera"]["position"];
selectedLayer: () => GlobalThis["reearth"]["layers"]["selected"];
layerSelectionReason: () => GlobalThis["reearth"]["layers"]["selectionReason"];
Expand Down Expand Up @@ -172,6 +183,9 @@ export function commonReearth({
get layers() {
return layers().root.children ?? [];
},
get tags() {
return tags();
},
get selectionReason() {
return layerSelectionReason();
},
Expand All @@ -187,6 +201,12 @@ export function commonReearth({
get findByIds() {
return layers().findByIds;
},
get findByTags() {
return layers().findByTags;
},
get findByTagLabels() {
return layers().findByTagLabels;
},
get find() {
return layers().find;
},
Expand Down
5 changes: 5 additions & 0 deletions src/components/molecules/Visualizer/Plugin/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
ReearthEventType,
FlyToDestination,
LookAtDestination,
Tag,
} from "./types";

export type EngineContext = {
Expand All @@ -35,6 +36,7 @@ export type Props = {
engine: EngineContext;
engineName: string;
sceneProperty?: any;
tags?: Tag[];
camera?: CameraPosition;
layers: LayerStore;
selectedLayer?: Layer;
Expand Down Expand Up @@ -69,6 +71,7 @@ export function Provider({
engine: { api, isMarshalable, builtinPrimitives },
engineName,
sceneProperty,
tags,
camera,
layers,
selectedLayer,
Expand All @@ -93,6 +96,7 @@ export function Provider({

const getLayers = useGet(layers);
const getSceneProperty = useGet(sceneProperty);
const getTags = useGet(tags ?? []);
const getCamera = useGet(camera);
const getSelectedLayer = useGet(selectedLayer);
const getLayerSelectionReason = useGet(layerSelectionReason);
Expand All @@ -111,6 +115,7 @@ export function Provider({
events: ev,
layers: getLayers,
sceneProperty: getSceneProperty,
tags: getTags,
camera: getCamera,
selectedLayer: getSelectedLayer,
layerSelectionReason: getLayerSelectionReason,
Expand Down
10 changes: 10 additions & 0 deletions src/components/molecules/Visualizer/Plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type Plugin = {
export type Layers = {
readonly layers: Layer[];
readonly selected?: Layer;
readonly tags?: Tag[];
readonly selectionReason?: string;
readonly overriddenInfobox?: OverriddenInfobox;
readonly overriddenProperties?: { [id: string]: any };
Expand All @@ -61,6 +62,8 @@ export type Layers = {
readonly hide: (...id: string[]) => void;
readonly findById: (id: string) => Layer | undefined;
readonly findByIds: (...id: string[]) => (Layer | undefined)[];
readonly findByTags: (...tagIds: string[]) => Layer[];
readonly findByTagLabels: (...tagLabels: string[]) => Layer[];
readonly find: (
fn: (layer: Layer, index: number, parents: Layer[]) => boolean,
) => Layer | undefined;
Expand Down Expand Up @@ -93,9 +96,16 @@ export type Layer<P = any, IBP = any> = {
infobox?: Infobox<IBP>;
isVisible?: boolean;
propertyId?: string;
tags?: Tag[];
readonly children?: Layer<P, IBP>[];
};

export type Tag = {
id: string;
label: string;
tags?: Tag[];
};

export type Infobox<BP = any> = {
property?: InfoboxProperty;
blocks?: Block<BP>[];
Expand Down
5 changes: 4 additions & 1 deletion src/components/molecules/Visualizer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
import type { Props as InfoboxProps, Block } from "./Infobox";
import { LayerStore, emptyLayerStore } from "./Layers";
import type { ProviderProps } from "./Plugin";
import { CameraOptions, FlyToDestination, LookAtDestination } from "./Plugin/types";
import type { CameraOptions, FlyToDestination, LookAtDestination, Tag } from "./Plugin/types";

export default ({
engineType,
Expand All @@ -27,6 +27,7 @@ export default ({
selectedBlockId: outerSelectedBlockId,
camera,
sceneProperty,
tags,
onLayerSelect,
onBlockSelect,
onCameraChange,
Expand All @@ -42,6 +43,7 @@ export default ({
selectedBlockId?: string;
camera?: Camera;
sceneProperty?: SceneProperty;
tags?: Tag[];
onLayerSelect?: (id?: string) => void;
onBlockSelect?: (id?: string) => void;
onCameraChange?: (c: Camera) => void;
Expand Down Expand Up @@ -139,6 +141,7 @@ export default ({
{
engineName: engineType || "",
sceneProperty,
tags,
camera,
selectedLayer,
layerSelectionReason,
Expand Down
5 changes: 5 additions & 0 deletions src/components/molecules/Visualizer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import useHooks from "./hooks";
import Infobox, { Props as InfoboxProps } from "./Infobox";
import Layers, { LayerStore } from "./Layers";
import { Provider } from "./Plugin";
import type { Tag } from "./Plugin/types";
import W from "./Widget";
import type { Widget } from "./Widget";
import WidgetAlignSystem, {
Expand All @@ -21,6 +22,7 @@ import WidgetAlignSystem, {
export type { SceneProperty, ClusterProperty } from "./Engine";
export type { InfoboxProperty, Block } from "./Infobox";
export type { Layer } from "./Layers";
export type { Tag } from "./Plugin/types";
export type {
Widget,
Alignment,
Expand All @@ -44,6 +46,7 @@ export type Props = PropsWithChildren<
layoutConstraint?: WidgetAlignSystemProps["layoutConstraint"];
};
sceneProperty?: SceneProperty;
tags?: Tag[];
pluginProperty?: { [key: string]: any };
clusterProperty?: ClusterProperty[];
clusterLayers?: string[];
Expand All @@ -70,6 +73,7 @@ export default function Visualizer({
layers,
widgets,
sceneProperty,
tags,
children,
pluginProperty,
clusterProperty,
Expand Down Expand Up @@ -121,6 +125,7 @@ export default function Visualizer({
selectedBlockId: outerSelectedBlockId,
camera: props.camera,
sceneProperty,
tags,
onLayerSelect,
onBlockSelect,
onCameraChange: props.onCameraChange,
Expand Down
30 changes: 30 additions & 0 deletions src/components/organisms/EarthEditor/CanvasArea/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
WidgetArea,
Alignment,
WidgetLayoutConstraint,
Tag,
} from "@reearth/components/molecules/Visualizer";
import {
GetLayersQuery,
Expand Down Expand Up @@ -148,6 +149,7 @@ const processLayer = (layer: EarthLayer5Fragment | undefined): Layer | undefined
layer.__typename === "LayerItem"
? processMergedInfobox(layer.merged?.infobox)
: processInfobox(layer.infobox),
tags: processLayerTags(layer.tags),
children:
layer.__typename === "LayerGroup"
? layer.layers?.map(l => processLayer(l ?? undefined)).filter((l): l is Layer => !!l)
Expand Down Expand Up @@ -296,3 +298,31 @@ export const convertToBlocks = (data?: GetBlocksQuery): BlockType[] | undefined
.filter((a): a is BlockType[] => !!a)
.reduce((a, b) => [...a, ...b], []);
};

export function processLayerTags(
tags: {
tagId: string;
tag?: Maybe<{ label: string }>;
children?: { tagId: string; tag?: Maybe<{ label: string }> }[];
}[],
): Tag[] {
return tags.map(t => ({
id: t.tagId,
label: t.tag?.label ?? "",
tags: t.children?.map(tt => ({ id: tt.tagId, label: tt.tag?.label ?? "" })),
}));
}

export function processSceneTags(
tags: {
id: string;
label: string;
tags?: { id: string; label: string }[];
}[],
): Tag[] {
return tags.map(t => ({
id: t.id,
label: t.label ?? "",
tags: t.tags?.map(tt => ({ id: tt.id, label: tt.label ?? "" })),
}));
}
33 changes: 17 additions & 16 deletions src/components/organisms/EarthEditor/CanvasArea/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from "@reearth/components/molecules/Visualizer/WidgetAlignSystem/hooks";
import {
useGetLayersQuery,
useGetClustersQuery,
useGetEarthWidgetsQuery,
useMoveInfoboxFieldMutation,
useRemoveInfoboxFieldMutation,
Expand All @@ -33,7 +32,13 @@ import {
} from "@reearth/state";
import { valueTypeToGQL, ValueTypes, valueToGQL, LatLng } from "@reearth/util/value";

import { convertLayers, convertWidgets, convertToBlocks, convertProperty } from "./convert";
import {
convertLayers,
convertWidgets,
convertToBlocks,
convertProperty,
processSceneTags,
} from "./convert";

export default (isBuilt?: boolean) => {
const intl = useIntl();
Expand Down Expand Up @@ -81,45 +86,40 @@ export default (isBuilt?: boolean) => {
variables: { sceneId: sceneId ?? "", lang: intl.locale },
skip: !sceneId,
});
const { data: widgetData } = useGetEarthWidgetsQuery({
variables: { sceneId: sceneId ?? "", lang: intl.locale },
skip: !sceneId,
});

// TODO
const { data: clusterData } = useGetClustersQuery({
const { data: sceneData } = useGetEarthWidgetsQuery({
variables: { sceneId: sceneId ?? "", lang: intl.locale },
skip: !sceneId,
});

const rootLayerId =
layerData?.node?.__typename === "Scene" ? layerData.node.rootLayer?.id : undefined;
const scene = widgetData?.node?.__typename === "Scene" ? widgetData.node : undefined;
const scene = sceneData?.node?.__typename === "Scene" ? sceneData.node : undefined;

// convert data
const selectedLayerId = selected?.type === "layer" ? selected.layerId : undefined;
const layers = useMemo(() => convertLayers(layerData), [layerData]);
const selectedLayer = selectedLayerId ? layers?.findById(selectedLayerId) : undefined;
const widgets = useMemo(() => convertWidgets(widgetData), [widgetData]);
const widgets = useMemo(() => convertWidgets(sceneData), [sceneData]);
const sceneProperty = useMemo(() => convertProperty(scene?.property), [scene?.property]);
const clusterScene = clusterData?.node?.__typename === "Scene" ? clusterData.node : undefined;
const tags = useMemo(() => processSceneTags(scene?.tags ?? []), [scene?.tags]);

// TODO: refactor
const clusterProperty = useMemo(
() => clusterScene?.clusters.reduce<any[]>((a, b) => [...a, convertProperty(b.property)], []),
[clusterScene?.clusters],
() => scene?.clusters.reduce<any[]>((a, b) => [...a, convertProperty(b.property)], []),
[scene?.clusters],
);

const clusterLayers = useMemo(
() =>
clusterScene?.clusters.reduce<any[]>(
scene?.clusters.reduce<any[]>(
(a, b) =>
flatMap([
...a,
convertProperty(b.property)?.layers?.map((layerItem: any) => layerItem.layer),
]).filter(item => !!item),
[],
),
[clusterScene?.clusters],
[scene?.clusters],
);

const pluginProperty = useMemo(
Expand Down Expand Up @@ -279,6 +279,7 @@ export default (isBuilt?: boolean) => {
pluginProperty,
clusterProperty,
clusterLayers,
tags,
widgets,
layers,
selectedLayer,
Expand Down
2 changes: 2 additions & 0 deletions src/components/organisms/EarthEditor/CanvasArea/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const CanvasArea: React.FC<Props> = ({ className, isBuilt }) => {
clusterLayers,
layers,
widgets,
tags,
selectedLayerId,
blocks,
isCapturing,
Expand Down Expand Up @@ -62,6 +63,7 @@ const CanvasArea: React.FC<Props> = ({ className, isBuilt }) => {
selectedBlockId={selectedBlockId}
rootLayerId={rootLayerId}
sceneProperty={sceneProperty}
tags={tags}
pluginProperty={pluginProperty}
clusterProperty={clusterProperty}
clusterLayers={clusterLayers}
Expand Down
Loading

0 comments on commit 1031c51

Please sign in to comment.