JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1.1
[ckeditor.git] / _source / plugins / font / 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 (function()\r
7 {\r
8         function addCombo( editor, comboName, styleType, lang, entries, defaultLabel, styleDefinition )\r
9         {\r
10                 var config = editor.config;\r
11 \r
12                 // Gets the list of fonts from the settings.\r
13                 var names = entries.split( ';' ),\r
14                         values = [];\r
15 \r
16                 // Create style objects for all fonts.\r
17                 var styles = {};\r
18                 for ( var i = 0 ; i < names.length ; i++ )\r
19                 {\r
20                         var parts = names[ i ];\r
21 \r
22                         if ( parts )\r
23                         {\r
24                                 parts = parts.split( '/' );\r
25 \r
26                                 var vars = {},\r
27                                         name = names[ i ] = parts[ 0 ];\r
28 \r
29                                 vars[ styleType ] = values[ i ] = parts[ 1 ] || name;\r
30 \r
31                                 styles[ name ] = new CKEDITOR.style( styleDefinition, vars );\r
32                         }\r
33                         else\r
34                                 names.splice( i--, 1 );\r
35                 }\r
36 \r
37                 editor.ui.addRichCombo( comboName,\r
38                         {\r
39                                 label : lang.label,\r
40                                 title : lang.panelTitle,\r
41                                 voiceLabel : lang.voiceLabel,\r
42                                 className : 'cke_' + ( styleType == 'size' ? 'fontSize' : 'font' ),\r
43                                 multiSelect : false,\r
44 \r
45                                 panel :\r
46                                 {\r
47                                         css : editor.skin.editor.css.concat( config.contentsCss ),\r
48                                         voiceLabel : lang.panelVoiceLabel\r
49                                 },\r
50 \r
51                                 init : function()\r
52                                 {\r
53                                         this.startGroup( lang.panelTitle );\r
54 \r
55                                         for ( var i = 0 ; i < names.length ; i++ )\r
56                                         {\r
57                                                 var name = names[ i ];\r
58 \r
59                                                 // Add the tag entry to the panel list.\r
60                                                 this.add( name, '<span style="font-' + styleType + ':' + values[ i ] + '">' + name + '</span>', name );\r
61                                         }\r
62                                 },\r
63 \r
64                                 onClick : function( value )\r
65                                 {\r
66                                         editor.focus();\r
67                                         editor.fire( 'saveSnapshot' );\r
68 \r
69                                         var style = styles[ value ];\r
70 \r
71                                         if ( this.getValue() == value )\r
72                                                 style.remove( editor.document );\r
73                                         else\r
74                                                 style.apply( editor.document );\r
75 \r
76                                         editor.fire( 'saveSnapshot' );\r
77                                 },\r
78 \r
79                                 onRender : function()\r
80                                 {\r
81                                         editor.on( 'selectionChange', function( ev )\r
82                                                 {\r
83                                                         var currentValue = this.getValue();\r
84 \r
85                                                         var elementPath = ev.data.path,\r
86                                                                 elements = elementPath.elements;\r
87 \r
88                                                         // For each element into the elements path.\r
89                                                         for ( var i = 0, element ; i < elements.length ; i++ )\r
90                                                         {\r
91                                                                 element = elements[i];\r
92 \r
93                                                                 // Check if the element is removable by any of\r
94                                                                 // the styles.\r
95                                                                 for ( var value in styles )\r
96                                                                 {\r
97                                                                         if ( styles[ value ].checkElementRemovable( element, true ) )\r
98                                                                         {\r
99                                                                                 if ( value != currentValue )\r
100                                                                                         this.setValue( value );\r
101                                                                                 return;\r
102                                                                         }\r
103                                                                 }\r
104                                                         }\r
105 \r
106                                                         // If no styles match, just empty it.\r
107                                                         this.setValue( '', defaultLabel );\r
108                                                 },\r
109                                                 this);\r
110                                 }\r
111                         });\r
112         }\r
113 \r
114         CKEDITOR.plugins.add( 'font',\r
115         {\r
116                 requires : [ 'richcombo', 'styles' ],\r
117 \r
118                 init : function( editor )\r
119                 {\r
120                         var config = editor.config;\r
121 \r
122                         addCombo( editor, 'Font', 'family', editor.lang.font, config.font_names, config.font_defaultLabel, config.font_style );\r
123                         addCombo( editor, 'FontSize', 'size', editor.lang.fontSize, config.fontSize_sizes, config.fontSize_defaultLabel, config.fontSize_style );\r
124                 }\r
125         });\r
126 })();\r
127 \r
128 /**\r
129  * The list of fonts names to be displayed in the Font combo in the toolbar.\r
130  * Entries are separated by semi-colons (;), while it's possible to have more\r
131  * than one font for each entry, in the HTML way (separated by comma).\r
132  *\r
133  * A display name may be optionally defined by prefixing the entries with the\r
134  * name and the slash character. For example, "Arial/Arial, Helvetica, sans-serif"\r
135  * will be displayed as "Arial" in the list, but will be outputted as\r
136  * "Arial, Helvetica, sans-serif".\r
137  * @type String\r
138  * @example\r
139  * config.font_names =\r
140  *     'Arial/Arial, Helvetica, sans-serif;' +\r
141  *     'Times New Roman/Times New Roman, Times, serif;' +\r
142  *     'Verdana';\r
143  * @example\r
144  * config.font_names = 'Arial;Times New Roman;Verdana';\r
145  */\r
146 CKEDITOR.config.font_names =\r
147         'Arial/Arial, Helvetica, sans-serif;' +\r
148         'Comic Sans MS/Comic Sans MS, cursive;' +\r
149         'Courier New/Courier New, Courier, monospace;' +\r
150         'Georgia/Georgia, serif;' +\r
151         'Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif;' +\r
152         'Tahoma/Tahoma, Geneva, sans-serif;' +\r
153         'Times New Roman/Times New Roman, Times, serif;' +\r
154         'Trebuchet MS/Trebuchet MS, Helvetica, sans-serif;' +\r
155         'Verdana/Verdana, Geneva, sans-serif';\r
156 \r
157 /**\r
158  * The text to be displayed in the Font combo is none of the available values\r
159  * matches the current cursor position or text selection.\r
160  * @type String\r
161  * @example\r
162  * // If the default site font is Arial, we may making it more explicit to the end user.\r
163  * config.font_defaultLabel = 'Arial';\r
164  */\r
165 CKEDITOR.config.font_defaultLabel = '';\r
166 \r
167 /**\r
168  * The style definition to be used to apply the font in the text.\r
169  * @type Object\r
170  * @example\r
171  * // This is actually the default value for it.\r
172  * config.font_style =\r
173  *     {\r
174  *         element              : 'span',\r
175  *         styles               : { 'font-family' : '#(family)' },\r
176  *         overrides    : [ { element : 'font', attributes : { 'face' : null } } ]\r
177  *     };\r
178  */\r
179 CKEDITOR.config.font_style =\r
180         {\r
181                 element         : 'span',\r
182                 styles          : { 'font-family' : '#(family)' },\r
183                 overrides       : [ { element : 'font', attributes : { 'face' : null } } ]\r
184         };\r
185 \r
186 /**\r
187  * The list of fonts size to be displayed in the Font Size combo in the\r
188  * toolbar. Entries are separated by semi-colons (;).\r
189  *\r
190  * Any kind of "CSS like" size can be used, like "12px", "2.3em", "130%",\r
191  * "larger" or "x-small".\r
192  *\r
193  * A display name may be optionally defined by prefixing the entries with the\r
194  * name and the slash character. For example, "Bigger Font/14px" will be\r
195  * displayed as "Bigger Font" in the list, but will be outputted as "14px".\r
196  * @type String\r
197  * @default '8/8px;9/9px;10/10px;11/11px;12/12px;14/14px;16/16px;18/18px;20/20px;22/22px;24/24px;26/26px;28/28px;36/36px;48/48px;72/72px'\r
198  * @example\r
199  * config.fontSize_sizes = '16/16px;24/24px;48/48px;';\r
200  * @example\r
201  * config.fontSize_sizes = '12px;2.3em;130%;larger;x-small';\r
202  * @example\r
203  * config.fontSize_sizes = '12 Pixels/12px;Big/2.3em;30 Percent More/130%;Bigger/larger;Very Small/x-small';\r
204  */\r
205 CKEDITOR.config.fontSize_sizes =\r
206         '8/8px;9/9px;10/10px;11/11px;12/12px;14/14px;16/16px;18/18px;20/20px;22/22px;24/24px;26/26px;28/28px;36/36px;48/48px;72/72px';\r
207 \r
208 /**\r
209  * The text to be displayed in the Font Size combo is none of the available\r
210  * values matches the current cursor position or text selection.\r
211  * @type String\r
212  * @example\r
213  * // If the default site font size is 12px, we may making it more explicit to the end user.\r
214  * config.fontSize_defaultLabel = '12px';\r
215  */\r
216 CKEDITOR.config.fontSize_defaultLabel = '';\r
217 \r
218 /**\r
219  * The style definition to be used to apply the font size in the text.\r
220  * @type Object\r
221  * @example\r
222  * // This is actually the default value for it.\r
223  * config.fontSize_style =\r
224  *     {\r
225  *         element              : 'span',\r
226  *         styles               : { 'font-size' : '#(size)' },\r
227  *         overrides    : [ { element : 'font', attributes : { 'size' : null } } ]\r
228  *     };\r
229  */\r
230 CKEDITOR.config.fontSize_style =\r
231         {\r
232                 element         : 'span',\r
233                 styles          : { 'font-size' : '#(size)' },\r
234                 overrides       : [ { element : 'font', attributes : { 'size' : null } } ]\r
235         };\r