-
Notifications
You must be signed in to change notification settings - Fork 382
Description
Bug Description
In the stories editor, there when a block is a selected, a context menu icon appears in the toolbar that looks like this.
If a CTA block is selected and the duplicate block is selected, the browser becomes unresponsive and sometimes crashes.
There might be some infinite loop, perhaps caused by some faulty React hook somewhere.
Expected Behaviour
Block should fail to duplicate or at least a warning about duplicate CTA blocks is shown.
Steps to reproduce
- Create new story in editor
- Create a second page
- Insert CTA block.
- Select CTA block.
- Click three dots in top toolbar.
- Select duplicate block.
Screenshots
If the browser doesn't crash, then two CTA blocks are shown.
Additional context
- WordPress version: 5.3
- Plugin version: 1.3
- Gutenberg plugin version (if applicable):
- AMP plugin template mode:
- PHP version:
- OS: Ubuntu 18.04
- Browser: Chrome 77.0.3865.75 (Official Build) (64-bit)
- Device: Dell XPS 13
Acceptance criteria
- AC1: Duplicating a CTA block should not cause the browser to lag / crash
- AC2: Duplicating a Page Attachment block should not cause the browser to lag / crash
Implementation brief
Reason for crashing
When either a CTA or a Page Attachment block is present on a Page, enforcing the block to be the last block on the Page is triggered. If there are more than one CTA / Page Attachment blocks then this causes the logic to be triggered infinitely since only one block can be the last block:
amp-wp/assets/src/stories-editor/blocks/amp-story-page/edit.js
Lines 145 to 166 in 744b640
| useEffect( () => { | |
| if ( childrenOrder.length <= 1 ) { | |
| return; | |
| } | |
| const ctaBlock = getCallToActionBlock( clientId ); | |
| const attachmentBlock = getPageAttachmentBlock( clientId ); | |
| let blockToMove = null; | |
| if ( ctaBlock ) { | |
| blockToMove = ctaBlock; | |
| } else if ( attachmentBlock ) { | |
| blockToMove = attachmentBlock; | |
| } | |
| if ( blockToMove ) { | |
| // If the either CTA or Attachment is not the last block, move it there. | |
| if ( childrenOrder[ childrenOrder.length - 1 ] !== blockToMove.clientId ) { | |
| moveBlockToPosition( blockToMove.clientId, clientId, clientId, childrenOrder.length - 1 ); | |
| } | |
| } | |
| }, [ childrenOrder, clientId, moveBlockToPosition ] ); |
Suggested solution
If more than one CTA/Page Attachment blocks are detected then before enforcing the block to be the last, two things should happen:
- The extra block(s) should be removed.
- A snackbar notice is displayed to let know about the block being removed.
We could add an additional condition to the same useEffect hook that enforces the CTA/Attachment block to be the last, to only run if there is only one CTA or one Page Attachment block. Additionally, we can add another useEffect which removes the extra block(s) if detected which can also display a snackbar notice saying that the block was removed since each Page can have only 1 CTA block or 1 Attachment block.
QA testing instructions
- Create a new story
- Add a new page
- Add a CTA block
- In the block toolbar, click on the three dots and then on "Duplicate"
-> Verify that block is not duplicated, but a warning message appears in the bottom left. - Add a new page
- Add a Page Attachment block
- In the block toolbar, click on the three dots and then on "Duplicate"
-> Verify that block is not duplicated, but a warning message appears in the bottom left.
Demo
https://www.youtube.com/watch?v=iItTJkcF7Qg&feature=youtu.be
Changelog entry
- Prevent duplicating CTA or Page Attachment blocks.

