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

Code Editor: Revert save editors value on change (#27717) #30524

Merged
merged 1 commit into from
Apr 6, 2021
Merged
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
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 );
} );
} );