Para testar é só digitar um texto, e pressionar Ctrl+Z para desfazer e Ctrl+Y para refazer.
*download do fonte: botão direito "View Source"
Código Fonte:
/*############################################################*/ /* */ /* Powered by: Samuel Facchinello */ /* http://desenvolvendoemflex.blogspot.com/ */ /* */ /*############################################################*/ package com.blogspot.desenvolvendoemflex.utils { import flash.events.Event; import flash.events.KeyboardEvent; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.controls.TextArea; /** * * @author SAMUEL FACCHINELLO * @site http://desenvolvendoemflex.blogspot.com/ */ public class TextAreaWithUndo extends TextArea { /** * Variable of control position Undo or Redo * */ [Bindable] private var _position:int = -1; public function get position():int { return _position; } /** * Variable that stores all typed letters * */ [Bindable] private var _array:ArrayCollection = new ArrayCollection(); public function get array():ArrayCollection { return _array; } /** * Constructor * */ public function TextAreaWithUndo() { super(); //add value default addValue(""); //add Event on KeyDown addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); } /** * Function with KeyDown * @param eventObj: KeyboardEvent * */ private function myKeyDown(eventObj:KeyboardEvent):void { // Was Ctrl key pressed? if (eventObj.ctrlKey) { var valueReturn:String = null; switch (eventObj.keyCode) { case 90: // Was Ctrl-Z pressed? valueReturn = returnPrevisionValue(); break; case 89: // Was Ctrl-Y pressed? valueReturn = returnNextValue(); break; default: break; } //if return exist if (valueReturn!=null) { //set text with return functions text = valueReturn; //select last _position in TextArea setSelection(text.length, text.length); } } else { //only adds to the list if any of these characters. //list from here: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html if ((eventObj.keyCode>=65&&eventObj.keyCode<=90)||(eventObj.keyCode>=48&&eventObj.keyCode<=57)||(eventObj.keyCode>=96&&eventObj.keyCode<=111)||(eventObj.keyCode>=186&&eventObj.keyCode<=222)) { addValue(text); } } } /** * Function that adds the letters in the array * @param value: propertie "text" of TextArea * */ private function addValue(value:String):void { //it was press Ctrl + Z ever, and was entered some value, clears the values of the position in which this forward if (_array.length>0&&_array.length-1>_position) { for (var i:int = _array.length-1; i>_position; i--) { _array.removeItemAt(i); } } //if the value is being added is equal to the last added, not added! if (_array.length>0) { if (value==_array.getItemAt(_array.length-1)) { return; } } _array.addItem(value); _position++; } /** * triggered when press Ctrl + Y * @return : Returns the next value from the list according to position * */ private function returnNextValue():String { if (_position+1>=0&&_position+1<_array.length) { _position++; return _array.getItemAt(_position) as String; } return null; } /** * triggered when press Ctrl + Z * @return : Returns the Prevision value from the list according to position * */ private function returnPrevisionValue():String { if (_position-1>=0) { _position--; return _array.getItemAt(_position) as String; } return null; } } }
Adicionei este componente ao Google Code, pode ser visto aqui: http://code.google.com/p/textarea-with-undo/
Qualquer elogio, dúvida, sugestão ou melhoria para o componente, deixe um comentário!!
Abraço, até a próxima.
*Tradução para o inglês com o Google Translate. :)