Customizing RichTextArea with insertHtml in GWT 2.0

479 views
Skip to first unread message

Trevis

unread,
Dec 31, 2009, 10:26:15 AM12/31/09
to Google Web Toolkit
I'd like to create a custom rich text aera. I think I I've got a fair
handle on how it works from playing with the showcase RichTextToolbar
source code. The Formatter.insertHtml method seems like it will get
almost exactly what i need. But, there is one critical peice of the
puzzle I have yet to solve. I need to grab the text that the user has
selected to make the HTML inserts work he way I need them to.

Am I missing it? How can I get the selected text?

Trevis

unread,
Jan 1, 2010, 1:07:31 PM1/1/10
to Google Web Toolkit
I feel like i must be missing something obvious but i'm not seeing any
convenience method to get the selected text. Seems too obvious to be
an oversight. Any help would be much appreciated.

Trevis

Anoop John

unread,
Jan 1, 2010, 1:10:46 PM1/1/10
to Google Web Toolkit
Hi Tevis,
If u want customize your text area you should use RichTextArea and a
toolbar for changing the font,color , creating a link etc. This tool
bar is freely available in http://gwt.google.com/samples/Showcase/Showcase.html#CwRichText.
Note that the code is available in gwt samples.

Anoop John

unread,
Jan 1, 2010, 1:11:51 PM1/1/10
to Google Web Toolkit

Trevis

unread,
Jan 1, 2010, 2:13:59 PM1/1/10
to Google Web Toolkit
Hey thanks,

I've seen that post which was what turned me on to RichTextToolbar
when i started to research this a couple of days ago. It looks great
and will definitely be the inspiration for how i create mine. The
problem is i want to do custom functionality that the raw Formatter
object doesn't supply. (custom youtube and embeding and a few more
things) which i can do just perfectly with insertHTML. The problem
is, i want to grab the text that the user has highlighted so that i
can use it when i insert the custom html but the API doesnt seem to
expose it. The custom RichTextToolbar implementation just calls
methods on RichTextArea and never actual seem to touch the selected
text directly. It just calls things like
RichTextArea.Formatter.createLink... which hands wrapping the selected
text internally.

I just wrote a hack work around for the time being so that i can
progress to other work but i'm still convinced that a getSelectedText
method exists somewhere. If it doesnt, it certainly should.

Here's the workaround that i just came up with:

private String crazyGetSelectedText(RichTextArea
rta) {
final String MARKER = "http://trevsmarker.com";
rta.getFormatter().createLink(MARKER);
String withMarker = rta.getHTML();

int markerIndex = withMarker.indexOf(MARKER);
int beginIndex = withMarker.indexOf('>', markerIndex)+1;
int endIndex = withMarker.indexOf('<', beginIndex);
String selected = withMarker.substring(beginIndex, endIndex);

rta.getFormatter().removeLink();
return selected;
}

That seems to get the job done but how horribly inefficient is that
solution?

Trevis

On Jan 1, 12:11 pm, Anoop John <anoopjoh...@gmail.com> wrote:
> Tevis Please see the post below.
>

> http://groups.google.com/group/Google-Web-Toolkit/browse_thread/threa...

Reply all
Reply to author
Forward
0 new messages