3 Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
\r
4 For licensing, see LICENSE.md or http://ckeditor.com/license
\r
8 <title>Toolbar Configuration — CKEditor Sample</title>
\r
9 <meta charset="utf-8">
\r
10 <meta name="ckeditor-sample-name" content="Toolbar Configurations">
\r
11 <meta name="ckeditor-sample-group" content="Advanced Samples">
\r
12 <meta name="ckeditor-sample-description" content="Configuring CKEditor to display full or custom toolbar layout.">
\r
13 <script src="../../../ckeditor.js"></script>
\r
14 <link href="../../../samples/sample.css" rel="stylesheet">
\r
17 <h1 class="samples">
\r
18 <a href="../../../samples/index.html">CKEditor Samples</a> » Toolbar Configuration
\r
20 <div class="description">
\r
22 This sample page demonstrates editor with loaded <a href="#fullToolbar">full toolbar</a> (all registered buttons) and, if
\r
23 current editor's configuration modifies default settings, also editor with <a href="#currentToolbar">modified toolbar</a>.
\r
26 <p>Since CKEditor 4 there are two ways to configure toolbar buttons.</p>
\r
28 <h2 class="samples">By <a href="http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-toolbar">config.toolbar</a></h2>
\r
31 You can explicitly define which buttons are displayed in which groups and in which order.
\r
32 This is the more precise setting, but less flexible. If newly added plugin adds its
\r
33 own button you'll have to add it manually to your <code>config.toolbar</code> setting as well.
\r
36 <p>To add a CKEditor instance with custom toolbar setting, insert the following JavaScript call to your code:</p>
\r
38 <pre class="samples">
\r
39 CKEDITOR.replace( <em>'textarea_id'</em>, {
\r
40 <strong>toolbar:</strong> [
\r
41 { name: 'document', items: [ 'Source', '-', 'NewPage', 'Preview', '-', 'Templates' ] }, // Defines toolbar group with name (used to create voice label) and items in 3 subgroups.
\r
42 [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ], // Defines toolbar group without name.
\r
43 '/', // Line break - next group will be placed in new line.
\r
44 { name: 'basicstyles', items: [ 'Bold', 'Italic' ] }
\r
48 <h2 class="samples">By <a href="http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-toolbarGroups">config.toolbarGroups</a></h2>
\r
51 You can define which groups of buttons (like e.g. <code>basicstyles</code>, <code>clipboard</code>
\r
52 and <code>forms</code>) are displayed and in which order. Registered buttons are associated
\r
53 with toolbar groups by <code>toolbar</code> property in their definition.
\r
54 This setting's advantage is that you don't have to modify toolbar configuration
\r
55 when adding/removing plugins which register their own buttons.
\r
58 <p>To add a CKEditor instance with custom toolbar groups setting, insert the following JavaScript call to your code:</p>
\r
60 <pre class="samples">
\r
61 CKEDITOR.replace( <em>'textarea_id'</em>, {
\r
62 <strong>toolbarGroups:</strong> [
\r
63 { name: 'document', groups: [ 'mode', 'document' ] }, // Displays document group with its two subgroups.
\r
64 { name: 'clipboard', groups: [ 'clipboard', 'undo' ] }, // Group's name will be used to create voice label.
\r
65 '/', // Line break - next group will be placed in new line.
\r
66 { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
\r
70 // NOTE: Remember to leave 'toolbar' property with the default value (null).
\r
74 <div id="currentToolbar" style="display: none">
\r
75 <h2 class="samples">Current toolbar configuration</h2>
\r
76 <p>Below you can see editor with current toolbar definition.</p>
\r
77 <textarea cols="80" id="editorCurrent" name="editorCurrent" rows="10"><p>This is some <strong>sample text</strong>. You are using <a href="http://ckeditor.com/">CKEditor</a>.</p></textarea>
\r
78 <pre id="editorCurrentCfg" class="samples"></pre>
\r
81 <div id="fullToolbar">
\r
82 <h2 class="samples">Full toolbar configuration</h2>
\r
83 <p>Below you can see editor with full toolbar, generated automatically by the editor.</p>
\r
85 <strong>Note</strong>: To create editor instance with full toolbar you don't have to set anything.
\r
86 Just leave <code>toolbar</code> and <code>toolbarGroups</code> with the default, <code>null</code> values.
\r
88 <textarea cols="80" id="editorFull" name="editorFull" rows="10"><p>This is some <strong>sample text</strong>. You are using <a href="http://ckeditor.com/">CKEditor</a>.</p></textarea>
\r
89 <pre id="editorFullCfg" class="samples"></pre>
\r
99 CKEDITOR.config.extraPlugins = 'toolbar';
\r
101 CKEDITOR.on( 'instanceReady', function( evt ) {
\r
102 var editor = evt.editor,
\r
103 editorCurrent = editor.name == 'editorCurrent',
\r
104 defaultToolbar = !( editor.config.toolbar || editor.config.toolbarGroups ),
\r
105 pre = CKEDITOR.document.getById( editor.name + 'Cfg' ),
\r
108 if ( editorCurrent ) {
\r
109 // If default toolbar configuration has been modified, show "current toolbar" section.
\r
110 if ( !defaultToolbar )
\r
111 CKEDITOR.document.getById( 'currentToolbar' ).show();
\r
116 if ( !buttonsNames )
\r
117 buttonsNames = createButtonsNamesHash( editor.ui.items );
\r
119 // Toolbar isn't set explicitly, so it was created automatically from toolbarGroups.
\r
120 if ( !editor.config.toolbar ) {
\r
122 '// Toolbar configuration generated automatically by the editor based on config.toolbarGroups.\n' +
\r
123 dumpToolbarConfiguration( editor ) +
\r
125 '// Toolbar groups configuration.\n' +
\r
126 dumpToolbarConfiguration( editor, true )
\r
128 // Toolbar groups doesn't count in this case - print only toolbar.
\r
130 output += '// Toolbar configuration.\n' +
\r
131 dumpToolbarConfiguration( editor );
\r
134 // Recreate to avoid old IE from loosing whitespaces on filling <pre> content.
\r
135 var preOutput = pre.getOuterHtml().replace( /(?=<\/)/, output );
\r
136 CKEDITOR.dom.element.createFromHtml( preOutput ).replace( pre );
\r
139 CKEDITOR.replace( 'editorCurrent', { height: 100 } );
\r
140 CKEDITOR.replace( 'editorFull', {
\r
141 // Reset toolbar settings, so full toolbar will be generated automatically.
\r
143 toolbarGroups: null,
\r
147 function dumpToolbarConfiguration( editor, printGroups ) {
\r
149 toolbar = editor.toolbar;
\r
151 for ( var i = 0; i < toolbar.length; ++i ) {
\r
152 var group = dumpToolbarGroup( toolbar[ i ], printGroups );
\r
154 output.push( group );
\r
157 return 'config.toolbar' + ( printGroups ? 'Groups' : '' ) + ' = [\n\t' + output.join( ',\n\t' ) + '\n];';
\r
160 function dumpToolbarGroup( group, printGroups ) {
\r
163 if ( typeof group == 'string' )
\r
164 return '\'' + group + '\'';
\r
165 if ( CKEDITOR.tools.isArray( group ) )
\r
166 return dumpToolbarItems( group );
\r
167 // Skip group when printing entire toolbar configuration and there are no items in this group.
\r
168 if ( !printGroups && !group.items )
\r
172 output.push( 'name: \'' + group.name + '\'' );
\r
174 if ( group.groups )
\r
175 output.push( 'groups: ' + dumpToolbarItems( group.groups ) );
\r
177 if ( !printGroups )
\r
178 output.push( 'items: ' + dumpToolbarItems( group.items ) );
\r
180 return '{ ' + output.join( ', ' ) + ' }';
\r
183 function dumpToolbarItems( items ) {
\r
184 if ( typeof items == 'string' )
\r
185 return '\'' + items + '\'';
\r
190 for ( var i = 0; i < items.length; ++i ) {
\r
192 if ( typeof item == 'string' )
\r
193 names.push( item );
\r
195 if ( item.type == CKEDITOR.UI_SEPARATOR )
\r
198 names.push( buttonsNames[ item.name ] );
\r
202 return '[ \'' + names.join( '\', \'' ) + '\' ]';
\r
205 // Creates { 'lowercased': 'LowerCased' } buttons names hash.
\r
206 function createButtonsNamesHash( items ) {
\r
210 for ( name in items ) {
\r
211 hash[ items[ name ].name ] = name;
\r
223 CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>
\r
226 Copyright © 2003-2013, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
\r
227 Knabben. All rights reserved.
\r