From af650e104c2efcaf1445ae7dc1fb01128f32c5e8 Mon Sep 17 00:00:00 2001 From: Artur AGH Date: Sun, 10 Dec 2023 14:49:55 +0100 Subject: [PATCH] layer block function DONE! Started legacy function --- src/components/canvas.tsx | 2 ++ src/components/layer-item.tsx | 22 +++++++++++--- src/components/transformable-image.tsx | 7 ++++- src/components/transformable-text.tsx | 6 +++- src/store/app.slice.ts | 42 ++++++++++++++++++++++++-- 5 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/components/canvas.tsx b/src/components/canvas.tsx index 09a5b34..369298d 100644 --- a/src/components/canvas.tsx +++ b/src/components/canvas.tsx @@ -109,6 +109,7 @@ const Canvas = () => { }} imageProps={item.params} key={item.id} + isBlocked={item.isBlocked} /> ); } @@ -116,6 +117,7 @@ const Canvas = () => { return ( selectItem(item.id)} isSelected={item.id === selectedItemId} diff --git a/src/components/layer-item.tsx b/src/components/layer-item.tsx index 417fc8d..41ffc45 100644 --- a/src/components/layer-item.tsx +++ b/src/components/layer-item.tsx @@ -1,10 +1,16 @@ import React from "react"; import { useSortable } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; -import { type StageItem, StageItemType } from "@/store/app.slice"; +import { + setBlockedItem, + type StageItem, + StageItemType, +} from "@/store/app.slice"; import { useAppDispatch } from "@/hooks"; import { Button } from "@/components/ui/button"; import { HiOutlineLockClosed, HiOutlineLockOpen } from "react-icons/hi2"; +import { IconButton } from "@radix-ui/themes"; +import { GripVertical } from "lucide-react"; export default function LayerItem({ item }: { item: StageItem }) { const dispatch = useAppDispatch(); @@ -17,14 +23,21 @@ export default function LayerItem({ item }: { item: StageItem }) { transform: CSS.Transform.toString(transform), transition, }; + + const handleBlockItemClicked = () => { + dispatch(setBlockedItem({ id: item.id, blocked: !item.isBlocked })); + }; + return (
+ + + {item.type} {item.type === StageItemType.Text ? ( @@ -40,9 +53,8 @@ export default function LayerItem({ item }: { item: StageItem }) { src={item.params.imageUrl} /> )} -
); diff --git a/src/components/transformable-image.tsx b/src/components/transformable-image.tsx index f3bcef7..ad6630a 100644 --- a/src/components/transformable-image.tsx +++ b/src/components/transformable-image.tsx @@ -13,6 +13,7 @@ export type TransformableImageProps = { isSelected: boolean; onSelect: () => void; onChange: (newAttrs: TransformableImageConfig) => void; + isBlocked: boolean; }; export const TransformableImage = ({ @@ -21,6 +22,7 @@ export const TransformableImage = ({ isSelected, onSelect, onChange, + isBlocked, }: TransformableImageProps) => { const imageRef = useRef>(null); const trRef = useRef>(null); @@ -46,8 +48,11 @@ export const TransformableImage = ({ ref={imageRef} {...imageProps} image={image} - draggable + draggable={!isBlocked} onDragEnd={(e) => { + if (isBlocked) { + return; + } onChange({ ...imageProps, image, diff --git a/src/components/transformable-text.tsx b/src/components/transformable-text.tsx index 628793a..a2e3906 100644 --- a/src/components/transformable-text.tsx +++ b/src/components/transformable-text.tsx @@ -38,6 +38,7 @@ export type TransformableTextProps = { id: string; isEditing?: boolean; onEditChange?: (isEditing: boolean) => void; + isBlocked: boolean; }; const TransformableText = ({ @@ -48,6 +49,7 @@ const TransformableText = ({ id, isEditing, onEditChange, + isBlocked, }: TransformableTextProps) => { console.log(textProps); const [value, setText] = useState(""); @@ -70,6 +72,7 @@ const TransformableText = ({ }; const handleDblClick = () => { + if (isBlocked) return; setText(text); onEditChange?.(true); }; @@ -121,8 +124,9 @@ const TransformableText = ({ align={"center"} {...textProps} text={text} - draggable + draggable={!isBlocked} onDragEnd={(e) => { + if (isBlocked) return; onChange({ ...textProps, text, diff --git a/src/store/app.slice.ts b/src/store/app.slice.ts index 3b2610d..cfdf8cc 100644 --- a/src/store/app.slice.ts +++ b/src/store/app.slice.ts @@ -13,6 +13,7 @@ export enum StageItemType { type StageItemCommon = { id: string; + isBlocked: boolean; }; export type StageTextItem = { @@ -77,9 +78,9 @@ export const appSlice = createSlice({ state.items.push({ type: StageItemType.Text, id: textId, + isBlocked: false, params: { text: action.payload.initialValue, - ...defaultTextConfig, }, }); @@ -98,6 +99,7 @@ export const appSlice = createSlice({ state.items.push({ type: StageItemType.Image, id: imageId, + isBlocked: false, params: { imageUrl: action.payload.imageUrl, width: action.payload.width, @@ -106,6 +108,21 @@ export const appSlice = createSlice({ }, }); }, + setBlockedItem: ( + state, + action: PayloadAction<{ id: string; blocked: boolean }>, + ) => { + const itemToUpdate = state.items.find( + (item) => item.id === action.payload.id, + ); + if (!itemToUpdate) return; + + if (state.selectedItemId === itemToUpdate.id) { + state.selectedItemId = null; + } + + itemToUpdate.isBlocked = action.payload.blocked; + }, selectBackground: (state, action: PayloadAction) => { state.background = { @@ -114,6 +131,14 @@ export const appSlice = createSlice({ }, selectItem: (state, action: PayloadAction) => { + const itemToSelect = state.items.find( + (item) => item.id === action.payload, + ); + + if (!itemToSelect || itemToSelect.isBlocked) { + return; + } + state.selectedItemId = action.payload; }, @@ -136,7 +161,12 @@ export const appSlice = createSlice({ const textToUpdateIndex = state.items.findIndex((t) => t.id === id); const textToUpdate = state.items[textToUpdateIndex]; - if (!textToUpdate || textToUpdate.type !== StageItemType.Text) return; + if ( + !textToUpdate || + textToUpdate.type !== StageItemType.Text || + textToUpdate.isBlocked + ) + return; state.items[textToUpdateIndex] = { ...textToUpdate, @@ -155,7 +185,12 @@ export const appSlice = createSlice({ const imageToUpdateIndex = state.items.findIndex((img) => img.id === id); const imageToUpdate = state.items[imageToUpdateIndex]; - if (!imageToUpdate || imageToUpdate.type !== StageItemType.Image) return; + if ( + !imageToUpdate || + imageToUpdate.type !== StageItemType.Image || + imageToUpdate.isBlocked + ) + return; (state.items[imageToUpdateIndex] as WritableDraft) = { ...imageToUpdate, @@ -186,4 +221,5 @@ export const { selectBackground, setStageItems, selectItem, + setBlockedItem, } = appSlice.actions;