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

Text orientation: Rotate vertical text when the text is aligned (Upside down text) #53175

Merged
merged 8 commits into from
Sep 1, 2023
87 changes: 86 additions & 1 deletion packages/block-editor/src/hooks/typography.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';
import { useMemo, useCallback } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';

/**
* Internal dependencies
Expand All @@ -16,7 +22,11 @@ import {
import { LINE_HEIGHT_SUPPORT_KEY } from './line-height';
import { FONT_FAMILY_SUPPORT_KEY } from './font-family';
import { FONT_SIZE_SUPPORT_KEY } from './font-size';
import { cleanEmptyObject, useBlockSettings } from './utils';
import {
cleanEmptyObject,
useBlockSettings,
shouldSkipSerialization,
} from './utils';

function omit( object, keys ) {
return Object.fromEntries(
Expand Down Expand Up @@ -45,6 +55,14 @@ export const TYPOGRAPHY_SUPPORT_KEYS = [
LETTER_SPACING_SUPPORT_KEY,
];

const hasWritingModeSupport = ( blockType ) => {
const writingModeSupport = getBlockSupport(
blockType,
WRITING_MODE_SUPPORT_KEY
);
return writingModeSupport;
};

function styleToAttributes( style ) {
const updatedStyle = { ...omit( style, [ 'fontFamily' ] ) };
const fontSizeValue = style?.typography?.fontSize;
Expand Down Expand Up @@ -153,3 +171,70 @@ export const hasTypographySupport = ( blockName ) => {
hasBlockSupport( blockName, key )
);
};

/**
* Filters registered block settings to extend the block edit wrapper
* to apply the desired classnames properly.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
export function addEditProps( settings ) {
if ( ! hasWritingModeSupport( settings ) ) {
return settings;
}

const existingGetEditWrapperProps = settings.getEditWrapperProps;
settings.getEditWrapperProps = ( attributes ) => {
let props = {};
if ( existingGetEditWrapperProps ) {
props = existingGetEditWrapperProps( attributes );
}
return addSaveProps( props, settings, attributes );
};

return settings;
}

/**
* Override props assigned to save component to inject typography classnames.
*
* @param {Object} props Additional props applied to save element.
* @param {Object} blockType Block type.
* @param {Object} attributes Block attributes.
*
* @return {Object} Filtered props applied to save element.
*/
export function addSaveProps( props, blockType, attributes ) {
if (
! hasWritingModeSupport( blockType ) ||
shouldSkipSerialization( blockType, WRITING_MODE_SUPPORT_KEY )
) {
return props;
}

const { style } = attributes;
const writingMode = style?.typography?.writingMode;
const newClassName = classnames( props.className, {
'has-writing-mode': writingMode ? true : false,
'is-vertical-lr': writingMode === 'vertical-lr' ? true : false,
'is-vertical-rl': writingMode === 'vertical-rl' ? true : false,
'is-horizontal-tb': writingMode === 'horizontal-tb' ? true : false,
} );
props.className = newClassName ? newClassName : undefined;

return props;
}

addFilter(
'blocks.getSaveContent.extraProps',
'core/typography/addSaveProps',
addSaveProps
);

addFilter(
'blocks.registerBlockType',
'core/typography/addEditProps',
addEditProps
);
8 changes: 8 additions & 0 deletions packages/block-library/src/paragraph/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ p.has-background {
:where(p.has-text-color:not(.has-link-color)) a {
color: inherit;
}

p.has-text-align-right.has-writing-mode.is-vertical-rl {
transform: scale(-1, -1);
carolinan marked this conversation as resolved.
Show resolved Hide resolved
}

body.rtl p.has-text-align-left.has-writing-mode.is-vertical-lr {
transform: scale(-1, -1);
}
6 changes: 6 additions & 0 deletions packages/block-library/src/post-navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ function render_block_core_post_navigation_link( $attributes, $content ) {
if ( isset( $attributes['textAlign'] ) ) {
$classes .= " has-text-align-{$attributes['textAlign']}";
}

$writing_mode = _wp_array_get( $attributes, array( 'style', 'typography', 'writingMode' ), null );
if ( isset( $writing_mode ) ) {
$classes .= ' has-writing-mode';
$classes .= ' is-{$writing_mode}';
}
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
// Set default values.
$format = '%link';
Expand Down
15 changes: 15 additions & 0 deletions packages/block-library/src/post-navigation-link/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,19 @@
}
}

&.has-writing-mode.is-vertical-rl {
writing-mode: vertical-rl;
carolinan marked this conversation as resolved.
Show resolved Hide resolved
}

&.has-writing-mode.is-vertical-lr {
writing-mode: vertical-lr;
}

&.has-text-align-right.has-writing-mode.is-vertical-rl {
transform: scale(-1, -1);
carolinan marked this conversation as resolved.
Show resolved Hide resolved
}
}

body.rtl .wp-block-post-navigation-link .has-text-align-left.has-writing-mode.is-vertical-lr {
transform: scale(-1, -1);
}