JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / format / plugin.js
1 /*\r
2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 CKEDITOR.plugins.add( 'format',\r
7 {\r
8         requires : [ 'richcombo', 'styles' ],\r
9 \r
10         init : function( editor )\r
11         {\r
12                 var config = editor.config,\r
13                         lang = editor.lang.format;\r
14 \r
15                 // Gets the list of tags from the settings.\r
16                 var tags = config.format_tags.split( ';' );\r
17 \r
18                 // Create style objects for all defined styles.\r
19                 var styles = {};\r
20                 for ( var i = 0 ; i < tags.length ; i++ )\r
21                 {\r
22                         var tag = tags[ i ];\r
23                         styles[ tag ] = new CKEDITOR.style( config[ 'format_' + tag ] );\r
24                         styles[ tag ]._.enterMode = editor.config.enterMode;\r
25                 }\r
26 \r
27                 editor.ui.addRichCombo( 'Format',\r
28                         {\r
29                                 label : lang.label,\r
30                                 title : lang.panelTitle,\r
31                                 className : 'cke_format',\r
32                                 panel :\r
33                                 {\r
34                                         css : editor.skin.editor.css.concat( config.contentsCss ),\r
35                                         multiSelect : false,\r
36                                         attributes : { 'aria-label' : lang.panelTitle }\r
37                                 },\r
38 \r
39                                 init : function()\r
40                                 {\r
41                                         this.startGroup( lang.panelTitle );\r
42 \r
43                                         for ( var tag in styles )\r
44                                         {\r
45                                                 var label = lang[ 'tag_' + tag ];\r
46 \r
47                                                 // Add the tag entry to the panel list.\r
48                                                 this.add( tag, styles[tag].buildPreview( label ), label );\r
49                                         }\r
50                                 },\r
51 \r
52                                 onClick : function( value )\r
53                                 {\r
54                                         editor.focus();\r
55                                         editor.fire( 'saveSnapshot' );\r
56 \r
57                                         var style = styles[ value ],\r
58                                                 elementPath = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() );\r
59 \r
60                                         style[ style.checkActive( elementPath ) ? 'remove' : 'apply' ]( editor.document );\r
61 \r
62                                         // Save the undo snapshot after all changes are affected. (#4899)\r
63                                         setTimeout( function()\r
64                                         {\r
65                                                 editor.fire( 'saveSnapshot' );\r
66                                         }, 0 );\r
67                                 },\r
68 \r
69                                 onRender : function()\r
70                                 {\r
71                                         editor.on( 'selectionChange', function( ev )\r
72                                                 {\r
73                                                         var currentTag = this.getValue();\r
74 \r
75                                                         var elementPath = ev.data.path;\r
76 \r
77                                                         for ( var tag in styles )\r
78                                                         {\r
79                                                                 if ( styles[ tag ].checkActive( elementPath ) )\r
80                                                                 {\r
81                                                                         if ( tag != currentTag )\r
82                                                                                 this.setValue( tag, editor.lang.format[ 'tag_' + tag ] );\r
83                                                                         return;\r
84                                                                 }\r
85                                                         }\r
86 \r
87                                                         // If no styles match, just empty it.\r
88                                                         this.setValue( '' );\r
89                                                 },\r
90                                                 this);\r
91                                 }\r
92                         });\r
93         }\r
94 });\r
95 \r
96 /**\r
97  * A list of semi colon separated style names (by default tags) representing\r
98  * the style definition for each entry to be displayed in the Format combo in\r
99  * the toolbar. Each entry must have its relative definition configuration in a\r
100  * setting named "format_(tagName)". For example, the "p" entry has its\r
101  * definition taken from config.format_p.\r
102  * @type String\r
103  * @default 'p;h1;h2;h3;h4;h5;h6;pre;address;div'\r
104  * @example\r
105  * config.format_tags = 'p;h2;h3;pre'\r
106  */\r
107 CKEDITOR.config.format_tags = 'p;h1;h2;h3;h4;h5;h6;pre;address;div';\r
108 \r
109 /**\r
110  * The style definition to be used to apply the "Normal" format.\r
111  * @type Object\r
112  * @default { element : 'p' }\r
113  * @example\r
114  * config.format_p = { element : 'p', attributes : { 'class' : 'normalPara' } };\r
115  */\r
116 CKEDITOR.config.format_p = { element : 'p' };\r
117 \r
118 /**\r
119  * The style definition to be used to apply the "Normal (DIV)" format.\r
120  * @type Object\r
121  * @default { element : 'div' }\r
122  * @example\r
123  * config.format_div = { element : 'div', attributes : { 'class' : 'normalDiv' } };\r
124  */\r
125 CKEDITOR.config.format_div = { element : 'div' };\r
126 \r
127 /**\r
128  * The style definition to be used to apply the "Formatted" format.\r
129  * @type Object\r
130  * @default { element : 'pre' }\r
131  * @example\r
132  * config.format_pre = { element : 'pre', attributes : { 'class' : 'code' } };\r
133  */\r
134 CKEDITOR.config.format_pre = { element : 'pre' };\r
135 \r
136 /**\r
137  * The style definition to be used to apply the "Address" format.\r
138  * @type Object\r
139  * @default { element : 'address' }\r
140  * @example\r
141  * config.format_address = { element : 'address', attributes : { 'class' : 'styledAddress' } };\r
142  */\r
143 CKEDITOR.config.format_address = { element : 'address' };\r
144 \r
145 /**\r
146  * The style definition to be used to apply the "Heading 1" format.\r
147  * @type Object\r
148  * @default { element : 'h1' }\r
149  * @example\r
150  * config.format_h1 = { element : 'h1', attributes : { 'class' : 'contentTitle1' } };\r
151  */\r
152 CKEDITOR.config.format_h1 = { element : 'h1' };\r
153 \r
154 /**\r
155  * The style definition to be used to apply the "Heading 1" format.\r
156  * @type Object\r
157  * @default { element : 'h2' }\r
158  * @example\r
159  * config.format_h2 = { element : 'h2', attributes : { 'class' : 'contentTitle2' } };\r
160  */\r
161 CKEDITOR.config.format_h2 = { element : 'h2' };\r
162 \r
163 /**\r
164  * The style definition to be used to apply the "Heading 1" format.\r
165  * @type Object\r
166  * @default { element : 'h3' }\r
167  * @example\r
168  * config.format_h3 = { element : 'h3', attributes : { 'class' : 'contentTitle3' } };\r
169  */\r
170 CKEDITOR.config.format_h3 = { element : 'h3' };\r
171 \r
172 /**\r
173  * The style definition to be used to apply the "Heading 1" format.\r
174  * @type Object\r
175  * @default { element : 'h4' }\r
176  * @example\r
177  * config.format_h4 = { element : 'h4', attributes : { 'class' : 'contentTitle4' } };\r
178  */\r
179 CKEDITOR.config.format_h4 = { element : 'h4' };\r
180 \r
181 /**\r
182  * The style definition to be used to apply the "Heading 1" format.\r
183  * @type Object\r
184  * @default { element : 'h5' }\r
185  * @example\r
186  * config.format_h5 = { element : 'h5', attributes : { 'class' : 'contentTitle5' } };\r
187  */\r
188 CKEDITOR.config.format_h5 = { element : 'h5' };\r
189 \r
190 /**\r
191  * The style definition to be used to apply the "Heading 1" format.\r
192  * @type Object\r
193  * @default { element : 'h6' }\r
194  * @example\r
195  * config.format_h6 = { element : 'h6', attributes : { 'class' : 'contentTitle6' } };\r
196  */\r
197 CKEDITOR.config.format_h6 = { element : 'h6' };\r