Skip to content

Commit 35f45b2

Browse files
committed
Simplify contextmenu api
1 parent 46abded commit 35f45b2

2 files changed

Lines changed: 23 additions & 19 deletions

File tree

addon/lint/lint.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,19 @@
7070
CodeMirror.on(node, "mouseout", hide);
7171
}
7272

73-
function showMenu (cm, annotations, e) {
73+
/**
74+
*
75+
* @param {*} cm
76+
* @param {Array<{ content: string, html: string, onClick: any }>} menus
77+
* @param {*} e
78+
*/
79+
function showMenu (cm, menus, e) {
7480
var target = e.target || e.srcElement;
7581
var state = cm.state.lint
7682

77-
if (!(state.options.menus && state.options.menus.length > 0)) {
83+
if (!menus && menus.length > 0) {
7884
return
7985
}
80-
81-
/** @type {Array<{ content: string, html: string, onClick: any }>} */
82-
const menus = state.options.menus
8386

8487
// build menu
8588
var hints = document.createElement("ul");
@@ -98,7 +101,9 @@
98101
}
99102
const onClick = item.onClick
100103
elt.addEventListener('click', (e) => {
101-
onClick(e, annotations)
104+
if (typeof onClick === 'function') {
105+
onClick(e)
106+
}
102107
remove()
103108
})
104109
}
@@ -130,6 +135,8 @@
130135
this.onMouseOver = function(e) { onMouseOver(cm, e); };
131136
this.onClick = function(e) { onClick(cm, e); };
132137
this.waitingFor = 0
138+
139+
this.contextMenuEnable = typeof options.contextmenu === 'function'
133140
}
134141

135142
function parseOptions(_cm, options) {
@@ -158,12 +165,10 @@
158165
showTooltipFor(cm, e, labels, inner);
159166
});
160167

161-
if (cm.state.lint.options.contextmenu) {
168+
if (cm.state.lint.contextMenuEnable) {
162169
marker.addEventListener('click', function (e) {
163-
if (typeof cm.state.lint.options.onClick === 'function') {
164-
cm.state.lint.options.onClick(cm, e)
165-
}
166-
showMenu(cm, annotations, e)
170+
const menus = cm.state.lint.options.contextmenu(annotations)
171+
showMenu(cm, menus, e)
167172
})
168173
}
169174

@@ -306,10 +311,10 @@
306311
}
307312

308313
function onClick (cm, e) {
309-
if (typeof cm.state.lint.options.onClick === 'function') {
310-
cm.state.lint.options.onClick(cm, e)
311-
}
312-
handleMarkerAction(cm, e, showMenu)
314+
handleMarkerAction(cm, e, function (cm, annotations, e) {
315+
const menus = cm.state.lint.options.contextmenu(annotations)
316+
showMenu(cm, menus, e)
317+
})
313318
}
314319

315320
CodeMirror.defineOption("lint", false, function(cm, val, old) {
@@ -330,7 +335,7 @@
330335
cm.on("change", onChange);
331336
if (state.options.tooltips != false && state.options.tooltips != "gutter")
332337
CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
333-
if (state.options.contextmenu) {
338+
if (state.contextMenuEnable) {
334339
CodeMirror.on(cm.getWrapperElement(), "click", state.onClick);
335340
}
336341

demo/lint.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ <h2>Linter Demo</h2>
167167
gutters: ["CodeMirror-lint-markers"],
168168
lint: {
169169
fixedTooltip: true,
170-
contextmenu: true,
171-
menus: [
170+
contextmenu: annotations => ([
172171
{
173172
content: 'Fix me',
174173
onClick (e, annotations) {
@@ -181,7 +180,7 @@ <h2>Linter Demo</h2>
181180
console.log(annotations)
182181
}
183182
}
184-
]
183+
])
185184
}
186185
});
187186
</script>

0 commit comments

Comments
 (0)