Saturday, November 24, 2012

Using

tag on Enter and
on Shift+Enter in RichTextArea - Solution

Hello,
I have spent some time to find solution to mentioned problem, as I saw that there are few topics asking for something like this.
So I extended RichTextArea class and here is my implementation of it.

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RichTextArea;

public class ExtendedRichTextArea extends RichTextArea implements ContextMenuHandler {
    private String content = "";
   
    public ExtendedRichTextArea() {
        sinkEvents(Event.MOUSEEVENTS | Event.KEYEVENTS | Event.ONCHANGE
                | Event.ONCLICK | Event.FOCUSEVENTS | Event.ONMOUSEUP
                | Event.ONDBLCLICK | Event.ONCONTEXTMENU);
    }

    @Override
    public void onBrowserEvent(Event event) {       
        switch (DOM.eventGetType(event)) {
            case Event.ONKEYPRESS:
                if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
                    if (!event.getShiftKey()) {
                        this.getFormatter().insertHTML("<p><br></p>");
                        event.preventDefault();
                    }
                }
                break;
            case Event.ONFOCUS:
                if (getHTML() == null || getHTML().isEmpty()) {
                    this.getFormatter().insertHTML("<p><br></p>");
                }
                break;
        }
    }
   
    @Override
    public String getHTML() {
        String html = super.getHTML();
        if(html.endsWith("<br>")) {
            return html.substring(0, super.getHTML().length() - 4);
        } else {
            return html;
        }
    }
}


Since browsers behave differently and in Firefox even thou in specs says that after inserting some data in block <br> tag should be removed, in order to keep response same for Firefox and in Chrome when retrieving data from text area you should replace response from getHTML() method like this:

ExtendedRichTextArea area = new ExtendedRichTextArea()
area.getHTML().replace("<br></p>", "</p>");

There are still some glitches with this, but hopefully someone will find it useful.

Cheers,
Milan

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/O286_QVm0NgJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate