2016-08-22 46 views

回答

0

我建議使用事件來捕獲工具應用時,然後應用你以後的具體行爲。

目前沒有很好的方式來訪問這些事件(我將在未來的版本中看到),但同時這裏有一個解決方法,它給你你以後的行爲,並且應用到任何工具都應該相對簡單:

// Override the behaviour of the `ToolboxUI.tools` method so that we can 
// bind to the applied event for the tools of interest. 
var superTools = ContentTools.ToolboxUI.prototype.tools; 
ContentTools.ToolboxUI.prototype.tools = function (tools) { 
    superTools.call(this, tools); 

    function _followWithP(ev) { 
    var element = ev.detail().element; 

    // Find the top level element 
    if (element.parent().type() !== 'Region') { 
     element = element.closest(function(node) { 
     return node.parent().type() === 'Region'; 
     }); 
    } 

    // Find the newely insert element 
    var newElement = element.nextSibling(); 

    // Check if this is the last item in the region 
    var siblings = newElement.parent().children; 
    if (newElement === siblings[siblings.length - 1]) { 
     // This is the last child so apply add a paragraph 
     var p = new ContentEdit.Text('p', {}, ''); 
     newElement.parent().attach(p); 
     p.focus(); 
    } 
    } 

    // Modify the applied behaviour of the image, table and video tools to use 
    // `_followWithP`. 
    this._toolUIs['image'].addEventListener('applied', _followWithP); 
    this._toolUIs['table'].addEventListener('applied', _followWithP); 
    this._toolUIs['video'].addEventListener('applied', _followWithP); 
}