Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update provider funcions #106

Merged
merged 1 commit into from
Aug 1, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/core/providers/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ShapeModel, ShapeRefs, ShapeType } from '@/core/model';
import { Coord, ShapeModel, ShapeRefs, ShapeType, Size } from '@/core/model';
import Konva from 'konva';
import { Node, NodeConfig } from 'konva/lib/Node';

Expand All @@ -22,9 +22,10 @@ export interface SelectionInfo {

export interface CanvasContextModel {
shapes: ShapeModel[];
setShapes: React.Dispatch<React.SetStateAction<ShapeModel[]>>;
scale: number;
setScale: React.Dispatch<React.SetStateAction<number>>;

addNewShape: (type: ShapeType, x: number, y: number) => void;
updateShapeSizeAndPosition: (id: string, position: Coord, size: Size) => void;
updateShapePosition: (id: string, position: Coord) => void;
selectionInfo: SelectionInfo;
}
28 changes: 26 additions & 2 deletions src/core/providers/canvas/canvas.provider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import { ShapeModel } from '@/core/model';
import { Coord, ShapeModel, ShapeType, Size } from '@/core/model';
import { CanvasContext } from './canvas.context';
import { useSelection } from './use-selection.hook';
import { createShape } from '@/pods/canvas/canvas.model';

interface Props {
children: React.ReactNode;
Expand All @@ -13,15 +14,38 @@ export const CanvasProvider: React.FC<Props> = props => {
const [scale, setScale] = React.useState(1);

const selectionInfo = useSelection(shapes, setShapes);
const addNewShape = (type: ShapeType, x: number, y: number) => {
setShapes(shapes => [...shapes, createShape({ x, y }, type)]);
};

const updateShapeSizeAndPosition = (
id: string,
position: Coord,
size: Size
) => {
setShapes(prevShapes =>
prevShapes.map(shape =>
shape.id === id ? { ...shape, ...position, ...size } : shape
)
);
};

const updateShapePosition = (id: string, { x, y }: Coord) => {
setShapes(prevShapes =>
prevShapes.map(shape => (shape.id === id ? { ...shape, x, y } : shape))
);
};

return (
<CanvasContext.Provider
value={{
shapes,
setShapes,
scale,
setScale,
selectionInfo,
addNewShape,
updateShapeSizeAndPosition,
updateShapePosition,
}}
>
{children}
Expand Down
17 changes: 11 additions & 6 deletions src/pods/canvas/canvas.pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import classes from './canvas.pod.module.css';
import { EditableComponent } from '@/common/components/inline-edit';

export const CanvasPod = () => {
const { shapes, setShapes, scale, selectionInfo } = useCanvasContext();
const {
shapes,
scale,
selectionInfo,
addNewShape,
updateShapeSizeAndPosition,
updateShapePosition,
} = useCanvasContext();

const {
shapeRefs,
Expand All @@ -24,10 +31,10 @@ export const CanvasPod = () => {
} = selectionInfo;

const { isDraggedOver, dropRef } = useDropShape();
const { stageRef } = useMonitorShape(dropRef, setShapes);
const { stageRef } = useMonitorShape(dropRef, addNewShape);

const { handleTransform, handleTransformerBoundBoxFunc } = useTransform(
setShapes,
updateShapeSizeAndPosition,
{
selectedShapeRef,
selectedShapeId,
Expand All @@ -38,9 +45,7 @@ export const CanvasPod = () => {
const handleDragEnd =
(id: string) => (e: Konva.KonvaEventObject<DragEvent>) => {
const { x, y } = e.target.position();
setShapes(prevShapes =>
prevShapes.map(shape => (shape.id === id ? { ...shape, x, y } : shape))
);
updateShapePosition(id, { x, y });
};

{
Expand Down
13 changes: 4 additions & 9 deletions src/pods/canvas/use-monitor-shape.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import {
convertFromDivElementCoordsToKonvaCoords,
} from './canvas.util';
import Konva from 'konva';
import { createShape } from './canvas.model';
import { ShapeModel } from '@/core/model';
import { ShapeType } from '@/core/model';

export const useMonitorShape = (
dropRef: React.MutableRefObject<null>,
setShapes: React.Dispatch<React.SetStateAction<ShapeModel[]>>
addNewShape: (type: ShapeType, x: number, y: number) => void
) => {
const stageRef = useRef<Konva.Stage>(null);

Expand All @@ -22,7 +21,7 @@ export const useMonitorShape = (
const destination = location.current.dropTargets[0];
invariant(destination);

const type = source.data.type;
const type = source.data.type as ShapeType;
const screenPosition =
extractScreenCoordinatesFromPragmaticLocation(location);

Expand Down Expand Up @@ -50,11 +49,7 @@ export const useMonitorShape = (
positionX = konvaCoord.x;
positionY = konvaCoord.y;
}

setShapes(shapes => [
...shapes,
createShape({ x: positionX, y: positionY }, type as any),
]);
addNewShape(type, positionX, positionY);
},
});
}, []);
Expand Down
16 changes: 2 additions & 14 deletions src/pods/canvas/use-transform.hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Node, NodeConfig } from 'konva/lib/Node';
import { Box } from 'konva/lib/shapes/Transformer';
import { Coord, ShapeModel, ShapeType, Size } from '@/core/model';
import { Coord, ShapeType, Size } from '@/core/model';

interface TransFormSelectedInfo {
selectedShapeRef: React.MutableRefObject<Node<NodeConfig> | null>;
Expand All @@ -9,23 +9,11 @@ interface TransFormSelectedInfo {
}

export const useTransform = (
setShapes: (value: React.SetStateAction<ShapeModel[]>) => void,
updateShapeSizeAndPosition: (id: string, position: Coord, size: Size) => void,
transformSelectedInfo: TransFormSelectedInfo
) => {
const { selectedShapeId, selectedShapeRef } = transformSelectedInfo;

const updateShapeSizeAndPosition = (
id: string,
position: Coord,
size: Size
) => {
setShapes(prevShapes =>
prevShapes.map(shape =>
shape.id === id ? { ...shape, ...position, ...size } : shape
)
);
};

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