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

Commit

Permalink
feat: extend plugin API supports close widget (#355)
Browse files Browse the repository at this point in the history
* feat: add plugin API ui.close

* refactor: clean up

* refactor: remove unnecessary function

* refactor: clean up viewoprt api

* refactor: use visible instead of enabled

* refactor: rename
  • Loading branch information
airslice committed Nov 18, 2022
1 parent 2257586 commit d025787
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/components/atoms/Plugin/PluginIFrame/useIFrame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export default function useIFrame({
[iframeCanBeVisible, onRender, postMessage],
);

useEffect(() => {
setIFrameState(([html, options]) => [html, { ...options, visible: !!iframeCanBeVisible }]);
}, [iframeCanBeVisible]);

useEffect(() => {
if (!ready) setIFrameLoaded(false);
}, [ready]);
Expand Down
6 changes: 3 additions & 3 deletions src/components/atoms/Plugin/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let cb: (message: any) => void | undefined;

Default.args = {
src: `/plugins/plugin.js`,
canBeVisible: true,
uiVisible: true,
iFrameProps: {
style: {
width: "300px",
Expand Down Expand Up @@ -50,7 +50,7 @@ export const HiddenIFrame: Story<Props> = args => <Component {...args} />;

HiddenIFrame.args = {
src: `/plugins/hidden.js`,
canBeVisible: true,
uiVisible: true,
iFrameProps: {
style: {
width: "300px",
Expand Down Expand Up @@ -132,6 +132,6 @@ AutoResize.args = {
onmessage = msg => { resize(msg.width, msg.height); };
`,
autoResize: "both",
canBeVisible: true,
uiVisible: true,
exposed: ({ main: { render, resize } }) => ({ render, resize }),
};
6 changes: 3 additions & 3 deletions src/components/atoms/Plugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type { API, IFrameType, Ref } from "./hooks";

export type Props = {
className?: string;
canBeVisible?: boolean;
uiVisible?: boolean;
skip?: boolean;
src?: string;
sourceCode?: string;
Expand All @@ -36,7 +36,7 @@ export type Props = {
const Plugin: ForwardRefRenderFunction<Ref, Props> = (
{
className,
canBeVisible,
uiVisible,
modalVisible,
popupVisible,
skip,
Expand Down Expand Up @@ -78,7 +78,7 @@ const Plugin: ForwardRefRenderFunction<Ref, Props> = (
type="main"
ref={mainIFrameRef}
ready={loaded}
visible={canBeVisible}
visible={uiVisible}
enabled
className={className}
iFrameProps={iFrameProps}
Expand Down
7 changes: 4 additions & 3 deletions src/components/molecules/Visualizer/Plugin/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
Plugin,
Tag,
PopupPosition,
Viewport,
} from "./types";

export type CommonReearth = Omit<
Expand All @@ -24,6 +23,7 @@ export type CommonReearth = Omit<

export function exposed({
render,
closeUI,
postMessage,
resize,
renderModal,
Expand Down Expand Up @@ -51,6 +51,7 @@ export function exposed({
extended?: boolean;
},
) => void;
closeUI: Reearth["ui"]["close"];
postMessage: Reearth["ui"]["postMessage"];
resize: Reearth["ui"]["resize"];
renderModal: (
Expand Down Expand Up @@ -91,7 +92,6 @@ export function exposed({
};
},
}),
viewport: commonReearth.viewport as Viewport,
layers: merge(commonReearth.layers, {
get add() {
return (layer: Layer, parentId?: string) =>
Expand All @@ -115,6 +115,7 @@ export function exposed({
},
postMessage,
resize,
close: closeUI,
},
modal: {
show: (
Expand Down Expand Up @@ -324,7 +325,7 @@ export function commonReearth({
getLocationFromScreen,
},
get viewport() {
return viewport?.();
return viewport();
},
engineName,
camera: {
Expand Down
15 changes: 14 additions & 1 deletion src/components/molecules/Visualizer/Plugin/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Options } from "quickjs-emscripten-sync";
import { useCallback, useEffect, useMemo, useRef, MutableRefObject } from "react";
import { useCallback, useEffect, useMemo, useRef, MutableRefObject, useState } from "react";
import type { RefObject } from "react";

import type { API as IFrameAPI } from "@reearth/components/atoms/Plugin";
Expand All @@ -19,6 +19,7 @@ export default function ({
extensionId,
pluginBaseUrl,
extensionType,
visible,
block,
layer,
widget,
Expand All @@ -34,6 +35,7 @@ export default function ({
extensionId?: string;
pluginBaseUrl?: string;
extensionType?: string;
visible?: boolean;
layer?: Layer;
widget?: Widget;
block?: Block;
Expand Down Expand Up @@ -61,6 +63,8 @@ export default function ({
const popupVisible = useRef<boolean>(false);
const externalRef = useRef<HTMLIFrameElement>(null);

const [uiVisible, setUIVisibility] = useState<boolean>(!!visible);

const { staticExposed, isMarshalable, onPreInit, onDispose, onModalClose, onPopupClose } =
useAPI({
extensionId,
Expand All @@ -75,6 +79,7 @@ export default function ({
externalRef,
onPluginModalShow,
onPluginPopupShow,
setUIVisibility,
onRender,
onResize,
}) ?? [];
Expand Down Expand Up @@ -131,6 +136,7 @@ export default function ({
skip: !staticExposed,
src,
isMarshalable,
uiVisible,
modalVisible: modalVisible.current,
popupVisible: popupVisible.current,
externalRef,
Expand All @@ -154,6 +160,7 @@ export function useAPI({
externalRef,
onPluginModalShow,
onPluginPopupShow,
setUIVisibility,
onRender,
onResize,
}: {
Expand All @@ -169,6 +176,7 @@ export function useAPI({
externalRef: RefObject<HTMLIFrameElement> | undefined;
onPluginModalShow?: (modalInfo?: PluginModalInfo) => void;
onPluginPopupShow?: (popupInfo?: PluginPopupInfo) => void;
setUIVisibility: (visible: boolean) => void;
onRender?: (
options:
| {
Expand Down Expand Up @@ -294,6 +302,10 @@ export function useAPI({
onRender?.(
typeof extended !== "undefined" || options ? { extended, ...options } : undefined,
);
setUIVisibility(true);
},
closeUI: () => {
setUIVisibility(false);
},
renderModal: (html, { ...options } = {}) => {
modal.render(html, options);
Expand Down Expand Up @@ -370,6 +382,7 @@ export function useAPI({
getWidget,
onPluginModalShow,
onPluginPopupShow,
setUIVisibility,
onRender,
onResize,
]);
Expand Down
4 changes: 3 additions & 1 deletion src/components/molecules/Visualizer/Plugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default function Plugin({
skip,
src,
isMarshalable,
uiVisible,
modalVisible,
popupVisible,
externalRef,
Expand All @@ -98,6 +99,7 @@ export default function Plugin({
extensionId,
extensionType,
pluginBaseUrl,
visible,
layer,
widget,
block,
Expand All @@ -117,7 +119,7 @@ export default function Plugin({
sourceCode={sourceCode}
autoResize={autoResize}
iFrameProps={iFrameProps}
canBeVisible={visible}
uiVisible={uiVisible}
modalVisible={modalVisible}
popupVisible={popupVisible}
modalContainer={pluginModalContainer}
Expand Down
1 change: 1 addition & 0 deletions src/components/molecules/Visualizer/Plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export type UI = {
/** Overrides whether the iframe is extended. This option is only available for widgets on an extendable area on the widget align system. */
extended?: boolean | undefined,
) => void;
readonly close: () => void;
};

export type Modal = {
Expand Down

0 comments on commit d025787

Please sign in to comment.