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

Block Supports: Allow skipping serialization of font size #30879

Merged
merged 9 commits into from
Apr 28, 2021

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Apr 15, 2021

Description

Part of #28913. Counterpart to #29142+#29253, or #30035.

Find affected blocks by grepping "fontSize": true in block.json files.

How has this been tested?

(Based on @nosolosw's #30035 (comment), and adapted for fontSize block support.)

  • Install and activate the TT1-blocks theme.
  • Add "__experimentalSkipFontSizeSerialization": true as a new key-value pair within the supports object for any block that also has "fontSize": true set. In the following, we'll be using the Heading block.

Here's a patch for your convenience:

diff --git a/packages/block-library/src/heading/block.json b/packages/block-library/src/heading/block.json
index 8d7e0fdd5c..a35cf68b6c 100644
--- a/packages/block-library/src/heading/block.json
+++ b/packages/block-library/src/heading/block.json
@@ -74,6 +74,7 @@
                                }
                        }
                },
+               "__experimentalSkipFontSizeSerialization": true,
                "__unstablePasteTextInline": true
        },
        "editorStyle": "wp-block-heading-editor",

The expected result is that:

  • The UI control for font size is shown.
  • Using the UI control still syncs the block attributes.
    • Use the font size control.
    • Use the dropdown to select a "named" font size, e.g. "Large".
    • Open the Code Editor and verify that the block has the proper font size value as fontSize attribute, e.g. <!-- wp:heading {"fontSize":"large"} --> -- but not as inlined markup (HTML class="has-large-font-size" attribute on the <h2 /> tag).
    • Return to the Visual Editor.
    • Use the font size control to set the block's font size to a numeric value (e.g. 25px).
    • Open the Code Editor and verify that the block has the proper font size value as a nested typography:{ fontSize: ... } } attribute, e.g. <!-- wp:heading {"style":{"typography":{"fontSize":"25px"}}} --> -- but not as inlined markup (HTML style="font-size:25px" attribute on the <h2 /> tag).
    • Make sure to also change the line height control, to verify that it is unaffected (i.e. there's still a style="line-height:... attribute on the <h2 /> tag).

Screencasts

Before

font-size-before

After

font-size

Question

fontSize is a non-experimental, "flat" (non-array) block-supports flag. This is the reason why we're introducing another "flat" flag to skip serialization specifically of font size here: __experimentalSkipFontSizeSerialization.

Compare that to "nested" block-supports flags (such as color), where we can include a "generic" __experimentalSkipSerialization in that array:

"color": {
"__experimentalSkipSerialization": true,
"gradients": true
},

A dedicated __experimentalSkipFontSizeSerialization flag doesn't scale particularly well, and might get awkward once we allow skipping of serialization for other typographic properties (line height, font family, font appearance, text decoration, text transform). Will each of these get their own __experimentalSkip...Serialization? Or will we have to group typographic block-supports flags into a shared typography object first? (But that might affect themes that use these already, won't it?) 🤔

@ockham ockham added the Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json label Apr 15, 2021
@ockham ockham self-assigned this Apr 15, 2021
@github-actions
Copy link

github-actions bot commented Apr 15, 2021

Size Change: -99.8 kB (-7%) ✅

Total Size: 1.39 MB

Filename Size Change
build/a11y/index.js 1.12 kB -24 B (-2%)
build/annotations/index.js 2.93 kB -861 B (-23%) 🎉
build/api-fetch/index.js 2.42 kB -1 kB (-29%) 🎉
build/autop/index.js 2.28 kB -553 B (-20%) 🎉
build/blob/index.js 672 B +7 B (+1%)
build/block-directory/index.js 6.6 kB -2.01 kB (-23%) 🎉
build/block-directory/style-rtl.css 993 B -12 B (-1%)
build/block-directory/style.css 995 B -11 B (-1%)
build/block-editor/index.js 116 kB -15.6 kB (-12%) 👏
build/block-editor/style-rtl.css 13 kB -29 B (0%)
build/block-editor/style.css 13 kB -29 B (0%)
build/block-library/blocks/button/style-rtl.css 501 B -2 B (0%)
build/block-library/blocks/button/style.css 500 B -3 B (-1%)
build/block-library/blocks/columns/style-rtl.css 422 B -14 B (-3%)
build/block-library/blocks/columns/style.css 422 B -13 B (-3%)
build/block-library/blocks/cover/editor-rtl.css 603 B -2 B (0%)
build/block-library/blocks/cover/editor.css 604 B -1 B (0%)
build/block-library/blocks/cover/style-rtl.css 1.22 kB -8 B (-1%)
build/block-library/blocks/cover/style.css 1.22 kB -7 B (-1%)
build/block-library/blocks/file/frontend.js 773 B +8 B (+1%)
build/block-library/blocks/freeform/editor-rtl.css 2.45 kB +4 B (0%)
build/block-library/blocks/freeform/editor.css 2.45 kB +4 B (0%)
build/block-library/blocks/gallery/style-rtl.css 1.06 kB -36 B (-3%)
build/block-library/blocks/gallery/style.css 1.05 kB -35 B (-3%)
build/block-library/blocks/legacy-widget/editor-rtl.css 557 B +159 B (+40%) 🚨
build/block-library/blocks/legacy-widget/editor.css 557 B +158 B (+40%) 🚨
build/block-library/blocks/media-text/editor-rtl.css 176 B -15 B (-8%)
build/block-library/blocks/media-text/editor.css 176 B -15 B (-8%)
build/block-library/blocks/media-text/style-rtl.css 492 B -43 B (-8%)
build/block-library/blocks/media-text/style.css 489 B -43 B (-8%)
build/block-library/blocks/navigation-link/style-rtl.css 1.17 kB -6 B (-1%)
build/block-library/blocks/navigation-link/style.css 1.17 kB -7 B (-1%)
build/block-library/blocks/navigation/editor-rtl.css 1.19 kB +3 B (0%)
build/block-library/blocks/navigation/editor.css 1.19 kB +3 B (0%)
build/block-library/blocks/table/style-rtl.css 407 B +5 B (+1%)
build/block-library/blocks/table/style.css 407 B +5 B (+1%)
build/block-library/blocks/template-part/editor-rtl.css 551 B -1 B (0%)
build/block-library/blocks/template-part/editor.css 550 B -1 B (0%)
build/block-library/blocks/video/editor-rtl.css 569 B +1 B (0%)
build/block-library/blocks/video/editor.css 570 B +1 B (0%)
build/block-library/blocks/video/style-rtl.css 169 B -4 B (-2%)
build/block-library/blocks/video/style.css 169 B -4 B (-2%)
build/block-library/editor-rtl.css 9.53 kB +110 B (+1%)
build/block-library/editor.css 9.52 kB +110 B (+1%)
build/block-library/index.js 142 kB -9.29 kB (-6%)
build/block-library/reset-rtl.css 506 B +4 B (+1%)
build/block-library/reset.css 507 B +4 B (+1%)
build/block-library/style-rtl.css 9.46 kB -95 B (-1%)
build/block-library/style.css 9.46 kB -99 B (-1%)
build/block-serialization-default-parser/index.js 1.3 kB -576 B (-31%) 🎉
build/blocks/index.js 46.6 kB -1.94 kB (-4%)
build/components/index.js 270 kB -17 kB (-6%)
build/components/style-rtl.css 16.3 kB -38 B (0%)
build/components/style.css 16.3 kB -41 B (0%)
build/compose/index.js 9.97 kB -1.61 kB (-14%) 👏
build/core-data/index.js 12 kB -5.21 kB (-30%) 🎉
build/customize-widgets/index.js 5.79 kB -2.49 kB (-30%) 🎉
build/data-controls/index.js 825 B -14 B (-2%)
build/data/index.js 7.55 kB -1.66 kB (-18%) 👏
build/date/index.js 31.8 kB -18 B (0%)
build/deprecated/index.js 738 B -49 B (-6%)
build/dom/index.js 4.61 kB -918 B (-17%) 👏
build/edit-navigation/index.js 13.3 kB -3.8 kB (-22%) 🎉
build/edit-navigation/style-rtl.css 2.84 kB -16 B (-1%)
build/edit-navigation/style.css 2.85 kB -16 B (-1%)
build/edit-post/index.js 335 kB -4.56 kB (-1%)
build/edit-post/style-rtl.css 6.95 kB -12 B (0%)
build/edit-post/style.css 6.93 kB -12 B (0%)
build/edit-site/index.js 25.2 kB -3.76 kB (-13%) 👏
build/edit-site/style-rtl.css 4.9 kB +1 B (0%)
build/edit-site/style.css 4.89 kB +2 B (0%)
build/edit-widgets/index.js 13 kB -3.67 kB (-22%) 🎉
build/edit-widgets/style-rtl.css 2.98 kB +6 B (0%)
build/edit-widgets/style.css 2.98 kB +6 B (0%)
build/editor/index.js 60.5 kB -5.25 kB (-8%)
build/element/index.js 3.44 kB -1.19 kB (-26%) 🎉
build/escape-html/index.js 739 B +4 B (+1%)
build/format-library/index.js 5.67 kB -1.09 kB (-16%) 👏
build/hooks/index.js 1.76 kB -516 B (-23%) 🎉
build/html-entities/index.js 628 B +5 B (+1%)
build/i18n/index.js 3.73 kB -313 B (-8%)
build/is-shallow-equal/index.js 710 B +12 B (+2%)
build/keyboard-shortcuts/index.js 1.65 kB -888 B (-35%) 🎉
build/keycodes/index.js 1.43 kB -515 B (-26%) 🎉
build/list-reusable-blocks/index.js 2.07 kB -1.12 kB (-35%) 🎉
build/media-utils/index.js 3.08 kB -2.31 kB (-43%) 🎉
build/notices/index.js 1.07 kB -783 B (-42%) 🎉
build/nux/index.js 2.31 kB -1.11 kB (-33%) 🎉
build/nux/style-rtl.css 718 B -13 B (-2%)
build/nux/style.css 716 B -11 B (-2%)
build/plugins/index.js 2 kB -961 B (-32%) 🎉
build/primitives/index.js 1.03 kB -385 B (-27%) 🎉
build/react-i18n/index.js 924 B -529 B (-36%) 🎉
build/redux-routine/index.js 2.82 kB -14 B (0%)
build/reusable-blocks/index.js 2.56 kB -1.27 kB (-33%) 🎉
build/rich-text/index.js 11.7 kB -1.85 kB (-14%) 👏
build/server-side-render/index.js 1.64 kB -965 B (-37%) 🎉
build/shortcode/index.js 1.69 kB -13 B (-1%)
build/token-list/index.js 847 B -422 B (-33%) 🎉
build/url/index.js 1.95 kB -1.07 kB (-35%) 🎉
build/viewport/index.js 1.28 kB -572 B (-31%) 🎉
build/wordcount/index.js 1.24 kB +13 B (+1%)
ℹ️ View Unchanged
Filename Size Change
build/block-library/blocks/archives/editor-rtl.css 61 B 0 B
build/block-library/blocks/archives/editor.css 60 B 0 B
build/block-library/blocks/audio/editor-rtl.css 58 B 0 B
build/block-library/blocks/audio/editor.css 58 B 0 B
build/block-library/blocks/audio/style-rtl.css 112 B 0 B
build/block-library/blocks/audio/style.css 112 B 0 B
build/block-library/blocks/block/editor-rtl.css 161 B 0 B
build/block-library/blocks/block/editor.css 161 B 0 B
build/block-library/blocks/button/editor-rtl.css 475 B 0 B
build/block-library/blocks/button/editor.css 474 B 0 B
build/block-library/blocks/buttons/editor-rtl.css 315 B 0 B
build/block-library/blocks/buttons/editor.css 315 B 0 B
build/block-library/blocks/buttons/style-rtl.css 368 B 0 B
build/block-library/blocks/buttons/style.css 368 B 0 B
build/block-library/blocks/calendar/style-rtl.css 208 B 0 B
build/block-library/blocks/calendar/style.css 208 B 0 B
build/block-library/blocks/categories/editor-rtl.css 84 B 0 B
build/block-library/blocks/categories/editor.css 83 B 0 B
build/block-library/blocks/categories/style-rtl.css 79 B 0 B
build/block-library/blocks/categories/style.css 79 B 0 B
build/block-library/blocks/code/style-rtl.css 90 B 0 B
build/block-library/blocks/code/style.css 90 B 0 B
build/block-library/blocks/columns/editor-rtl.css 190 B 0 B
build/block-library/blocks/columns/editor.css 190 B 0 B
build/block-library/blocks/embed/editor-rtl.css 486 B 0 B
build/block-library/blocks/embed/editor.css 486 B 0 B
build/block-library/blocks/embed/style-rtl.css 401 B 0 B
build/block-library/blocks/embed/style.css 400 B 0 B
build/block-library/blocks/file/editor-rtl.css 301 B 0 B
build/block-library/blocks/file/editor.css 300 B 0 B
build/block-library/blocks/file/style-rtl.css 255 B 0 B
build/block-library/blocks/file/style.css 255 B 0 B
build/block-library/blocks/gallery/editor-rtl.css 704 B 0 B
build/block-library/blocks/gallery/editor.css 705 B 0 B
build/block-library/blocks/group/editor-rtl.css 160 B 0 B
build/block-library/blocks/group/editor.css 160 B 0 B
build/block-library/blocks/group/style-rtl.css 57 B 0 B
build/block-library/blocks/group/style.css 57 B 0 B
build/block-library/blocks/heading/editor-rtl.css 129 B 0 B
build/block-library/blocks/heading/editor.css 129 B 0 B
build/block-library/blocks/heading/style-rtl.css 76 B 0 B
build/block-library/blocks/heading/style.css 76 B 0 B
build/block-library/blocks/html/editor-rtl.css 281 B 0 B
build/block-library/blocks/html/editor.css 281 B 0 B
build/block-library/blocks/image/editor-rtl.css 717 B 0 B
build/block-library/blocks/image/editor.css 716 B 0 B
build/block-library/blocks/image/style-rtl.css 476 B 0 B
build/block-library/blocks/image/style.css 478 B 0 B
build/block-library/blocks/latest-comments/style-rtl.css 281 B 0 B
build/block-library/blocks/latest-comments/style.css 282 B 0 B
build/block-library/blocks/latest-posts/editor-rtl.css 137 B 0 B
build/block-library/blocks/latest-posts/editor.css 137 B 0 B
build/block-library/blocks/latest-posts/style-rtl.css 523 B 0 B
build/block-library/blocks/latest-posts/style.css 522 B 0 B
build/block-library/blocks/list/style-rtl.css 63 B 0 B
build/block-library/blocks/list/style.css 63 B 0 B
build/block-library/blocks/more/editor-rtl.css 434 B 0 B
build/block-library/blocks/more/editor.css 434 B 0 B
build/block-library/blocks/navigation-link/editor-rtl.css 597 B 0 B
build/block-library/blocks/navigation-link/editor.css 597 B 0 B
build/block-library/blocks/navigation/style-rtl.css 272 B 0 B
build/block-library/blocks/navigation/style.css 271 B 0 B
build/block-library/blocks/nextpage/editor-rtl.css 395 B 0 B
build/block-library/blocks/nextpage/editor.css 395 B 0 B
build/block-library/blocks/page-list/editor-rtl.css 239 B 0 B
build/block-library/blocks/page-list/editor.css 240 B 0 B
build/block-library/blocks/page-list/style-rtl.css 167 B 0 B
build/block-library/blocks/page-list/style.css 167 B 0 B
build/block-library/blocks/paragraph/editor-rtl.css 157 B 0 B
build/block-library/blocks/paragraph/editor.css 157 B 0 B
build/block-library/blocks/paragraph/style-rtl.css 247 B 0 B
build/block-library/blocks/paragraph/style.css 248 B 0 B
build/block-library/blocks/post-author/editor-rtl.css 209 B 0 B
build/block-library/blocks/post-author/editor.css 209 B 0 B
build/block-library/blocks/post-author/style-rtl.css 183 B 0 B
build/block-library/blocks/post-author/style.css 184 B 0 B
build/block-library/blocks/post-comments-form/style-rtl.css 250 B 0 B
build/block-library/blocks/post-comments-form/style.css 250 B 0 B
build/block-library/blocks/post-content/editor-rtl.css 139 B 0 B
build/block-library/blocks/post-content/editor.css 139 B 0 B
build/block-library/blocks/post-excerpt/editor-rtl.css 73 B 0 B
build/block-library/blocks/post-excerpt/editor.css 73 B 0 B
build/block-library/blocks/post-excerpt/style-rtl.css 69 B 0 B
build/block-library/blocks/post-excerpt/style.css 69 B 0 B
build/block-library/blocks/post-featured-image/editor-rtl.css 338 B 0 B
build/block-library/blocks/post-featured-image/editor.css 338 B 0 B
build/block-library/blocks/post-featured-image/style-rtl.css 100 B 0 B
build/block-library/blocks/post-featured-image/style.css 100 B 0 B
build/block-library/blocks/post-title/style-rtl.css 60 B 0 B
build/block-library/blocks/post-title/style.css 60 B 0 B
build/block-library/blocks/preformatted/style-rtl.css 103 B 0 B
build/block-library/blocks/preformatted/style.css 103 B 0 B
build/block-library/blocks/pullquote/editor-rtl.css 183 B 0 B
build/block-library/blocks/pullquote/editor.css 183 B 0 B
build/block-library/blocks/pullquote/style-rtl.css 318 B 0 B
build/block-library/blocks/pullquote/style.css 318 B 0 B
build/block-library/blocks/query-loop/editor-rtl.css 83 B 0 B
build/block-library/blocks/query-loop/editor.css 82 B 0 B
build/block-library/blocks/query-loop/style-rtl.css 315 B 0 B
build/block-library/blocks/query-loop/style.css 317 B 0 B
build/block-library/blocks/query-pagination-numbers/editor-rtl.css 122 B 0 B
build/block-library/blocks/query-pagination-numbers/editor.css 121 B 0 B
build/block-library/blocks/query-pagination/editor-rtl.css 270 B 0 B
build/block-library/blocks/query-pagination/editor.css 262 B 0 B
build/block-library/blocks/query-pagination/style-rtl.css 168 B 0 B
build/block-library/blocks/query-pagination/style.css 168 B 0 B
build/block-library/blocks/query-title/editor-rtl.css 86 B 0 B
build/block-library/blocks/query-title/editor.css 86 B 0 B
build/block-library/blocks/query/editor-rtl.css 131 B 0 B
build/block-library/blocks/query/editor.css 132 B 0 B
build/block-library/blocks/quote/style-rtl.css 169 B 0 B
build/block-library/blocks/quote/style.css 169 B 0 B
build/block-library/blocks/rss/editor-rtl.css 201 B 0 B
build/block-library/blocks/rss/editor.css 202 B 0 B
build/block-library/blocks/rss/style-rtl.css 290 B 0 B
build/block-library/blocks/rss/style.css 290 B 0 B
build/block-library/blocks/search/editor-rtl.css 189 B 0 B
build/block-library/blocks/search/editor.css 189 B 0 B
build/block-library/blocks/search/style-rtl.css 359 B 0 B
build/block-library/blocks/search/style.css 362 B 0 B
build/block-library/blocks/separator/editor-rtl.css 99 B 0 B
build/block-library/blocks/separator/editor.css 99 B 0 B
build/block-library/blocks/separator/style-rtl.css 251 B 0 B
build/block-library/blocks/separator/style.css 251 B 0 B
build/block-library/blocks/shortcode/editor-rtl.css 512 B 0 B
build/block-library/blocks/shortcode/editor.css 512 B 0 B
build/block-library/blocks/site-logo/editor-rtl.css 440 B 0 B
build/block-library/blocks/site-logo/editor.css 441 B 0 B
build/block-library/blocks/site-logo/style-rtl.css 154 B 0 B
build/block-library/blocks/site-logo/style.css 154 B 0 B
build/block-library/blocks/social-link/editor-rtl.css 164 B 0 B
build/block-library/blocks/social-link/editor.css 165 B 0 B
build/block-library/blocks/social-links/editor-rtl.css 796 B 0 B
build/block-library/blocks/social-links/editor.css 795 B 0 B
build/block-library/blocks/social-links/style-rtl.css 1.32 kB 0 B
build/block-library/blocks/social-links/style.css 1.33 kB 0 B
build/block-library/blocks/spacer/editor-rtl.css 308 B 0 B
build/block-library/blocks/spacer/editor.css 308 B 0 B
build/block-library/blocks/spacer/style-rtl.css 48 B 0 B
build/block-library/blocks/spacer/style.css 48 B 0 B
build/block-library/blocks/table/editor-rtl.css 478 B 0 B
build/block-library/blocks/table/editor.css 478 B 0 B
build/block-library/blocks/tag-cloud/editor-rtl.css 118 B 0 B
build/block-library/blocks/tag-cloud/editor.css 118 B 0 B
build/block-library/blocks/tag-cloud/style-rtl.css 94 B 0 B
build/block-library/blocks/tag-cloud/style.css 94 B 0 B
build/block-library/blocks/term-description/editor-rtl.css 90 B 0 B
build/block-library/blocks/term-description/editor.css 90 B 0 B
build/block-library/blocks/text-columns/editor-rtl.css 95 B 0 B
build/block-library/blocks/text-columns/editor.css 95 B 0 B
build/block-library/blocks/text-columns/style-rtl.css 166 B 0 B
build/block-library/blocks/text-columns/style.css 166 B 0 B
build/block-library/blocks/verse/style-rtl.css 87 B 0 B
build/block-library/blocks/verse/style.css 87 B 0 B
build/block-library/common-rtl.css 1.31 kB 0 B
build/block-library/common.css 1.31 kB 0 B
build/block-library/theme-rtl.css 692 B 0 B
build/block-library/theme.css 693 B 0 B
build/block-serialization-spec-parser/index.js 3.06 kB 0 B
build/customize-widgets/style-rtl.css 666 B 0 B
build/customize-widgets/style.css 667 B 0 B
build/dom-ready/index.js 577 B 0 B
build/edit-post/classic-rtl.css 454 B 0 B
build/edit-post/classic.css 454 B 0 B
build/editor/style-rtl.css 3.9 kB 0 B
build/editor/style.css 3.9 kB 0 B
build/format-library/style-rtl.css 637 B 0 B
build/format-library/style.css 639 B 0 B
build/list-reusable-blocks/style-rtl.css 629 B 0 B
build/list-reusable-blocks/style.css 628 B 0 B
build/priority-queue/index.js 790 B 0 B
build/reusable-blocks/style-rtl.css 225 B 0 B
build/reusable-blocks/style.css 225 B 0 B
build/warning/index.js 1.14 kB 0 B

compressed-size-action

@ockham ockham marked this pull request as ready for review April 16, 2021 19:22
@ockham ockham requested a review from oandregal April 16, 2021 19:22
Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

This tests as advertised and is looking pretty good 👍

I believe we might need an additional check for skipping serialization in withFontSizeInlineStyles in hooks/font-size.js.

This will prevent the inline styles being applied to the block when selecting a different font size from the dropdown or a custom font size that matches a preset.

	if (
		! hasBlockSupport( blockName, FONT_SIZE_SUPPORT_KEY ) ||
		hasBlockSupport(
			blockName,
			'__experimentalSkipFontSizeSerialization'
		)
	) {
		return <BlockListBlock { ...props } />;
	}

Regarding the issue of the flat __experimentalSkipFontSizeSerialization flag, it would feel neater if the typography support flags were collected under typography like color or border. As mentioned though that would be a breaking change for themes already using them.

Could an intermediate step be to use a typography.__experimentalSkipSerialization or __experimentalSkipTypographySerialization flag and check that one flag across the typography related block supports?

In other words do we need separate __experimentalSkip...Serialization flags?

Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

Could an intermediate step be to use a typography.__experimentalSkipSerialization

I like the idea of this intermediate step.

Our block.json supports API right now has inconsistencies in how we handle typography vs colors:

		"color": {
			"gradients": true,
			"link": true
		},
		"fontSize": true,
		"lineHeight": true,

It is also inconsistent with theme.json given that there we have:

			"typography": {
				"fontSize": "value",
				"lineHeight": "value",
			}

The intermediate step allows us not to build on these inconsistencies. I guess we can change the shape of block.json and be back-compatible. We can have a back-compatibility layer that checks if things like fontSize and lineHeight are present in the data structure and if yes nests them under typography. Our code excluding the back-compatibility layer would only rely on the new structure.
The shape change would be done in another PR. I'm not sure if a change like this would require a new "apiVersion", I guess it is something minor that does not requires it, but I'm cc'ing @gziolo in case he has some thoughts.

@gziolo
Copy link
Member

gziolo commented Apr 22, 2021

The shape change would be done in another PR. I'm not sure if a change like this would require a new "apiVersion", I guess it is something minor that does not requires it, but I'm cc'ing @gziolo in case he has some thoughts.

It's already document at https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/, but it would be good to validate whether it was released in the previous major version of WordPress. If not then we can simply change it and keep the backward compatibility layer only in the plugin for a few more releases.

In general, the question goes to @nosolosw how much supports in block.json should align with theme.json as it doesn't necessarily translate one to one. Conceptually, I would be in favor of using the same naming and grouping in both places to make it consistent. If they are also grouped in the UI in the same way, then it adds another reason to make the extra efforts to bring harmony how all that fits together.

Concluding, if this is only a simple mapping layer that would have to map some properties from their deprecated names, then we should definitely go for it. In the documentation, we should simply remove old variants in favor of the refined approach.

@ockham
Copy link
Contributor Author

ockham commented Apr 22, 2021

This tests as advertised and is looking pretty good 👍

Yay! 🎉 Thanks for reviewing and testing!

I believe we might need an additional check for skipping serialization in withFontSizeInlineStyles in hooks/font-size.js.

This will prevent the inline styles being applied to the block when selecting a different font size from the dropdown or a custom font size that matches a preset.

	if (
		! hasBlockSupport( blockName, FONT_SIZE_SUPPORT_KEY ) ||
		hasBlockSupport(
			blockName,
			'__experimentalSkipFontSizeSerialization'
		)
	) {
		return <BlockListBlock { ...props } />;
	}

I stared at this file for a while when I was working on this PR and decided to leave it as-it. I don't claim I understood it fully at the time (and there's a chance that I still don't 😅 ), but my reading was as follows:
This only modifies the BlockListBlock wrapper -- which is editor-specific, and doesn't get serialized.

The logic in withFontSizeInlineStyles will only kick in if the fontSize attribute is set (to a "named" font size, such as large), whereas style.typography.fontSize isn't. In that case, the logic will look up the font size (in px) per information found in theme.json's typography.fontSizes setting. It will then add that fontSize to the BlockListBlock wrapper's style.

I think it's fine to retain this behavior, even if we're disabling serialization 🤔 The only thing that changes compared to the previous situation is that we're no longer attaching a class or style to the actual markup element. But we're still storing the fontSize attribute; so I think whatever necessitated the style to be added to the BlockListBlock wrapper still holds.

LMK if this makes sense -- I'm still not 100% sure myself 😅

This comment is a bit cryptic:

* Ideally, this is not needed and themes load the font-size classes on the
* editor.
-- Not sure how a theme would load font-size classes in the editor? I thought it would do that through the typography.fontSizes setting -- but that's what withFontSizeInlineStyles is evaluating anyway 🤔 cc/ @nosolosw @jorgefilipecosta

@ockham
Copy link
Contributor Author

ockham commented Apr 22, 2021

Could an intermediate step be to use a typography.__experimentalSkipSerialization

I like the idea of this intermediate step.

Sounds good to me, too 👍

Our block.json supports API right now has inconsistencies in how we handle typography vs colors:

		"color": {
			"gradients": true,
			"link": true
		},
		"fontSize": true,
		"lineHeight": true,

It is also inconsistent with theme.json given that there we have:

			"typography": {
				"fontSize": "value",
				"lineHeight": "value",
			}

The intermediate step allows us not to build on these inconsistencies. I guess we can change the shape of block.json and be back-compatible. We can have a back-compatibility layer that checks if things like fontSize and lineHeight are present in the data structure and if yes nests them under typography.

We already have something like that in the useEditorFeature hook, don't we? Except it almost looks like we're doing the opposite there -- e.g. mapping a nested typography.customLineHeight to a flat enableCustomLineHeight 🤔

'typography.customLineHeight': ( settings ) =>
settings.enableCustomLineHeight,

This is getting pretty confusing 😅 All said and done, would we end up with

  • a editor-feature deprecation that transforms typography.customLineHeight into enableCustomLineHeight (as we already do), and
  • a theme-setting deprecation that transforms lineHeight into typography.lineHeight? Oh boy 😬

Our code excluding the back-compatibility layer would only rely on the new structure.
The shape change would be done in another PR. I'm not sure if a change like this would require a new "apiVersion", I guess it is something minor that does not requires it, but I'm cc'ing @gziolo in case he has some thoughts.

Should this PR be blocked by the shape change, or can we do that after merging this one?

@gziolo
Copy link
Member

gziolo commented Apr 23, 2021

Should this PR be blocked by the shape change, or can we do that after merging this one?

Definitely not a blocker. It should be done separately so we could reason about all changes in one place.

@aaronrobertshaw
Copy link
Contributor

LMK if this makes sense -- I'm still not 100% sure myself 😅

Thanks for the extra background @ockham, I don't claim to be an expert myself either 😄

I stared at this file for a while when I was working on this PR and decided to leave it as-it. I don't claim I understood it fully at the time (and there's a chance that I still don't 😅 ), but my reading was as follows:
This only modifies the BlockListBlock wrapper -- which is editor-specific, and doesn't get serialized.

While it's correct that it's editor specific, the font size styles are serialized for the block's actual markup in the DOM. The markup you see in the code editor view is untouched.

So changing the font size selection from the drop down would impact the visual size of the text. If this is being used on a more complicated block where the font size should only be applied to an inner element, while it is in the editor, this rogue style would cause a disjoint between what the user sees in the editor vs frontend.

Current Situation Updated withFontSizeInlineStyles
FontSizeSerialization FontSizeSerialization2

With the tweak to withFontSizeInlineStyles the styles applied to the block's markup in the DOM more closely matches the markup seen in the code editor view as well.

Should this PR be blocked by the shape change, or can we do that after merging this one?

A separate PR for the shape change gets my vote as well and shouldn't block this one.

@ockham
Copy link
Contributor Author

ockham commented Apr 23, 2021

Thanks for taking the time to explain, and to prepare those Gifs, @aaronrobertshaw!

So changing the font size selection from the drop down would impact the visual size of the text. If this is being used on a more complicated block where the font size should only be applied to an inner element, while it is in the editor, this rogue style would cause a disjoint between what the user sees in the editor vs frontend.

This is where the penny dropped for me. Thanks, I'll apply the change you suggested in #30879 (review) 😊

Should this PR be blocked by the shape change, or can we do that after merging this one?
A separate PR for the shape change gets my vote as well and shouldn't block this one.

Glad that you and @gziolo agree! ✨

@ockham
Copy link
Contributor Author

ockham commented Apr 23, 2021

So changing the font size selection from the drop down would impact the visual size of the text. If this is being used on a more complicated block where the font size should only be applied to an inner element, while it is in the editor, this rogue style would cause a disjoint between what the user sees in the editor vs frontend.

This is where the penny dropped for me. Thanks, I'll apply the change you suggested in #30879 (review) 😊

Done per the last couple of commits. Should be ready for another look 😊

@ockham ockham force-pushed the add/block-supports-skip-serialization-for-font-size branch from bc3ec91 to 581bb3f Compare April 26, 2021 10:16
@aaronrobertshaw aaronrobertshaw self-requested a review April 27, 2021 04:59
Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

Thanks for the updates on this 👍

I'd say this one is nearly ready to merge. I just have one comment relating to a difference in flags being checked server-side vs client.

The skipping of font size serialization works well for static blocks. The issue mentioned above however means you'd need a second flag for the same behaviour to occur for a dynamic block rendering via PHP.

Adding the __experimentalSkipFontSizeSerialization flag to the Post Tags block results in the experience below i.e. serialization still occurs for dynamic blocks on the frontend. Bringing the flag checked within typography.php in line fixes this issue.

lib/block-supports/typography.php Outdated Show resolved Hide resolved
Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
@aaronrobertshaw aaronrobertshaw self-requested a review April 27, 2021 23:31
Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

LGTM ✨

This tests well for me now with static and dynamic blocks in both the editor and on the frontend.

@ockham
Copy link
Contributor Author

ockham commented Apr 28, 2021

Thanks @aaronrobertshaw! Really appreciated your review! ✨

@ockham ockham merged commit a4003a0 into trunk Apr 28, 2021
@ockham ockham deleted the add/block-supports-skip-serialization-for-font-size branch April 28, 2021 09:49
@github-actions github-actions bot added this to the Gutenberg 10.6 milestone Apr 28, 2021
@oandregal oandregal mentioned this pull request Apr 28, 2021
82 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants