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

APPLE-173: Allow Default Tag Styles to be Editable via Customize JSON #1109

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
5 changes: 5 additions & 0 deletions assets/themes/classic.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"caption_line_height": 19,
"caption_size": 14,
"caption_tracking": -1,
"cite_color": "#333333",
"cite_font": "Helvetica-Bold",
"cite_line_height": 19,
"cite_size": 14,
"cite_tracking": -1,
"dropcap_background_color": "",
"dropcap_color": "#000000",
"dropcap_font": "Baskerville",
Expand Down
5 changes: 5 additions & 0 deletions assets/themes/colorful.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"caption_line_height": 22,
"caption_size": 14,
"caption_tracking": -3,
"cite_color": "#3045ca",
"cite_font": "Georgia",
"cite_line_height": 22,
"cite_size": 14,
"cite_tracking": -3,
"date_color": "#000000",
"date_font": "Optima-Regular",
"date_format": "#F j, Y#",
Expand Down
5 changes: 5 additions & 0 deletions assets/themes/dark.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"caption_line_height": 19,
"caption_size": 14,
"caption_tracking": -1,
"cite_color": "#999999",
"cite_font": "Helvetica-Light",
"cite_line_height": 19,
"cite_size": 14,
"cite_tracking": -1,
"date_color": "#e2e2e2",
"date_font": "Helvetica-Bold",
"date_format": "#F j \\a\\t g:i A#",
Expand Down
5 changes: 5 additions & 0 deletions assets/themes/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"caption_line_height": 24,
"caption_size": 16,
"caption_tracking": 0,
"cite_color": "#4f4f4f",
"cite_font": "AvenirNext-Italic",
"cite_line_height": 24,
"cite_size": 16,
"cite_tracking": 0,
"date_color": "#7c7c7c",
"date_font": "AvenirNext-Medium",
"date_format": "#M j, Y | g:i A#",
Expand Down
5 changes: 5 additions & 0 deletions assets/themes/modern.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"caption_line_height": 17,
"caption_size": 14,
"caption_tracking": 0,
"cite_color": "#000000",
"cite_font": "AvenirNext-DemiBold",
"cite_line_height": 17,
"cite_size": 14,
"cite_tracking": 0,
"date_color": "#000000",
"date_font": "AvenirNext-Regular",
"date_format": "#F j, Y, g:ia#",
Expand Down
5 changes: 5 additions & 0 deletions assets/themes/pastel.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"caption_line_height": 19,
"caption_size": 14,
"caption_tracking": -1,
"cite_color": "#333333",
"cite_font": "AvenirNext-Regular",
"cite_line_height": 19,
"cite_size": 14,
"cite_tracking": -1,
"date_color": "#000000",
"date_font": "AvenirNext-Bold",
"date_format": "#F j \\a\\t g:i A#",
Expand Down
75 changes: 13 additions & 62 deletions includes/apple-exporter/builders/class-text-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

namespace Apple_Exporter\Builders;

use Apple_Exporter\Component_Spec;
use Apple_Exporter\Components\Body;
use Apple_Exporter\Exporter_Content;
use Apple_Exporter\Exporter_Content_Settings;
use Apple_Exporter\Theme;

/**
* A class which is used to set top-level text styles.
Expand Down Expand Up @@ -89,69 +92,17 @@ public function register_style( $name, $values ) {
* @access private
*/
private function add_html_styles() {

// Get information about the currently loaded theme.
$theme = \Apple_Exporter\Theme::get_used();

$conditional = [];
if ( ! empty( $theme->get_value( 'monospaced_color_dark' ) ) ) {
$conditional = [
'conditional' => [
'textColor' => $theme->get_value( 'monospaced_color_dark' ),
'conditions' => [
'minSpecVersion' => '1.14',
'preferredColorScheme' => 'dark',
],
],
];
// Try to get the text styles spec from the body component.
$body = new Body();
$specs = $body->get_specs();
if ( empty( $specs['default-text-styles'] ) || ! $specs['default-text-styles'] instanceof Component_Spec ) {
return;
}

// Add style for <code> tags.
$this->register_style(
'default-tag-code',
array_merge(
[
'fontName' => $theme->get_value( 'monospaced_font' ),
'fontSize' => intval( $theme->get_value( 'monospaced_size' ) ),
'tracking' => intval( $theme->get_value( 'monospaced_tracking' ) ) / 100,
'lineHeight' => intval( $theme->get_value( 'monospaced_line_height' ) ),
'textColor' => $theme->get_value( 'monospaced_color' ),
],
$conditional
)
);

// Add style for <pre> tags.
$this->register_style(
'default-tag-pre',
array_merge(
[
'textAlignment' => 'left',
'fontName' => $theme->get_value( 'monospaced_font' ),
'fontSize' => intval( $theme->get_value( 'monospaced_size' ) ),
'tracking' => intval( $theme->get_value( 'monospaced_tracking' ) ) / 100,
'lineHeight' => intval( $theme->get_value( 'monospaced_line_height' ) ),
'textColor' => $theme->get_value( 'monospaced_color' ),
'paragraphSpacingBefore' => 18,
'paragraphSpacingAfter' => 18,
],
$conditional
)
);

// Add style for <samp> tags.
$this->register_style(
'default-tag-samp',
array_merge(
[
'fontName' => $theme->get_value( 'monospaced_font' ),
'fontSize' => intval( $theme->get_value( 'monospaced_size' ) ),
'tracking' => intval( $theme->get_value( 'monospaced_tracking' ) ) / 100,
'lineHeight' => intval( $theme->get_value( 'monospaced_line_height' ) ),
'textColor' => $theme->get_value( 'monospaced_color' ),
],
$conditional
)
);
// Get the computed specs for the text styles including any overrides from the theme via Customize JSON and apply them.
$computed_styles = $specs['default-text-styles']->substitute_values( [] );
foreach ( $computed_styles as $name => $values ) {
$this->register_style( $name, $values );
}
}
}
43 changes: 43 additions & 0 deletions includes/apple-exporter/class-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,37 @@ private static function initialize_options() {
'callback' => [ get_called_class(), 'render_meta_component_order' ],
'type' => 'array',
],
'cite_color' => [
'default' => '#4f4f4f',
'label' => __( 'Citation font color', 'apple-news' ),
'type' => 'color',
],
'cite_color_dark' => [
'default' => '',
'label' => __( 'Citation font color', 'apple-news' ),
'type' => 'color',
],
'cite_font' => [
'default' => 'AvenirNext-Italic',
'label' => __( 'Citation font face', 'apple-news' ),
'type' => 'font',
],
'cite_line_height' => [
'default' => 24.0,
'label' => __( 'Citation line height', 'apple-news' ),
'type' => 'float',
],
'cite_size' => [
'default' => 16,
'label' => __( 'Citation font size', 'apple-news' ),
'type' => 'integer',
],
'cite_tracking' => [
'default' => 0,
'description' => __( '(Percentage of font size)', 'apple-news' ),
'label' => __( 'Citation tracking', 'apple-news' ),
'type' => 'integer',
],
'monospaced_color' => [
'default' => '#4f4f4f',
'label' => __( 'Monospaced font color', 'apple-news' ),
Expand Down Expand Up @@ -2435,6 +2466,18 @@ private function initialize_groups() {
'table_header_color_dark',
],
],
'cite' => [
'label' => __( 'Citation (<cite>)', 'apple-news' ),
'settings' => [
'cite_font',
'cite_size',
'cite_line_height',
'cite_tracking',
'cite_color',
'dark_mode_colors_heading',
'cite_color_dark',
],
],
'monospaced' => [
'label' => __( 'Monospaced (<pre>, <code>, <samp>)', 'apple-news' ),
'settings' => [
Expand Down
84 changes: 84 additions & 0 deletions includes/apple-exporter/components/class-body.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ public function register_specs(): void {
$conditional
)
);

$this->register_spec(
'default-text-styles',
__( 'Default Text Styles', 'apple-news' ),
$this->get_default_text_styles()
);
}

/**
Expand Down Expand Up @@ -433,6 +439,84 @@ private function get_default_style_values() {
];
}

/**
* Compile default text styles applied to the document as a whole based on HTML tags.
*
* @return array
*/
private function get_default_text_styles() {
$theme = Theme::get_used();
$conditionals = [];
if ( ! empty( $theme->get_value( 'cite_color_dark' ) ) ) {
$conditionals['cite'] = [
'conditional' => [
'textColor' => '#cite_color_dark#',
'conditions' => [
'minSpecVersion' => '1.14',
'preferredColorScheme' => 'dark',
],
],
];
}
if ( ! empty( $theme->get_value( 'monospaced_color_dark' ) ) ) {
$conditionals['monospaced'] = [
'conditional' => [
'textColor' => '#monospaced_color_dark#',
'conditions' => [
'minSpecVersion' => '1.14',
'preferredColorScheme' => 'dark',
],
],
];
}

return [
'default-tag-cite' => array_merge(
[
'fontName' => '#cite_font#',
'fontSize' => '#cite_size#',
'tracking' => '#cite_tracking#',
'lineHeight' => '#cite_line_height#',
'textColor' => '#cite_color',
],
$conditionals['cite'] ?? []
),
'default-tag-code' => array_merge(
[
'fontName' => '#monospaced_font#',
'fontSize' => '#monospaced_size#',
'tracking' => '#monospaced_tracking#',
'lineHeight' => '#monospaced_line_height#',
'textColor' => '#monospaced_color',
],
$conditionals['monospaced'] ?? []
),
'default-tag-pre' => array_merge(
[
'textAlignment' => 'left',
'fontName' => '#monospaced_font#',
'fontSize' => '#monospaced_size#',
'tracking' => '#monospaced_tracking#',
'lineHeight' => '#monospaced_line_height#',
'textColor' => '#monospaced_color#',
'paragraphSpacingBefore' => 18,
'paragraphSpacingAfter' => 18,
],
$conditionals['monospaced'] ?? []
),
'default-tag-samp' => array_merge(
[
'fontName' => '#monospaced_font#',
'fontSize' => '#monospaced_size#',
'tracking' => '#monospaced_tracking#',
'lineHeight' => '#monospaced_line_height#',
'textColor' => '#monospaced_color#',
],
$conditionals['monospaced'] ?? []
),
];
}

/**
* Set the default style for the component.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/apple-exporter/components/class-footnotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function build( $html ) { // phpcs:ignore VariableAnalysis.CodeAnalysi
$count = $key + 1;
$text = preg_replace(
'/<li(.*?)>(.*?)<\/li>/',
"<p$1>${count}. $2</p>",
"<p$1>{$count}. $2</p>",
$item
);
preg_match( '/id="(.*?)"/', $text, $matches );
Expand Down
20 changes: 12 additions & 8 deletions includes/class-apple-news.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,25 +1110,29 @@ public function upgrade_to_2_4_0(): void {
public function upgrade_to_2_5_0(): void {
$registry = Theme::get_registry();
foreach ( $registry as $theme_name ) {
$theme_object = Admin_Apple_Themes::get_theme_by_name( $theme_name );
$save_theme = false;
$theme_object = Admin_Apple_Themes::get_theme_by_name( $theme_name );
$json_templates = $theme_object->get_value( 'json_templates' );

// Migrate heading layouts from being centrally defined to being defined per heading level.
$json_templates = $theme_object->get_value( 'json_templates' );
if ( ! empty( $json_templates['heading']['heading-layout'] ) ) {
$heading_layout = $json_templates['heading']['heading-layout'];
unset( $json_templates['heading']['heading-layout'] );
for ( $heading_level = 1; $heading_level <= 6; $heading_level++ ) {
$json_templates['heading'][ 'heading-layout-' . $heading_level ] = $heading_layout;
}
$theme_object->set_value( 'json_templates', $json_templates );
$save_theme = true;
}

// Save the theme if there were changes.
if ( $save_theme ) {
$theme_object->save();
}
// Set defaults for new <cite> styles based on caption settings.
$theme_object->set_value( 'cite_color', $theme_object->get_value( 'caption_color' ) );
$theme_object->set_value( 'cite_color_dark', $theme_object->get_value( 'caption_color_dark' ) );
$theme_object->set_value( 'cite_font', $theme_object->get_value( 'caption_font' ) );
$theme_object->set_value( 'cite_line_height', $theme_object->get_value( 'caption_line_height' ) );
$theme_object->set_value( 'cite_size', $theme_object->get_value( 'caption_size' ) );
$theme_object->set_value( 'cite_tracking', $theme_object->get_value( 'caption_tracking' ) );

// Save our changes.
$theme_object->save();
}
}

Expand Down
Loading