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

Feature/#122 other properties #190

Merged
merged 2 commits into from
Aug 14, 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
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"@atlaskit/pragmatic-drag-and-drop": "^1.2.1",
"@types/lodash.clonedeep": "^4.5.9",
"immer": "^10.1.1",
"konva": "^9.3.12",
"lodash.clonedeep": "^4.5.0",
"react": "^18.3.1",
Expand Down
9 changes: 6 additions & 3 deletions src/common/components/front-components/button-shape.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const getButtonShapeSizeRestrictions = (): ShapeSizeRestrictions =>
buttonShapeRestrictions;

export const ButtonShape = forwardRef<any, ShapeProps>(
({ x, y, width, height, id, onSelected, text, ...shapeProps }, ref) => {
(
{ x, y, width, height, id, onSelected, text, otherProps, ...shapeProps },
ref
) => {
const { width: restrictedWidth, height: restrictedHeight } =
fitSizeToShapeSizeRestrictions(buttonShapeRestrictions, width, height);

Expand All @@ -37,9 +40,9 @@ export const ButtonShape = forwardRef<any, ShapeProps>(
width={restrictedWidth}
height={restrictedHeight}
cornerRadius={14}
stroke="black"
stroke={otherProps?.stroke ?? 'black'}
strokeWidth={2}
fill="white"
fill={otherProps?.backgroundColor ?? 'white'}
/>
<Text
x={0}
Expand Down
21 changes: 17 additions & 4 deletions src/common/components/front-components/input-shape.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ShapeSizeRestrictions } from '@/core/model';
import { forwardRef } from 'react';
import { forwardRef, useMemo } from 'react';
import { ShapeProps } from './shape.model';
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions';
import { Group, Rect, Text } from 'react-konva';
Expand All @@ -17,10 +17,23 @@ export const getInputShapeSizeRestrictions = (): ShapeSizeRestrictions =>
inputShapeRestrictions;

export const InputShape = forwardRef<any, ShapeProps>(
({ x, y, width, height, id, onSelected, text, ...shapeProps }, ref) => {
(
{ x, y, width, height, id, onSelected, text, otherProps, ...shapeProps },
ref
) => {
const { width: restrictedWidth, height: restrictedHeight } =
fitSizeToShapeSizeRestrictions(inputShapeRestrictions, width, height);

const stroke = useMemo(
() => otherProps?.stroke ?? 'black',
[otherProps?.stroke]
);

const fill = useMemo(
() => otherProps?.backgroundColor ?? 'white',
[otherProps?.backgroundColor]
);

return (
<Group
x={x}
Expand All @@ -37,9 +50,9 @@ export const InputShape = forwardRef<any, ShapeProps>(
width={restrictedWidth}
height={restrictedHeight}
cornerRadius={5}
stroke="black"
stroke={stroke}
strokeWidth={2}
fill="white"
fill={fill}
/>
<Text
x={10}
Expand Down
3 changes: 2 additions & 1 deletion src/common/components/front-components/shape.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Important: we extend from Shapeconfig so we can get additional shape params
// TODO: we will need to add more props like for instance text content

import { ShapeType } from '@/core/model';
import { OtherProps, ShapeType } from '@/core/model';
import { ShapeConfig } from 'konva/lib/Shape';

// but we have to check how to pass it to the shape (there will be different types of shapes)
Expand All @@ -12,4 +12,5 @@ export interface ShapeProps extends ShapeConfig {
width: number;
height: number;
onSelected: (id: string, type: ShapeType) => void;
otherProps?: OtherProps;
}
6 changes: 6 additions & 0 deletions src/core/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export interface Coord {
y: number;
}

export interface OtherProps {
stroke?: string;
backgroundColor?: string;
}

export interface ShapeModel {
id: string;
x: number;
Expand All @@ -63,4 +68,5 @@ export interface ShapeModel {
hasLateralTransformer: boolean;
editType?: EditType;
text?: string;
otherProps?: OtherProps;
}
15 changes: 14 additions & 1 deletion src/core/providers/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Coord, ShapeModel, ShapeRefs, ShapeType, Size } from '@/core/model';
import {
Coord,
OtherProps,
ShapeModel,
ShapeRefs,
ShapeType,
Size,
} from '@/core/model';
import Konva from 'konva';
import { Node, NodeConfig } from 'konva/lib/Node';

Expand All @@ -16,8 +23,14 @@ export interface SelectionInfo {
selectedShapeRef: React.MutableRefObject<Node<NodeConfig> | null>;
selectedShapeId: string;
selectedShapeType: ShapeType | null;
getSelectedShapeData: () => ShapeModel | undefined;
setZIndexOnSelected: (action: ZIndexAction) => void;
updateTextOnSelected: (text: string) => void;
// TODO: Update, A. KeyOf B. Move To useSelectionInfo
updateOtherPropsOnSelected: <K extends keyof OtherProps>(
key: K,
value: OtherProps[K]
) => void;
}

export interface CanvasContextModel {
Expand Down
22 changes: 21 additions & 1 deletion src/core/providers/canvas/use-selection.hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef, useState } from 'react';
import Konva from 'konva';
import { ShapeRefs, ShapeType } from '@/core/model';
import { OtherProps, ShapeModel, ShapeRefs, ShapeType } from '@/core/model';
import { DocumentModel, SelectionInfo, ZIndexAction } from './canvas.model';
import { performZIndexAction } from './zindex.util';

Expand Down Expand Up @@ -69,6 +69,24 @@ export const useSelection = (
}));
};

// TODO: Rather implement this using immmer

const updateOtherPropsOnSelected = <K extends keyof OtherProps>(
key: K,
value: OtherProps[K]
) => {
setDocument(prevDocument => ({
shapes: prevDocument.shapes.map(shape =>
shape.id === selectedShapeId
? { ...shape, otherProps: { ...shape.otherProps, [key]: value } }
: shape
),
}));
};

const getSelectedShapeData = (): ShapeModel | undefined =>
document.shapes.find(shape => shape.id === selectedShapeId);

return {
transformerRef,
shapeRefs,
Expand All @@ -77,7 +95,9 @@ export const useSelection = (
selectedShapeRef,
selectedShapeId,
selectedShapeType,
getSelectedShapeData,
setZIndexOnSelected,
updateTextOnSelected,
updateOtherPropsOnSelected,
};
};
22 changes: 21 additions & 1 deletion src/pods/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Coord, ShapeType, Size, ShapeModel, EditType } from '@/core/model';
import {
Coord,
ShapeType,
Size,
ShapeModel,
EditType,
OtherProps,
} from '@/core/model';
import { v4 as uuidv4 } from 'uuid';

import {
Expand Down Expand Up @@ -233,6 +240,18 @@ const getShapeEditInlineType = (shapeType: ShapeType): EditType | undefined => {
return result;
};

export const generateDefaultOtherProps = (
shapeType: ShapeType
): OtherProps | undefined => {
switch (shapeType) {
case 'input':
case 'button':
return { stroke: '#000000', backgroundColor: '#FFFFFF' };
default:
return undefined;
}
};

// TODO: create interfaces to hold Coordination and Size
// coordinate: { x: number, y: number }
// size: { width: number, height: number }
Expand All @@ -251,6 +270,7 @@ export const createShape = (coord: Coord, shapeType: ShapeType): ShapeModel => {
hasLateralTransformer: doesShapeHaveLateralTransformer(shapeType),
text: generateDefaultTextValue(shapeType),
editType: getShapeEditInlineType(shapeType),
otherProps: generateDefaultOtherProps(shapeType),
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const renderButton = (
onTransformEnd={handleTransform}
isEditable={shape.allowsInlineEdition}
text={shape.text}
otherProps={shape.otherProps}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const renderInput = (
onTransformEnd={handleTransform}
isEditable={shape.allowsInlineEdition}
text={shape.text}
otherProps={shape.otherProps}
/>
);
};
20 changes: 20 additions & 0 deletions src/pods/properties/components/color-picker.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
interface Props {
label: string;
color: string;
onChange: (color: string) => void;
}

export const ColorPicker: React.FC<Props> = props => {
const { label, color, onChange } = props;

return (
<div>
<label>{label}</label>
<input
type="color"
value={color}
onChange={e => onChange(e.target.value)}
/>
</div>
);
};
20 changes: 20 additions & 0 deletions src/pods/properties/properties.pod.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import { useCanvasContext } from '@/core/providers';
import classes from './properties.pod.module.css';
import { ZIndexOptions } from './components/zindex/zindex-option.component';
import { ColorPicker } from './components/color-picker.component';

export const PropertiesPod = () => {
const { selectionInfo } = useCanvasContext();
const { getSelectedShapeData, updateOtherPropsOnSelected } = selectionInfo;

const selectedShapeID = selectionInfo?.selectedShapeRef.current ?? null;

if (!selectedShapeID) {
return null;
}

const selectedShapeData = getSelectedShapeData();

return (
<div>
<div className={classes.title}>
<p>Properties</p>
</div>
<ZIndexOptions selectionInfo={selectionInfo} />
{selectedShapeData?.otherProps?.stroke && (
<ColorPicker
label="Color"
color={selectedShapeData.otherProps.stroke}
onChange={color => updateOtherPropsOnSelected('stroke', color)}
/>
)}
{selectedShapeData?.otherProps?.backgroundColor && (
<ColorPicker
label="Background Color"
color={selectedShapeData.otherProps.backgroundColor}
onChange={color =>
updateOtherPropsOnSelected('backgroundColor', color)
}
/>
)}
</div>
);
};
Loading