Fix enter and copy past behaviour not respects allowedBlocks and child blocks restrictions #7230
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes part of #6569
Part of a general polishing to get #6993.
When pasting or pressing enter the allowedBlocks property was not enforced.
E.g., pressing enter most of the times created a paragraph even if the paragraph block was not allowed.
This PR makes two main changes:
In onInsertBlocks, we now verify if it is possible to insert the blocks passed as the argument. Not allowed blocks are ignored. This change handles pasting unallowed blocks and makes pressing enter on placeholders and similar areas not create a paragraph block if the paragraph is not allowed.
onSplit now checks if the split can occur (a block of a given type e.g: a paragraph can be inserted) if the division cannot happen we try to add the pasted blocks without splitting the original block.
Test block: https://gist.github.com/jorgefilipecosta/be3c487d10af57838f90e3227225c6d7 allows nesting of lists and quotes. Contains a template that inserts other blocks so we can test.
Child blocks test:
https://gist.github.com/noisysocks/9b9503bd6489ffa51088d35c7a2a8ba0
How has this been tested?
I used the block in the gist and verified that if I press enter on the placeholder inside the block, a paragraph block is not created, because the paragraph is not supported.
I verified that's it is not possible to split paragraphs and headings by pressing enter (another paragraph block cannot be inserted).
I added an image and a quote outside the block, multi-selected them and copied (command +c ). "Pasted" the content in the heading, paragraph, and list inside the test block. In all the cases the image was ignored, and the quote added. In the paragraph and heading because the split was not possible the quote was pasted after the blocks. In the list, the split was possible (because we can insert another list) so the list block was divided, and the quote was added in the middle.
I added a Product block (from Child blocks test), with two Buy buttons inside, multi-selected them and copied. I checked that they could not be pasted in paragraphs outside the Product block.
In master, it was always possible to split unless a template lock existed. And it was always possible to paste blocks even if they were not allowed in the place they are pasted.