DMC*_*DMC 8 jquery-ui jquery-ui-sortable ckeditor
我遇到了CKEditor 4和jQuery UI的可排序方法的问题,如果我对具有CKEditor实例的容器进行排序,它会删除该值并抛出错误"未捕获的TypeError:无法调用未定义的方法'getSelection'".它也使编辑器无法编辑.我能够在CKEditor 3中解决这个问题,其中包含以下黑客攻击之一: CKEditor冻结jQuery UI重新排序
在查看Chrome DOM检查器时,似乎会删除iframe的内容.
以下是原始测试代码:
<html>
<head>
<title>test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<script src="ckeditor.js"></script>
<script type="text/javascript">
$(function(){
var tmpStore = {};
$('#sortable').sortable({
cursor: 'move',
// Hack that use to work on V3 but not on V4:
// https://stackoverflow.com/questions/3379653/ckeditor-freezes-on-jquery-ui-reorder
start:function (event,ui) {
$('textarea').each(function(){
var id = $(this).attr('id');
tmpStore[id] = CKEDITOR.instances[id].getData();
})
},
stop: function(event, ui) {
$('textarea').each(function(){
var id = $(this).attr('id');
CKEDITOR.instances[id].setData(tmpStore[id]);
})
}
});
$('textarea').each(function(){
var ckId = $(this).attr('id');
config = {};
CKEDITOR.replace(ckId, config);
})
})
li { padding: 10px; width: 800px; height: 300px; }
</head>
<body>
<ul id="sortable">
<li><textarea id="test1" name="test1">test1</textarea></li>
<li><textarea id="test2" name="test1">test2</textarea></li>
<li><textarea id="test3" name="test1">test3</textarea></li>
</ul>
</body>
</html>
我遇到了同样的问题,并根据这里的答案进行了修复。请看下面的小提琴
问题:https : //jsfiddle.net/33qt24L9/1/
$(function() {
$( "#sortable" ).sortable({
placeholder: "ui-state-highlight"
});
CKEDITOR.replace( 'editor1' );
CKEDITOR.replace( 'editor2' );
CKEDITOR.replace( 'editor3' );
CKEDITOR.replace( 'editor4' );
});
Run Code Online (Sandbox Code Playgroud)
已解决的问题:https : //jsfiddle.net/57djq2bh/2/
$(function() {
$( "#sortable" ).sortable({
placeholder: "ui-state-highlight",
start: function (event, ui)
{
var id_textarea = ui.item.find(".ckeditor").attr("id");
CKEDITOR.instances[id_textarea].destroy();
},
stop: function (event, ui)
{
var id_textarea = ui.item.find(".ckeditor").attr("id");
CKEDITOR.replace(id_textarea);
}
});
CKEDITOR.replace( 'editor1' );
CKEDITOR.replace( 'editor2' );
CKEDITOR.replace( 'editor3' );
CKEDITOR.replace( 'editor4' );
});
Run Code Online (Sandbox Code Playgroud)
编辑:如果像我一样,您每个编辑器都有单独的配置,这里的更新代码将有所帮助:
start: function (event, ui)
{
$('.wysiwyg', ui.item).each(function(){
var tagId = $(this).attr('id');
var ckeClone = $(this).next('.cke').clone().addClass('cloned');
ckeConfigs[tagId] = CKEDITOR.instances[tagId].config;
CKEDITOR.instances[tagId].destroy();
$(this).hide().after(ckeClone);
});
},
stop: function(event, ui) {
// for each textarea init ckeditor anew and remove the clone
$('.wysiwyg', ui.item).each(function(){
var tagId = $(this).attr('id');
CKEDITOR.replace(tagId, ckeConfigs[tagId]);
$(this).next('.cloned').remove();
});
}
Run Code Online (Sandbox Code Playgroud)
感谢:https : //github.com/trsteel88/TrsteelCkeditorBundle/issues/53
一旦底层 DOM 结构被修改,你就必须重新创建 CKEditor。使用editor.getData()之前保存编辑器数据,并在创建新实例后editor.destroy()使用恢复内容。editor.setData( data )没有其他方法可以解决这个问题,因为 CKEditor 强烈依赖于 DOM 结构。