Using ACE with WT

后端 未结 1 1822
温柔的废话
温柔的废话 2020-12-10 23:01

UPDATE 3 Final working code below. YOU NEED THE ace.js FROM THE src FOLDER! It will not work from the libs, you need the pre-packaged version from their sit

相关标签:
1条回答
  • 2020-12-10 23:11

    Maybe this puts you in the right direction:

    
    wApp->require("lib/ace/ace.js")
    // A WContainerWidget is rendered as a div
    WContainerWidget *editor = new WContainerWidget(parent);
    // editor->jsRef() is a text string that will be the element when executed in JS
    editor->doJavaScript(
        editor->jsRef() + ".editor = ace.edit(" + editor->jsRef() + ");" +
        editor->jsRef() + ".editor.setTheme('ace/theme/monokai');" +
        editor->jsRef() + ".editor.getSession().setMode('ace/mode/javascript');"
      );
    

    That should decorate the editor. Wt does not automatically send the modifications to a div to the server, so you do this manually through a JSignal (emits a signal from JS to C++):

    
    JSignal <std::string> *jsignal = new JSignal<std::string>(editor, "textChanged");
    jsignal->connect(this, MyClass::textChanged);
    
    WPushButton *b = new WPushButton("Save", parent);
    b->clicked().connect("function(object, event) {" +
        jsignal->createCall(editor->jsRef() + ".editor.getValue()") +
      ";}");
    

    (code above is not tested so you may need to adjust a bit)

    I have integrated CodeMirror in an earlier JWt (java) project like this:

    
    import eu.webtoolkit.jwt.WApplication;
    import eu.webtoolkit.jwt.WContainerWidget;
    import eu.webtoolkit.jwt.WTextArea;
    
    public class CodeMirrorTextArea extends WContainerWidget {
            private WTextArea textArea;
            public CodeMirrorTextArea(WContainerWidget parent) {
                    super(parent);
    
                    textArea = new WTextArea(this);
    
                    WApplication app = WApplication.getInstance();
    
                    app.require(app.resolveRelativeUrl("codemirror-2.32/lib/codemirror.js"));
                    app.require(app.resolveRelativeUrl("codemirror-2.32/mode/groovy/groovy.js"));
    
                    //TODO:
                    //We save the editor state to the text area on each key stroke,
                    //it appears to be not a performance issue,
                    //however it might very well become one when editing larger fragments of code.
                    //A better solution would be to save this state to the text area only when
                    //the form is submitted, currently this is not yet possible in Wt???.
    
                    String js =
                            "var e = " + textArea.getJsRef() + ";" +
                            "var cm = CodeMirror.fromTextArea(e, {" +
                            "       onKeyEvent : function (editor, event) {" +
                        "           editor.save();" +
                        "   }," +
                            "       lineNumbers: true" +
                            "       });" +
                            "var self = " + getJsRef() + ";" +
                            "self.cm = cm;";
    
                    this.doJavaScript(js);
            }
    
            public CodeMirrorTextArea() {
                    this(null);
            }
    
            public void setText(String text) {
                    textArea.setText(text);
            }
    
            public String getText() {
                    return textArea.getText();
            }
    
            public void setMarker(int line, String htmlMarker) {
                    String js =
                            "var self = " + getJsRef() + ";" +
                            "self.cm.setMarker(" + line + ", " + jsStringLiteral(htmlMarker +
    "%N%") + ");";
    
                    this.doJavaScript(js);
            }
    
            public void clearMarker(int line) {
                    String js =
                            "var self = " + getJsRef() + ";" +
                            "self.cm.clearMarker(" + line + ");";
    
                    this.doJavaScript(js);
            }
    }
    
    0 讨论(0)
提交回复
热议问题