2013-12-13 15 views
1

我试图从光标位置提取所有文本。下面是代码,我使用:函数从CKEditor中的光标位置获取文本行为奇怪

originalText = editor.getData(); 
var startTag = "<span id=\x22Start\x22>&nbsp;</span>"; 
var stopTag = "<span id=\x22Stop\x22>&nbsp;</span>"; 
var startElement = CKEDITOR.dom.element.createFromHtml(startTag, editor.document); 
editor.insertElement(startElement); 
sText = editor.getData(); 
sText1 = sText + stopTag; 
editor.setData(sText1); 
// up to here, I've incapsulated the required text with span tags 
// Using the replace function, I remove end tag of the Start span as well as removing the start tag of the Stop span! 
sText1 = editor.getData(); 
sText2 = sText1.replace("<span id=\"Start\">&nbsp;</span>", "<span id=\"Start\">"); 
sText2 = sText2.replace("<span id=\"Stop\">&nbsp;</span>", "</span>"); 
// I set the data (HTML) back to the editor 
editor.setData(sText2); 
//alert(sText2); 
// I use the innerHTML to get the text 
el = editor.document.$.getElementById("Start"); 
return el.innerHTML; 

问题: 的el.innerHTML工作正常,但只有当警报()是取消注释!我知道setData是异步的,并通过使用setData()上的回调将解决问题,但不幸的是它不适用于我:(

+0

你想做什么?意思是什么你的recuirements?我认为这里的答案是完全重构你做事的方式。 – Nenotlep

回答

0

快速修复尝试确实是通过使用回调。使用回调不能返回el.innerHTML,你必须调用一个外部函数,你没有显示调用这段代码的代码,所以很难确定如何按照你想要的方式来重构这段代码,下面是一个虚拟版本这不是测试,只是用来给你的回调可以如何使用的例子

function extractTextStartingFromCursorPosition(yourCallback) { 
    originalText = editor.getData(); 
    var startTag = "<span id=\x22Start\x22>&nbsp;</span>"; 
    var stopTag = "<span id=\x22Stop\x22>&nbsp;</span>"; 
    var startElement = CKEDITOR.dom.element.createFromHtml(startTag, editor.document); 
    editor.insertElement(startElement); 
    sText = editor.getData(); 
    sText1 = sText + stopTag; 

    editor.setData(sText1, function() { 
     // up to here, I've incapsulated the required text with span tags 
     // Using the replace function, I remove end tag of the Start span as well as removing the start tag of the Stop span! 
     sText1 = editor.getData(); 
     sText2 = sText1.replace("<span id=\"Start\">&nbsp;</span>", "<span id=\"Start\">"); 
     sText2 = sText2.replace("<span id=\"Stop\">&nbsp;</span>", "</span>"); 

     // I set the data (HTML) back to the editor 
     editor.setData(sText2, function() { 
      // I use the innerHTML to get the text 
      el = editor.document.$.getElementById("Start"); 
      yourCallback(el.innerHTML); 
     }); 
    }); 
} 

所以,如果你以前使用这样的功能:

var html = extractTextStartingFromCursorPosition(); 
doSomethingWithHtml(html); 

现在你可以使用这样的:

extractTextStartingFromCursorPosition(function(html) { 
    doSomethingWithHtml(html); 
}); 

虽然我还是觉得,这个功能可能会以其他方式来做得更好,但我没有时间来测试/重构一个新的解决方案为相同的要求。

+0

伟大的工作Nenotlep,它做到了,我只需要在第二个回调中使用另一个setData回调将编辑器初始化为其原始内容。顺便说一句,对不起,如果这听起来很琐碎,但我怎样才能提取功能以外的结果?再次感谢。 – Mushahidi

+0

@Mushahidi很高兴听到!其实这是一个非常好的问题,并不是微不足道的。这真的取决于你的情况以及你想如何使用它。通过结果我认为你的意思是'el.innerHTML',对吧?你想在哪里做什么?我会在'doSomethingWithHtml()'函数中插入你需要使用的所有东西,但这确实取决于情况。 – Nenotlep

+1

是的,我的意思是el.innerHTML。你说这不是微不足道的,因为我很难重构它。我希望我可以把所有内容doSomethingWithHtml(),但这不是一个选项!无论如何,在你的帮助下,我现在向前迈进了一步。 – Mushahidi