huytd / ascii-d

A cross-platform ASCII diagram drawing app https://web.ascii-draw.com
https://ascii-draw.com
BSD 3-Clause "New" or "Revised" License
271 stars 14 forks source link

feat: Undo and Redo #20

Closed huytd closed 1 year ago

huytd commented 1 year ago

Implemented the Undo and Redo feature.

Usage:

https://user-images.githubusercontent.com/613943/226196309-96682237-be3b-4ea0-afc5-3e13e1d79930.mov

Due to the way we handled Text editing tool, every keystroke will be recorded as an Undo step, so, if you type "Hello", there will be 5 undo steps.

For other tools (Rect, Line, Erase, Copy/Paste,...), undo step will be recorded as a whole edit.

Currently, there's no limitation on how much undo steps to be recorded (unlimited history!), it gonna crash when the computer run out of memory, but let's wait and see.

widcardw commented 1 year ago

However, the Eraser tool does not seem to work well. Maybe we should work into it further.

https://github.com/huytd/ascii-d/blob/4b2a184dee9b5d64ee1c5963967e0a5e84d11e41/src/tools/eraser.rs#L33-L48

The problem may result from the cursor event: the function draw is triggered on mouse (pressed) move, so a large amount of states are pushed into one version. A strategy should be implemented so that draw or version push are called only the cursor_position changes.

huytd commented 1 year ago

Ah. good point. Yeah I totally forgot about the fact that draw can be called multiple times on the same cell. Yeah I think your suggestion is the way to go about this. Let me push a fix

widcardw commented 1 year ago

Thanks. I am trying to fix this problem with these changes below

image

But I've found another problem. If you follow these steps:

Then you will get this

image

It seems that there may be some problem with the version stack. Please check it out also, thanks.