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

ServerSideRender: add new skipBlockSupportAttributes prop #44491

Merged
merged 25 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3b35e88
ServerSideRender: add new `skipBlockSupportAttributes` prop
t-hamano Sep 27, 2022
2b6ec9a
Update readme
t-hamano Sep 27, 2022
43fde25
update changelog
t-hamano Sep 27, 2022
59a6178
add unit test
t-hamano Sep 27, 2022
b70814f
Fix typo
t-hamano Sep 27, 2022
8791cc3
Don't remove attributes and styles which serialization is omitted
t-hamano Oct 2, 2022
05ab652
Merge branch 'trunk' into enhancement/server-side-render-global-styles
t-hamano Oct 2, 2022
a42b10e
Update unit tests
t-hamano Oct 2, 2022
f1b0670
Update readme
t-hamano Oct 2, 2022
04f3af0
Don't remove custom style
t-hamano Oct 2, 2022
abf2d7e
Support color.link and update tests
t-hamano Oct 2, 2022
e027d44
Update function docs
t-hamano Oct 2, 2022
d9600d6
Revert "Update function docs"
t-hamano Oct 5, 2022
1a7fd7c
Revert "Support color.link and update tests"
t-hamano Oct 5, 2022
43910b2
Revert "Don't remove custom style"
t-hamano Oct 5, 2022
5d811c8
Revert "Update readme"
t-hamano Oct 5, 2022
bd1ff1e
Revert "Update unit tests"
t-hamano Oct 5, 2022
aa507a1
Revert "Don't remove attributes and styles which serialization is omi…
t-hamano Oct 5, 2022
452c11d
Merge branch 'trunk' into enhancement/server-side-render-global-styles
t-hamano Oct 5, 2022
7b02d8d
Don't pass className
t-hamano Oct 5, 2022
4be4688
Update unit test
t-hamano Oct 5, 2022
ce8937a
Update readme
t-hamano Oct 7, 2022
d58e60c
Update readme
t-hamano Oct 7, 2022
60e711d
Fix conflicts
t-hamano Oct 7, 2022
0de5c1f
Revert unexpected changes
t-hamano Oct 7, 2022
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
4 changes: 4 additions & 0 deletions packages/server-side-render/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Feature

- Add `skipBlockSupportAttributes` props to prevent duplication of styles in the block wrapper and the `ServerSideRender` components. [#44491](https://github.com/WordPress/gutenberg/pull/44491)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the description is unnatural, please feel free to update it 🙏


## 3.17.0 (2022-10-05)

## 3.16.0 (2022-09-21)
Expand Down
9 changes: 9 additions & 0 deletions packages/server-side-render/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ The HTTP request method to use, either 'GET' or 'POST'. It's 'GET' by default. T

- Type: `String`
- Required: No
- Default: 'GET'

#### Example:

Expand All @@ -76,6 +77,14 @@ function add_rest_method( $endpoints ) {
add_filter( 'rest_endpoints', 'add_rest_method');
```

### skipBlockSupportAttributes

Remove attributes and style properties applied by the block supports. This prevents duplication of styles in the block wrapper and the `ServerSideRender` components. Even if certain features skip serialization to HTML markup by `__experimentalSkipSerialization`, all attributes and style properties are removed.

- Type: `Boolean`
- Required: No
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
- Default: false

### urlQueryArgs

Query arguments to apply to the request URL.
Expand Down
31 changes: 30 additions & 1 deletion packages/server-side-render/src/server-side-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { addQueryArgs } from '@wordpress/url';
import { Placeholder, Spinner } from '@wordpress/components';
import { __experimentalSanitizeBlockAttributes } from '@wordpress/blocks';

const EMPTY_OBJECT = {};

export function rendererPath( block, attributes = null, urlQueryArgs = {} ) {
return addQueryArgs( `/wp/v2/block-renderer/${ block }`, {
context: 'edit',
Expand All @@ -22,6 +24,27 @@ export function rendererPath( block, attributes = null, urlQueryArgs = {} ) {
} );
}

export function removeBlockSupportAttributes( attributes ) {
const {
backgroundColor,
borderColor,
fontFamily,
fontSize,
gradient,
textColor,
className,
...restAttributes
} = attributes;

const { border, color, elements, spacing, typography, ...restStyles } =
attributes?.style || EMPTY_OBJECT;

return {
...restAttributes,
style: restStyles,
};
}

function DefaultEmptyResponsePlaceholder( { className } ) {
return (
<Placeholder className={ className }>
Expand Down Expand Up @@ -69,6 +92,7 @@ export default function ServerSideRender( props ) {
className,
httpMethod = 'GET',
urlQueryArgs,
skipBlockSupportAttributes = false,
EmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,
ErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,
LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder,
Expand All @@ -88,10 +112,15 @@ export default function ServerSideRender( props ) {

setIsLoading( true );

const sanitizedAttributes =
let sanitizedAttributes =
attributes &&
__experimentalSanitizeBlockAttributes( block, attributes );

if ( skipBlockSupportAttributes ) {
sanitizedAttributes =
removeBlockSupportAttributes( sanitizedAttributes );
}

// If httpMethod is 'POST', send the attributes in the request body instead of the URL.
// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
const isPostRequest = 'POST' === httpMethod;
Expand Down
67 changes: 66 additions & 1 deletion packages/server-side-render/src/test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* Internal dependencies
*/
import { rendererPath } from '../server-side-render';
import {
rendererPath,
removeBlockSupportAttributes,
} from '../server-side-render';

describe( 'rendererPath', () => {
test( 'should return an base path for empty input', () => {
Expand Down Expand Up @@ -75,4 +78,66 @@ describe( 'rendererPath', () => {
'/wp/v2/block-renderer/core/test-block?context=edit&attributes%5BstringArg%5D=test&id=1234'
);
} );

test( 'Should remove attributes and style properties applied by the block supports', () => {
expect(
removeBlockSupportAttributes( {
backgroundColor: 'foreground',
borderColor: 'foreground',
fontFamily: 'system-font',
fontSize: 'small',
gradient: 'vivid-cyan-blue-to-vivid-purple',
textColor: 'foreground',
customAttribute: 'customAttribute',
className: 'custom-class',
style: {
border: {
radius: '10px',
style: 'solid',
width: '10px',
},
color: {
background: '#000000',
text: '#000000',
},
elements: {
link: {
color: {
text: '#000000',
},
},
},
spacing: {
margin: {
top: '10px',
right: '10px',
bottom: '10px',
left: '10px',
},
padding: {
top: '10px',
right: '10px',
bottom: '10px',
left: '10px',
},
},
typography: {
fontSize: '10px',
fontStyle: 'normal',
fontWeight: '500',
letterSpacing: '10px',
lineHeight: '1',
textDecoration: 'line-through',
textTransform: 'uppercase',
},
customStyle: 'customStyle',
},
} )
).toEqual( {
customAttribute: 'customAttribute',
style: {
customStyle: 'customStyle',
},
} );
} );
} );