@@ -24,7 +24,7 @@ import {
2424 useTranslation ,
2525} from '@payloadcms/ui'
2626import { abortAndIgnore } from '@payloadcms/ui/shared'
27- import { $getNodeByKey } from 'lexical'
27+ import { $getNodeByKey , SKIP_DOM_SELECTION_TAG } from 'lexical'
2828import {
2929 type BlocksFieldClient ,
3030 type ClientBlock ,
@@ -269,15 +269,20 @@ export const BlockComponent: React.FC<BlockComponentProps> = (props) => {
269269 ) as BlockFields
270270
271271 // Things like default values may come back from the server => update the node with the new data
272- editor . update ( ( ) => {
273- const node = $getNodeByKey ( nodeKey )
274- if ( node && $isBlockNode ( node ) ) {
275- const newData = newFormStateData
276- newData . blockType = blockType
277-
278- node . setFields ( newData , true )
279- }
280- } )
272+ editor . update (
273+ ( ) => {
274+ const node = $getNodeByKey ( nodeKey )
275+ if ( node && $isBlockNode ( node ) ) {
276+ const newData = newFormStateData
277+ newData . blockType = blockType
278+
279+ node . setFields ( newData , true )
280+ }
281+ } ,
282+ // Without this, the outer editor's reconciler resets DOM selection
283+ // back into its own root, kicking focus out of any nested richText.
284+ { tag : SKIP_DOM_SELECTION_TAG } ,
285+ )
281286
282287 setInitialState ( state )
283288 if ( ! CustomLabelFromProps ) {
@@ -377,14 +382,19 @@ export const BlockComponent: React.FC<BlockComponentProps> = (props) => {
377382 ) as BlockFields
378383
379384 setTimeout ( ( ) => {
380- editor . update ( ( ) => {
381- const node = $getNodeByKey ( nodeKey )
382- if ( node && $isBlockNode ( node ) ) {
383- const newData = newFormStateData
384- newData . blockType = blockType
385- node . setFields ( newData , true )
386- }
387- } )
385+ editor . update (
386+ ( ) => {
387+ const node = $getNodeByKey ( nodeKey )
388+ if ( node && $isBlockNode ( node ) ) {
389+ const newData = newFormStateData
390+ newData . blockType = blockType
391+ node . setFields ( newData , true )
392+ }
393+ } ,
394+ // Without this, the outer editor's reconciler resets DOM selection
395+ // back into its own root, kicking focus out of any nested richText.
396+ { tag : SKIP_DOM_SELECTION_TAG } ,
397+ )
388398 } , 0 )
389399
390400 if ( submit ) {
@@ -677,12 +687,17 @@ export const BlockComponent: React.FC<BlockComponentProps> = (props) => {
677687 onSubmit = { ( formState , newData ) => {
678688 // This is only called when form is submitted from drawer - usually only the case if the block has a custom Block component
679689 newData . blockType = blockType
680- editor . update ( ( ) => {
681- const node = $getNodeByKey ( nodeKey )
682- if ( node && $isBlockNode ( node ) ) {
683- node . setFields ( newData as BlockFields , true )
684- }
685- } )
690+ editor . update (
691+ ( ) => {
692+ const node = $getNodeByKey ( nodeKey )
693+ if ( node && $isBlockNode ( node ) ) {
694+ node . setFields ( newData as BlockFields , true )
695+ }
696+ } ,
697+ // Without this, the outer editor's reconciler resets DOM selection
698+ // back into its own root, kicking focus out of any nested richText.
699+ { tag : SKIP_DOM_SELECTION_TAG } ,
700+ )
686701 toggleDrawer ( )
687702 } }
688703 submitted = { submitted }
0 commit comments