generated from SSShooter/mind-elixir-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
nodeMenu.ts
260 lines (252 loc) · 8.37 KB
/
nodeMenu.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import './nodeMenu.less'
import './icons/iconfont.js'
import i18n from './i18n.js'
import type { MindElixirInstance } from 'mind-elixir'
const createDiv = (id, innerHTML) => {
const div = document.createElement('div')
div.id = id
div.innerHTML = innerHTML
return div
}
const colorList = [
'#2c3e50',
'#34495e',
'#7f8c8d',
'#94a5a6',
'#bdc3c7',
'#ecf0f1',
'#8e44ad',
'#9b59b6',
'#2980b9',
'#3298db',
'#c0392c',
'#e74c3c',
'#d35400',
'#f39c11',
'#f1c40e',
'#17a085',
'#27ae61',
'#2ecc71',
]
export default function (mind: MindElixirInstance) {
console.log('install node menu')
function clearSelect(klass, remove) {
const elems = mind.container.querySelectorAll(klass)
;[].forEach.call(elems, function (el) {
el.classList.remove(remove)
})
}
// create element
const locale = i18n[mind.locale] ? mind.locale : 'en'
const styleDiv = createDiv(
'nm-style',
`
<div class="nm-fontsize-container">
${['15', '24', '32']
.map((size) => {
return `<div class="size" data-size="${size}">
<svg class="icon" style="width: ${size}px;height: ${size}px" aria-hidden="true">
<use xlink:href="#icon-a"></use>
</svg></div>`
})
.join('')}<div class="bold"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-B"></use>
</svg></div>
</div>
<div class="nm-fontcolor-container">
${colorList
.map((color) => {
return `<div class="split6"><div class="palette" data-color="${color}" style="background-color: ${color};"></div></div>`
})
.join('')}
</div>
<div class="bof">
<span class="font">${i18n[locale].font}</span>
<span class="background">${i18n[locale].background}</span>
</div>`
)
const tagDiv = createDiv(
'nm-tag',
`${i18n[locale].tag}<input class="nm-tag" tabindex="-1" placeholder="${i18n[locale].tagsSeparate}" />`
)
const iconDiv = createDiv(
'nm-icon',
`${i18n[locale].icon}<input class="nm-icon" tabindex="-1" placeholder="${i18n[locale].iconsSeparate}" />`
)
const urlDiv = createDiv(
'nm-url',
`${i18n[locale].url}<input class="nm-url" tabindex="-1" />`
)
const memoDiv = createDiv(
'nm-memo',
`${
i18n[locale].memo || 'Memo'
}<textarea class="nm-memo" rows="5" tabindex="-1" />`
)
// create container
const menuContainer = document.createElement('div')
menuContainer.className = 'node-menu'
menuContainer.innerHTML = `
<div class="button-container"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-close"></use>
</svg></div>
`
menuContainer.appendChild(styleDiv)
menuContainer.appendChild(tagDiv)
menuContainer.appendChild(iconDiv)
menuContainer.appendChild(urlDiv)
menuContainer.appendChild(memoDiv)
menuContainer.hidden = true
mind.container.append(menuContainer)
// query input element
const sizeSelector = menuContainer.querySelectorAll('.size')
const bold: HTMLElement = menuContainer.querySelector('.bold')
const buttonContainer: HTMLElement =
menuContainer.querySelector('.button-container')
const fontBtn: HTMLElement = menuContainer.querySelector('.font')
const tagInput: HTMLInputElement = mind.container.querySelector('.nm-tag')
const iconInput: HTMLInputElement = mind.container.querySelector('.nm-icon')
const urlInput: HTMLInputElement = mind.container.querySelector('.nm-url')
const memoInput: HTMLInputElement = mind.container.querySelector('.nm-memo')
// handle input and button click
let bgOrFont
const E = mind.findEle
menuContainer.onclick = (e) => {
if (!mind.currentNode) return
const nodeObj = mind.currentNode.nodeObj
const target = e.target as HTMLElement
if (target.className === 'palette') {
clearSelect('.palette', 'nmenu-selected')
target.className = 'palette nmenu-selected'
const color = target.dataset.color
const patch = { style: {} as any }
if (bgOrFont === 'font') {
patch.style.color = color
} else if (bgOrFont === 'background') {
patch.style.background = color
}
console.log(patch)
mind.reshapeNode(mind.currentNode, patch)
} else if (target.className === 'background') {
clearSelect('.palette', 'nmenu-selected')
bgOrFont = 'background'
target.className = 'background selected'
target.previousElementSibling.className = 'font'
if (nodeObj.style && nodeObj.style.background) {
menuContainer.querySelector(
'.palette[data-color="' + nodeObj.style.background + '"]'
).className = 'palette nmenu-selected'
}
} else if (target.className === 'font') {
clearSelect('.palette', 'nmenu-selected')
bgOrFont = 'font'
target.className = 'font selected'
target.nextElementSibling.className = 'background'
if (nodeObj.style && nodeObj.style.color) {
menuContainer.querySelector(
'.palette[data-color="' + nodeObj.style.color + '"]'
).className = 'palette nmenu-selected'
}
}
}
Array.from(sizeSelector).map((dom) => {
;(dom as HTMLElement).onclick = (e) => {
clearSelect('.size', 'size-selected')
const size = e.currentTarget as HTMLElement
size.className = 'size size-selected'
mind.reshapeNode(mind.currentNode, {
style: { fontSize: size.dataset.size },
})
}
})
bold.onclick = (e: MouseEvent & { currentTarget: Element }) => {
let fontWeight = ''
if (mind.currentNode.nodeObj?.style?.fontWeight === 'bold') {
e.currentTarget.className = 'bold'
} else {
fontWeight = 'bold'
e.currentTarget.className = 'bold size-selected'
}
mind.reshapeNode(mind.currentNode, { style: { fontWeight } })
}
tagInput.onchange = (e: InputEvent & { target: HTMLInputElement }) => {
if (!mind.currentNode) return
if (typeof e.target.value === 'string') {
const newTags = e.target.value.split(',')
mind.reshapeNode(mind.currentNode, { tags: newTags.filter((tag) => tag) })
}
}
iconInput.onchange = (e: InputEvent & { target: HTMLInputElement }) => {
if (!mind.currentNode) return
if (typeof e.target.value === 'string') {
const newIcons = e.target.value.split(',')
mind.reshapeNode(mind.currentNode, {
icons: newIcons.filter((icon) => icon),
})
}
}
urlInput.onchange = (e: InputEvent & { target: HTMLInputElement }) => {
if (!mind.currentNode) return
mind.reshapeNode(mind.currentNode, { hyperLink: e.target.value })
}
memoInput.onchange = (e: InputEvent & { target: HTMLInputElement }) => {
if (!mind.currentNode) return
mind.currentNode.nodeObj.memo = e.target.value
mind.bus.fire('operation', {
name: 'updateMemo',
obj: mind.currentNode.nodeObj,
})
}
let state = 'open'
buttonContainer.onclick = (e) => {
menuContainer.classList.toggle('close')
if (state === 'open') {
state = 'close'
buttonContainer.innerHTML = `<svg class="icon" aria-hidden="true"><use xlink:href="#icon-menu"></use></svg>`
} else {
state = 'open'
buttonContainer.innerHTML = `<svg class="icon" aria-hidden="true"><use xlink:href="#icon-close"></use></svg>`
}
}
// handle node selection
mind.bus.addListener('unselectNode', function () {
menuContainer.hidden = true
})
mind.bus.addListener('selectNode', function (nodeObj, clickEvent) {
if (!clickEvent) return
menuContainer.hidden = false
clearSelect('.palette', 'nmenu-selected')
clearSelect('.size', 'size-selected')
clearSelect('.bold', 'size-selected')
bgOrFont = 'font'
fontBtn.className = 'font selected'
fontBtn.nextElementSibling.className = 'background'
if (nodeObj.style) {
if (nodeObj.style.fontSize) {
menuContainer.querySelector(
'.size[data-size="' + nodeObj.style.fontSize + '"]'
).className = 'size size-selected'
}
if (nodeObj.style.fontWeight) {
menuContainer.querySelector('.bold').className = 'bold size-selected'
}
if (nodeObj.style.color) {
menuContainer.querySelector(
'.palette[data-color="' + nodeObj.style.color + '"]'
).className = 'palette nmenu-selected'
}
}
if (nodeObj.tags) {
tagInput.value = nodeObj.tags.join(',')
} else {
tagInput.value = ''
}
if (nodeObj.icons) {
iconInput.value = nodeObj.icons.join(',')
} else {
iconInput.value = ''
}
urlInput.value = nodeObj.hyperLink || ''
memoInput.value = nodeObj.memo || ''
})
}