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

Fix edited Classic block's content deletion when switching to Code editor #23927

Merged
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
29 changes: 16 additions & 13 deletions packages/block-library/src/classic/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,22 @@ export default class ClassicEdit extends Component {
bookmark = null;
} );

editor.on(
'Paste Change input Undo Redo',
debounce( () => {
const value = editor.getContent();

if ( value !== editor._lastChange ) {
editor._lastChange = value;
setAttributes( {
content: value,
} );
}
}, 250 )
);
const debouncedOnChange = debounce( () => {
const value = editor.getContent();

if ( value !== editor._lastChange ) {
editor._lastChange = value;
setAttributes( {
content: value,
} );
}
}, 250 );
editor.on( 'Paste Change input Undo Redo', debouncedOnChange );

// We need to cancel the debounce call because when we remove
// the editor (onUnmount) this callback is executed in
// another tick. This results in setting the content to empty.
editor.on( 'remove', debouncedOnChange.cancel );

editor.on( 'keydown', ( event ) => {
if ( isKeyboardEvent.primary( event, 'z' ) ) {
Expand Down