Merge branch 'dev' into 'main'

cs fixed zoom & fit graph to root node

See merge request frontend/squiz!103
This commit is contained in:
Nastya 2023-12-31 12:19:46 +00:00
commit 278c068ebc
2 changed files with 181 additions and 167 deletions

@ -95,6 +95,23 @@ function CsComponent({
gearsContainer, gearsContainer,
}); });
function fitGraphToRootNode() {
const cy = cyRef.current;
if (!cy) return;
const rootNode = cy.nodes().filter((n) => n.data("root"))[0];
if (!rootNode) throw new Error("Root node not found");
const height = cy.height();
const position = rootNode.position();
const shift = rootNode.width() / 2;
cy.pan({
x: position.x + shift,
y: position.y + height / 2,
});
}
useLayoutEffect(() => { useLayoutEffect(() => {
const cy = cyRef?.current; const cy = cyRef?.current;
if (desireToOpenABranchingModal) { if (desireToOpenABranchingModal) {
@ -301,7 +318,7 @@ function CsComponent({
// cy.data('changed', true) // cy.data('changed', true)
const elecs = eles?.layout(layoutOptions).run(); const elecs = eles?.layout(layoutOptions).run();
cy?.on("add", () => cy.data("changed", true)); cy?.on("add", () => cy.data("changed", true));
cy?.fit(); fitGraphToRootNode();
//cy?.layout().run() //cy?.layout().run()
firstRender.current = false; firstRender.current = false;
} }
@ -335,9 +352,7 @@ function CsComponent({
fontSize: "16px", fontSize: "16px",
}} }}
variant="text" variant="text"
onClick={() => { onClick={fitGraphToRootNode}
cyRef.current?.fit();
}}
> >
Выровнять Выровнять
</Button> </Button>
@ -358,6 +373,8 @@ function CsComponent({
cyRef.current = cy; cyRef.current = cy;
}} }}
autoungrabify={true} autoungrabify={true}
zoom={0.6}
zoomingEnabled={false}
/> />
<DeleteNodeModal removeNode={removeNode} /> <DeleteNodeModal removeNode={removeNode} />
</> </>

@ -104,17 +104,12 @@ export const usePopper = ({
container.append(layoutsContainer.current); container.append(layoutsContainer.current);
} }
const ext = cy.extent();
const nodesInView = cy.nodes().filter((n) => {
const bb = n.boundingBox();
return (
bb.x2 > ext.x1 && bb.x1 < ext.x2 && bb.y2 > ext.y1 && bb.y1 < ext.y2
);
});
cy?.removeAllListeners(); cy?.removeAllListeners();
nodesInView.toArray()?.forEach((item) => { cy
.nodes()
.toArray()
?.forEach((item) => {
const node = item as NodeSingularWithPopper; const node = item as NodeSingularWithPopper;
const layoutsPopper = node.popper({ const layoutsPopper = node.popper({
@ -203,7 +198,9 @@ export const usePopper = ({
let gearsPopper: Popper | null = null; let gearsPopper: Popper | null = null;
if (node.data().root !== true) { if (node.data().root !== true) {
console.log(node.data("parentType")); console.log(node.data("parentType"));
const parentQuestion = getQuestionByContentId(node.data("parentType")); const parentQuestion = getQuestionByContentId(
node.data("parentType"),
);
gearsPopper = node.popper({ gearsPopper = node.popper({
popper: { popper: {
@ -253,8 +250,7 @@ export const usePopper = ({
await layoutsPopper.update(); await layoutsPopper.update();
}; };
const onZoom = (event: AbstractEventObject) => { const zoom = cy.zoom();
const zoom = event.cy.zoom();
//update(); //update();
@ -319,7 +315,6 @@ export const usePopper = ({
element.style.width = `${60 * zoom}px`; element.style.width = `${60 * zoom}px`;
element.style.height = `${40 * zoom}px`; element.style.height = `${40 * zoom}px`;
}); });
};
//node?.on("position", update); //node?.on("position", update);
let pressed = false; let pressed = false;
@ -356,7 +351,9 @@ export const usePopper = ({
} }
}); });
cy?.on("zoom render", onZoom); cy.on("render", () => {
update();
});
}); });
}; };