@jupyterlab/tooltip 4.0.0-alpha.19 → 4.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlab/tooltip",
3
- "version": "4.0.0-alpha.19",
3
+ "version": "4.0.0-alpha.20",
4
4
  "description": "JupyterLab - Tooltip Widget",
5
5
  "homepage": "https://github.com/jupyterlab/jupyterlab",
6
6
  "bugs": {
@@ -27,7 +27,8 @@
27
27
  "lib/*.js.map",
28
28
  "lib/*.js",
29
29
  "style/*.css",
30
- "style/index.js"
30
+ "style/index.js",
31
+ "src/**/*.{ts,tsx}"
31
32
  ],
32
33
  "scripts": {
33
34
  "build": "tsc -b",
@@ -36,18 +37,18 @@
36
37
  "watch": "tsc -b --watch"
37
38
  },
38
39
  "dependencies": {
39
- "@jupyterlab/codeeditor": "^4.0.0-alpha.19",
40
- "@jupyterlab/rendermime": "^4.0.0-alpha.19",
41
- "@jupyterlab/services": "^7.0.0-alpha.19",
42
- "@jupyterlab/ui-components": "^4.0.0-alpha.34",
43
- "@lumino/coreutils": "^2.0.0-beta.0",
44
- "@lumino/messaging": "^2.0.0-beta.0",
45
- "@lumino/widgets": "^2.0.0-beta.1"
40
+ "@jupyterlab/codeeditor": "^4.0.0-alpha.20",
41
+ "@jupyterlab/rendermime": "^4.0.0-alpha.20",
42
+ "@jupyterlab/services": "^7.0.0-alpha.20",
43
+ "@jupyterlab/ui-components": "^4.0.0-alpha.35",
44
+ "@lumino/coreutils": "^2.0.0-rc.0",
45
+ "@lumino/messaging": "^2.0.0-rc.0",
46
+ "@lumino/widgets": "^2.0.0-rc.0"
46
47
  },
47
48
  "devDependencies": {
48
49
  "rimraf": "~3.0.0",
49
- "typedoc": "~0.22.10",
50
- "typescript": "~4.7.3"
50
+ "typedoc": "~0.23.25",
51
+ "typescript": "~5.0.0-beta"
51
52
  },
52
53
  "publishConfig": {
53
54
  "access": "public"
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+ /**
4
+ * @packageDocumentation
5
+ * @module tooltip
6
+ */
7
+
8
+ export * from './tokens';
9
+ export * from './widget';
package/src/tokens.ts ADDED
@@ -0,0 +1,55 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { CodeEditor } from '@jupyterlab/codeeditor';
5
+ import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
6
+ import { Kernel } from '@jupyterlab/services';
7
+ import { Token } from '@lumino/coreutils';
8
+ import { Widget } from '@lumino/widgets';
9
+
10
+ /**
11
+ * The tooltip manager token.
12
+ */
13
+ export const ITooltipManager = new Token<ITooltipManager>(
14
+ '@jupyterlab/tooltip:ITooltipManager'
15
+ );
16
+
17
+ /**
18
+ * A manager to register tooltips with parent widgets.
19
+ */
20
+ export interface ITooltipManager {
21
+ /**
22
+ * Invoke a tooltip.
23
+ */
24
+ invoke(options: ITooltipManager.IOptions): void;
25
+ }
26
+
27
+ /**
28
+ * A namespace for `ITooltipManager` interface specifications.
29
+ */
30
+ export namespace ITooltipManager {
31
+ /**
32
+ * An interface for tooltip-compatible objects.
33
+ */
34
+ export interface IOptions {
35
+ /**
36
+ * The referent anchor the tooltip follows.
37
+ */
38
+ readonly anchor: Widget;
39
+
40
+ /**
41
+ * The referent editor for the tooltip.
42
+ */
43
+ readonly editor: CodeEditor.IEditor;
44
+
45
+ /**
46
+ * The kernel the tooltip communicates with to populate itself.
47
+ */
48
+ readonly kernel: Kernel.IKernelConnection;
49
+
50
+ /**
51
+ * The renderer the tooltip uses to render API responses.
52
+ */
53
+ readonly rendermime: IRenderMimeRegistry;
54
+ }
55
+ }
package/src/widget.ts ADDED
@@ -0,0 +1,289 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { HoverBox } from '@jupyterlab/ui-components';
5
+ import { CodeEditor } from '@jupyterlab/codeeditor';
6
+ import {
7
+ IRenderMime,
8
+ IRenderMimeRegistry,
9
+ MimeModel
10
+ } from '@jupyterlab/rendermime';
11
+ import { JSONObject } from '@lumino/coreutils';
12
+ import { Message } from '@lumino/messaging';
13
+ import { PanelLayout, Widget } from '@lumino/widgets';
14
+
15
+ /**
16
+ * The class name added to each tooltip.
17
+ */
18
+ const TOOLTIP_CLASS = 'jp-Tooltip';
19
+
20
+ /**
21
+ * The class name added to the tooltip content.
22
+ */
23
+ const CONTENT_CLASS = 'jp-Tooltip-content';
24
+
25
+ /**
26
+ * The class added to the body when a tooltip exists on the page.
27
+ */
28
+ const BODY_CLASS = 'jp-mod-tooltip';
29
+
30
+ /**
31
+ * The minimum height of a tooltip widget.
32
+ */
33
+ const MIN_HEIGHT = 20;
34
+
35
+ /**
36
+ * The maximum height of a tooltip widget.
37
+ */
38
+ const MAX_HEIGHT = 250;
39
+
40
+ /**
41
+ * A flag to indicate that event handlers are caught in the capture phase.
42
+ */
43
+ const USE_CAPTURE = true;
44
+
45
+ /**
46
+ * A tooltip widget.
47
+ */
48
+ export class Tooltip extends Widget {
49
+ /**
50
+ * Instantiate a tooltip.
51
+ */
52
+ constructor(options: Tooltip.IOptions) {
53
+ super();
54
+
55
+ const layout = (this.layout = new PanelLayout());
56
+ const model = new MimeModel({ data: options.bundle });
57
+
58
+ this.anchor = options.anchor;
59
+ this.addClass(TOOLTIP_CLASS);
60
+ this.hide();
61
+ this._editor = options.editor;
62
+ this._position = options.position;
63
+ this._rendermime = options.rendermime;
64
+
65
+ const mimeType = this._rendermime.preferredMimeType(options.bundle, 'any');
66
+
67
+ if (!mimeType) {
68
+ return;
69
+ }
70
+
71
+ this._content = this._rendermime.createRenderer(mimeType);
72
+ this._content
73
+ .renderModel(model)
74
+ .then(() => this._setGeometry())
75
+ .catch(error => console.error('tooltip rendering failed', error));
76
+ this._content.addClass(CONTENT_CLASS);
77
+ layout.addWidget(this._content);
78
+ }
79
+
80
+ /**
81
+ * The anchor widget that the tooltip widget tracks.
82
+ */
83
+ readonly anchor: Widget;
84
+
85
+ /**
86
+ * Dispose of the resources held by the widget.
87
+ */
88
+ dispose(): void {
89
+ if (this._content) {
90
+ this._content.dispose();
91
+ this._content = null;
92
+ }
93
+ super.dispose();
94
+ }
95
+
96
+ /**
97
+ * Handle the DOM events for the widget.
98
+ *
99
+ * @param event - The DOM event sent to the widget.
100
+ *
101
+ * #### Notes
102
+ * This method implements the DOM `EventListener` interface and is
103
+ * called in response to events on the dock panel's node. It should
104
+ * not be called directly by user code.
105
+ */
106
+ handleEvent(event: Event): void {
107
+ if (this.isHidden || this.isDisposed) {
108
+ return;
109
+ }
110
+
111
+ const { node } = this;
112
+ const target = event.target as HTMLElement;
113
+
114
+ switch (event.type) {
115
+ case 'keydown':
116
+ if (node.contains(target)) {
117
+ return;
118
+ }
119
+ this.dispose();
120
+ break;
121
+ case 'mousedown':
122
+ if (node.contains(target)) {
123
+ this.activate();
124
+ return;
125
+ }
126
+ this.dispose();
127
+ break;
128
+ case 'scroll':
129
+ this._evtScroll(event as MouseEvent);
130
+ break;
131
+ default:
132
+ break;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Handle `'activate-request'` messages.
138
+ */
139
+ protected onActivateRequest(msg: Message): void {
140
+ this.node.tabIndex = 0;
141
+ this.node.focus();
142
+ }
143
+
144
+ /**
145
+ * Handle `'after-attach'` messages.
146
+ */
147
+ protected onAfterAttach(msg: Message): void {
148
+ document.body.classList.add(BODY_CLASS);
149
+ document.addEventListener('keydown', this, USE_CAPTURE);
150
+ document.addEventListener('mousedown', this, USE_CAPTURE);
151
+ this.anchor.node.addEventListener('scroll', this, USE_CAPTURE);
152
+ this.update();
153
+ }
154
+
155
+ /**
156
+ * Handle `before-detach` messages for the widget.
157
+ */
158
+ protected onBeforeDetach(msg: Message): void {
159
+ document.body.classList.remove(BODY_CLASS);
160
+ document.removeEventListener('keydown', this, USE_CAPTURE);
161
+ document.removeEventListener('mousedown', this, USE_CAPTURE);
162
+ this.anchor.node.removeEventListener('scroll', this, USE_CAPTURE);
163
+ }
164
+
165
+ /**
166
+ * Handle `'update-request'` messages.
167
+ */
168
+ protected onUpdateRequest(msg: Message): void {
169
+ if (this.isHidden) {
170
+ this.show();
171
+ }
172
+ this._setGeometry();
173
+ super.onUpdateRequest(msg);
174
+ }
175
+
176
+ /**
177
+ * Handle scroll events for the widget
178
+ */
179
+ private _evtScroll(event: MouseEvent) {
180
+ // All scrolls except scrolls in the actual hover box node may cause the
181
+ // referent editor that anchors the node to move, so the only scroll events
182
+ // that can safely be ignored are ones that happen inside the hovering node.
183
+ if (this.node.contains(event.target as HTMLElement)) {
184
+ return;
185
+ }
186
+
187
+ this.update();
188
+ }
189
+
190
+ /**
191
+ * Find the position of the first character of the current token.
192
+ */
193
+ private _getTokenPosition(): CodeEditor.IPosition | undefined {
194
+ const editor = this._editor;
195
+ const cursor = editor.getCursorPosition();
196
+ const end = editor.getOffsetAt(cursor);
197
+ const line = editor.getLine(cursor.line);
198
+
199
+ if (!line) {
200
+ return;
201
+ }
202
+
203
+ const tokens = line.substring(0, end).split(/\W+/);
204
+ const last = tokens[tokens.length - 1];
205
+ const start = last ? end - last.length : end;
206
+ return editor.getPositionAt(start);
207
+ }
208
+
209
+ /**
210
+ * Set the geometry of the tooltip widget.
211
+ */
212
+ private _setGeometry(): void {
213
+ // determine position for hover box placement
214
+ const position = this._position ? this._position : this._getTokenPosition();
215
+
216
+ if (!position) {
217
+ return;
218
+ }
219
+
220
+ const editor = this._editor;
221
+
222
+ const anchor = editor.getCoordinateForPosition(position);
223
+ const style = window.getComputedStyle(this.node);
224
+ const paddingLeft = parseInt(style.paddingLeft!, 10) || 0;
225
+
226
+ const host =
227
+ (editor.host.closest('.jp-MainAreaWidget > .lm-Widget') as HTMLElement) ||
228
+ editor.host;
229
+
230
+ // Calculate the geometry of the tooltip.
231
+ HoverBox.setGeometry({
232
+ anchor,
233
+ host,
234
+ maxHeight: MAX_HEIGHT,
235
+ minHeight: MIN_HEIGHT,
236
+ node: this.node,
237
+ offset: { horizontal: -1 * paddingLeft },
238
+ privilege: 'below',
239
+ outOfViewDisplay: {
240
+ top: 'stick-inside',
241
+ bottom: 'stick-inside'
242
+ },
243
+ style: style
244
+ });
245
+ }
246
+
247
+ private _content: IRenderMime.IRenderer | null = null;
248
+ private _editor: CodeEditor.IEditor;
249
+ private _position: CodeEditor.IPosition | undefined;
250
+ private _rendermime: IRenderMimeRegistry;
251
+ }
252
+
253
+ /**
254
+ * A namespace for tooltip widget statics.
255
+ */
256
+ export namespace Tooltip {
257
+ /**
258
+ * Instantiation options for a tooltip widget.
259
+ */
260
+ export interface IOptions {
261
+ /**
262
+ * The anchor widget that the tooltip widget tracks.
263
+ */
264
+ anchor: Widget;
265
+
266
+ /**
267
+ * The data that populates the tooltip widget.
268
+ */
269
+ bundle: JSONObject;
270
+
271
+ /**
272
+ * The editor referent of the tooltip model.
273
+ */
274
+ editor: CodeEditor.IEditor;
275
+
276
+ /**
277
+ * The rendermime instance used by the tooltip model.
278
+ */
279
+ rendermime: IRenderMimeRegistry;
280
+
281
+ /**
282
+ * Position at which the tooltip should be placed.
283
+ *
284
+ * If not given, the position of the first character
285
+ * in the current token will be used.
286
+ */
287
+ position?: CodeEditor.IPosition;
288
+ }
289
+ }