2016-02-04 48 views
2

我想添加一些验证逻辑的代码插件为tinyMCE。停止窗口从关闭tinyMCE onSubmit函数

然而,看起来,当窗口的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被调用时阻止窗口关闭的想法吗?还是我必须去另一条路线?

回答

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函数的默认行为。