import {useCallback, Fragment} from 'react'; import {v4 as uuidv4} from 'uuid'; import {Editor, StringField, NumberField, OverlayHandle} from './Editor'; import type {SvgItem} from './App'; import type {Action} from './Editor'; export type Circle = { type: 'circle', id: string, stroke: string, strokeWidth: number, cx: number, cy: number, r: number, } type CircleRenderProps = { circle: Circle, } export function CircleRender({circle}: CircleRenderProps) { return ; } export function CircleOverlaySVG({circle}: CircleRenderProps) { return ; } type CircleEditorProps = { circle: Circle, onChange: Action, } export function CircleEditor({circle, onChange}: CircleEditorProps) { const onCxChange = useCallback((cx: number) => { onChange((item) => { if (item.type !== 'circle') { return item; } return { ...item, cx, }; }); }, [onChange]); const onCyChange = useCallback((cy: number) => { onChange((item) => { if (item.type !== 'circle') { return item; } return { ...item, cy, }; }); }, [onChange]); const onRChange = useCallback((r: number) => { onChange((item) => { if (item.type !== 'circle') { return item; } return { ...item, r, }; }); }, [onChange]); const onStrokeChange = useCallback((stroke: string) => { onChange((item) => { if (item.type !== 'circle') { return item; } return { ...item, stroke, }; }); }, [onChange]); const onStrokeWidthChange = useCallback((strokeWidth: number) => { onChange((item) => { if (item.type !== 'circle') { return item; } return { ...item, strokeWidth, }; }); }, [onChange]); return ; } type CircleOverlayDOMProps = { circle: Circle, width: number, height: number, onChange: Action, } export function CircleOverlayDOM({circle, width, height, onChange}: CircleOverlayDOMProps) { const onCChange = useCallback((cx: number, cy: number) => { onChange((item) => { if (item.type !== 'circle') { return item; } return { ...item, cx, cy, }; }); }, [ onChange]); const onRChange = useCallback((rx: number, ry: number) => { onChange((item) => { if (item.type !== 'circle') { return item; } const r = Math.sqrt((rx - circle.cx) * (rx - circle.cx) + (ry - circle.cy) * (ry - circle.cy)); return { ...item, r, }; }); }, [onChange]); return ; } export function normalizeCircle(circle: any): Circle | null { if (!circle) { return null; } return { type: 'circle', id: typeof circle.id === 'string' ? circle.id : uuidv4(), cx: typeof circle.cx === 'number' ? circle.cx : 0, cy: typeof circle.cy === 'number' ? circle.cy : 0, r: typeof circle.r === 'number' ? circle.r : 0, stroke: typeof circle.stroke === 'string' ? circle.stroke : 'black', strokeWidth: typeof circle.strokeWidth === 'number' ? circle.strokeWidth : 0, }; }