Преглед на файлове

fontSize.supportAllValues will not work with named presets.

Kamil Piechaczek преди 5 години
родител
ревизия
fc7f17c667

+ 5 - 2
packages/ckeditor5-font/docs/features/font.md

@@ -173,13 +173,12 @@ ClassicEditor
 
 By default, all `font-size` values that are not specified in the `config.fontSize.options` are stripped. You can enable support for all font sizes by using the {@link module:font/fontfamily~FontFamilyConfig#supportAllValues `config.fontSize.supportAllValues`} option.
 
-
 ```js
 ClassicEditor
 	.create( document.querySelector( '#editor' ), {
 		fontSize: {
 			options: [
-				// ...
+				// Numerical values.
 			],
 			supportAllValues: true
 		},
@@ -189,6 +188,10 @@ ClassicEditor
 	.catch( ... );
 ```
 
+<info-box info>
+	When this options is enabled, available options for the {@link module:font/fontsize~FontSize} plugin should be numerical values.
+</info-box>
+
 ## Configuring the font color and font background color features
 
 Both font color and font background color features are configurable and share the same configuration format.

+ 4 - 1
packages/ckeditor5-font/src/fontsize.js

@@ -139,9 +139,12 @@ export default class FontSize extends Plugin {
  * You can preserve pasted font size values by switching the option:
  *
  *		const fontSizeConfig = {
- *			supportAllValues: true
+ *			options: [ 9, 10, 11, 12, 13, 14, 15 ],
+ *			supportAllValues: true,
  *		};
  *
+ * You need to also define numerical options for the plugin.
+ *
  * Now, the font sizes, not specified in the editor's configuration, won't be removed when pasting the content.
  *
  * @member {Boolean} module:font/fontsize~FontSizeConfig#supportAllValues

+ 21 - 2
packages/ckeditor5-font/src/fontsize/fontsizeediting.js

@@ -12,6 +12,7 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import FontSizeCommand from './fontsizecommand';
 import { normalizeOptions } from './utils';
 import { buildDefinition, FONT_SIZE } from '../utils';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 /**
  * The font size editing feature.
@@ -75,7 +76,7 @@ export default class FontSizeEditing extends Plugin {
 
 		// Set-up the two-way conversion.
 		if ( supportAllValues ) {
-			this._prepareAnyValueConverters();
+			this._prepareAnyValueConverters( definition );
 		} else {
 			editor.conversion.attributeToElement( definition );
 		}
@@ -88,11 +89,29 @@ export default class FontSizeEditing extends Plugin {
 	 * Those converters enable keeping any value found as `style="font-size: *"` as a value of an attribute on a text even
 	 * if it isn't defined in the plugin configuration.
 	 *
+	 * @param {Object} definition {@link module:engine/conversion/conversion~ConverterDefinition Converter definition} out of input data.
 	 * @private
 	 */
-	_prepareAnyValueConverters() {
+	_prepareAnyValueConverters( definition ) {
 		const editor = this.editor;
 
+		// If `fontSize.supportAllValues=true`, we do not allow to use named presets in the plugin's configuration.
+		const presets = definition.model.values.filter( value => !String( value ).match( /[\d.]+[\w%]+/ ) );
+
+		if ( presets.length ) {
+			/**
+			 * If {@link module:font/fontsize~FontSizeConfig#supportAllValues} is `true`, you need to use numerical values as
+			 * font size options.
+			 *
+			 * See valid examples described in the {@link module:font/fontsize~FontSizeConfig#options plugin configuration}.
+			 *
+			 * @error font-size-named-presets
+			 */
+			const message = 'font-size-named-presets: ' +
+				'If set `fontSize.supportAllValues` on `true`, you cannot use named presets as plugin\'s configuration.';
+			throw new CKEditorError( message, null, { presets } );
+		}
+
 		editor.conversion.for( 'downcast' ).attributeToElement( {
 			model: FONT_SIZE,
 			view: ( attributeValue, writer ) => {

+ 32 - 1
packages/ckeditor5-font/tests/fontsize/fontsizeediting.js

@@ -9,6 +9,7 @@ import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 describe( 'FontSizeEditing', () => {
 	let editor, doc;
@@ -68,7 +69,17 @@ describe( 'FontSizeEditing', () => {
 					.create( {
 						plugins: [ FontSizeEditing, Paragraph ],
 						fontSize: {
-							supportAllValues: true
+							supportAllValues: true,
+							options: [
+								'6.25%',
+								'8em',
+								'10px',
+								12,
+								{
+									model: 14,
+									title: '14px'
+								}
+							]
 						}
 					} )
 					.then( newEditor => {
@@ -101,6 +112,26 @@ describe( 'FontSizeEditing', () => {
 					expect( editor.getData() ).to.equal( '<p>f<span style="font-size:18px;">o</span>o</p>' );
 				} );
 			} );
+
+			it( 'should throw an error if used with default configuration of the plugin', () => {
+				return VirtualTestEditor
+					.create( {
+						plugins: [ FontSizeEditing ],
+						fontSize: {
+							supportAllValues: true
+						}
+					} )
+					.then(
+						() => {
+							throw new Error( 'Supposed to be rejected' );
+						},
+						error => {
+							assertCKEditorError( error, /font-size-named-presets/, null, {
+								presets: [ 'tiny', 'small', 'big', 'huge' ]
+							} );
+						}
+					);
+			} );
 		} );
 	} );
 

+ 2 - 0
packages/ckeditor5-font/tests/integration.js

@@ -63,6 +63,7 @@ describe( 'Integration test Font', () => {
 						supportAllValues: true
 					},
 					fontSize: {
+						options: [ 10, 12, 14 ],
 						supportAllValues: true
 					}
 				} )
@@ -125,6 +126,7 @@ describe( 'Integration test Font', () => {
 						supportAllValues: true
 					},
 					fontSize: {
+						options: [ 10, 12, 14 ],
 						supportAllValues: true
 					}
 				} )