1
|
|
|
|
2
|
|
|
class MenuItem { |
3
|
|
|
/** |
4
|
|
|
* Determine if this MenuItem is currently active at the cursor position |
5
|
|
|
* |
6
|
|
|
* @param {EditorState} editorState The current EditorView.state |
7
|
|
|
* |
8
|
|
|
* @return {boolean} |
9
|
|
|
*/ |
10
|
|
|
isActive(editorState) { // eslint-disable-line class-methods-use-this,no-unused-vars |
|
|
|
|
11
|
|
|
return false; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create a new MenuItem |
16
|
|
|
* |
17
|
|
|
* The constructor needs the following keys: |
18
|
|
|
* |
19
|
|
|
* command: must be command function created by prosemirror-commands or similar |
20
|
|
|
* |
21
|
|
|
* It should have at least one of the following: |
22
|
|
|
* render: a function that gets the EditorView and returns a DOM node |
23
|
|
|
* icon: an Element that contains an icon for the menu-button (ideally inline svg) |
24
|
|
|
* label: A human readable label for the menu item |
25
|
|
|
* |
26
|
|
|
* Optional: |
27
|
|
|
* isActive: A function that gets the EditorView and returns true if the item as active and false otherwise |
28
|
|
|
* update: A function that is called on update, gets the EditorView and this.dom and can change the latter |
29
|
|
|
* |
30
|
|
|
* @param {object} options |
31
|
|
|
*/ |
32
|
|
|
constructor(options = {}) { |
33
|
|
|
if (typeof options.command !== 'function') { |
34
|
|
|
throw new Error('command is not a function!'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
this.options = options; |
38
|
|
|
|
39
|
|
|
if (typeof options.isActive === 'function') { |
40
|
|
|
this.isActive = options.isActive; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Render the menu icon and attach listeners |
46
|
|
|
* |
47
|
|
|
* @param {EditorView} editorView |
48
|
|
|
* @return {Element} |
49
|
|
|
*/ |
50
|
|
|
render(editorView) { |
51
|
|
|
let dom; |
52
|
|
|
if (typeof this.options.render === 'function') { |
53
|
|
|
dom = this.options.render(editorView); |
54
|
|
|
} else if (this.options.icon instanceof Element) { |
|
|
|
|
55
|
|
|
dom = MenuItem.renderSVGIcon(this.options.icon, this.options.label); |
56
|
|
|
} else if (typeof this.options.label === 'string') { |
57
|
|
|
dom = jQuery('<span>') |
58
|
|
|
.addClass('menuitem menulabel') |
59
|
|
|
.attr('title', this.options.label) |
60
|
|
|
.text(this.options.label[0]) |
61
|
|
|
.get(0); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
dom.addEventListener('mousedown', (e) => { |
|
|
|
|
65
|
|
|
e.preventDefault(); |
66
|
|
|
// editorView.focus(); |
67
|
|
|
this.options.command(editorView.state, editorView.dispatch, editorView); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
this.dom = dom; |
71
|
|
|
return dom; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* |
77
|
|
|
* @param {EditorView} editorView |
78
|
|
|
* |
79
|
|
|
* @return {void} |
80
|
|
|
*/ |
81
|
|
|
update(editorView) { |
82
|
|
|
if (typeof this.options.update === 'function') { |
83
|
|
|
this.options.update(editorView, this.dom); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (this.isActive(editorView.state)) { |
87
|
|
|
this.dom.classList.add('is-active'); |
88
|
|
|
} else { |
89
|
|
|
this.dom.classList.remove('is-active'); |
90
|
|
|
} |
91
|
|
|
this.dom.style.display = this.options.command(editorView.state, null, editorView) ? '' : 'none'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add classes and title (if available) to the icon |
96
|
|
|
* |
97
|
|
|
* @param {HTMLSpanElement} icon <span>-element with the icon inside |
98
|
|
|
* @param {string} title Title to display |
99
|
|
|
* @return {HTMLSpanElement} |
100
|
|
|
*/ |
101
|
|
|
static renderSVGIcon(icon, title = '') { |
102
|
|
|
const $span = jQuery('<span>'); |
103
|
|
|
$span.addClass('menuitem'); |
104
|
|
|
$span.append(jQuery(icon).addClass('menuicon').attr('title', title)); |
105
|
|
|
if (title) { |
106
|
|
|
$span.append(jQuery('<span>').text(title).addClass('menulabel')); |
107
|
|
|
} |
108
|
|
|
return $span.get(0); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
export default MenuItem; |
113
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.