1 |
|
2 |
|
3 |
|
4 | import { Cell, ICellModel } from '@jupyterlab/cells';
|
5 | import { CodeEditor, JSONEditor } from '@jupyterlab/codeeditor';
|
6 | import { ObservableJSON } from '@jupyterlab/observables';
|
7 | import { IMapChange } from '@jupyter/ydoc';
|
8 | import { ITranslator, nullTranslator } from '@jupyterlab/translation';
|
9 | import { Collapser } from '@jupyterlab/ui-components';
|
10 | import { ArrayExt } from '@lumino/algorithm';
|
11 | import { ReadonlyPartialJSONValue } from '@lumino/coreutils';
|
12 | import { ConflatableMessage, Message, MessageLoop } from '@lumino/messaging';
|
13 | import { PanelLayout, Widget } from '@lumino/widgets';
|
14 | import { INotebookModel } from './model';
|
15 | import { NotebookPanel } from './panel';
|
16 | import { INotebookTools, INotebookTracker } from './tokens';
|
17 |
|
18 | class RankedPanel<T extends Widget = Widget> extends Widget {
|
19 | constructor() {
|
20 | super();
|
21 | this.layout = new PanelLayout();
|
22 | this.addClass('jp-RankedPanel');
|
23 | }
|
24 |
|
25 | addWidget(widget: Widget, rank: number): void {
|
26 | const rankItem = { widget, rank };
|
27 | const index = ArrayExt.upperBound(this._items, rankItem, Private.itemCmp);
|
28 | ArrayExt.insert(this._items, index, rankItem);
|
29 |
|
30 | const layout = this.layout as PanelLayout;
|
31 | layout.insertWidget(index, widget);
|
32 | }
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 | protected onChildRemoved(msg: Widget.ChildMessage): void {
|
39 | const index = ArrayExt.findFirstIndex(
|
40 | this._items,
|
41 | item => item.widget === msg.child
|
42 | );
|
43 | if (index !== -1) {
|
44 | ArrayExt.removeAt(this._items, index);
|
45 | }
|
46 | }
|
47 |
|
48 | private _items: Private.IRankItem<T>[] = [];
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | export class NotebookTools extends Widget implements INotebookTools {
|
55 | |
56 |
|
57 |
|
58 | constructor(options: NotebookTools.IOptions) {
|
59 | super();
|
60 | this.addClass('jp-NotebookTools');
|
61 |
|
62 | this.translator = options.translator || nullTranslator;
|
63 |
|
64 | this._tools = [];
|
65 |
|
66 | this.layout = new PanelLayout();
|
67 |
|
68 | this._tracker = options.tracker;
|
69 | this._tracker.currentChanged.connect(
|
70 | this._onActiveNotebookPanelChanged,
|
71 | this
|
72 | );
|
73 | this._tracker.activeCellChanged.connect(this._onActiveCellChanged, this);
|
74 | this._tracker.selectionChanged.connect(this._onSelectionChanged, this);
|
75 | this._onActiveNotebookPanelChanged();
|
76 | this._onActiveCellChanged();
|
77 | this._onSelectionChanged();
|
78 | }
|
79 |
|
80 | |
81 |
|
82 |
|
83 | get activeCell(): Cell | null {
|
84 | return this._tracker.activeCell;
|
85 | }
|
86 |
|
87 | |
88 |
|
89 |
|
90 | get selectedCells(): Cell[] {
|
91 | const panel = this._tracker.currentWidget;
|
92 | if (!panel) {
|
93 | return [];
|
94 | }
|
95 | const notebook = panel.content;
|
96 | return notebook.widgets.filter(cell => notebook.isSelectedOrActive(cell));
|
97 | }
|
98 |
|
99 | |
100 |
|
101 |
|
102 | get activeNotebookPanel(): NotebookPanel | null {
|
103 | return this._tracker.currentWidget;
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 | addItem(options: NotebookTools.IAddOptions): void {
|
110 | const tool = options.tool;
|
111 | const rank = options.rank ?? 100;
|
112 |
|
113 | let section: RankedPanel<NotebookTools.Tool>;
|
114 | const extendedTool = this._tools.find(
|
115 | extendedTool => extendedTool.section === options.section
|
116 | );
|
117 | if (extendedTool) section = extendedTool.panel;
|
118 | else {
|
119 | throw new Error(`The section ${options.section} does not exist`);
|
120 | }
|
121 |
|
122 | tool.addClass('jp-NotebookTools-tool');
|
123 | section.addWidget(tool, rank);
|
124 |
|
125 |
|
126 | tool.notebookTools = this;
|
127 |
|
128 |
|
129 | MessageLoop.sendMessage(tool, NotebookTools.ActiveNotebookPanelMessage);
|
130 | MessageLoop.sendMessage(tool, NotebookTools.ActiveCellMessage);
|
131 | }
|
132 |
|
133 | |
134 |
|
135 |
|
136 | addSection(options: NotebookTools.IAddSectionOptions): void {
|
137 | const sectionName = options.sectionName;
|
138 | const label = options.label || options.sectionName;
|
139 | const widget = options.tool;
|
140 | let rank = options.rank ?? null;
|
141 |
|
142 | const newSection = new RankedPanel<NotebookTools.Tool>();
|
143 | newSection.title.label = label;
|
144 |
|
145 | if (widget) newSection.addWidget(widget, 0);
|
146 |
|
147 | this._tools.push({
|
148 | section: sectionName,
|
149 | panel: newSection,
|
150 | rank: rank
|
151 | });
|
152 |
|
153 | if (rank != null)
|
154 | (this.layout as PanelLayout).insertWidget(
|
155 | rank,
|
156 | new Collapser({ widget: newSection })
|
157 | );
|
158 | else {
|
159 |
|
160 | let advancedToolsRank = null;
|
161 | const layout = this.layout as PanelLayout;
|
162 | for (let i = 0; i < layout.widgets.length; i++) {
|
163 | let w = layout.widgets[i];
|
164 | if (w instanceof Collapser) {
|
165 | if (w.widget.id === 'advancedToolsSection') {
|
166 | advancedToolsRank = i;
|
167 | break;
|
168 | }
|
169 | }
|
170 | }
|
171 |
|
172 | if (advancedToolsRank !== null)
|
173 | (this.layout as PanelLayout).insertWidget(
|
174 | advancedToolsRank,
|
175 | new Collapser({ widget: newSection })
|
176 | );
|
177 | else
|
178 | (this.layout as PanelLayout).addWidget(
|
179 | new Collapser({ widget: newSection })
|
180 | );
|
181 | }
|
182 | }
|
183 |
|
184 | |
185 |
|
186 |
|
187 | private _onActiveNotebookPanelChanged(): void {
|
188 | if (
|
189 | this._prevActiveNotebookModel &&
|
190 | !this._prevActiveNotebookModel.isDisposed
|
191 | ) {
|
192 | this._prevActiveNotebookModel.metadataChanged.disconnect(
|
193 | this._onActiveNotebookPanelMetadataChanged,
|
194 | this
|
195 | );
|
196 | }
|
197 | const activeNBModel =
|
198 | this.activeNotebookPanel && this.activeNotebookPanel.content
|
199 | ? this.activeNotebookPanel.content.model
|
200 | : null;
|
201 | this._prevActiveNotebookModel = activeNBModel;
|
202 | if (activeNBModel) {
|
203 | activeNBModel.metadataChanged.connect(
|
204 | this._onActiveNotebookPanelMetadataChanged,
|
205 | this
|
206 | );
|
207 | }
|
208 | for (const widget of this._toolChildren()) {
|
209 | MessageLoop.sendMessage(widget, NotebookTools.ActiveNotebookPanelMessage);
|
210 | }
|
211 | }
|
212 |
|
213 | |
214 |
|
215 |
|
216 | private _onActiveCellChanged(): void {
|
217 | if (this._prevActiveCell && !this._prevActiveCell.isDisposed) {
|
218 | this._prevActiveCell.metadataChanged.disconnect(
|
219 | this._onActiveCellMetadataChanged,
|
220 | this
|
221 | );
|
222 | }
|
223 | const activeCell = this.activeCell ? this.activeCell.model : null;
|
224 | this._prevActiveCell = activeCell;
|
225 | if (activeCell) {
|
226 | activeCell.metadataChanged.connect(
|
227 | this._onActiveCellMetadataChanged,
|
228 | this
|
229 | );
|
230 | }
|
231 | for (const widget of this._toolChildren()) {
|
232 | MessageLoop.sendMessage(widget, NotebookTools.ActiveCellMessage);
|
233 | }
|
234 | }
|
235 |
|
236 | |
237 |
|
238 |
|
239 | private _onSelectionChanged(): void {
|
240 | for (const widget of this._toolChildren()) {
|
241 | MessageLoop.sendMessage(widget, NotebookTools.SelectionMessage);
|
242 | }
|
243 | }
|
244 |
|
245 | |
246 |
|
247 |
|
248 | private _onActiveNotebookPanelMetadataChanged(
|
249 | sender: INotebookModel,
|
250 | args: IMapChange
|
251 | ): void {
|
252 | const message = new ObservableJSON.ChangeMessage(
|
253 | 'activenotebookpanel-metadata-changed',
|
254 | { oldValue: undefined, newValue: undefined, ...args }
|
255 | );
|
256 | for (const widget of this._toolChildren()) {
|
257 | MessageLoop.sendMessage(widget, message);
|
258 | }
|
259 | }
|
260 |
|
261 | |
262 |
|
263 |
|
264 | private _onActiveCellMetadataChanged(
|
265 | sender: ICellModel,
|
266 | args: IMapChange
|
267 | ): void {
|
268 | const message = new ObservableJSON.ChangeMessage(
|
269 | 'activecell-metadata-changed',
|
270 | { newValue: undefined, oldValue: undefined, ...args }
|
271 | );
|
272 | for (const widget of this._toolChildren()) {
|
273 | MessageLoop.sendMessage(widget, message);
|
274 | }
|
275 | }
|
276 |
|
277 | private *_toolChildren() {
|
278 | for (let tool of this._tools) {
|
279 | yield* tool.panel.children();
|
280 | }
|
281 | }
|
282 |
|
283 | translator: ITranslator;
|
284 | private _tools: Array<NotebookTools.IToolPanel>;
|
285 | private _tracker: INotebookTracker;
|
286 | private _prevActiveCell: ICellModel | null;
|
287 | private _prevActiveNotebookModel: INotebookModel | null;
|
288 | }
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | export namespace NotebookTools {
|
294 | |
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | export type ReadonlyPartialJSONOptionValueArray = [
|
301 | ReadonlyPartialJSONValue | undefined,
|
302 | ReadonlyPartialJSONValue
|
303 | ][];
|
304 |
|
305 | |
306 |
|
307 |
|
308 | export interface IToolPanel {
|
309 | |
310 |
|
311 |
|
312 | section: string;
|
313 |
|
314 | |
315 |
|
316 |
|
317 | panel: RankedPanel<NotebookTools.Tool>;
|
318 |
|
319 | |
320 |
|
321 |
|
322 | rank?: number | null;
|
323 | }
|
324 |
|
325 | |
326 |
|
327 |
|
328 | export interface IOptions {
|
329 | |
330 |
|
331 |
|
332 | tracker: INotebookTracker;
|
333 |
|
334 | |
335 |
|
336 |
|
337 | translator?: ITranslator;
|
338 | }
|
339 |
|
340 | |
341 |
|
342 |
|
343 | export interface IAddOptions {
|
344 | |
345 |
|
346 |
|
347 | tool: INotebookTools.ITool;
|
348 |
|
349 | |
350 |
|
351 |
|
352 | section: string;
|
353 |
|
354 | |
355 |
|
356 |
|
357 | rank?: number;
|
358 | }
|
359 |
|
360 | |
361 |
|
362 |
|
363 | export interface IAddSectionOptions {
|
364 | |
365 |
|
366 |
|
367 | sectionName: string;
|
368 |
|
369 | |
370 |
|
371 |
|
372 | tool?: INotebookTools.ITool;
|
373 |
|
374 | |
375 |
|
376 |
|
377 | label?: string;
|
378 |
|
379 | |
380 |
|
381 |
|
382 | rank?: number;
|
383 | }
|
384 |
|
385 | |
386 |
|
387 |
|
388 | export const ActiveNotebookPanelMessage = new ConflatableMessage(
|
389 | 'activenotebookpanel-changed'
|
390 | );
|
391 |
|
392 | |
393 |
|
394 |
|
395 | export const ActiveCellMessage = new ConflatableMessage('activecell-changed');
|
396 |
|
397 | |
398 |
|
399 |
|
400 | export const SelectionMessage = new ConflatableMessage('selection-changed');
|
401 |
|
402 | |
403 |
|
404 |
|
405 | export class Tool extends Widget implements INotebookTools.ITool {
|
406 | |
407 |
|
408 |
|
409 | notebookTools: INotebookTools;
|
410 |
|
411 | dispose(): void {
|
412 | super.dispose();
|
413 | if (this.notebookTools) {
|
414 | this.notebookTools = null!;
|
415 | }
|
416 | }
|
417 |
|
418 | |
419 |
|
420 |
|
421 |
|
422 |
|
423 | processMessage(msg: Message): void {
|
424 | super.processMessage(msg);
|
425 | switch (msg.type) {
|
426 | case 'activenotebookpanel-changed':
|
427 | this.onActiveNotebookPanelChanged(msg);
|
428 | break;
|
429 | case 'activecell-changed':
|
430 | this.onActiveCellChanged(msg);
|
431 | break;
|
432 | case 'selection-changed':
|
433 | this.onSelectionChanged(msg);
|
434 | break;
|
435 | case 'activecell-metadata-changed':
|
436 | this.onActiveCellMetadataChanged(msg as ObservableJSON.ChangeMessage);
|
437 | break;
|
438 | case 'activenotebookpanel-metadata-changed':
|
439 | this.onActiveNotebookPanelMetadataChanged(
|
440 | msg as ObservableJSON.ChangeMessage
|
441 | );
|
442 | break;
|
443 | default:
|
444 | break;
|
445 | }
|
446 | }
|
447 |
|
448 | |
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 | protected onActiveNotebookPanelChanged(msg: Message): void {
|
455 |
|
456 | }
|
457 |
|
458 | |
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 | protected onActiveCellChanged(msg: Message): void {
|
465 |
|
466 | }
|
467 |
|
468 | |
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 | protected onSelectionChanged(msg: Message): void {
|
475 |
|
476 | }
|
477 |
|
478 | |
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 | protected onActiveCellMetadataChanged(
|
485 | msg: ObservableJSON.ChangeMessage
|
486 | ): void {
|
487 |
|
488 | }
|
489 |
|
490 | |
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | protected onActiveNotebookPanelMetadataChanged(
|
497 | msg: ObservableJSON.ChangeMessage
|
498 | ): void {
|
499 |
|
500 | }
|
501 | }
|
502 |
|
503 | |
504 |
|
505 |
|
506 | export class MetadataEditorTool extends Tool {
|
507 | |
508 |
|
509 |
|
510 | constructor(options: MetadataEditorTool.IOptions) {
|
511 | super();
|
512 | const { editorFactory } = options;
|
513 | this.addClass('jp-MetadataEditorTool');
|
514 | const layout = (this.layout = new PanelLayout());
|
515 |
|
516 | this._editorFactory = editorFactory;
|
517 | this._editorLabel = options.label || 'Edit Metadata';
|
518 | this.createEditor();
|
519 | const titleNode = new Widget({ node: document.createElement('label') });
|
520 | titleNode.node.textContent = options.label || 'Edit Metadata';
|
521 | layout.addWidget(titleNode);
|
522 | layout.addWidget(this.editor);
|
523 | }
|
524 |
|
525 | |
526 |
|
527 |
|
528 | get editor(): JSONEditor {
|
529 | return this._editor;
|
530 | }
|
531 |
|
532 | |
533 |
|
534 |
|
535 | protected onActiveNotebookPanelChanged(msg: Message): void {
|
536 | this.editor.dispose();
|
537 | if (this.notebookTools.activeNotebookPanel) {
|
538 | this.createEditor();
|
539 | }
|
540 | }
|
541 |
|
542 | protected createEditor() {
|
543 | this._editor = new JSONEditor({
|
544 | editorFactory: this._editorFactory
|
545 | });
|
546 | this.editor.title.label = this._editorLabel;
|
547 |
|
548 | (this.layout as PanelLayout).addWidget(this.editor);
|
549 | }
|
550 |
|
551 | private _editor: JSONEditor;
|
552 | private _editorLabel: string;
|
553 | private _editorFactory: CodeEditor.Factory;
|
554 | }
|
555 |
|
556 | |
557 |
|
558 |
|
559 | export namespace MetadataEditorTool {
|
560 | |
561 |
|
562 |
|
563 | export interface IOptions {
|
564 | |
565 |
|
566 |
|
567 | editorFactory: CodeEditor.Factory;
|
568 |
|
569 | |
570 |
|
571 |
|
572 | label?: string;
|
573 |
|
574 | |
575 |
|
576 |
|
577 | collapsed?: boolean;
|
578 |
|
579 | |
580 |
|
581 |
|
582 | translator?: ITranslator;
|
583 | }
|
584 | }
|
585 | }
|
586 |
|
587 |
|
588 |
|
589 |
|
590 | namespace Private {
|
591 | |
592 |
|
593 |
|
594 | export interface IRankItem<T extends Widget = Widget> {
|
595 | |
596 |
|
597 |
|
598 | widget: T;
|
599 |
|
600 | |
601 |
|
602 |
|
603 | rank: number;
|
604 | }
|
605 |
|
606 | |
607 |
|
608 |
|
609 | export function itemCmp(first: IRankItem, second: IRankItem): number {
|
610 | return first.rank - second.rank;
|
611 | }
|
612 | }
|