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