Skip to content

Commit

Permalink
refactor: remove root property
Browse files Browse the repository at this point in the history
fix #271
  • Loading branch information
SSShooter committed Sep 7, 2024
1 parent caaf3b3 commit 99f1356
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ if (import.meta.env.MODE !== 'lite') {
nodeData: {
id: generateUUID(),
topic: topic || 'new topic',
root: true,
children: [],
},
})
Expand Down
4 changes: 1 addition & 3 deletions src/interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const install = function (this: MindElixirInstance, plugin: (instance: Mi
* @param {TargetElement} el - Target element return by E('...'), default value: currentTarget.
*/
export const focusNode = function (this: MindElixirInstance, el: Topic) {
if (el.nodeObj.root) return
if (!el.nodeObj.parent) return
if (this.tempDirection === null) {
this.tempDirection = this.direction
}
Expand All @@ -204,7 +204,6 @@ export const focusNode = function (this: MindElixirInstance, el: Topic) {
this.isFocusMode = true
}
this.nodeData = el.nodeObj
this.nodeData.root = true
this.initRight()
this.toCenter()
}
Expand All @@ -218,7 +217,6 @@ export const focusNode = function (this: MindElixirInstance, el: Topic) {
export const cancelFocus = function (this: MindElixirInstance) {
this.isFocusMode = false
if (this.tempDirection !== null) {
delete this.nodeData.root
this.nodeData = this.nodeDataBackup
this.direction = this.tempDirection
this.tempDirection = null
Expand Down
10 changes: 5 additions & 5 deletions src/nodeOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export const insertSibling = function (this: MindElixirInstance, type: 'before'
const nodeEle = el || this.currentNode
if (!nodeEle) return
const nodeObj = nodeEle.nodeObj
if (nodeObj.root === true) {
if (!nodeObj.parent) {
this.addChild()
return
} else if (nodeObj.parent?.root === true && nodeObj.parent?.children?.length === 1) {
} else if (!nodeObj.parent?.parent && nodeObj.parent?.children?.length === 1) {
// add at least one node to another side
this.addChild(findEle(nodeObj.parent!.id), node)
return
Expand Down Expand Up @@ -93,7 +93,7 @@ export const insertParent = function (this: MindElixirInstance, el?: Topic, node
if (!nodeEle) return
rmSubline(nodeEle)
const nodeObj = nodeEle.nodeObj
if (nodeObj.root === true) {
if (!nodeObj.parent) {
return
}
const newNodeObj = node || this.generateNewObj()
Expand Down Expand Up @@ -210,7 +210,7 @@ export const removeNode = function (this: MindElixirInstance, el?: Topic) {
const tpc = el || this.currentNode
if (!tpc) return
const nodeObj = tpc.nodeObj
if (nodeObj.root === true) {
if (!nodeObj.parent) {
throw new Error('Can not remove root node')
}
const siblings = nodeObj.parent!.children!
Expand Down Expand Up @@ -239,7 +239,7 @@ export const removeNodes = function (this: MindElixirInstance, tpcs: Topic[]) {
tpcs = unionTopics(tpcs)
for (const tpc of tpcs) {
const nodeObj = tpc.nodeObj
if (nodeObj.root === true) {
if (!nodeObj.parent) {
continue
}
const siblingLength = removeNodeObj(nodeObj)
Expand Down
6 changes: 3 additions & 3 deletions src/plugin/keypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const handleLeftRight = function (mei: MindElixirInstance, direction: 'lhs' | 'r
if (!current) return
const nodeObj = current.nodeObj
const main = current.offsetParent.offsetParent.parentElement
if (nodeObj.root) {
if (!nodeObj.parent) {
direction === 'lhs' ? selectRootLeft(mei) : selectRootRight(mei)
} else if (main.className === direction) {
selectFirstChild(mei, current)
} else {
if (nodeObj.parent?.root) {
if (!nodeObj.parent?.parent) {
selectRoot(mei)
} else {
selectParent(mei, current)
Expand All @@ -47,7 +47,7 @@ const handlePrevNext = function (mei: MindElixirInstance, direction: 'previous'
const current = mei.currentNode || mei.currentNodes?.[0]
if (!current) return
const nodeObj = current.nodeObj
if (nodeObj.root) return
if (!nodeObj.parent) return
const s = (direction + 'Sibling') as 'previousSibling' | 'nextSibling'
const sibling = current.parentElement.parentElement[s]
if (sibling) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/mobileMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default function (mind: MindElixirInstance, option?: any) {
})
mind.bus.addListener('selectNode', function (nodeObj: NodeObj) {
menuContainer.hidden = false
if (nodeObj.root) {
if (!nodeObj.parent) {
isRoot = true
} else {
isRoot = false
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/nodeDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const clearPreview = function (el: Element | null) {
const canMove = function (el: Element, dragged: Topic[]) {
for (const node of dragged) {
const isContain = node.parentElement.parentElement.contains(el)
const ok = el && el.tagName === 'ME-TPC' && el !== node && !isContain && (el as Topic).nodeObj.root !== true
const ok = el && el.tagName === 'ME-TPC' && el !== node && !isContain && (el as Topic).nodeObj.parent
if (!ok) return false
}
return true
Expand Down
8 changes: 4 additions & 4 deletions src/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const calcRange = function (nodes: Topic[]) {
const min = range[0] || 0
const max = range[range.length - 1] || 0
const parent = parentChains[0][index - 1].node
if (parent.root) throw new Error('Please select nodes in the same main topic.')
if (!parent.parent) throw new Error('Please select nodes in the same main topic.')

return {
parent: parent.id,
Expand Down Expand Up @@ -104,10 +104,10 @@ const getDirection = function ({ parent, start }: Summary) {
const parentEl = findEle(parent)
const parentObj = parentEl.nodeObj
let side: 'lhs' | 'rls'
if (parentObj.root === true) {
side = findEle(parentObj.children![start].id).closest('me-main')?.className as 'lhs' | 'rls'
} else {
if (parentObj.parent) {
side = parentEl.closest('me-main')?.className as 'lhs' | 'rls'
} else {
side = findEle(parentObj.children![start].id).closest('me-main')?.className as 'lhs' | 'rls'
}
return side
}
Expand Down
3 changes: 1 addition & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export type NodeObj = {
hyperLink?: string
expanded?: boolean
direction?: number
root?: boolean
image?: {
url: string
width: number
Expand All @@ -165,7 +164,7 @@ export type NodeObj = {
// main node specific properties
branchColor?: string
// add programatically
parent?: NodeObj // root node has no parent
parent?: NodeObj // root node has no parent!
// TODO: checkbox
// checkbox?: boolean | undefined
dangerouslySetInnerHTML?: string
Expand Down

0 comments on commit 99f1356

Please sign in to comment.