fix:shapaes bidings error

This commit is contained in:
DiamondThree
2022-08-18 18:22:52 +08:00
parent 5d32d656d5
commit 4724aee96e
5 changed files with 132 additions and 42 deletions
@@ -9,24 +9,45 @@ export const useShapes = (workspace: string, rootBlockId: string) => {
const { pageClientWidth } = usePageClientWidth();
// page padding left and right total 300px
const editorShapeInitSize = pageClientWidth - 300;
const [blocks, setBlocks] = useState<ReturnEditorBlock[]>();
const [blocks, setBlocks] = useState<{
shapes: [ReturnEditorBlock[]];
bindings: string;
}>();
useEffect(() => {
services.api.editorBlock
.get({ workspace, ids: [rootBlockId] })
.then(async blockData => {
const shapes = await Promise.all(
(blockData?.[0]?.children || []).map(async childId => {
const childBlock = (
await services.api.editorBlock.get({
workspace,
ids: [childId],
})
)?.[0];
return childBlock;
})
);
setBlocks(shapes);
});
Promise.all([
services.api.editorBlock
.get({ workspace, ids: [rootBlockId] })
.then(async blockData => {
const shapes = await Promise.all(
(blockData?.[0]?.children || []).map(async childId => {
const childBlock = (
await services.api.editorBlock.get({
workspace,
ids: [childId],
})
)?.[0];
return childBlock;
})
);
return shapes;
// setBlocks(shapes);
}),
]).then(shapes => {
console.log(shapes);
services.api.editorBlock
.get({
workspace: workspace,
ids: [rootBlockId],
})
.then(blcoks => {
setBlocks({
shapes,
bindings: blcoks[0].properties.bindings?.value,
});
// setBindings(blcoks[0].properties.bindings?.value);
});
});
let unobserve: () => void;
services.api.editorBlock
.observe({ workspace, id: rootBlockId }, async blockData => {
@@ -40,8 +61,23 @@ export const useShapes = (workspace: string, rootBlockId: string) => {
)?.[0];
return childBlock;
})
);
setBlocks(shapes);
).then(shapes => {
console.log(shapes);
services.api.editorBlock
.get({
workspace: workspace,
ids: [rootBlockId],
})
.then(blcoks => {
setBlocks({
shapes: [shapes],
bindings: blcoks[0].properties.bindings?.value,
});
// setBindings(blcoks[0].properties.bindings?.value);
});
});
return shapes;
// setBlocks(shapes);
})
.then(cb => {
unobserve = cb;
@@ -53,8 +89,7 @@ export const useShapes = (workspace: string, rootBlockId: string) => {
}, [workspace, rootBlockId]);
let groupCount = 0;
return blocks?.reduce((acc, block) => {
let blocksShapes = blocks?.shapes[0]?.reduce((acc, block) => {
const shapeProps = block.properties.shapeProps?.value
? JSON.parse(block.properties.shapeProps.value)
: {};
@@ -75,4 +110,24 @@ export const useShapes = (workspace: string, rootBlockId: string) => {
return acc;
}, {} as Record<string, TDShape>);
return {
shapes: blocksShapes,
bindings: JSON.parse(blocks?.bindings ?? '{}'),
};
};
export const useBindings = (workspace: string, rootBlockId: string) => {
const [bindings, setBindings] = useState<string>();
useEffect(() => {
services.api.editorBlock
.get({
workspace: workspace,
ids: [rootBlockId],
})
.then(blcoks => {
setBindings(blcoks[0].properties.bindings?.value);
});
return () => {};
}, [workspace, rootBlockId]);
return bindings ? JSON.parse(bindings) : {};
};