From cac83e24b60f5e6ae43d870902447acaaafb2dbb Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 29 Apr 2022 16:03:44 +0400 Subject: [PATCH] Text Editor: Avoid content loss --- .../editor/src/components/post-text-editor/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/components/post-text-editor/index.js b/packages/editor/src/components/post-text-editor/index.js index 55bcb7228bf01..50aadbdaf2cc1 100644 --- a/packages/editor/src/components/post-text-editor/index.js +++ b/packages/editor/src/components/post-text-editor/index.js @@ -7,7 +7,7 @@ import Textarea from 'react-autosize-textarea'; * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { useState } from '@wordpress/element'; +import { useEffect, useState } from '@wordpress/element'; import { parse } from '@wordpress/blocks'; import { useDispatch, useSelect } from '@wordpress/data'; import { useInstanceId } from '@wordpress/compose'; @@ -65,6 +65,16 @@ export default function PostTextEditor() { } }; + // Ensure changes aren't lost when the component unmounts + useEffect( () => { + return () => { + if ( isDirty ) { + const blocks = parse( value ); + resetEditorBlocks( blocks ); + } + }; + }, [ isDirty, value ] ); + return ( <>