2011-06-13 60 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.$); 
    } 
};