-
-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathdev.ts
135 lines (121 loc) · 3.29 KB
/
dev.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import type { MindElixirCtor } from './index'
import MindElixir from './index'
import example from './exampleData/1'
import example2 from './exampleData/2'
import example3 from './exampleData/3'
import type { Options, MindElixirData, MindElixirInstance } from './types/index'
import type { Operation } from './utils/pubsub'
import style from '../index.css?raw'
import katex from '../katex.css?raw'
interface Window {
m?: MindElixirInstance
M: MindElixirCtor
E: typeof MindElixir.E
downloadPng: ReturnType<typeof download>
downloadSvg: ReturnType<typeof download>
destroy: () => void
}
declare let window: Window
const E = MindElixir.E
const options: Options = {
el: '#map',
newTopicName: '子节点',
direction: MindElixir.SIDE,
// direction: MindElixir.RIGHT,
locale: 'en',
// mouseSelectionButton: 2,
draggable: true,
editable: true,
// if you set contextMenu to false, you should handle contextmenu event by yourself, e.g. preventDefault
contextMenu: true,
contextMenuOption: {
focus: true,
link: true,
extend: [
{
name: 'Node edit',
onclick: () => {
alert('extend menu')
},
},
],
},
toolBar: true,
nodeMenu: true,
keypress: true,
allowUndo: true,
before: {
insertSibling(el, obj) {
console.log('insertSibling', el, obj)
return true
},
async addChild(el, obj) {
console.log('addChild', el, obj)
// await sleep()
return true
},
},
}
let mind = new MindElixir(options)
const data = MindElixir.new('new topic')
mind.init(example)
const m2 = new MindElixir({
el: '#map2',
selectionContainer: 'body', // use body to make selection usable when transform is not 0
})
m2.init(data)
function sleep() {
return new Promise<void>(res => {
setTimeout(() => res(), 1000)
})
}
// console.log('test E function', E('bd4313fbac40284b'))
mind.bus.addListener('operation', (operation: Operation) => {
console.log(operation)
// return {
// name: action name,
// obj: target object
// }
// name: [insertSibling|addChild|removeNode|beginEdit|finishEdit]
// obj: target
// name: moveNodeIn
// obj: {from:target1,to:target2}
})
mind.bus.addListener('selectNode', node => {
console.log(node)
})
mind.bus.addListener('expandNode', node => {
console.log('expandNode: ', node)
})
const download = (type: 'svg' | 'png') => {
return async () => {
try {
let blob = null
if (type === 'png') blob = await mind.exportPng(false, style + katex)
else blob = await mind.exportSvg(false, style + katex)
if (!blob) return
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = 'filename.' + type
a.click()
URL.revokeObjectURL(url)
} catch (e) {
console.error(e)
}
}
}
window.downloadPng = download('png')
window.downloadSvg = download('svg')
window.m = mind
// window.m2 = mind2
window.M = MindElixir
window.E = MindElixir.E
console.log('MindElixir Version', MindElixir.version)
window.destroy = () => {
mind.destroy()
// @ts-expect-error remove reference
mind = null
// @ts-expect-error remove reference
window.m = null
}