Skip to content

Commit 179fb69

Browse files
committed
refactor: refactor removeNode
1 parent 3dede73 commit 179fb69

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/interact.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const selectPrevSibling = function (this: MindElixirInstance) {
7272
const sibling = this.currentNode.parentElement.parentElement.previousSibling
7373
let target: Topic
7474
if (sibling) {
75-
target = sibling.firstChild.firstChild as Topic
75+
target = sibling.firstChild.firstChild
7676
} else {
7777
return false
7878
}

src/nodeOperation.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,30 +362,31 @@ export const moveDownNode = function (this: MindElixirInstance, el?: Topic) {
362362
export const removeNode = function (this: MindElixirInstance, el?: Topic) {
363363
const nodeEle = el || this.currentNode
364364
if (!nodeEle) return
365-
console.log('removeNode', nodeEle)
366365
const nodeObj = nodeEle.nodeObj
367366
if (nodeObj.root === true) {
368367
throw new Error('Can not remove root node')
369368
}
370369
const siblings = nodeObj.parent!.children!
371-
const index = siblings.findIndex(node => node === nodeObj)
372-
const next: NodeObj | undefined = siblings[index + 1]
373-
const originSiblingId = next && next.id
370+
const i = siblings.findIndex(node => node === nodeObj)
371+
const siblingLength = removeNodeObj(nodeObj)
374372

375-
const childrenLength = removeNodeObj(nodeObj)
376373
const t = nodeEle.parentNode
377-
if (childrenLength === 0) {
374+
if (siblingLength === 0) {
378375
// remove epd when children length === 0
379376
const c = t.parentNode.parentNode
380377
// root doesn't have epd
381378
if (c.tagName !== 'ME-MAIN') {
382379
c.previousSibling.children[1].remove()
383380
}
384-
this.selectParent()
381+
}
382+
// automatically select sibling or parent
383+
if (siblings.length !== 0) {
384+
const sibling = siblings[i] || siblings[i - 1]
385+
this.selectNode(findEle(sibling.id))
385386
} else {
386-
// select sibling automatically
387-
this.selectPrevSibling() || this.selectNextSibling()
387+
this.selectNode(findEle(nodeObj.parent!.id))
388388
}
389+
389390
for (const prop in this.linkData) {
390391
// MAYBEBUG should traverse all children node
391392
const link = this.linkData[prop]
@@ -394,13 +395,12 @@ export const removeNode = function (this: MindElixirInstance, el?: Topic) {
394395
this.removeLink(linkEle)
395396
}
396397
}
397-
// remove GRP
398398
t.parentNode.remove()
399399
this.linkDiv()
400400
this.bus.fire('operation', {
401401
name: 'removeNode',
402402
obj: nodeObj,
403-
originSiblingId,
403+
originIndex: i,
404404
originParentId: nodeObj?.parent?.id,
405405
})
406406
}

src/utils/pubsub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export type Operation =
3939
| {
4040
name: 'removeNode'
4141
obj: NodeObj
42-
originSiblingId?: string
42+
originIndex?: number
4343
originParentId?: string
4444
}
4545

0 commit comments

Comments
 (0)