Skip to content

Commit d696f5c

Browse files
committed
feat: add removeNodes method
1 parent ac9234c commit d696f5c

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

src/nodeOperation.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
moveNodeBeforeObj,
1515
moveNodeAfterObj,
1616
} from './utils/objectManipulation'
17-
import { addChildDom, judgeDirection } from './utils/domManipulation'
17+
import { addChildDom, judgeDirection, removeNodeDom } from './utils/domManipulation'
1818

1919
const mainToSub = function (tpc: Topic) {
2020
const mainNode = tpc.parentElement.parentElement
@@ -256,9 +256,9 @@ export const copyNode = function (this: MindElixirInstance, node: Topic, to: Top
256256
export const moveUpNode = function (this: MindElixirInstance, el?: Topic) {
257257
const nodeEle = el || this.currentNode
258258
if (!nodeEle) return
259-
const grp = nodeEle.parentNode.parentNode
260259
const obj = nodeEle.nodeObj
261260
moveUpObj(obj)
261+
const grp = nodeEle.parentNode.parentNode
262262
grp.parentNode.insertBefore(grp, grp.previousSibling)
263263
this.linkDiv()
264264
this.bus.fire('operation', {
@@ -280,9 +280,9 @@ export const moveUpNode = function (this: MindElixirInstance, el?: Topic) {
280280
export const moveDownNode = function (this: MindElixirInstance, el?: Topic) {
281281
const nodeEle = el || this.currentNode
282282
if (!nodeEle) return
283-
const grp = nodeEle.parentNode.parentNode
284283
const obj = nodeEle.nodeObj
285284
moveDownObj(obj)
285+
const grp = nodeEle.parentNode.parentNode
286286
if (grp.nextSibling) {
287287
grp.nextSibling.insertAdjacentElement('afterend', grp)
288288
} else {
@@ -306,25 +306,17 @@ export const moveDownNode = function (this: MindElixirInstance, el?: Topic) {
306306
* removeNode(E('bd4313fbac40284b'))
307307
*/
308308
export const removeNode = function (this: MindElixirInstance, el?: Topic) {
309-
const nodeEle = el || this.currentNode
310-
if (!nodeEle) return
311-
const nodeObj = nodeEle.nodeObj
309+
const tpc = el || this.currentNode
310+
if (!tpc) return
311+
const nodeObj = tpc.nodeObj
312312
if (nodeObj.root === true) {
313313
throw new Error('Can not remove root node')
314314
}
315315
const siblings = nodeObj.parent!.children!
316316
const i = siblings.findIndex(node => node === nodeObj)
317317
const siblingLength = removeNodeObj(nodeObj)
318+
removeNodeDom(tpc, siblingLength)
318319

319-
const t = nodeEle.parentNode
320-
if (siblingLength === 0) {
321-
// remove epd when children length === 0
322-
const c = t.parentNode.parentNode
323-
// root doesn't have epd
324-
if (c.tagName !== 'ME-MAIN') {
325-
c.previousSibling.children[1].remove()
326-
}
327-
}
328320
// automatically select sibling or parent
329321
if (siblings.length !== 0) {
330322
const sibling = siblings[i] || siblings[i - 1]
@@ -333,7 +325,6 @@ export const removeNode = function (this: MindElixirInstance, el?: Topic) {
333325
this.selectNode(findEle(nodeObj.parent!.id))
334326
}
335327

336-
t.parentNode.remove()
337328
this.linkDiv()
338329
this.bus.fire('operation', {
339330
name: 'removeNode',
@@ -343,6 +334,21 @@ export const removeNode = function (this: MindElixirInstance, el?: Topic) {
343334
})
344335
}
345336

337+
export const removeNodes = function (this: MindElixirInstance, tpcs: Topic[]) {
338+
for (const tpc of tpcs) {
339+
const nodeObj = tpc.nodeObj
340+
if (nodeObj.root === true) {
341+
continue
342+
}
343+
const siblingLength = removeNodeObj(nodeObj)
344+
removeNodeDom(tpc, siblingLength)
345+
}
346+
this.linkDiv()
347+
this.bus.fire('operation', {
348+
name: 'removeNodes',
349+
objs: tpcs.map(tpc => tpc.nodeObj),
350+
})
351+
}
346352
/**
347353
* @function
348354
* @instance

src/plugin/keypress.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,13 @@ export default function (mind: MindElixirInstance) {
150150
// bug
151151
if (mind.currentLink) mind.removeLink()
152152
else if (mind.currentSummary) mind.removeSummary(mind.currentSummary.summaryObj.id)
153-
else mind.removeNode()
153+
else {
154+
if (mind.currentNode) {
155+
mind.removeNode()
156+
} else if (mind.currentNodes) {
157+
mind.removeNodes(mind.currentNodes)
158+
}
159+
}
154160
} else {
155161
const keyHandler = key2func[e.keyCode]
156162
keyHandler && keyHandler(e)

src/plugin/operationHistory.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Operation } from '../utils/pubsub'
55

66
type History = {
77
prev: MindElixirData
8-
currentNodeId: string
8+
currentNodeId: string | undefined
99
next: MindElixirData
1010
}
1111

@@ -17,17 +17,19 @@ export default function (mei: MindElixirInstance) {
1717
if (operation.name === 'beginEdit') return
1818
history = history.slice(0, currentIndex + 1)
1919
const next = mei.getData()
20-
history.push({ prev: current, currentNodeId: operation.obj.id, next })
20+
let currentNodeId = undefined
21+
if ('obj' in operation) currentNodeId = operation.obj.id
22+
history.push({ prev: current, currentNodeId, next })
2123
current = next
2224
currentIndex = history.length - 1
23-
console.log('operation', operation.obj.id, history)
25+
// console.log('operation', operation.obj.id, history)
2426
})
2527
mei.undo = function () {
2628
if (currentIndex > -1) {
2729
const h = history[currentIndex]
2830
current = h.prev
2931
mei.refresh(h.prev)
30-
mei.selectNode(findEle(h.currentNodeId))
32+
if (h.currentNodeId) mei.selectNode(findEle(h.currentNodeId))
3133
currentIndex--
3234
console.log('current', current)
3335
}
@@ -38,7 +40,7 @@ export default function (mei: MindElixirInstance) {
3840
const h = history[currentIndex]
3941
current = h.next
4042
mei.refresh(h.next)
41-
mei.selectNode(findEle(h.currentNodeId))
43+
if (h.currentNodeId) mei.selectNode(findEle(h.currentNodeId))
4244
}
4345
}
4446
mei.map.addEventListener('keydown', (e: KeyboardEvent) => {

src/utils/domManipulation.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ export const judgeDirection = function (direction: number, obj: NodeObj) {
2323
}
2424
}
2525

26-
export const addChildDom = function (this: MindElixirInstance, nodeEle: Topic, node?: NodeObj) {
27-
if (!nodeEle) return null
28-
const nodeObj = nodeEle.nodeObj
26+
export const addChildDom = function (this: MindElixirInstance, tpc: Topic, node?: NodeObj) {
27+
if (!tpc) return null
28+
const nodeObj = tpc.nodeObj
2929
if (nodeObj.expanded === false) {
30-
this.expandNode(nodeEle, true)
30+
this.expandNode(tpc, true)
3131
// dom had resetted
32-
nodeEle = findEle(nodeObj.id) as Topic
32+
tpc = findEle(nodeObj.id) as Topic
3333
}
3434
const newNodeObj = node || this.generateNewObj()
3535
if (nodeObj.children) nodeObj.children.push(newNodeObj)
3636
else nodeObj.children = [newNodeObj]
3737
fillParent(this.nodeData)
3838

39-
const top = nodeEle.parentElement
39+
const top = tpc.parentElement
4040

4141
const { grp, top: newTop } = this.createWrapper(newNodeObj)
4242
if (top.tagName === 'ME-PARENT') {
@@ -59,3 +59,16 @@ export const addChildDom = function (this: MindElixirInstance, nodeEle: Topic, n
5959
}
6060
return { newTop, newNodeObj }
6161
}
62+
63+
export const removeNodeDom = function (tpc: Topic, siblingLength: number) {
64+
const p = tpc.parentNode
65+
if (siblingLength === 0) {
66+
// remove epd when children length === 0
67+
const c = p.parentNode.parentNode
68+
// root doesn't have epd
69+
if (c.tagName !== 'ME-MAIN') {
70+
c.previousSibling.children[1].remove()
71+
}
72+
}
73+
p.parentNode.remove()
74+
}

src/utils/pubsub.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type OperationType =
55
| 'moveNodeAfter'
66
| 'moveNodeBefore'
77
| 'removeNode'
8+
| 'removeNodes'
89
| 'addChild'
910
| 'copyNode'
1011
| 'reshapeNode'
@@ -48,6 +49,10 @@ export type Operation =
4849
originIndex?: number
4950
originParentId?: string
5051
}
52+
| {
53+
name: 'removeNodes'
54+
objs: NodeObj[]
55+
}
5156

5257
export type EventMap = {
5358
operation: (info: Operation) => void

0 commit comments

Comments
 (0)