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 Library - Site Title, Site Tagline]: Add delay to content changes #37234

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion packages/block-library/src/site-tagline/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
RichText,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useDebounce } from '@wordpress/compose';

export default function SiteTaglineEdit( { attributes, setAttributes } ) {
const { textAlign } = attributes;
Expand All @@ -23,6 +24,7 @@ export default function SiteTaglineEdit( { attributes, setAttributes } ) {
'site',
'description'
);
const debouncedOnChange = useDebounce( setSiteTagline, 500 );
const { canUserEdit, readOnlySiteTagLine } = useSelect( ( select ) => {
const { canUser, getEntityRecord } = select( coreStore );
const siteData = getEntityRecord( 'root', '__unstableBase' );
Expand All @@ -41,7 +43,7 @@ export default function SiteTaglineEdit( { attributes, setAttributes } ) {
const siteTaglineContent = canUserEdit ? (
<RichText
allowedFormats={ [] }
onChange={ setSiteTagline }
onChange={ debouncedOnChange }
aria-label={ __( 'Site tagline text' ) }
placeholder={ __( 'Write site tagline…' ) }
tagName="p"
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/site-title/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { ToggleControl, PanelBody } from '@wordpress/components';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { decodeEntities } from '@wordpress/html-entities';
import { useDebounce } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -32,6 +33,7 @@ export default function SiteTitleEdit( {
} ) {
const { level, textAlign, isLink, linkTarget } = attributes;
const [ title, setTitle ] = useEntityProp( 'root', 'site', 'title' );
const debouncedOnChange = useDebounce( setTitle, 500 );
const { canUserEdit, readOnlyTitle } = useSelect( ( select ) => {
const { canUser, getEntityRecord } = select( coreStore );
const siteData = getEntityRecord( 'root', '__unstableBase' );
Expand All @@ -56,7 +58,7 @@ export default function SiteTitleEdit( {
aria-label={ __( 'Site title text' ) }
placeholder={ __( 'Write site title…' ) }
value={ title }
onChange={ setTitle }
onChange={ debouncedOnChange }
allowedFormats={ [] }
disableLineBreaks
__unstableOnSplitAtEnd={ () =>
Expand Down
4 changes: 4 additions & 0 deletions packages/e2e-tests/specs/editor/blocks/site-title.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ describe( 'Site Title block', () => {
await pressKeyWithModifier( 'primary', 'a' );

await page.keyboard.type( 'New Site Title' );
// Wait a bit as the Site Title's onChange is debounced.
await page.waitForSelector(
'.editor-post-publish-button__button.has-changes-dot'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

In other words, this change shouldn't be necessary for the test to pass.


await saveEntities();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ describe( 'Multi-entity save flow', () => {
await page.focus( editableSiteTagLineSelector );
await page.keyboard.type( '...' );

// Wait a bit as the Site Title's and Site Tagline's `onChange` are debounced,
// and we can't know when these changes took effect with a selector.
// eslint-disable-next-line no-restricted-syntax
await page.waitForTimeout( 1000 );

await clickButton( 'Publish' );
await page.waitForSelector( savePanelSelector );
let checkboxInputs = await page.$$( checkboxInputSelector );
Expand Down