2

我正在尝试向 tinyMCE 的代码插件添加一些验证逻辑。

然而,似乎当调用窗口的 onSubmit 函数时,窗口默认关闭。

onSubmit 函数当前如下所示:

        onSubmit: function (e) {
            // We get a lovely "Wrong document" error in IE 11 if we
            // don't move the focus to the editor before creating an undo
            editor.focus();
            editor.undoManager.transact(function () {
                editor.setContent(e.data.code);
            });

            editor.selection.setCursorLocation();
            editor.nodeChanged();

        }

我想做的是向插件添加一些验证逻辑,以防止 tinyMCE 重新格式化无效的 html,而是显示 html 无效的消息。本质上,是这样的:

        onSubmit: function (e) {
            // We get a lovely "Wrong document" error in IE 11 if we
            // don't move the focus to the editor before creating an undo
            var isCodeValid = true;

            //check if code valid
            isCodeValid = ValidateCode(e.data.code);

            if (isCodeValid) {
            //if code valid, send to tinyMCE to let it do it's thing
                editor.focus();
                editor.undoManager.transact(function () {
                    editor.setContent(e.data.code);
                });

                editor.selection.setCursorLocation();
                editor.nodeChanged();
            }
            else {
            //if code invalid, display error message and keep text editor window open
                tinyMCE.activeEditor.windowManager.alert("Your HTML is invalid. Please check your code and try submitting again.");
                return;
            }

        }

但是,似乎 onSubmit 函数无论如何都会关闭文本编辑器窗口。我想知道是否有办法阻止它这样做。我已经浏览了留下很多需要解释的文档,并查看了其他插件作为示例。我能找到的最接近的是 searchandreplce 插件。“查找”按钮调用 onSubmit 函数,但如果“查找”文本字段为空白,它似乎保持打开状态。但是,它背后的逻辑似乎与我可以在 Code 插件中使用的逻辑大不相同。

任何熟悉 tinyMCE API 的人都可以给我任何关于如何在调用 onSubmit 时防止窗口关闭的想法吗?还是我必须走另一条路?

4

2 回答 2

1

根据这个问题,取消事件的方法是return false;. 这将使弹出窗口保持打开状态。您的代码将变为:

onSubmit: function (e) {
  // We get a lovely "Wrong document" error in IE 11 if we
  // don't move the focus to the editor before creating an undo
  var isCodeValid = true;

  //check if code valid
  isCodeValid = ValidateCode(e.data.code);

  if (isCodeValid) {
    //if code valid, send to tinyMCE to let it do it's thing
    editor.focus();
    editor.undoManager.transact(function () {
      editor.setContent(e.data.code);
    });

    editor.selection.setCursorLocation();
    editor.nodeChanged();
  }
  else {
    //if code invalid, display error message and keep text editor window open
    tinyMCE.activeEditor.windowManager.alert("Your HTML is invalid. Please check your code and try submitting again.");
    return false;
  }
}
于 2018-09-20T08:35:37.087 回答
0

我终于想通了。您需要做的就是添加 e.preventDefault(); 在 onSubmit 函数开始时,窗口不会关闭。文档没有帮助,但是以 searchandreplace 插件为例,我找到了答案。我现在拥有的是这样的:

onSubmit: function (e) {
        e.preventDefault();
        // We get a lovely "Wrong document" error in IE 11 if we
        // don't move the focus to the editor before creating an undo
        var isCodeValid = true;

        //check if code valid
        isCodeValid = ValidateCode(e.data.code);

        if (isCodeValid) {
        //if code valid, send to tinyMCE to let it do it's thing
            editor.focus();
            editor.undoManager.transact(function () {
                editor.setContent(e.data.code);
            });

            editor.selection.setCursorLocation();
            editor.nodeChanged();
        }
        else {
        //if code invalid, display error message and keep text editor window open
            tinyMCE.activeEditor.windowManager.alert("Your HTML is invalid. Please check your code and try submitting again.");
            return;
        }

    }

e.PreventDefault() 似乎停止了 onSubmit 函数的默认行为。

于 2016-02-05T18:52:11.150 回答