Skip to content

Commit

Permalink
fix: WAS bug on reearth/core (#416)
Browse files Browse the repository at this point in the history
fix: was bug
  • Loading branch information
keiya01 committed Feb 2, 2023
1 parent f1a8dd3 commit 0452740
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
9 changes: 2 additions & 7 deletions src/core/Crust/Widgets/WidgetAlignSystem/MobileZone.tsx
Expand Up @@ -7,6 +7,7 @@ import { styled } from "@reearth/theme";

import Area from "./Area";
import type { WidgetZone, WidgetLayoutConstraint, Theme, WidgetProps } from "./types";
import { filterSections } from "./utils";

export type Props = {
children?: ReactNode;
Expand All @@ -18,7 +19,6 @@ export type Props = {
renderWidget?: (props: WidgetProps) => ReactNode;
};

const sections = ["left", "center", "right"] as const;
const areas = ["top", "middle", "bottom"] as const;

export default function MobileZone({
Expand All @@ -31,12 +31,7 @@ export default function MobileZone({
renderWidget,
}: Props) {
const filteredSections = useMemo(() => {
return sections.filter(
s =>
areas.filter(a => zone?.[s]?.[a]?.widgets?.find(w => !invisibleWidgetIDs?.includes(w.id)))
.length ||
(s === "center" && children),
);
return filterSections(zone, invisibleWidgetIDs, s => s === "center" && children);
}, [zone, children, invisibleWidgetIDs]);

const initialPos = useMemo(() => (filteredSections.length === 3 ? 1 : 0), [filteredSections]);
Expand Down
12 changes: 10 additions & 2 deletions src/core/Crust/Widgets/WidgetAlignSystem/index.tsx
@@ -1,4 +1,4 @@
import React, { ReactNode } from "react";
import React, { ReactNode, useMemo } from "react";
import { GridWrapper } from "react-align";

import { styled } from "@reearth/theme";
Expand All @@ -13,6 +13,7 @@ import type {
Theme,
WidgetProps,
} from "./types";
import { filterSections } from "./utils";
import ZoneComponent from "./Zone";

export type {
Expand Down Expand Up @@ -65,6 +66,13 @@ const WidgetAlignSystem: React.FC<Props> = ({
});
const Zone = isMobile ? MobileZone : ZoneComponent;

const hasInner = useMemo(() => {
if (!alignSystem?.inner) {
return;
}
return !!filterSections(alignSystem?.inner, invisibleWidgetIDs).length;
}, [alignSystem, invisibleWidgetIDs]);

return (
<WidetAlignSystemWrapper editorMode={editing}>
<GridWrapper
Expand All @@ -79,7 +87,7 @@ const WidgetAlignSystem: React.FC<Props> = ({
invisibleWidgetIDs={invisibleWidgetIDs}
theme={theme}
renderWidget={renderWidget}>
{(!isMobile || alignSystem?.inner) && (
{(!isMobile || hasInner) && (
<ZoneComponent
zoneName="inner"
invisibleWidgetIDs={invisibleWidgetIDs}
Expand Down
16 changes: 16 additions & 0 deletions src/core/Crust/Widgets/WidgetAlignSystem/utils.ts
@@ -0,0 +1,16 @@
import { WidgetZone } from "./types";

const sections = ["left", "center", "right"] as const;
const areas = ["top", "middle", "bottom"] as const;

export const filterSections = (
zone?: WidgetZone,
invisibleWidgetIDs?: string[],
cb?: (s: (typeof sections)[number]) => void,
) => {
return sections.filter(
s =>
areas.filter(a => zone?.[s]?.[a]?.widgets?.find(w => !invisibleWidgetIDs?.includes(w.id)))
.length || cb?.(s),
);
};
37 changes: 20 additions & 17 deletions src/core/Crust/Widgets/useWidgetAlignSystem.ts
Expand Up @@ -43,8 +43,7 @@ export default ({ alignSystem }: { alignSystem: WidgetAlignSystem | undefined })

setOverrideAlignSystem(alignSystem => {
if (!alignSystem) return alignSystem;
let next = { ...alignSystem };
Object.keys(next).forEach(zoneName_ => {
Object.keys(alignSystem).forEach(zoneName_ => {
const zoneName = zoneName_ as keyof WidgetAlignSystem;
const zone = alignSystem[zoneName];
if (zone) {
Expand All @@ -59,27 +58,14 @@ export default ({ alignSystem }: { alignSystem: WidgetAlignSystem | undefined })
const sourceIndex = area.widgets.findIndex(w => w.id === widgetId);
if (sourceIndex !== -1) {
[widget] = area.widgets.splice(sourceIndex, 1);
next = {
...next,
[zoneName]: {
...next[zoneName],
[sectionName]: {
...(next[zoneName]?.[sectionName] || {}),
[areaName]: {
...(next[zoneName]?.[sectionName]?.[areaName] || {}),
...area,
},
},
},
};
}
}
});
}
});
}
});
return { ...next };
return { ...alignSystem };
});

setTimeout(() => {
Expand Down Expand Up @@ -118,7 +104,24 @@ export default ({ alignSystem }: { alignSystem: WidgetAlignSystem | undefined })
targetArea.widgets.push(widget);
}

return { ...alignSystem };
if (targetArea.widgets.length) {
const next = {
...alignSystem,
[options.zone]: {
...alignSystem[options.zone],
[options.section]: {
...(alignSystem[options.zone]?.[options.section] || {}),
[options.area]: {
...(alignSystem[options.zone]?.[options.section]?.[options.area] || {}),
widgets: [...targetArea.widgets],
},
},
},
};
return { ...next };
}

return alignSystem;
});
}, 0);
}, []);
Expand Down

0 comments on commit 0452740

Please sign in to comment.