Skip to content

Commit

Permalink
#568 setCanvasSize from settings modal
Browse files Browse the repository at this point in the history
  • Loading branch information
oleojake committed Dec 22, 2024
1 parent 340502a commit 9a4e099
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/core/providers/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -110,4 +115,6 @@ export interface CanvasContextModel {
setIsThumbnailContextMenuVisible: React.Dispatch<
React.SetStateAction<boolean>
>;
canvasSize: CanvasSize;
setCanvasSize: (canvasDimensions: CanvasSize) => void;
}
12 changes: 11 additions & 1 deletion src/core/providers/canvas/canvas.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,6 +29,10 @@ export const CanvasProvider: React.FC<Props> = props => {
const [fileName, setFileName] = React.useState<string>('');
const [isThumbnailContextMenuVisible, setIsThumbnailContextMenuVisible] =
React.useState(false);
const [canvasSize, setCanvasSize] = React.useState<CanvasSize>({
width: 3000,
height: 3000,
});

const {
addSnapshot,
Expand Down Expand Up @@ -331,6 +339,8 @@ export const CanvasProvider: React.FC<Props> = props => {
activePageIndex: document.activePageIndex,
isThumbnailContextMenuVisible,
setIsThumbnailContextMenuVisible,
canvasSize: canvasSize,
setCanvasSize: setCanvasSize,
}}
>
{children}
Expand Down
4 changes: 1 addition & 3 deletions src/pods/canvas/canvas.pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export const CanvasPod = () => {
const [isTransfomerBeingDragged, setIsTransfomerBeingDragged] =
useState(false);

const canvasSize = React.useMemo(() => ({ width: 3000, height: 3000 }), []);

const {
shapes,
scale,
Expand All @@ -31,6 +29,7 @@ export const CanvasPod = () => {
updateShapeSizeAndPosition,
updateShapePosition,
stageRef,
canvasSize,
} = useCanvasContext();

const {
Expand Down Expand Up @@ -153,7 +152,6 @@ export const CanvasPod = () => {
style={{ opacity: isDraggedOver ? 0.5 : 1 }}
>
<ContextMenu dropRef={dropRef} />
{/*TODO: move size to canvas provider?*/}
{/* onMouseDown={handleClearSelection}*/}
<Stage
width={canvasSize.width}
Expand Down
33 changes: 28 additions & 5 deletions src/pods/settings/components/canvas-size-settings.component.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
import { CanvasSize } from '@/core/providers/canvas/canvas.model';
import classes from './canvas-size-settings.component.module.css';

export const CanvasSizeSettings = () => {
interface CanvasSizeSettingsProps {
canvasSize: CanvasSize;
setNewCanvasSize: React.Dispatch<React.SetStateAction<CanvasSize>>;
newCanvasSize: CanvasSize;
}

export const CanvasSizeSettings: React.FC<CanvasSizeSettingsProps> = props => {
const { canvasSize, setNewCanvasSize, newCanvasSize } = props;

return (
<div className={classes.container}>
<p className={classes.title}>Canvas Dimensions</p>
<div className={classes.canvasSizeSettings}>
<p> Width:</p>
<p> Width (px):</p>
<input
type="text"
placeholder="Search icons..."
value={String(newCanvasSize.width)}
onChange={e =>
setNewCanvasSize({
...newCanvasSize,
width: Number(e.target.value),
})
}
placeholder={String(canvasSize.width)}
className={classes.searchInput}
/>
</div>
<div className={classes.canvasSizeSettings}>
<p>Height:</p>
<p>Height (px):</p>
<input
type="text"
placeholder="Search icons..."
value={String(newCanvasSize.height)}
onChange={e =>
setNewCanvasSize({
...newCanvasSize,
height: Number(e.target.value),
})
}
placeholder={String(canvasSize.height)}
className={classes.searchInput}
/>
</div>
Expand Down
14 changes: 14 additions & 0 deletions src/pods/settings/settings.pod.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
30 changes: 29 additions & 1 deletion src/pods/settings/settings.pod.tsx
Original file line number Diff line number Diff line change
@@ -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>(canvasSize);
const { closeModal } = useModalDialogContext();

const handleSelection = () => {
setCanvasSize(newCanvasSize);
closeModal();
};

useEffect(() => {
setIsInlineEditing(true);

return () => {
setIsInlineEditing(false);
};
}, []);

return (
<div className={classes.container}>
<p className={classes.title}>Settings</p>
<CanvasSizeSettings />
<CanvasSizeSettings
canvasSize={canvasSize}
setNewCanvasSize={setNewCanvasSize}
newCanvasSize={newCanvasSize}
/>
<button className={classes.button} onClick={handleSelection}>
Confirm Settings
</button>
</div>
);
};

0 comments on commit 9a4e099

Please sign in to comment.