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

Parent Block Replaced by Text Intended for Child Block #67356

Open
3 of 6 tasks
zhyian opened this issue Nov 27, 2024 · 5 comments
Open
3 of 6 tasks

Parent Block Replaced by Text Intended for Child Block #67356

zhyian opened this issue Nov 27, 2024 · 5 comments
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Package] Block editor /packages/block-editor [Status] Needs More Info Follow-up required in order to be actionable. [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. [Type] Bug An existing feature does not function as intended

Comments

@zhyian
Copy link

zhyian commented Nov 27, 2024

Description

When using the BlockEditorProvider and BlockList components in a custom block, pasting copied text into a specific block within the BlockList unexpectedly replaces the entire parent block instead of inserting the text into the intended block. This disrupts the block hierarchy and content structure.

Using the template lock prevents copy-pasting from working entirely, as it stops the replacement of locked parent blocks.

Notably, this issue only occurs when pasting text from outside the editor, for example from the sidebar. When copying text from within the editor, such as from a paragraph block, the behavior works as expected.

Step-by-step reproduction instructions

Here’s a simple block example to help reproduce the issue:

import { registerBlockType } from '@wordpress/blocks';
import metadata from './block.json';

import { BlockEditorProvider, BlockList, WritingFlow } from '@wordpress/block-editor';
import { parse } from '@wordpress/blocks';
import { useState } from '@wordpress/element';

registerBlockType( metadata.name, {
	edit: () => {
		const content = '<!-- wp:paragraph --><p>Test!</p> <!-- /wp:paragraph -->'
		const [blocks, setBlocks] = useState(parse(content));
		const handleBlocksChange = (newBlocks) => {
			setBlocks(newBlocks);
		};

		return (
			<BlockEditorProvider
				value={blocks}
				onInput={handleBlocksChange}
				onChange={handleBlocksChange}
			>
				<WritingFlow>
					<BlockList />
				</WritingFlow>
			</BlockEditorProvider>
		);
	}
} );

Steps:

  1. Download a plugin with provided block: https://github.com/zhyian/todo-list
  2. Activate it on clear WordPress installation.
  3. Create a new page and insert Todo List block
  4. Copy text not from the block editor, for example any text from the sidebar.
  5. Select a Todo Block in the Document Overview and set a cursor inside any of inner blocks.
  6. Paste the text and you will see that whole Todo Block is replaced by text.

Screenshots, screen recording, code snippet

Image

Environment info

The issue can be reproduced starting from WordPress 6.6. On WordPress 6.5.5, the problem does not occur.
Active theme: Twenty Twenty-Four.
No plugins are active, except for the custom block created for this example.

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
@zhyian zhyian added the [Type] Bug An existing feature does not function as intended label Nov 27, 2024
@karmatosed karmatosed added the Needs Testing Needs further testing to be confirmed. label Dec 4, 2024
@ellatrix
Copy link
Member

ellatrix commented Dec 23, 2024

@zhyian I'm not sure BlockEditorProvider is the right approach. Could you try a controlled useInnerBlocksProps hooks with the value and onChange props? This is what the core blocks such as core/block (reusable block) and core/post-content use. You should also remove the WritingFlow wrapper.

@zhyian
Copy link
Author

zhyian commented Dec 31, 2024

Thanks so much for the suggestion, @ellatrix! In this example, it would definitely work, but this is just a simplified case to reproduce the issue. In our real-world scenario, the block includes multiple fields (essentially small custom editors), so using useInnerBlocksProps doesn’t seem practical since only one component with inner blocks can be used within a block.

@Mamaduka Mamaduka added the [Package] Block editor /packages/block-editor label Jan 22, 2025
@Mamaduka
Copy link
Member

Hi, @zhyian

Can you share additional details about what your custom implementation is trying to achieve that's missing from useInnerBlocksProps?

Does removing the WritingFlow wrapper have any effect on the issue?

@Mamaduka Mamaduka added [Status] Needs More Info Follow-up required in order to be actionable. Needs Technical Feedback Needs testing from a developer perspective. and removed Needs Testing Needs further testing to be confirmed. labels Jan 22, 2025
Copy link

github-actions bot commented Feb 7, 2025

Help us move this issue forward. This issue is being marked stale since it has no activity after 15 days of requesting more information. Please add info requested so we can help move the issue forward. Note: The triage policy is to close stale issues that need more info and no response after 2 weeks.

@github-actions github-actions bot added the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Feb 7, 2025
@zhyian
Copy link
Author

zhyian commented Feb 25, 2025

Hi @Mamaduka,

Does removing the WritingFlow wrapper have any effect on the issue?

Unfortunately, no.

Can you share additional details about what your custom implementation is trying to achieve that's missing from useInnerBlocksProps?

Let's assume we have a custom block with multiple fields, where each field acts as a small custom editor with its own set of inner blocks. Each custom editor stores its inner blocks in a specific post meta. As far as I know, there’s no way to store multiple sets of inner blocks directly within our custom block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Package] Block editor /packages/block-editor [Status] Needs More Info Follow-up required in order to be actionable. [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

4 participants