PromucFlow_constructor/app/client/src/resizable/index.tsx

295 lines
7.3 KiB
TypeScript
Raw Normal View History

import React, { ReactNode, useState, useEffect, forwardRef, Ref } from "react";
2020-02-21 12:16:49 +00:00
import styled, { StyledComponent } from "styled-components";
2020-02-18 19:56:58 +00:00
import { useDrag } from "react-use-gesture";
import { Spring } from "react-spring/renderprops";
2020-03-20 11:17:30 +00:00
const ResizeWrapper = styled.div<{ pevents: boolean }>`
2020-02-18 19:56:58 +00:00
display: block;
2020-03-20 11:17:30 +00:00
& {
* {
2020-12-24 04:32:25 +00:00
pointer-events: ${(props) => !props.pevents && "none"};
2020-03-20 11:17:30 +00:00
}
}
2020-02-18 19:56:58 +00:00
`;
const getSnappedValues = (
x: number,
y: number,
snapGrid: { x: number; y: number },
) => {
return {
x: Math.round(x / snapGrid.x) * snapGrid.x,
y: Math.round(y / snapGrid.y) * snapGrid.y,
};
};
type ResizableHandleProps = {
dragCallback: (x: number, y: number) => void;
component: StyledComponent<"div", Record<string, unknown>>;
onStart: () => void;
onStop: () => void;
2020-02-18 19:56:58 +00:00
snapGrid: {
x: number;
y: number;
};
};
function ResizableHandle(props: ResizableHandleProps) {
2020-02-18 19:56:58 +00:00
const bind = useDrag(
({ first, last, dragging, movement: [mx, my], memo }) => {
const snapped = getSnappedValues(mx, my, props.snapGrid);
if (dragging && memo && (snapped.x !== memo.x || snapped.y !== memo.y)) {
props.dragCallback(snapped.x, snapped.y);
}
if (first) {
props.onStart();
}
if (last) {
props.onStop();
}
return snapped;
},
);
2020-02-21 12:16:49 +00:00
return <props.component {...bind()} />;
}
2020-02-18 19:56:58 +00:00
type ResizableProps = {
handles: {
List Widget Phase 2 (#4189) * update meta properties + default properties map * update widget registery * update get meta property * update metahoc + widgetfactory + data tree evaluator * try sending function as string to worker * revert data tree evaluator update * pass default props map from dataTreeWidget file * wip * save child meta properties * remove console.log * save meta and default map in list * update listwidget * remove console.log + unused variables * revert getMetaPropertiesMap function * fix data tree test * fix list widget test * fix entity definition test * fix overriting of item in updatedItems * remove todo comments * fix meta prop issue * revert making meta properties from undefiend to "" & fix filepicker bug * fix test case * change items to listData and updatedItems to items * remove console.log * fix test * extract derived properties to dervied.js * disabled top, left, right resize handler list widget container * add test for dervied js * add test for selectedItem * fix background color bug on hover * remove console.log * fix chart widget inside list widget * fix checkbox issue + points raised by yogesh * revert the createImmerReducer usage * fix parse derived properties * remove internal props object that fails the test * fix import typo * allow bottom resize handler * fix template height check * fix template height check * update template size check * fix the is visible invalid prop issue * fix migration of list widget phase 2 * fix migration * remove unused import * fix migration * fix migration * remove console.log * hide delete option for container in entity explorer * fix testcases * remove unused import * fix switch widget meta prop Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-06-18 07:42:57 +00:00
left?: StyledComponent<"div", Record<string, unknown>>;
top?: StyledComponent<"div", Record<string, unknown>>;
bottom?: StyledComponent<"div", Record<string, unknown>>;
right?: StyledComponent<"div", Record<string, unknown>>;
bottomRight?: StyledComponent<"div", Record<string, unknown>>;
topLeft?: StyledComponent<"div", Record<string, unknown>>;
topRight?: StyledComponent<"div", Record<string, unknown>>;
bottomLeft?: StyledComponent<"div", Record<string, unknown>>;
2020-02-18 19:56:58 +00:00
};
componentWidth: number;
componentHeight: number;
children: ReactNode;
onStart: () => void;
onStop: (
size: { width: number; height: number },
position: { x: number; y: number },
) => void;
2020-02-18 19:56:58 +00:00
snapGrid: { x: number; y: number };
enable: boolean;
isColliding: (
size: { width: number; height: number },
position: { x: number; y: number },
) => boolean;
className?: string;
2020-02-18 19:56:58 +00:00
};
export const Resizable = forwardRef(
(props: ResizableProps, ref: Ref<HTMLDivElement>) => {
const [pointerEvents, togglePointerEvents] = useState(true);
const [newDimensions, set] = useState({
2020-02-18 19:56:58 +00:00
width: props.componentWidth,
height: props.componentHeight,
x: 0,
y: 0,
reset: false,
2020-02-18 19:56:58 +00:00
});
const setNewDimensions = (rect: {
width: number;
height: number;
x: number;
y: number;
}) => {
const { height, width, x, y } = rect;
const isColliding = props.isColliding({ width, height }, { x, y });
if (!isColliding) {
set({ ...rect, reset: false });
}
};
useEffect(() => {
set({
width: props.componentWidth,
height: props.componentHeight,
x: 0,
y: 0,
reset: true,
});
}, [props.componentHeight, props.componentWidth]);
List Widget Phase 2 (#4189) * update meta properties + default properties map * update widget registery * update get meta property * update metahoc + widgetfactory + data tree evaluator * try sending function as string to worker * revert data tree evaluator update * pass default props map from dataTreeWidget file * wip * save child meta properties * remove console.log * save meta and default map in list * update listwidget * remove console.log + unused variables * revert getMetaPropertiesMap function * fix data tree test * fix list widget test * fix entity definition test * fix overriting of item in updatedItems * remove todo comments * fix meta prop issue * revert making meta properties from undefiend to "" & fix filepicker bug * fix test case * change items to listData and updatedItems to items * remove console.log * fix test * extract derived properties to dervied.js * disabled top, left, right resize handler list widget container * add test for dervied js * add test for selectedItem * fix background color bug on hover * remove console.log * fix chart widget inside list widget * fix checkbox issue + points raised by yogesh * revert the createImmerReducer usage * fix parse derived properties * remove internal props object that fails the test * fix import typo * allow bottom resize handler * fix template height check * fix template height check * update template size check * fix the is visible invalid prop issue * fix migration of list widget phase 2 * fix migration * remove unused import * fix migration * fix migration * remove console.log * hide delete option for container in entity explorer * fix testcases * remove unused import * fix switch widget meta prop Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-06-18 07:42:57 +00:00
const handles = [];
if (props.handles.left) {
handles.push({
dragCallback: (x: number) => {
setNewDimensions({
width: props.componentWidth - x,
height: newDimensions.height,
x,
y: newDimensions.y,
});
},
component: props.handles.left,
List Widget Phase 2 (#4189) * update meta properties + default properties map * update widget registery * update get meta property * update metahoc + widgetfactory + data tree evaluator * try sending function as string to worker * revert data tree evaluator update * pass default props map from dataTreeWidget file * wip * save child meta properties * remove console.log * save meta and default map in list * update listwidget * remove console.log + unused variables * revert getMetaPropertiesMap function * fix data tree test * fix list widget test * fix entity definition test * fix overriting of item in updatedItems * remove todo comments * fix meta prop issue * revert making meta properties from undefiend to "" & fix filepicker bug * fix test case * change items to listData and updatedItems to items * remove console.log * fix test * extract derived properties to dervied.js * disabled top, left, right resize handler list widget container * add test for dervied js * add test for selectedItem * fix background color bug on hover * remove console.log * fix chart widget inside list widget * fix checkbox issue + points raised by yogesh * revert the createImmerReducer usage * fix parse derived properties * remove internal props object that fails the test * fix import typo * allow bottom resize handler * fix template height check * fix template height check * update template size check * fix the is visible invalid prop issue * fix migration of list widget phase 2 * fix migration * remove unused import * fix migration * fix migration * remove console.log * hide delete option for container in entity explorer * fix testcases * remove unused import * fix switch widget meta prop Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-06-18 07:42:57 +00:00
});
}
if (props.handles.right) {
handles.push({
dragCallback: (x: number) => {
setNewDimensions({
width: props.componentWidth + x,
height: newDimensions.height,
x: newDimensions.x,
y: newDimensions.y,
});
},
component: props.handles.right,
List Widget Phase 2 (#4189) * update meta properties + default properties map * update widget registery * update get meta property * update metahoc + widgetfactory + data tree evaluator * try sending function as string to worker * revert data tree evaluator update * pass default props map from dataTreeWidget file * wip * save child meta properties * remove console.log * save meta and default map in list * update listwidget * remove console.log + unused variables * revert getMetaPropertiesMap function * fix data tree test * fix list widget test * fix entity definition test * fix overriting of item in updatedItems * remove todo comments * fix meta prop issue * revert making meta properties from undefiend to "" & fix filepicker bug * fix test case * change items to listData and updatedItems to items * remove console.log * fix test * extract derived properties to dervied.js * disabled top, left, right resize handler list widget container * add test for dervied js * add test for selectedItem * fix background color bug on hover * remove console.log * fix chart widget inside list widget * fix checkbox issue + points raised by yogesh * revert the createImmerReducer usage * fix parse derived properties * remove internal props object that fails the test * fix import typo * allow bottom resize handler * fix template height check * fix template height check * update template size check * fix the is visible invalid prop issue * fix migration of list widget phase 2 * fix migration * remove unused import * fix migration * fix migration * remove console.log * hide delete option for container in entity explorer * fix testcases * remove unused import * fix switch widget meta prop Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-06-18 07:42:57 +00:00
});
}
if (props.handles.bottom) {
handles.push({
dragCallback: (x: number, y: number) => {
setNewDimensions({
width: newDimensions.width,
height: props.componentHeight + y,
x: newDimensions.x,
y: newDimensions.y,
});
},
component: props.handles.bottom,
List Widget Phase 2 (#4189) * update meta properties + default properties map * update widget registery * update get meta property * update metahoc + widgetfactory + data tree evaluator * try sending function as string to worker * revert data tree evaluator update * pass default props map from dataTreeWidget file * wip * save child meta properties * remove console.log * save meta and default map in list * update listwidget * remove console.log + unused variables * revert getMetaPropertiesMap function * fix data tree test * fix list widget test * fix entity definition test * fix overriting of item in updatedItems * remove todo comments * fix meta prop issue * revert making meta properties from undefiend to "" & fix filepicker bug * fix test case * change items to listData and updatedItems to items * remove console.log * fix test * extract derived properties to dervied.js * disabled top, left, right resize handler list widget container * add test for dervied js * add test for selectedItem * fix background color bug on hover * remove console.log * fix chart widget inside list widget * fix checkbox issue + points raised by yogesh * revert the createImmerReducer usage * fix parse derived properties * remove internal props object that fails the test * fix import typo * allow bottom resize handler * fix template height check * fix template height check * update template size check * fix the is visible invalid prop issue * fix migration of list widget phase 2 * fix migration * remove unused import * fix migration * fix migration * remove console.log * hide delete option for container in entity explorer * fix testcases * remove unused import * fix switch widget meta prop Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-06-18 07:42:57 +00:00
});
}
if (props.handles.bottomRight) {
handles.push({
dragCallback: (x: number, y: number) => {
setNewDimensions({
width: props.componentWidth + x,
height: props.componentHeight + y,
x: newDimensions.x,
y: newDimensions.y,
});
},
component: props.handles.bottomRight,
List Widget Phase 2 (#4189) * update meta properties + default properties map * update widget registery * update get meta property * update metahoc + widgetfactory + data tree evaluator * try sending function as string to worker * revert data tree evaluator update * pass default props map from dataTreeWidget file * wip * save child meta properties * remove console.log * save meta and default map in list * update listwidget * remove console.log + unused variables * revert getMetaPropertiesMap function * fix data tree test * fix list widget test * fix entity definition test * fix overriting of item in updatedItems * remove todo comments * fix meta prop issue * revert making meta properties from undefiend to "" & fix filepicker bug * fix test case * change items to listData and updatedItems to items * remove console.log * fix test * extract derived properties to dervied.js * disabled top, left, right resize handler list widget container * add test for dervied js * add test for selectedItem * fix background color bug on hover * remove console.log * fix chart widget inside list widget * fix checkbox issue + points raised by yogesh * revert the createImmerReducer usage * fix parse derived properties * remove internal props object that fails the test * fix import typo * allow bottom resize handler * fix template height check * fix template height check * update template size check * fix the is visible invalid prop issue * fix migration of list widget phase 2 * fix migration * remove unused import * fix migration * fix migration * remove console.log * hide delete option for container in entity explorer * fix testcases * remove unused import * fix switch widget meta prop Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-06-18 07:42:57 +00:00
});
}
if (props.handles.bottomLeft) {
handles.push({
dragCallback: (x: number, y: number) => {
setNewDimensions({
width: props.componentWidth - x,
height: props.componentHeight + y,
x,
y: newDimensions.y,
});
},
component: props.handles.bottomLeft,
List Widget Phase 2 (#4189) * update meta properties + default properties map * update widget registery * update get meta property * update metahoc + widgetfactory + data tree evaluator * try sending function as string to worker * revert data tree evaluator update * pass default props map from dataTreeWidget file * wip * save child meta properties * remove console.log * save meta and default map in list * update listwidget * remove console.log + unused variables * revert getMetaPropertiesMap function * fix data tree test * fix list widget test * fix entity definition test * fix overriting of item in updatedItems * remove todo comments * fix meta prop issue * revert making meta properties from undefiend to "" & fix filepicker bug * fix test case * change items to listData and updatedItems to items * remove console.log * fix test * extract derived properties to dervied.js * disabled top, left, right resize handler list widget container * add test for dervied js * add test for selectedItem * fix background color bug on hover * remove console.log * fix chart widget inside list widget * fix checkbox issue + points raised by yogesh * revert the createImmerReducer usage * fix parse derived properties * remove internal props object that fails the test * fix import typo * allow bottom resize handler * fix template height check * fix template height check * update template size check * fix the is visible invalid prop issue * fix migration of list widget phase 2 * fix migration * remove unused import * fix migration * fix migration * remove console.log * hide delete option for container in entity explorer * fix testcases * remove unused import * fix switch widget meta prop Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-06-18 07:42:57 +00:00
});
}
if (props.handles.topRight) {
handles.push({
dragCallback: (x: number, y: number) => {
setNewDimensions({
width: props.componentWidth + x,
height: props.componentHeight - y,
x: newDimensions.x,
y: y,
});
},
component: props.handles.topRight,
List Widget Phase 2 (#4189) * update meta properties + default properties map * update widget registery * update get meta property * update metahoc + widgetfactory + data tree evaluator * try sending function as string to worker * revert data tree evaluator update * pass default props map from dataTreeWidget file * wip * save child meta properties * remove console.log * save meta and default map in list * update listwidget * remove console.log + unused variables * revert getMetaPropertiesMap function * fix data tree test * fix list widget test * fix entity definition test * fix overriting of item in updatedItems * remove todo comments * fix meta prop issue * revert making meta properties from undefiend to "" & fix filepicker bug * fix test case * change items to listData and updatedItems to items * remove console.log * fix test * extract derived properties to dervied.js * disabled top, left, right resize handler list widget container * add test for dervied js * add test for selectedItem * fix background color bug on hover * remove console.log * fix chart widget inside list widget * fix checkbox issue + points raised by yogesh * revert the createImmerReducer usage * fix parse derived properties * remove internal props object that fails the test * fix import typo * allow bottom resize handler * fix template height check * fix template height check * update template size check * fix the is visible invalid prop issue * fix migration of list widget phase 2 * fix migration * remove unused import * fix migration * fix migration * remove console.log * hide delete option for container in entity explorer * fix testcases * remove unused import * fix switch widget meta prop Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-06-18 07:42:57 +00:00
});
}
if (props.handles.topLeft) {
handles.push({
dragCallback: (x: number, y: number) => {
setNewDimensions({
width: props.componentWidth - x,
height: props.componentHeight - y,
x: x,
y: y,
});
},
component: props.handles.topLeft,
List Widget Phase 2 (#4189) * update meta properties + default properties map * update widget registery * update get meta property * update metahoc + widgetfactory + data tree evaluator * try sending function as string to worker * revert data tree evaluator update * pass default props map from dataTreeWidget file * wip * save child meta properties * remove console.log * save meta and default map in list * update listwidget * remove console.log + unused variables * revert getMetaPropertiesMap function * fix data tree test * fix list widget test * fix entity definition test * fix overriting of item in updatedItems * remove todo comments * fix meta prop issue * revert making meta properties from undefiend to "" & fix filepicker bug * fix test case * change items to listData and updatedItems to items * remove console.log * fix test * extract derived properties to dervied.js * disabled top, left, right resize handler list widget container * add test for dervied js * add test for selectedItem * fix background color bug on hover * remove console.log * fix chart widget inside list widget * fix checkbox issue + points raised by yogesh * revert the createImmerReducer usage * fix parse derived properties * remove internal props object that fails the test * fix import typo * allow bottom resize handler * fix template height check * fix template height check * update template size check * fix the is visible invalid prop issue * fix migration of list widget phase 2 * fix migration * remove unused import * fix migration * fix migration * remove console.log * hide delete option for container in entity explorer * fix testcases * remove unused import * fix switch widget meta prop Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-06-18 07:42:57 +00:00
});
}
if (props.handles.top) {
handles.push({
dragCallback: (x: number, y: number) => {
setNewDimensions({
width: newDimensions.width,
height: props.componentHeight - y,
y: y,
x: newDimensions.x,
});
},
component: props.handles.top,
});
}
2020-02-18 19:56:58 +00:00
const onResizeStop = () => {
togglePointerEvents(true);
props.onStop(
{
width: newDimensions.width,
height: newDimensions.height,
},
{
x: newDimensions.x,
y: newDimensions.y,
},
);
};
const renderHandles = handles.map((handle, index) => (
<ResizableHandle
{...handle}
key={index}
onStart={() => {
togglePointerEvents(false);
props.onStart();
}}
onStop={onResizeStop}
snapGrid={props.snapGrid}
/>
));
return (
<Spring
config={{
clamp: true,
friction: 0,
tension: 999,
}}
from={{
width: props.componentWidth,
height: props.componentHeight,
}}
immediate={newDimensions.reset ? true : false}
to={{
width: newDimensions.width,
height: newDimensions.height,
transform: `translate3d(${newDimensions.x}px,${newDimensions.y}px,0)`,
}}
>
2020-12-24 04:32:25 +00:00
{(_props) => (
<ResizeWrapper
className={props.className}
pevents={pointerEvents}
ref={ref}
style={_props}
>
{props.children}
{props.enable && renderHandles}
</ResizeWrapper>
)}
</Spring>
);
},
);
2020-02-18 19:56:58 +00:00
export default Resizable;