import { atom , PrimitiveAtom } from "jotai" ; import { sendAtom , modeAtom } from "./modeMachine" ; import { adjustSvgPath } from "../utils/svgpath" ; type TShapeCommon = { x : number ; y : number ; scale : number ; } ; export type TShapePath = TShapeCommon & { path : string ; color : string ; } ; export type TShapeImage = TShapeCommon & { image : string ; width : number ; height : number ; rotate : number ; } ; export type TShape = TShapePath | TShapeImage ; export type ShapeAtom = PrimitiveAtom < TShape > ; export const colorAtom = atom ( "black" ) ; export const allShapesAtom = atom < ShapeAtom [ ] > ( [ ] ) ; export const addShapePathAtom = atom ( null , ( get , set , path ) => { const color = get ( colorAtom ) ; const shape = { ... adjustSvgPath ( path ) , scale : 1 , color } ; set ( allShapesAtom , [ ... get ( allShapesAtom ) , atom ( shape ) as ShapeAtom ] ) ; } ) ; export const addShapeImageAtom = atom ( null , ( get , set , image ) => { const shape = { x : 100 , y : 100 , scale : 1 , image , width : 300 , height : 300 , rotate : 0 , } ; const shapeAtom = atom ( shape ) as ShapeAtom ; set ( allShapesAtom , [ ... get ( allShapesAtom ) , shapeAtom ] ) ; } ) ; export const deleteShapeAtom = atom ( null , ( get , set , shapeAtom ) => { const mode = get ( modeAtom ) ; if ( mode === "erase" ) { set ( sendAtom , { type : "UNSELECT_SHAPE" , shapeAtom } ) ; set ( allShapesAtom , ( prev ) => prev . filter ( ( item ) => item !== shapeAtom ) ) ; } } ) ;