Browse Source

Merge pull request #541 from ckeditor/i/6106

Internal: Moved `normalizeColorOptions()` and `getLocalizedColorOptions()` from `ckeditor5-font` (see ckeditor/ckeditor5/issues/6106).
Maciej 5 years ago
parent
commit
b0d2adbf9e

+ 96 - 0
packages/ckeditor5-ui/src/colorgrid/utils.js

@@ -0,0 +1,96 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module ui/colorgrid/utils
+ */
+
+/**
+ * Returns color configuration options as defined in `editor.config.(fontColor|fontBackgroundColor).colors` or
+ * `editor.config.table.(tableProperties|tableCellProperties).(background|border).colors
+ * but processed to account for editor localization in the correct language.
+ *
+ * Note: The reason behind this method is that there is no way to use {@link module:utils/locale~Locale#t}
+ * when the user configuration is defined because the editor does not exist yet.
+ *
+ * @param {module:utils/locale~Locale} locale The {@link module:core/editor/editor~Editor#locale} instance.
+ * @param {Array.<module:ui/colorgrid/colorgrid~ColorDefinition>} options
+ * @returns {Array.<module:ui/colorgrid/colorgrid~ColorDefinition>}.
+ */
+export function getLocalizedColorOptions( locale, options ) {
+	const t = locale.t;
+	const localizedColorNames = {
+		Black: t( 'Black' ),
+		'Dim grey': t( 'Dim grey' ),
+		Grey: t( 'Grey' ),
+		'Light grey': t( 'Light grey' ),
+		White: t( 'White' ),
+		Red: t( 'Red' ),
+		Orange: t( 'Orange' ),
+		Yellow: t( 'Yellow' ),
+		'Light green': t( 'Light green' ),
+		Green: t( 'Green' ),
+		Aquamarine: t( 'Aquamarine' ),
+		Turquoise: t( 'Turquoise' ),
+		'Light blue': t( 'Light blue' ),
+		Blue: t( 'Blue' ),
+		Purple: t( 'Purple' )
+	};
+
+	return options.map( colorOption => {
+		const label = localizedColorNames[ colorOption.label ];
+
+		if ( label && label != colorOption.label ) {
+			colorOption.label = label;
+		}
+
+		return colorOption;
+	} );
+}
+
+/**
+ * Creates a unified color definition object from color configuration options.
+ * The object contains the information necessary to both render the UI and initialize the conversion.
+ *
+ * @param {module:ui/colorgrid/colorgrid~ColorDefinition} options
+ * @returns {Array.<module:ui/colorgrid/colorgrid~ColorDefinition>}
+ */
+export function normalizeColorOptions( options ) {
+	return options
+		.map( normalizeSingleColorDefinition )
+		.filter( option => !!option );
+}
+
+// Creates a normalized color definition from the user-defined configuration.
+//
+// @param {String|module:ui/colorgrid/colorgrid~ColorDefinition}
+// @returns {module:ui/colorgrid/colorgrid~ColorDefinition}
+export function normalizeSingleColorDefinition( color ) {
+	if ( typeof color === 'string' ) {
+		return {
+			model: color.replace( / /g, '' ),
+			label: color,
+			hasBorder: false,
+			view: {
+				name: 'span',
+				styles: {
+					color
+				}
+			}
+		};
+	} else {
+		return {
+			model: color.color.replace( / /g, '' ),
+			label: color.label || color.color,
+			hasBorder: color.hasBorder === undefined ? false : color.hasBorder,
+			view: {
+				name: 'span',
+				styles: {
+					color: `${ color.color }`
+				}
+			}
+		};
+	}
+}

+ 172 - 0
packages/ckeditor5-ui/tests/colorgrid/utils.js

@@ -0,0 +1,172 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+import {
+	normalizeColorOptions,
+	getLocalizedColorOptions
+} from '../../src/colorgrid/utils';
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+
+describe( 'utils', () => {
+	testUtils.createSinonSandbox();
+
+	describe( 'normalizeColorOptions()', () => {
+		it( 'should return normalized config object from string', () => {
+			const normalizedOption = normalizeColorOptions( [ 'black' ] );
+
+			expect( normalizedOption ).to.deep.equal( [
+				{
+					model: 'black',
+					label: 'black',
+					hasBorder: false,
+					view: {
+						name: 'span',
+						styles: {
+							color: 'black'
+						}
+					}
+				}
+			] );
+		} );
+
+		it( 'should return normalized config object from object( color )', () => {
+			const normalizedOption = normalizeColorOptions( [ { color: 'black' } ] );
+
+			expect( normalizedOption ).to.deep.equal( [
+				{
+					model: 'black',
+					label: 'black',
+					hasBorder: false,
+					view: {
+						name: 'span',
+						styles: {
+							color: 'black'
+						}
+					}
+				}
+			] );
+		} );
+
+		it( 'should return normalized config object from object( color, label )', () => {
+			const normalizedOption = normalizeColorOptions( [
+				{
+					color: 'black',
+					label: 'Black'
+				}
+			] );
+
+			expect( normalizedOption ).to.deep.equal( [
+				{
+					model: 'black',
+					label: 'Black',
+					hasBorder: false,
+					view: {
+						name: 'span',
+						styles: {
+							color: 'black'
+						}
+					}
+				}
+			] );
+		} );
+
+		it( 'should return normalized config object from object( color, label, hasBorder )', () => {
+			const normalizedOption = normalizeColorOptions( [
+				{
+					color: 'black',
+					label: 'Black',
+					hasBorder: true
+				}
+			] );
+
+			expect( normalizedOption ).to.deep.equal( [
+				{
+					model: 'black',
+					label: 'Black',
+					hasBorder: true,
+					view: {
+						name: 'span',
+						styles: {
+							color: 'black'
+						}
+					}
+				}
+			] );
+		} );
+
+		it( 'should return normalized config object from object( color, hasBorder )', () => {
+			const normalizedOption = normalizeColorOptions( [
+				{
+					color: 'black',
+					hasBorder: true
+				}
+			] );
+
+			expect( normalizedOption ).to.deep.equal( [
+				{
+					model: 'black',
+					label: 'black',
+					hasBorder: true,
+					view: {
+						name: 'span',
+						styles: {
+							color: 'black'
+						}
+					}
+				}
+			] );
+		} );
+	} );
+
+	describe( 'getLocalizedColorOptions()', () => {
+		const locale = {
+			t: string => 'Localized:' + string
+		};
+
+		it( 'should return localized color options', () => {
+			expect( getLocalizedColorOptions( locale, [
+				{
+					color: 'red',
+					label: 'Red'
+				},
+				{
+					color: 'blue',
+					label: 'Blue'
+				}
+			] ) ).to.deep.equal( [
+				{
+					color: 'red',
+					label: 'Localized:Red'
+				},
+				{
+					color: 'blue',
+					label: 'Localized:Blue'
+				}
+			] );
+		} );
+
+		it( 'should omit unknown color options', () => {
+			expect( getLocalizedColorOptions( locale, [
+				{
+					color: 'red',
+					label: 'Red'
+				},
+				{
+					color: 'unknown',
+					label: 'Unknown'
+				}
+			] ) ).to.deep.equal( [
+				{
+					color: 'red',
+					label: 'Localized:Red'
+				},
+				{
+					color: 'unknown',
+					label: 'Unknown'
+				}
+			] );
+		} );
+	} );
+} );