forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notebooktools.ts
933 lines (838 loc) · 24.4 KB
/
notebooktools.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { ArrayExt, each, chain } from '@lumino/algorithm';
import {
ReadonlyPartialJSONValue,
ReadonlyPartialJSONObject
} from '@lumino/coreutils';
import { ConflatableMessage, Message, MessageLoop } from '@lumino/messaging';
import { h, VirtualDOM, VirtualNode } from '@lumino/virtualdom';
import { PanelLayout, Widget } from '@lumino/widgets';
import { Collapse, Styling } from '@jupyterlab/apputils';
import { Cell, ICellModel } from '@jupyterlab/cells';
import {
CodeEditor,
CodeEditorWrapper,
JSONEditor
} from '@jupyterlab/codeeditor';
import * as nbformat from '@jupyterlab/nbformat';
import { IObservableMap, ObservableJSON } from '@jupyterlab/observables';
import { NotebookPanel } from './panel';
import { INotebookModel } from './model';
import { INotebookTools, INotebookTracker } from './tokens';
class RankedPanel<T extends Widget = Widget> extends Widget {
constructor() {
super();
this.layout = new PanelLayout();
this.addClass('jp-RankedPanel');
}
addWidget(widget: Widget, rank: number): void {
const rankItem = { widget, rank };
const index = ArrayExt.upperBound(this._items, rankItem, Private.itemCmp);
ArrayExt.insert(this._items, index, rankItem);
const layout = this.layout as PanelLayout;
layout.insertWidget(index, widget);
}
/**
* Handle the removal of a child
*
*/
protected onChildRemoved(msg: Widget.ChildMessage): void {
const index = ArrayExt.findFirstIndex(
this._items,
item => item.widget === msg.child
);
if (index !== -1) {
ArrayExt.removeAt(this._items, index);
}
}
private _items: Private.IRankItem<T>[] = [];
}
/**
* A widget that provides metadata tools.
*/
export class NotebookTools extends Widget implements INotebookTools {
/**
* Construct a new NotebookTools object.
*/
constructor(options: NotebookTools.IOptions) {
super();
this.addClass('jp-NotebookTools');
this._commonTools = new RankedPanel<NotebookTools.Tool>();
this._advancedTools = new RankedPanel<NotebookTools.Tool>();
this._advancedTools.title.label = 'Advanced Tools';
const layout = (this.layout = new PanelLayout());
layout.addWidget(this._commonTools);
layout.addWidget(new Collapse({ widget: this._advancedTools }));
this._tracker = options.tracker;
this._tracker.currentChanged.connect(
this._onActiveNotebookPanelChanged,
this
);
this._tracker.activeCellChanged.connect(this._onActiveCellChanged, this);
this._tracker.selectionChanged.connect(this._onSelectionChanged, this);
this._onActiveNotebookPanelChanged();
this._onActiveCellChanged();
this._onSelectionChanged();
}
/**
* The active cell widget.
*/
get activeCell(): Cell | null {
return this._tracker.activeCell;
}
/**
* The currently selected cells.
*/
get selectedCells(): Cell[] {
const panel = this._tracker.currentWidget;
if (!panel) {
return [];
}
const notebook = panel.content;
return notebook.widgets.filter(cell => notebook.isSelectedOrActive(cell));
}
/**
* The current notebook.
*/
get activeNotebookPanel(): NotebookPanel | null {
return this._tracker.currentWidget;
}
/**
* Add a cell tool item.
*/
addItem(options: NotebookTools.IAddOptions): void {
const tool = options.tool;
const rank = options.rank ?? 100;
let section: RankedPanel<NotebookTools.Tool>;
if (options.section === 'advanced') {
section = this._advancedTools;
} else {
section = this._commonTools;
}
tool.addClass('jp-NotebookTools-tool');
section.addWidget(tool, rank);
// TODO: perhaps the necessary notebookTools functionality should be
// consolidated into a single object, rather than a broad reference to this.
tool.notebookTools = this;
// Trigger the tool to update its active notebook and cell.
MessageLoop.sendMessage(tool, NotebookTools.ActiveNotebookPanelMessage);
MessageLoop.sendMessage(tool, NotebookTools.ActiveCellMessage);
}
/**
* Handle a change to the notebook panel.
*/
private _onActiveNotebookPanelChanged(): void {
if (
this._prevActiveNotebookModel &&
!this._prevActiveNotebookModel.isDisposed
) {
this._prevActiveNotebookModel.metadata.changed.disconnect(
this._onActiveNotebookPanelMetadataChanged,
this
);
}
const activeNBModel =
this.activeNotebookPanel && this.activeNotebookPanel.content
? this.activeNotebookPanel.content.model
: null;
this._prevActiveNotebookModel = activeNBModel;
if (activeNBModel) {
activeNBModel.metadata.changed.connect(
this._onActiveNotebookPanelMetadataChanged,
this
);
}
each(this._toolChildren(), widget => {
MessageLoop.sendMessage(widget, NotebookTools.ActiveNotebookPanelMessage);
});
}
/**
* Handle a change to the active cell.
*/
private _onActiveCellChanged(): void {
if (this._prevActiveCell && !this._prevActiveCell.isDisposed) {
this._prevActiveCell.metadata.changed.disconnect(
this._onActiveCellMetadataChanged,
this
);
}
const activeCell = this.activeCell ? this.activeCell.model : null;
this._prevActiveCell = activeCell;
if (activeCell) {
activeCell.metadata.changed.connect(
this._onActiveCellMetadataChanged,
this
);
}
each(this._toolChildren(), widget => {
MessageLoop.sendMessage(widget, NotebookTools.ActiveCellMessage);
});
}
/**
* Handle a change in the selection.
*/
private _onSelectionChanged(): void {
each(this._toolChildren(), widget => {
MessageLoop.sendMessage(widget, NotebookTools.SelectionMessage);
});
}
/**
* Handle a change in the active cell metadata.
*/
private _onActiveNotebookPanelMetadataChanged(
sender: IObservableMap<ReadonlyPartialJSONValue | undefined>,
args: IObservableMap.IChangedArgs<ReadonlyPartialJSONValue>
): void {
const message = new ObservableJSON.ChangeMessage(
'activenotebookpanel-metadata-changed',
args
);
each(this._toolChildren(), widget => {
MessageLoop.sendMessage(widget, message);
});
}
/**
* Handle a change in the notebook model metadata.
*/
private _onActiveCellMetadataChanged(
sender: IObservableMap<ReadonlyPartialJSONValue | undefined>,
args: IObservableMap.IChangedArgs<ReadonlyPartialJSONValue>
): void {
const message = new ObservableJSON.ChangeMessage(
'activecell-metadata-changed',
args
);
each(this._toolChildren(), widget => {
MessageLoop.sendMessage(widget, message);
});
}
private _toolChildren() {
return chain(this._commonTools.children(), this._advancedTools.children());
}
private _commonTools: RankedPanel<NotebookTools.Tool>;
private _advancedTools: RankedPanel<NotebookTools.Tool>;
private _tracker: INotebookTracker;
private _prevActiveCell: ICellModel | null;
private _prevActiveNotebookModel: INotebookModel | null;
}
/**
* The namespace for NotebookTools class statics.
*/
export namespace NotebookTools {
/**
* The options used to create a NotebookTools object.
*/
export interface IOptions {
/**
* The notebook tracker used by the notebook tools.
*/
tracker: INotebookTracker;
}
/**
* The options used to add an item to the notebook tools.
*/
export interface IAddOptions {
/**
* The tool to add to the notebook tools area.
*/
tool: Tool;
/**
* The section to which the tool should be added.
*/
section?: 'common' | 'advanced';
/**
* The rank order of the widget among its siblings.
*/
rank?: number;
}
/**
* A singleton conflatable `'activenotebookpanel-changed'` message.
*/
export const ActiveNotebookPanelMessage = new ConflatableMessage(
'activenotebookpanel-changed'
);
/**
* A singleton conflatable `'activecell-changed'` message.
*/
export const ActiveCellMessage = new ConflatableMessage('activecell-changed');
/**
* A singleton conflatable `'selection-changed'` message.
*/
export const SelectionMessage = new ConflatableMessage('selection-changed');
/**
* The base notebook tool, meant to be subclassed.
*/
export class Tool extends Widget implements INotebookTools.ITool {
/**
* The notebook tools object.
*/
notebookTools: INotebookTools;
dispose() {
super.dispose();
if (this.notebookTools) {
this.notebookTools = null!;
}
}
/**
* Process a message sent to the widget.
*
* @param msg - The message sent to the widget.
*/
processMessage(msg: Message): void {
super.processMessage(msg);
switch (msg.type) {
case 'activenotebookpanel-changed':
this.onActiveNotebookPanelChanged(msg);
break;
case 'activecell-changed':
this.onActiveCellChanged(msg);
break;
case 'selection-changed':
this.onSelectionChanged(msg);
break;
case 'activecell-metadata-changed':
this.onActiveCellMetadataChanged(msg as ObservableJSON.ChangeMessage);
break;
case 'activenotebookpanel-metadata-changed':
this.onActiveNotebookPanelMetadataChanged(
msg as ObservableJSON.ChangeMessage
);
break;
default:
break;
}
}
/**
* Handle a change to the notebook panel.
*
* #### Notes
* The default implementation is a no-op.
*/
protected onActiveNotebookPanelChanged(msg: Message): void {
/* no-op */
}
/**
* Handle a change to the active cell.
*
* #### Notes
* The default implementation is a no-op.
*/
protected onActiveCellChanged(msg: Message): void {
/* no-op */
}
/**
* Handle a change to the selection.
*
* #### Notes
* The default implementation is a no-op.
*/
protected onSelectionChanged(msg: Message): void {
/* no-op */
}
/**
* Handle a change to the metadata of the active cell.
*
* #### Notes
* The default implementation is a no-op.
*/
protected onActiveCellMetadataChanged(
msg: ObservableJSON.ChangeMessage
): void {
/* no-op */
}
/**
* Handle a change to the metadata of the active cell.
*
* #### Notes
* The default implementation is a no-op.
*/
protected onActiveNotebookPanelMetadataChanged(
msg: ObservableJSON.ChangeMessage
): void {
/* no-op */
}
}
/**
* A cell tool displaying the active cell contents.
*/
export class ActiveCellTool extends Tool {
/**
* Construct a new active cell tool.
*/
constructor() {
super();
this.addClass('jp-ActiveCellTool');
this.addClass('jp-InputArea');
this.layout = new PanelLayout();
}
/**
* Dispose of the resources used by the tool.
*/
dispose() {
if (this._model === null) {
return;
}
this._model.dispose();
this._model = null!;
super.dispose();
}
/**
* Handle a change to the active cell.
*/
protected onActiveCellChanged(): void {
const activeCell = this.notebookTools.activeCell;
const layout = this.layout as PanelLayout;
const count = layout.widgets.length;
for (let i = 0; i < count; i++) {
layout.widgets[0].dispose();
}
if (this._cellModel && !this._cellModel.isDisposed) {
this._cellModel.value.changed.disconnect(this._onValueChanged, this);
this._cellModel.mimeTypeChanged.disconnect(
this._onMimeTypeChanged,
this
);
}
if (!activeCell) {
const cell = new Widget();
cell.addClass('jp-InputArea-editor');
cell.addClass('jp-InputArea-editor');
layout.addWidget(cell);
this._cellModel = null;
return;
}
const promptNode = activeCell.promptNode
? (activeCell.promptNode.cloneNode(true) as HTMLElement)
: undefined;
const prompt = new Widget({ node: promptNode });
const factory = activeCell.contentFactory.editorFactory;
const cellModel = (this._cellModel = activeCell.model);
cellModel.value.changed.connect(this._onValueChanged, this);
cellModel.mimeTypeChanged.connect(this._onMimeTypeChanged, this);
this._model.value.text = cellModel.value.text.split('\n')[0];
this._model.mimeType = cellModel.mimeType;
const model = this._model;
const editorWidget = new CodeEditorWrapper({ model, factory });
editorWidget.addClass('jp-InputArea-editor');
editorWidget.addClass('jp-InputArea-editor');
editorWidget.editor.setOption('readOnly', true);
layout.addWidget(prompt);
layout.addWidget(editorWidget);
}
/**
* Handle a change to the current editor value.
*/
private _onValueChanged(): void {
this._model.value.text = this._cellModel!.value.text.split('\n')[0];
}
/**
* Handle a change to the current editor mimetype.
*/
private _onMimeTypeChanged(): void {
this._model.mimeType = this._cellModel!.mimeType;
}
private _model = new CodeEditor.Model();
private _cellModel: CodeEditor.IModel | null;
}
/**
* A raw metadata editor.
*/
export class MetadataEditorTool extends Tool {
/**
* Construct a new raw metadata tool.
*/
constructor(options: MetadataEditorTool.IOptions) {
super();
const { editorFactory } = options;
this.addClass('jp-MetadataEditorTool');
const layout = (this.layout = new PanelLayout());
this.editor = new JSONEditor({
editorFactory
});
this.editor.title.label = options.label || 'Edit Metadata';
const titleNode = new Widget({ node: document.createElement('label') });
titleNode.node.textContent = options.label || 'Edit Metadata';
layout.addWidget(titleNode);
layout.addWidget(this.editor);
}
/**
* The editor used by the tool.
*/
readonly editor: JSONEditor;
}
/**
* The namespace for `MetadataEditorTool` static data.
*/
export namespace MetadataEditorTool {
/**
* The options used to initialize a metadata editor tool.
*/
export interface IOptions {
/**
* The editor factory used by the tool.
*/
editorFactory: CodeEditor.Factory;
/**
* The label for the JSON editor
*/
label?: string;
/**
* Initial collapse state, defaults to true.
*/
collapsed?: boolean;
}
}
/**
* A notebook metadata editor
*/
export class NotebookMetadataEditorTool extends MetadataEditorTool {
constructor(options: MetadataEditorTool.IOptions) {
options.label = options.label || 'Notebook Metadata';
super(options);
}
/**
* Handle a change to the notebook.
*/
protected onActiveNotebookPanelChanged(msg: Message): void {
this._update();
}
/**
* Handle a change to the notebook metadata.
*/
protected onActiveNotebookPanelMetadataChanged(msg: Message): void {
this._update();
}
private _update() {
const nb =
this.notebookTools.activeNotebookPanel &&
this.notebookTools.activeNotebookPanel.content;
this.editor.source = nb?.model?.metadata ?? null;
}
}
/**
* A cell metadata editor
*/
export class CellMetadataEditorTool extends MetadataEditorTool {
constructor(options: MetadataEditorTool.IOptions) {
options.label = options.label || 'Cell Metadata';
super(options);
}
/**
* Handle a change to the active cell.
*/
protected onActiveCellChanged(msg: Message): void {
this._update();
}
/**
* Handle a change to the active cell metadata.
*/
protected onActiveCellMetadataChanged(msg: Message): void {
this._update();
}
private _update() {
const cell = this.notebookTools.activeCell;
this.editor.source = cell ? cell.model.metadata : null;
}
}
/**
* A cell tool that provides a selection for a given metadata key.
*/
export class KeySelector extends Tool {
/**
* Construct a new KeySelector.
*/
constructor(options: KeySelector.IOptions) {
// TODO: use react
super({ node: Private.createSelectorNode(options) });
this.addClass('jp-KeySelector');
this.key = options.key;
this._default = options.default;
this._validCellTypes = options.validCellTypes || [];
this._getter = options.getter || this._getValue;
this._setter = options.setter || this._setValue;
}
/**
* The metadata key used by the selector.
*/
readonly key: string;
/**
* The select node for the widget.
*/
get selectNode(): HTMLSelectElement {
return this.node.getElementsByTagName('select')[0] as HTMLSelectElement;
}
/**
* 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 notebook panel's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'change':
this.onValueChanged();
break;
default:
break;
}
}
/**
* Handle `after-attach` messages for the widget.
*/
protected onAfterAttach(msg: Message): void {
const node = this.selectNode;
node.addEventListener('change', this);
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
const node = this.selectNode;
node.removeEventListener('change', this);
}
/**
* Handle a change to the active cell.
*/
protected onActiveCellChanged(msg: Message): void {
const select = this.selectNode;
const activeCell = this.notebookTools.activeCell;
if (!activeCell) {
select.disabled = true;
select.value = '';
return;
}
const cellType = activeCell.model.type;
if (
this._validCellTypes.length &&
this._validCellTypes.indexOf(cellType) === -1
) {
select.value = '';
select.disabled = true;
return;
}
select.disabled = false;
this._changeGuard = true;
const getter = this._getter;
select.value = JSON.stringify(getter(activeCell));
this._changeGuard = false;
}
/**
* Handle a change to the metadata of the active cell.
*/
protected onActiveCellMetadataChanged(msg: ObservableJSON.ChangeMessage) {
if (this._changeGuard) {
return;
}
const select = this.selectNode;
const cell = this.notebookTools.activeCell;
if (msg.args.key === this.key && cell) {
this._changeGuard = true;
const getter = this._getter;
select.value = JSON.stringify(getter(cell));
this._changeGuard = false;
}
}
/**
* Handle a change to the value.
*/
protected onValueChanged(): void {
const activeCell = this.notebookTools.activeCell;
if (!activeCell || this._changeGuard) {
return;
}
this._changeGuard = true;
const select = this.selectNode;
const setter = this._setter;
setter(activeCell, JSON.parse(select.value));
this._changeGuard = false;
}
/**
* Get the value for the data.
*/
private _getValue = (cell: Cell) => {
let value = cell.model.metadata.get(this.key);
if (value === undefined) {
value = this._default;
}
return value;
};
/**
* Set the value for the data.
*/
private _setValue = (
cell: Cell,
value: ReadonlyPartialJSONValue | undefined
) => {
if (value === this._default) {
cell.model.metadata.delete(this.key);
} else {
cell.model.metadata.set(this.key, value);
}
};
private _changeGuard = false;
private _validCellTypes: string[];
private _getter: (cell: Cell) => ReadonlyPartialJSONValue | undefined;
private _setter: (
cell: Cell,
value: ReadonlyPartialJSONValue | undefined
) => void;
private _default: ReadonlyPartialJSONValue | undefined;
}
/**
* The namespace for `KeySelector` static data.
*/
export namespace KeySelector {
/**
* The options used to initialize a keyselector.
*/
export interface IOptions {
/**
* The metadata key of interest.
*/
key: string;
/**
* The map of options to values.
*
* #### Notes
* If a value equals the default, choosing it may erase the key from the
* metadata.
*/
optionsMap: ReadonlyPartialJSONObject;
/**
* The optional title of the selector - defaults to capitalized `key`.
*/
title?: string;
/**
* The optional valid cell types - defaults to all valid types.
*/
validCellTypes?: nbformat.CellType[];
/**
* An optional value getter for the selector.
*
* @param cell - The currently active cell.
*
* @returns The appropriate value for the selector.
*/
getter?: (cell: Cell) => ReadonlyPartialJSONValue | undefined;
/**
* An optional value setter for the selector.
*
* @param cell - The currently active cell.
*
* @param value - The value of the selector.
*
* #### Notes
* The setter should set the appropriate metadata value given the value of
* the selector.
*/
setter?: (
cell: Cell,
value: ReadonlyPartialJSONValue | undefined
) => void;
/**
* Default value for default setters and getters if value is not found.
*/
default?: ReadonlyPartialJSONValue;
}
}
/**
* Create a slideshow selector.
*/
export function createSlideShowSelector(): KeySelector {
const options: KeySelector.IOptions = {
key: 'slideshow',
title: 'Slide Type',
optionsMap: {
'-': null,
Slide: 'slide',
'Sub-Slide': 'subslide',
Fragment: 'fragment',
Skip: 'skip',
Notes: 'notes'
},
getter: cell => {
const value = cell.model.metadata.get('slideshow') as
| ReadonlyPartialJSONObject
| undefined;
return value && value['slide_type'];
},
setter: (cell, value) => {
let data = cell.model.metadata.get('slideshow') || Object.create(null);
if (value === null) {
// Make a shallow copy so we aren't modifying the original metadata.
data = { ...data };
delete data.slide_type;
} else {
data = { ...data, slide_type: value };
}
if (Object.keys(data).length > 0) {
cell.model.metadata.set('slideshow', data);
} else {
cell.model.metadata.delete('slideshow');
}
}
};
return new KeySelector(options);
}
/**
* Create an nbconvert selector.
*/
export function createNBConvertSelector(
optionsMap: ReadonlyPartialJSONObject
): KeySelector {
return new KeySelector({
key: 'raw_mimetype',
title: 'Raw NBConvert Format',
optionsMap: optionsMap,
validCellTypes: ['raw']
});
}
}
/**
* A namespace for private data.
*/
namespace Private {
/**
* An object which holds a widget and its sort rank.
*/
export interface IRankItem<T extends Widget = Widget> {
/**
* The widget for the item.
*/
widget: T;
/**
* The sort rank of the menu.
*/
rank: number;
}
/**
* A comparator function for widget rank items.
*/
export function itemCmp(first: IRankItem, second: IRankItem): number {
return first.rank - second.rank;
}
/**
* Create the node for a KeySelector.
*/
export function createSelectorNode(
options: NotebookTools.KeySelector.IOptions
): HTMLElement {
const name = options.key;
const title = options.title || name[0].toLocaleUpperCase() + name.slice(1);
const optionNodes: VirtualNode[] = [];
for (const label in options.optionsMap) {
const value = JSON.stringify(options.optionsMap[label]);
optionNodes.push(h.option({ value }, label));
}
const node = VirtualDOM.realize(
h.div({}, h.label(title, h.select({}, optionNodes)))
);
Styling.styleNode(node);
return node;
}
}