Skip to content

Commit 99f1356

Browse files
committed
refactor: remove root property
fix #271
1 parent caaf3b3 commit 99f1356

File tree

8 files changed

+16
-20
lines changed

8 files changed

+16
-20
lines changed

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ if (import.meta.env.MODE !== 'lite') {
153153
nodeData: {
154154
id: generateUUID(),
155155
topic: topic || 'new topic',
156-
root: true,
157156
children: [],
158157
},
159158
})

src/interact.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export const install = function (this: MindElixirInstance, plugin: (instance: Mi
195195
* @param {TargetElement} el - Target element return by E('...'), default value: currentTarget.
196196
*/
197197
export const focusNode = function (this: MindElixirInstance, el: Topic) {
198-
if (el.nodeObj.root) return
198+
if (!el.nodeObj.parent) return
199199
if (this.tempDirection === null) {
200200
this.tempDirection = this.direction
201201
}
@@ -204,7 +204,6 @@ export const focusNode = function (this: MindElixirInstance, el: Topic) {
204204
this.isFocusMode = true
205205
}
206206
this.nodeData = el.nodeObj
207-
this.nodeData.root = true
208207
this.initRight()
209208
this.toCenter()
210209
}
@@ -218,7 +217,6 @@ export const focusNode = function (this: MindElixirInstance, el: Topic) {
218217
export const cancelFocus = function (this: MindElixirInstance) {
219218
this.isFocusMode = false
220219
if (this.tempDirection !== null) {
221-
delete this.nodeData.root
222220
this.nodeData = this.nodeDataBackup
223221
this.direction = this.tempDirection
224222
this.tempDirection = null

src/nodeOperation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ export const insertSibling = function (this: MindElixirInstance, type: 'before'
5656
const nodeEle = el || this.currentNode
5757
if (!nodeEle) return
5858
const nodeObj = nodeEle.nodeObj
59-
if (nodeObj.root === true) {
59+
if (!nodeObj.parent) {
6060
this.addChild()
6161
return
62-
} else if (nodeObj.parent?.root === true && nodeObj.parent?.children?.length === 1) {
62+
} else if (!nodeObj.parent?.parent && nodeObj.parent?.children?.length === 1) {
6363
// add at least one node to another side
6464
this.addChild(findEle(nodeObj.parent!.id), node)
6565
return
@@ -93,7 +93,7 @@ export const insertParent = function (this: MindElixirInstance, el?: Topic, node
9393
if (!nodeEle) return
9494
rmSubline(nodeEle)
9595
const nodeObj = nodeEle.nodeObj
96-
if (nodeObj.root === true) {
96+
if (!nodeObj.parent) {
9797
return
9898
}
9999
const newNodeObj = node || this.generateNewObj()
@@ -210,7 +210,7 @@ export const removeNode = function (this: MindElixirInstance, el?: Topic) {
210210
const tpc = el || this.currentNode
211211
if (!tpc) return
212212
const nodeObj = tpc.nodeObj
213-
if (nodeObj.root === true) {
213+
if (!nodeObj.parent) {
214214
throw new Error('Can not remove root node')
215215
}
216216
const siblings = nodeObj.parent!.children!
@@ -239,7 +239,7 @@ export const removeNodes = function (this: MindElixirInstance, tpcs: Topic[]) {
239239
tpcs = unionTopics(tpcs)
240240
for (const tpc of tpcs) {
241241
const nodeObj = tpc.nodeObj
242-
if (nodeObj.root === true) {
242+
if (!nodeObj.parent) {
243243
continue
244244
}
245245
const siblingLength = removeNodeObj(nodeObj)

src/plugin/keypress.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ const handleLeftRight = function (mei: MindElixirInstance, direction: 'lhs' | 'r
3131
if (!current) return
3232
const nodeObj = current.nodeObj
3333
const main = current.offsetParent.offsetParent.parentElement
34-
if (nodeObj.root) {
34+
if (!nodeObj.parent) {
3535
direction === 'lhs' ? selectRootLeft(mei) : selectRootRight(mei)
3636
} else if (main.className === direction) {
3737
selectFirstChild(mei, current)
3838
} else {
39-
if (nodeObj.parent?.root) {
39+
if (!nodeObj.parent?.parent) {
4040
selectRoot(mei)
4141
} else {
4242
selectParent(mei, current)
@@ -47,7 +47,7 @@ const handlePrevNext = function (mei: MindElixirInstance, direction: 'previous'
4747
const current = mei.currentNode || mei.currentNodes?.[0]
4848
if (!current) return
4949
const nodeObj = current.nodeObj
50-
if (nodeObj.root) return
50+
if (!nodeObj.parent) return
5151
const s = (direction + 'Sibling') as 'previousSibling' | 'nextSibling'
5252
const sibling = current.parentElement.parentElement[s]
5353
if (sibling) {

src/plugin/mobileMenu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default function (mind: MindElixirInstance, option?: any) {
9191
})
9292
mind.bus.addListener('selectNode', function (nodeObj: NodeObj) {
9393
menuContainer.hidden = false
94-
if (nodeObj.root) {
94+
if (!nodeObj.parent) {
9595
isRoot = true
9696
} else {
9797
isRoot = false

src/plugin/nodeDraggable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const clearPreview = function (el: Element | null) {
3131
const canMove = function (el: Element, dragged: Topic[]) {
3232
for (const node of dragged) {
3333
const isContain = node.parentElement.parentElement.contains(el)
34-
const ok = el && el.tagName === 'ME-TPC' && el !== node && !isContain && (el as Topic).nodeObj.root !== true
34+
const ok = el && el.tagName === 'ME-TPC' && el !== node && !isContain && (el as Topic).nodeObj.parent
3535
if (!ok) return false
3636
}
3737
return true

src/summary.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const calcRange = function (nodes: Topic[]) {
5959
const min = range[0] || 0
6060
const max = range[range.length - 1] || 0
6161
const parent = parentChains[0][index - 1].node
62-
if (parent.root) throw new Error('Please select nodes in the same main topic.')
62+
if (!parent.parent) throw new Error('Please select nodes in the same main topic.')
6363

6464
return {
6565
parent: parent.id,
@@ -104,10 +104,10 @@ const getDirection = function ({ parent, start }: Summary) {
104104
const parentEl = findEle(parent)
105105
const parentObj = parentEl.nodeObj
106106
let side: 'lhs' | 'rls'
107-
if (parentObj.root === true) {
108-
side = findEle(parentObj.children![start].id).closest('me-main')?.className as 'lhs' | 'rls'
109-
} else {
107+
if (parentObj.parent) {
110108
side = parentEl.closest('me-main')?.className as 'lhs' | 'rls'
109+
} else {
110+
side = findEle(parentObj.children![start].id).closest('me-main')?.className as 'lhs' | 'rls'
111111
}
112112
return side
113113
}

src/types/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ export type NodeObj = {
156156
hyperLink?: string
157157
expanded?: boolean
158158
direction?: number
159-
root?: boolean
160159
image?: {
161160
url: string
162161
width: number
@@ -165,7 +164,7 @@ export type NodeObj = {
165164
// main node specific properties
166165
branchColor?: string
167166
// add programatically
168-
parent?: NodeObj // root node has no parent
167+
parent?: NodeObj // root node has no parent!
169168
// TODO: checkbox
170169
// checkbox?: boolean | undefined
171170
dangerouslySetInnerHTML?: string

0 commit comments

Comments
 (0)