我正在尝试为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时阻止窗口关闭吗?或者我必须走另一条路?
答案 0 :(得分:1)
根据this question,取消事件的方法是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;
}
}
答案 1 :(得分: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函数的默认行为。