更新 3
以下解决方案在 Chrome、Firefox 和 Safari
中比较稳定:
var $editor = $("#input").cleditor()[0];
var focused = false;
var isFirefox = typeof InstallTrigger !== 'undefined';
var text = '';
$('.draggable').draggable({
appendTo:'body',
helper:'clone',
iframeFix: true,
revert:'invalid',
cursorAt:{top:0,left:0}
});
$("#w-edit").droppable({
drop: function (event, ui) {
text = ui.draggable.text();
if(!focused && !isFirefox)
$editor.focus();
else
$editor.focused();
}
});
$editor.focused(function(){
if(text != ''){
$editor.execCommand('inserthtml',text, false);
text = '';
}
focused = true;
});
$editor.blurred(function(){
focused = false;
});
$editor.change(function(){
if(!$('#w-edit').hasClass('ui-droppable')){
focused = false;
$("#w-edit").droppable({
drop: function (event, ui) {
text = ui.draggable.text();
if(!focused && !isFirefox)
$editor.focus();
else
$editor.focused();
}
});
}
});
http://jsfiddle.net/C3cFk/
我发现的唯一问题是,如果你重新调整窗口大小,然后拖动一个项目,可能无法预测新拖动的项目在文本区域中显示的位置。老实说,我不知道该如何解决这个问题,而且因为 IE 的问题,我有些心力憔悴,尝试使其正常工作。
IE 问题
在 IE 中(测试过 IE 10),除非每次都点击编辑器,否则它的效果不好,因为有一半的时间单词会相互覆盖。execCommand('inserthtml'
不起作用或者不太好用。我修改了代码,使用pasteHTMl
,这是 IE 支持的,但仍然存在问题,例如如果您在编辑器外面单击并拖动元素,有时会替换所有文本。或者如果您调整页面大小,然后再拖动更多元素,则会将元素附加到页面顶部而不是编辑器,除非您首先在编辑器中单击。我不认为这个解决方案适用于IE8
,因为还有更多限制需要处理,而我没有简单的测试或开发 IE8 的方法,所以我将放弃 IE8。
这是我为IE10
找到的最佳解决方案:
$(document).ready(function(){
var $editor = $("#input").cleditor()[0];
var focused = false;
var text ='';
var temp;
$('.draggable').draggable({
appendTo:'body',
helper:'clone',
iframeFix: true,
revert:'invalid',
cursorAt:{top:0,left:0}
});
$("#w-edit").droppable({
drop: function (event, ui) {
temp = $(this).contents().find("html body")[0];
text = ui.draggable.text();
if(!focused)
$editor.focus();
else
$editor.focused();
}
});
$editor.focused(function(){
if(text != ''){
var doc = document.getElementById("w-edit").contentWindow.document;
if (doc.selection && doc.selection.createRange) {
var range = doc.selection.createRange();
if (range.pasteHTML) {
range.pasteHTML(text);
}
}
$editor.updateTextArea();
text = '';
}
focused = true;
});
$editor.blurred(function(){
focused = false;
});
$editor.change(function(){
if(!$('#w-edit').hasClass('ui-droppable')){
$("#w-edit").droppable({
drop: function (event, ui) {
temp = $(this).contents().find("html body")[0];
text = ui.draggable.text();
if(!focused)
$editor.focus();
else
$editor.focused();
}
});
}
});
});
总的来说,这种东西在所有浏览器中似乎都很难搞定。为了使其正常工作,我不得不针对每个浏览器进行不同的处理。
注意:
我已经在IE8上测试了我的先前回答中的代码(你曾经说它在IE8上无法工作,文本不会出现在光标所在位置),并且对我有效。(请确保您的浏览器模式和文档模式为IE8):
$(document).ready(function(){
var $editor = $("#input").cleditor()[0];
$('.draggable').draggable({
appendTo:'body',
helper:'clone',
iframeFix: true,
revert:'invalid',
cursorAt:{top:0,left:0}
});
$("#w-edit").droppable({
drop: function (event, ui) {
console.log('drop');
$(this).contents().find("html body").append(ui.draggable.text()+' ');
$editor.updateTextArea();
}
});
$editor.change(function(){
if(!$('#w-edit').hasClass('ui-droppable')){
$("#w-edit").droppable({
drop: function (event, ui) {
$(this).contents().find("html body").append(ui.draggable.text()+' ');
$editor.updateTextArea();
}
});
}
});
});
在IE中位置不正确
的问题。你是指它在IE中无法工作吗?你使用的IE版本是什么?即使在IE10
中,cleditor
也可以正常加载,我可以研究一下,但似乎这是插件本身的问题。 - TrevorWYSIWYG
编辑器实现拖放
功能非常困难。 - Trevor