From 99f1356eedb27532bf46d6baa6cff76b752a5940 Mon Sep 17 00:00:00 2001 From: ssshooter Date: Sat, 7 Sep 2024 17:35:15 +0800 Subject: [PATCH] refactor: remove `root` property fix #271 --- src/index.ts | 1 - src/interact.ts | 4 +--- src/nodeOperation.ts | 10 +++++----- src/plugin/keypress.ts | 6 +++--- src/plugin/mobileMenu.ts | 2 +- src/plugin/nodeDraggable.ts | 2 +- src/summary.ts | 8 ++++---- src/types/index.ts | 3 +-- 8 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/index.ts b/src/index.ts index 307e8370..34b0f09b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -153,7 +153,6 @@ if (import.meta.env.MODE !== 'lite') { nodeData: { id: generateUUID(), topic: topic || 'new topic', - root: true, children: [], }, }) diff --git a/src/interact.ts b/src/interact.ts index 93f93abb..29d5bcb3 100644 --- a/src/interact.ts +++ b/src/interact.ts @@ -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 } @@ -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() } @@ -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 diff --git a/src/nodeOperation.ts b/src/nodeOperation.ts index bdf11b81..49db7ecc 100644 --- a/src/nodeOperation.ts +++ b/src/nodeOperation.ts @@ -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 @@ -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() @@ -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! @@ -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) diff --git a/src/plugin/keypress.ts b/src/plugin/keypress.ts index 29ed8679..620ccdde 100644 --- a/src/plugin/keypress.ts +++ b/src/plugin/keypress.ts @@ -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) @@ -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) { diff --git a/src/plugin/mobileMenu.ts b/src/plugin/mobileMenu.ts index d6370c23..9b1355a0 100644 --- a/src/plugin/mobileMenu.ts +++ b/src/plugin/mobileMenu.ts @@ -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 diff --git a/src/plugin/nodeDraggable.ts b/src/plugin/nodeDraggable.ts index 4309f68c..4ec2bda3 100644 --- a/src/plugin/nodeDraggable.ts +++ b/src/plugin/nodeDraggable.ts @@ -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 diff --git a/src/summary.ts b/src/summary.ts index c23ed1b5..7c263f9f 100644 --- a/src/summary.ts +++ b/src/summary.ts @@ -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, @@ -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 } diff --git a/src/types/index.ts b/src/types/index.ts index 777f3799..617b18b2 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -156,7 +156,6 @@ export type NodeObj = { hyperLink?: string expanded?: boolean direction?: number - root?: boolean image?: { url: string width: number @@ -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