Search code examples
eventsgwtcopy-pasterichtextarea

GWT capture event in RichTextArea or in IFrame


I have a RichTextArea

 private RichTextArea richTextArea;

and I'm trying to capture a paste event like this:

DOM.sinkEvents((com.google.gwt.user.client.Element) richTextArea.getElement(), com.google.gwt.user.client.Event.ONPASTE);
DOM.setEventListener((com.google.gwt.user.client.Element) richTextArea.getElement(), new EventListener(){
  @Override public void onBrowserEvent(Event event) {
     switch (event.getTypeInt()) {
        case Event.ONPASTE: Window.alert("hey");break;
     }
   }
});

But it doesn't work, when I paste text on the richTextArea the alert is not triggered.

Any idea how to capture this paste event?

Thanks!


Solution

  • You cannot add the event to the RichTextArea, which actually is an iframe, but to it's body.

    Although you could use jsni, I would use gwtquery because its simplicity:

    // First attach the widget to the DOM
    RootPanel.get().add(richTextArea);
    
    // We only can bind events to the content, once the iframe document has been created, 
    // and this happens after it has been attached. Note that richtTextArea uses a timeout
    // to initialize, so we have to delay the event binding as well
    $(richTextArea).delay(1, lazy().contents().find("body").bind(Event.ONPASTE, new Function(){
      @Override public boolean f(Event e) {
        Window.alert("OnPaste");
        return true;
      }
    }).done());