File: plugin.js

package info (click to toggle)
ckeditor 4.22.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 290,692 kB
  • sloc: javascript: 237,176; sh: 202; makefile: 64; python: 37; php: 15; xml: 5
file content (512 lines) | stat: -rw-r--r-- 16,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/**
 * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
 */

( function() {
	var StyleData = CKEDITOR.tools.createClass( {
		// @param {Object} options
		// @param {String} options.entries Values from the configuration used to build a rich combo.
		// The values should be obtained from ( CKEDITOR.config#fontSize_sizes | CKEDITOR.config#font_names )
		// @param {String} options.styleVariable A variable name used in the style definition to build proper CKEDITOR.style.
		// By default it should be: ( 'size' | 'family' )
		// @param {Object} options.styleDefinition A template used to build each individual style definition based on entries.
		// The values should be obtained from ( CKEDITOR.config#fontSize_style | CKEDITOR.config#font_style )
		$: function( options ) {
			var entries = options.entries.split( ';' );

			this._.data = {};
			this._.names = [];

			for ( var i = 0; i < entries.length; i++ ) {
				var parts = entries[ i ],
					name,
					value,
					vars;

				if ( parts ) {
					parts = parts.split( '/' );
					name = parts[ 0 ];
					value = parts[ 1 ];
					vars = {};

					vars[ options.styleVariable ] = value || name;

					this._.data[ name ] = new CKEDITOR.style( options.styleDefinition, vars );
					this._.data[ name ]._.definition.name = name;

					this._.names.push( name );
				} else {
					entries.splice( i, 1 );
					i--;
				}
			}
		},

		proto: {
			getStyle: function( name ) {
				return this._.data[ name ];
			},

			addToCombo: function( combo ) {
				for ( var i = 0; i < this._.names.length; i++ ) {
					var name = this._.names[ i ];
					combo.add( name, this.getStyle( name ).buildPreview(), name );
				}
			},

			getMatchingValue: function( editor, path ) {
				var elements = path.elements;

				for ( var i = 0, element, value; i < elements.length; i++ ) {
					element = elements[ i ];

					// Check if the element is removable by any of the styles.
					value = this._.findMatchingStyleName( editor, element );

					if ( value ) {
						return value;
					}
				}

				return null;
			}
		},

		_: {
			findMatchingStyleName: function( editor, element ) {
				return CKEDITOR.tools.array.find( this._.names, function( name ) {
					return this.getStyle( name ).checkElementMatch( element, true, editor );
				}, this );
			}
		}
	} );

	// @param {CKEDITOR.editor} editor
	// @param {Object} definition
	// @param {String} definition.comboName
	// @oaram {String} definition.commandName The name used to register the command in the editor.
	// @param {String} definition.styleVariable It has 'size' or 'family' as a value.
	// @param {Object} definition.lang A reference to the language object used for a given combo.
	// @param {String} definition.entries Values used for given combo options.
	// @param {String} definition.defaultLabel The label used to describe the default value.
	// @param {Object} definition.styleDefinition An object representing the defintion for a given font combo obtained
	// from the the configuration.
	// @param {Number} definition.order A value used to position the icon in the toolbar.
	function addCombo( editor, definition ) {
		var config = editor.config,
			lang = definition.lang,
			defaultContentStyle = new CKEDITOR.style( definition.styleDefinition ),
			stylesData = new StyleData( {
				entries: definition.entries,
				styleVariable: definition.styleVariable,
				styleDefinition: definition.styleDefinition
			} ),
			command;

		editor.addCommand( definition.commandName , {
			exec: function( editor, data ) {
				var newStyle = data.newStyle,
					oldStyle = data.oldStyle,
					range = editor.getSelection().getRanges()[ 0 ],
					isRemove = newStyle === undefined;

				if ( !oldStyle && !newStyle ) {
					return;
				}

				// If the range is collapsed we can't simply use the editor.removeStyle method
				// because it will remove the entire element and we want to split it instead.
				if ( oldStyle && range.collapsed ) {
					splitElementOnCollapsedRange( {
						editor: editor,
						range: range,
						style: oldStyle
					} );
				}

				if ( isRemove ) {
					editor.removeStyle( oldStyle );
				} else {
					if ( oldStyle && !isEqualStyle( oldStyle, newStyle ) ) {
						editor.removeStyle( oldStyle );
					}

					editor.applyStyle( newStyle );
				}
			},

			refresh: function( editor, path ) {
				if ( !defaultContentStyle.checkApplicable( path, editor, editor.activeFilter ) ) {
					this.setState( CKEDITOR.TRISTATE_DISABLED );
				}
			}
		} );

		command = editor.getCommand( definition.commandName );

		editor.ui.addRichCombo( definition.comboName, {
			label: lang.label,
			title: lang.panelTitle,
			command: definition.commandName,
			toolbar: 'styles,' + definition.order,
			defaultValue: 'cke-default',
			allowedContent: defaultContentStyle,
			requiredContent: defaultContentStyle,
			contentTransformations: definition.styleDefinition.element === 'span' ? [
				[
					{
						element: 'font',
						check: 'span',
						left: function( element ) {
							return !!element.attributes.size ||
								!!element.attributes.align ||
								!!element.attributes.face;
						},
						right: function( element ) {
							var sizes = [
								'', // Non-existent size "0"
								'x-small',
								'small',
								'medium',
								'large',
								'x-large',
								'xx-large',
								'48px' // Closest value to what size="7" might mean.
							];

							element.name = 'span';

							if ( element.attributes.size ) {
								element.styles[ 'font-size' ] = sizes[ element.attributes.size ];
								delete element.attributes.size;
							}

							if ( element.attributes.align ) {
								element.styles[ 'text-align' ] = element.attributes.align;
								delete element.attributes.align;
							}

							if ( element.attributes.face ) {
								element.styles[ 'font-family' ] = element.attributes.face;
								delete element.attributes.face;
							}
						}
					}
				]
			] : null,
			panel: {
				css: [ CKEDITOR.skin.getPath( 'editor' ) ].concat( config.contentsCss ),
				multiSelect: false,
				attributes: { 'aria-label': lang.panelTitle }
			},

			init: function() {
				var defaultText = '(' + editor.lang.common.optionDefault + ')';

				this.startGroup( lang.panelTitle );

				// Add `(Default)` item as a first element on the drop-down list.
				this.add( this.defaultValue, defaultText, defaultText );

				stylesData.addToCombo( this );
			},

			onClick: onClickHandler,

			onRender: function() {
				editor.on( 'selectionChange', function( ev ) {
					var currentValue = this.getValue(),
						elementPath = ev.data.path,
						value = stylesData.getMatchingValue( editor, elementPath );

					if ( value ) {
						if ( value != currentValue ) {
							this.setValue( value );
						}

						return;
					}

					// If no styles match, just empty it.
					this.setValue( '', definition.defaultLabel );
				}, this );

				command.on( 'state', function() {
					this.setState( command.state );
				}, this );
			},

			refresh: function() {
				this.setState( command.state );
			}
		} );

		function onClickHandler( newValue ) {
			var oldValue = this.getValue();

			editor.focus();

			editor.fire( 'saveSnapshot' );

			editor.execCommand( definition.commandName, {
				newStyle: stylesData.getStyle( newValue ),
				oldStyle: stylesData.getStyle( oldValue )
			} );

			editor.fire( 'saveSnapshot' );
		}
	}

	function isEqualStyle( styleA, styleB ) {
		if ( !( styleA instanceof CKEDITOR.style ) || !( styleB instanceof CKEDITOR.style ) ) {
			return false;
		}

		var hasEqualAttributes = compareAttributes( styleA, styleB ),
			hasEqualStyles = compareStyles( styleA, styleB );

		return hasEqualAttributes && hasEqualStyles;

		function compareAttributes( styleA, styleB ) {
			var styleAAttributes = styleA.getDefinition().attributes,
				styleBAttributes = styleB.getDefinition().attributes;

			return CKEDITOR.tools.objectCompare( styleAAttributes, styleBAttributes );
		}

		function compareStyles( styleA, styleB ) {
			return CKEDITOR.style.getStyleText( styleA.getDefinition() ) === CKEDITOR.style.getStyleText( styleB.getDefinition() );
		}
	}

	//  * @param {Object} options
	//  * @param {CKEDITOR.editor} options.editor An instance of the editor.
	//  * @param {CKEDITOR.dom.range} options.range The analyzed range.
	//  * @param {CKEDITOR.style} options.style The old style which might already exist on this range.
	function splitElementOnCollapsedRange( options ) {
		var editor = options.editor,
			range = options.range,
			style = options.style,
			path,
			matching,
			startBoundary,
			endBoundary,
			node,
			bm;

		path = editor.elementPath();
		// Find the style element.
		matching = path.contains( function( el ) {
			return style.checkElementRemovable( el );
		} );

		if ( !matching ) {
			return;
		}

		startBoundary = range.checkBoundaryOfElement( matching, CKEDITOR.START );
		endBoundary = range.checkBoundaryOfElement( matching, CKEDITOR.END );

		// If we are at both boundaries it means that the element is empty.
		// Remove it but in a way that we won't lose other empty inline elements inside it.
		// Example: <p>x<span style="font-size:48px"><em>[]</em></span>x</p>
		// Result: <p>x<em>[]</em>x</p>
		if ( startBoundary && endBoundary ) {
			bm = range.createBookmark();
			// Replace the element with its children (TODO element.replaceWithChildren).
			while ( ( node = matching.getFirst() ) ) {
				node.insertBefore( matching );
			}
			matching.remove();
			range.moveToBookmark( bm );

		// If we are at the boundary of the style element, move out and copy nested styles/elements.
		} else if ( startBoundary || endBoundary ) {
			range.moveToPosition( matching, startBoundary ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_END );
			cloneSubtreeIntoRange( range, path.elements.slice(), matching );
		} else {
			// Split the element and clone the elements that were in the path
			// (between the startContainer and the matching element)
			// into the new place.
			range.splitElement( matching );
			range.moveToPosition( matching, CKEDITOR.POSITION_AFTER_END );
			cloneSubtreeIntoRange( range, path.elements.slice(), matching );
		}

		editor.getSelection().selectRanges( [ range ] );
	}

	// Clones the subtree between `subtreeStart` (exclusive) and the
	// leaf (inclusive) and inserts it into the range.
	//
	// @param range
	// @param {CKEDITOR.dom.element[]} elements Elements path in the standard order: leaf -> root.
	// @param {CKEDITOR.dom.element/null} substreeStart The start of the subtree.
	// If null, then the leaf belongs to the subtree.
	function cloneSubtreeIntoRange( range, elements, subtreeStart ) {
		var current = elements.pop();
		if ( !current ) {
			return;
		}
		// Rewind the elements array up to the subtreeStart and then start the real cloning.
		if ( subtreeStart ) {
			return cloneSubtreeIntoRange( range, elements, current.equals( subtreeStart ) ? null : subtreeStart );
		}

		var clone = current.clone();
		range.insertNode( clone );
		range.moveToPosition( clone, CKEDITOR.POSITION_AFTER_START );

		cloneSubtreeIntoRange( range, elements );
	}

	CKEDITOR.plugins.add( 'font', {
		requires: 'richcombo',
		// jscs:disable maximumLineLength
		lang: 'af,ar,az,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,es-mx,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,oc,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%
		// jscs:enable maximumLineLength
		init: function( editor ) {
			var config = editor.config;

			addCombo( editor, {
				comboName: 'Font',
				commandName: 'font',
				styleVariable: 'family',
				lang: editor.lang.font,
				entries: config.font_names,
				defaultLabel: config.font_defaultLabel,
				styleDefinition: config.font_style,
				order: 30
			} );
			addCombo( editor, {
				comboName: 'FontSize',
				commandName: 'fontSize',
				styleVariable: 'size',
				lang: editor.lang.font.fontSize,
				entries: config.fontSize_sizes,
				defaultLabel: config.fontSize_defaultLabel,
				styleDefinition: config.fontSize_style,
				order: 40
			} );
		}
	} );
} )();

/**
 * The list of font names to be displayed in the Font combo in the toolbar.
 * Entries are separated by semi-colons (`';'`) and it is possible to have more
 * than one font for each entry, in the HTML way (separated by commas).
 *
 * A display name may be optionally defined by prefixing the entries with the
 * name and the slash character. For example, `'Arial/Arial, Helvetica, sans-serif'`
 * will be displayed as `'Arial'` in the list, but will be output as
 * `'Arial, Helvetica, sans-serif'`.
 *
 *		config.font_names =
 *			'Arial/Arial, Helvetica, sans-serif;' +
 *			'Times New Roman/Times New Roman, Times, serif;' +
 *			'Verdana';
 *
 *		config.font_names = 'Arial;Times New Roman;Verdana';
 *
 * @cfg {String} [font_names=see source]
 * @member CKEDITOR.config
 */
CKEDITOR.config.font_names = 'Arial/Arial, Helvetica, sans-serif;' +
	'Comic Sans MS/Comic Sans MS, cursive;' +
	'Courier New/Courier New, Courier, monospace;' +
	'Georgia/Georgia, serif;' +
	'Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif;' +
	'Tahoma/Tahoma, Geneva, sans-serif;' +
	'Times New Roman/Times New Roman, Times, serif;' +
	'Trebuchet MS/Trebuchet MS, Helvetica, sans-serif;' +
	'Verdana/Verdana, Geneva, sans-serif';

/**
 * The text to be displayed in the Font combo if none of the available values
 * matches the current cursor position or text selection.
 *
 *		// If the default site font is Arial, we may make it more explicit to the end user.
 *		config.font_defaultLabel = 'Arial';
 *
 * @cfg {String} [font_defaultLabel='']
 * @member CKEDITOR.config
 */
CKEDITOR.config.font_defaultLabel = '';

/**
 * The style definition to be used to apply the font in the text.
 *
 *		// This is actually the default value for it.
 *		config.font_style = {
 *			element:		'span',
 *			styles:			{ 'font-family': '#(family)' },
 *			overrides:		[ { element: 'font', attributes: { 'face': null } } ]
 *     };
 *
 * @cfg {Object} [font_style=see example]
 * @member CKEDITOR.config
 */
CKEDITOR.config.font_style = {
	element: 'span',
	styles: { 'font-family': '#(family)' },
	overrides: [ {
		element: 'font', attributes: { 'face': null }
	} ]
};

/**
 * The list of font sizes to be displayed in the Font Size combo in the
 * toolbar. Entries are separated by semi-colons (`';'`).
 *
 * Any kind of "CSS-like" size can be used, like `'12px'`, `'2.3em'`, `'130%'`,
 * `'larger'` or `'x-small'`.
 *
 * A display name may be optionally defined by prefixing the entries with the
 * name and the slash character. For example, `'Bigger Font/14px'` will be
 * displayed as `'Bigger Font'` in the list, but will be output as `'14px'`.
 *
 *		config.fontSize_sizes = '16/16px;24/24px;48/48px;';
 *
 *		config.fontSize_sizes = '12px;2.3em;130%;larger;x-small';
 *
 *		config.fontSize_sizes = '12 Pixels/12px;Big/2.3em;30 Percent More/130%;Bigger/larger;Very Small/x-small';
 *
 * @cfg {String} [fontSize_sizes=see source]
 * @member CKEDITOR.config
 */
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';

/**
 * The text to be displayed in the Font Size combo if none of the available
 * values matches the current cursor position or text selection.
 *
 *		// If the default site font size is 12px, we may make it more explicit to the end user.
 *		config.fontSize_defaultLabel = '12px';
 *
 * @cfg {String} [fontSize_defaultLabel='']
 * @member CKEDITOR.config
 */
CKEDITOR.config.fontSize_defaultLabel = '';

/**
 * The style definition to be used to apply the font size in the text.
 *
 *		// This is actually the default value for it.
 *		config.fontSize_style = {
 *			element:		'span',
 *			styles:			{ 'font-size': '#(size)' },
 *			overrides:		[ { element: 'font', attributes: { 'size': null } } ]
 *		};
 *
 * @cfg {Object} [fontSize_style=see example]
 * @member CKEDITOR.config
 */
CKEDITOR.config.fontSize_style = {
	element: 'span',
	styles: { 'font-size': '#(size)' },
	overrides: [ {
		element: 'font', attributes: { 'size': null }
	} ]
};