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