← All changes
|
src/Agent/workflows/block-selector/tools/update-block-content.js
+13
-1
3.0.4
→
trunk
View file →
| @@ -1,8 +1,11 @@ | ||
| 1 | +import { healBlockMarkup } from '@agent/lib/heal-blocks'; | |
| 2 | +import { ensureCoreBlocksRegistered } from '@agent/lib/register-blocks'; | |
| 1 | 3 | import apiFetch from '@wordpress/api-fetch'; |
| 2 | 4 | |
| 3 | 5 | // TODO: check for post_lock and error that someone is editing |
| 4 | 6 | export default async ({ previousContent, newContent }) => { |
| 7 | + await ensureCoreBlocksRegistered(); | |
| 5 | 8 | const { postType, postId } = window.extAgentData.context; |
| 6 | 9 | |
| 7 | 10 | const type = await apiFetch({ |
| 8 | 11 | path: `/wp/v2/types/${encodeURIComponent(postType)}`, |
| @@ -12,9 +15,18 @@ | ||
| 12 | 15 | const response = await apiFetch({ |
| 13 | 16 | path: `/wp/v2/${base}/${postId}?context=edit`, |
| 14 | 17 | }); |
| 15 | 18 | |
| 16 | - const content = response.content.raw.replace(previousContent, newContent); | |
| 19 | + const content = response.content.raw.replace( | |
| 20 | + previousContent, | |
| 21 | + healBlockMarkup(newContent), | |
| 22 | + ); | |
| 23 | + | |
| 24 | + // A string-replace that matched nothing writes the post back unchanged; | |
| 25 | + // refuse so the user sees a retry prompt instead of a false success. | |
| 26 | + if (content === response.content.raw) { | |
| 27 | + return { refused: true, reason: 'no-op' }; | |
| 28 | + } | |
| 17 | 29 | |
| 18 | 30 | await apiFetch({ |
| 19 | 31 | path: `/wp/v2/${base}/${postId}`, |
| 20 | 32 | method: 'POST', |