toolbar.html 8.3 KB

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