« First Flex Camp Romania in 2008
» Adobe Player Security Update

ActionScript, How to, RIA

Find Cursor Position in a HtmlText Object (RichTextEditor, TextArea, TextField)

18.03.08 | 11 Comments

Table of contents

  1. Find Cursor Position in a HtmlText Object (RichTextEditor, TextArea, TextField)
  2. Find Cursor Position in a HtmlText Object (RichTextEditor, TextArea, TextField) - UPDATE

Last task I finished was something linked with templates. I needed to create a simple template engine where adding a field was a necessity. I started using RichTextEditor which is based on a TextArea so it implements htmlText property. That is an easy way having all that nice functionalities like bold, italic, font choosing, color chooser, alignment and others, in your application.

What I needed is to insert some tags at a specific point in the text. The problem I run into was that RTE (RichTextEditor) shows you the position of the cursor in the text but not in the htmlText which is not a bug.

For example… I had the text bellow:

<TEXTFORMAT LEADING="2"><P ALIGN="LEFT">
<FONT SIZE="13" COLOR="#555555" LETTERSPACING="0" KERNING="0">
asd asd {asd asd asdasd <U>asd</U>  mmm
</FONT></P></TEXTFORMAT>

which has HTML tags in it as is rendered as this:

asd asd {asd asd asdasd asd mmm

When positioning the cursor after the first group “asd” the normal the selection.beginIndex property of the RichTextEditor displayed the position in the normal text, which is three, but in my case I needed the position in the HTML string (htmlText) to be able to insert there my tag.

I asked some of my flex colleagues and we found a way of counting the position in the htmlText from the position in the normal text. The way is parsing char by char the htmlText string and where the < char was found we set a flag which will enable us to know when we are inside a tag this way not counting that chars. You can see everything at the end of this post in the calculateHtmlPosition function.

Important thing to mention (example)…

htmlText:

<u>asd</u> asd

text:

asd asd

When the cursor is before “asd” in the RTE the position in the htmlText is 0 - that means that the cursor is positioned before the <u> opening tag. But… when the cursor is positioned after “asd” the position in the htmlText excludes the closing </u> tag. If the cursor is after the space and just before the second “asd” the position is including both closing tag and the space - so 5 more characters. This how it should work. You can see this in many other applications like Microsoft Word where, if you are starting to type immediately after an underlined block of text, your new inserted text is also underlined.

I hope you understood this article. There may be other ways to achieve this but this is my way of resolving it and if you found one of those other ways please tell us. We are willing to learn how to do it better.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import mx.containers.HBox;
            import mx.controls.Alert;
            import mx.controls.Button;
            import mx.controls.RichTextEditor;
 
            public var RTE:RichTextEditor = new RichTextEditor();
            public var RTEText:String = '<TEXTFORMAT LEADING="2">
                <P ALIGN="LEFT">
                <FONT SIZE="13" COLOR="#555555" LETTERSPACING="0" KERNING="0">
                asd asd {asd asd asdasd <U>asd</U>  mmm
                </FONT></P></TEXTFORMAT>';
 
            public function init():void
            {
                RTE.htmlText = RTEText;
                addChild(RTE);
 
                var hbox:HBox = new HBox();
                hbox.y = 310;
 
                var btn:Button = new Button();
                btn.label = "HTML";
                var btn_pos:Button = new Button();
                btn_pos.label = "Pos";
                var btn_pos2:Button = new Button();
                btn_pos2.label = "Norm Pos";
                hbox.addChild(btn);
                hbox.addChild(btn_pos);
                hbox.addChild(btn_pos2);
 
                addChild(hbox);
 
                btn.addEventListener(MouseEvent.CLICK,showHTML);
                btn_pos.addEventListener(MouseEvent.CLICK,getHtmlPosition);
                btn_pos2.addEventListener(MouseEvent.CLICK,getPosition);
            }
 
            // This function is OBSOLETE - Please see next article in this Serie
            public function calculateHtmlPosition(htmlstr:String, pos:int):int
            {
                if (pos <= -1)
                    return -1;
 
                var htmlPos:int = 0;
                var isInsideTag:Boolean = false;
                var cnt:int = 0;
                for (var i:int = 0; i < htmlstr.length; i++)
                {
                    if (cnt>=pos) 
                        break;
                    var currentChar:String = htmlstr.charAt(i);
                    if (currentChar == "<")
                        isInsideTag = true;
                    if (!isInsideTag)
                        cnt++;
                    if (currentChar ==">")
                        isInsideTag = false;
                }
                return i;
            }
 
            public function showHTML(event:MouseEvent):void
            {
                Alert.show(RTE.htmlText);
            }
 
            public function getHtmlPosition(event:MouseEvent):void
            {
                Alert.show(calculateHtmlPosition(RTE.htmlText, RTE.selection.beginIndex).toString());
            }
 
            public function getPosition(event:MouseEvent):void
            {
                Alert.show(RTE.selection.beginIndex.toString());
            }
        ]]>
    </mx:Script>
</mx:Application>

Popularity: 84%

Share and Enjoy:
  • Technorati
  • StumbleUpon
  • del.icio.us
  • NewsVine
  • Reddit
  • Digg
  • Furl
  • co.mments
  • blogmarks
  • Slashdot
  • DZone
  • Taggly
  • YahooMyWeb
  • connotea
  • Webride




Tags: , , , ,

This post was written by Andrei Ionescu

Views: 2552

related

popular

11 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:

:


« First Flex Camp Romania in 2008
» Adobe Player Security Update