2016-02-04 65 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函數的默認行爲。