better handling nested block attributes and innerBlocks

This commit is contained in:
Nikita 2024-12-11 19:09:05 +03:00
parent cf1d2c7c65
commit b2c3fbf927

View file

@ -109,29 +109,7 @@ export default class BlocksStreamProcessor {
const parsed = JSON.parse(completedJson);
if (Array.isArray(parsed) && parsed.length > 0) {
const contentMatches = [
...jsonContent.matchAll(
/"content"\s*:\s*"([^"]*)(?:[^"]*)?/g
),
];
const updatedBlocks = parsed.map((block, index) => {
if (
block.name === 'core/paragraph' &&
contentMatches[index]
) {
return {
...block,
attributes: {
...block.attributes,
content: contentMatches[index][1],
},
};
}
return block;
});
const transformedBlocks = updatedBlocks
const transformedBlocks = parsed
.map((block) => this.transformToBlock(block))
.filter(Boolean);
@ -326,9 +304,14 @@ export default class BlocksStreamProcessor {
.filter(Boolean)
: [];
const attributes = blockData.attributes || {};
// Recursively process attributes
const processedAttributes = this.processAttributes(attributes);
return createBlock(
blockData.name,
blockData.attributes || {},
processedAttributes,
innerBlocks
);
} catch (error) {
@ -337,6 +320,20 @@ export default class BlocksStreamProcessor {
}
}
processAttributes(attributes) {
const processedAttributes = {};
for (const [key, value] of Object.entries(attributes)) {
if (typeof value === 'object' && value !== null) {
processedAttributes[key] = this.processAttributes(value);
} else {
processedAttributes[key] = value;
}
}
return processedAttributes;
}
async dispatchBlocks(blocks, isFinal = false) {
const now = Date.now();
const timeSinceLastUpdate = now - this.lastUpdate;