1
|
|
|
const { MenuItem } = require('./MenuItem'); |
2
|
|
|
|
3
|
|
|
class Dropdown extends MenuItem { |
4
|
|
|
constructor(content, options) { |
5
|
|
|
console.log('constructing Dropdown'); |
|
|
|
|
6
|
|
|
console.log(options); |
7
|
|
|
super({ |
8
|
|
|
command: () => true, |
9
|
|
|
render: view => this.renderIcon(view, options), |
10
|
|
|
}); |
11
|
|
|
this.content = Array.isArray(content) ? content : [content]; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
renderIcon(editorView, options) { |
15
|
|
|
console.log('rendering Dropdown Icon'); |
|
|
|
|
16
|
|
|
const $menuItemContainer = jQuery('<span>').addClass('dropdown'); |
17
|
|
|
const $dropdownLabel = jQuery('<span>').text(options.label).addClass('menuitem menulabel'); |
18
|
|
|
$menuItemContainer.append($dropdownLabel); |
19
|
|
|
|
20
|
|
|
jQuery($dropdownLabel).on('mousedown', (e) => { |
21
|
|
|
e.preventDefault(); |
22
|
|
|
e.stopPropagation(); |
23
|
|
|
if (this.open) { |
24
|
|
|
this.open = false; |
25
|
|
|
} else { |
26
|
|
|
this.open = true; // TODO: add listener to close it by clicking anywhere else |
27
|
|
|
} |
28
|
|
|
this.renderDropdownItems(editorView, $menuItemContainer); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
return $menuItemContainer.get(0); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
renderDropdownItems(editorView, $menuItemContainer) { |
35
|
|
|
if (!this.open) { |
36
|
|
|
jQuery(this.contentDom).remove(); |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
this.contentDom = document.createElement('div'); |
40
|
|
|
this.contentDom.className = 'dropdown_content'; |
41
|
|
|
this.content.forEach((item) => { |
42
|
|
|
const itemDom = item.render(editorView); |
43
|
|
|
this.contentDom.appendChild(itemDom); |
44
|
|
|
}); |
45
|
|
|
$menuItemContainer.append(this.contentDom); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
update(editorView) { |
49
|
|
|
if (!this.open) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
this.content.forEach((item) => { |
53
|
|
|
item.update(editorView); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
exports.Dropdown = Dropdown; |
59
|
|
|
|