Have you ever noticed that the Javafx TextArea undo / redo seems pretty unreliable?
It seems to be because when you set any text directly on the TextArea with Textarea1.Text = ??? it resets the Undo state. (I haven't been able to confirm this in any documentation as I can't find anything relevant).
So how can we get around that? The TextArea API has many commands available to do regular tasks like insert text, replace text, replace selected text and many others. Using these commands instead of directly setting the text keeps the TextArea's Undo Manager in a state that is useful.
The attached B4xLib contains the available commands and has 2 modules, A Code module which is a static version of the library to which you need to pass the target TextArea to every sub and a Class module which needs to be declared an initialized with the Target TextArea. It's up to you which version you try or use both. Most of the commands are used directly by the control to navigate, cut and paste etc, so you probably won't need to use them, but I added them for the occasions that you do.
Update V0.2
I hope you find them useful.
It seems to be because when you set any text directly on the TextArea with Textarea1.Text = ??? it resets the Undo state. (I haven't been able to confirm this in any documentation as I can't find anything relevant).
So how can we get around that? The TextArea API has many commands available to do regular tasks like insert text, replace text, replace selected text and many others. Using these commands instead of directly setting the text keeps the TextArea's Undo Manager in a state that is useful.
The attached B4xLib contains the available commands and has 2 modules, A Code module which is a static version of the library to which you need to pass the target TextArea to every sub and a Class module which needs to be declared an initialized with the Target TextArea. It's up to you which version you try or use both. Most of the commands are used directly by the control to navigate, cut and paste etc, so you probably won't need to use them, but I added them for the occasions that you do.
Class : TA_Commands ShortName : TA_Commands
Methods:
Initialize(TA As TextArea)
Initializes the object. You can add parameters to this method if needed.
Backward
Moves the caret position backward.
CancelEdit
If the field is currently being edited, this call will set text to the last commited value.
Clear
Clears the text.
CommitValue
Commit the current text and convert it to a value.
Copy
Transfers the currently selected range in the text to the clipboard, leaving the current selection.
Cut
Transfers the currently selected range in the text to the clipboard, removing the current selection.
DeleteNextChar As Boolean
Deletes the character that follows the current caret position from the text if there is no selection, or deletes the selection if there is one.
DeletePreviousChar As Boolean
Deletes the character that precedes the current caret position from the text if there is no selection, or deletes the selection if there is one.
DeleteText(Start As Int, TEnd As Int)
Removes a range of characters from the content.
Deselect
Clears the selection.
End_
Moves the caret to after the last char of the text.
EndOfNextWord
Moves the caret to the end of the next word.
ExtendSelection(Pos As Int)
This function will extend the selection to include the specified pos.
Forward
Moves the caret position forward.
GetSelectedText As String
Gets the value of the property selectedText.
GetText(Start As Int, TEnd As Int) As String
Returns a subset of the text input's content.
Home
Moves the caret to before the first char of the text.
InsertText(Index As Int, Text As String)
Inserts a sequence of characters into the content.
IsRedoable As Boolean
Gets the value of the property redoable.
IsUndoable As Boolean
Gets the value of the property undoable.
NextWord
Moves the caret to the beginning of next word.
Paste
Transfers the contents in the clipboard into this text, replacing the current selection.
PositionCaret(Pos As Int)
Positions the caret to the position indicated by pos.
PreviousWord
Moves the caret to the beginning of previous word.
Redo
If possible, redoes the last undone modification.
ReplaceSelection(Replacement As String)
Replaces the selection with the given replacement String.
ReplaceText(Start As Int, TEnd As Int, Text As String)
Replaces a range of characters with the given text.
SelectBackward
Moves the selection backward one char in the text.
SelectEnd
Moves the caret to after the last char of text.
SelectEndOfNextWord
Moves the caret to the end of the next word.
SelectForward
Moves the selection forward one char in the text.
SelectHome
Moves the caret to before the first char of text.
SelectNextWord
Moves the caret to the beginning of next word.
SelectPositionCaret(Pos As Int)
Positions the caret to the position indicated by pos and extends the selection, if there is one.
SelectPreviousWord
Moves the caret to the beginning of previous word.
SelectRange(Anchor As Int, CaretPosition As Int)
Positions the anchor and caretPosition explicitly
Undo
If possible, undoes the last modification.
Update V0.2
Fixed erroneous comments
Changed type of passed TextControl to Object so it will work with TextField as well as TextArea.
I hope you find them useful.
Attachments
Last edited: