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

Commit

Permalink
fix(plugin): layers.overrideProperty property merging and rerendering (
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 authored Oct 25, 2021
1 parent ebe1314 commit e5c2550
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"type-fest": "^2.3.4",
"typescript": "^4.4.3",
"typescript-styled-plugin": "^0.18.1",
"use-custom-compare": "^1.2.0",
"webpack": "^5.54.0",
"webpack-cli": "^4.5.0",
"webpack-dev-server": "^4.3.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { PolygonHierarchy, Cartesian3 } from "cesium";
import { isEqual } from "lodash";
import React, { useMemo } from "react";
import { PolygonGraphics, Entity } from "resium";
import { useCustomCompareMemo } from "use-custom-compare";

import { Polygon as PolygonValue, toColor } from "@reearth/util/value";

Expand Down Expand Up @@ -36,7 +38,7 @@ const Polygon: React.FC<PrimitiveProps<Property>> = ({ layer }) => {
shadows,
} = property?.default ?? {};

const hierarchy = useMemo(
const hierarchy = useCustomCompareMemo(
() =>
polygon?.[0]
? new PolygonHierarchy(
Expand All @@ -50,6 +52,7 @@ const Polygon: React.FC<PrimitiveProps<Property>> = ({ layer }) => {
)
: undefined,
[polygon],
isEqual,
);

const memoStrokeColor = useMemo(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Cartesian3 } from "cesium";
import { isEqual } from "lodash";
import React, { useMemo } from "react";
import { PolylineGraphics, Entity } from "resium";
import { useCustomCompareMemo } from "use-custom-compare";

import { Coordinates, toColor } from "@reearth/util/value";

Expand Down Expand Up @@ -29,9 +31,10 @@ const Polyline: React.FC<PrimitiveProps<Property>> = ({ layer }) => {
shadows,
} = property?.default ?? {};

const positions = useMemo(
const positions = useCustomCompareMemo(
() => coordinates?.map(c => Cartesian3.fromDegrees(c.lng, c.lat, c.height)),
[coordinates],
isEqual,
);
const material = useMemo(() => toColor(strokeColor), [strokeColor]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Rectangle, Color, ImageMaterialProperty } from "cesium";
import { isEqual } from "lodash";
import React, { useMemo } from "react";
import { RectangleGraphics, Entity } from "resium";
import { useCustomCompareMemo } from "use-custom-compare";

import { Rect as RectValue } from "@reearth/util/value";

Expand Down Expand Up @@ -39,7 +41,7 @@ const Rect: React.FC<PrimitiveProps<Property>> = ({ layer }) => {
shadows,
} = property?.default ?? {};

const coordinates = useMemo(
const coordinates = useCustomCompareMemo(
() =>
rect &&
rect.west <= rect.east &&
Expand All @@ -55,6 +57,7 @@ const Rect: React.FC<PrimitiveProps<Property>> = ({ layer }) => {
? Rectangle.fromDegrees(rect.west, rect.south, rect.east, rect.north)
: undefined,
[rect],
isEqual,
);

const material = useMemo(
Expand Down
11 changes: 11 additions & 0 deletions src/components/molecules/Visualizer/Primitive/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { mergeProperty } from ".";

test("mergeProperty", () => {
const a = { a: { b: { lat: 0, lng: 1 } }, c: [{ d: 1 }, { d: 2 }], d: 1 };
const b = { a: { b: { lat: 1 } }, c: [{ d: 3 }] };
const e = { a: { b: { lat: 1 } }, c: [{ d: 3 }], d: 1 };

const result = mergeProperty(a, b);
expect(result).toEqual(e);
expect(result).not.toBe(a);
});
25 changes: 11 additions & 14 deletions src/components/molecules/Visualizer/Primitive/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { merge, cloneDeep } from "lodash-es";
import { mergeWith } from "lodash";
import React, { ComponentType, useMemo } from "react";

import { useContext } from "../Plugin";
Expand Down Expand Up @@ -30,7 +30,7 @@ export default function PrimitiveComponent<P = any, PP = any, SP = any>({
const overridenProperty = useMemo(
() =>
props.layer && overriddenProperties?.[props.layer.id]
? merge(cloneDeep(props.layer.property), overriddenProperties[props.layer.id])
? mergeProperty(props.layer.property, overriddenProperties[props.layer.id])
: undefined,
[overriddenProperties, props.layer],
);
Expand All @@ -52,16 +52,13 @@ export default function PrimitiveComponent<P = any, PP = any, SP = any>({
return isHidden || !props.layer?.isVisible ? null : Builtin ? (
<Builtin {...props} layer={actualLayer} />
) : null;
// <Plugin
// pluginId={props.layer?.pluginId}
// extensionId={props.layer?.extensionId}
// sourceCode={(props.layer as any)?.__REEARTH_SOURCECODE} // for debugging
// extensionType="primitive"
// pluginBaseUrl={pluginBaseUrl}
// visible={false}
// property={props.pluginProperty}
// sceneProperty={props.sceneProperty}
// layer={props.layer}
// pluginProperty={pluginProperty}
// />
}

export function mergeProperty(a: any, b: any) {
return mergeWith(
{ ...a },
b,
(s: any, v: any, _k: string | number | symbol, _obj: any, _src: any, stack: { size: number }) =>
stack.size > 0 || Array.isArray(v) ? v ?? s : undefined,
);
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19016,6 +19016,11 @@ use-composed-ref@^1.0.0:
dependencies:
ts-essentials "^2.0.3"

use-custom-compare@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/use-custom-compare/-/use-custom-compare-1.2.0.tgz#1d091361a3dbbc6092d16f8b123f0ea255040b6a"
integrity sha512-eqWhT9cPz3Y1R7CW/SHVpVudwAqmQD3mAOO/dk+mVp9NhVpgVigDl1w6WEeHOMa3JbhwQYOrCI9fLAdbtAOSXQ==

use-debounce@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/use-debounce/-/use-debounce-7.0.0.tgz#00a67d23d4fe09905e11145a99278da06c01c880"
Expand Down

0 comments on commit e5c2550

Please sign in to comment.