2019-10-01 20:07:43 +00:00
|
|
|
import React, { useContext } from "react";
|
2019-09-22 20:25:05 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { Resizable, ResizeDirection } from "re-resizable";
|
|
|
|
|
import { WidgetProps, WidgetOperations } from "../widgets/BaseWidget";
|
2019-10-01 20:07:43 +00:00
|
|
|
import { ContainerProps, ParentBoundsContext } from "./ContainerComponent";
|
2019-09-22 20:25:05 +00:00
|
|
|
|
|
|
|
|
export type ResizableComponentProps = WidgetProps & ContainerProps;
|
|
|
|
|
|
|
|
|
|
const ResizableContainer = styled(Resizable)`
|
2019-10-01 20:07:43 +00:00
|
|
|
position: relative;
|
|
|
|
|
z-index: 10;
|
2019-09-22 20:25:05 +00:00
|
|
|
border: ${props => {
|
|
|
|
|
return Object.values(props.theme.borders[0]).join(" ");
|
|
|
|
|
}};
|
2019-10-01 20:07:43 +00:00
|
|
|
&:after,
|
|
|
|
|
&:before {
|
|
|
|
|
content: "";
|
|
|
|
|
position: absolute;
|
|
|
|
|
width: 8px;
|
|
|
|
|
height: 8px;
|
|
|
|
|
border-radius: 50%;
|
|
|
|
|
z-index: 9;
|
|
|
|
|
background: ${props => props.theme.colors.containerBorder};
|
|
|
|
|
}
|
|
|
|
|
&:after {
|
|
|
|
|
right: -4px;
|
|
|
|
|
top: 50%;
|
|
|
|
|
}
|
2019-09-22 20:25:05 +00:00
|
|
|
|
2019-10-01 20:07:43 +00:00
|
|
|
&:before {
|
|
|
|
|
left: calc(50%);
|
|
|
|
|
top: calc(100% - 4px);
|
2019-09-30 03:25:14 +00:00
|
|
|
}
|
|
|
|
|
`;
|
2019-09-22 20:25:05 +00:00
|
|
|
|
|
|
|
|
export const ResizableComponent = (props: ResizableComponentProps) => {
|
2019-10-01 20:07:43 +00:00
|
|
|
const { boundingParent } = useContext(ParentBoundsContext);
|
2019-09-22 20:25:05 +00:00
|
|
|
const updateSize = (
|
|
|
|
|
e: Event,
|
|
|
|
|
dir: ResizeDirection,
|
|
|
|
|
ref: any,
|
|
|
|
|
delta: { width: number; height: number },
|
|
|
|
|
) => {
|
|
|
|
|
props.updateWidget &&
|
|
|
|
|
props.updateWidget(WidgetOperations.RESIZE, props.widgetId, delta);
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<ResizableContainer
|
|
|
|
|
size={{
|
2019-09-24 12:36:03 +00:00
|
|
|
width: props.style.componentWidth as number,
|
|
|
|
|
height: props.style.componentHeight as number,
|
2019-09-22 20:25:05 +00:00
|
|
|
}}
|
|
|
|
|
style={{ ...props.style }}
|
|
|
|
|
onResizeStop={updateSize}
|
2019-09-25 21:38:03 +00:00
|
|
|
grid={[props.parentColumnSpace, props.parentRowSpace]}
|
2019-10-01 20:07:43 +00:00
|
|
|
bounds={boundingParent ? boundingParent.current || undefined : "window"}
|
2019-09-25 21:38:03 +00:00
|
|
|
enable={{
|
2019-10-01 20:07:43 +00:00
|
|
|
top: false,
|
2019-09-25 21:38:03 +00:00
|
|
|
right: true,
|
|
|
|
|
bottom: true,
|
2019-10-01 20:07:43 +00:00
|
|
|
left: false,
|
2019-09-25 21:38:03 +00:00
|
|
|
topRight: false,
|
|
|
|
|
topLeft: false,
|
2019-10-01 20:07:43 +00:00
|
|
|
bottomRight: false,
|
|
|
|
|
bottomLeft: false,
|
2019-09-25 21:38:03 +00:00
|
|
|
}}
|
2019-09-22 20:25:05 +00:00
|
|
|
>
|
|
|
|
|
{props.children}
|
|
|
|
|
</ResizableContainer>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ResizableComponent;
|