WHAT'S NEW?
Loading...

UndoRedo class as3


package com.PustakaFlash.text 
{
 import flash.text.TextField;
 import flash.events.Event;
 import flash.utils.Timer;
 import flash.events.TimerEvent;
 import flash.events.TextEvent;

 public class UndoRedo
 {
  private var a:Array = new Array();
  private var r:Array = new Array();
  private var c:TextField;
  private var d:String;
  private var e:String;
  private var f:Timer;
  private var g:int = 5;
  
  public function UndoRedo():void
  /* 
   * import com.PustakaFlash.text.*;
   * var hm:UndoRedo = new UndoRedo();
   * inputTxt.text = "";
   * hm.mulai(inputTxt,500,5);
   * inputTxt.addEventListener(Event.CHANGE, onChange);
   *  function onChange(e:Event):void
   * {
   * }
   * //------------------------------------------------------------
   * undo.addEventListener(MouseEvent.CLICK, onUndo);
   * redo.addEventListener(MouseEvent.CLICK, onRedo);
   * 
   * function onUndo(e:MouseEvent):void
   * {
   * hm.Undo();
   * }
   * function onRedo(e:MouseEvent):void
   * {
   * hm.Redo();
   * }
  */
  public function mulai(textfield:TextField,sensitivity:int,gory:int):void
  {
   c = textfield;
   d = c.text;
   g = gory;
   
   if (sensitivity < 1)
   {
    sensitivity = 1;  
   }
   if (sensitivity > 50000)
   {
    sensitivity = 50000; 
   }
   
   f = new Timer(sensitivity);
   
   c.addEventListener(Event.CHANGE, onChange);
   c.addEventListener(TextEvent.TEXT_INPUT, onInput);
   f.addEventListener(TimerEvent.TIMER, onTimer);
  }
  
  private function onChange(e:Event):void
  {
   f.reset();       
   f.start();
  }
  
  private function onInput(e:TextEvent):void
  {
   r = [];
  }
  
  private function onTimer(e:TimerEvent):void
  {
   f.stop();      
   saveHistory();
  }
  
  private function saveHistory():void
  {
   a.push(d);  
   
   if (a.length > g)
   {
    a.shift(); 
   }
   
   d = c.text;   
   e = d;
  }
  
  
  public function clearHistory():void
  {
   a = [c.text];
   r = [];
   d = c.text;
   e = "";
  }
  
  
  public function Undo():void
  {
   var undoID:int = a.length-1;
   var temp:String = "";
   
   if (undoID != -1)     
   {
    temp = a[undoID];  
    r.push(e);  
    a.splice(undoID, 1); 
    
    d = temp;
    e = temp;
   }
   else
   {
    temp = c.text;   
   }
   
   c.text = temp;    
  }
  
  public function Redo():void
  {
   var redoID:int = r.length-1;
   var temp:String = "";
   
   if (redoID != -1)     
   {
    temp = r[redoID];  
    a.push(temp);   
    r.splice(redoID, 1); 
   }
   else
   {
    temp = c.text;   
   }
   
   c.text = temp;    
  }
 }
}