From 9a4e099c682174244253774c6f9e84da6998ee3c Mon Sep 17 00:00:00 2001 From: oleojake Date: Sun, 22 Dec 2024 16:22:19 +0100 Subject: [PATCH] #568 setCanvasSize from settings modal --- src/core/providers/canvas/canvas.model.ts | 7 ++++ src/core/providers/canvas/canvas.provider.tsx | 12 ++++++- src/pods/canvas/canvas.pod.tsx | 4 +-- .../canvas-size-settings.component.tsx | 33 ++++++++++++++++--- src/pods/settings/settings.pod.module.css | 14 ++++++++ src/pods/settings/settings.pod.tsx | 30 ++++++++++++++++- 6 files changed, 90 insertions(+), 10 deletions(-) diff --git a/src/core/providers/canvas/canvas.model.ts b/src/core/providers/canvas/canvas.model.ts index 6ec874d7..17fa3915 100644 --- a/src/core/providers/canvas/canvas.model.ts +++ b/src/core/providers/canvas/canvas.model.ts @@ -9,6 +9,11 @@ import { import Konva from 'konva'; import { Node, NodeConfig } from 'konva/lib/Node'; +export interface CanvasSize { + width: number; + height: number; +} + export type ZIndexAction = 'top' | 'bottom' | 'up' | 'down'; export interface Page { @@ -110,4 +115,6 @@ export interface CanvasContextModel { setIsThumbnailContextMenuVisible: React.Dispatch< React.SetStateAction >; + canvasSize: CanvasSize; + setCanvasSize: (canvasDimensions: CanvasSize) => void; } diff --git a/src/core/providers/canvas/canvas.provider.tsx b/src/core/providers/canvas/canvas.provider.tsx index e6a0ba56..a2cf6998 100644 --- a/src/core/providers/canvas/canvas.provider.tsx +++ b/src/core/providers/canvas/canvas.provider.tsx @@ -5,7 +5,11 @@ import { useSelection } from './use-selection.hook'; import { createShape } from '@/pods/canvas/model'; import { useHistoryManager } from '@/common/undo-redo'; import { useStateWithInterceptor } from './canvas.hook'; -import { createDefaultDocumentModel, DocumentModel } from './canvas.model'; +import { + CanvasSize, + createDefaultDocumentModel, + DocumentModel, +} from './canvas.model'; import { v4 as uuidv4 } from 'uuid'; import Konva from 'konva'; import { isPageIndexValid, removeShapesFromList } from './canvas.business'; @@ -25,6 +29,10 @@ export const CanvasProvider: React.FC = props => { const [fileName, setFileName] = React.useState(''); const [isThumbnailContextMenuVisible, setIsThumbnailContextMenuVisible] = React.useState(false); + const [canvasSize, setCanvasSize] = React.useState({ + width: 3000, + height: 3000, + }); const { addSnapshot, @@ -331,6 +339,8 @@ export const CanvasProvider: React.FC = props => { activePageIndex: document.activePageIndex, isThumbnailContextMenuVisible, setIsThumbnailContextMenuVisible, + canvasSize: canvasSize, + setCanvasSize: setCanvasSize, }} > {children} diff --git a/src/pods/canvas/canvas.pod.tsx b/src/pods/canvas/canvas.pod.tsx index 6e0ee005..e2f06233 100644 --- a/src/pods/canvas/canvas.pod.tsx +++ b/src/pods/canvas/canvas.pod.tsx @@ -21,8 +21,6 @@ export const CanvasPod = () => { const [isTransfomerBeingDragged, setIsTransfomerBeingDragged] = useState(false); - const canvasSize = React.useMemo(() => ({ width: 3000, height: 3000 }), []); - const { shapes, scale, @@ -31,6 +29,7 @@ export const CanvasPod = () => { updateShapeSizeAndPosition, updateShapePosition, stageRef, + canvasSize, } = useCanvasContext(); const { @@ -153,7 +152,6 @@ export const CanvasPod = () => { style={{ opacity: isDraggedOver ? 0.5 : 1 }} > - {/*TODO: move size to canvas provider?*/} {/* onMouseDown={handleClearSelection}*/} { +interface CanvasSizeSettingsProps { + canvasSize: CanvasSize; + setNewCanvasSize: React.Dispatch>; + newCanvasSize: CanvasSize; +} + +export const CanvasSizeSettings: React.FC = props => { + const { canvasSize, setNewCanvasSize, newCanvasSize } = props; + return (

Canvas Dimensions

-

Width:

+

Width (px):

+ setNewCanvasSize({ + ...newCanvasSize, + width: Number(e.target.value), + }) + } + placeholder={String(canvasSize.width)} className={classes.searchInput} />
-

Height:

+

Height (px):

+ setNewCanvasSize({ + ...newCanvasSize, + height: Number(e.target.value), + }) + } + placeholder={String(canvasSize.height)} className={classes.searchInput} />
diff --git a/src/pods/settings/settings.pod.module.css b/src/pods/settings/settings.pod.module.css index b718d33c..952815d2 100644 --- a/src/pods/settings/settings.pod.module.css +++ b/src/pods/settings/settings.pod.module.css @@ -17,3 +17,17 @@ font-weight: var(--fw-extrabold); line-height: 1.3; } + +.button { + color: white; + margin-top: var(--space-lg); + background-color: var(--primary-500); + padding: var(--space-s) var(--space-md); + border-radius: var(--border-radius-m); + font-size: var(--fs-md); + cursor: pointer; +} + +.button:hover { + background-color: var(--primary-600); +} diff --git a/src/pods/settings/settings.pod.tsx b/src/pods/settings/settings.pod.tsx index 318679dc..83a7baca 100644 --- a/src/pods/settings/settings.pod.tsx +++ b/src/pods/settings/settings.pod.tsx @@ -1,11 +1,39 @@ +import { useCanvasContext } from '@/core/providers'; import { CanvasSizeSettings } from './components'; import classes from './settings.pod.module.css'; +import { useEffect, useState } from 'react'; +import { CanvasSize } from '@/core/providers/canvas/canvas.model'; +import { useModalDialogContext } from '@/core/providers/model-dialog-providers/model-dialog.provider'; export const SettingsPod = () => { + const { canvasSize, setCanvasSize, setIsInlineEditing } = useCanvasContext(); + const [newCanvasSize, setNewCanvasSize] = useState(canvasSize); + const { closeModal } = useModalDialogContext(); + + const handleSelection = () => { + setCanvasSize(newCanvasSize); + closeModal(); + }; + + useEffect(() => { + setIsInlineEditing(true); + + return () => { + setIsInlineEditing(false); + }; + }, []); + return (

Settings

- + +
); };