|
|
@@ -4,39 +4,37 @@
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
- * @module font/fontfamily/fontfamilyediting
|
|
|
+ * @module font/fontfamily/utils
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
* Returns {@link module:font/fontfamily/fontfamilyediting~FontFamilyConfig#options} array with options normalized in the
|
|
|
- * {@link module:font/fontfamily/fontfamilyediting~FontFamilyOption} format, translated
|
|
|
- * `title` for each style.
|
|
|
+ * {@link module:font/fontfamily/fontfamilyediting~FontFamilyOption} format.
|
|
|
*
|
|
|
+ * @param {Array.<String|Object>} configuredOptions An array of options taken from configuration.
|
|
|
* @returns {Array.<module:font/fontfamily/fontfamilyediting~FontFamilyOption>}
|
|
|
*/
|
|
|
export function normalizeOptions( configuredOptions ) {
|
|
|
- const options = [];
|
|
|
- for ( const item of configuredOptions ) {
|
|
|
- const itemDefinition = getItemDefinition( item );
|
|
|
-
|
|
|
- // Set only valid definitions.
|
|
|
- if ( itemDefinition ) {
|
|
|
- options.push( itemDefinition );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return options;
|
|
|
+ // Convert options to objects.
|
|
|
+ return configuredOptions
|
|
|
+ .map( getOptionDefinition )
|
|
|
+ // Filter out undefined values that `getOptionDefinition` might return.
|
|
|
+ .filter( option => !!option );
|
|
|
}
|
|
|
|
|
|
-// Returns item definition from preset
|
|
|
-function getItemDefinition( item ) {
|
|
|
- // Probably it is full item definition so return it.
|
|
|
- if ( typeof item === 'object' ) {
|
|
|
- return item;
|
|
|
+// Returns an option definition either created from string shortcut.
|
|
|
+// If object is passed then this method will return it without alternating it. Returns undefined for item than cannot be parsed.
|
|
|
+//
|
|
|
+// @param {String|Object} option
|
|
|
+// @returns {undefined|module:font/fontfamily/fontfamilyediting~FontFamilyOption}
|
|
|
+function getOptionDefinition( option ) {
|
|
|
+ // Treat any object as full item definition provided by user in configuration.
|
|
|
+ if ( typeof option === 'object' ) {
|
|
|
+ return option;
|
|
|
}
|
|
|
|
|
|
// Handle 'default' string as a special case. It will be used to remove the fontFamily attribute.
|
|
|
- if ( item === 'default' ) {
|
|
|
+ if ( option === 'default' ) {
|
|
|
return {
|
|
|
title: 'Default',
|
|
|
model: undefined
|
|
|
@@ -44,11 +42,12 @@ function getItemDefinition( item ) {
|
|
|
}
|
|
|
|
|
|
// Ignore values that we cannot parse to a definition.
|
|
|
- if ( typeof item !== 'string' ) {
|
|
|
+ if ( typeof option !== 'string' ) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- return generateFontPreset( item );
|
|
|
+ // Return font family definition from font string.
|
|
|
+ return generateFontPreset( option );
|
|
|
}
|
|
|
|
|
|
// Creates a predefined preset for pixel size. It deconstructs font-family like string into full configuration option.
|
|
|
@@ -56,13 +55,14 @@ function getItemDefinition( item ) {
|
|
|
//
|
|
|
// @param {String} A font definition form configuration.
|
|
|
function generateFontPreset( fontDefinition ) {
|
|
|
- // Remove quotes from font names - will be normalized later.
|
|
|
+ // Remove quotes from font names. They will be normalized later.
|
|
|
const fontNames = fontDefinition.replace( /"|'/g, '' ).split( ',' );
|
|
|
|
|
|
// The first matched font name will be used as dropdown list item title and as model value
|
|
|
const firstFontName = fontNames[ 0 ];
|
|
|
|
|
|
- const cssFontNames = fontNames.map( normalizeFontName );
|
|
|
+ // CSS-compatible font names.
|
|
|
+ const cssFontNames = fontNames.map( normalizeFontNameForCSS );
|
|
|
|
|
|
// TODO: Maybe we can come with something better here?
|
|
|
// TODO: Also document this behavior in engine as it uses matcher~Pattern not ViewElementDefinition.
|
|
|
@@ -98,11 +98,11 @@ function generateFontPreset( fontDefinition ) {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
-// Normalizes font name for the view style attribute.
|
|
|
+// Normalizes font name for the style attribute. It adds braces (') if font name contains spaces.
|
|
|
//
|
|
|
// @param {String} fontName
|
|
|
// @returns {String}
|
|
|
-function normalizeFontName( fontName ) {
|
|
|
+function normalizeFontNameForCSS( fontName ) {
|
|
|
fontName = fontName.trim();
|
|
|
|
|
|
// Compound font names should be quoted.
|