From 9ef6c819b9d29ceb96e2e7b5a1649d97756a23ca Mon Sep 17 00:00:00 2001 From: KirillSerg Date: Mon, 20 May 2024 21:38:28 +0300 Subject: [PATCH 1/4] feat: added arrow-line to the toolbar --- src/components/Toolbar.tsx | 53 +++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/src/components/Toolbar.tsx b/src/components/Toolbar.tsx index cf595c7..762e153 100644 --- a/src/components/Toolbar.tsx +++ b/src/components/Toolbar.tsx @@ -24,10 +24,10 @@ const Toolbar = () => { xmlns="http://www.w3.org/2000/svg" > { xmlns="http://www.w3.org/2000/svg" > @@ -65,7 +65,7 @@ const Toolbar = () => { xmlns="http://www.w3.org/2000/svg" > { /> + + ); }; From e898d6e6439f86a272292ef9d22a688e3807c894 Mon Sep 17 00:00:00 2001 From: KirillSerg Date: Mon, 20 May 2024 23:57:06 +0300 Subject: [PATCH 2/4] feat: implemented adding arrow-line (marker) --- src/components/ArrowLine.tsx | 33 +++++++++++++++++++++ src/components/Canvas.tsx | 13 +++++++++ src/components/Toolbar.tsx | 56 +++++++++++++++++++++++++----------- src/store/store.ts | 5 +++- src/types/CommonTypes.ts | 1 + 5 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 src/components/ArrowLine.tsx diff --git a/src/components/ArrowLine.tsx b/src/components/ArrowLine.tsx new file mode 100644 index 0000000..74bcc10 --- /dev/null +++ b/src/components/ArrowLine.tsx @@ -0,0 +1,33 @@ +// interface Props { +// element: Element; +// } + +// const ArrowLine = ({ element }: Props) => { +// return ( +// <> +// +// +// +// +// +// +// +// ); +// }; + +// export default ArrowLine; diff --git a/src/components/Canvas.tsx b/src/components/Canvas.tsx index 307a6ba..d8a8eb7 100644 --- a/src/components/Canvas.tsx +++ b/src/components/Canvas.tsx @@ -69,6 +69,19 @@ const Canvas = () => { xmlns="http://www.w3.org/2000/svg" tabIndex={0} > + + + + + + {elements.map((element) => ( { - const [creatingElementType, setCreatingElementType] = useAtom( - creatingElementTypeAtom, - ); + const [elementTypeName, setElementTypeName] = + useState('free'); + const [, setCreatingElementType] = useAtom(creatingElementTypeAtom); + const [, setInitialElement] = useAtom(initialElementAtom); + + const handlerSetElementTypeName = (typeName: keyof typeof elementsType) => { + setElementTypeName(typeName); + setCreatingElementType(elementsType[typeName] as Element['type']); + + if (typeName === 'arrow_line') + setInitialElement((prev) => { + return { ...prev, markerEnd: 'url(#arrow)' }; + }); + }; return (
diff --git a/src/store/store.ts b/src/store/store.ts index afc3714..159034b 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -19,11 +19,13 @@ const initialElement: Element = { x2: 0, y2: 0, points: "", + markerEnd: "", stroke: 'black', strokeWidth: 4, fill: 'none', } +export const initialElementAtom = atom(initialElement) export const creatingElementTypeAtom = atom("free") export const elementsAtom = atomWithStorage("elements", []) export const selectedElementAtom = atom(null) @@ -68,7 +70,7 @@ export const onMouseDownAtom = atom( if (get(isDrawingAtom)) { const creatingElementType = get(creatingElementTypeAtom) const newEl = { - ...initialElement, + ...get(initialElementAtom), type: creatingElementType, id: crypto.randomUUID(), x: update.x, @@ -176,5 +178,6 @@ export const onMouseUpAtom = atom( set(creatingElementTypeAtom, "free") set(isDraggingAtom, false) set(selectingAreaAtom, null) + set(initialElementAtom, initialElement) } ) diff --git a/src/types/CommonTypes.ts b/src/types/CommonTypes.ts index fd1c47b..f92f999 100644 --- a/src/types/CommonTypes.ts +++ b/src/types/CommonTypes.ts @@ -15,6 +15,7 @@ export interface Element { y2: number; // pointsarr: Array; //[[x,y], [x,y], [x,y], ...] points: string //"x,y x,y x,y ..." + markerEnd: string; stroke: string; strokeWidth: number; fill: string; From 4e1c00b1ccad26da3da19875a2a96e4b02dc0ba6 Mon Sep 17 00:00:00 2001 From: KirillSerg Date: Tue, 21 May 2024 00:05:49 +0300 Subject: [PATCH 3/4] fix: cleaning of toolbar element name state --- src/components/Toolbar.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/components/Toolbar.tsx b/src/components/Toolbar.tsx index 93ac6d6..bd1692a 100644 --- a/src/components/Toolbar.tsx +++ b/src/components/Toolbar.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { useAtom } from 'jotai'; import { creatingElementTypeAtom, initialElementAtom } from '../store/store'; import { Element } from '../types/CommonTypes'; @@ -15,7 +15,9 @@ const elementsType = { const Toolbar = () => { const [elementTypeName, setElementTypeName] = useState('free'); - const [, setCreatingElementType] = useAtom(creatingElementTypeAtom); + const [creatingElementType, setCreatingElementType] = useAtom( + creatingElementTypeAtom, + ); const [, setInitialElement] = useAtom(initialElementAtom); const handlerSetElementTypeName = (typeName: keyof typeof elementsType) => { @@ -28,6 +30,12 @@ const Toolbar = () => { }); }; + useEffect(() => { + if (creatingElementType === 'free') { + setElementTypeName('free'); + } + }, [creatingElementType]); + return (