如何将剪切/粘贴(通过jsni)方法注册到RichTextArea

How to register cut/paste (by jsni) method to RichTextArea?

本文关键字:jsni 方法 注册 RichTextArea 通过 粘贴      更新时间:2023-09-26

以下代码用于将属性样式设置为body元素,并向其注册剪切/粘贴代码,但失败了。奇怪的是,在调试时,变量body不为空,并且属性";风格";存在,但框架没有这个孩子。运行工作没有按预期工作,并且NPE在调用setBodyContent()时被抛出。

public class TextAreaExt extends RichTextArea
{
private BodyElement body;
@Override
protected void onLoad()
{
    super.onLoad();
    body = ((FrameElement) getElement().cast())
                .getContentDocument().getBody();
    body.setAttribute("style", "color:gray;");
    registerOnCut(body);
}
public void setBodyContent(String text)
{
    body.setInnerText(text);
}
private native void registerOnCut(BodyElement element) /*-{
    var that = this;
    console.log("registerOnCut");
    element.oncut = $entry(function(event) {
        //invoke method to adjust height based on content
        return false;
    });
    element.onpaste = $entry(function(event) {
        //invoke method to adjust height based on content
        return false;
    });
}-*/;
}

不要调用onLoad方法来初始化主体,而是使用addInitializeHandler进行初始化。

  this.addInitializeHandler(new InitializeHandler() {
        @Override
        public void onInitialize(InitializeEvent event) {
            body = ((FrameElement)getElement().cast()).getContentDocument().getBody();
            registerOnCut(body);
        }
    });