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

feat: allowing widget and block plugins to resize when they are expandable #170

Merged
merged 13 commits into from
Feb 9, 2022
15 changes: 12 additions & 3 deletions src/components/atoms/Plugin/IFrame/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function useHook({
autoResizeMessageKey?: string;
html?: string;
ref?: Ref<RefType>;
autoResize?: boolean;
autoResize?: "both" | "width-only" | "height-only";
visible?: boolean;
iFrameProps?: IframeHTMLAttributes<HTMLIFrameElement>;
onLoad?: () => void;
Expand Down Expand Up @@ -137,8 +137,17 @@ export default function useHook({
() => ({
style: {
display: visible ? undefined : "none",
width: visible ? (autoResize ? iFrameSize?.[0] : "100%") : "0px",
height: visible ? (autoResize ? iFrameSize?.[1] : "100%") : "0px",
// TODO: width iFrameSize?.[0]
width: visible
? autoResize == "height-only" || autoResize == "both"
? "100%"
: iFrameSize?.[0]
: "0px",
height: visible
? autoResize == "width-only" || autoResize == "both"
? "100%"
: iFrameSize?.[1]
: "0px",
minWidth: "100%",
maxWidth: "100%",
...iFrameProps?.style,
Expand Down
1 change: 0 additions & 1 deletion src/components/atoms/Plugin/IFrame/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const Default: Story<Props> = args => {
};

Default.args = {
autoResize: false,
visible: true,
iFrameProps: {
style: {
Expand Down
4 changes: 2 additions & 2 deletions src/components/atoms/Plugin/IFrame/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import useHook, { RefType } from "./hooks";
export type Ref = RefType;

export type Props = {
autoResize?: boolean;
autoResize?: "both" | "width-only" | "height-only";
className?: string;
html?: string;
visible?: boolean;
Expand Down Expand Up @@ -37,7 +37,7 @@ const IFrame: React.ForwardRefRenderFunction<Ref, Props> = (
return html ? (
<iframe
frameBorder="no"
scrolling={autoResize ? "no" : undefined}
scrolling={autoResize ? undefined : "no"}
data-testid="iframe"
srcDoc=""
key={html}
Expand Down
6 changes: 3 additions & 3 deletions src/components/atoms/Plugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Props = {
src?: string;
sourceCode?: string;
renderPlaceholder?: ReactNode;
filled?: boolean;
autoResize?: "both" | "width-only" | "height-only";
iFrameProps?: IframeHTMLAttributes<HTMLIFrameElement>;
isMarshalable?: boolean | "json" | ((target: any) => boolean | "json");
exposed?: ((api: IFrameAPI) => { [key: string]: any }) | { [key: string]: any };
Expand All @@ -31,7 +31,7 @@ const Plugin: React.FC<Props> = ({
src,
sourceCode,
renderPlaceholder,
filled,
autoResize,
iFrameProps,
isMarshalable,
exposed,
Expand All @@ -55,7 +55,7 @@ const Plugin: React.FC<Props> = ({

return iFrameHtml ? (
<IFrame
autoResize={!filled}
autoResize={autoResize}
className={className}
html={iFrameHtml}
ref={iFrameRef}
Expand Down
1 change: 1 addition & 0 deletions src/components/molecules/Visualizer/Block/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function BlockComponent<P = any, PP = any>({
) : (
<Wrapper editable={props?.isEditable} onClick={props?.onClick} selected={props?.isSelected}>
<Plugin
autoResize="height-only"
pluginId={props.block?.pluginId}
extensionId={props.block?.extensionId}
sourceCode={(props.block as any)?.__REEARTH_SOURCECODE} // for debugging
Expand Down
13 changes: 13 additions & 0 deletions src/components/molecules/Visualizer/Plugin/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function ({
pluginId,
extensionId,
pluginBaseUrl,
autoResize,
extensionType,
block,
layer,
Expand All @@ -22,6 +23,7 @@ export default function ({
pluginId?: string;
extensionId?: string;
pluginBaseUrl?: string;
autoResize?: "both" | "width-only" | "height-only";
extensionType?: string;
layer?: Layer;
widget?: Widget;
Expand Down Expand Up @@ -51,6 +53,16 @@ export default function ({
? `${pluginBaseUrl}/${`${pluginId}/${extensionId}`.replace(/\.\./g, "")}.js`
: undefined;

const actualAutoResize =
autoResize ??
(widget?.extended?.horizontally && widget.extended.vertically
? "both"
: widget?.extended?.vertically
? "width-only"
: widget?.extended?.horizontally
? "height-only"
: undefined);

return {
skip: !staticExposed,
src,
Expand All @@ -60,6 +72,7 @@ export default function ({
onMessage,
onPreInit,
onDispose,
actualAutoResize,
};
}

Expand Down
17 changes: 15 additions & 2 deletions src/components/molecules/Visualizer/Plugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type Props = {
pluginId?: string;
extensionId?: string;
extensionType?: string;
autoResize?: "both" | "width-only" | "height-only";
visible?: boolean;
iFrameProps?: PluginProps["iFrameProps"];
property?: any;
Expand All @@ -40,6 +41,7 @@ export default function Plugin({
pluginId,
extensionId,
extensionType,
autoResize,
iFrameProps,
visible,
pluginBaseUrl = "/plugins",
Expand All @@ -49,11 +51,22 @@ export default function Plugin({
pluginProperty,
onClick,
}: Props): JSX.Element | null {
const { skip, src, isMarshalable, onPreInit, onDispose, exposed, onError, onMessage } = useHooks({
const {
skip,
src,
isMarshalable,
actualAutoResize,
onPreInit,
onDispose,
exposed,
onError,
onMessage,
} = useHooks({
pluginId,
extensionId,
extensionType,
pluginBaseUrl,
autoResize,
layer,
widget,
block,
Expand All @@ -65,7 +78,7 @@ export default function Plugin({
className={className}
src={src}
sourceCode={sourceCode}
filled={!!widget?.extended?.horizontally || !!widget?.extended?.vertically}
autoResize={actualAutoResize}
iFrameProps={iFrameProps}
canBeVisible={visible}
isMarshalable={isMarshalable}
Expand Down