feat: center the same layers

This commit is contained in:
Lin, Hung-Shin committed 2023-11-16 12:53:44 +08:00
1 parent 7a8df716da
commit b19d6bf6e2
3 files changed
+55 -2

No files matched your search

+5 -2
View File
@@ -53,12 +53,15 @@ const data = [
},
]
const marginX = 100
const marginY = 100
const dag = new DAG(data)
const nodes = dag.getNodes().map((node) => ({
id: node._id,
name: node.title,
x: 50 * node.index,
y: 100 * node.layer,
x: marginX * node.index - (marginX * dag.getLayer(node._id)) / 2,
y: marginY * node.layer,
}))
const edges = dag.getEdgesArray().map((edge) => ({
+12
View File
@@ -30,6 +30,18 @@ export default class DAG {
return Array.from(this.#edges.values())
}
getLayers() {
return this.#layers
}
getLayer(id) {
const node = this.getNode(id)
if (!node) {
throw new Error(`Node ${id} not found`)
}
return this.#layers[node.layer - 1]
}
#setLayer(id) {
const node = this.getNode(id)
if (!node) {
+38
View File
@@ -125,3 +125,41 @@ describe('dag edges', () => {
)
})
})
describe('dag layers', () => {
it('two layer', () => {
const data = [
{
_id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getLayers()).toEqual([1, 1])
})
it('branch layers', () => {
const data = [
{
_id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
requirements: ['helloworld'],
},
{
_id: 'discover',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getLayers()).toEqual([1, 2])
})
})