2011-07-05 83 views
2

我想一個新的菜單項添加到TinyMCE的上下文菜單,當用戶點擊它執行命令,到目前爲止,我有這一點,這是行不通的:TinyMCE的項目添加到右鍵菜單

tinyMCE.init({ 
... 

setup : function(ed) { 
    ed.onContextMenu.add(function(ed, menu) { 
     menu.add({title : 'Menu 1', onclick : function() { 
      alert('Item 1 was clicked.'); 
     }}); 
    }); 
} 

上面的代碼拋出一個錯誤,說「menu.add不是一個函數」,如果我刪除menu.add的東西,並放置一個console.log(菜單),它會打開上下文菜單時返回「contextmenu」。

什麼是將項目添加到上下文菜單的正確方法?最好不必修改插件本身。提前致謝。

回答

4

你需要像

ed.onContextMenu.add(function(ed, e) { 

if (!e.ctrlKey) { 

    // Restore the last selection since it was removed 
    if (lastRng) 
     ed.selection.setRng(lastRng); 

    var menu = this._getMenu(ed); 

    if ((typeof menu).toLowerCase() == 'object') 
    { 
     menu.showMenu(e.clientX, e.clientY); 

     Event.add(ed.getDoc(), 'click', function(e) { 
      hide(ed, e); 
     }); 
     Event.cancel(e); 
    } 
} 

}); 

和功能_getMenu在那裏你可以插入文本菜單選項:

//example this will only display if an image was clicked 
if (node !== "undefined" && node.nodeName.toLowerCase() == 'img') { 

m.add({ 
    title: 'my menu', 
}); 


m.addSeparator(); 

// Inline-Element editieren 
m.add({ 
    title: 'to be choosen1', 
    icon: 'http://...', 
    cmd: 'undo' 
}); 

t.onContextMenu.dispatch(t, m, el, col); 

return m; 
} 

編輯:

你可以使用得到默認的菜單(插件contextmenu需要是活動的)

var editor = tinymce.get(editor_id); 
var menu = editor.plugins.contextmenu._getMenu(editor); 

添加到菜單條目應該工作如下

menu.add({title : 'undo', icon : 'undo', cmd : 'Undo'}); 

,可能有必要渲染明確使用showMenu菜單。 將菜單插入到contextemenu的另一種方法是修改tiny_mce/plugins/contextemneu目錄中的editor_plugin.js並直接添加條目。你也可以複製插件,修改和重命名它 - 讓它作爲一個自定義插件工作。

+0

感謝您的回覆,它有助於理解它。在調整好代碼之後,我打開了一個新的dropmenu,其中包含項目,但是我只想添加一個項目到默認的上下文菜單中,而不是創建一個新項目。那可能嗎 ?我似乎無法得到該默認dropmenu的參考?我怎樣才能做到這一點 ? – mfreitas

+0

看到我編輯的帖子 – Thariama

+0

非常感謝,解決了我的問題。 – mfreitas

0

您可以添加上下文菜單是這樣的:

setup : function(ed) { 
    ed.onContextMenu.add(function(ed, menu) { 
     displayContextMenu(ed,e); 
     }}); 
    }); 
} 

function displayContextMenu(ed,e){ 
    var m = ed.plugins.contextmenu._getMenu(ed); 
    m.add({title : 'advanced.bold_desc', icon : 'bold', cmd : 'bold'}); 
} 
相關問題