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

Preserve Customize Selections in State on SiteGen Editor Page #464

Merged
merged 7 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,38 @@ const DesignColorsPanel = forwardRef(
ref
) => {
const resetToDefaultColors = () => {
if ( isEditingCustomColors ) {
setSelectedPalette( 0 );
setSelectedColor( colors[ 0 ] );
setShowCustomColors( false );
}
const slug = currentData.sitegen?.homepages?.active?.slug;
if ( slug ) {
const defaultDataToReset = defaultGlobalData[ slug ];

if ( defaultDataToReset ) {
const updatedData = {
...currentData,
sitegen: {
...currentData.sitegen,
homepages: {
...currentData.sitegen.homepages,
active: {
...currentData.sitegen.homepages.active,
color: {
...currentData.sitegen.homepages.active
.color,
palette: [
...defaultDataToReset.color.palette,
],
},
},
if ( ! slug ) {
return;
}
const defaultDataToReset = defaultGlobalData[ slug ];
if ( ! defaultDataToReset ) {
return;
}
const updatedData = {
...currentData,
sitegen: {
...currentData.sitegen,
homepages: {
...currentData.sitegen.homepages,
active: {
...currentData.sitegen.homepages.active,
color: {
...currentData.sitegen.homepages.active.color,
selectedPalette: null,
palette: [
...defaultDataToReset.color.palette,
],
},
},
};
setSelectedColor( null );
setSelectedPalette( null );
setCurrentOnboardingData( updatedData );
}
}
},
},
};

setSelectedColor( {} );
setSelectedPalette( null );
setShowCustomColors( false );
setCurrentOnboardingData( updatedData );
};

useImperativeHandle( ref, () => ( {
Expand Down Expand Up @@ -101,7 +99,7 @@ const DesignColorsPanel = forwardRef(

const [ colors ] = useState( palettes );
const [ customColors, setCustomColors ] = useState( null );
const [ selectedColor, setSelectedColor ] = useState( null );
const [ selectedColor, setSelectedColor ] = useState( {} );
const [ showCustomColors, setShowCustomColors ] = useState( false );
const [ isEditingCustomColors, setIsEditingCustomColors ] =
useState( false );
Expand All @@ -112,15 +110,19 @@ const DesignColorsPanel = forwardRef(
const [ showColorPicker, setShowColorPicker ] = useState( false );

useEffect( () => {
const activeColor = currentData.sitegen.homepages.active.color;

if ( ! customColors ) {
const storedCustomColors =
currentData.sitegen.homepages.active.color
.customColors;
if ( storedCustomColors ) {
setCustomColors( storedCustomColors );
} else {
const defaultCustomColors = palettes[ 0 ];
setCustomColors( defaultCustomColors );
const customColorsToSet = activeColor.customColors;
setCustomColors( customColorsToSet || palettes[ 0 ] );
}

if ( ! selectedPalette ) {
const selectedPaletteToSet = activeColor.selectedPalette;
setSelectedPalette( selectedPaletteToSet );
if ( selectedPaletteToSet === 'custom' ) {
setShowCustomColors( true );
setSelectedCustomColors( true );
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -184,36 +186,33 @@ const DesignColorsPanel = forwardRef(
return;
}

const activeColor = currentData.sitegen.homepages.active.color;

if ( selectedPalette === 'custom' ) {
currentData.sitegen.homepages.data[ slug ].color.customColors =
selectedColor;
currentData.sitegen.homepages.active.color.customColors =
selectedColor;
activeColor.customColors = selectedColor;
}

activeColor.selectedPalette = selectedPalette;

const colorPaletteIndex =
selectedPalette === 'custom' ? 0 : selectedPalette;
colorPalettes[ colorPaletteIndex ].primary = selectedColor.primary;
if ( colorPalettes[ colorPaletteIndex ].secondary ) {
colorPalettes[ colorPaletteIndex ].secondary =
selectedColor.secondary;
} else {
colorPalettes[ colorPaletteIndex ].base =
selectedColor.secondary;
}
const selectedPaletteColors = colorPalettes[ colorPaletteIndex ];

colorPalettes[ colorPaletteIndex ].tertiary =
selectedColor.tertiary;
selectedPaletteColors.primary = selectedColor.primary;
selectedPaletteColors.secondary = selectedColor.secondary;
selectedPaletteColors.base = selectedColor.secondary;
selectedPaletteColors.tertiary = selectedColor.tertiary;

currentData.sitegen.homepages.data[ slug ].color.palette =
convertColorSchema( colorPalettes[ colorPaletteIndex ] );
currentData.sitegen.homepages.active.color.palette =
convertColorSchema( colorPalettes[ colorPaletteIndex ] );
activeColor.palette = convertColorSchema( selectedPaletteColors );
currentData.sitegen.homepages.data[ slug ].color = activeColor;
setCurrentOnboardingData( currentData );
};

useEffect( () => {
if ( selectedColor !== null && selectedPalette !== null ) {
if (
Object.keys( selectedColor ).length !== 0 &&
selectedPalette !== null
) {
handleUpdatePreviewSettings();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -345,9 +344,9 @@ const DesignColorsPanel = forwardRef(
label={
idx === 0
? __(
'Default',
'wp-module-onboarding'
)
'Default',
'wp-module-onboarding'
)
: ''
}
selectedPalette={ selectedPalette }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,20 @@ const DesignFontsPanel = forwardRef(
const resetToDefaultFonts = () => {
setStylesOfCurrentData();
setSelectedGroup( null );
setShowCustomFonts( false );
setSelectedCustomFont( null );
const slug = currentData.sitegen?.homepages?.active?.slug;
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
if ( ! slug ) {
return;
}
currentData.sitegen.homepages.data[ slug ].selectedFontGroup = null;
currentData.sitegen.homepages.active.selectedFontGroup = null;
setCurrentOnboardingData( currentData );
};

const setStylesOfCurrentData = ( heading = '', body = '' ) => {
const slug = currentData.sitegen?.homepages?.active?.slug;
if ( ! slug ) {
return;
}
const styles = {
blocks: [
{
Expand All @@ -214,17 +222,14 @@ const DesignFontsPanel = forwardRef(
],
};

if ( slug ) {
currentData.sitegen.homepages.data[ slug ] = {
...currentData.sitegen.homepages.data[ slug ],
styles,
};
currentData.sitegen.homepages.active = {
...currentData.sitegen.homepages.active,
styles,
};
setCurrentOnboardingData( currentData );
}
const updatedData = {
...currentData.sitegen.homepages.data[ slug ],
styles,
};

currentData.sitegen.homepages.data[ slug ] = updatedData;
currentData.sitegen.homepages.active = updatedData;
setCurrentOnboardingData( currentData );
};

useImperativeHandle( ref, () => ( {
Expand Down Expand Up @@ -269,11 +274,23 @@ const DesignFontsPanel = forwardRef(
);

useEffect( () => {
if ( ! customFont.headings ) {
const storedCustomFonts =
currentData.sitegen.homepages.active.customFont;
if ( storedCustomFonts ) {
setCustomFont( storedCustomFonts );
const activeCustomFont =
currentData.sitegen.homepages.active.customFont;
const storedSelectedGroup =
currentData.sitegen.homepages.active.selectedFontGroup;

if ( ! customFont.headings && activeCustomFont ) {
setCustomFont( activeCustomFont );
setSelectedCustomFont( activeCustomFont );
}

if (
! selectedGroup &&
( storedSelectedGroup || storedSelectedGroup === 0 )
) {
setSelectedGroup( storedSelectedGroup );
if ( storedSelectedGroup === 'custom' ) {
setShowCustomFonts( true );
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand All @@ -282,20 +299,24 @@ const DesignFontsPanel = forwardRef(
const handleUpdatePreviewSettings = () => {
let headings;
let body;
const slug = currentData.sitegen?.homepages?.active?.slug;
if ( selectedGroup === 'custom' ) {
headings = `var(--wp--preset--font-family--${ customFont.headings })`;
body = `var(--wp--preset--font-family--${ customFont.body })`;
const slug = currentData.sitegen?.homepages?.active?.slug;
if ( slug ) {
currentData.sitegen.homepages.data[ slug ].customFont =
customFont;
currentData.sitegen.homepages.active.customFont =
customFont;
customFont;
}
} else {
headings = `var(--wp--preset--font-family--${ fontGroups[ selectedGroup ].headingsSlug })`;
body = `var(--wp--preset--font-family--${ fontGroups[ selectedGroup ].bodySlug })`;
}
currentData.sitegen.homepages.data[ slug ].selectedFontGroup =
selectedGroup;
currentData.sitegen.homepages.active.selectedFontGroup =
selectedGroup;
setStylesOfCurrentData( headings, body );
};

Expand Down
16 changes: 16 additions & 0 deletions src/OnboardingSPA/steps/SiteGen/Preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ const SiteGenPreview = () => {
const data = homepages[ slug ];
const newPreviewSettings = cloneDeep( globalStyles[ 0 ] );
newPreviewSettings.settings.color.palette = data.color.palette;
const body =
data.styles?.blocks[ 0 ]?.[ 'core/body' ].typography.fontFamily;
const headings =
data.styles?.blocks[ 0 ]?.[ 'core/heading' ].typography
.fontFamily;
if ( newPreviewSettings.styles.typography ) {
newPreviewSettings.styles.typography.fontFamily = body;
}
if (
newPreviewSettings.styles.blocks[ 'core/heading' ].typography
) {
newPreviewSettings.styles.blocks[
'core/heading'
].typography.fontFamily = headings;
}

let blockGrammar = '';
[ 'header', 'content', 'footer' ].forEach( ( part ) => {
if ( part in data ) {
Expand Down
Loading