3 Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
\r
4 For licensing, see LICENSE.html 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 <meta name="ckeditor-sample-isnew" content="1">
\r
14 <script src="../../../ckeditor.js"></script>
\r
15 <link href="../../../samples/sample.css" rel="stylesheet">
\r
18 <h1 class="samples">
\r
19 <a href="../../../samples/index.html">CKEditor Samples</a> » Toolbar Configuration
\r
21 <div class="description">
\r
23 This sample page demonstrates editor with loaded <a href="#fullToolbar">full toolbar</a> (all registered buttons) and, if
\r
24 current editor's configuration modifies default settings, also editor with <a href="#currentToolbar">modified toolbar</a>.
\r
27 <p>Since CKEditor 4 there are two ways to configure toolbar buttons.</p>
\r
29 <h2 class="samples">By <a href="http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-toolbar">config.toolbar</a></h2>
\r
32 You can explicitly define which buttons are displayed in which groups and in which order.
\r
33 This is the more precise setting, but less flexible. If newly added plugin adds its
\r
34 own button you'll have to add it manually to your <code>config.toolbar</code> setting as well.
\r
37 <p>To add a CKEditor instance with custom toolbar setting, insert the following JavaScript call to your code:</p>
\r
39 <pre class="samples">
\r
40 CKEDITOR.replace( <em>'textarea_id'</em>, {
\r
41 <strong>toolbar:</strong> [
\r
42 { name: 'document', items: [ 'Source', '-', 'NewPage', 'Preview', '-', 'Templates' ] }, // Defines toolbar group with name (used to create voice label) and items in 3 subgroups.
\r
43 [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ], // Defines toolbar group without name.
\r
44 '/', // Line break - next group will be placed in new line.
\r
45 { name: 'basicstyles', items: [ 'Bold', 'Italic' ] }
\r
49 <h2 class="samples">By <a href="http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-toolbarGroups">config.toolbarGroups</a></h2>
\r
52 You can define which groups of buttons (like e.g. <code>basicstyles</code>, <code>clipboard</code>
\r
53 and <code>forms</code>) are displayed and in which order. Registered buttons are associated
\r
54 with toolbar groups by <code>toolbar</code> property in their definition.
\r
55 This setting's advantage is that you don't have to modify toolbar configuration
\r
56 when adding/removing plugins which register their own buttons.
\r
59 <p>To add a CKEditor instance with custom toolbar groups setting, insert the following JavaScript call to your code:</p>
\r
61 <pre class="samples">
\r
62 CKEDITOR.replace( <em>'textarea_id'</em>, {
\r
63 <strong>toolbarGroups:</strong> [
\r
64 { name: 'document', groups: [ 'mode', 'document' ] }, // Displays document group with its two subgroups.
\r
65 { name: 'clipboard', groups: [ 'clipboard', 'undo' ] }, // Group's name will be used to create voice label.
\r
66 '/', // Line break - next group will be placed in new line.
\r
67 { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
\r
71 // NOTE: Remember to leave 'toolbar' property with the default value (null).
\r
75 <div id="currentToolbar" style="display: none">
\r
76 <h2 class="samples">Current toolbar configuration</h2>
\r
77 <p>Below you can see editor with current toolbar definition.</p>
\r
78 <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
79 <pre id="editorCurrentCfg" class="samples"></pre>
\r
82 <div id="fullToolbar">
\r
83 <h2 class="samples">Full toolbar configuration</h2>
\r
84 <p>Below you can see editor with full toolbar, generated automatically by the editor.</p>
\r
86 <strong>Note</strong>: To create editor instance with full toolbar you don't have to set anything.
\r
87 Just leave <code>toolbar</code> and <code>toolbarGroups</code> with the default, <code>null</code> values.
\r
89 <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
90 <pre id="editorFullCfg" class="samples"></pre>
\r
98 CKEDITOR.config.extraPlugins = 'toolbar';
\r
100 CKEDITOR.on( 'instanceReady', function( evt ) {
\r
101 var editor = evt.editor,
\r
102 editorCurrent = editor.name == 'editorCurrent',
\r
103 defaultToolbar = !( editor.config.toolbar || editor.config.toolbarGroups ),
\r
104 pre = CKEDITOR.document.getById( editor.name + 'Cfg' ),
\r
107 if ( editorCurrent ) {
\r
108 // If default toolbar configuration has been modified, show "current toolbar" section.
\r
109 if ( !defaultToolbar )
\r
110 CKEDITOR.document.getById( 'currentToolbar' ).show();
\r
115 // Toolbar isn't set explicitly, so it was created automatically from toolbarGroups.
\r
116 if ( !editor.config.toolbar ) {
\r
118 '// Toolbar configuration generated automatically by the editor based on config.toolbarGroups.\n' +
\r
119 dumpToolbarConfiguration( editor ) +
\r
121 '// Toolbar groups configuration.\n' +
\r
122 dumpToolbarConfiguration( editor, true )
\r
124 // Toolbar groups doesn't count in this case - print only toolbar.
\r
126 output += '// Toolbar configuration.\n' +
\r
127 dumpToolbarConfiguration( editor );
\r
130 // Recreate to avoid old IE from loosing whitespaces on filling <pre> content.
\r
131 var preOutput = pre.getOuterHtml().replace( /(?=<\/)/, output );
\r
132 CKEDITOR.dom.element.createFromHtml( preOutput ).replace( pre );
\r
136 CKEDITOR.replace( 'editorCurrent', { height: 100 } );
\r
137 CKEDITOR.replace( 'editorFull', {
\r
138 // Reset toolbar settings, so full toolbar will be generated automatically.
\r
140 toolbarGroups: null,
\r
144 function dumpToolbarConfiguration( editor, printGroups ) {
\r
146 toolbar = editor.toolbar;
\r
148 for ( var i = 0; i < toolbar.length; ++i ) {
\r
149 var group = dumpToolbarGroup( toolbar[ i ], printGroups );
\r
151 output.push( group );
\r
154 return 'config.toolbar' + ( printGroups ? 'Groups' : '' ) + ' = [\n\t' + output.join( ',\n\t' ) + '\n];';
\r
157 function dumpToolbarGroup( group, printGroups ) {
\r
160 if ( typeof group == 'string' )
\r
161 return '\'' + group + '\'';
\r
162 if ( CKEDITOR.tools.isArray( group ) )
\r
163 return dumpToolbarItems( group );
\r
164 // Skip group when printing entire toolbar configuration and there are no items in this group.
\r
165 if ( !printGroups && !group.items )
\r
169 output.push( 'name: \'' + group.name + '\'' );
\r
171 if ( group.groups )
\r
172 output.push( 'groups: ' + dumpToolbarItems( group.groups ) );
\r
174 if ( !printGroups )
\r
175 output.push( 'items: ' + dumpToolbarItems( group.items ) );
\r
177 return '{ ' + output.join( ', ' ) + ' }';
\r
180 function dumpToolbarItems( items ) {
\r
181 if ( typeof items == 'string' )
\r
182 return '\'' + items + '\'';
\r
183 return '[ \'' + items.join( '\', \'' ) + '\' ]';
\r
192 CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>
\r
195 Copyright © 2003-2013, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
\r
196 Knabben. All rights reserved.
\r