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