Skip to content

Commit

Permalink
revert #27717 (#30524)
Browse files Browse the repository at this point in the history
Co-authored-by: grzim <[email protected]>
  • Loading branch information
grzim and grzim authored Apr 6, 2021
1 parent b28dc9e commit ea2f6f8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 37 deletions.
18 changes: 3 additions & 15 deletions packages/editor/src/components/post-text-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand All @@ -67,7 +54,8 @@ export default function PostTextEditor() {
*/
const stopEditing = () => {
if ( isDirty ) {
saveText();
const blocks = parse( value );
resetEditorBlocks( blocks );
setIsDirty( false );
}
};
Expand Down
24 changes: 2 additions & 22 deletions packages/editor/src/components/post-text-editor/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) => (
Expand Down Expand Up @@ -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( <PostTextEditor /> );
} );
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 );
} );
} );

0 comments on commit ea2f6f8

Please sign in to comment.