2015-03-03 55 views
2

我有ckeditor小部件的問題。我有內聯的不可編輯的文本小部件,我可以在編輯器中使用其默認功能拖放到其他位置。所以我需要檢查我放棄我的小部件的位置,如果這個地方是無法移動的(根據我的規則,我們TABLE)取消事件傳播,小部件應該留在以前的地方。CKEDITOR小工具拖放到需要的元素

editor.widgets.add('customWidgetAdd', { 
    inline: true, 
    template: '<span class="simplebox">' + 
      '<span class="simplebox-title" ></span>' + 
     '</span>', 
    init: function(){ 
     var that = this; 

      that.widgetData = ko.observable(self.activeWidgetData); 

      var subscription = that.widgetData.subscribe(function (value) {           
       $(that.element.$).find('.simplebox-title').text(value.name); 
       if (that.isSelected) { 
        self.activeWidgetData = value; 
       } 
      }); 
      var destroyListener = function(ev){ 
       subscription.dispose(); 
      };   
      that.once('destroy', destroyListener); 

      that.on('doubleclick', function (evt) { 
       editor.execCommand(editAction.command); 
      });     

      that.on('select', function (evt){ 
       that.isSelected = true; 
       self.activeWidgetData = that.widgetData(); 
      }); 

      that.on('deselect', function (evt){ 

       try { 
        var endContainer = editor.getSelection().getRanges()[0].endContainer.getName(); 
       } catch (e) { 

       } 

       that.isSelected = false; 
       if (endContainer == 'td' || endContainer == 'th') { 

         //SO here comes the problem. My rule is executed and 
//I want CKEDITOR do nothing from here... but stil widget is getting cutted  from DOM and inserted to place where I have dropped it... 
         //that.removeListener('destroy', destroyListener); 
         //that.removeAllListeners(); 
         evt.cancel(); 
         evt.stop(); 

         return false;       
       } 
      }); 
    } 
}); 
+0

我想**不**可以將內聯小部件放入ckeditors表 – Eugene 2015-03-03 13:25:40

回答

3

不幸的是,在這種情況下沒有簡單的解決方案。 你能做到這一點的只有一個辦法是訂閱編輯的drop事件,並取消它,如果需要,如:

editor.on('contentDom', function() { 
    var editable = editor.editable(), 
     // #11123 Firefox needs to listen on document, because otherwise event won't be fired. 
     // #11086 IE8 cannot listen on document. 
     dropTarget = (CKEDITOR.env.ie && CKEDITOR.env.version < 9) || editable.isInline() ? editable : editor.document; 

    editable.attachListener(dropTarget, 'drop', function(evt) { 
     //do all checks here 
    }); 
}); 

你可以找到它是如何工作in CKEditor(見的功能setupDragAndDrop代碼)

+0

謝謝!適合我工作 – Eugene 2015-03-03 15:32:08

+0

CKEditor 4.5將引入DnD API,您將能夠簡單地收聽編輯器事件(如「drop」,「dragover」等)。大多數功能已經在[major](https://github.com/ckeditor/ckeditor-dev/tree/major)分支上提供。 – Reinmar 2015-03-04 19:02:05

+0

啊,這是一個好消息!謝謝@Reinmar! – 2015-03-04 20:34:45