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

feat(plugin): add layers.overriddenProperties, find, findAll, walk #110

Merged
merged 3 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/components/molecules/Visualizer/Layers/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class LayerStore {
"property",
"propertyId",
"title",
"type",
],
function (key) {
const id = (this as any).id;
Expand All @@ -29,6 +30,16 @@ export class LayerStore {
return target?.children?.map(c => self.#pmap.get(c.id));
}

if (key === "type") {
return target
? `${
!target.pluginId || target.pluginId === "reearth"
? ""
: `${target.pluginId.replace(/#.*?$|^.*~/g, "")}/`
}${target.extensionId || ""}`
: undefined;
}

return target?.[key];
},
);
Expand Down Expand Up @@ -62,6 +73,44 @@ export class LayerStore {
return ids.map(id => this.findById(id));
};

find = (
fn: (layer: PluginLayer, index: number, parents: PluginLayer[]) => boolean,
): PluginLayer | undefined => {
return this.walk((...args) => (fn(...args) ? args[0] : undefined));
};

findAll = (
fn: (layer: PluginLayer, index: number, parents: PluginLayer[]) => boolean,
): PluginLayer[] => {
const res: PluginLayer[] = [];
this.walk((...args) => {
if (fn(...args)) res.push(args[0]);
});
return res;
};

walk = <T>(
fn: (layer: PluginLayer, index: number, parents: PluginLayer[]) => T | void,
): T | undefined => {
function w(layers: PluginLayer[], parents: PluginLayer[]): T | undefined {
for (let i = 0; i < layers.length; i++) {
const l = layers[i];
const res =
fn(l, i, parents) ??
(Array.isArray(l.children) && l.children.length > 0
? w(l.children, [...parents, l])
: undefined);
if (typeof res !== "undefined") {
return res;
}
}
return undefined;
}

if (!this.#proot.children) return;
return w(this.#proot.children, [this.#proot]);
};

get root(): PluginLayer {
return this.#proot;
}
Expand Down
30 changes: 26 additions & 4 deletions src/components/molecules/Visualizer/Plugin/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function commonReearth({
selectedLayer,
layerSelectionReason,
layerOverriddenInfobox,
layerOverriddenProperties,
selectLayer,
showLayer,
hideLayer,
Expand All @@ -121,6 +122,7 @@ export function commonReearth({
selectedLayer: () => GlobalThis["reearth"]["layers"]["selected"];
layerSelectionReason: () => GlobalThis["reearth"]["layers"]["selectionReason"];
layerOverriddenInfobox: () => GlobalThis["reearth"]["layers"]["overriddenInfobox"];
layerOverriddenProperties: () => GlobalThis["reearth"]["layers"]["overriddenProperties"];
selectLayer: GlobalThis["reearth"]["layers"]["select"];
showLayer: GlobalThis["reearth"]["layers"]["show"];
hideLayer: GlobalThis["reearth"]["layers"]["hide"];
Expand Down Expand Up @@ -149,10 +151,21 @@ export function commonReearth({
},
},
layers: {
select: selectLayer,
show: showLayer,
hide: hideLayer,
overrideProperty: overrideLayerProperty,
get select() {
return selectLayer;
},
get show() {
return showLayer;
},
get hide() {
return hideLayer;
},
get overriddenProperties() {
return layerOverriddenProperties();
},
get overrideProperty() {
return overrideLayerProperty;
},
get layers() {
return layers().root.children ?? [];
},
Expand All @@ -171,6 +184,15 @@ export function commonReearth({
get findByIds() {
return layers().findByIds;
},
get find() {
return layers().find;
},
get findAll() {
return layers().findAll;
},
get walk() {
return layers().walk;
},
},
...events,
};
Expand Down
4 changes: 4 additions & 0 deletions src/components/molecules/Visualizer/Plugin/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type Props = {
selectedLayer?: Layer;
layerSelectionReason?: string;
layerOverridenInfobox?: OverriddenInfobox;
layerOverriddenProperties?: { [key: string]: any };
showLayer: (...id: string[]) => void;
hideLayer: (...id: string[]) => void;
selectLayer: (id?: string, options?: { reason?: string }) => void;
Expand Down Expand Up @@ -73,6 +74,7 @@ export function Provider({
selectedLayer,
layerSelectionReason,
layerOverridenInfobox,
layerOverriddenProperties,
showLayer,
hideLayer,
selectLayer,
Expand All @@ -95,6 +97,7 @@ export function Provider({
const getSelectedLayer = useGet(selectedLayer);
const getLayerSelectionReason = useGet(layerSelectionReason);
const getLayerOverriddenInfobox = useGet(layerOverridenInfobox);
const getLayerOverriddenProperties = useGet(layerOverriddenProperties);

const value = useMemo<Context>(
() => ({
Expand All @@ -112,6 +115,7 @@ export function Provider({
selectedLayer: getSelectedLayer,
layerSelectionReason: getLayerSelectionReason,
layerOverriddenInfobox: getLayerOverriddenInfobox,
layerOverriddenProperties: getLayerOverriddenProperties,
showLayer,
hideLayer,
selectLayer,
Expand Down
9 changes: 9 additions & 0 deletions src/components/molecules/Visualizer/Plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,20 @@ export type Layers = {
readonly selected?: Layer;
readonly selectionReason?: string;
readonly overriddenInfobox?: OverriddenInfobox;
readonly overriddenProperties?: { [id: string]: any };
/** Selects the layer with the specified ID; if the ID is undefined, the currently selected later will be deselected. */
readonly select: (id?: string, options?: SelectLayerOptions) => void;
readonly show: (...id: string[]) => void;
readonly hide: (...id: string[]) => void;
readonly findById: (id: string) => Layer | undefined;
readonly findByIds: (...id: string[]) => (Layer | undefined)[];
readonly find: (
fn: (layer: Layer, index: number, parents: Layer[]) => boolean,
) => Layer | undefined;
readonly findAll: (fn: (layer: Layer, index: number, parents: Layer[]) => boolean) => Layer[];
readonly walk: <T>(
fn: (layer: Layer, index: number, parents: Layer[]) => T | void,
) => T | undefined;
readonly overrideProperty: (id: string, property: any) => void;
};

Expand All @@ -76,6 +84,7 @@ export type OverriddenInfobox = {
/** Layer is acutually displayed data on the map in which layers are flattened. All properties are stored with all dataset links, etc. resolved. */
export type Layer<P = any, IBP = any> = {
id: string;
type?: string;
pluginId?: string;
extensionId?: string;
title?: string;
Expand Down
9 changes: 5 additions & 4 deletions src/components/molecules/Visualizer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default ({
const {
selectedLayer,
selectedLayerId,
layeroverriddenProperties,
layerOverriddenProperties,
layerSelectionReason,
layerOverridenInfobox,
infobox,
Expand Down Expand Up @@ -143,6 +143,7 @@ export default ({
selectedLayer,
layerSelectionReason,
layerOverridenInfobox,
layerOverriddenProperties,
showLayer: showLayers,
hideLayer: hideLayers,
selectLayer,
Expand All @@ -164,7 +165,7 @@ export default ({
selectedLayerId,
selectedLayer,
layerSelectionReason,
layeroverriddenProperties,
layerOverriddenProperties,
selectedBlockId,
innerCamera,
infobox,
Expand Down Expand Up @@ -253,7 +254,7 @@ function useLayers({
setPrimitiveOverridenInfobox(undefined);
}, [outerSelectedPrimitiveId]);

const [layeroverriddenProperties, setLayeroverriddenProperties] = useState<{
const [layerOverriddenProperties, setLayeroverriddenProperties] = useState<{
[id in string]: any;
}>({});
const overrideLayerProperty = useCallback((id: string, property: any) => {
Expand All @@ -276,7 +277,7 @@ function useLayers({
selectedLayerId,
layerSelectionReason,
layerOverridenInfobox,
layeroverriddenProperties,
layerOverriddenProperties,
infobox,
isLayerHidden,
selectLayer,
Expand Down
4 changes: 2 additions & 2 deletions src/components/molecules/Visualizer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default function Visualizer({
selectedBlockId,
innerCamera,
infobox,
layeroverriddenProperties,
layerOverriddenProperties,
isLayerHidden,
selectLayer,
selectBlock,
Expand Down Expand Up @@ -162,7 +162,7 @@ export default function Visualizer({
selectedLayerId={selectedLayerId}
layers={layers}
isLayerHidden={isLayerHidden}
overriddenProperties={layeroverriddenProperties}
overriddenProperties={layerOverriddenProperties}
/>
{ready &&
widgets?.floatingWidgets?.map(widget => (
Expand Down