wpcom-customize-preview.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. console.log( hex );
  39. console.log( hsl );
  40. return 'hsl( ' + hsl.h * 360 + ',' + hsl.s * 100 + '%,' + ( hsl.l * 100 + amount ) + '%)';
  41. }
  42. ( function( $ ) {
  43. // Hide Front Page Title
  44. wp.customize( 'hide_front_page_title', function( value ) {
  45. value.bind( function( to ) {
  46. if ( true === to ) {
  47. $( 'body' ).addClass( 'hide-homepage-title' );
  48. } else {
  49. $( 'body' ).removeClass( 'hide-homepage-title' );
  50. }
  51. } );
  52. } );
  53. // Since the plugin handles customizer preview updates via the postMessage transport,
  54. // we need to manually override the "extra CSS" when a user selects a different color palette.
  55. wp.customize( 'colors_manager[colors]', function( value ) {
  56. value.bind( function( to ) {
  57. var background = to.bg;
  58. var foreground = to.txt;
  59. var primary = to.link;
  60. var secondary = to.fg1;
  61. var tertiary = to.fg2;
  62. var foregroundLight = changeColorLuminescence( foreground, 10 );
  63. var foregroundDark = changeColorLuminescence( foreground, -10 );
  64. var primaryHover = changeColorLuminescence( primary, 10 );
  65. var secondaryHover = changeColorLuminescence( secondary, 10 );
  66. console.log( primary );
  67. console.log( primaryHover );
  68. const extraCSS = ':root {' +
  69. '--global--color-background: ' + background + ';' +
  70. '--global--color-foreground: ' + foreground + ';' +
  71. '--global--color-foreground-light: ' + foregroundLight + ';' +
  72. '--global--color-foreground-dark: ' + foregroundDark + ';' +
  73. '--global--color-primary: ' + primary + ';' +
  74. '--global--color-primary-hover: ' + primaryHover + ';' +
  75. '--global--color-secondary: ' + secondary + ';' +
  76. '--global--color-secondary-hover: ' + secondaryHover + ';' +
  77. '--global--color-tertiary: ' + tertiary + ';' +
  78. '--global--color-border: ' + tertiary + ';' +
  79. '}';
  80. // Append an extra style element that overrides the previous extra CSS
  81. if ( $('#custom-colors-extra-css').length ) {
  82. $( '#custom-colors-extra-css' ).html( extraCSS );
  83. } else {
  84. $( 'head' ).append( '<style id="custom-colors-extra-css">' + extraCSS + '</style>' );
  85. }
  86. if ( typeof cssVars !== 'undefined' ) {
  87. cssVars( {} );
  88. }
  89. } );
  90. } );
  91. } )( jQuery );