0

I did a search and was not able to find it. We have a Spark TextArea with maxChars="3900". But it doesn't work when copy/paste to the text area. I tried to add this to the changingHandler:

 if (ta.text.length > 3900)
        {
                        Alert.show("The maximum characters length is 3900. Please limit the characters to the max limit");
                        ta.text = ta.text.substr(0, 3900);
                    } else 
                    {
                        if (event.operation is PasteOperation)
                        {
....//Other logic
                        }
       }

The problem is it does not work all the time. The Alert shows up only some times when it is over 3900 chars. Not sure why. I also added the same to the changeHandler as well. But that does not get triggered at all.

Please let know what I'm missing. I need to show an alert & trim the chars to the max each time it goes above the max limit.

Thanks

Harish

zero323
  • 322,348
  • 103
  • 959
  • 935
Harry
  • 546
  • 6
  • 22
  • 50
  • It doesn't work how? Are you receiving an error? OR unexpected behavior? Please provide details. – JeffryHouser Oct 25 '12 at 19:59
  • I have a word document with text count of 1600. I copy/pasted 3 times but did not trigger the alert. Then when I tried to copy the text in the textArea, then it decided to show the alert & them trim the text! Weird! – Harry Oct 25 '12 at 20:02
  • 1
    I'm still unclear what the problem is. It sounds like things are working exactly as you expected? – JeffryHouser Oct 25 '12 at 20:58
  • In your handler method, befor if block, try to trace your text string length. – Guoliang Oct 29 '12 at 05:55
  • @DavidLiGuoliang I tried it. I think the issue is when I paste a chunk of 1600 characters the first time, it shows the textArea.length = 0. When I paste the same chunk the 2nd time, now it shows as length = 1600 (it is actually 3200). So that's why it does not throw the alert when the size is > 3900. Any ideas on why this happens? – Harry Oct 31 '12 at 15:21
  • Okay the problem is it works if I use changeHandler instead of changingHandler. However I need to use the changingHandler for another logic. Is it possible to use both. For some reason they do not work together. Any suggestions please. – Harry Oct 31 '12 at 15:41

1 Answers1

2

First, we need to clear one thing: when the changing handler is triggered, that's means: the text is changing, but the change not apply yet.

if the text in your textare is ""(empty), now, I paste 1600 chars, the changing handler invoked, the length of the text is still 0, because it's changing, not change.

so now, if you got a change handler, when you trace the length, it should be 1600.

but, if you use "event.preventDefault();" in your changing method, and do nothing to change the text in your changing handler, your change handler should not be triggered.

so, my suggestion is :

  1. Using changing handler is correct.
  2. in the handler, get the text in clipboard, then you can get the length of the text in clipboard, with this length + textArea.text.length, you'll get the length if the paste processed. if the total lenght is bigger than your limition, you can prevent the event, and do whatever you want to do.

here is some code for you:

protected function textArea_changingHandler(event:TextOperationEvent):void
        {
            trace(event.type + " - " + textArea.text.length); //  this length is befor the paste

            if(event.operation is PasteOperation) {
                // Text in the clipboard.
                var textPaste:String = Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT) == null ? "" : Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT) as String; 
                // this length is after the paste(if the paste will complete)
                var totalLength:int = textArea.text.length + textPaste.length; 
                trace("String length after Paste: " + totalLength);

                if(totalLength > 3900) {
                    event.preventDefault();
                    textArea.text += "[Paste:" + textPaste.substr(0, 2) + "]"; // process your text here. 
                }
            }
        }
Guoliang
  • 885
  • 2
  • 12
  • 20