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

Question about batch refreshing block content #12625

Closed
nosro opened this issue Dec 5, 2018 · 2 comments
Closed

Question about batch refreshing block content #12625

nosro opened this issue Dec 5, 2018 · 2 comments
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@nosro
Copy link

nosro commented Dec 5, 2018

Description
I am updating all blocks on a post programmatically. I am replacing certain patterns of text with link elements.

This is a sidebar plugin which has a button to trigger this manipulation.

I have successfully transformed the content via an "editPost" method. Along the lines of:

dispatch('core/editor').editPost({content: myNewContent);

This partially works. When I save the post manually and reload it, my new content appears. However, until I do that, the current editor version is not updated. If I make edits, those trample over the transformed "content" value I had set.

I have tried dispatching the "refreshPost" action, but that does not seem to do it. It seems to me that some cached value is not being cleared and refreshed.

Gutenberg Version 4.6.1

@designsimply designsimply added [Type] Help Request Help with setup, implementation, or "How do I?" questions. Needs Technical Feedback Needs testing from a developer perspective. labels Dec 5, 2018
@mkania
Copy link

mkania commented Dec 12, 2018

I was having the same issue and "solved" it by doing this, which is essentially generating a new set of blocks from our modified content and replacing the existing blocks with our new blocks, which updates the editor interface appropriately:

var blocks = wp.blocks.pasteHandler( { HTML: myNewContent, mode: 'BLOCKS' } ); wp.data.dispatch('core/editor').resetBlocks(blocks);

Not sure if that's the ideal or correct way to do it, but it served my purposes at least.

@nosro
Copy link
Author

nosro commented Dec 30, 2018

@mkania Thank you for that tip. It led me to a good solution. I can offer a slight variation on your technique. Instead of using the wp.blocks.pasteHandler I am taking my current post content, modifying it as I wish, then re-parsing the blocks, like this:

const newContent = wp.data.select('core/editor').getEditedPostAttribute('content');

... [manipulate content string as you please ]...

const blocks = wp.blocks.parse(newContent);

wp.data.dispatch('core/editor').resetBlocks(blocks);

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. [Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

3 participants