Skip to content

Commit 84d5bf9

Browse files
committed
refactor: refactor expandNode
1 parent 4590adc commit 84d5bf9

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

api/mind-elixir.api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@
10061006
},
10071007
{
10081008
"kind": "Content",
1009-
"text": ") => void;\n mainToSub: (this: "
1009+
"text": ") => void;\n rmSubline: (this: "
10101010
},
10111011
{
10121012
"kind": "Reference",

src/interact.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { rmSubline } from './nodeOperation'
12
import type { Topic } from './types/dom'
23
import type { MindElixirData, MindElixirInstance, NodeObj } from './types/index'
34
import { findEle } from './utils/dom'
@@ -278,8 +279,26 @@ export const expandNode = function (this: MindElixirInstance, el: Topic, isExpan
278279
} else {
279280
node.expanded = true
280281
}
282+
const parent = el.parentNode
283+
const expander = parent.children[1]!
284+
expander.expanded = node.expanded
285+
expander.className = node.expanded ? 'minus' : ''
286+
287+
rmSubline(el)
288+
if (node.expanded) {
289+
const children = this.createChildren(
290+
node.children!.map(child => {
291+
const wrapper = this.createWrapper(child)
292+
return wrapper.grp
293+
})
294+
)
295+
parent.parentNode.appendChild(children)
296+
} else {
297+
const children = parent.parentNode.children[1]
298+
children.remove()
299+
}
281300
// TODO 在此函数构造 html 结构,而非调用 layout
282-
this.layout()
301+
// this.layout()
283302
// linkDiv 已实现只更新特定主节点
284303
this.linkDiv()
285304
this.bus.fire('expandNode', node)

src/linkDiv.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@ const linkDiv = function (this: MindElixirInstance, mainNode?: Wrapper) {
5959
if (mainNode && mainNode !== el) {
6060
continue
6161
}
62-
if (el.childElementCount) {
63-
const svg = createLinkSvg('subLines')
64-
// svg tag name is lower case
65-
const svgLine = el.lastChild as SVGSVGElement
66-
if (svgLine.tagName === 'svg') svgLine.remove()
67-
el.appendChild(svg)
68-
69-
traverseChildren(this, svg, branchColor, el, direction, true)
70-
}
62+
63+
const svg = createLinkSvg('subLines')
64+
// svg tag name is lower case
65+
const svgLine = el.lastChild as SVGSVGElement
66+
if (svgLine.tagName === 'svg') svgLine.remove()
67+
el.appendChild(svg)
68+
69+
traverseChildren(this, svg, branchColor, el, direction, true)
7170
}
7271

7372
this.renderArrow()
@@ -87,6 +86,7 @@ const traverseChildren = function (
8786
) {
8887
const parent = wrapper.firstChild
8988
const children = wrapper.children[1].children
89+
if (children.length === 0) return
9090

9191
const pT = parent.offsetTop
9292
const pL = parent.offsetLeft
@@ -120,10 +120,7 @@ const traverseChildren = function (
120120
continue
121121
}
122122

123-
const nextChildren = child.children[1].children
124-
if (nextChildren.length > 0) {
125-
traverseChildren(mei, svgContainer, bc, child, direction)
126-
}
123+
traverseChildren(mei, svgContainer, bc, child, direction)
127124
}
128125
}
129126

src/nodeOperation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const typeMap: Record<string, InsertPosition> = {
1111
after: 'afterend',
1212
}
1313

14-
export const mainToSub = function (tpc: Topic) {
14+
export const rmSubline = function (tpc: Topic) {
1515
const mainNode = tpc.parentElement.parentElement
1616
const lc = mainNode.lastElementChild
1717
if (lc?.tagName === 'svg') lc?.remove() // clear svg group of main node
@@ -73,7 +73,7 @@ export const insertSibling = function (this: MindElixirInstance, type: 'before'
7373
export const insertParent = function (this: MindElixirInstance, el?: Topic, node?: NodeObj) {
7474
const nodeEle = el || this.currentNode
7575
if (!nodeEle) return
76-
mainToSub(nodeEle)
76+
rmSubline(nodeEle)
7777
const nodeObj = nodeEle.nodeObj
7878
if (nodeObj.root === true) {
7979
return
@@ -82,7 +82,6 @@ export const insertParent = function (this: MindElixirInstance, el?: Topic, node
8282
insertParentNodeObj(nodeObj, newNodeObj)
8383
fillParent(this.nodeData)
8484

85-
// warning: the tricky part
8685
const grp0 = nodeEle.parentElement.parentElement
8786
console.time('insertParent_DOM')
8887
const { grp, top } = this.createWrapper(newNodeObj, true)
@@ -274,7 +273,7 @@ const moveNode = (from: Topic[], type: 'before' | 'after', to: Topic, mei: MindE
274273
const obj = f.nodeObj
275274
moveNodeObj(type, obj, toObj)
276275
fillParent(mei.nodeData)
277-
mainToSub(f)
276+
rmSubline(f)
278277
const fromGrp = f.parentElement.parentNode
279278
const toGrp = to.parentElement.parentNode
280279
toGrp.insertAdjacentElement(typeMap[type], fromGrp)

src/utils/domManipulation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fillParent } from '.'
22
import { LEFT, RIGHT, SIDE } from '../const'
3-
import { mainToSub } from '../nodeOperation'
3+
import { rmSubline } from '../nodeOperation'
44
import type { MindElixirInstance, NodeObj } from '../types'
55
import type { Topic, Wrapper } from '../types/dom'
66
import { findEle, createExpander } from './dom'
@@ -28,7 +28,7 @@ export const realAddChild = function (mei: MindElixirInstance, to: Topic, wrappe
2828
const tpc = wrapper.children[0].children[0]
2929
const top = to.parentElement
3030
if (top.tagName === 'ME-PARENT') {
31-
mainToSub(tpc)
31+
rmSubline(tpc)
3232
if (top.children[1]) {
3333
top.nextSibling.appendChild(wrapper)
3434
} else {

0 commit comments

Comments
 (0)