-
-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathindex.ts
184 lines (162 loc) · 5.59 KB
/
index.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import './index.less'
import './iconfont/iconfont.js'
import { LEFT, RIGHT, SIDE, DARK_THEME, THEME } from './const'
import { generateUUID } from './utils/index'
import initMouseEvent from './mouse'
import Bus from './utils/pubsub'
import { findEle } from './utils/dom'
import { createLinkSvg, createLine } from './utils/svg'
import dragMoveHelper from './utils/dragMoveHelper'
import type { MindElixirData, MindElixirInstance, MindElixirMethods, Options } from './types/index'
import methods from './methods'
import { sub, main } from './utils/generateBranch'
// @ts-expect-error json file
import { version } from '../package.json'
// TODO show up animation
const $d = document
function MindElixir(
this: MindElixirInstance,
{
el,
direction,
locale,
draggable,
editable,
contextMenu,
contextMenuOption,
toolBar,
keypress,
mouseSelectionButton,
selectionContainer,
before,
newTopicName,
allowUndo,
generateMainBranch,
generateSubBranch,
overflowHidden,
theme,
alignment,
}: Options
): void {
let ele: HTMLElement | null = null
const elType = Object.prototype.toString.call(el)
if (elType === '[object HTMLDivElement]') {
ele = el as HTMLElement
} else if (elType === '[object String]') {
ele = document.querySelector(el as string) as HTMLElement
}
if (!ele) throw new Error('MindElixir: el is not a valid element')
ele.style.position = 'relative'
ele.innerHTML = ''
this.mindElixirBox = ele as HTMLElement
this.disposable = []
this.before = before || {}
this.locale = locale || 'en'
this.contextMenuOption = contextMenuOption
this.contextMenu = contextMenu === undefined ? true : contextMenu
this.toolBar = toolBar === undefined ? true : toolBar
this.keypress = keypress === undefined ? true : keypress
this.mouseSelectionButton = mouseSelectionButton || 0
// record the direction before enter focus mode, must true in focus mode, reset to null after exit focus
this.direction = typeof direction === 'number' ? direction : 1
this.draggable = draggable === undefined ? true : draggable
this.newTopicName = newTopicName || 'new node'
this.editable = editable === undefined ? true : editable
this.allowUndo = allowUndo === undefined ? false : allowUndo
// this.parentMap = {} // deal with large amount of nodes
this.currentNode = null // the selected <tpc/> element
this.currentArrow = null // the selected link svg element
this.scaleVal = 1
this.tempDirection = null
this.generateMainBranch = generateMainBranch || main
this.generateSubBranch = generateSubBranch || sub
this.overflowHidden = overflowHidden || false
this.bus = Bus.create()
this.container = $d.createElement('div') // map container
this.selectionContainer = selectionContainer || this.container
this.container.className = 'map-container'
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
this.theme = theme || (mediaQuery.matches ? DARK_THEME : THEME)
// infrastructure
const canvas = $d.createElement('div') // map-canvas Element
canvas.className = 'map-canvas'
this.map = canvas
this.map.setAttribute('tabindex', '0')
this.container.appendChild(this.map)
this.mindElixirBox.appendChild(this.container)
this.nodes = $d.createElement('me-nodes')
this.nodes.className = 'main-node-container'
this.lines = createLinkSvg('lines') // main link container
this.summarySvg = createLinkSvg('summary') // summary container
this.linkController = createLinkSvg('linkcontroller') // bezier controller container
this.P2 = $d.createElement('div') // bezier P2
this.P3 = $d.createElement('div') // bezier P3
this.P2.className = this.P3.className = 'circle'
this.P2.style.display = this.P3.style.display = 'none'
this.line1 = createLine() // bezier auxiliary line1
this.line2 = createLine() // bezier auxiliary line2
this.linkController.appendChild(this.line1)
this.linkController.appendChild(this.line2)
this.linkSvgGroup = createLinkSvg('topiclinks') // storage user custom link svg
this.alignment = alignment ?? 'root'
this.map.appendChild(this.nodes)
if (this.overflowHidden) {
this.container.style.overflow = 'hidden'
} else initMouseEvent(this)
}
MindElixir.prototype = methods
MindElixir.LEFT = LEFT
MindElixir.RIGHT = RIGHT
MindElixir.SIDE = SIDE
MindElixir.THEME = THEME
MindElixir.DARK_THEME = DARK_THEME
/**
* @memberof MindElixir
* @static
*/
MindElixir.version = version
/**
* @function
* @memberof MindElixir
* @static
* @name E
* @param {string} id Node id.
* @return {TargetElement} Target element.
* @example
* E('bd4313fbac40284b')
*/
MindElixir.E = findEle
/**
* @function new
* @memberof MindElixir
* @static
* @param {String} topic root topic
*/
if (import.meta.env.MODE !== 'lite') {
MindElixir.new = (topic: string): MindElixirData => ({
nodeData: {
id: generateUUID(),
topic: topic || 'new topic',
children: [],
},
})
}
MindElixir.dragMoveHelper = dragMoveHelper
export interface MindElixirCtor {
new (options: Options): MindElixirInstance
E: typeof findEle
new: typeof MindElixir.new
version: string
LEFT: typeof LEFT
RIGHT: typeof RIGHT
SIDE: typeof SIDE
THEME: typeof THEME
DARK_THEME: typeof DARK_THEME
prototype: MindElixirMethods
dragMoveHelper: typeof dragMoveHelper
}
export default MindElixir as unknown as MindElixirCtor
// types
export type * from './utils/pubsub'
export type * from './types/index'
export type * from './types/dom'