@jupyterlab/notebook-extension 4.6.2 → 4.6.3
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/lib/index.js +10 -4
- package/lib/index.js.map +1 -1
- package/lib/tool-widgets/activeCellToolWidget.d.ts +40 -2
- package/lib/tool-widgets/activeCellToolWidget.js +160 -29
- package/lib/tool-widgets/activeCellToolWidget.js.map +1 -1
- package/package.json +31 -31
- package/src/index.ts +10 -4
- package/src/tool-widgets/activeCellToolWidget.tsx +187 -38
|
@@ -22,14 +22,52 @@ declare namespace Private {
|
|
|
22
22
|
*
|
|
23
23
|
* ## Note
|
|
24
24
|
* This field does not work as other metadata form fields, as it does not update metadata.
|
|
25
|
+
*
|
|
26
|
+
* A single instance is meant to be shared by every render of the field. The
|
|
27
|
+
* displayed cell follows the notebook tracker rather than the render calls, so
|
|
28
|
+
* that the field keeps working when the metadata form is not being rebuilt, and
|
|
29
|
+
* so that it does not hold on to a cell model of a closed notebook.
|
|
30
|
+
*
|
|
31
|
+
* One instance owns one node, so the field can only be mounted in one place at
|
|
32
|
+
* a time: `render` moves the node to the most recent mount point and leaves any
|
|
33
|
+
* earlier one empty. Nothing renders this field twice today.
|
|
25
34
|
*/
|
|
26
35
|
export declare class ActiveCellTool extends NotebookTools.Tool {
|
|
27
36
|
constructor(options: Private.IOptions);
|
|
37
|
+
dispose(): void;
|
|
28
38
|
render(props: FieldProps): JSX.Element;
|
|
29
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Follow the active cell of the tracker, which is null once the last
|
|
41
|
+
* notebook is closed.
|
|
42
|
+
*/
|
|
43
|
+
private _onActiveCellChanged;
|
|
44
|
+
/**
|
|
45
|
+
* Handle a change to the current cell source, mime type or execution count.
|
|
46
|
+
*/
|
|
47
|
+
private _onCellContentChanged;
|
|
48
|
+
/**
|
|
49
|
+
* Stop listening to the cell model the tool is currently displaying.
|
|
50
|
+
*/
|
|
51
|
+
private _disconnectCellModel;
|
|
52
|
+
/**
|
|
53
|
+
* Reflect the execution count of the current cell.
|
|
54
|
+
*/
|
|
55
|
+
private _updatePrompt;
|
|
56
|
+
/**
|
|
57
|
+
* Refresh the preview, writing an outdated prompt along with it.
|
|
58
|
+
*
|
|
59
|
+
* The prompt is written in the same task as the preview it belongs with, so
|
|
60
|
+
* that the field never shows the prompt of one cell above the source line of
|
|
61
|
+
* another while a language mode is being loaded.
|
|
62
|
+
*/
|
|
63
|
+
private _update;
|
|
30
64
|
private _tracker;
|
|
65
|
+
private _languages;
|
|
31
66
|
private _cellModel;
|
|
32
|
-
private
|
|
67
|
+
private _previewDebouncer;
|
|
68
|
+
private _updateId;
|
|
69
|
+
private _pendingUpdate;
|
|
70
|
+
private _promptOutdated;
|
|
33
71
|
private _editorEl;
|
|
34
72
|
private _inputPrompt;
|
|
35
73
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import React from 'react';
|
|
6
6
|
import { NotebookTools } from '@jupyterlab/notebook';
|
|
7
7
|
import { PanelLayout, Widget } from '@lumino/widgets';
|
|
8
|
-
import { InputPrompt } from '@jupyterlab/cells';
|
|
8
|
+
import { InputPrompt, isCodeCellModel } from '@jupyterlab/cells';
|
|
9
9
|
import { Debouncer } from '@lumino/polling';
|
|
10
10
|
/**
|
|
11
11
|
* The class name added to the ActiveCellTool.
|
|
@@ -24,12 +24,25 @@ const ACTIVE_CELL_TOOL_CELL_CONTENT_CLASS = 'jp-ActiveCellTool-CellContent';
|
|
|
24
24
|
*
|
|
25
25
|
* ## Note
|
|
26
26
|
* This field does not work as other metadata form fields, as it does not update metadata.
|
|
27
|
+
*
|
|
28
|
+
* A single instance is meant to be shared by every render of the field. The
|
|
29
|
+
* displayed cell follows the notebook tracker rather than the render calls, so
|
|
30
|
+
* that the field keeps working when the metadata form is not being rebuilt, and
|
|
31
|
+
* so that it does not hold on to a cell model of a closed notebook.
|
|
32
|
+
*
|
|
33
|
+
* One instance owns one node, so the field can only be mounted in one place at
|
|
34
|
+
* a time: `render` moves the node to the most recent mount point and leaves any
|
|
35
|
+
* earlier one empty. Nothing renders this field twice today.
|
|
27
36
|
*/
|
|
28
37
|
export class ActiveCellTool extends NotebookTools.Tool {
|
|
29
38
|
constructor(options) {
|
|
30
39
|
super();
|
|
31
|
-
|
|
40
|
+
this._cellModel = null;
|
|
41
|
+
this._updateId = 0;
|
|
42
|
+
this._pendingUpdate = false;
|
|
43
|
+
this._promptOutdated = false;
|
|
32
44
|
this._tracker = options.tracker;
|
|
45
|
+
this._languages = options.languages;
|
|
33
46
|
this.addClass(ACTIVE_CELL_TOOL_CLASS);
|
|
34
47
|
this.layout = new PanelLayout();
|
|
35
48
|
this._inputPrompt = new InputPrompt();
|
|
@@ -42,37 +55,155 @@ export class ActiveCellTool extends NotebookTools.Tool {
|
|
|
42
55
|
container.className = ACTIVE_CELL_TOOL_CELL_CONTENT_CLASS;
|
|
43
56
|
this._editorEl = editor;
|
|
44
57
|
this.layout.addWidget(new Widget({ node }));
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
this.
|
|
58
|
+
// Only edits to the current cell are rate-limited; switching cells updates
|
|
59
|
+
// the display immediately, see `_onActiveCellChanged`.
|
|
60
|
+
this._previewDebouncer = new Debouncer(() => this._update(), 150);
|
|
61
|
+
this._tracker.activeCellChanged.connect(this._onActiveCellChanged, this);
|
|
62
|
+
// `activeCellChanged` is not emitted when the last notebook is closed:
|
|
63
|
+
// NotebookTracker.onCurrentChanged returns early on a null widget. Without
|
|
64
|
+
// this second connection the field would keep the cell model of a closed
|
|
65
|
+
// notebook, and its shared model, alive for the rest of the session.
|
|
66
|
+
this._tracker.currentChanged.connect(this._onActiveCellChanged, this);
|
|
67
|
+
this._onActiveCellChanged();
|
|
68
|
+
}
|
|
69
|
+
dispose() {
|
|
70
|
+
if (this.isDisposed) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
this._tracker.activeCellChanged.disconnect(this._onActiveCellChanged, this);
|
|
74
|
+
this._tracker.currentChanged.disconnect(this._onActiveCellChanged, this);
|
|
75
|
+
this._disconnectCellModel();
|
|
76
|
+
this._previewDebouncer.dispose();
|
|
77
|
+
super.dispose();
|
|
61
78
|
}
|
|
62
79
|
render(props) {
|
|
80
|
+
// The content is driven by the tracker; React only supplies the mount
|
|
81
|
+
// point, which is a different element on every rebuild of the form.
|
|
82
|
+
return (React.createElement("div", { ref: ref => {
|
|
83
|
+
if (!ref || this.node.parentElement === ref) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
ref.appendChild(this.node);
|
|
87
|
+
if (this._pendingUpdate) {
|
|
88
|
+
this._update().catch(console.warn);
|
|
89
|
+
}
|
|
90
|
+
} }));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Follow the active cell of the tracker, which is null once the last
|
|
94
|
+
* notebook is closed.
|
|
95
|
+
*/
|
|
96
|
+
_onActiveCellChanged() {
|
|
63
97
|
var _a, _b;
|
|
64
|
-
const
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
this.
|
|
70
|
-
|
|
71
|
-
.
|
|
72
|
-
|
|
98
|
+
const cellModel = (_b = (_a = this._tracker.activeCell) === null || _a === void 0 ? void 0 : _a.model) !== null && _b !== void 0 ? _b : null;
|
|
99
|
+
if (cellModel === this._cellModel) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this._disconnectCellModel();
|
|
103
|
+
this._cellModel = cellModel;
|
|
104
|
+
if (cellModel) {
|
|
105
|
+
cellModel.sharedModel.changed.connect(this._onCellContentChanged, this);
|
|
106
|
+
cellModel.mimeTypeChanged.connect(this._onCellContentChanged, this);
|
|
107
|
+
}
|
|
108
|
+
// The prompt now belongs to a cell whose source line is not on screen yet,
|
|
109
|
+
// so leave it to `_update` to write both together.
|
|
110
|
+
this._promptOutdated = true;
|
|
111
|
+
this._update().catch(console.warn);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Handle a change to the current cell source, mime type or execution count.
|
|
115
|
+
*/
|
|
116
|
+
_onCellContentChanged() {
|
|
117
|
+
if (!this.node.isConnected) {
|
|
118
|
+
this._pendingUpdate = true;
|
|
119
|
+
this._promptOutdated = true;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (!this._promptOutdated) {
|
|
123
|
+
// The cell is unchanged and no switch is in flight, so writing the prompt
|
|
124
|
+
// now cannot pair it with the source line of a different cell.
|
|
125
|
+
this._updatePrompt();
|
|
126
|
+
}
|
|
127
|
+
this._previewDebouncer.invoke().catch(console.warn);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Stop listening to the cell model the tool is currently displaying.
|
|
131
|
+
*/
|
|
132
|
+
_disconnectCellModel() {
|
|
133
|
+
const cellModel = this._cellModel;
|
|
134
|
+
if (!cellModel) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
cellModel.sharedModel.changed.disconnect(this._onCellContentChanged, this);
|
|
138
|
+
cellModel.mimeTypeChanged.disconnect(this._onCellContentChanged, this);
|
|
139
|
+
this._cellModel = null;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Reflect the execution count of the current cell.
|
|
143
|
+
*/
|
|
144
|
+
_updatePrompt() {
|
|
145
|
+
var _a;
|
|
146
|
+
const cellModel = this._cellModel;
|
|
147
|
+
if (cellModel && isCodeCellModel(cellModel)) {
|
|
148
|
+
this._inputPrompt.executionCount = `${(_a = cellModel.executionCount) !== null && _a !== void 0 ? _a : ''}`;
|
|
149
|
+
this._inputPrompt.show();
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
this._inputPrompt.executionCount = null;
|
|
153
|
+
this._inputPrompt.hide();
|
|
154
|
+
}
|
|
73
155
|
}
|
|
74
|
-
|
|
75
|
-
|
|
156
|
+
/**
|
|
157
|
+
* Refresh the preview, writing an outdated prompt along with it.
|
|
158
|
+
*
|
|
159
|
+
* The prompt is written in the same task as the preview it belongs with, so
|
|
160
|
+
* that the field never shows the prompt of one cell above the source line of
|
|
161
|
+
* another while a language mode is being loaded.
|
|
162
|
+
*/
|
|
163
|
+
async _update() {
|
|
164
|
+
if (!this.node.isConnected) {
|
|
165
|
+
// Nothing is on screen; catch up when the field is mounted again.
|
|
166
|
+
this._pendingUpdate = true;
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
this._pendingUpdate = false;
|
|
170
|
+
const cellModel = this._cellModel;
|
|
171
|
+
const pending = ++this._updateId;
|
|
172
|
+
if (!cellModel) {
|
|
173
|
+
this._promptOutdated = false;
|
|
174
|
+
this._updatePrompt();
|
|
175
|
+
this._editorEl.replaceChildren();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const source = cellModel.sharedModel.getSource();
|
|
179
|
+
const lineEnd = source.indexOf('\n');
|
|
180
|
+
const firstLine = lineEnd === -1 ? source : source.slice(0, lineEnd);
|
|
181
|
+
// Highlight into a detached node, so that the preview is never cleared
|
|
182
|
+
// while waiting for a language mode to load.
|
|
183
|
+
const staging = document.createElement('pre');
|
|
184
|
+
try {
|
|
185
|
+
await this._languages.highlight(firstLine, this._languages.findByMIME(cellModel.mimeType), staging);
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
// Fall back to unhighlighted source rather than keeping the previous
|
|
189
|
+
// cell's line on screen.
|
|
190
|
+
console.warn(error);
|
|
191
|
+
staging.replaceChildren(document.createTextNode(firstLine));
|
|
192
|
+
}
|
|
193
|
+
if (pending !== this._updateId) {
|
|
194
|
+
// A newer update started while this one was highlighting; it still owes
|
|
195
|
+
// the prompt write, so `_promptOutdated` is deliberately left set.
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (this._promptOutdated) {
|
|
199
|
+
this._updatePrompt();
|
|
200
|
+
this._promptOutdated = false;
|
|
201
|
+
}
|
|
202
|
+
const fragment = document.createDocumentFragment();
|
|
203
|
+
while (staging.firstChild) {
|
|
204
|
+
fragment.appendChild(staging.firstChild);
|
|
205
|
+
}
|
|
206
|
+
this._editorEl.replaceChildren(fragment);
|
|
76
207
|
}
|
|
77
208
|
}
|
|
78
209
|
//# sourceMappingURL=activeCellToolWidget.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activeCellToolWidget.js","sourceRoot":"","sources":["../../src/tool-widgets/activeCellToolWidget.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"activeCellToolWidget.js","sourceRoot":"","sources":["../../src/tool-widgets/activeCellToolWidget.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACjE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;GAEG;AACH,MAAM,sBAAsB,GAAG,mBAAmB,CAAC;AACnD;;GAEG;AACH,MAAM,8BAA8B,GAAG,2BAA2B,CAAC;AACnE;;GAEG;AACH,MAAM,mCAAmC,GAAG,+BAA+B,CAAC;AAmB5E;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,cAAe,SAAQ,aAAa,CAAC,IAAI;IACpD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.6.3",
|
|
4
4
|
"description": "JupyterLab - Notebook Extension",
|
|
5
5
|
"homepage": "https://github.com/jupyterlab/jupyterlab",
|
|
6
6
|
"bugs": {
|
|
@@ -37,36 +37,36 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@jupyter/ydoc": "^4.0.0",
|
|
40
|
-
"@jupyterlab/application": "^4.6.
|
|
41
|
-
"@jupyterlab/apputils": "^4.7.
|
|
42
|
-
"@jupyterlab/cell-toolbar": "^4.6.
|
|
43
|
-
"@jupyterlab/cells": "^4.6.
|
|
44
|
-
"@jupyterlab/codeeditor": "^4.6.
|
|
45
|
-
"@jupyterlab/codemirror": "^4.6.
|
|
46
|
-
"@jupyterlab/completer": "^4.6.
|
|
47
|
-
"@jupyterlab/coreutils": "^6.6.
|
|
48
|
-
"@jupyterlab/docmanager": "^4.6.
|
|
49
|
-
"@jupyterlab/docmanager-extension": "^4.6.
|
|
50
|
-
"@jupyterlab/docregistry": "^4.6.
|
|
51
|
-
"@jupyterlab/documentsearch": "^4.6.
|
|
52
|
-
"@jupyterlab/filebrowser": "^4.6.
|
|
53
|
-
"@jupyterlab/launcher": "^4.6.
|
|
54
|
-
"@jupyterlab/logconsole": "^4.6.
|
|
55
|
-
"@jupyterlab/lsp": "^4.6.
|
|
56
|
-
"@jupyterlab/mainmenu": "^4.6.
|
|
57
|
-
"@jupyterlab/metadataform": "^4.6.
|
|
58
|
-
"@jupyterlab/nbformat": "^4.6.
|
|
59
|
-
"@jupyterlab/notebook": "^4.6.
|
|
60
|
-
"@jupyterlab/observables": "^5.6.
|
|
61
|
-
"@jupyterlab/property-inspector": "^4.6.
|
|
62
|
-
"@jupyterlab/rendermime": "^4.6.
|
|
63
|
-
"@jupyterlab/services": "^7.6.
|
|
64
|
-
"@jupyterlab/settingregistry": "^4.6.
|
|
65
|
-
"@jupyterlab/statedb": "^4.6.
|
|
66
|
-
"@jupyterlab/statusbar": "^4.6.
|
|
67
|
-
"@jupyterlab/toc": "^6.6.
|
|
68
|
-
"@jupyterlab/translation": "^4.6.
|
|
69
|
-
"@jupyterlab/ui-components": "^4.6.
|
|
40
|
+
"@jupyterlab/application": "^4.6.3",
|
|
41
|
+
"@jupyterlab/apputils": "^4.7.3",
|
|
42
|
+
"@jupyterlab/cell-toolbar": "^4.6.3",
|
|
43
|
+
"@jupyterlab/cells": "^4.6.3",
|
|
44
|
+
"@jupyterlab/codeeditor": "^4.6.3",
|
|
45
|
+
"@jupyterlab/codemirror": "^4.6.3",
|
|
46
|
+
"@jupyterlab/completer": "^4.6.3",
|
|
47
|
+
"@jupyterlab/coreutils": "^6.6.3",
|
|
48
|
+
"@jupyterlab/docmanager": "^4.6.3",
|
|
49
|
+
"@jupyterlab/docmanager-extension": "^4.6.3",
|
|
50
|
+
"@jupyterlab/docregistry": "^4.6.3",
|
|
51
|
+
"@jupyterlab/documentsearch": "^4.6.3",
|
|
52
|
+
"@jupyterlab/filebrowser": "^4.6.3",
|
|
53
|
+
"@jupyterlab/launcher": "^4.6.3",
|
|
54
|
+
"@jupyterlab/logconsole": "^4.6.3",
|
|
55
|
+
"@jupyterlab/lsp": "^4.6.3",
|
|
56
|
+
"@jupyterlab/mainmenu": "^4.6.3",
|
|
57
|
+
"@jupyterlab/metadataform": "^4.6.3",
|
|
58
|
+
"@jupyterlab/nbformat": "^4.6.3",
|
|
59
|
+
"@jupyterlab/notebook": "^4.6.3",
|
|
60
|
+
"@jupyterlab/observables": "^5.6.3",
|
|
61
|
+
"@jupyterlab/property-inspector": "^4.6.3",
|
|
62
|
+
"@jupyterlab/rendermime": "^4.6.3",
|
|
63
|
+
"@jupyterlab/services": "^7.6.3",
|
|
64
|
+
"@jupyterlab/settingregistry": "^4.6.3",
|
|
65
|
+
"@jupyterlab/statedb": "^4.6.3",
|
|
66
|
+
"@jupyterlab/statusbar": "^4.6.3",
|
|
67
|
+
"@jupyterlab/toc": "^6.6.3",
|
|
68
|
+
"@jupyterlab/translation": "^4.6.3",
|
|
69
|
+
"@jupyterlab/ui-components": "^4.6.3",
|
|
70
70
|
"@lumino/algorithm": "^2.0.4",
|
|
71
71
|
"@lumino/commands": "^2.3.3",
|
|
72
72
|
"@lumino/coreutils": "^2.2.2",
|
package/src/index.ts
CHANGED
|
@@ -1199,12 +1199,18 @@ const activeCellTool: JupyterFrontEndPlugin<void> = {
|
|
|
1199
1199
|
formRegistry: IFormRendererRegistry,
|
|
1200
1200
|
languages: IEditorLanguageRegistry
|
|
1201
1201
|
) => {
|
|
1202
|
+
// The field renderer is used by rjsf as a React component, so it runs on
|
|
1203
|
+
// every rebuild of the metadata form. The tool is created once and reused:
|
|
1204
|
+
// constructing one per render would rebuild the prompt and the preview from
|
|
1205
|
+
// scratch (showing them empty until the next update) and would leave a
|
|
1206
|
+
// connection to the cell model behind for every abandoned instance.
|
|
1207
|
+
const tool = new ActiveCellTool({
|
|
1208
|
+
tracker,
|
|
1209
|
+
languages
|
|
1210
|
+
});
|
|
1202
1211
|
const component: IFormRenderer = {
|
|
1203
1212
|
fieldRenderer: (props: FieldProps) => {
|
|
1204
|
-
return
|
|
1205
|
-
tracker,
|
|
1206
|
-
languages
|
|
1207
|
-
}).render(props);
|
|
1213
|
+
return tool.render(props);
|
|
1208
1214
|
}
|
|
1209
1215
|
};
|
|
1210
1216
|
formRegistry.addRenderer(
|
|
@@ -10,8 +10,8 @@ import type { INotebookTracker } from '@jupyterlab/notebook';
|
|
|
10
10
|
import { NotebookTools } from '@jupyterlab/notebook';
|
|
11
11
|
import type { ISharedText } from '@jupyter/ydoc';
|
|
12
12
|
import { PanelLayout, Widget } from '@lumino/widgets';
|
|
13
|
-
import type {
|
|
14
|
-
import { InputPrompt } from '@jupyterlab/cells';
|
|
13
|
+
import type { ICellModel } from '@jupyterlab/cells';
|
|
14
|
+
import { InputPrompt, isCodeCellModel } from '@jupyterlab/cells';
|
|
15
15
|
import { Debouncer } from '@lumino/polling';
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -49,12 +49,21 @@ namespace Private {
|
|
|
49
49
|
*
|
|
50
50
|
* ## Note
|
|
51
51
|
* This field does not work as other metadata form fields, as it does not update metadata.
|
|
52
|
+
*
|
|
53
|
+
* A single instance is meant to be shared by every render of the field. The
|
|
54
|
+
* displayed cell follows the notebook tracker rather than the render calls, so
|
|
55
|
+
* that the field keeps working when the metadata form is not being rebuilt, and
|
|
56
|
+
* so that it does not hold on to a cell model of a closed notebook.
|
|
57
|
+
*
|
|
58
|
+
* One instance owns one node, so the field can only be mounted in one place at
|
|
59
|
+
* a time: `render` moves the node to the most recent mount point and leaves any
|
|
60
|
+
* earlier one empty. Nothing renders this field twice today.
|
|
52
61
|
*/
|
|
53
62
|
export class ActiveCellTool extends NotebookTools.Tool {
|
|
54
63
|
constructor(options: Private.IOptions) {
|
|
55
64
|
super();
|
|
56
|
-
const { languages } = options;
|
|
57
65
|
this._tracker = options.tracker;
|
|
66
|
+
this._languages = options.languages;
|
|
58
67
|
|
|
59
68
|
this.addClass(ACTIVE_CELL_TOOL_CLASS);
|
|
60
69
|
this.layout = new PanelLayout();
|
|
@@ -71,51 +80,191 @@ export class ActiveCellTool extends NotebookTools.Tool {
|
|
|
71
80
|
this._editorEl = editor;
|
|
72
81
|
(this.layout as PanelLayout).addWidget(new Widget({ node }));
|
|
73
82
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
this.
|
|
83
|
+
// Only edits to the current cell are rate-limited; switching cells updates
|
|
84
|
+
// the display immediately, see `_onActiveCellChanged`.
|
|
85
|
+
this._previewDebouncer = new Debouncer<void, void, null[]>(
|
|
86
|
+
() => this._update(),
|
|
87
|
+
150
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
this._tracker.activeCellChanged.connect(this._onActiveCellChanged, this);
|
|
91
|
+
// `activeCellChanged` is not emitted when the last notebook is closed:
|
|
92
|
+
// NotebookTracker.onCurrentChanged returns early on a null widget. Without
|
|
93
|
+
// this second connection the field would keep the cell model of a closed
|
|
94
|
+
// notebook, and its shared model, alive for the rest of the session.
|
|
95
|
+
this._tracker.currentChanged.connect(this._onActiveCellChanged, this);
|
|
96
|
+
this._onActiveCellChanged();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
dispose(): void {
|
|
100
|
+
if (this.isDisposed) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
this._tracker.activeCellChanged.disconnect(this._onActiveCellChanged, this);
|
|
104
|
+
this._tracker.currentChanged.disconnect(this._onActiveCellChanged, this);
|
|
105
|
+
this._disconnectCellModel();
|
|
106
|
+
this._previewDebouncer.dispose();
|
|
107
|
+
super.dispose();
|
|
96
108
|
}
|
|
97
109
|
|
|
98
110
|
render(props: FieldProps): JSX.Element {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
111
|
+
// The content is driven by the tracker; React only supplies the mount
|
|
112
|
+
// point, which is a different element on every rebuild of the form.
|
|
113
|
+
return (
|
|
114
|
+
<div
|
|
115
|
+
ref={ref => {
|
|
116
|
+
if (!ref || this.node.parentElement === ref) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
ref.appendChild(this.node);
|
|
120
|
+
if (this._pendingUpdate) {
|
|
121
|
+
this._update().catch(console.warn);
|
|
122
|
+
}
|
|
123
|
+
}}
|
|
124
|
+
></div>
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Follow the active cell of the tracker, which is null once the last
|
|
130
|
+
* notebook is closed.
|
|
131
|
+
*/
|
|
132
|
+
private _onActiveCellChanged(): void {
|
|
133
|
+
const cellModel = this._tracker.activeCell?.model ?? null;
|
|
134
|
+
if (cellModel === this._cellModel) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this._disconnectCellModel();
|
|
138
|
+
this._cellModel = cellModel;
|
|
139
|
+
if (cellModel) {
|
|
140
|
+
(cellModel.sharedModel as ISharedText).changed.connect(
|
|
141
|
+
this._onCellContentChanged,
|
|
142
|
+
this
|
|
143
|
+
);
|
|
144
|
+
cellModel.mimeTypeChanged.connect(this._onCellContentChanged, this);
|
|
145
|
+
}
|
|
146
|
+
// The prompt now belongs to a cell whose source line is not on screen yet,
|
|
147
|
+
// so leave it to `_update` to write both together.
|
|
148
|
+
this._promptOutdated = true;
|
|
149
|
+
this._update().catch(console.warn);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Handle a change to the current cell source, mime type or execution count.
|
|
154
|
+
*/
|
|
155
|
+
private _onCellContentChanged(): void {
|
|
156
|
+
if (!this.node.isConnected) {
|
|
157
|
+
this._pendingUpdate = true;
|
|
158
|
+
this._promptOutdated = true;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (!this._promptOutdated) {
|
|
162
|
+
// The cell is unchanged and no switch is in flight, so writing the prompt
|
|
163
|
+
// now cannot pair it with the source line of a different cell.
|
|
164
|
+
this._updatePrompt();
|
|
165
|
+
}
|
|
166
|
+
this._previewDebouncer.invoke().catch(console.warn);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Stop listening to the cell model the tool is currently displaying.
|
|
171
|
+
*/
|
|
172
|
+
private _disconnectCellModel(): void {
|
|
173
|
+
const cellModel = this._cellModel;
|
|
174
|
+
if (!cellModel) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
(cellModel.sharedModel as ISharedText).changed.disconnect(
|
|
178
|
+
this._onCellContentChanged,
|
|
103
179
|
this
|
|
104
180
|
);
|
|
105
|
-
|
|
106
|
-
this.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
181
|
+
cellModel.mimeTypeChanged.disconnect(this._onCellContentChanged, this);
|
|
182
|
+
this._cellModel = null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Reflect the execution count of the current cell.
|
|
187
|
+
*/
|
|
188
|
+
private _updatePrompt(): void {
|
|
189
|
+
const cellModel = this._cellModel;
|
|
190
|
+
if (cellModel && isCodeCellModel(cellModel)) {
|
|
191
|
+
this._inputPrompt.executionCount = `${cellModel.executionCount ?? ''}`;
|
|
192
|
+
this._inputPrompt.show();
|
|
193
|
+
} else {
|
|
194
|
+
this._inputPrompt.executionCount = null;
|
|
195
|
+
this._inputPrompt.hide();
|
|
196
|
+
}
|
|
110
197
|
}
|
|
111
198
|
|
|
112
|
-
|
|
113
|
-
|
|
199
|
+
/**
|
|
200
|
+
* Refresh the preview, writing an outdated prompt along with it.
|
|
201
|
+
*
|
|
202
|
+
* The prompt is written in the same task as the preview it belongs with, so
|
|
203
|
+
* that the field never shows the prompt of one cell above the source line of
|
|
204
|
+
* another while a language mode is being loaded.
|
|
205
|
+
*/
|
|
206
|
+
private async _update(): Promise<void> {
|
|
207
|
+
if (!this.node.isConnected) {
|
|
208
|
+
// Nothing is on screen; catch up when the field is mounted again.
|
|
209
|
+
this._pendingUpdate = true;
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
this._pendingUpdate = false;
|
|
213
|
+
|
|
214
|
+
const cellModel = this._cellModel;
|
|
215
|
+
const pending = ++this._updateId;
|
|
216
|
+
|
|
217
|
+
if (!cellModel) {
|
|
218
|
+
this._promptOutdated = false;
|
|
219
|
+
this._updatePrompt();
|
|
220
|
+
this._editorEl.replaceChildren();
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const source = cellModel.sharedModel.getSource();
|
|
225
|
+
const lineEnd = source.indexOf('\n');
|
|
226
|
+
const firstLine = lineEnd === -1 ? source : source.slice(0, lineEnd);
|
|
227
|
+
|
|
228
|
+
// Highlight into a detached node, so that the preview is never cleared
|
|
229
|
+
// while waiting for a language mode to load.
|
|
230
|
+
const staging = document.createElement('pre');
|
|
231
|
+
try {
|
|
232
|
+
await this._languages.highlight(
|
|
233
|
+
firstLine,
|
|
234
|
+
this._languages.findByMIME(cellModel.mimeType),
|
|
235
|
+
staging
|
|
236
|
+
);
|
|
237
|
+
} catch (error) {
|
|
238
|
+
// Fall back to unhighlighted source rather than keeping the previous
|
|
239
|
+
// cell's line on screen.
|
|
240
|
+
console.warn(error);
|
|
241
|
+
staging.replaceChildren(document.createTextNode(firstLine));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (pending !== this._updateId) {
|
|
245
|
+
// A newer update started while this one was highlighting; it still owes
|
|
246
|
+
// the prompt write, so `_promptOutdated` is deliberately left set.
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (this._promptOutdated) {
|
|
251
|
+
this._updatePrompt();
|
|
252
|
+
this._promptOutdated = false;
|
|
253
|
+
}
|
|
254
|
+
const fragment = document.createDocumentFragment();
|
|
255
|
+
while (staging.firstChild) {
|
|
256
|
+
fragment.appendChild(staging.firstChild);
|
|
257
|
+
}
|
|
258
|
+
this._editorEl.replaceChildren(fragment);
|
|
114
259
|
}
|
|
115
260
|
|
|
116
261
|
private _tracker: INotebookTracker;
|
|
117
|
-
private
|
|
118
|
-
private
|
|
262
|
+
private _languages: IEditorLanguageRegistry;
|
|
263
|
+
private _cellModel: ICellModel | null = null;
|
|
264
|
+
private _previewDebouncer: Debouncer<void, void, null[]>;
|
|
265
|
+
private _updateId = 0;
|
|
266
|
+
private _pendingUpdate = false;
|
|
267
|
+
private _promptOutdated = false;
|
|
119
268
|
private _editorEl: HTMLPreElement;
|
|
120
269
|
private _inputPrompt: InputPrompt;
|
|
121
270
|
}
|