UNPKG

2.89 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { WidgetTracker } from '@jupyterlab/apputils';
4import { Signal } from '@lumino/signaling';
5export class NotebookTracker extends WidgetTracker {
6 constructor() {
7 super(...arguments);
8 this._activeCell = null;
9 this._activeCellChanged = new Signal(this);
10 this._selectionChanged = new Signal(this);
11 }
12 /**
13 * The currently focused cell.
14 *
15 * #### Notes
16 * This is a read-only property. If there is no cell with the focus, then this
17 * value is `null`.
18 */
19 get activeCell() {
20 const widget = this.currentWidget;
21 if (!widget) {
22 return null;
23 }
24 return widget.content.activeCell || null;
25 }
26 /**
27 * A signal emitted when the current active cell changes.
28 *
29 * #### Notes
30 * If there is no cell with the focus, then `null` will be emitted.
31 */
32 get activeCellChanged() {
33 return this._activeCellChanged;
34 }
35 /**
36 * A signal emitted when the selection state changes.
37 */
38 get selectionChanged() {
39 return this._selectionChanged;
40 }
41 /**
42 * Add a new notebook panel to the tracker.
43 *
44 * @param panel - The notebook panel being added.
45 */
46 add(panel) {
47 const promise = super.add(panel);
48 panel.content.activeCellChanged.connect(this._onActiveCellChanged, this);
49 panel.content.selectionChanged.connect(this._onSelectionChanged, this);
50 return promise;
51 }
52 /**
53 * Dispose of the resources held by the tracker.
54 */
55 dispose() {
56 this._activeCell = null;
57 super.dispose();
58 }
59 /**
60 * Handle the current change event.
61 */
62 onCurrentChanged(widget) {
63 // Store an internal reference to active cell to prevent false positives.
64 const activeCell = this.activeCell;
65 if (activeCell && activeCell === this._activeCell) {
66 return;
67 }
68 this._activeCell = activeCell;
69 if (!widget) {
70 return;
71 }
72 // Since the notebook has changed, immediately signal an active cell change
73 this._activeCellChanged.emit(widget.content.activeCell || null);
74 }
75 _onActiveCellChanged(sender, cell) {
76 // Check if the active cell change happened for the current notebook.
77 if (this.currentWidget && this.currentWidget.content === sender) {
78 this._activeCell = cell || null;
79 this._activeCellChanged.emit(this._activeCell);
80 }
81 }
82 _onSelectionChanged(sender) {
83 // Check if the selection change happened for the current notebook.
84 if (this.currentWidget && this.currentWidget.content === sender) {
85 this._selectionChanged.emit(void 0);
86 }
87 }
88}
89//# sourceMappingURL=tracker.js.map
\No newline at end of file