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

Commit

Permalink
support selecting blocks with plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jan 21, 2022
1 parent 1054287 commit ca21eb4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/components/atoms/Plugin/IFrame/hooks.ts
Expand Up @@ -23,6 +23,7 @@ export default function useHook({
iFrameProps,
onLoad,
onMessage,
onClick,
}: {
autoResizeMessageKey?: string;
html?: string;
Expand All @@ -32,6 +33,7 @@ export default function useHook({
iFrameProps?: IframeHTMLAttributes<HTMLIFrameElement>;
onLoad?: () => void;
onMessage?: (message: any) => void;
onClick?: () => void;
} = {}): {
ref: RefObject<HTMLIFrameElement>;
props: IframeHTMLAttributes<HTMLIFrameElement>;
Expand Down Expand Up @@ -145,6 +147,18 @@ export default function useHook({
[autoResize, iFrameProps, iFrameSize, visible],
);

useEffect(() => {
const handleBlur = () => {
if (iFrameRef.current && iFrameRef.current === document.activeElement) {
onClick?.();
}
};
window.addEventListener("blur", handleBlur);
return () => {
window.removeEventListener("blur", handleBlur);
};
}, [onClick]);

return {
ref: iFrameRef,
props,
Expand Down
4 changes: 3 additions & 1 deletion src/components/atoms/Plugin/IFrame/index.tsx
Expand Up @@ -12,10 +12,11 @@ export type Props = {
iFrameProps?: IframeHTMLAttributes<HTMLIFrameElement>;
onLoad?: () => void;
onMessage?: (message: any) => void;
onClick?: () => void;
};

const IFrame: React.ForwardRefRenderFunction<Ref, Props> = (
{ autoResize, className, html, visible, iFrameProps, onLoad, onMessage },
{ autoResize, className, html, visible, iFrameProps, onLoad, onMessage, onClick },
ref,
) => {
const {
Expand All @@ -30,6 +31,7 @@ const IFrame: React.ForwardRefRenderFunction<Ref, Props> = (
ref,
onLoad,
onMessage,
onClick,
});

return html ? (
Expand Down
3 changes: 3 additions & 0 deletions src/components/atoms/Plugin/index.tsx
Expand Up @@ -21,6 +21,7 @@ export type Props = {
onPreInit?: () => void;
onError?: (err: any) => void;
onDispose?: () => void;
onClick?: () => void;
};

const Plugin: React.FC<Props> = ({
Expand All @@ -38,6 +39,7 @@ const Plugin: React.FC<Props> = ({
onPreInit,
onError,
onDispose,
onClick,
}) => {
const { iFrameRef, iFrameHtml, iFrameVisible } = useHook({
iframeCanBeVisible: canBeVisible,
Expand All @@ -60,6 +62,7 @@ const Plugin: React.FC<Props> = ({
visible={iFrameVisible}
onMessage={onMessage}
iFrameProps={iFrameProps}
onClick={onClick}
/>
) : renderPlaceholder ? (
<>{renderPlaceholder}</>
Expand Down
3 changes: 2 additions & 1 deletion src/components/molecules/Visualizer/Block/index.tsx
Expand Up @@ -54,6 +54,7 @@ export default function BlockComponent<P = any, PP = any>({
pluginProperty={props.pluginProperty}
layer={props.layer}
block={props.block}
onClick={props.onClick}
/>
</Wrapper>
);
Expand All @@ -62,7 +63,7 @@ export default function BlockComponent<P = any, PP = any>({
const Wrapper = styled.div<{ editable?: boolean; selected?: boolean }>`
border: 1px solid
${({ selected, editable, theme }) =>
editable && selected ? theme.infoBox.accent2 : "transparent"};
editable && selected ? theme.infoBox.accent2 : "transparent"};
border-radius: 6px;
&:hover {
Expand Down
3 changes: 3 additions & 0 deletions src/components/molecules/Visualizer/Plugin/index.tsx
Expand Up @@ -31,6 +31,7 @@ export type Props = {
layer?: Layer;
widget?: Widget;
block?: Block;
onClick?: () => void;
};

export default function Plugin({
Expand All @@ -46,6 +47,7 @@ export default function Plugin({
widget,
block,
pluginProperty,
onClick,
}: Props): JSX.Element | null {
const { skip, src, isMarshalable, onPreInit, onDispose, exposed, onError, onMessage } = useHooks({
pluginId,
Expand All @@ -72,6 +74,7 @@ export default function Plugin({
onMessage={onMessage}
onPreInit={onPreInit}
onDispose={onDispose}
onClick={onClick}
/>
) : null;
}

0 comments on commit ca21eb4

Please sign in to comment.