toolbar.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  4. For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  5. -->
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Toolbar Configuration &mdash; CKEditor Sample</title>
  10. <meta name="ckeditor-sample-name" content="Toolbar Configurations">
  11. <meta name="ckeditor-sample-group" content="Advanced Samples">
  12. <meta name="ckeditor-sample-description" content="Configuring CKEditor to display full or custom toolbar layout.">
  13. <script src="../../../ckeditor.js"></script>
  14. <link href="../../../samples/old/sample.css" rel="stylesheet">
  15. </head>
  16. <body>
  17. <h1 class="samples">
  18. <a href="../../../samples/old/index.html">CKEditor Samples</a> &raquo; Toolbar Configuration
  19. </h1>
  20. <div class="warning deprecated">
  21. This sample is not maintained anymore. Check out the <a href="../../../samples/toolbarconfigurator/index.html#basic">brand new CKEditor Toolbar Configurator</a>.
  22. </div>
  23. <div class="description">
  24. <p>
  25. This sample page demonstrates editor with loaded <a href="#fullToolbar">full toolbar</a> (all registered buttons) and, if
  26. current editor's configuration modifies default settings, also editor with <a href="#currentToolbar">modified toolbar</a>.
  27. </p>
  28. <p>Since CKEditor 4 there are two ways to configure toolbar buttons.</p>
  29. <h2 class="samples">By <a href="https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_config.html#cfg-toolbar">config.toolbar</a></h2>
  30. <p>
  31. You can explicitly define which buttons are displayed in which groups and in which order.
  32. This is the more precise setting, but less flexible. If newly added plugin adds its
  33. own button you'll have to add it manually to your <code>config.toolbar</code> setting as well.
  34. </p>
  35. <p>To add a CKEditor instance with custom toolbar setting, insert the following JavaScript call to your code:</p>
  36. <pre class="samples">
  37. CKEDITOR.replace( <em>'textarea_id'</em>, {
  38. <strong>toolbar:</strong> [
  39. { name: 'document', items: [ 'Source', '-', 'NewPage', 'Preview', '-', 'Templates' ] }, // Defines toolbar group with name (used to create voice label) and items in 3 subgroups.
  40. [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ], // Defines toolbar group without name.
  41. '/', // Line break - next group will be placed in new line.
  42. { name: 'basicstyles', items: [ 'Bold', 'Italic' ] }
  43. ]
  44. });</pre>
  45. <h2 class="samples">By <a href="https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_config.html#cfg-toolbarGroups">config.toolbarGroups</a></h2>
  46. <p>
  47. You can define which groups of buttons (like e.g. <code>basicstyles</code>, <code>clipboard</code>
  48. and <code>forms</code>) are displayed and in which order. Registered buttons are associated
  49. with toolbar groups by <code>toolbar</code> property in their definition.
  50. This setting's advantage is that you don't have to modify toolbar configuration
  51. when adding/removing plugins which register their own buttons.
  52. </p>
  53. <p>To add a CKEditor instance with custom toolbar groups setting, insert the following JavaScript call to your code:</p>
  54. <pre class="samples">
  55. CKEDITOR.replace( <em>'textarea_id'</em>, {
  56. <strong>toolbarGroups:</strong> [
  57. { name: 'document', groups: [ 'mode', 'document' ] }, // Displays document group with its two subgroups.
  58. { name: 'clipboard', groups: [ 'clipboard', 'undo' ] }, // Group's name will be used to create voice label.
  59. '/', // Line break - next group will be placed in new line.
  60. { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
  61. { name: 'links' }
  62. ]
  63. // NOTE: Remember to leave 'toolbar' property with the default value (null).
  64. });</pre>
  65. </div>
  66. <div id="currentToolbar" style="display: none">
  67. <h2 class="samples">Current toolbar configuration</h2>
  68. <p>Below you can see editor with current toolbar definition.</p>
  69. <textarea cols="80" id="editorCurrent" name="editorCurrent" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="https://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
  70. <pre id="editorCurrentCfg" class="samples"></pre>
  71. </div>
  72. <div id="fullToolbar">
  73. <h2 class="samples">Full toolbar configuration</h2>
  74. <p>Below you can see editor with full toolbar, generated automatically by the editor.</p>
  75. <p>
  76. <strong>Note</strong>: To create editor instance with full toolbar you don't have to set anything.
  77. Just leave <code>toolbar</code> and <code>toolbarGroups</code> with the default, <code>null</code> values.
  78. </p>
  79. <textarea cols="80" id="editorFull" name="editorFull" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="https://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
  80. <pre id="editorFullCfg" class="samples"></pre>
  81. </div>
  82. <script>
  83. (function() {
  84. 'use strict';
  85. var buttonsNames;
  86. CKEDITOR.config.extraPlugins = 'toolbar';
  87. CKEDITOR.on( 'instanceReady', function( evt ) {
  88. var editor = evt.editor,
  89. editorCurrent = editor.name == 'editorCurrent',
  90. defaultToolbar = !( editor.config.toolbar || editor.config.toolbarGroups || editor.config.removeButtons ),
  91. pre = CKEDITOR.document.getById( editor.name + 'Cfg' ),
  92. output = '';
  93. if ( editorCurrent ) {
  94. // If default toolbar configuration has been modified, show "current toolbar" section.
  95. if ( !defaultToolbar )
  96. CKEDITOR.document.getById( 'currentToolbar' ).show();
  97. else
  98. return;
  99. }
  100. if ( !buttonsNames )
  101. buttonsNames = createButtonsNamesHash( editor.ui.items );
  102. // Toolbar isn't set explicitly, so it was created automatically from toolbarGroups.
  103. if ( !editor.config.toolbar ) {
  104. output +=
  105. '// Toolbar configuration generated automatically by the editor based on config.toolbarGroups.\n' +
  106. dumpToolbarConfiguration( editor ) +
  107. '\n\n' +
  108. '// Toolbar groups configuration.\n' +
  109. dumpToolbarConfiguration( editor, true )
  110. }
  111. // Toolbar groups doesn't count in this case - print only toolbar.
  112. else {
  113. output += '// Toolbar configuration.\n' +
  114. dumpToolbarConfiguration( editor );
  115. }
  116. // Recreate to avoid old IE from loosing whitespaces on filling <pre> content.
  117. var preOutput = pre.getOuterHtml().replace( /(?=<\/)/, output );
  118. CKEDITOR.dom.element.createFromHtml( preOutput ).replace( pre );
  119. } );
  120. CKEDITOR.replace( 'editorCurrent', { height: 100 } );
  121. CKEDITOR.replace( 'editorFull', {
  122. // Reset toolbar settings, so full toolbar will be generated automatically.
  123. toolbar: null,
  124. toolbarGroups: null,
  125. removeButtons: null,
  126. height: 100
  127. } );
  128. function dumpToolbarConfiguration( editor, printGroups ) {
  129. var output = [],
  130. toolbar = editor.toolbar;
  131. for ( var i = 0; i < toolbar.length; ++i ) {
  132. var group = dumpToolbarGroup( toolbar[ i ], printGroups );
  133. if ( group )
  134. output.push( group );
  135. }
  136. return 'config.toolbar' + ( printGroups ? 'Groups' : '' ) + ' = [\n\t' + output.join( ',\n\t' ) + '\n];';
  137. }
  138. function dumpToolbarGroup( group, printGroups ) {
  139. var output = [];
  140. if ( typeof group == 'string' )
  141. return '\'' + group + '\'';
  142. if ( CKEDITOR.tools.isArray( group ) )
  143. return dumpToolbarItems( group );
  144. // Skip group when printing entire toolbar configuration and there are no items in this group.
  145. if ( !printGroups && !group.items )
  146. return;
  147. if ( group.name )
  148. output.push( 'name: \'' + group.name + '\'' );
  149. if ( group.groups )
  150. output.push( 'groups: ' + dumpToolbarItems( group.groups ) );
  151. if ( !printGroups )
  152. output.push( 'items: ' + dumpToolbarItems( group.items ) );
  153. return '{ ' + output.join( ', ' ) + ' }';
  154. }
  155. function dumpToolbarItems( items ) {
  156. if ( typeof items == 'string' )
  157. return '\'' + items + '\'';
  158. var names = [],
  159. i, item;
  160. for ( var i = 0; i < items.length; ++i ) {
  161. item = items[ i ];
  162. if ( typeof item == 'string' )
  163. names.push( item );
  164. else {
  165. if ( item.type == CKEDITOR.UI_SEPARATOR )
  166. names.push( '-' );
  167. else
  168. names.push( buttonsNames[ item.name ] );
  169. }
  170. }
  171. return '[ \'' + names.join( '\', \'' ) + '\' ]';
  172. }
  173. // Creates { 'lowercased': 'LowerCased' } buttons names hash.
  174. function createButtonsNamesHash( items ) {
  175. var hash = {},
  176. name;
  177. for ( name in items ) {
  178. hash[ items[ name ].name ] = name;
  179. }
  180. return hash;
  181. }
  182. })();
  183. </script>
  184. <div id="footer">
  185. <hr>
  186. <p>
  187. CKEditor - The text editor for the Internet - <a class="samples" href="https://ckeditor.com/">https://ckeditor.com</a>
  188. </p>
  189. <p id="copy">
  190. Copyright &copy; 2003-2019, <a class="samples" href="https://cksource.com/">CKSource</a> - Frederico
  191. Knabben. All rights reserved.
  192. </p>
  193. </div>
  194. </body>
  195. </html>