diff --git a/packages/editor/src/components/post-text-editor/index.js b/packages/editor/src/components/post-text-editor/index.js index 577f474192701..b764c02014f44 100644 --- a/packages/editor/src/components/post-text-editor/index.js +++ b/packages/editor/src/components/post-text-editor/index.js @@ -7,13 +7,12 @@ import Textarea from 'react-autosize-textarea'; * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { useState, useEffect } from '@wordpress/element'; +import { useState } from '@wordpress/element'; import { parse } from '@wordpress/blocks'; import { useDispatch, useSelect } from '@wordpress/data'; import { useInstanceId } from '@wordpress/compose'; import { VisuallyHidden } from '@wordpress/components'; -export const DEBOUNCE_TIME = 300; export default function PostTextEditor() { const postContent = useSelect( ( select ) => select( 'core/editor' ).getEditedPostContent(), @@ -30,18 +29,6 @@ export default function PostTextEditor() { setValue( postContent ); } - const saveText = () => { - const blocks = parse( value ); - resetEditorBlocks( blocks ); - }; - - useEffect( () => { - const timeoutId = setTimeout( saveText, DEBOUNCE_TIME ); - return () => { - clearTimeout( timeoutId ); - }; - }, [ value ] ); - /** * Handles a textarea change event to notify the onChange prop callback and * reflect the new value in the component's own state. This marks the start @@ -67,7 +54,8 @@ export default function PostTextEditor() { */ const stopEditing = () => { if ( isDirty ) { - saveText(); + const blocks = parse( value ); + resetEditorBlocks( blocks ); setIsDirty( false ); } }; diff --git a/packages/editor/src/components/post-text-editor/test/index.js b/packages/editor/src/components/post-text-editor/test/index.js index c6f8492a1fb58..d132c3b47cd79 100644 --- a/packages/editor/src/components/post-text-editor/test/index.js +++ b/packages/editor/src/components/post-text-editor/test/index.js @@ -7,14 +7,13 @@ import Textarea from 'react-autosize-textarea'; /** * WordPress dependencies */ -import * as wp from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; /** * Internal dependencies */ -import PostTextEditor, { DEBOUNCE_TIME } from '../'; +import PostTextEditor from '../'; -const useSelect = wp.useSelect; // "Downgrade" ReactAutosizeTextarea to a regular textarea. Assumes aligned // props interface. jest.mock( 'react-autosize-textarea', () => ( props ) => ( @@ -176,23 +175,4 @@ describe( 'PostTextEditor', () => { expect( textarea.props.value ).toBe( 'Goodbye World' ); } ); - it( 'debounce value update after given time', () => { - let wrapper; - act( () => { - wrapper = create( ); - } ); - const mockDispatchFn = jest.fn(); - jest.mock( '@wordpress/data/src/components/use-dispatch', () => ( { - useDispatch: () => ( { - editPost: jest.fn(), - resetEditorBlocks: mockDispatchFn, - } ), - } ) ); - - const textarea = wrapper.root.findByType( Textarea ); - act( () => textarea.props.onChange( { target: { value: 'text' } } ) ); - setTimeout( () => { - expect( mockDispatchFn ).toHaveBeenCalled(); - }, DEBOUNCE_TIME ); - } ); } );