Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRESS2 331 Maintain State for colors and typography steps #102

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/Data/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class Options {
'site_icon' => 'site_icon',
'show_on_front' => 'show_on_front',
'page_on_front' => 'page_on_front',
'custom_theme_styles' => 'custom_theme_styles',
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
);

protected static $initialization_options = array(
Expand Down
40 changes: 39 additions & 1 deletion includes/RestApi/Themes/ThemeVariationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace NewfoldLabs\WP\Module\Onboarding\RestApi\Themes;

use NewfoldLabs\WP\Module\Onboarding\Permissions;
use NewfoldLabs\WP\Module\Onboarding\Data\Options;

/**
* Class ThemeVariationsController
Expand Down Expand Up @@ -43,16 +44,32 @@ public function register_routes() {
'callback' => array( $this, 'get_theme_variations' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
),
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array($this, 'set_theme_variation'),
'permission_callback' => array(Permissions::class, 'rest_is_authorized_admin'),
),
)
);
}

/**
* Retrieves the active themes variations.
*
* @return \WP_REST_Response|\WP_Error
* @return \array|\WP_Error
*/
public function get_theme_variations( \WP_REST_Request $request ) {

$default = $request->get_param('defaultValue');
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved

// If there exists an old Custom Theme then return that
if( 'false' === $default && false !== \get_option(Options::get_option_name('custom_theme_styles')) )
{
return array(
\get_option(Options::get_option_name('custom_theme_styles'))
);
}

$active_variation = \WP_Theme_JSON_Resolver::get_merged_data( 'theme' )->get_raw_data();
$active_variation_global_style = array(
'title' => 'Default',
Expand All @@ -67,4 +84,25 @@ public function get_theme_variations( \WP_REST_Request $request ) {
);
}

/**
* Saves the custom active theme variations.
*
* @return \WP_REST_Response|\WP_Error
*/
public function set_theme_variation(\WP_REST_Request $request)
{
// The theme data with the new Colors and Fonts
$theme_data = json_decode($request->get_body(), true);
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved

if($theme_data){
// Save the new Theme style into the db
\update_option(Options::get_option_name('custom_theme_styles'), $theme_data);
}

return new \WP_REST_Response(
$theme_data,
200
);
}

}
46 changes: 17 additions & 29 deletions src/OnboardingSPA/components/Drawer/DrawerPanel/DesignColors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { useState, useEffect } from '@wordpress/element';
import { Popover, ColorPicker } from '@wordpress/components';

import { store as nfdOnboardingStore } from '../../../store';
import { getGlobalStyles, getThemeColors } from '../../../utils/api/themes';
import { getGlobalStyles, getThemeColors, setGlobalStyles } from '../../../utils/api/themes';
import { useGlobalStylesOutput } from '../../../utils/global-styles/use-global-styles-output';
import GlobalStyleParent from '../../GlobalStyleParent';

const DesignColors = () => {

const [isLoaded, setIsLoaded] = useState(false);
const [globalStyles, setGlobalStyles] = useState();
const [selectedColors, setSelectedColors] = useState();
const [showColorPicker, setShowColorPicker] = useState(false);
const [isAccordionClosed, setIsAccordionClosed] = useState(true);
Expand Down Expand Up @@ -69,7 +69,7 @@ const DesignColors = () => {
}
}

async function saveThemeColorPalette(colorStyle, colorPalettesTemp = colorPalettes, selectedColorsLocalTemp = selectedColors, globalStylesTemp = globalStyles) {
async function saveThemeColorPalette(colorStyle, colorPalettesTemp = colorPalettes, selectedColorsLocalTemp = selectedColors, globalStylesTemp = storedPreviewSettings) {
const isCustomStyle = colorStyle === 'custom';
let selectedGlobalStyle = globalStylesTemp;
let selectedThemeColorPalette = selectedGlobalStyle?.settings?.color?.palette?.theme;
Expand Down Expand Up @@ -104,7 +104,6 @@ const DesignColors = () => {
}

selectedGlobalStyle.settings.color.palette.theme = selectedThemeColorPalette;
setGlobalStyles(selectedGlobalStyle);
updatePreviewSettings(
useGlobalStylesOutput(selectedGlobalStyle, storedPreviewSettings)
);
Expand All @@ -114,7 +113,7 @@ const DesignColors = () => {
}

async function saveCustomColors() {
let selectedGlobalStyle = globalStyles;
let selectedGlobalStyle = storedPreviewSettings;
let selectedThemeColorPalette = selectedGlobalStyle?.settings?.color?.palette?.theme;

if (selectedThemeColorPalette) {
Expand All @@ -140,7 +139,6 @@ const DesignColors = () => {
}

selectedGlobalStyle.settings.color.palette.theme = selectedThemeColorPalette;
setGlobalStyles(selectedGlobalStyle);
updatePreviewSettings(
useGlobalStylesOutput(selectedGlobalStyle, storedPreviewSettings)
);
Expand All @@ -149,18 +147,7 @@ const DesignColors = () => {

const getColorStylesAndPatterns = async () => {
const colorPalettes = await getThemeColors();
const globalStyles = await getGlobalStyles();
setColorPalettes(colorPalettes?.body);
let selectedGlobalStyle;
if (currentData?.data?.theme?.variation) {
selectedGlobalStyle = globalStyles.body.filter(
(globalStyle) =>
globalStyle.title === currentData.data.theme.variation
)[0];
} else {
selectedGlobalStyle = globalStyles.body[0];
}
setGlobalStyles(selectedGlobalStyle);
let selectedColors;
let selectedColorsLocal;
if (!currentData?.data?.palette?.slug === '') {
Expand All @@ -178,7 +165,7 @@ const DesignColors = () => {
}
}
setSelectedColors(selectedColors);
saveThemeColorPalette(currentData?.data?.palette['slug'], colorPalettes?.body, selectedColorsLocal, selectedGlobalStyle);
saveThemeColorPalette(currentData?.data?.palette['slug'], colorPalettes?.body, selectedColorsLocal, storedPreviewSettings);
setIsLoaded(true);

};
Expand Down Expand Up @@ -229,7 +216,6 @@ const DesignColors = () => {
} else {
selectedGlobalStyle = globalStyles.body[0];
}
setGlobalStyles(selectedGlobalStyle);
updatePreviewSettings(
useGlobalStylesOutput(selectedGlobalStyle, storedPreviewSettings)
);
Expand Down Expand Up @@ -331,16 +317,18 @@ const DesignColors = () => {
}

return (
<div className='theme-colors--drawer'>
<h2>{__('Color Palettes', 'wp-module-onboarding')}</h2>
{/* {selectedColors?.slug &&
<div className='theme-colors--drawer--reset' onClick={resetColors}>
<div>Reset Button</div>
</div>
} */}
{ colorPalettes && buildPalettes() }
{ colorPalettes && buildCustomPalette() }
</div>
<GlobalStyleParent>
<div className='theme-colors--drawer'>
<h2>{__('Color Palettes', 'wp-module-onboarding')}</h2>
{/* {selectedColors?.slug &&
<div className='theme-colors--drawer--reset' onClick={resetColors}>
<div>Reset Button</div>
</div>
} */}
{ colorPalettes && buildPalettes() }
{ colorPalettes && buildCustomPalette() }
</div>
</GlobalStyleParent>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const DesignThemeStylesPreview = () => {
if ( patternResponse?.error ) {
return updateThemeStatus( THEME_STATUS_NOT_ACTIVE );
}
const globalStylesResponse = await getGlobalStyles();
const globalStylesResponse = await getGlobalStyles(true);
if ( globalStylesResponse?.error ) {
return updateThemeStatus( THEME_STATUS_NOT_ACTIVE );
}
Expand Down
58 changes: 20 additions & 38 deletions src/OnboardingSPA/components/Drawer/DrawerPanel/DesignTypography.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useEffect, useRef } from '@wordpress/element';

import GlobalStyleParent from '../../GlobalStyleParent';
import { store as nfdOnboardingStore } from '../../../store';
import { getGlobalStyles, getThemeFonts } from '../../../utils/api/themes';
import { useGlobalStylesOutput } from '../../../utils/global-styles/use-global-styles-output';
Expand All @@ -11,7 +12,6 @@ const DesignTypography = () => {
const [ rerender, doRerender ] = useState( 0 );
const [ isLoaded, setIsLoaded ] = useState( false );
const [ selectedFont, setSelectedFont ] = useState();
const [ globalStyles, setGlobalStyles ] = useState();
const [ fontPalettes, setFontPalettes ] = useState();
const [ isAccordionClosed, setIsAccordionClosed ] = useState( true );

Expand All @@ -29,35 +29,16 @@ const DesignTypography = () => {

const getFontStylesAndPatterns = async () => {
const fontPalettes = await getThemeFonts();
const globalStyles = await getGlobalStyles();
setFontPalettes( fontPalettes?.body );

let selectedGlobalStyle;
if ( currentData?.data?.theme?.variation ) {
selectedGlobalStyle = globalStyles.body.filter(
( globalStyle ) =>
globalStyle.title === currentData.data.theme.variation
)[ 0 ];
} else {
selectedGlobalStyle = globalStyles.body[ 0 ];
}
setGlobalStyles( selectedGlobalStyle );

if ( currentData?.data?.typography?.slug !== '' ) {
handleClick(
currentData?.data?.typography?.slug,
selectedGlobalStyle,
storedPreviewSettings,
fontPalettes?.body
);
} else {
updatePreviewSettings(
useGlobalStylesOutput(
selectedGlobalStyle,
storedPreviewSettings
)
);
}
const stylesCustom = selectedGlobalStyle?.settings?.styles[ 0 ]?.css;
const stylesCustom = storedPreviewSettings?.settings?.styles[ 0 ]?.css;
if ( stylesCustom ) {
// Loads in all CSS variables related to fontFamily
const regex = /--wp--preset--font-family.*;/;
Expand All @@ -75,20 +56,20 @@ const DesignTypography = () => {

const handleClick = async (
fontStyle,
selectedGlobalStyle = globalStyles,
selectedGlobalStyle = storedPreviewSettings,
fontPalettesCopy = fontPalettes
) => {
setSelectedFont( fontStyle );

// Changes the Global Styles to Recompute css properties
const globalStylesCopy = selectedGlobalStyle;
globalStylesCopy.styles.typography.fontFamily =
fontPalettesCopy[ fontStyle ]?.styles?.typography?.fontFamily;
globalStylesCopy.styles.blocks[ 'core/heading' ].typography.fontFamily =
fontPalettesCopy[ fontStyle ]?.styles.blocks[
'core/heading'
].typography.fontFamily;
setGlobalStyles( globalStylesCopy );

// globalStylesCopy.styles.typography.fontFamily =
// fontPalettesCopy[ fontStyle ]?.styles?.typography?.fontFamily;
// globalStylesCopy.styles.blocks[ 'core/heading' ].typography.fontFamily =
// fontPalettesCopy[ fontStyle ]?.styles.blocks[
// 'core/heading'
// ].typography.fontFamily;
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved

// Saves the data to the Store
currentData.data.typography.slug = fontStyle;
Expand Down Expand Up @@ -136,7 +117,6 @@ const DesignTypography = () => {
} else {
selectedGlobalStyle = globalStyles.body[ 0 ];
}
setGlobalStyles( selectedGlobalStyle );
updatePreviewSettings(
useGlobalStylesOutput( selectedGlobalStyle, storedPreviewSettings )
);
Expand Down Expand Up @@ -222,17 +202,19 @@ const DesignTypography = () => {
}

return (
<div ref={ drawerFontOptions } className="theme-fonts--drawer">
<h2>{ __( 'Font Palettes', 'wp-module-onboarding' ) }</h2>
{ /* { selectedFont &&
<GlobalStyleParent>
<div ref={drawerFontOptions} className="theme-fonts--drawer">
<h2>{__('Font Palettes', 'wp-module-onboarding')}</h2>
{ /* { selectedFont &&
<div className='theme-fonts--drawer--reset' onClick={resetFonts}>
<div>Reset Button</div>
</div>
} */ }
{ fontPalettes && buildPalettes() }
{ fontPalettes && buildCustomPalette() }
<div className="custom-font-palette--hidden">{ rerender }</div>
</div>
{fontPalettes && buildPalettes()}
{fontPalettes && buildCustomPalette()}
<div className="custom-font-palette--hidden">{rerender}</div>
</div>
</GlobalStyleParent>
);
};
export default DesignTypography;
74 changes: 74 additions & 0 deletions src/OnboardingSPA/components/GlobalStyleParent/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';

import { store as nfdOnboardingStore } from '../../store';
import { getGlobalStyles, setGlobalStyles } from '../../utils/api/themes';
import { useGlobalStylesOutput } from '../../utils/global-styles/use-global-styles-output';

/**
* Global Style Parent Component
* The Fetching of Global Style Object from either store or API is
* common to a lot many places and this component does the trick import { useState, useEffect } from '@wordpress/element';for us.
*
* @returns Global Style Parent
*/

const GlobalStyleParent = ({ children }) => {
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved

const [isLoaded, setIsLoaded] = useState(false);

const {
currentData,
storedPreviewSettings,
} = useSelect((select) => {
return {
currentData:
select(nfdOnboardingStore).getCurrentOnboardingData(),
storedPreviewSettings:
select(nfdOnboardingStore).getPreviewSettings(),
};
}, []);

const { updateThemeStatus, updatePreviewSettings } =
useDispatch(nfdOnboardingStore);

const getStylesAndPatterns = async () => {
const globalStyles = await getGlobalStyles();
if (globalStyles?.error) {
return updateThemeStatus(THEME_STATUS_NOT_ACTIVE);
}
let selectedGlobalStyle;
if (storedPreviewSettings?.title)
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
selectedGlobalStyle = storedPreviewSettings;
else if (currentData.data.theme.variation) {
selectedGlobalStyle = globalStyles.body.filter(
(globalStyle) =>
globalStyle.title === currentData.data.theme.variation
)[0];
} else {
selectedGlobalStyle = globalStyles.body[0];
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
}

if(selectedGlobalStyle)
setGlobalStyles({
...selectedGlobalStyle,
'title': currentData.data.theme.variation,
'version': 2
});

updatePreviewSettings(
useGlobalStylesOutput(selectedGlobalStyle, storedPreviewSettings)
);
setIsLoaded(true);
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
};

useEffect(() => {
if (!isLoaded) getStylesAndPatterns();
}, [isLoaded]);

return (
children
);
};

export default GlobalStyleParent;
Loading