You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.6 KiB

  1. ---
  2. layout: default
  3. title: Custom sidebar
  4. title_nav: Custom sidebar
  5. description_short: Introducing sidebar panel creation.
  6. description: A short introduction to creating sidebars.
  7. keywords: sidebar
  8. ---
  9. TinyMCE allows developers to create sidebars and add custom UI widgets inside a constrained and easily accessible area of the editor. The sidebar is designed to allow administrators and plugin developers to provide additional tools that can be accessed by TinyMCE users.
  10. ## Editor sidebar API
  11. The sidebar API allows developers to add sidebars on editor instances in a similar way as adding buttons or menu items. Developers can either add sidebars directly in the `tinymce.init` using the setup callback or inside your plugin.
  12. This is the syntax for the addSidebar function: `editor.ui.registry.addSidebar(name:String, spec:Object)`
  13. When a new sidebar is registered, a corresponding toolbar button for toggling the sidebar open and close is also created using the same name. This button can then be included in the toolbar by adding the sidebar name to the [`toolbar`]({{site.baseurl}}/configure/editor-appearance/#toolbar/) option.
  14. ### Specification object
  15. #### `tooltip`
  16. The `tooltip` specifies a tooltip to be displayed when hovering over the sidebar toggle button.
  17. **Type**: `String`
  18. #### `icon`
  19. The `icon` specifies an icon for the sidebar toggle button. The icon should be the name of an icon provided by the TinyMCE skin or a [custom icon]({{site.baseurl}}/api/tinymce.editor.ui/tinymce.editor.ui.registry/#addicon/).
  20. **Type**: `String`
  21. #### `onSetup`
  22. The `onSetup` specifies a function to be called when the panel is first created. It passes in an API object and should return a callback that takes an API. The default is `(api) => (api) => {}`.
  23. `onSetup` is a complex property. It requires a function that takes the sidebar’s API and should return a callback that takes the sidebar's API and returns nothing. This occurs because `onSetup` runs whenever the sidebar is rendered, and the returned callback is executed when the sidebar is destroyed. Therefore the returned function is essentially an `onTeardown` handler, and can be used to unbind events and callbacks.
  24. **Type**: `function`
  25. #### `onShow`
  26. The `onShow` specifies a function to be called when the panel displayed. It passes in an API object.
  27. **Type**: `function`
  28. #### `onHide`
  29. The `onHide` specifies a function to be called when the panel is hidden. It passes in an API object.
  30. **Type**: `function`
  31. ### API Object
  32. #### `element():HTMLElement`
  33. The `element():HTMLElement` function returns the root element of the sidebar panel.
  34. ## Example inside the tinymce.init
  35. ```js
  36. tinymce.init({
  37. ...
  38. toolbar: "mysidebar",
  39. setup: function (editor) {
  40. editor.ui.registry.addSidebar('mysidebar', {
  41. tooltip: 'My sidebar',
  42. icon: 'comment',
  43. onSetup: function (api) {
  44. console.log('Render panel', api.element());
  45. },
  46. onShow: function (api) {
  47. console.log('Show panel', api.element());
  48. api.element().innerHTML = 'Hello world!';
  49. },
  50. onHide: function (api) {
  51. console.log('Hide panel', api.element());
  52. }
  53. });
  54. }
  55. });
  56. ```
  57. ## Example inside a TinyMCE plugin
  58. ```js
  59. tinymce.PluginManager.add('myplugin', function (editor) {
  60. editor.ui.registry.addSidebar('mysidebar', {
  61. tooltip: 'My sidebar',
  62. icon: 'comment',
  63. onSetup: function (api) {
  64. console.log('Render panel', api.element());
  65. },
  66. onShow: function (api) {
  67. console.log('Show panel', api.element());
  68. api.element().innerHTML = 'Hello world!';
  69. },
  70. onHide: function (api) {
  71. console.log('Hide panel', api.element());
  72. }
  73. });
  74. });
  75. ```