Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit Post: Prevent rendering self also when the wrapper block matches the inserted block #31592

Merged
merged 2 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,19 @@ function addToBlockType( renderedBlocks, blockName, uniqueId ) {
* tree. Blocks susceptible to recursion can use this hook in their `Edit`
* function to prevent said recursion.
*
* @param {*} uniqueId Any value that acts as a unique identifier for a block instance.
* @param {*} uniqueId Any value that acts as a unique identifier for a block instance.
* @param {string} blockName Optional block name.
*
* @return {[boolean, Function]} A tuple of:
* - a boolean describing whether the provided id
* has already been rendered;
* - a React context provider to be used to wrap
* other elements.
*/
export default function useNoRecursiveRenders( uniqueId ) {
export default function useNoRecursiveRenders( uniqueId, blockName = '' ) {
const previouslyRenderedBlocks = useContext( RenderedRefsContext );
const { name: blockName } = useBlockEditContext();
const { name } = useBlockEditContext();
blockName = blockName || name;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a nice idea to cover it with a unit test. I will explore options next week.

const hasAlreadyRendered = Boolean(
previouslyRenderedBlocks[ blockName ]?.has( uniqueId )
);
Expand Down
48 changes: 34 additions & 14 deletions packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { motion, AnimatePresence } from 'framer-motion';
import {
VisualEditorGlobalKeyboardShortcuts,
PostTitle,
store as editorStore,
} from '@wordpress/editor';
import {
WritingFlow,
Expand All @@ -28,6 +29,7 @@ import {
__experimentalLayoutStyle as LayoutStyle,
__unstableUseMouseMoveTypingReset as useMouseMoveTypingReset,
__unstableIframe as Iframe,
__experimentalUseNoRecursiveRenders as useNoRecursiveRenders,
} from '@wordpress/block-editor';
import { useRef } from '@wordpress/element';
import { Button } from '@wordpress/components';
Expand Down Expand Up @@ -80,14 +82,25 @@ function MaybeIframe( {
}

export default function VisualEditor( { styles } ) {
const { deviceType, isTemplateMode } = useSelect( ( select ) => {
const {
deviceType,
isTemplateMode,
wrapperBlockName,
wrapperUniqueId,
} = useSelect( ( select ) => {
const {
isEditingTemplate,
__experimentalGetPreviewDeviceType,
} = select( editPostStore );
const { getCurrentPostId, getCurrentPostType } = select( editorStore );
return {
deviceType: __experimentalGetPreviewDeviceType(),
isTemplateMode: isEditingTemplate(),
wrapperBlockName:
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
getCurrentPostType() === 'wp_block'
? 'core/block'
: 'core/post-content',
wrapperUniqueId: getCurrentPostId(),
};
}, [] );
const hasMetaBoxes = useSelect(
Expand Down Expand Up @@ -148,6 +161,11 @@ export default function VisualEditor( { styles } ) {

const blockSelectionClearerRef = useBlockSelectionClearer( true );

const [ , RecursionProvider ] = useNoRecursiveRenders(
wrapperUniqueId,
wrapperBlockName
);

return (
<motion.div
className={ classnames( 'edit-post-visual-editor', {
Expand Down Expand Up @@ -200,19 +218,21 @@ export default function VisualEditor( { styles } ) {
<PostTitle />
</div>
) }
<BlockList
__experimentalLayout={
themeSupportsLayout
? {
type: 'default',
// Find a way to inject this in the support flag code (hooks).
alignments: themeSupportsLayout
? alignments
: undefined,
}
: undefined
}
/>
<RecursionProvider>
<BlockList
__experimentalLayout={
themeSupportsLayout
? {
type: 'default',
// Find a way to inject this in the support flag code (hooks).
alignments: themeSupportsLayout
? alignments
: undefined,
}
: undefined
}
/>
</RecursionProvider>
</WritingFlow>
</motion.div>
</AnimatePresence>
Expand Down