customize-preview-wpcom.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * File wpcom-customize-preview.js.
  3. *
  4. * Instantly live-update customizer settings in the preview for improved user experience,
  5. * targeting the updates needed to hide the page title on the homepage on WordPress.com.
  6. *
  7. * This file needs to avoid ESNext syntax to work in older browsers
  8. */
  9. // From https://gist.github.com/xenozauros/f6e185c8de2a04cdfecf
  10. function hexToHSL( hex ) {
  11. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( hex );
  12. r = parseInt( result[1], 16 );
  13. g = parseInt( result[2], 16 );
  14. b = parseInt( result[3], 16 );
  15. r /= 255, g /= 255, b /= 255;
  16. var max = Math.max( r, g, b ), min = Math.min( r, g, b );
  17. var h, s, l = ( max + min ) / 2;
  18. if( max == min ){
  19. h = s = 0; // achromatic
  20. } else {
  21. var d = max - min;
  22. s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  23. switch( max ){
  24. case r: h = ( g - b ) / d + ( g < b ? 6 : 0 ); break;
  25. case g: h = ( b - r ) / d + 2; break;
  26. case b: h = ( r - g ) / d + 4; break;
  27. }
  28. h /= 6;
  29. }
  30. var HSL = new Object();
  31. HSL['h'] = h;
  32. HSL['s'] = s;
  33. HSL['l'] = l;
  34. return HSL;
  35. }
  36. function changeColorLuminescence( hex, amount ) {
  37. var hsl = hexToHSL( hex );
  38. return 'hsl( ' + hsl.h * 360 + ',' + hsl.s * 100 + '%,' + ( hsl.l * 100 + amount ) + '%)';
  39. }
  40. ( function( $ ) {
  41. // Hide Front Page Title
  42. wp.customize( 'hide_front_page_title', function( value ) {
  43. value.bind( function( to ) {
  44. if ( true === to ) {
  45. $( 'body' ).addClass( 'hide-homepage-title' );
  46. } else {
  47. $( 'body' ).removeClass( 'hide-homepage-title' );
  48. }
  49. } );
  50. } );
  51. // Since the plugin handles customizer preview updates via the postMessage transport,
  52. // we need to manually override the "extra CSS" when a user selects a different color palette.
  53. wp.customize( 'colors_manager[colors]', function( value ) {
  54. value.bind( function( to ) {
  55. var background = to.bg;
  56. var foreground = to.txt;
  57. var primary = to.link;
  58. var secondary = to.fg1;
  59. var border = to.fg2;
  60. var backgroundLowContrast = changeColorLuminescence( background, -10 );
  61. var backgroundHighContrast = changeColorLuminescence( background, 10 );
  62. var foregroundLowContrast = changeColorLuminescence( foreground, 10 );
  63. var foregroundHighContrast = changeColorLuminescence( foreground, -10 );
  64. var borderLowContrast = changeColorLuminescence( border, 10 );
  65. var borderHighContrast = changeColorLuminescence( border, -10 );
  66. var primaryHover = changeColorLuminescence( primary, 10 );
  67. var secondaryHover = changeColorLuminescence( secondary, 10 );
  68. const extraCSS = ':root {' +
  69. '--wp--preset--color--background: ' + background + ';' +
  70. '--wp--preset--color--background-low-contrast: ' + backgroundLowContrast + ';' +
  71. '--wp--preset--color--background-high-contrast: ' + backgroundHighContrast + ';' +
  72. '--wp--preset--color--foreground: ' + foreground + ';' +
  73. '--wp--preset--color--foreground-low-contrast: ' + foregroundLowContrast + ';' +
  74. '--wp--preset--color--foreground-high-contrast: ' + foregroundHighContrast + ';' +
  75. '--wp--preset--color--primary: ' + primary + ';' +
  76. '--wp--preset--color--primary-hover: ' + primaryHover + ';' +
  77. '--wp--preset--color--secondary: ' + secondary + ';' +
  78. '--wp--preset--color--secondary-hover: ' + secondaryHover + ';' +
  79. '--wp--preset--color--border: ' + border + ';' +
  80. '--wp--preset--color--border-low-contrast: ' + borderLowContrast + ';' +
  81. '--wp--preset--color--border-high-contrast: ' + borderHighContrast + ';' +
  82. '}';
  83. // Append an extra style element that overrides the previous extra CSS
  84. if ( $('#custom-colors-extra-css').length ) {
  85. $( '#custom-colors-extra-css' ).html( extraCSS );
  86. } else {
  87. $( 'head' ).append( '<style id="custom-colors-extra-css">' + extraCSS + '</style>' );
  88. }
  89. if ( typeof cssVars !== 'undefined' ) {
  90. cssVars( {} );
  91. }
  92. } );
  93. } );
  94. } )( jQuery );