Skip to main content
Participating Frequently
November 4, 2009
Question

Capturing Text that was just Pasted

  • November 4, 2009
  • 1 reply
  • 477 views

Hello,

It seems there should be a way to do this but I just can't seem to figure it out.

If text is pasted into a Spark TextArea, I want to be able to capture the text that was just pasted into a string or even better capture the start/end positions of the pasted characters in the TextArea's textFlow.

I know there has to be a way to do this yet I can't seem to find it.

Thanks in advance.

This topic has been closed for replies.

1 reply

emansouriAuthor
Participating Frequently
November 4, 2009

I was just able to figure this out in the last hour or so with help from another thread.

It turns out the key to this is in the FlowOperationEvent.

TextFlow dispatches a FlowOperation.FLOW_OPERATION_BEGIN and FlowOperation.FLOW_OPERATION_END event whenever there is an operation that changes the flow of the TextFlow.

The FlowOperationEvent has an operation:FlowOperation property that you must cast to a PasteOperation.

The PasteOperation has a bug in it and the value of PasteOperation.absoluteEnd is always the same as PasteOperation.absoluteStart.

So you have to look at the change in length of the textFlow text to determine the new content that was pasted.

Here is code I am using in my implementation.  Note PasteEvent is my custom event type.

        private var _textLengthFlowStart:uint;
        private var _textLengthFlowEnd:uint;

            _textArea.textFlow.addEventListener(FlowOperationEvent.FLOW_OPERATION_BEGIN,flowBeginHandler);
            _textArea.textFlow.addEventListener(FlowOperationEvent.FLOW_OPERATION_END,flowEndHandler);


        private function flowBeginHandler(event:FlowOperationEvent):void
        {
            var flowOperation:FlowOperation = event.operation;
            var operationType:String = flash.utils.getQualifiedClassName(flowOperation).split("::")[1];
           
            switch(operationType)
            {
                case "PasteOperation":
                    _textLengthFlowStart = flowOperation.textFlow.textLength;
                    var pasteOperation:PasteOperation = PasteOperation(flowOperation);
                    break;
            }
           
        }
       
        private function flowEndHandler(event:FlowOperationEvent):void
        {
            var flowOperation:FlowOperation = event.operation;
            var operationType:String = flash.utils.getQualifiedClassName(flowOperation).split("::")[1];
           
            switch(operationType)
            {
                case "PasteOperation":
                    _textLengthFlowEnd = flowOperation.textFlow.textLength;
                    var pasteOperation:PasteOperation = PasteOperation(flowOperation);
                    var startIndex:uint = pasteOperation.absoluteStart;
                    var endIndex:uint = startIndex + (_textLengthFlowEnd - _textLengthFlowStart);
                    var pastedText:String = textArea.text.slice(startIndex,endIndex);
                    textArea.dispatchEvent(new PasteEvent(PasteEvent.PASTE_COMPLETE,pastedText,startIndex));
                    break;
            }
        }