Skip to content

Commit

Permalink
Merge pull request #173 from Lemoncode/feature/#150-create-line-shape…
Browse files Browse the repository at this point in the history
…-under-basic-shapes

#150 line shape created
  • Loading branch information
brauliodiez authored Aug 12, 2024
2 parents 608afc4 + 60a63b9 commit 7085ad2
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 19 deletions.
4 changes: 4 additions & 0 deletions public/shapes/line.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/common/components/front-basic-sapes/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './rectangle-basic-shape';
export * from './diamond-shape';
export * from './line-basic-shape';
51 changes: 51 additions & 0 deletions src/common/components/front-basic-sapes/line-basic-shape.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { forwardRef } from 'react';
import { Group, Line, Rect } from 'react-konva';
import { ShapeSizeRestrictions } from '@/core/model';
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions';
import { ShapeProps } from '../front-components/shape.model';

const lineShapeRestrictions: ShapeSizeRestrictions = {
minWidth: 50,
minHeight: 10,
maxWidth: -1,
maxHeight: 10,
defaultWidth: 200,
defaultHeight: 10,
};

export const getlineShapeRestrictions = (): ShapeSizeRestrictions =>
lineShapeRestrictions;

export const LineShape = forwardRef<any, ShapeProps>(
({ x, y, width, height, id, onSelected, text, ...shapeProps }, ref) => {
const { width: restrictedWidth, height: restrictedHeight } =
fitSizeToShapeSizeRestrictions(lineShapeRestrictions, width, height);

return (
<Group
x={x}
y={y}
ref={ref}
width={restrictedWidth}
height={restrictedHeight}
{...shapeProps}
onClick={() => onSelected(id, 'line')}
>
{/* Transparent rectangle for applying margin */}
<Rect
width={restrictedWidth}
height={restrictedHeight}
fill="transparent"
/>

<Line
x={0}
y={restrictedHeight / 2}
points={[0, 0, restrictedWidth, 0]}
stroke="black"
strokeWidth={2}
/>
</Group>
);
}
);
2 changes: 2 additions & 0 deletions src/core/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type ShapeType =
| 'rectangle'
| 'videoPlayer'
| 'diamond'
| 'line';
| 'accordion';
/* | "text"| "button" | "radio" | "image"*/

Expand Down Expand Up @@ -54,6 +55,7 @@ export interface ShapeModel {
height: number;
type: ShapeType;
allowsInlineEdition: boolean;
hasLateralTransformer: boolean;
editType?: EditType;
text?: string;
}
1 change: 1 addition & 0 deletions src/pods/basic-shapes-gallery/basic-gallery-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { ItemInfo } from '@/common/components/gallery/components/model';
export const mockBasicShapesCollection: ItemInfo[] = [
{ thumbnailSrc: '/shapes/rectangle.svg', type: 'rectangle' },
{ thumbnailSrc: '/shapes/diamond.svg', type: 'diamond' },
{ thumbnailSrc: '/shapes/line.svg', type: 'line' },
];
16 changes: 16 additions & 0 deletions src/pods/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getLabelSizeRestrictions } from '@/common/components/front-components/l
import {
getDiamondShapeSizeRestrictions,
getRectangleShapeSizeRestrictions,
getlineShapeRestrictions,
} from '@/common/components/front-basic-sapes';
import {
getAccordionShapeSizeRestrictions,
Expand Down Expand Up @@ -110,6 +111,11 @@ export const getDefaultSizeFromShape = (shapeType: ShapeType): Size => {
width: getDiamondShapeSizeRestrictions().defaultWidth,
height: getDiamondShapeSizeRestrictions().defaultHeight,
};
case 'line':
return {
width: getlineShapeRestrictions().defaultWidth,
height: getlineShapeRestrictions().defaultHeight,
};
case 'accordion':
return {
width: getAccordionShapeSizeRestrictions().defaultWidth,
Expand Down Expand Up @@ -139,6 +145,15 @@ const doesShapeAllowInlineEdition = (shapeType: ShapeType): boolean => {
}
};

const doesShapeHaveLateralTransformer = (shapeType: ShapeType): boolean => {
switch (shapeType) {
case 'line':
return true;
default:
return false;
}
};

const generateDefaultTextValue = (shapeType: ShapeType): string | undefined => {
switch (shapeType) {
case 'input':
Expand Down Expand Up @@ -189,6 +204,7 @@ export const createShape = (coord: Coord, shapeType: ShapeType): ShapeModel => {
height,
type: shapeType,
allowsInlineEdition: doesShapeAllowInlineEdition(shapeType),
hasLateralTransformer: doesShapeHaveLateralTransformer(shapeType),
text: generateDefaultTextValue(shapeType),
editType: getShapeEditInlineType(shapeType),
};
Expand Down
7 changes: 1 addition & 6 deletions src/pods/canvas/canvas.pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ export const CanvasPod = () => {
} = useSnapIn(transformerRef, selectedShapeKonvaId);

const { handleTransform, handleTransformerBoundBoxFunc } = useTransform(
updateShapeSizeAndPosition,
{
selectedShapeRef,
selectedShapeId,
selectedShapeType,
}
updateShapeSizeAndPosition
);

const handleDragEnd =
Expand Down
10 changes: 8 additions & 2 deletions src/pods/canvas/shape-renderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import {
renderMobilePhoneContainer,
renderTablet,
} from './simple-container';
import { renderRectangle } from './simple-basic-shapes/rectangle.rerender';
import { renderVideoPlayer } from './simple-rich-components';
import { renderDiamond } from './simple-basic-shapes';
import {
renderDiamond,
renderRectangle,
renderLine,
} from './simple-basic-shapes';
import { renderAccordion } from './simple-rich-components/accordion.renderer';


export const renderShapeComponent = (
shape: ShapeModel,
shapeRenderedProps: ShapeRendererProps
Expand Down Expand Up @@ -66,6 +70,8 @@ export const renderShapeComponent = (
return renderVideoPlayer(shape, shapeRenderedProps);
case 'diamond':
return renderDiamond(shape, shapeRenderedProps);
case 'line':
return renderLine(shape, shapeRenderedProps);
case 'accordion':
return renderAccordion(shape, shapeRenderedProps);
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './rectangle.rerender';
export * from './diamond.renderer';
export * from './line.renderer';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { LineShape } from '@/common/components/front-basic-sapes';
import { ShapeRendererProps } from '../model';
import { ShapeModel } from '@/core/model';

export const renderLine = (
shape: ShapeModel,
shapeRenderedProps: ShapeRendererProps
) => {
const { handleSelected, shapeRefs, handleDragEnd, handleTransform } =
shapeRenderedProps;

return (
<LineShape
id={shape.id}
key={shape.id}
ref={shapeRefs.current[shape.id]}
x={shape.x}
y={shape.y}
name="shape"
width={shape.width}
height={shape.height}
draggable
hasLateralTransformer={shape.hasLateralTransformer}
onSelected={handleSelected}
onDragEnd={handleDragEnd(shape.id)}
onTransform={handleTransform}
onTransformEnd={handleTransform}
/>
);
};
39 changes: 28 additions & 11 deletions src/pods/canvas/use-transform.hook.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import { Node, NodeConfig } from 'konva/lib/Node';
import { Box } from 'konva/lib/shapes/Transformer';
import { Coord, ShapeType, Size } from '@/core/model';

interface TransFormSelectedInfo {
selectedShapeRef: React.MutableRefObject<Node<NodeConfig> | null>;
selectedShapeId: string;
selectedShapeType: ShapeType | null;
}
import { Coord, Size } from '@/core/model';
import { useEffect } from 'react';
import { useCanvasContext } from '@/core/providers';

export const useTransform = (
updateShapeSizeAndPosition: (id: string, position: Coord, size: Size) => void,
transformSelectedInfo: TransFormSelectedInfo
updateShapeSizeAndPosition: (id: string, position: Coord, size: Size) => void
) => {
const { selectedShapeId, selectedShapeRef } = transformSelectedInfo;
const { selectedShapeId, selectedShapeRef, transformerRef } =
useCanvasContext().selectionInfo;

useEffect(() => {
const selectedShape = selectedShapeRef.current;
const transformer = transformerRef.current;
if (selectedShape && transformer) {
const hasLateralTransformer = selectedShape.attrs.hasLateralTransformer;
if (hasLateralTransformer) {
transformerRef.current.enabledAnchors(['middle-left', 'middle-right']);
} else {
transformerRef.current.enabledAnchors([
'top-left',
'top-center',
'top-right',
'middle-left',
'middle-right',
'bottom-left',
'bottom-center',
'bottom-right',
]);
}
}
}, [selectedShapeId]);

const handleTransform = () => {
const node = selectedShapeRef.current;
Expand Down

0 comments on commit 7085ad2

Please sign in to comment.