diff --git a/src/App.tsx b/src/App.tsx index 5078e2a..aec7f9b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,6 @@ import { useAtom, useAtomValue } from 'jotai'; import Canvas from './components/Canvas'; import Inspector from './components/Inspector'; -// import Layers from './components/Layers'; import Toolbar from './components/Toolbar'; import Zoom from './components/Zoom'; import { isDrawingAtom, onKeyPressAtom } from './store/store'; @@ -30,7 +29,6 @@ const App = () => { - {/* */}
diff --git a/src/assets/icons/Icons.tsx b/src/assets/icons/Icons.tsx new file mode 100644 index 0000000..a20e512 --- /dev/null +++ b/src/assets/icons/Icons.tsx @@ -0,0 +1,46 @@ +export const IconLayerToBackFront = () => { + return ( + + ); +}; + +export const IconLayerToBackwardForward = () => { + return ( + + ); +}; diff --git a/src/components/Inspector.tsx b/src/components/Inspector.tsx index eaddfaf..6c4dc8d 100644 --- a/src/components/Inspector.tsx +++ b/src/components/Inspector.tsx @@ -1,14 +1,13 @@ import { useMemo } from 'react'; import { useAtom, useAtomValue } from 'jotai'; -import deleteIcon from '../assets/icons/trash.svg'; import { deleteElementsAtom, creationInitialElementAtom, selectedElementAtom, updateElementsAtom, isDrawingAtom, + setLayersAtom, } from '../store/store'; -import { ElementProps } from '../types/CommonTypes'; import LineArrowProp from './inspectorElements/LineArrowProp'; import LineProp from './inspectorElements/LineProp'; import EdgeRoundProp from './inspectorElements/EdgeRoundProp'; @@ -19,10 +18,20 @@ import StrokeWidthProp from './inspectorElements/StrokeWidthProp'; import ColorsPalette from './inspectorElements/ColorsPalette'; import StrokeStyleProp from './inspectorElements/StrokeStyleProp'; import DuplicateProp from './inspectorElements/DuplicateProp'; +import LayersBtn from './inspectorElements/LayersBtn'; + +import { ElementProps } from '../types/CommonTypes'; + +import deleteIcon from '../assets/icons/trash.svg'; +import { + IconLayerToBackFront, + IconLayerToBackwardForward, +} from '../assets/icons/Icons'; const Inspector = () => { const [, deleteElements] = useAtom(deleteElementsAtom); const [, updateElements] = useAtom(updateElementsAtom); + const [, setLayers] = useAtom(setLayersAtom); const selectedElement = useAtomValue(selectedElementAtom); const [creationInitialElement, setCreationInitialElement] = useAtom( creationInitialElementAtom, @@ -234,6 +243,40 @@ const Inspector = () => { strokeLinecap="round" />
+ + <> +

Layers

+
+ { + setLayers('back'); + }} + /> + { + setLayers('backward'); + }} + /> + { + setLayers('forward'); + }} + /> + { + setLayers('front'); + }} + /> +
+ )} diff --git a/src/components/Layers.tsx b/src/components/Layers.tsx deleted file mode 100644 index 429862f..0000000 --- a/src/components/Layers.tsx +++ /dev/null @@ -1,9 +0,0 @@ -const Layers = () => { - return ( - - ); -}; - -export default Layers; diff --git a/src/components/inspectorElements/LayersBtn.tsx b/src/components/inspectorElements/LayersBtn.tsx new file mode 100644 index 0000000..c54505b --- /dev/null +++ b/src/components/inspectorElements/LayersBtn.tsx @@ -0,0 +1,18 @@ +interface Props { + className?: string; + Icon?: () => JSX.Element; + handlerClick?: () => void; +} + +const LayersBtn = ({ className, Icon, handlerClick }: Props) => { + return ( + + ); +}; + +export default LayersBtn; diff --git a/src/store/store.ts b/src/store/store.ts index e2084a1..ad6d018 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -255,6 +255,8 @@ export const onDragStartAtom = atom( } else { set(selectedElementAtom, [element]) } + } else { + set(selectedElementAtom, [element]) } set(isDraggingAtom, true) } @@ -465,6 +467,66 @@ export const duplicateAtom = atom( } ) +export const setLayersAtom = atom( + null, + (get, set, direction: string) => { + const selectedElement = get(selectedElementAtom) + switch (direction) { + case "back": + set(elementsAtom, (prev) => { + const filteredArray = prev.filter((el) => !selectedElement.find((selectedEl) => selectedEl.id === el.id)) + return [...selectedElement, ...filteredArray] + }) + break; + case "backward": + // spaghetti code / crappy code + set(elementsAtom, (prev) => { + const resultArray: typeof prev = [] + const elementsToMove: typeof prev = [] + const indexes: number[] = [] + prev.forEach((el, i) => { + // filtering and saving index & element to insert + if (selectedElement.find((selectedEl) => selectedEl.id === el.id && i > 0)) { + elementsToMove.push(el) + indexes.push(i) + } else { + resultArray.push(el) + } + }) + elementsToMove.forEach((el, i) => resultArray.splice(indexes[i] - 1, 0, el)) + return resultArray + }) + break; + case "forward": + // spaghetti code / crappy code + set(elementsAtom, (prev) => { + const resultArray: typeof prev = [] + const elementsToMove: typeof prev = [] + const indexes: number[] = [] + prev.forEach((el, i) => { + if (selectedElement.find((selectedEl) => selectedEl.id === el.id)) { + elementsToMove.push(el) + indexes.push(i) + } else { + resultArray.push(el) + } + }) + elementsToMove.forEach((el, i) => resultArray.splice(indexes[i] + 1, 0, el)) + return resultArray + }) + break; + case "front": + set(elementsAtom, (prev) => { + const filteredArray = prev.filter((el) => !selectedElement.find((selectedEl) => selectedEl.id === el.id)) + return [...filteredArray, ...selectedElement] + }) + break; + + } + + } +) + export const setHistoryAtom = atom( null, (get, set) => {