Skip to content

Commit d2556c9

Browse files
committed
refactor: abstract line-generate function
#122
1 parent 8c7ee6d commit d2556c9

File tree

2 files changed

+37
-47
lines changed

2 files changed

+37
-47
lines changed

src/dev.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ const options = {
5656
},
5757
},
5858
primaryLinkStyle: 1,
59-
primaryNodeVerticalGap: 15, // 25
60-
primaryNodeHorizontalGap: 15, // 65
59+
primaryNodeVerticalGap: 25, // 25
60+
primaryNodeHorizontalGap: 65, // 65
6161
}
6262

6363
const mind = new (MindElixir as any)(options)

src/linkDiv.ts

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export default function linkDiv(primaryNode) {
7272
const alignLeft = 10000 + root.offsetWidth / 2 + primaryNodeHorizontalGap
7373
for (let i = 0; i < primaryNodeList.length; i++) {
7474
let x1 = 10000
75+
const y1 = 10000
7576
let x2, y2
7677
const el = primaryNodeList[i]
7778
const elOffsetH = el.offsetHeight
@@ -81,20 +82,6 @@ export default function linkDiv(primaryNode) {
8182
x2 = alignRight - 15 // padding
8283
y2 = base + currentOffsetL + elOffsetH / 2
8384

84-
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d#path_commands
85-
if (this.primaryLinkStyle === 2) {
86-
if (this.direction === SIDE) {
87-
x1 = 10000 - root.offsetWidth / 6
88-
}
89-
if (y2 < 10000) {
90-
primaryPath += `M ${x1} 10000 V ${y2 + 20} C ${x1} ${y2} ${x1} ${y2} ${x1 - 20} ${y2} H ${x2}`
91-
} else {
92-
primaryPath += `M ${x1} 10000 V ${y2 - 20} C ${x1} ${y2} ${x1} ${y2} ${x1 - 20} ${y2} H ${x2}`
93-
}
94-
} else {
95-
primaryPath += `M 10000 10000 C 10000 10000 ${10000 + 2 * primaryNodeHorizontalGap * 0.03} ${y2} ${x2} ${y2}`
96-
}
97-
9885
if (shortSide === 'l') {
9986
currentOffsetL += elOffsetH + shortSideGap
10087
} else {
@@ -106,24 +93,22 @@ export default function linkDiv(primaryNode) {
10693
x2 = alignLeft + 15 // padding
10794
y2 = base + currentOffsetR + elOffsetH / 2
10895

109-
if (this.primaryLinkStyle === 2) {
110-
if (this.direction === SIDE) {
111-
x1 = 10000 + root.offsetWidth / 6
112-
}
113-
if (y2 < 10000) {
114-
primaryPath += `M ${x1} 10000 V ${y2 + 20} C ${x1} ${y2} ${x1} ${y2} ${x1 + 20} ${y2} H ${x2}`
115-
} else {
116-
primaryPath += `M ${x1} 10000 V ${y2 - 20} C ${x1} ${y2} ${x1} ${y2} ${x1 + 20} ${y2} H ${x2}`
117-
}
118-
} else {
119-
primaryPath += `M 10000 10000 C 10000 10000 ${10000 + 2 * primaryNodeHorizontalGap * 0.03} ${y2} ${x2} ${y2}`
120-
}
12196
if (shortSide === 'r') {
12297
currentOffsetR += elOffsetH + shortSideGap
12398
} else {
12499
currentOffsetR += elOffsetH + primaryNodeVerticalGap
125100
}
126101
}
102+
103+
if (this.primaryLinkStyle === 2) {
104+
if (this.direction === SIDE) {
105+
if (el.className === 'lhs') { x1 = 10000 - root.offsetWidth / 6 } else { x1 = 10000 + root.offsetWidth / 6 }
106+
}
107+
primaryPath += generatePrimaryLine2({ x1, y1, x2, y2 })
108+
} else {
109+
primaryPath += generatePrimaryLine1({ x1, y1, x2, y2 })
110+
}
111+
127112
// set position of expander
128113
const expander = el.children[0].children[1]
129114
if (expander) {
@@ -193,31 +178,14 @@ function traverseChildren(children: HTMLCollection, parent: HTMLElement, first?:
193178
x1 = parentOL + GAP
194179
xMiddle = parentOL
195180
x2 = parentOL - childT.offsetWidth + GAP
196-
197-
if (childTOT + childTOH < parentOT + parentOH / 2 + 50 && childTOT + childTOH > parentOT + parentOH / 2 - 50) {
198-
// draw straight line if the distance is between +-50
199-
path += `M ${x1} ${y1} H ${xMiddle} V ${y2} H ${x2}`
200-
} else if (childTOT + childTOH >= parentOT + parentOH / 2) {
201-
// child bottom lower than parent
202-
path += `M ${x1} ${y1} H ${xMiddle} V ${y2 - TURNPOINT_R} A ${TURNPOINT_R} ${TURNPOINT_R} 0 0 1 ${xMiddle - TURNPOINT_R} ${y2} H ${x2}`
203-
} else {
204-
// child bottom higher than parent
205-
path += `M ${x1} ${y1} H ${xMiddle} V ${y2 + TURNPOINT_R} A ${TURNPOINT_R} ${TURNPOINT_R} 0 0 0 ${xMiddle - TURNPOINT_R} ${y2} H ${x2}`
206-
}
207181
} else if (direction === 'rhs') {
208182
x1 = parentOL + parentOW - GAP
209183
xMiddle = parentOL + parentOW
210184
x2 = parentOL + parentOW + childT.offsetWidth - GAP
211-
212-
if (childTOT + childTOH < parentOT + parentOH / 2 + 50 && childTOT + childTOH > parentOT + parentOH / 2 - 50) {
213-
path += `M ${x1} ${y1} H ${xMiddle} V ${y2} H ${x2}`
214-
} else if (childTOT + childTOH >= parentOT + parentOH / 2) {
215-
path += `M ${x1} ${y1} H ${xMiddle} V ${y2 - TURNPOINT_R} A ${TURNPOINT_R} ${TURNPOINT_R} 0 0 0 ${xMiddle + TURNPOINT_R} ${y2} H ${x2}`
216-
} else {
217-
path += `M ${x1} ${y1} H ${xMiddle} V ${y2 + TURNPOINT_R} A ${TURNPOINT_R} ${TURNPOINT_R} 0 0 1 ${xMiddle + TURNPOINT_R} ${y2} H ${x2}`
218-
}
219185
}
220186

187+
path += generateSubLine({ x1, y1, x2, y2, xMiddle })
188+
221189
const expander = childT.children[1] as Expander
222190
if (expander) {
223191
expander.style.top = (childT.offsetHeight - expander.offsetHeight) / 2 + 'px'
@@ -238,3 +206,25 @@ function traverseChildren(children: HTMLCollection, parent: HTMLElement, first?:
238206
}
239207
return path
240208
}
209+
210+
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d#path_commands
211+
function generatePrimaryLine2({ x1, y1, x2, y2 }) {
212+
return `M ${x1} 10000 V ${y2 > y1 ? (y2 - 20) : (y2 + 20)} C ${x1} ${y2} ${x1} ${y2} ${x2 > x1 ? (x1 + 20) : (x1 - 20)} ${y2} H ${x2}`
213+
}
214+
215+
function generatePrimaryLine1({ x1, y1, x2, y2 }) {
216+
return `M ${x1} ${y1} Q ${x1} ${y2} ${x2} ${y2}`
217+
}
218+
219+
function generateSubLine({ x1, y1, x2, y2, xMiddle }) {
220+
if (y2 < y1 + 50 && y2 > y1 - 50) {
221+
// draw straight line if the distance is between +-50
222+
return `M ${x1} ${y1} H ${xMiddle} V ${y2} H ${x2}`
223+
} else if (y2 >= y1) {
224+
// child bottom lower than parent
225+
return `M ${x1} ${y1} H ${xMiddle} V ${y2 - TURNPOINT_R} A ${TURNPOINT_R} ${TURNPOINT_R} 0 0 ${x1 > x2 ? 1 : 0} ${x1 > x2 ? (xMiddle - TURNPOINT_R) : (xMiddle + TURNPOINT_R)} ${y2} H ${x2}`
226+
} else {
227+
// child bottom higher than parent
228+
return `M ${x1} ${y1} H ${xMiddle} V ${y2 + TURNPOINT_R} A ${TURNPOINT_R} ${TURNPOINT_R} 0 0 ${x1 > x2 ? 0 : 1} ${x1 > x2 ? (xMiddle - TURNPOINT_R) : (xMiddle + TURNPOINT_R)} ${y2} H ${x2}`
229+
}
230+
}

0 commit comments

Comments
 (0)