Skip to content

Commit 9bf55f3

Browse files
committed
feat: update calcRange for single selected node
1 parent a76f384 commit 9bf55f3

File tree

7 files changed

+32
-34
lines changed

7 files changed

+32
-34
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module.exports = {
1111
},
1212
rules: {
1313
'@typescript-eslint/consistent-type-imports': 'error',
14+
'@typescript-eslint/no-non-null-assertion': 'off',
1415
},
1516
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mind-elixir",
3-
"version": "3.0.5-beta.1",
3+
"version": "3.0.5-beta.2",
44
"type": "module",
55
"description": "Mind elixir is a free open source mind map core.",
66
"keywords": [

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ MindElixir.DARK_THEME = DARK_THEME
130130
* @memberof MindElixir
131131
* @static
132132
*/
133-
MindElixir.version = '3.0.5-beta.1'
133+
MindElixir.version = '3.0.5-beta.2'
134134
/**
135135
* @function
136136
* @memberof MindElixir

src/mouse.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ const isTopic = (target: HTMLElement) => {
99

1010
export default function (mind: MindElixirInstance) {
1111
mind.map.addEventListener('click', e => {
12-
console.log('click', e)
1312
if (dragMoveHelper.moved) {
1413
dragMoveHelper.clear()
1514
return
1615
}
16+
mind.unselectNode()
17+
mind.unselectNodes()
18+
mind.unselectSummary()
1719
// e.preventDefault() // can cause <a /> tags don't work
1820
const target = e.target as any
1921
if (target.tagName === 'ME-EPD') {
@@ -22,22 +24,15 @@ export default function (mind: MindElixirInstance) {
2224
return
2325
} else if (isTopic(target)) {
2426
mind.selectNode(target as Topic, false, e)
25-
mind.unselectNodes()
26-
mind.unselectSummary()
2727
} else if (target.tagName === 'text') {
2828
mind.selectSummary(target.parentElement as unknown as SummarySvgGroup)
29-
mind.unselectNode()
30-
mind.unselectNodes()
3129
} else if (target.tagName === 'path') {
3230
if (target?.parentElement?.tagName === 'g') {
3331
mind.selectLink(target.parentElement as CustomSvg)
3432
}
3533
} else if (target.className === 'circle') {
3634
// skip circle
3735
} else {
38-
mind.unselectNode()
39-
mind.unselectNodes()
40-
mind.unselectSummary()
4136
// lite version doesn't have hideLinkController
4237
mind.hideLinkController && mind.hideLinkController()
4338
}

src/plugin/contextMenu.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export default function (mind: MindElixirInstance, option: any) {
8989
add_sibling.className = ''
9090
remove_child.className = ''
9191
}
92-
mind.selectNode(target)
92+
if (!mind.currentNodes) mind.selectNode(target)
9393
menuContainer.hidden = false
9494
const height = menuUl.offsetHeight
9595
const width = menuUl.offsetWidth
@@ -175,9 +175,7 @@ export default function (mind: MindElixirInstance, option: any) {
175175
}
176176
summary.onclick = () => {
177177
menuContainer.hidden = true
178-
if (mind.currentNodes && mind.currentNodes.length > 1) {
179-
mind.createSummary()
180-
mind.unselectNodes()
181-
}
178+
mind.createSummary()
179+
mind.unselectNodes()
182180
}
183181
}

src/plugin/selection.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ export default function (mei: MindElixirInstance) {
2222
startScrollMargins: { x: 10, y: 10 },
2323
},
2424
},
25-
features: {
26-
// Configuration in case a selectable gets just clicked.
27-
singleTap: {
28-
// Enable single-click selection (Also disables range-selection via shift + ctrl).
29-
allow: true,
30-
31-
// 'native' (element was mouse-event target) or 'touch' (element visually touched).
32-
intersect: 'native',
33-
},
34-
},
3525
})
3626
.on('beforestart', ({ event }) => {
3727
if ((event as MouseEvent).button !== 0) return false
@@ -41,6 +31,7 @@ export default function (mei: MindElixirInstance) {
4131
})
4232
.on('start', ({ event }) => {
4333
if (!(event as MouseEvent).ctrlKey && !(event as MouseEvent).metaKey) {
34+
mei.unselectNode()
4435
mei.unselectNodes()
4536
mei.unselectSummary()
4637
selection.clearSelection(true, true)

src/summary.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@ export type SummarySvgGroup = SVGGElement & {
1515
summaryObj: Summary
1616
}
1717

18-
const calcRange = function (currentNodes: Topic[]) {
18+
const calcRange = function (nodes: Topic[]) {
19+
if (nodes.length === 0) throw new Error('No selected node.')
20+
if (nodes.length === 1) {
21+
const obj = nodes[0].nodeObj
22+
const parent = nodes[0].nodeObj.parent
23+
if (!parent) throw new Error('Can not select root node.')
24+
const i = parent.children!.findIndex(child => obj === child)
25+
return {
26+
parent: parent.id,
27+
start: i,
28+
end: i,
29+
}
30+
}
1931
let maxLen = 0
20-
const parentChains = currentNodes.map(item => {
32+
const parentChains = nodes.map(item => {
2133
let node = item.nodeObj
2234
const parentChain = []
2335
while (node.parent) {
@@ -34,7 +46,6 @@ const calcRange = function (currentNodes: Topic[]) {
3446
// find minimum common parent
3547
findMcp: for (; index < maxLen; index++) {
3648
const base = parentChains[0][index]?.node
37-
console.log(base)
3849
for (let i = 1; i < parentChains.length; i++) {
3950
const parentChain = parentChains[i]
4051
if (parentChain[index]?.node !== base) {
@@ -43,12 +54,10 @@ const calcRange = function (currentNodes: Topic[]) {
4354
}
4455
}
4556
const range = parentChains.map(chain => chain[index - 1].index).sort()
46-
console.log(parentChains, 'parentChains')
4757
const min = range[0] || 0
4858
const max = range[range.length - 1] || 0
4959
const parent = parentChains[0][index - 1].node
50-
console.log(parent)
51-
// if (parent.root) throw new Error('Please select nodes in the same main topic.')
60+
if (parent.root) throw new Error('Please select nodes in the same main topic.')
5261

5362
// const start = parent.children![min].id
5463
// const end = parent.children![max].id
@@ -137,8 +146,13 @@ const drawSummary = function (mei: MindElixirInstance, summary: Summary) {
137146
}
138147

139148
export const createSummary = function (this: MindElixirInstance) {
140-
if (!this.currentNodes) return
141-
const { parent, start, end } = calcRange(this.currentNodes)
149+
let nodes: Topic[] = []
150+
if (this.currentNode) {
151+
nodes = [this.currentNode]
152+
} else if (this.currentNodes) {
153+
nodes = this.currentNodes
154+
}
155+
const { parent, start, end } = calcRange(nodes)
142156
const summary = { id: generateUUID(), parent, start, end, text: 'summary' }
143157
const g = drawSummary(this, summary) as SummarySvgGroup
144158
this.summaries.push(summary)
@@ -227,7 +241,6 @@ export const editSummary = function (this: MindElixirInstance, el: SummarySvgGro
227241
if (!div) return
228242
const node = el.summaryObj
229243
const text = div.textContent?.trim() || ''
230-
console.log(text)
231244
if (text === '') node.text = origin
232245
else node.text = text
233246
div.remove()

0 commit comments

Comments
 (0)