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

feat: extend plugin API event with modalclose popupclose #354

Merged
merged 1 commit into from
Nov 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 49 additions & 9 deletions src/components/molecules/Visualizer/Plugin/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,7 @@ export default function ({
const popupVisible = useRef<boolean>(false);
const externalRef = useRef<HTMLIFrameElement>(null);

useEffect(() => {
modalVisible.current = shownPluginModalInfo?.id === (widget?.id ?? block?.id);
}, [modalVisible, shownPluginModalInfo, pluginId, extensionId, widget?.id, block?.id]);

useEffect(() => {
popupVisible.current = shownPluginPopupInfo?.id === (widget?.id ?? block?.id);
}, [popupVisible, shownPluginPopupInfo, pluginId, extensionId, widget?.id, block?.id]);

const { staticExposed, isMarshalable, onPreInit, onDispose } =
const { staticExposed, isMarshalable, onPreInit, onDispose, onModalClose, onPopupClose } =
useAPI({
extensionId,
extensionType,
Expand All @@ -87,6 +79,42 @@ export default function ({
onResize,
}) ?? [];

useEffect(() => {
const visible = shownPluginModalInfo?.id === (widget?.id ?? block?.id);
if (modalVisible.current !== visible) {
modalVisible.current = visible;
if (!modalVisible.current) {
onModalClose();
}
}
}, [
modalVisible,
shownPluginModalInfo,
pluginId,
extensionId,
widget?.id,
block?.id,
onModalClose,
]);

useEffect(() => {
const visible = shownPluginPopupInfo?.id === (widget?.id ?? block?.id);
if (popupVisible.current !== visible) {
popupVisible.current = visible;
if (!popupVisible.current) {
onPopupClose();
}
}
}, [
popupVisible,
shownPluginPopupInfo,
pluginId,
extensionId,
widget?.id,
block?.id,
onPopupClose,
]);

const onError = useCallback(
(err: any) => {
console.error(`plugin error from ${pluginId}/${extensionId}: `, err);
Expand Down Expand Up @@ -160,6 +188,8 @@ export function useAPI({
isMarshalable: Options["isMarshalable"] | undefined;
onPreInit: () => void;
onDispose: () => void;
onModalClose: () => void;
onPopupClose: () => void;
} {
const ctx = useContext();
const getLayer = useGet(layer);
Expand Down Expand Up @@ -348,10 +378,20 @@ export function useAPI({
event.current?.[1]("update");
}, [block, layer, widget, ctx?.reearth.scene.property]);

const onModalClose = useCallback(() => {
event.current?.[1]("modalclose");
}, []);

const onPopupClose = useCallback(() => {
event.current?.[1]("popupclose");
}, []);

return {
staticExposed,
isMarshalable,
onPreInit,
onDispose,
onModalClose,
onPopupClose,
};
}
2 changes: 2 additions & 0 deletions src/components/molecules/Visualizer/Plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export type ReearthEventType = {
wheel: [props: MouseEvent];
tick: [props: Date];
resize: [props: Viewport];
modalclose: [];
popupclose: [];
};

/** Access to the metadata of this plugin and extension currently executed. */
Expand Down