How to setcursor position in GWT RichTextArea

2019-04-07 17:59发布

问题:

Is there a way to set the cusror position in GWT RichTextArea. There is method setCusrorPosition() to do so in TextArea, but not in RichTextArea. Perhaps there is a native JavaScript (called from GWT) that could set the cursor position in the RichTextArea?

回答1:

You are right RichTextArea is not providing the setSelectionRange method, but i have created one using the JSNI.

Below is the method,

public native void setSelectionRange(Element elem, int pos, int length) /*-{
    try {
        var selection = null, range2 = null;
        var iframeWindow = elem.contentWindow;
        var iframeDocument = iframeWindow.document;

        selection = iframeWindow.getSelection();
        range2 = selection.getRangeAt(0);

        //create new range
        var range = iframeDocument.createRange();
        range.setStart(selection.anchorNode, pos);
        range.setEnd(selection.anchorNode, length);

        //remove the old range and add the newly created range
        if (selection.removeRange) { // Firefox, Opera, IE after version 9
            selection.removeRange(range2);
        } else {
            if (selection.removeAllRanges) { // Safari, Google Chrome
                selection.removeAllRanges();
            }
        }
        selection.addRange(range);
    } catch (e) {
        $wnd.alert(e);
    }
}-*/;

For using above method write below code:

final RichTextArea tr = new RichTextArea();
    Button b = new Button("Test");
    b.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            setSelectionRange(tr.getElement(), 15, 20);
            tr.setFocus(true);
        }
    });
    RootPanel.get().add(tr);
    RootPanel.get().add(b);

Note: Do remember to put the validation checks of "pos" and "length" you are passing in setSelectionRange() method. This code had been tested in IE9, FF, Chrome.



回答2:

Not sure if this is still required, but I have been trying to get this working all day and have finally managed to hack my way to a solution. This has only been tested in Chrome/Safari. Hope it helps someone.

public static native void setCursor(Element elem, int pos, int length) /*-{
    var node = elem.contentWindow.document.body;
    var range = elem.contentWindow.getSelection().getRangeAt(0);

    var treeWalker = $doc.createTreeWalker(node, NodeFilter.SHOW_TEXT, function(node) {
        var nodeRange = $doc.createRange();
        nodeRange.selectNodeContents(node);
        return NodeFilter.FILTER_ACCEPT;
    });

    var charCount = 0;
    while (treeWalker.nextNode()) {
        if (charCount + treeWalker.currentNode.length > pos)
            break;

        charCount += treeWalker.currentNode.length;
    }

    var newRange = elem.contentWindow.document.createRange();
    newRange.setStart(treeWalker.currentNode, 1);
    newRange.setEnd(treeWalker.currentNode, 1);

    var selection = elem.contentWindow.getSelection();

    if (selection.removeRange) { // Firefox, Opera, IE after version 9
        selection.removeRange(range);
    } else if (selection.removeAllRanges) { // Safari, Google Chrome
        selection.removeAllRanges();
    }

    selection.addRange(newRange);
}-*/;

This code was edited on November 28, 2016 to correct for minor syntax errors.