4

我正在寻找一种将 CKEditor 集成到我的 GWT 项目中的方法。

我做了一些谷歌搜索,发现了这个项目:https ://code.google.com/p/gwt-ckeditor/ 已被废弃多年。所以CKEditor已经完全过时了。

我还看到 CKEditor 在 GWT 之外被加载到在 GWT 中创建的文本区域中。我不确定这是否是一个好方法。

如果有人能给我一些建议,将不胜感激。提前致谢

4

4 回答 4

5

您可以使用 JSNI 来激活 CKEditor。要加载 javascript 文件,您可以在 html 页面中加载它,或者使用ScriptInjectorStyleInjector

在 GWT 中,创建一个组件:

package com.google.editor;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.TakesValue;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;

public class CKeditor extends Composite implements TakesValue<String> {
  TextArea text = new TextArea();
  protected JavaScriptObject editor;

  public CKeditor() {
    initWidget(text);
  }

  @Override
  protected void onAttach() {
    super.onAttach();
    initCKEditor(text.getElement().getId());
  }

  private native void initCKEditor(String id) /*-{
    this.@com.google.editor.CKeditor::editor =  CKEDITOR.replace( id );
  }-*/;

  @Override
  public native void setValue(String value) /*-{
    this.@com.google.editor.CKeditor::editor.setData(value);
  }-*/;

  @Override
  public native String getValue() /*-{
    this.@com.google.editor.CKeditor::editor.setData(value);
  }-*/;
}

这是一个示例,添加您要在 CKEditor 中设置的所有配置

于 2013-09-23T13:52:56.437 回答
3

我还建议使用 ScriptInjector,因为它会为您提供脚本最终加载且一切正常的回调。

此后,您必须使用 $wnd 正确处理 CKEDITOR 并替换本机代码中的 textarea:

  private native void initCKEditor(String id) /*-{
    this.@com.google.editor.CKeditor::editor =  $wnd.CKEDITOR.replace( id );
  }-*/;
于 2014-10-26T09:08:07.617 回答
1

Patrice 的回答非常有帮助,但它最初对我不起作用,因为 TextArea text = new TextArea(); 正在创建一个没有 id 字段的 TextArea。为了解决这个问题,我只是手动添加了一个 id 字段:

text.getElement().setId("make_up_an_id_name_here");

还要确保将 ckeditor 文件夹放在您的 war 目录中。

如果您选择在 project_name.html 文件中链接到它,请将此行添加到插入主 ...nocache.js 脚本的行上方:

<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
于 2015-01-20T10:19:58.027 回答
0

text.getElement().setId(DOM.createUniqueId());

于 2016-10-02T04:57:13.140 回答