Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Undo/Redo within a TextArea

Im wondering how to implement undo redo functionality with a TextArea. I already have an undoredo framework functionality working, now I have two questions.

  1. When do I start/stop a new undo/redo command, eg when a user hits undo, how far back do I go.
  2. How do I implement this(1.) in a normal TextArea

My thinking: I thinking that I should create a new undo command, when anything but a alphanumber+space is hit. To do this I would use the keyDown event and test if the key is alpha num if it is not I will reset the command.

Sound good?

like image 775
rid00z Avatar asked Jun 01 '26 14:06

rid00z


2 Answers

Listening for keydown events would miss any text editing that user does with the mouse (cut/copy/paste).

I think a better approach would be to listen for 'change' event on the control (which fires whenever the content changes through user input), and just push the full content of the control (its 'text' or 'htmlText' attribute) with every change event into a undo-buffer (an Array of Strings). I assume that the memory usage is not an issue (it probably isn't, depending on the expected size of the controls content and number of undo levels).

This way, you implement undo/redo just by copying the corresponding control state (moving up and down through array, basically) in the undo buffer back into the control.

The 'proper' approach would be to track the actual edits, and would be condsiderably more complicated.

like image 162
jpop Avatar answered Jun 03 '26 06:06

jpop


1.When do I start/stop a new undo/redo command, eg when a user hits undo, how far back do I go.

Do you think your users will need to undo multiple steps? If so, then you may want to have a history (e.g. Paint .NET) and allow infinite undo-s. Otherwise, just remember the most recently performed action.

like image 20
Zian Choy Avatar answered Jun 03 '26 05:06

Zian Choy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!