Custom toolbar button
优质
小牛编辑
125浏览
2023-12-01
This example shows you how to add a custom button to the toolbar in Tiny 5.
TinyMCE HTML JS Edit on CodePen
<textarea id="custom-toolbar-button">
<p style="text-align: center; font-size: 15px;"><img title="TinyMCE Logo" src="//www.tiny.cloud/images/glyph-tinymce@2x.png" alt="TinyMCE Logo" width="110" height="97" />
</p>
<h2 style="text-align: center;">Welcome to the TinyMCE editor demo!</h2>
<p>Select a menu item from the listbox above and it will insert contents into the editor at the caret position.</p>
<h2>Got questions or need help?</h2>
<ul>
<li>Our <a href="https://www.tiny.cloud/docs/">documentation</a> is a great resource for learning how to configure TinyMCE.</li>
<li>Have a specific question? Visit the <a href="https://community.tinymce.com/forum/">Community Forum</a>.</li>
<li>We also offer enterprise grade support as part of <a href="https://www.tiny.cloud/pricing">TinyMCE Enterprise</a>.</li>
</ul>
<h2>Found a bug?</h2>
<p>If you think you have found a bug please create an issue on the <a href="https://github.com/tinymce/tinymce/issues">GitHub repo</a> to report it to the developers.</p>
<h2>Finally ...</h2>
<p>Don't forget to check out our other product <a href="http://www.plupload.com" target="_blank">Plupload</a>, your ultimate upload solution featuring HTML5 upload support.</p>
<p>Thanks for supporting TinyMCE! We hope it helps you and your users create great content.
<br>All the best from the TinyMCE team.</p>
</textarea>
tinymce.init({
selector: 'textarea#custom-toolbar-button',
toolbar: 'customInsertButton customDateButton',
setup: function (editor) {
editor.ui.registry.addButton('customInsertButton', {
text: 'My Button',
onAction: function (_) {
editor.insertContent(' <strong>It\'s my button!</strong> ');
}
});
var toTimeHtml = function (date) {
return '<time datetime="' + date.toString() + '">' + date.toDateString() + '</time>';
};
editor.ui.registry.addButton('customDateButton', {
icon: 'insert-time',
tooltip: 'Insert Current Date',
disabled: true,
onAction: function (_) {
editor.insertContent(toTimeHtml(new Date()));
},
onSetup: function (buttonApi) {
var editorEventCallback = function (eventApi) {
buttonApi.setDisabled(eventApi.element.nodeName.toLowerCase() === 'time');
};
editor.on('NodeChange', editorEventCallback);
/* onSetup should always return the unbind handlers */
return function (buttonApi) {
editor.off('NodeChange', editorEventCallback);
};
}
});
}
});