插件代碼:
tinymce.PluginManager.add('customem', function(editor, url) {
// Add a button that opens a window
editor.addButton('customEmElementButton', {
text: 'Custom EM',
icon: false,
onclick: function() {
// Open window
editor.windowManager.open({
title: 'Please input text',
body: [
{type: 'textbox', name: 'description', label: 'Text'}
],
onsubmit: function(e) {
// Insert content when the window form is submitted
editor.insertContent('<emstart>EM Start</emstart><p>' + e.data.description + '</p><emend>EM End</emend>');
}
});
}
});
// Adds a menu item to the tools menu
editor.addMenuItem('customEmElementMenuItem', {
text: 'Custom EM Element',
context: 'tools',
onclick: function() {
editor.insertContent('<emstart>EM Start</emstart><p>Example text!</p><emend>EM End</emend>');
}
});
});
的最後一步是將您的自定義插件加載到編輯器(使用插件和工具欄配置選項)和享受結果:
tinymce.init({
selector: "textarea#editor",
height: "500px",
plugins: [
"code, preview, contextmenu, image, link, searchreplace, customem"
],
toolbar: "bold italic | example | code | preview | link | searchreplace | customEmElementButton",
contextmenu: "bold italic",
extended_valid_elements : "emstart,emend",
custom_elements: "emstart,emend",
content_css: "editor.css",
});
編輯器現在看起來是這樣的:
和源就像你的例子:
非常感謝!我想知道爲什麼他們不把這樣的例子放在文檔中,或者我錯過了這樣的寶藏? – 2016-06-26 13:44:11
很高興答案有幫助。至少在編寫答案時,我認爲它更像是理解*更大的圖像*並將事實放在一起(並試驗) - AFAIK在文檔中沒有這樣的例子。 – pasty 2016-06-26 14:38:22