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

Handle color palette as it appears in the editor #176

Merged
merged 2 commits into from
May 4, 2020
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
41 changes: 17 additions & 24 deletions includes/class-newspack-newsletters-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
* Newspack Newsletters Renderer Class.
*/
final class Newspack_Newsletters_Renderer {
/**
* The color palette to be used.
*
* @var Object
*/
protected static $color_palette = null;

/**
* Convert a list to HTML attributes.
*
Expand Down Expand Up @@ -67,41 +74,26 @@ private static function get_font_size( $block_attrs ) {
*/
private static function get_colors( $block_attrs ) {
$colors = array();
// Gutenberg's default color palette.
// https://github.com/WordPress/gutenberg/blob/359858da0675943d8a759a0a7c03e7b3846536f5/packages/block-editor/src/store/defaults.js#L30-L85 .
$colors_palette = array(
'pale-pink' => '#f78da7',
'vivid-red' => '#cf2e2e',
'luminous-vivid-orange' => '#ff6900',
'luminous-vivid-amber' => '#fcb900',
'light-green-cyan' => '#7bdcb5',
'vivid-green-cyan' => '#00d084',
'pale-cyan-blue' => '#8ed1fc',
'vivid-cyan-blue' => '#0693e3',
'very-light-gray' => '#eeeeee',
'cyan-bluish-gray' => '#abb8c3',
'very-dark-gray' => '#313131',
);

// For text.
if ( isset( $block_attrs['textColor'], $colors_palette[ $block_attrs['textColor'] ] ) ) {
$colors['color'] = $colors_palette[ $block_attrs['textColor'] ];
if ( isset( $block_attrs['textColor'], self::$color_palette[ $block_attrs['textColor'] ] ) ) {
$colors['color'] = self::$color_palette[ $block_attrs['textColor'] ];
}
// customTextColor is set inline, but it's passed here for consistency.
if ( isset( $block_attrs['customTextColor'] ) ) {
$colors['color'] = $block_attrs['customTextColor'];
}
if ( isset( $block_attrs['backgroundColor'], $colors_palette[ $block_attrs['backgroundColor'] ] ) ) {
$colors['background-color'] = $colors_palette[ $block_attrs['backgroundColor'] ];
if ( isset( $block_attrs['backgroundColor'], self::$color_palette[ $block_attrs['backgroundColor'] ] ) ) {
$colors['background-color'] = self::$color_palette[ $block_attrs['backgroundColor'] ];
}
// customBackgroundColor is set inline, but not on mjml wrapper element.
if ( isset( $block_attrs['customBackgroundColor'] ) ) {
$colors['background-color'] = $block_attrs['customBackgroundColor'];
}

// For separators.
if ( isset( $block_attrs['color'], $colors_palette[ $block_attrs['color'] ] ) ) {
$colors['border-color'] = $colors_palette[ $block_attrs['color'] ];
if ( isset( $block_attrs['color'], self::$color_palette[ $block_attrs['color'] ] ) ) {
$colors['border-color'] = self::$color_palette[ $block_attrs['color'] ];
}
if ( isset( $block_attrs['customColor'] ) ) {
$colors['border-color'] = $block_attrs['customColor'];
Expand Down Expand Up @@ -524,9 +516,10 @@ private static function render_mjml_component( $block, $is_in_column = false, $i
* @return string MJML markup.
*/
private static function render_mjml( $post ) {
$title = $post->post_title;
$blocks = parse_blocks( $post->post_content );
$body = '';
self::$color_palette = get_post_meta( $post->ID, 'color_palette', true );
$title = $post->post_title;
$blocks = parse_blocks( $post->post_content );
$body = '';
foreach ( $blocks as $block ) {
$block_content = self::render_mjml_component( $block );
if ( ! empty( $block_content ) ) {
Expand Down
24 changes: 24 additions & 0 deletions includes/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ public static function register_meta() {
'auth_callback' => '__return_true',
]
);
/**
* The default color palette lives in the editor frontend and is not
* retrievable on the backend. The workaround is to set it as post meta
* so that it's available to the email renderer.
*/
\register_meta(
'post',
'color_palette',
[
'object_subtype' => self::NEWSPACK_NEWSLETTERS_CPT,
'show_in_rest' => array(
'schema' => array(
'type' => 'object',
'properties' => array(),
'additionalProperties' => array(
'type' => 'string',
),
),
),
'type' => 'object',
'single' => true,
'auth_callback' => '__return_true',
]
);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/editor/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const Editor = compose( [
isCleanNewPost,
} = select( 'core/editor' );
const { getActiveGeneralSidebarName } = select( 'core/edit-post' );
const { getSettings } = select( 'core/block-editor' );
const meta = getEditedPostAttribute( 'meta' );

return {
isCleanNewPost: isCleanNewPost(),
postId: getCurrentPostId(),
Expand All @@ -35,6 +37,10 @@ const Editor = compose( [
: false,
activeSidebarName: getActiveGeneralSidebarName(),
isPublishingOrSavingPost: isSavingPost() || isPublishingPost(),
colorPalette: getSettings().colors.reduce(
( colors, { slug, color } ) => ( { ...colors, [ slug ]: color } ),
{}
),
};
} ),
withDispatch( dispatch => {
Expand Down Expand Up @@ -63,6 +69,10 @@ const Editor = compose( [
}
}, [ props.isPublishingOrSavingPost ]);

useEffect(() => {
props.editPost( { meta: { color_palette: props.colorPalette } } );
}, []);

// Lock or unlock post publishing.
useEffect(() => {
if ( props.isReady ) {
Expand Down