| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
import { addQueryArgs } from '@wordpress/url'; |
| 3 |
|
| 4 |
// Shared so the tool and DOMHighlighter build the same query args and can't drift. |
| 5 |
export const blockCodeQueryArgs = (block, postId) => { |
| 6 |
if (!block?.id) return null; |
| 7 |
if (block.source?.kind === 'template-part') { |
| 8 |
const { partSlug } = block.source; |
| 9 |
return partSlug ? { blockId: String(block.id), partSlug } : null; |
| 10 |
} |
| 11 |
return postId ? { blockId: String(block.id), postId: String(postId) } : null; |
| 12 |
}; |
| 13 |
|
| 14 |
// One block's current serialized code by TagBlocks id, scoped to the container's source. |
| 15 |
export const fetchBlockCodeById = async (blockId, source, postId) => { |
| 16 |
const args = blockCodeQueryArgs({ id: blockId, source }, postId); |
| 17 |
if (!args) return ''; |
| 18 |
const response = await apiFetch({ |
| 19 |
path: addQueryArgs('/extendify/v1/agent/get-block-code', args), |
| 20 |
}); |
| 21 |
return response?.block ?? ''; |
| 22 |
}; |
| 23 |
|