]>
Commit | Line | Data |
---|---|---|
1 | /**\r | |
2 | * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.\r | |
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license\r | |
4 | */\r | |
5 | \r | |
6 | ( function() {\r | |
7 | function addCombo( editor, comboName, styleType, lang, entries, defaultLabel, styleDefinition, order ) {\r | |
8 | var config = editor.config,\r | |
9 | style = new CKEDITOR.style( styleDefinition );\r | |
10 | \r | |
11 | // Gets the list of fonts from the settings.\r | |
12 | var names = entries.split( ';' ),\r | |
13 | values = [];\r | |
14 | \r | |
15 | // Create style objects for all fonts.\r | |
16 | var styles = {};\r | |
17 | for ( var i = 0; i < names.length; i++ ) {\r | |
18 | var parts = names[ i ];\r | |
19 | \r | |
20 | if ( parts ) {\r | |
21 | parts = parts.split( '/' );\r | |
22 | \r | |
23 | var vars = {},\r | |
24 | name = names[ i ] = parts[ 0 ];\r | |
25 | \r | |
26 | vars[ styleType ] = values[ i ] = parts[ 1 ] || name;\r | |
27 | \r | |
28 | styles[ name ] = new CKEDITOR.style( styleDefinition, vars );\r | |
29 | styles[ name ]._.definition.name = name;\r | |
30 | } else {\r | |
31 | names.splice( i--, 1 );\r | |
32 | }\r | |
33 | }\r | |
34 | \r | |
35 | editor.ui.addRichCombo( comboName, {\r | |
36 | label: lang.label,\r | |
37 | title: lang.panelTitle,\r | |
38 | toolbar: 'styles,' + order,\r | |
39 | allowedContent: style,\r | |
40 | requiredContent: style,\r | |
41 | \r | |
42 | panel: {\r | |
43 | css: [ CKEDITOR.skin.getPath( 'editor' ) ].concat( config.contentsCss ),\r | |
44 | multiSelect: false,\r | |
45 | attributes: { 'aria-label': lang.panelTitle }\r | |
46 | },\r | |
47 | \r | |
48 | init: function() {\r | |
49 | this.startGroup( lang.panelTitle );\r | |
50 | \r | |
51 | for ( var i = 0; i < names.length; i++ ) {\r | |
52 | var name = names[ i ];\r | |
53 | \r | |
54 | // Add the tag entry to the panel list.\r | |
55 | this.add( name, styles[ name ].buildPreview(), name );\r | |
56 | }\r | |
57 | },\r | |
58 | \r | |
59 | onClick: function( value ) {\r | |
60 | editor.focus();\r | |
61 | editor.fire( 'saveSnapshot' );\r | |
62 | \r | |
63 | var previousValue = this.getValue(),\r | |
64 | style = styles[ value ];\r | |
65 | \r | |
66 | // When applying one style over another, first remove the previous one (#12403).\r | |
67 | // NOTE: This is only a temporary fix. It will be moved to the styles system (#12687).\r | |
68 | if ( previousValue && value != previousValue ) {\r | |
69 | var previousStyle = styles[ previousValue ],\r | |
70 | range = editor.getSelection().getRanges()[ 0 ];\r | |
71 | \r | |
72 | // If the range is collapsed we can't simply use the editor.removeStyle method\r | |
73 | // because it will remove the entire element and we want to split it instead.\r | |
74 | if ( range.collapsed ) {\r | |
75 | var path = editor.elementPath(),\r | |
76 | // Find the style element.\r | |
77 | matching = path.contains( function( el ) {\r | |
78 | return previousStyle.checkElementRemovable( el );\r | |
79 | } );\r | |
80 | \r | |
81 | if ( matching ) {\r | |
82 | var startBoundary = range.checkBoundaryOfElement( matching, CKEDITOR.START ),\r | |
83 | endBoundary = range.checkBoundaryOfElement( matching, CKEDITOR.END ),\r | |
84 | node, bm;\r | |
85 | \r | |
86 | // If we are at both boundaries it means that the element is empty.\r | |
87 | // Remove it but in a way that we won't lose other empty inline elements inside it.\r | |
88 | // Example: <p>x<span style="font-size:48px"><em>[]</em></span>x</p>\r | |
89 | // Result: <p>x<em>[]</em>x</p>\r | |
90 | if ( startBoundary && endBoundary ) {\r | |
91 | bm = range.createBookmark();\r | |
92 | // Replace the element with its children (TODO element.replaceWithChildren).\r | |
93 | while ( ( node = matching.getFirst() ) ) {\r | |
94 | node.insertBefore( matching );\r | |
95 | }\r | |
96 | matching.remove();\r | |
97 | range.moveToBookmark( bm );\r | |
98 | \r | |
99 | // If we are at the boundary of the style element, just move out.\r | |
100 | } else if ( startBoundary ) {\r | |
101 | range.moveToPosition( matching, CKEDITOR.POSITION_BEFORE_START );\r | |
102 | } else if ( endBoundary ) {\r | |
103 | range.moveToPosition( matching, CKEDITOR.POSITION_AFTER_END );\r | |
104 | } else {\r | |
105 | // Split the element and clone the elements that were in the path\r | |
106 | // (between the startContainer and the matching element)\r | |
107 | // into the new place.\r | |
108 | range.splitElement( matching );\r | |
109 | range.moveToPosition( matching, CKEDITOR.POSITION_AFTER_END );\r | |
110 | cloneSubtreeIntoRange( range, path.elements.slice(), matching );\r | |
111 | }\r | |
112 | \r | |
113 | editor.getSelection().selectRanges( [ range ] );\r | |
114 | }\r | |
115 | } else {\r | |
116 | editor.removeStyle( previousStyle );\r | |
117 | }\r | |
118 | }\r | |
119 | \r | |
120 | editor[ previousValue == value ? 'removeStyle' : 'applyStyle' ]( style );\r | |
121 | \r | |
122 | editor.fire( 'saveSnapshot' );\r | |
123 | },\r | |
124 | \r | |
125 | onRender: function() {\r | |
126 | editor.on( 'selectionChange', function( ev ) {\r | |
127 | var currentValue = this.getValue();\r | |
128 | \r | |
129 | var elementPath = ev.data.path,\r | |
130 | elements = elementPath.elements;\r | |
131 | \r | |
132 | // For each element into the elements path.\r | |
133 | for ( var i = 0, element; i < elements.length; i++ ) {\r | |
134 | element = elements[ i ];\r | |
135 | \r | |
136 | // Check if the element is removable by any of\r | |
137 | // the styles.\r | |
138 | for ( var value in styles ) {\r | |
139 | if ( styles[ value ].checkElementMatch( element, true, editor ) ) {\r | |
140 | if ( value != currentValue )\r | |
141 | this.setValue( value );\r | |
142 | return;\r | |
143 | }\r | |
144 | }\r | |
145 | }\r | |
146 | \r | |
147 | // If no styles match, just empty it.\r | |
148 | this.setValue( '', defaultLabel );\r | |
149 | }, this );\r | |
150 | },\r | |
151 | \r | |
152 | refresh: function() {\r | |
153 | if ( !editor.activeFilter.check( style ) )\r | |
154 | this.setState( CKEDITOR.TRISTATE_DISABLED );\r | |
155 | }\r | |
156 | } );\r | |
157 | }\r | |
158 | \r | |
159 | // Clones the subtree between subtreeStart (exclusive) and the\r | |
160 | // leaf (inclusive) and inserts it into the range.\r | |
161 | //\r | |
162 | // @param range\r | |
163 | // @param {CKEDITOR.dom.element[]} elements Elements path in the standard order: leaf -> root.\r | |
164 | // @param {CKEDITOR.dom.element/null} substreeStart The start of the subtree.\r | |
165 | // If null, then the leaf belongs to the subtree.\r | |
166 | function cloneSubtreeIntoRange( range, elements, subtreeStart ) {\r | |
167 | var current = elements.pop();\r | |
168 | if ( !current ) {\r | |
169 | return;\r | |
170 | }\r | |
171 | // Rewind the elements array up to the subtreeStart and then start the real cloning.\r | |
172 | if ( subtreeStart ) {\r | |
173 | return cloneSubtreeIntoRange( range, elements, current.equals( subtreeStart ) ? null : subtreeStart );\r | |
174 | }\r | |
175 | \r | |
176 | var clone = current.clone();\r | |
177 | range.insertNode( clone );\r | |
178 | range.moveToPosition( clone, CKEDITOR.POSITION_AFTER_START );\r | |
179 | \r | |
180 | cloneSubtreeIntoRange( range, elements );\r | |
181 | }\r | |
182 | \r | |
183 | CKEDITOR.plugins.add( 'font', {\r | |
184 | requires: 'richcombo',\r | |
185 | // jscs:disable maximumLineLength\r | |
186 | lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%\r | |
187 | // jscs:enable maximumLineLength\r | |
188 | init: function( editor ) {\r | |
189 | var config = editor.config;\r | |
190 | \r | |
191 | addCombo( editor, 'Font', 'family', editor.lang.font, config.font_names, config.font_defaultLabel, config.font_style, 30 );\r | |
192 | addCombo( editor, 'FontSize', 'size', editor.lang.font.fontSize, config.fontSize_sizes, config.fontSize_defaultLabel, config.fontSize_style, 40 );\r | |
193 | }\r | |
194 | } );\r | |
195 | } )();\r | |
196 | \r | |
197 | /**\r | |
198 | * The list of fonts names to be displayed in the Font combo in the toolbar.\r | |
199 | * Entries are separated by semi-colons (`';'`), while it's possible to have more\r | |
200 | * than one font for each entry, in the HTML way (separated by comma).\r | |
201 | *\r | |
202 | * A display name may be optionally defined by prefixing the entries with the\r | |
203 | * name and the slash character. For example, `'Arial/Arial, Helvetica, sans-serif'`\r | |
204 | * will be displayed as `'Arial'` in the list, but will be outputted as\r | |
205 | * `'Arial, Helvetica, sans-serif'`.\r | |
206 | *\r | |
207 | * config.font_names =\r | |
208 | * 'Arial/Arial, Helvetica, sans-serif;' +\r | |
209 | * 'Times New Roman/Times New Roman, Times, serif;' +\r | |
210 | * 'Verdana';\r | |
211 | *\r | |
212 | * config.font_names = 'Arial;Times New Roman;Verdana';\r | |
213 | *\r | |
214 | * @cfg {String} [font_names=see source]\r | |
215 | * @member CKEDITOR.config\r | |
216 | */\r | |
217 | CKEDITOR.config.font_names = 'Arial/Arial, Helvetica, sans-serif;' +\r | |
218 | 'Comic Sans MS/Comic Sans MS, cursive;' +\r | |
219 | 'Courier New/Courier New, Courier, monospace;' +\r | |
220 | 'Georgia/Georgia, serif;' +\r | |
221 | 'Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif;' +\r | |
222 | 'Tahoma/Tahoma, Geneva, sans-serif;' +\r | |
223 | 'Times New Roman/Times New Roman, Times, serif;' +\r | |
224 | 'Trebuchet MS/Trebuchet MS, Helvetica, sans-serif;' +\r | |
225 | 'Verdana/Verdana, Geneva, sans-serif';\r | |
226 | \r | |
227 | /**\r | |
228 | * The text to be displayed in the Font combo is none of the available values\r | |
229 | * matches the current cursor position or text selection.\r | |
230 | *\r | |
231 | * // If the default site font is Arial, we may making it more explicit to the end user.\r | |
232 | * config.font_defaultLabel = 'Arial';\r | |
233 | *\r | |
234 | * @cfg {String} [font_defaultLabel='']\r | |
235 | * @member CKEDITOR.config\r | |
236 | */\r | |
237 | CKEDITOR.config.font_defaultLabel = '';\r | |
238 | \r | |
239 | /**\r | |
240 | * The style definition to be used to apply the font in the text.\r | |
241 | *\r | |
242 | * // This is actually the default value for it.\r | |
243 | * config.font_style = {\r | |
244 | * element: 'span',\r | |
245 | * styles: { 'font-family': '#(family)' },\r | |
246 | * overrides: [ { element: 'font', attributes: { 'face': null } } ]\r | |
247 | * };\r | |
248 | *\r | |
249 | * @cfg {Object} [font_style=see example]\r | |
250 | * @member CKEDITOR.config\r | |
251 | */\r | |
252 | CKEDITOR.config.font_style = {\r | |
253 | element: 'span',\r | |
254 | styles: { 'font-family': '#(family)' },\r | |
255 | overrides: [ {\r | |
256 | element: 'font', attributes: { 'face': null }\r | |
257 | } ]\r | |
258 | };\r | |
259 | \r | |
260 | /**\r | |
261 | * The list of fonts size to be displayed in the Font Size combo in the\r | |
262 | * toolbar. Entries are separated by semi-colons (`';'`).\r | |
263 | *\r | |
264 | * Any kind of "CSS like" size can be used, like `'12px'`, `'2.3em'`, `'130%'`,\r | |
265 | * `'larger'` or `'x-small'`.\r | |
266 | *\r | |
267 | * A display name may be optionally defined by prefixing the entries with the\r | |
268 | * name and the slash character. For example, `'Bigger Font/14px'` will be\r | |
269 | * displayed as `'Bigger Font'` in the list, but will be outputted as `'14px'`.\r | |
270 | *\r | |
271 | * config.fontSize_sizes = '16/16px;24/24px;48/48px;';\r | |
272 | *\r | |
273 | * config.fontSize_sizes = '12px;2.3em;130%;larger;x-small';\r | |
274 | *\r | |
275 | * config.fontSize_sizes = '12 Pixels/12px;Big/2.3em;30 Percent More/130%;Bigger/larger;Very Small/x-small';\r | |
276 | *\r | |
277 | * @cfg {String} [fontSize_sizes=see source]\r | |
278 | * @member CKEDITOR.config\r | |
279 | */\r | |
280 | CKEDITOR.config.fontSize_sizes = '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 | |
281 | \r | |
282 | /**\r | |
283 | * The text to be displayed in the Font Size combo is none of the available\r | |
284 | * values matches the current cursor position or text selection.\r | |
285 | *\r | |
286 | * // If the default site font size is 12px, we may making it more explicit to the end user.\r | |
287 | * config.fontSize_defaultLabel = '12px';\r | |
288 | *\r | |
289 | * @cfg {String} [fontSize_defaultLabel='']\r | |
290 | * @member CKEDITOR.config\r | |
291 | */\r | |
292 | CKEDITOR.config.fontSize_defaultLabel = '';\r | |
293 | \r | |
294 | /**\r | |
295 | * The style definition to be used to apply the font size in the text.\r | |
296 | *\r | |
297 | * // This is actually the default value for it.\r | |
298 | * config.fontSize_style = {\r | |
299 | * element: 'span',\r | |
300 | * styles: { 'font-size': '#(size)' },\r | |
301 | * overrides: [ { element :'font', attributes: { 'size': null } } ]\r | |
302 | * };\r | |
303 | *\r | |
304 | * @cfg {Object} [fontSize_style=see example]\r | |
305 | * @member CKEDITOR.config\r | |
306 | */\r | |
307 | CKEDITOR.config.fontSize_style = {\r | |
308 | element: 'span',\r | |
309 | styles: { 'font-size': '#(size)' },\r | |
310 | overrides: [ {\r | |
311 | element: 'font', attributes: { 'size': null }\r | |
312 | } ]\r | |
313 | };\r |