0

I have a simple text editor with cut, paste and undo buttons. I have this for cut and paste but i don't how to create undo button?

var clipboardFmt:TextFormat = new TextFormat();
var initialPoint:Number = new Number();
var finalpoint:Number = new Number();
var clipBoard:String = new String();

cut_button.addEventListener(MouseEvent.CLICK,cutText);
paste_button.addEventListener(MouseEvent.CLICK, pastefromClipboard);

function cutText(event:MouseEvent):void
{
    clipBoard = txt.text.substring(txt.selectionBeginIndex,txt.selectionEndIndex);
    System.setClipboard(clipBoard);
    txt.replaceText(txt.selectionBeginIndex,txt.selectionEndIndex,"");
}

function pastefromClipboard(e:Event):void
{
    txt.replaceText(txt.selectionBeginIndex,txt.selectionEndIndex,clipBoard);
    finalpoint = initialPoint + clipBoard.length;
    txt.setSelection(initialPoint,finalpoint);
    txt.setTextFormat(clipboardFmt, initialPoint,finalpoint);
}

txt.addEventListener(Event.CHANGE,count);
function count(e:Event):void
{
    wn.text = countWords(txt.text);
    function countWords(input:String):int
    {
        return input.match(/[^\s]+/g).length;
    }
}
4

2 回答 2

0

You need to store a copy of text-field text in a variable. Each time text-field's value is being changed but before the change is applied (the event is called TextEvent.TEXT_INPUT) save a copy of text-field value into this variable. When undo button is pressed set text-field value to a value stored in this variable.

This is the simplest solution which will allow you to undo only one step back. You can use an array instead of single variable in order to store several states of text-field which will allow you to undo several steps back.

于 2013-08-09T11:02:00.830 回答
0

创建一个基于数组的撤消系统。不要使用剪贴板,它不会工作。

于 2013-08-09T11:06:16.687 回答