|
69 | 69 |
|
70 | 70 | CodeMirror.on(node, "mouseout", hide); |
71 | 71 | } |
| 72 | + |
| 73 | + function showMenu (cm, annotations, e) { |
| 74 | + var target = e.target || e.srcElement; |
| 75 | + var state = cm.state.lint |
| 76 | + |
| 77 | + if (!(state.options.menus && state.options.menus.length > 0)) { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + /** @type {Array<{ content: string, html: string, onClick: any }>} */ |
| 82 | + const menus = state.options.menus |
| 83 | + |
| 84 | + // build menu |
| 85 | + var hints = document.createElement("ul"); |
| 86 | + var theme = cm.options.theme; |
| 87 | + hints.className = "CodeMirror-hints " + theme; |
| 88 | + hints.style.position = 'fixed' |
| 89 | + hints.style.zIndex = '999' |
| 90 | + |
| 91 | + for (let item of menus) { |
| 92 | + const elt = hints.appendChild(document.createElement('li')) |
| 93 | + elt.className = 'CodeMirror-hint' |
| 94 | + if (item.content) { |
| 95 | + elt.textContent = item.content |
| 96 | + } else { |
| 97 | + elt.innerHTML = item.html |
| 98 | + } |
| 99 | + const onClick = item.onClick |
| 100 | + elt.addEventListener('click', (e) => { |
| 101 | + onClick(e, annotations) |
| 102 | + remove() |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + function remove () { |
| 107 | + if (hints.parentNode) { |
| 108 | + hints.parentNode.removeChild(hints) |
| 109 | + } |
| 110 | + state.hints = null |
| 111 | + } |
| 112 | + |
| 113 | + if (state.hints) { |
| 114 | + remove() |
| 115 | + } |
| 116 | + |
| 117 | + state.hints = hints |
| 118 | + document.body.appendChild(hints) |
| 119 | + const { left, top } = target.getBoundingClientRect() |
| 120 | + |
| 121 | + hints.style.top = top + 5 + 'px' |
| 122 | + hints.style.left = left + 20 + 'px' |
| 123 | + } |
72 | 124 |
|
73 | 125 | function LintState(cm, options, hasGutter) { |
74 | 126 | this.marked = []; |
|
94 | 146 | state.marked.length = 0; |
95 | 147 | } |
96 | 148 |
|
97 | | - function makeMarker(cm, labels, severity, multiple, tooltips) { |
| 149 | + function makeMarker(cm, labels, severity, multiple, tooltips, annotations) { |
98 | 150 | var marker = document.createElement("div"), inner = marker; |
99 | 151 | marker.className = "CodeMirror-lint-marker-" + severity; |
100 | 152 | if (multiple) { |
|
106 | 158 | showTooltipFor(cm, e, labels, inner); |
107 | 159 | }); |
108 | 160 |
|
| 161 | + if (cm.state.lint.options.contextmenu) { |
| 162 | + 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) |
| 167 | + }) |
| 168 | + } |
| 169 | + |
109 | 170 | return marker; |
110 | 171 | } |
111 | 172 |
|
|
203 | 264 |
|
204 | 265 | if (state.hasGutter) |
205 | 266 | cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, anns.length > 1, |
206 | | - state.options.tooltips)); |
| 267 | + state.options.tooltips, anns)); |
207 | 268 | } |
208 | 269 | if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm); |
209 | 270 | } |
|
224 | 285 | } |
225 | 286 | showTooltipFor(cm, e, tooltip, target); |
226 | 287 | } |
227 | | - |
228 | | - function onMouseOver(cm, e) { |
| 288 | + |
| 289 | + function handleMarkerAction (cm, e, cb) { |
229 | 290 | var target = e.target || e.srcElement; |
230 | 291 | if (!/\bCodeMirror-lint-mark-/.test(target.className)) return; |
231 | 292 | var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2; |
|
245 | 306 | } |
246 | 307 |
|
247 | 308 | function onClick (cm, e) { |
248 | | - handleMarkerAction(cm, e, function popupContextMenu (cm, annotations, e) { |
249 | | - console.log(`WIP: click action, ${JSON.stringify(annotations)}`) |
250 | | - }) |
| 309 | + if (typeof cm.state.lint.options.onClick === 'function') { |
| 310 | + cm.state.lint.options.onClick(cm, e) |
| 311 | + } |
| 312 | + handleMarkerAction(cm, e, showMenu) |
251 | 313 | } |
252 | 314 |
|
253 | 315 | CodeMirror.defineOption("lint", false, function(cm, val, old) { |
|
0 commit comments