CKEditor Toolbar

This website contains links to software which is either no longer maintained or will be supported only until the end of 2019 (CKFinder 2). For the latest documentation about current CKSource projects, including software like CKEditor 4/CKEditor 5, CKFinder 3, Cloud Services, Letters, Accessibility Checker, please visit the new documentation website.

If you look for an information about very old versions of CKEditor, FCKeditor and CKFinder check also the CKEditor forum, which was closed in 2015. If not, please head to StackOverflow for support.

m
 
(23 intermediate revisions by 4 users not shown)
Line 1: Line 1:
While the editor is full featured, not all options may be needed in all cases. Because of this, customizing the toolbar is one of the most common and required tasks when dealing with CKEditor.  
+
{{#CUSTOMTITLE:CKEditor Toolbar}}
 +
While CKEditor is a full-featured WYSIWYG editor, not all of its options may be needed in all cases. Because of this, toolbar customization is one of the most common and required tasks when dealing with CKEditor.  
  
== Toolbar definition ==
+
== Toolbar Definition ==
 +
A toolbar definition is a JavaScript array that contains the elements to be displayed in all '''toolbar rows''' available in the editor.
  
A toolbar definition is a JavaScript array that contains the elements to be displayed in all "toolbar rows" available in the editor. There are two ways to set the desired toolbar definition in the editor. It can be set directly into the "toolbar" setting, or it can instead be set to a configuration named "toolbar_<name>", where "<name>" is a name that can be used to identify the toolbar in the "[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.toolbar toolbar]" setting. The following is the default setting we have in the editor.  
+
The toolbar configuration can be defined in CKEditor using one of the following methods:
<pre>config.toolbar = 'Full';
+
* The <code>[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.toolbar toolbar]</code> setting.
 +
* The <code>toolbar_''name''</code> setting, where the <code>''name''</code> element is the name used to identify the toolbar in the <code>toolbar</code> setting.
 +
 
 +
The following code snippet contains the default CKEditor toolbar setting.  
 +
<source language="js">
 +
config.toolbar = 'Full';
  
 
config.toolbar_Full =
 
config.toolbar_Full =
 
[
 
[
    ['Source','-','Save','NewPage','Preview','-','Templates'],
+
{ name: 'document', items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },
    ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
+
{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
    ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
+
{ name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },
    ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
+
{ name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton',  
    '/',
+
        'HiddenField' ] },
    ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
+
'/',
    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
+
{ name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
+
{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv',
    ['Link','Unlink','Anchor'],
+
'-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
    ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
+
{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
    '/',
+
{ name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe' ] },
    ['Styles','Format','Font','FontSize'],
+
'/',
    ['TextColor','BGColor'],
+
{ name: 'styles', items : [ 'Styles','Format','Font','FontSize' ] },
    ['Maximize', 'ShowBlocks','-','About']
+
{ name: 'colors', items : [ 'TextColor','BGColor' ] },
 +
{ name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }
 
];
 
];
  
 
config.toolbar_Basic =
 
config.toolbar_Basic =
 
[
 
[
    ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','About']
+
['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','About']
 
];
 
];
</pre>  
+
</source>  
Note that two toolbar definitions have been defined, one named "Full" and the other "Basic". The "Full" definition has been set to be used in the toolbar setting.  
+
Note that in the code above (just like in the default CKEditor configuration) two toolbar definitions have been defined. The first one is named '''Full''' and the second one is '''Basic'''. The toolbar was set to use the '''Full''' definition.
  
=== Toolbar Bands  ===
+
=== Toolbar Button Groups ===
 +
Every toolbar definition is composed of a series of '''toolbar button groups'''. Each toolbar group has a label (name) and the listing of buttons that belong to the group, placed in square brackets (<code>[ ]</code>). The toolbar group items move together to new rows when the editor is resized.
  
Every toolbar definition is composed of a series of "toolbar bands" that are grouped in the final toolbar layout. The bands items move together on new rows when resizing the editor.  
+
The group label will be used by assisstive devices such as screen readers. It is added in the <code>name</code> attribute and is first looked up in the CKEditor language file (under the <code>editor.lang.toolbarGroups.''group_name''</code> entry). When its definition is found, the text value of this entry is used. If the <code>name</code> attribute value does not appear in the language file, it is taken as a literal toolbar group label and read aloud.
  
As you can see in the above definitions, every toolbar band is defined as a separated JavaScript array of strings. Every string indicates toolbar item to be used. Toolbar items are defined by plugins.  
+
As visible in the code above, each toolbar button group is defined as a separate JavaScript array of strings. Every string stands for a single toolbar item to be used. Toolbar items are defined by plugins.  
  
You can also include a separator in the toolbar band by including the dash ("-") character on it.  
+
You can also include a separator in the toolbar group by including a dash (<code>"-"</code>) character in it.
  
=== Forcing Row Break ===
+
=== Forcing Row Break ===
 +
The '''Full''' definition contains numerous slash (<code>"/"</code>) characters that were placed between toolbar button groups. They can be used to force a break at the point where they were placed, rendering the next toolbar group in a new row. This placement will not be changed when CKEditor is resized.
  
Looking at the "Full" definition you will note some slash ("/") characters between toolbar bands. This slash can be used to force a break at that point, having the next band to be rendered in a new row and not following the previous one.  
+
== Toolbar Customization ==
 +
A simple way to configure the toolbars of all CKEditor instances is to add a custom toolbar definition inside the <code>config.js</code> file, or even better &mdash; in a separate custom configuration file (see [[CKEditor 3.x/Developers Guide/Setting Configurations|Setting Configuration]]). The easiest way to do that is to copy the default '''Full''' toolbar definition, and strip it down to your needs.
  
== Customizing the toolbar  ==
+
For example, the following is a good recommended toolbar definition that can be placed in the <code>config.js</code> file:  
 
+
<source language="javascript">
A simple way to configure all editors toolbar is by simply adding a custom toolbar definition inside the config.js file, or even better in a separated configuration file (see "[[CKEditor_3.x/Developers_Guide/Setting_Configurations|Setting Configurations]]"). The easiest way for that is by simply copying the above "Full" toolbar definition, and strip it down to your needs. For example, the following is a good recommended toolbar definition to have in the config.js file:  
+
CKEDITOR.editorConfig = function( config )
<pre>CKEDITOR.editorConfig = function( config )
 
 
{
 
{
    config.toolbar = 'MyToolbar';
+
config.toolbar = 'MyToolbar';
  
    config.toolbar_MyToolbar =
+
config.toolbar_MyToolbar =
    [
+
[
        ['NewPage','Preview'],
+
{ name: 'document', items : [ 'NewPage','Preview' ] },
        ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Scayt'],
+
{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
        ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
+
{ name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','Scayt' ] },
        ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
+
{ name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'
        '/',
+
                ,'Iframe' ] },
        ['Styles','Format'],
+
                '/',
        ['Bold','Italic','Strike'],
+
{ name: 'styles', items : [ 'Styles','Format' ] },
        ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
+
{ name: 'basicstyles', items : [ 'Bold','Italic','Strike','-','RemoveFormat' ] },
        ['Link','Unlink','Anchor'],
+
{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote' ] },
        ['Maximize','-','About']
+
{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
    ];
+
{ name: 'tools', items : [ 'Maximize','-','About' ] }
 +
];
 
};
 
};
</pre>  
+
</source>  
You can create as many toolbar definitions as you want inside the configuration file. Later, based on some criteria, you can decide the toolbar to use for each editor instance. For example, with the following code, two editors are created in the page, each one using a different toolbar:  
+
Inside the configuration file you can create as many toolbar definitions as you need. Later, based on some criteria, you can choose the toolbar to use for each CKEditor instance. Each toolbar definition can be used as many times as required (or not used at all, for that matter). For example, with the following code, two editors are created on the page and each one is using a different toolbar:  
<pre>CKEDITOR.replace( 'editor1',
+
<source language="javascript">
    {
+
CKEDITOR.replace( 'editor1',
        toolbar&nbsp;: 'MyToolbar'
+
{
    });
+
toolbar : 'MyToolbar'
 +
});
  
 
CKEDITOR.replace( 'editor2',
 
CKEDITOR.replace( 'editor2',
    {
+
{
        toolbar&nbsp;: 'Basic'
+
toolbar : 'Basic'
    });
+
});
</pre>  
+
</source>  
It's also possible to set the toolbar definition in-page, when creating the editor instance directly. In that case, just assign it to the toolbar setting directly, for example:  
+
It is also possible to set the toolbar definition in-page, at the same time when you create an editor instance. If this is the case, assign the toolbar setting directly to the editor instance, like in the example below:  
<pre>CKEDITOR.replace( 'editor1',
+
<source language="javascript">
    {
+
CKEDITOR.replace( 'editor1',
        toolbar&nbsp;:
+
{
        [
+
toolbar :
            ['Styles', 'Format'],
+
[
            ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', '-', 'About']
+
{ name: 'basicstyles', items : [ 'Bold','Italic' ] },
        ]
+
{ name: 'paragraph', items : [ 'NumberedList','BulletedList' ] },
    });
+
{ name: 'tools', items : [ 'Maximize','-','About' ] }
</pre>
+
]
 +
});
 +
</source>
 +
 
 +
<note>Please note that the toolbar definition was changed in '''CKEditor 3.6''', when a concept of toolbar button groups was introduced. This feature is WAI-ARIA compliant and important for accessibility support, so we recommend to upgrade custom definitions created for previous versions and follow the new standard described above.</note>

Latest revision as of 12:13, 5 March 2012

While CKEditor is a full-featured WYSIWYG editor, not all of its options may be needed in all cases. Because of this, toolbar customization is one of the most common and required tasks when dealing with CKEditor.

Toolbar Definition

A toolbar definition is a JavaScript array that contains the elements to be displayed in all toolbar rows available in the editor.

The toolbar configuration can be defined in CKEditor using one of the following methods:

  • The toolbar setting.
  • The toolbar_name setting, where the name element is the name used to identify the toolbar in the toolbar setting.

The following code snippet contains the default CKEditor toolbar setting.

config.toolbar = 'Full';

config.toolbar_Full =
[
	{ name: 'document', items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },
	{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
	{ name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },
	{ name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 
        'HiddenField' ] },
	'/',
	{ name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
	{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv',
	'-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
	{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
	{ name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe' ] },
	'/',
	{ name: 'styles', items : [ 'Styles','Format','Font','FontSize' ] },
	{ name: 'colors', items : [ 'TextColor','BGColor' ] },
	{ name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }
];

config.toolbar_Basic =
[
	['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','About']
];

Note that in the code above (just like in the default CKEditor configuration) two toolbar definitions have been defined. The first one is named Full and the second one is Basic. The toolbar was set to use the Full definition.

Toolbar Button Groups

Every toolbar definition is composed of a series of toolbar button groups. Each toolbar group has a label (name) and the listing of buttons that belong to the group, placed in square brackets ([ ]). The toolbar group items move together to new rows when the editor is resized.

The group label will be used by assisstive devices such as screen readers. It is added in the name attribute and is first looked up in the CKEditor language file (under the editor.lang.toolbarGroups.group_name entry). When its definition is found, the text value of this entry is used. If the name attribute value does not appear in the language file, it is taken as a literal toolbar group label and read aloud.

As visible in the code above, each toolbar button group is defined as a separate JavaScript array of strings. Every string stands for a single toolbar item to be used. Toolbar items are defined by plugins.

You can also include a separator in the toolbar group by including a dash ("-") character in it.

Forcing Row Break

The Full definition contains numerous slash ("/") characters that were placed between toolbar button groups. They can be used to force a break at the point where they were placed, rendering the next toolbar group in a new row. This placement will not be changed when CKEditor is resized.

Toolbar Customization

A simple way to configure the toolbars of all CKEditor instances is to add a custom toolbar definition inside the config.js file, or even better — in a separate custom configuration file (see Setting Configuration). The easiest way to do that is to copy the default Full toolbar definition, and strip it down to your needs.

For example, the following is a good recommended toolbar definition that can be placed in the config.js file:

CKEDITOR.editorConfig = function( config )
{
	config.toolbar = 'MyToolbar';

	config.toolbar_MyToolbar =
	[
		{ name: 'document', items : [ 'NewPage','Preview' ] },
		{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
		{ name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','Scayt' ] },
		{ name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'
                 ,'Iframe' ] },
                '/',
		{ name: 'styles', items : [ 'Styles','Format' ] },
		{ name: 'basicstyles', items : [ 'Bold','Italic','Strike','-','RemoveFormat' ] },
		{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote' ] },
		{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
		{ name: 'tools', items : [ 'Maximize','-','About' ] }
	];
};

Inside the configuration file you can create as many toolbar definitions as you need. Later, based on some criteria, you can choose the toolbar to use for each CKEditor instance. Each toolbar definition can be used as many times as required (or not used at all, for that matter). For example, with the following code, two editors are created on the page and each one is using a different toolbar:

CKEDITOR.replace( 'editor1',
	{
		toolbar : 'MyToolbar'
	});

CKEDITOR.replace( 'editor2',
	{
		toolbar : 'Basic'
	});

It is also possible to set the toolbar definition in-page, at the same time when you create an editor instance. If this is the case, assign the toolbar setting directly to the editor instance, like in the example below:

CKEDITOR.replace( 'editor1',
	{
		toolbar :
		[
			{ name: 'basicstyles', items : [ 'Bold','Italic' ] },
			{ name: 'paragraph', items : [ 'NumberedList','BulletedList' ] },
			{ name: 'tools', items : [ 'Maximize','-','About' ] }
		]
	});
important note
Please note that the toolbar definition was changed in CKEditor 3.6, when a concept of toolbar button groups was introduced. This feature is WAI-ARIA compliant and important for accessibility support, so we recommend to upgrade custom definitions created for previous versions and follow the new standard described above.
This page was last edited on 5 March 2012, at 12:13.