Undo Function Code
Undo Function Code
useEffect(() => {
setSelectedShape(tool);
}, [tool]);
return (
<div className="canvas-container">
<Stage
width={window.innerWidth}
height={window.innerHeight}
onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
>
<Layer>
{shapes.map((shape, index) => {
switch (shape.type) {
case 'rect':
return renderRect(shape, index);
case 'circle':
return renderCircle(shape, index);
case 'line':
return renderLine(shape, index);
case 'text':
return renderText(shape, index);
case 'pencil':
return renderPencil(shape, index);
default:
return null;
}
})}
<Transformer
ref={transformerRef}
keepRatio={false}
enabledAnchors={[
'top-left',
'top-right',
'bottom-left',
'bottom-right',
'middle-left',
'middle-right',
'top-center',
'bottom-center',
]}
/>
</Layer>
</Stage>
{editingText !== null && editingText < shapes.length && (
<textarea
ref={textAreaRef}
value={shapes[editingText].text}
onChange={handleTextEdit}
onBlur={handleTextBlur}
style={{
position: 'absolute',
top: shapes[editingText].y,
left: shapes[editingText].x,
}}
/>
)}
</div>
);
};
Canvas.propTypes = {
tool: PropTypes.string.isRequired,
color: PropTypes.string.isRequired,
history: PropTypes.array.isRequired,
addToHistory: PropTypes.func.isRequired,
};