Skip to content

Commit dc788ae

Browse files
committed
feat: add destroy()
fixed #269
1 parent 5b56c90 commit dc788ae

File tree

9 files changed

+63
-12
lines changed

9 files changed

+63
-12
lines changed

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": "4.0.5",
3+
"version": "4.1.0",
44
"type": "module",
55
"description": "Mind elixir is a free open source mind map core.",
66
"keywords": [

src/arrow.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ export function editArrowLabel(this: MindElixirInstance, el: CustomSvg) {
308308
console.time('editSummary')
309309
if (!el) return
310310
const textEl = el.children[2]
311-
console.log(textEl, el)
312311
editSvgText(this, textEl, div => {
313312
const node = el.arrowObj
314313
const text = div.textContent?.trim() || ''

src/dev.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import style from '../index.css?raw'
99
import katex from '../katex.css?raw'
1010

1111
interface Window {
12-
m: MindElixirInstance
12+
m?: MindElixirInstance
1313
M: MindElixirCtor
1414
E: typeof MindElixir.E
1515
downloadPng: ReturnType<typeof download>
1616
downloadSvg: ReturnType<typeof download>
17+
destroy: () => void
1718
}
1819

1920
declare let window: Window
@@ -58,7 +59,7 @@ const options: Options = {
5859
},
5960
}
6061

61-
const mind = new MindElixir(options)
62+
let mind = new MindElixir(options)
6263

6364
const data = MindElixir.new('new topic')
6465
mind.init(example)
@@ -73,7 +74,7 @@ function sleep() {
7374
setTimeout(() => res(), 1000)
7475
})
7576
}
76-
console.log('test E function', E('bd4313fbac40284b'))
77+
// console.log('test E function', E('bd4313fbac40284b'))
7778

7879
mind.bus.addListener('operation', (operation: Operation) => {
7980
console.log(operation)
@@ -120,3 +121,11 @@ window.m = mind
120121
// window.m2 = mind2
121122
window.M = MindElixir
122123
window.E = MindElixir.E
124+
125+
window.destroy = () => {
126+
mind.destroy()
127+
// @ts-expect-error remove reference
128+
mind = null
129+
// @ts-expect-error remove reference
130+
window.m = null
131+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function MindElixir(
3939
theme,
4040
}: Options
4141
): void {
42-
console.log('ME_version ' + MindElixir.version, this)
4342
let ele: HTMLElement | null = null
4443
const elType = Object.prototype.toString.call(el)
4544
if (elType === '[object HTMLDivElement]') {
@@ -53,6 +52,7 @@ function MindElixir(
5352
ele.innerHTML = ''
5453
ele.style.setProperty('--gap', GAP + 'px')
5554
this.mindElixirBox = ele as HTMLElement
55+
this.disposable = []
5656
this.before = before || {}
5757
this.locale = locale || 'en'
5858
this.contextMenuOption = contextMenuOption
@@ -129,7 +129,7 @@ MindElixir.DARK_THEME = DARK_THEME
129129
* @memberof MindElixir
130130
* @static
131131
*/
132-
MindElixir.version = '4.0.5'
132+
MindElixir.version = '4.1.0'
133133
/**
134134
* @function
135135
* @memberof MindElixir

src/methods.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ const methods = {
9191
}
9292
if (isMobile() && this.mobileMenu) {
9393
mobileMenu(this)
94-
} else {
95-
this.contextMenu && contextMenu(this, this.contextMenuOption)
94+
} else if (this.contextMenu) {
95+
this.disposable.push(contextMenu(this, this.contextMenuOption))
9696
}
9797
this.draggable && nodeDraggable(this)
9898
this.allowUndo && operationHistory(this)
@@ -101,6 +101,34 @@ const methods = {
101101
this.layout()
102102
this.linkDiv()
103103
},
104+
destroy(this: Partial<MindElixirInstance>) {
105+
this.disposable!.forEach(fn => fn())
106+
this.mindElixirBox?.remove()
107+
this.mindElixirBox = undefined
108+
this.nodeData = undefined
109+
this.arrows = undefined
110+
this.summaries = undefined
111+
this.currentArrow = undefined
112+
this.currentNode = undefined
113+
this.currentNodes = undefined
114+
this.currentSummary = undefined
115+
this.waitCopy = undefined
116+
this.theme = undefined
117+
this.direction = undefined
118+
this.bus = undefined
119+
this.container = undefined
120+
this.map = undefined
121+
this.lines = undefined
122+
this.linkController = undefined
123+
this.linkSvgGroup = undefined
124+
this.P2 = undefined
125+
this.P3 = undefined
126+
this.line1 = undefined
127+
this.line2 = undefined
128+
this.nodes = undefined
129+
this.selection?.destroy()
130+
this.selection = undefined
131+
},
104132
}
105133

106134
export default methods

src/plugin/contextMenu.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,19 @@ export default function (mind: MindElixirInstance, option: any) {
194194
mind.createSummary()
195195
mind.unselectNodes()
196196
}
197+
return () => {
198+
// maybe usefull?
199+
add_child.onclick = null
200+
add_parent.onclick = null
201+
add_sibling.onclick = null
202+
remove_child.onclick = null
203+
focus.onclick = null
204+
unfocus.onclick = null
205+
up.onclick = null
206+
down.onclick = null
207+
link.onclick = null
208+
summary.onclick = null
209+
menuContainer.onclick = null
210+
mind.container.oncontextmenu = null
211+
}
197212
}

src/plugin/operationHistory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default function (mei: MindElixirInstance) {
6262
if (h.currentObject.type === 'node') mei.selectNode(findEle(h.currentObject.value))
6363
else if (h.currentObject.type === 'nodes') mei.selectNodes(h.currentObject.value.map(id => findEle(id)))
6464
currentIndex--
65-
console.log('current', current)
65+
// console.log('current', current)
6666
}
6767
}
6868
mei.redo = function () {

src/types/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export type Theme = {
4040
* @public
4141
*/
4242
export interface MindElixirInstance extends MindElixirMethods {
43+
disposable: Array<() => void>
4344
isFocusMode: boolean
4445
nodeDataBackup: NodeObj
4546
mindElixirBox: HTMLElement
@@ -59,7 +60,7 @@ export interface MindElixirInstance extends MindElixirMethods {
5960
theme: Theme
6061
userTheme?: Theme
6162
direction: number
62-
locale: string
63+
locale: Locale
6364
draggable: boolean
6465
editable: boolean
6566
contextMenu: boolean

src/utils/dom.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ export const editTopic = function (this: MindElixirInstance, el: Topic) {
172172
if (!div) return
173173
const node = el.nodeObj
174174
const topic = div.textContent?.trim() || ''
175-
console.log(topic)
176175
if (topic === '') node.topic = origin
177176
else node.topic = topic
178177
div.remove()

0 commit comments

Comments
 (0)