2011-06-13 81 views

回答

1

我遇到同样的问题,不幸的是链接到由ghostCoder所做的修复不再可用。

除了嵌套的div问题,我们已经强制粘贴为纯文本(所以数据被粘贴为纯文本,除非使用“粘贴从单词”),我们正在使用div作为我们的enterMode。在粘贴的数据行中,中断被替换为< br/>标签,而我们希望每行都是元素内的包装。

这是我们最终解决问题的方法。您应该知道我们没有更改autoParagraph config option,因此它默认为true,我不建议在配置级别禁用它。我会尝试对此解决方案进行编码评论,以便您了解这里实际完成的内容。

config.on.instanceReady = function(e) { 
    var enterMode = e.editor.config.enterMode, elem = 'div'; 
    if(enterMode === CKEDITOR.ENTER_P) { 
     elem = 'p'; 
    } 

    // We didn't encounter any issues when using br as enterMode so 
    // continue only if enterMode is div or p element 
    if(enterMode === CKEDITOR.ENTER_DIV || enterMode === CKEDITOR.ENTER_P) { 
     // Disable autoParagraph in initialization to avoid nested div issue. 
     // When autoParagraph is manipulated this way it will still be active 
     // later on so if you write some inline content in source mode it 
     // will be properly wrapped inside <p> or <div> element. 
     e.editor.config.autoParagraph = false; 

     // Handle paste event to get rid of <br /> line breaks issue 
     e.editor.on('paste', function(evt) { 
      var data = ''; 

      // Stop event, handle everything here manually 
      evt.stop(); 

      // Get data either from event's text or html property 
      if(evt.data.text) { 
       // Remove HTML markup from pasted data 
       data = evt.data.text.replace(/(<([^>]+)>)/ig, ''); 
       // Replace all line breaks with </div><div> or </p><p> markup 
       // And wrap the whole content inside <div> or <p> element 
       data = '<' + elem + '>' 
       + data.replace(/\r\n|\r|\n/g, '</' + elem + '><' + elem + '>') 
       + '</' + elem + '>'; 
      } else { 
       // If data was not pasted as plain text just 
       // get data as is from event's html property 
       data = evt.data.html; 
      } 

      // Insert HTML data to editor 
      evt.editor.insertHtml(data); 
      // Fire afterPaste manually as we have stopped the event 
      // and afterPaste wouldn't get triggered otherwise 
      evt.editor.fire('afterPaste'); 
     }, e.editor.element.$); 
    } 
};