/
widget.ts
294 lines (253 loc) · 7.06 KB
/
widget.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { HoverBox } from '@jupyterlab/ui-components';
import { CodeEditor } from '@jupyterlab/codeeditor';
import {
IRenderMime,
IRenderMimeRegistry,
MimeModel
} from '@jupyterlab/rendermime';
import { JSONObject } from '@lumino/coreutils';
import { Message } from '@lumino/messaging';
import { PanelLayout, Widget } from '@lumino/widgets';
/**
* The class name added to each tooltip.
*/
const TOOLTIP_CLASS = 'jp-Tooltip';
/**
* The class name added to the tooltip content.
*/
const CONTENT_CLASS = 'jp-Tooltip-content';
/**
* The class added to the body when a tooltip exists on the page.
*/
const BODY_CLASS = 'jp-mod-tooltip';
/**
* The minimum height of a tooltip widget.
*/
const MIN_HEIGHT = 20;
/**
* The maximum height of a tooltip widget.
*/
const MAX_HEIGHT = 250;
/**
* A flag to indicate that event handlers are caught in the capture phase.
*/
const USE_CAPTURE = true;
/**
* A tooltip widget.
*/
export class Tooltip extends Widget {
/**
* Instantiate a tooltip.
*/
constructor(options: Tooltip.IOptions) {
super();
const layout = (this.layout = new PanelLayout());
const model = new MimeModel({ data: options.bundle });
this.anchor = options.anchor;
this.addClass(TOOLTIP_CLASS);
this.hide();
this._editor = options.editor;
this._position = options.position;
this._rendermime = options.rendermime;
const mimeType = this._rendermime.preferredMimeType(options.bundle, 'any');
if (!mimeType) {
return;
}
this._content = this._rendermime.createRenderer(mimeType);
this._content
.renderModel(model)
.then(() => this._setGeometry())
.catch(error => console.error('tooltip rendering failed', error));
this._content.addClass(CONTENT_CLASS);
layout.addWidget(this._content);
}
/**
* The anchor widget that the tooltip widget tracks.
*/
readonly anchor: Widget;
/**
* Dispose of the resources held by the widget.
*/
dispose(): void {
if (this._content) {
this._content.dispose();
this._content = null;
}
super.dispose();
}
/**
* Handle the DOM events for the widget.
*
* @param event - The DOM event sent to the widget.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the dock panel's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
if (this.isHidden || this.isDisposed) {
return;
}
const { node } = this;
const target = event.target as HTMLElement;
switch (event.type) {
case 'keydown':
if (node.contains(target)) {
return;
}
this.dispose();
break;
case 'mousedown':
if (node.contains(target)) {
this.activate();
return;
}
this.dispose();
break;
case 'scroll':
this._evtScroll(event as MouseEvent);
break;
default:
break;
}
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this.node.tabIndex = 0;
this.node.focus();
}
/**
* Handle `'after-attach'` messages.
*/
protected onAfterAttach(msg: Message): void {
document.body.classList.add(BODY_CLASS);
document.addEventListener('keydown', this, USE_CAPTURE);
document.addEventListener('mousedown', this, USE_CAPTURE);
this.anchor.node.addEventListener('scroll', this, USE_CAPTURE);
this.update();
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
document.body.classList.remove(BODY_CLASS);
document.removeEventListener('keydown', this, USE_CAPTURE);
document.removeEventListener('mousedown', this, USE_CAPTURE);
this.anchor.node.removeEventListener('scroll', this, USE_CAPTURE);
}
/**
* Handle `'update-request'` messages.
*/
protected onUpdateRequest(msg: Message): void {
if (this.isHidden) {
this.show();
}
this._setGeometry();
super.onUpdateRequest(msg);
}
/**
* Handle scroll events for the widget
*/
private _evtScroll(event: MouseEvent) {
// All scrolls except scrolls in the actual hover box node may cause the
// referent editor that anchors the node to move, so the only scroll events
// that can safely be ignored are ones that happen inside the hovering node.
if (this.node.contains(event.target as HTMLElement)) {
return;
}
this.update();
}
/**
* Find the position of the first character of the current token.
*/
private _getTokenPosition(): CodeEditor.IPosition | undefined {
const editor = this._editor;
const cursor = editor.getCursorPosition();
const end = editor.getOffsetAt(cursor);
const line = editor.getLine(cursor.line);
if (!line) {
return;
}
const tokens = line.substring(0, end).split(/\W+/);
const last = tokens[tokens.length - 1];
const start = last ? end - last.length : end;
return editor.getPositionAt(start);
}
/**
* Set the geometry of the tooltip widget.
*/
private _setGeometry(): void {
// determine position for hover box placement
const position = this._position ? this._position : this._getTokenPosition();
if (!position) {
return;
}
const editor = this._editor;
const anchor = editor.getCoordinateForPosition(position);
if (!anchor) {
return;
}
const style = window.getComputedStyle(this.node);
const paddingLeft = parseInt(style.paddingLeft!, 10) || 0;
const host =
(editor.host.closest('.jp-MainAreaWidget > .lm-Widget') as HTMLElement) ||
editor.host;
// Calculate the geometry of the tooltip.
HoverBox.setGeometry({
anchor,
host,
maxHeight: MAX_HEIGHT,
minHeight: MIN_HEIGHT,
node: this.node,
offset: { horizontal: -1 * paddingLeft },
privilege: 'below',
outOfViewDisplay: {
top: 'stick-inside',
bottom: 'stick-inside'
},
style: style
});
}
private _content: IRenderMime.IRenderer | null = null;
private _editor: CodeEditor.IEditor;
private _position: CodeEditor.IPosition | undefined;
private _rendermime: IRenderMimeRegistry;
}
/**
* A namespace for tooltip widget statics.
*/
export namespace Tooltip {
/**
* Instantiation options for a tooltip widget.
*/
export interface IOptions {
/**
* The anchor widget that the tooltip widget tracks.
*/
anchor: Widget;
/**
* The data that populates the tooltip widget.
*/
bundle: JSONObject;
/**
* The editor referent of the tooltip model.
*/
editor: CodeEditor.IEditor;
/**
* The rendermime instance used by the tooltip model.
*/
rendermime: IRenderMimeRegistry;
/**
* Position at which the tooltip should be placed.
*
* If not given, the position of the first character
* in the current token will be used.
*/
position?: CodeEditor.IPosition;
}
}