@@ -34,6 +34,7 @@ import {
3434} from '@wordpress/components'
3535import { useBlockProps } from '@wordpress/block-editor'
3636import apiFetch from '@wordpress/api-fetch'
37+
3738const convertBlocksToArray = block => {
3839 const innerBlocks = block . innerBlocks . map ( innerBlock => convertBlocksToArray ( innerBlock ) )
3940 return [ block . name , block . attributes , innerBlocks ]
@@ -99,6 +100,7 @@ const Edit = props => {
99100 const disabledBlocksRef = useRef ( [ ] )
100101 const callbackRef = useRef ( null )
101102 const blocksToRemoveRef = useRef ( [ ] )
103+ const insertIndexRef = useRef ( - 1 )
102104
103105 const blockProps = useBlockProps ( {
104106 className : 'ugb-design-library-block' ,
@@ -261,11 +263,23 @@ const Edit = props => {
261263 }
262264 }
263265
264- if ( blocks . length ) {
266+ if ( ! blocks . length ) {
267+ return
268+ }
269+
270+ if ( insertIndexRef . current !== - 1 ) {
271+ dispatch ( 'core/block-editor' ) . insertBlocks ( blocks , insertIndexRef . current )
272+ } else {
265273 dispatch ( 'core/block-editor' ) . replaceBlocks ( clientId , blocks )
266- if ( callbackRef . current ) {
267- callbackRef . current ( )
268- }
274+ }
275+
276+ if ( blocksToRemoveRef . current . length ) {
277+ dispatch ( 'core/block-editor' ) . removeBlocks ( blocksToRemoveRef . current )
278+ blocksToRemoveRef . current = [ ]
279+ }
280+
281+ if ( callbackRef . current ) {
282+ callbackRef . current ( )
269283 }
270284 }
271285
@@ -294,18 +308,10 @@ const Edit = props => {
294308 }
295309 }
296310
297- const removeBlocks = ( ) => {
298- if ( blocksToRemoveRef . current . length ) {
299- dispatch ( 'core/block-editor' ) . removeBlocks ( blocksToRemoveRef . current )
300- }
301- }
302-
303311 const onClickSecondary = async ( ) => {
304- removeBlocks ( )
305312 await addDesigns ( false )
306313 }
307314 const onClickPrimary = async ( ) => {
308- removeBlocks ( )
309315 await addDesigns ( true )
310316 }
311317
@@ -363,10 +369,11 @@ const Edit = props => {
363369 callbackRef . current = callback
364370
365371 if ( type === 'pages' ) {
366- const currentBlocks = select ( 'core/block-editor' ) . getBlockOrder ( ) . filter ( id => id !== clientId )
372+ const allBlocks = select ( 'core/block-editor' ) . getBlockOrder ( )
373+ const blocksToRemove = allBlocks . filter ( id => id !== clientId )
367374
368- if ( currentBlocks . length ) {
369- blocksToRemoveRef . current = currentBlocks
375+ if ( blocksToRemove . length ) {
376+ blocksToRemoveRef . current = allBlocks
370377 setIsDialogOpen ( DIALOG_OPTIONS . REMOVE_BLOCKS )
371378 return
372379 }
@@ -394,33 +401,53 @@ const Edit = props => {
394401 { __ ( 'Adding this page design will replace all existing blocks in the editor. Are you sure you want to continue?' , i18n ) }
395402 </ span >
396403 </ div >
397- < Flex justify = "flex-end" >
404+ < Flex direction = "column" align = "flex-end" >
398405 < Button
399406 __next40pxDefaultSize
400- variant = "secondary "
407+ variant = "primary "
401408 onClick = { ( ) => {
402- blocksToRemoveRef . current = [ ]
403- setIsDialogOpen ( DIALOG_OPTIONS . CLOSE )
404- } }
409+ insertIndexRef . current = 0
410+ if ( disabledBlocksRef . current . size ) {
411+ // Close this dialog and reopen after a while to show the notice for disabled blocks
412+ // The existing blocks will be removed later
413+ setIsDialogOpen ( DIALOG_OPTIONS . CLOSE )
414+ setTimeout ( ( ) => setIsDialogOpen ( DIALOG_OPTIONS . DISABLED_BLOCKS ) , 500 )
415+ return
416+ }
417+
418+ addDesigns ( false )
419+ } }
405420 >
406- { __ ( 'Cancel ' , i18n ) }
421+ { __ ( 'Replace existing content with page design ' , i18n ) }
407422 </ Button >
408423 < Button
409424 __next40pxDefaultSize
410- variant = "primary "
425+ variant = "secondary "
411426 onClick = { ( ) => {
427+ insertIndexRef . current = blocksToRemoveRef . current . length
428+ // When appending the page design, only remove the design library block
429+ blocksToRemoveRef . current = [ clientId ]
430+
412431 if ( disabledBlocksRef . current . size ) {
413- // Close this dialog and reopen after a while to show the notice for disabled blocks
414- // The existing blocks will be removed later
415432 setIsDialogOpen ( DIALOG_OPTIONS . CLOSE )
416433 setTimeout ( ( ) => setIsDialogOpen ( DIALOG_OPTIONS . DISABLED_BLOCKS ) , 500 )
417- } else {
418- removeBlocks ( )
419- addDesigns ( false )
434+
435+ return
420436 }
437+ addDesigns ( false )
421438 } }
422439 >
423- { __ ( 'Insert page design' , i18n ) }
440+ { __ ( 'Append page design only' , i18n ) }
441+ </ Button >
442+ < Button
443+ __next40pxDefaultSize
444+ variant = "tertiary"
445+ onClick = { ( ) => {
446+ blocksToRemoveRef . current = [ ]
447+ setIsDialogOpen ( DIALOG_OPTIONS . CLOSE )
448+ } }
449+ >
450+ { __ ( 'Cancel' , i18n ) }
424451 </ Button >
425452 </ Flex >
426453 </ > }
0 commit comments