fix: NaN on add node

This commit is contained in:
skeris 2023-12-03 03:46:58 +03:00
parent 4623dfd2ee
commit 05b52e72a7
2 changed files with 40 additions and 12 deletions

@ -1,7 +1,7 @@
import { MessageIcon } from "@icons/messagIcon";
import { PointsIcon } from "@icons/questionsPage/PointsIcon";
import { DeleteIcon } from "@icons/questionsPage/deleteIcon";
import TextareaAutosize from "@mui/base/TextareaAutosize";
import { TextareaAutosize } from "@mui/base/TextareaAutosize";
import {
Box,
FormControl,

@ -139,6 +139,7 @@ export const CsComponent = ({
if (Object.keys(targetQuestion).length !== 0 && Object.keys(targetQuestion).length !== 0 && parentNodeContentId && parentNodeChildren !== undefined) {
clearDataAfterAddNode({ parentNodeContentId, targetQuestion, parentNodeChildren })
cy?.data('changed',true)
cy?.add([
{
data: {
@ -152,7 +153,8 @@ export const CsComponent = ({
target: targetQuestion.content.id
}
}
]).layout(lyopts).run()
])
cy?.layout(lyopts).run()
} else {
enqueueSnackbar("Добавляемый вопрос не найден")
}
@ -235,6 +237,8 @@ export const CsComponent = ({
cy?.remove(edge)
})
removeButtons(targetNodeContentId)
cy?.data('changed',true)
cy?.layout(lyopts).run()
}
@ -277,10 +281,22 @@ export const CsComponent = ({
const readyLO = (e) => {
console.log(e.cy.data('firstNode'),"SKEEER",e.cy.data('changed'))
if (e.cy.data('firstNode') === 'nonroot') {
e.cy.data('firstNode','root')
e.cy.nodes().sort((a,b) => (a.data('root')?1:-1)).layout(lyopts).run()
} else {
e.cy.data('changed', false)
e.cy.removeData('firstNode')
}
//удаляем иконки
e.cy.nodes().forEach((ele: any) => {
const data = ele.data()
console.log(data)
data.id && removeButtons(data.id);
})
initialPopperIcons(e)
@ -292,29 +308,33 @@ export const CsComponent = ({
positions: (e) => {
console.log('BBBBBBBBBBBBBBB', e.cy().data('changed'))
if (!e.cy().data('changed')) {
console.log(e.cy().data('changed'))
return e.data('oldPos')
} console.log('POSITIIIIIIIONS')
} else {e.removeData('oldPos')}
console.log('POSITIIIIIIIONS')
const id = e.id()
const incomming = e.cy().edges(`[target="${id}"]`)
const layer = 0
e.removeData('lastChild')
if (incomming.length === 0) {
const children = e.cy().edges(`[source="${id}"]`)
if (e.cy().data('firstNode') === undefined)
e.cy().data('firstNode','root')
e.data('root', true)
const children = e.cy().edges(`[source="${id}"]`).targets()
e.data('layer', layer)
e.data('children', children.targets().length)
e.data('children', children.length)
const queue = []
children.forEach(n => {
queue.push({ task: n.target(), layer: layer + 1 })
queue.push({ task: n, layer: layer + 1 })
})
while (queue.length) {
const task = queue.pop()
task.task.data('layer', task.layer)
const children = e.cy().edges(`[source="${task.task.id()}"]`)
task.task.data('children', children.targets().length)
console.log('SAMSHIIIIT',task.layer,task.task.data().layer)
const children = e.cy().edges(`[source="${task.task.id()}"]`).targets()
task.task.data('children', children.length)
if (children.length !== 0) {
children.forEach(n => queue.push({ task: n.target(), layer: task.layer + 1 }))
children.forEach(n => queue.push({ task: n, layer: task.layer + 1 }))
}
}
queue.push({ parent: e, children: children.targets() })
@ -337,10 +357,18 @@ export const CsComponent = ({
task?.parent.data('subtreeWidth', task.children.reduce((p, n) => p + n.data('subtreeWidth'), 0))
}
const pos = { x: 200 * e.data('layer'), y: 0 }
const pos = { x: 0, y: 0 }
console.log(e.data())
e.data('oldPos', pos)
return pos
} else {
console.log(e.cy().data('firstNode'))
if (e.cy().data('firstNode') !== 'root') {
e.cy().data('firstNode','nonroot')
return {x:0,y:0}
}
if (e.cy().data('firstNode') === undefined)
e.cy().data('firstNode','nonroot')
const parent = e.cy().edges(`[target="${e.id()}"]`)[0].source()
const wing = (parent.data('children') === 1) ? 0 : parent.data('subtreeWidth') / 2
const lastOffset = parent.data('lastChild')
@ -357,7 +385,7 @@ export const CsComponent = ({
const pos = { x: 250 * e.data('layer'), y: (parent.position().y - wing) }
e.data('oldPos', pos)
return pos
}
}
}
}, // map of (node id) => (position obj); or function(node){ return somPos; }
zoom: undefined, // the zoom level to set (prob want fit = false if set)