utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module font/fontfamily/utils
  7. */
  8. /**
  9. * Normalizes the {@link module:font/fontfamily~FontFamilyConfig#options configuration options}
  10. * to the {@link module:font/fontfamily~FontFamilyOption} format.
  11. *
  12. * @param {Array.<String|Object>} configuredOptions An array of options taken from the configuration.
  13. * @returns {Array.<module:font/fontfamily~FontFamilyOption>}
  14. */
  15. export function normalizeOptions( configuredOptions ) {
  16. // Convert options to objects.
  17. return configuredOptions
  18. .map( getOptionDefinition )
  19. // Filter out undefined values that `getOptionDefinition` might return.
  20. .filter( option => !!option );
  21. }
  22. // Returns an option definition either created from string shortcut.
  23. // If object is passed then this method will return it without alternating it. Returns undefined for item than cannot be parsed.
  24. //
  25. // @param {String|Object} option
  26. // @returns {undefined|module:font/fontfamily~FontFamilyOption}
  27. function getOptionDefinition( option ) {
  28. // Treat any object as full item definition provided by user in configuration.
  29. if ( typeof option === 'object' ) {
  30. return option;
  31. }
  32. // Handle 'default' string as a special case. It will be used to remove the fontFamily attribute.
  33. if ( option === 'default' ) {
  34. return {
  35. title: 'Default',
  36. model: undefined
  37. };
  38. }
  39. // Ignore values that we cannot parse to a definition.
  40. if ( typeof option !== 'string' ) {
  41. return;
  42. }
  43. // Return font family definition from font string.
  44. return generateFontPreset( option );
  45. }
  46. // Creates a predefined preset for pixel size. It deconstructs font-family like string into full configuration option.
  47. // A font definition is passed as coma delimited set of font family names. Font names might be quoted.
  48. //
  49. // @param {String} A font definition form configuration.
  50. function generateFontPreset( fontDefinition ) {
  51. // Remove quotes from font names. They will be normalized later.
  52. const fontNames = fontDefinition.replace( /"|'/g, '' ).split( ',' );
  53. // The first matched font name will be used as dropdown list item title and as model value.
  54. const firstFontName = fontNames[ 0 ];
  55. // CSS-compatible font names.
  56. const cssFontNames = fontNames.map( normalizeFontNameForCSS ).join( ', ' );
  57. return {
  58. title: firstFontName,
  59. model: cssFontNames,
  60. view: {
  61. name: 'span',
  62. styles: {
  63. 'font-family': cssFontNames
  64. },
  65. priority: 7
  66. }
  67. };
  68. }
  69. // Normalizes font name for the style attribute. It adds braces (') if font name contains spaces.
  70. //
  71. // @param {String} fontName
  72. // @returns {String}
  73. function normalizeFontNameForCSS( fontName ) {
  74. fontName = fontName.trim();
  75. // Compound font names should be quoted.
  76. if ( fontName.indexOf( ' ' ) > 0 ) {
  77. fontName = `'${ fontName }'`;
  78. }
  79. return fontName;
  80. }