Skip to content

Commit

Permalink
Merge pull request #72 from Lemoncode/feature/#52-Move-data-to-global…
Browse files Browse the repository at this point in the history
…-provider

Implemented canvas provider
  • Loading branch information
brauliodiez authored Jul 29, 2024
2 parents ca8679d + 16ad78f commit d6c64e6
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/core/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ export type ShapeType =
| 'browser'
| 'timepickerinput';
/* | "text"| "button" | "radio" | "image"*/

export interface ShapeModel {
id: string;
x: number;
y: number;
width: number;
height: number;
type: ShapeType;
}
6 changes: 6 additions & 0 deletions src/core/providers/canvas/canvas.context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import { CanvasContextModel } from './canvas.model';

export const CanvasContext = React.createContext<CanvasContextModel | null>(
null
);
6 changes: 6 additions & 0 deletions src/core/providers/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ShapeModel } from '@/core/model';

export interface CanvasContextModel {
shapes: ShapeModel[];
setShapes: React.Dispatch<React.SetStateAction<ShapeModel[]>>;
}
29 changes: 29 additions & 0 deletions src/core/providers/canvas/canvas.provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { ShapeModel } from '@/core/model';
import { CanvasContext } from './canvas.context';

interface Props {
children: React.ReactNode;
}

export const CanvasProvider: React.FC<Props> = props => {
const { children } = props;
const [shapes, setShapes] = React.useState<ShapeModel[]>([]);

return (
<CanvasContext.Provider value={{ shapes, setShapes }}>
{children}
</CanvasContext.Provider>
);
};

export const useCanvasContext = () => {
const context = React.useContext(CanvasContext);
if (context === null) {
throw new Error(
'useCanvasContext: Ensure you have wrapped your app with CanvasProvider'
);
}

return context;
};
1 change: 1 addition & 0 deletions src/core/providers/canvas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './canvas.provider';
1 change: 1 addition & 0 deletions src/core/providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './canvas';
5 changes: 4 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import App from './App.tsx';
import './normalize.css';
import './reset.css';
import './main.css';
import { CanvasProvider } from './core/providers';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
<CanvasProvider>
<App />
</CanvasProvider>
</React.StrictMode>
);
11 changes: 1 addition & 10 deletions src/pods/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ShapeType } from '@/core/model';
import { ShapeModel, ShapeType } from '@/core/model';
import { v4 as uuidv4 } from 'uuid';
import {
getComboBoxShapeSizeRestrictions,
Expand All @@ -21,15 +21,6 @@ export interface Coord {
y: number;
}

export interface ShapeModel {
id: string;
x: number;
y: number;
width: number;
height: number;
type: ShapeType;
}

const getDefaultSizeFromShape = (shapeType: ShapeType): Size => {
switch (shapeType) {
case 'combobox':
Expand Down
10 changes: 5 additions & 5 deletions src/pods/canvas/canvas.pod.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import classes from './canvas.pod.module.css';
import { createRef, useState } from 'react';
import { createRef } from 'react';
import Konva from 'konva';
import { useCanvasContext } from '@/core/providers';
import { Layer, Stage, Transformer } from 'react-konva';
import { ShapeModel } from './canvas.model';
import { useSelection } from './use-selection.hook';
import Konva from 'konva';
import { useTransform } from './use-transform.hook';
import { renderShapeComponent } from './shape-renderer';
import { useDropShape } from './use-drop-shape.hook';
import { useMonitorShape } from './use-monitor-shape.hook';
import classes from './canvas.pod.module.css';
import {
moveZIndexDownOneLevel,
moveZIndexToBottom,
Expand All @@ -16,7 +16,7 @@ import {
} from './zindex.util';

export const CanvasPod = () => {
const [shapes, setShapes] = useState<ShapeModel[]>([]);
const { shapes, setShapes } = useCanvasContext();

const {
shapeRefs,
Expand Down

0 comments on commit d6c64e6

Please sign in to comment.