当前位置: 首页 > 面试题库 >

与WT一起使用ACE

谢阳曜
2023-03-14
问题内容

UPDATE 3 下面的最终工作代码。您需要src文件夹中的ace.js!从库中无法使用,您需要从其站点下载预包装的版本。

WText *editor = new WText(root());
editor->setText("function(){\n hello.abc();\n}\n");
editor->setInline(false);

上面的代码可以设置ACE窗口的内容。

MyClass::MyClass(const WEnvironment& env)
: WApplication(env)
{
wApp->require("ace-builds/src/ace.js");
// A WContainerWidget is rendered as a div
WContainerWidget *editor = new WContainerWidget(root());
editor->resize(500, 500);

std::string editor_ref = editor->jsRef(); // is a text string that will be the element when executed in JS

std::string command = 
  editor_ref + ".editor = ace.edit(" + editor_ref + ");" +
  editor_ref + ".editor.setTheme(\"ace/theme/monokai\");" +
  editor_ref + ".editor.getSession().setMode(\"ace/mode/javascript\");";

editor->doJavaScript(command);


JSignal <std::string> *jsignal = new JSignal<std::string>(editor, "textChanged");
jsignal->connect(this, &MyClass::textChanged);

WPushButton *b = new WPushButton("Save", root());

command = "function(object, event) {" +
  jsignal->createCall(editor_ref + ".editor.getValue()") +
  ";}";

b->clicked().connect(command);
}

void MyClass::textChanged(std::string incoming)
{

}

更新2 这是我的项目看起来像atm的样子,仍然在右上角显示了白色的屏幕,并带有来自WT的红色“正在加载…”消息。下面有更多注释。

MyClass::MyClass(const WEnvironment& env)
: WApplication(env)
{
wApp->require("lib/ace/ace.js");
// A WContainerWidget is rendered as a div
WContainerWidget *editor = new WContainerWidget(root());
editor->resize(500, 500);

std::string editor_ref = editor->jsRef(); // is a text string that will be the element when executed in JS

std::string command = 
  editor_ref + ".editor = ace.edit(" + editor_ref + ");" +
  editor_ref + ".editor.setTheme(\"ace/theme/monokai\");" +
  editor_ref + ".editor.getSession().setMode(\"ace/mode/javascript\");";

editor->doJavaScript(command);


JSignal <std::string> *jsignal = new JSignal<std::string>(editor, "textChanged");
jsignal->connect(this, &MyClass::textChanged);

WPushButton *b = new WPushButton("Save", root());

command = "function(object, event) {" +
  jsignal->createCall(editor_ref + ".editor.getValue()") +
  ";}";

b->clicked().connect(command);
}

void MyClass::textChanged(std::string incoming)
{

}

当用于编辑器-> doJavaScript(command)时,“ command”变量等于以下变量

"Wt3_3_0.$('oy4ycjy').editor = ace.edit(Wt3_3_0.$('oy4ycjy'));Wt3_3_0.$('oy4ycjy').editor.setTheme('ace/theme/monokai');Wt3_3_0.$('oy4ycjy').editor.getSession().setMode('ace/mode/javascript');"

当它用于b-> clicked()。connect(command);时,“ command”变量等于以下变量:

"function(object, event) {Wt.emit('oy4ycjy','textChanged',Wt3_3_0.$('oy4ycjy').editor.getValue());;}"

更新1

向我的构造函数添加了建议的代码,但是页面不会从纯白色屏幕更改。在这个WT项目中,我什么也没做,只运行了这段代码。

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

editor_ref的值是“ Wt3_3_0。$(’oumvrgm’)”减去引号。

还尝试在下面添加代码,但该页面仍然空白。

JSignal <std::string> *jsignal = new JSignal<std::string>(editor, "textChanged");
jsignal->connect(this, &MyClass::textChanged);

WPushButton *b = new WPushButton("Save", root());
b->clicked().connect("function(object, event) {" +
  jsignal->createCall(editor->jsRef() + ".editor.getValue()") +
  ";}");

我也发现将其注释掉

editor_ref + ".editor = ace.edit('" + editor_ref + "');" +

使按钮显示出来,但是屏幕右上方有一个红色的“正在加载…”注释,因此WT正在等待某些操作。

我现在将textChanged作为不执行任何操作的功能。

原始帖子

所以,我的问题是这个。如何在WT
http://www.webtoolkit.eu/wt中获得ACE
http://ace.ajax.org/#nav=about。更具体地说,在WT Wt
:: WTextArea或Wt ::
WTabWidget中的ACE中,首选文本区域。我已经尝试这样做了几天,但并没有取得太大的成功。

我已经能够将ACE嵌入HTML页面中,因为他们的网站说“只需将其复制并粘贴到您的页面中”就可以了,这真的很简单。但是,我需要通过WT将其本地加载到容器中。我将他们的仓库从GIT下载到我的机器上,并尝试使用

require("lib/ace/ace.js");

doJavaScript(...);

使用各种命令无法成功…我在Java和HTML方面不如C ++强大,因此如果涉及到很多Java / HTML,我将要求尽可能多的细节。在此先感谢队友!


问题答案:

也许这使您朝着正确的方向前进:

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');"
  );

那应该装饰编辑器。Wt不会自动将对div的修改发送到服务器,因此您可以通过JSignal手动执行此操作(发出从JS到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()") +
  ";}");

(上面的代码未经测试,因此您可能需要稍作调整)

我已经将CodeMirror集成在早期的JWt(java)项目中,如下所示:

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);
        }
}


 类似资料:
  • 我有一个烧瓶服务器运行在http://127.0.0.1:5000和一个vuejs前端运行http://localhost:8080我已经做了api,并用postman测试了它,一切都如预期的那样工作:( > 将请求发布到/登录- (将请求发送至/登录)- 烧瓶API代码: 登录。vue: 指数vue 当我使用邮递员登录时,我得到的响应为;当我使用邮递员获取url/索引时,我得到响应。数据但当我使

  • Wt

    Wt(音同'witty')是一个C++库,同时也是开发和部署web应用的服务器。 Wt不是所谓框架(framework),它只是一个库,它不会将编程方式强加于开发者。 Wt的API是以widget为中心(widget-centric)的,并受到现有C++图形用户界面(GUI) 的应用编程接口(APIs)的启发。Wt为开发者提供了几乎所有web实现细节的抽象,其中 包括事件处理和图像支持。 基于页面

  • wt-console 是一个轻巧、可扩展的本机开发人员和测试人员工具。注:从WeTrident项目提炼而来。 �� 特性 通过简单的接入即可在App内查看日志,提供优美的日志格式展示。 结合 wt-console-server 可以很方便的实现日志上传功能。 �� 截图 �� 基本用法 将TianYan嵌入到App最外层View中: export default class SimpleApp e

  • 问题内容: 我已经使用Selenium和最初的PhantomJS开发了一些Python脚本。在走向自动下载时,我改用了(带头的)Firefox(运行了),然后选择了无头选项的Chrome,这样我就不会打开浏览器了。 我的第一个脚本访问一个页面和几个HTML元素,与无头Chrome完美搭配。 但是第二个 仅适用于带头的Chrome 。如果添加“无头”选项,它将不再起作用。当我尝试以无头模式打印HTM

  • 问题内容: 如果您有一个Array,并且想使用Java8 forEach()方法,那么哪种方法更好或更有效: 要么 差异是否显着,或者有更好的解决方案来解决? 问题答案: 都不行 如果您已经有一个数组, 我会用: 因为您将数组到流的转换留给了JDK-让它负责效率等。 但是,由于您没有数组,因此我将使用的varargs创建值的流: 这又让JDK负责有效地按需传输值。

  • 问题内容: 因此,我一直在为这个(应该是)简单的练习而绞尽脑汁,以使该程序将日期字符串转换为对象,对其进行格式化,并在完成后将其作为字符串再次返回。 这是程序的最后一点,它从文件中获取一小段文本,将其分解为单独的记录,然后将记录分解为单独的数据并将它们分配给个人对象。 我已经在多个位置检查了该代码,并且该代码完全执行了应该执行的操作,直到调用了format函数(该函数抛出)为止。为对象分配了应该分