mirror of
https://github.com/WenPai-org/wpmind.git
synced 2025-08-03 19:19:46 +08:00
better handling nested block attributes and innerBlocks
This commit is contained in:
parent
cf1d2c7c65
commit
b2c3fbf927
1 changed files with 21 additions and 24 deletions
|
@ -109,29 +109,7 @@ export default class BlocksStreamProcessor {
|
||||||
const parsed = JSON.parse(completedJson);
|
const parsed = JSON.parse(completedJson);
|
||||||
|
|
||||||
if (Array.isArray(parsed) && parsed.length > 0) {
|
if (Array.isArray(parsed) && parsed.length > 0) {
|
||||||
const contentMatches = [
|
const transformedBlocks = parsed
|
||||||
...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
|
|
||||||
.map((block) => this.transformToBlock(block))
|
.map((block) => this.transformToBlock(block))
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
||||||
|
@ -326,9 +304,14 @@ export default class BlocksStreamProcessor {
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
|
const attributes = blockData.attributes || {};
|
||||||
|
|
||||||
|
// Recursively process attributes
|
||||||
|
const processedAttributes = this.processAttributes(attributes);
|
||||||
|
|
||||||
return createBlock(
|
return createBlock(
|
||||||
blockData.name,
|
blockData.name,
|
||||||
blockData.attributes || {},
|
processedAttributes,
|
||||||
innerBlocks
|
innerBlocks
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} 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) {
|
async dispatchBlocks(blocks, isFinal = false) {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const timeSinceLastUpdate = now - this.lastUpdate;
|
const timeSinceLastUpdate = now - this.lastUpdate;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue