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

refactor selection in canvas provider #81

Merged
merged 1 commit into from
Jul 30, 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
16 changes: 10 additions & 6 deletions src/core/providers/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { ShapeModel, ShapeRefs, ShapeType } from '@/core/model';
import Konva from 'konva';
import { Node, NodeConfig } from 'konva/lib/Node';

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

export interface SelectionInfo {
transformerRef: React.RefObject<Konva.Transformer>;
shapeRefs: React.MutableRefObject<ShapeRefs>;
handleSelected: (id: string, type: ShapeType) => void;
Expand All @@ -20,3 +15,12 @@ export interface CanvasContextModel {
selectedShapeId: string;
selectedShapeType: ShapeType | null;
}

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

selectionInfo: SelectionInfo;
}
18 changes: 2 additions & 16 deletions src/core/providers/canvas/canvas.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ export const CanvasProvider: React.FC<Props> = props => {
const [shapes, setShapes] = React.useState<ShapeModel[]>([]);
const [scale, setScale] = React.useState(1);

const {
shapeRefs,
transformerRef,
handleSelected,
handleClearSelection,
selectedShapeRef,
selectedShapeId,
selectedShapeType,
} = useSelection(shapes);
const selectionInfo = useSelection(shapes);

return (
<CanvasContext.Provider
Expand All @@ -29,13 +21,7 @@ export const CanvasProvider: React.FC<Props> = props => {
setShapes,
scale,
setScale,
shapeRefs,
transformerRef,
handleSelected,
handleClearSelection,
selectedShapeRef,
selectedShapeId,
selectedShapeType,
selectionInfo,
}}
>
{children}
Expand Down
3 changes: 2 additions & 1 deletion src/core/providers/canvas/use-selection.hook.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useEffect, useRef, useState } from 'react';
import Konva from 'konva';
import { ShapeModel, ShapeRefs, ShapeType } from '@/core/model';
import { SelectionInfo } from './canvas.model';

export const useSelection = (shapes: ShapeModel[]) => {
export const useSelection = (shapes: ShapeModel[]): SelectionInfo => {
const transformerRef = useRef<Konva.Transformer>(null);
const shapeRefs = useRef<ShapeRefs>({});
const selectedShapeRef = useRef<Konva.Node | null>(null);
Expand Down
7 changes: 3 additions & 4 deletions src/pods/canvas/canvas.pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ import {
import { ShapeModel } from '@/core/model';

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

const {
shapes,
setShapes,
scale,
shapeRefs,
transformerRef,
handleSelected,
handleClearSelection,
selectedShapeRef,
selectedShapeId,
selectedShapeType,
} = useCanvasContext();
} = selectionInfo;

const { isDraggedOver, dropRef } = useDropShape();
const { stageRef } = useMonitorShape(dropRef, setShapes);
Expand Down
Loading