UNPKG

22.4 kBTypeScriptView Raw
1import { Extension } from '@codemirror/state';
2import { ISessionContext } from '@jupyterlab/apputils';
3import { IChangedArgs } from '@jupyterlab/coreutils';
4import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
5import { IOutputPrompt, IStdin, OutputArea, Stdin } from '@jupyterlab/outputarea';
6import { IRenderMime, IRenderMimeRegistry } from '@jupyterlab/rendermime';
7import { KernelMessage } from '@jupyterlab/services';
8import { IMapChange } from '@jupyter/ydoc';
9import { ITranslator } from '@jupyterlab/translation';
10import { JSONObject } from '@lumino/coreutils';
11import { Message } from '@lumino/messaging';
12import { ISignal, Signal } from '@lumino/signaling';
13import { PanelLayout, Widget } from '@lumino/widgets';
14import { ICellFooter, ICellHeader } from './headerfooter';
15import { IInputPrompt, InputArea } from './inputarea';
16import { CellModel, IAttachmentsCellModel, ICellModel, ICodeCellModel, IMarkdownCellModel, IRawCellModel } from './model';
17/** ****************************************************************************
18 * Cell
19 ******************************************************************************/
20/**
21 * A base cell widget.
22 */
23export declare class Cell<T extends ICellModel = ICellModel> extends Widget {
24 /**
25 * Construct a new base cell widget.
26 */
27 constructor(options: Cell.IOptions<T>);
28 /**
29 * Initialize view state from model.
30 *
31 * #### Notes
32 * Should be called after construction. For convenience, returns this, so it
33 * can be chained in the construction, like `new Foo().initializeState();`
34 */
35 initializeState(): this;
36 /**
37 * The content factory used by the widget.
38 */
39 readonly contentFactory: Cell.IContentFactory;
40 /**
41 * Signal to indicate that widget has changed visibly (in size, in type, etc)
42 */
43 get displayChanged(): ISignal<this, void>;
44 /**
45 * Whether the cell is in viewport or not.
46 */
47 get inViewport(): boolean;
48 set inViewport(v: boolean);
49 /**
50 * Will emit true just after the node is attached to the DOM
51 * Will emit false just before the node is detached of the DOM
52 */
53 get inViewportChanged(): ISignal<Cell, boolean>;
54 /**
55 * Whether the cell is a placeholder not yet fully rendered or not.
56 */
57 protected get placeholder(): boolean;
58 protected set placeholder(v: boolean);
59 /**
60 * Get the prompt node used by the cell.
61 */
62 get promptNode(): HTMLElement | null;
63 /**
64 * Get the CodeEditorWrapper used by the cell.
65 */
66 get editorWidget(): CodeEditorWrapper | null;
67 /**
68 * Get the CodeEditor used by the cell.
69 */
70 get editor(): CodeEditor.IEditor | null;
71 /**
72 * Editor configuration
73 */
74 get editorConfig(): Record<string, any>;
75 /**
76 * Cell headings
77 */
78 get headings(): Cell.IHeading[];
79 /**
80 * Get the model used by the cell.
81 */
82 get model(): T;
83 /**
84 * Get the input area for the cell.
85 */
86 get inputArea(): InputArea | null;
87 /**
88 * The read only state of the cell.
89 */
90 get readOnly(): boolean;
91 set readOnly(value: boolean);
92 /**
93 * Whether the cell is a placeholder that defer rendering
94 *
95 * #### Notes
96 * You can wait for the promise `Cell.ready` to wait for the
97 * cell to be rendered.
98 */
99 isPlaceholder(): boolean;
100 /**
101 * Save view editable state to model
102 */
103 saveEditableState(): void;
104 /**
105 * Load view editable state from model.
106 */
107 loadEditableState(): void;
108 /**
109 * A promise that resolves when the widget renders for the first time.
110 */
111 get ready(): Promise<void>;
112 /**
113 * Set the prompt for the widget.
114 */
115 setPrompt(value: string): void;
116 /**
117 * The view state of input being hidden.
118 */
119 get inputHidden(): boolean;
120 set inputHidden(value: boolean);
121 /**
122 * Save view collapse state to model
123 */
124 saveCollapseState(): void;
125 /**
126 * Revert view collapse state from model.
127 */
128 loadCollapseState(): void;
129 /**
130 * Handle the input being hidden.
131 *
132 * #### Notes
133 * This is called by the `inputHidden` setter so that subclasses
134 * can perform actions upon the input being hidden without accessing
135 * private state.
136 */
137 protected handleInputHidden(value: boolean): void;
138 /**
139 * Whether to sync the collapse state to the cell model.
140 */
141 get syncCollapse(): boolean;
142 set syncCollapse(value: boolean);
143 /**
144 * Whether to sync the editable state to the cell model.
145 */
146 get syncEditable(): boolean;
147 set syncEditable(value: boolean);
148 /**
149 * Clone the cell, using the same model.
150 */
151 clone(): Cell<T>;
152 /**
153 * Dispose of the resources held by the widget.
154 */
155 dispose(): void;
156 /**
157 * Update the editor configuration with the partial provided dictionary.
158 *
159 * @param v Partial editor configuration
160 */
161 updateEditorConfig(v: Record<string, any>): void;
162 /**
163 * Create children widgets.
164 */
165 protected initializeDOM(): void;
166 /**
167 * Get the editor options at initialization.
168 *
169 * @returns Editor options
170 */
171 protected getEditorOptions(): InputArea.IOptions['editorOptions'];
172 /**
173 * Handle `before-attach` messages.
174 */
175 protected onBeforeAttach(msg: Message): void;
176 /**
177 * Handle `after-attach` messages.
178 */
179 protected onAfterAttach(msg: Message): void;
180 /**
181 * Handle `'activate-request'` messages.
182 */
183 protected onActivateRequest(msg: Message): void;
184 /**
185 * Handle `resize` messages.
186 */
187 protected onResize(msg: Widget.ResizeMessage): void;
188 /**
189 * Handle `update-request` messages.
190 */
191 protected onUpdateRequest(msg: Message): void;
192 protected onContentChanged(): void;
193 /**
194 * Handle changes in the metadata.
195 */
196 protected onMetadataChanged(model: CellModel, args: IMapChange): void;
197 protected prompt: string;
198 protected translator: ITranslator;
199 protected _displayChanged: Signal<this, void>;
200 private _editorConfig;
201 private _editorExtensions;
202 private _input;
203 private _inputHidden;
204 private _inputWrapper;
205 private _inputPlaceholder;
206 private _inViewport;
207 private _inViewportChanged;
208 private _model;
209 private _placeholder;
210 private _readOnly;
211 private _ready;
212 private _resizeDebouncer;
213 private _syncCollapse;
214 private _syncEditable;
215}
216/**
217 * The namespace for the `Cell` class statics.
218 */
219export declare namespace Cell {
220 /**
221 * An options object for initializing a cell widget.
222 */
223 interface IOptions<T extends ICellModel> {
224 /**
225 * The model used by the cell.
226 */
227 model: T;
228 /**
229 * The factory object for customizable cell children.
230 */
231 contentFactory: IContentFactory;
232 /**
233 * The configuration options for the text editor widget.
234 */
235 editorConfig?: Record<string, any>;
236 /**
237 * Editor extensions to be added.
238 */
239 editorExtensions?: Extension[];
240 /**
241 * Cell widget layout.
242 */
243 layout?: PanelLayout;
244 /**
245 * The maximum number of output items to display in cell output.
246 */
247 maxNumberOutputs?: number;
248 /**
249 * Whether to split stdin line history by kernel session or keep globally accessible.
250 */
251 inputHistoryScope?: 'global' | 'session';
252 /**
253 * Whether this cell is a placeholder for future rendering.
254 */
255 placeholder?: boolean;
256 /**
257 * The application language translator.
258 */
259 translator?: ITranslator;
260 }
261 /**
262 * Cell heading
263 */
264 interface IHeading {
265 /**
266 * Heading text.
267 */
268 text: string;
269 /**
270 * HTML heading level.
271 */
272 level: number;
273 /**
274 * Index of the output containing the heading
275 */
276 outputIndex?: number;
277 /**
278 * Type of heading
279 */
280 type: HeadingType;
281 }
282 /**
283 * Type of headings
284 */
285 enum HeadingType {
286 /**
287 * Heading from HTML output
288 */
289 HTML = 0,
290 /**
291 * Heading from Markdown cell or Markdown output
292 */
293 Markdown = 1
294 }
295 /**
296 * The factory object for customizable cell children.
297 *
298 * This is used to allow users of cells to customize child content.
299 *
300 * This inherits from `OutputArea.IContentFactory` to avoid needless nesting and
301 * provide a single factory object for all notebook/cell/outputarea related
302 * widgets.
303 */
304 interface IContentFactory extends OutputArea.IContentFactory, InputArea.IContentFactory {
305 /**
306 * Create a new cell header for the parent widget.
307 */
308 createCellHeader(): ICellHeader;
309 /**
310 * Create a new cell header for the parent widget.
311 */
312 createCellFooter(): ICellFooter;
313 }
314 /**
315 * The default implementation of an `IContentFactory`.
316 *
317 * This includes a CodeMirror editor factory to make it easy to use out of the box.
318 */
319 class ContentFactory implements IContentFactory {
320 /**
321 * Create a content factory for a cell.
322 */
323 constructor(options: ContentFactory.IOptions);
324 /**
325 * The readonly editor factory that create code editors
326 */
327 get editorFactory(): CodeEditor.Factory;
328 /**
329 * Create a new cell header for the parent widget.
330 */
331 createCellHeader(): ICellHeader;
332 /**
333 * Create a new cell footer for the parent widget.
334 */
335 createCellFooter(): ICellFooter;
336 /**
337 * Create an input prompt.
338 */
339 createInputPrompt(): IInputPrompt;
340 /**
341 * Create the output prompt for the widget.
342 */
343 createOutputPrompt(): IOutputPrompt;
344 /**
345 * Create an stdin widget.
346 */
347 createStdin(options: Stdin.IOptions): IStdin;
348 private _editorFactory;
349 }
350 /**
351 * A namespace for cell content factory.
352 */
353 namespace ContentFactory {
354 /**
355 * Options for the content factory.
356 */
357 interface IOptions {
358 /**
359 * The editor factory used by the content factory.
360 */
361 editorFactory: CodeEditor.Factory;
362 }
363 }
364}
365/** ****************************************************************************
366 * CodeCell
367 ******************************************************************************/
368/**
369 * Code cell layout
370 *
371 * It will not detached the output area when the cell is detached.
372 */
373export declare class CodeCellLayout extends PanelLayout {
374 /**
375 * A message handler invoked on a `'before-attach'` message.
376 *
377 * #### Notes
378 * The default implementation of this method forwards the message
379 * to all widgets. It assumes all widget nodes are attached to the
380 * parent widget node.
381 *
382 * This may be reimplemented by subclasses as needed.
383 */
384 protected onBeforeAttach(msg: Message): void;
385 /**
386 * A message handler invoked on an `'after-detach'` message.
387 *
388 * #### Notes
389 * The default implementation of this method forwards the message
390 * to all widgets. It assumes all widget nodes are attached to the
391 * parent widget node.
392 *
393 * This may be reimplemented by subclasses as needed.
394 */
395 protected onAfterDetach(msg: Message): void;
396}
397/**
398 * A widget for a code cell.
399 */
400export declare class CodeCell extends Cell<ICodeCellModel> {
401 /**
402 * Construct a code cell widget.
403 */
404 constructor(options: CodeCell.IOptions);
405 /**
406 * Maximum number of outputs to display.
407 */
408 protected maxNumberOutputs: number | undefined;
409 /**
410 * Create children widgets.
411 */
412 protected initializeDOM(): void;
413 protected getOutputPlaceholderText(): string | undefined;
414 /**
415 * Initialize view state from model.
416 *
417 * #### Notes
418 * Should be called after construction. For convenience, returns this, so it
419 * can be chained in the construction, like `new Foo().initializeState();`
420 */
421 initializeState(): this;
422 get headings(): Cell.IHeading[];
423 /**
424 * Get the output area for the cell.
425 */
426 get outputArea(): OutputArea;
427 /**
428 * The view state of output being collapsed.
429 */
430 get outputHidden(): boolean;
431 set outputHidden(value: boolean);
432 /**
433 * Save view collapse state to model
434 */
435 saveCollapseState(): void;
436 /**
437 * Revert view collapse state from model.
438 *
439 * We consider the `collapsed` metadata key as the source of truth for outputs
440 * being hidden.
441 */
442 loadCollapseState(): void;
443 /**
444 * Whether the output is in a scrolled state?
445 */
446 get outputsScrolled(): boolean;
447 set outputsScrolled(value: boolean);
448 /**
449 * Update the Prompt Overlay Icon
450 */
451 updatePromptOverlayIcon(): void;
452 /**
453 * Save view collapse state to model
454 */
455 saveScrolledState(): void;
456 /**
457 * Revert view collapse state from model.
458 */
459 loadScrolledState(): void;
460 /**
461 * Whether to sync the scrolled state to the cell model.
462 */
463 get syncScrolled(): boolean;
464 set syncScrolled(value: boolean);
465 /**
466 * Handle the input being hidden.
467 *
468 * #### Notes
469 * This method is called by the case cell implementation and is
470 * subclasses here so the code cell can watch to see when input
471 * is hidden without accessing private state.
472 */
473 protected handleInputHidden(value: boolean): void;
474 /**
475 * Clone the cell, using the same model.
476 */
477 clone(): CodeCell;
478 /**
479 * Clone the OutputArea alone, returning a simplified output area, using the same model.
480 */
481 cloneOutputArea(): OutputArea;
482 /**
483 * Dispose of the resources used by the widget.
484 */
485 dispose(): void;
486 /**
487 * Handle changes in the model.
488 */
489 protected onStateChanged(model: ICellModel, args: IChangedArgs<any>): void;
490 /**
491 * Callback on output changes
492 */
493 protected onOutputChanged(): void;
494 /**
495 * Handle changes in the metadata.
496 */
497 protected onMetadataChanged(model: CellModel, args: IMapChange): void;
498 /**
499 * Handle changes in the number of outputs in the output area.
500 */
501 private _outputLengthHandler;
502 /**
503 * Handle changes in input/output proportions in side-by-side mode.
504 */
505 private _sizeChangedHandler;
506 private _headingsCache;
507 private _rendermime;
508 private _outputHidden;
509 private _outputsScrolled;
510 private _outputWrapper;
511 private _outputPlaceholder;
512 private _output;
513 private _syncScrolled;
514}
515/**
516 * The namespace for the `CodeCell` class statics.
517 */
518export declare namespace CodeCell {
519 /**
520 * An options object for initializing a base cell widget.
521 */
522 interface IOptions extends Cell.IOptions<ICodeCellModel> {
523 /**
524 * Code cell layout.
525 */
526 layout?: CodeCellLayout;
527 /**
528 * The mime renderer for the cell widget.
529 */
530 rendermime: IRenderMimeRegistry;
531 }
532 /**
533 * Execute a cell given a client session.
534 */
535 function execute(cell: CodeCell, sessionContext: ISessionContext, metadata?: JSONObject): Promise<KernelMessage.IExecuteReplyMsg | void>;
536}
537/**
538 * `AttachmentsCell` - A base class for a cell widget that allows
539 * attachments to be drag/drop'd or pasted onto it
540 */
541export declare abstract class AttachmentsCell<T extends IAttachmentsCellModel> extends Cell<T> {
542 /**
543 * Handle the DOM events for the widget.
544 *
545 * @param event - The DOM event sent to the widget.
546 *
547 * #### Notes
548 * This method implements the DOM `EventListener` interface and is
549 * called in response to events on the notebook panel's node. It should
550 * not be called directly by user code.
551 */
552 handleEvent(event: Event): void;
553 /**
554 * Get the editor options at initialization.
555 *
556 * @returns Editor options
557 */
558 protected getEditorOptions(): InputArea.IOptions['editorOptions'];
559 /**
560 * Modify the cell source to include a reference to the attachment.
561 */
562 protected abstract updateCellSourceWithAttachment(attachmentName: string, URI?: string): void;
563 /**
564 * Handle `after-attach` messages for the widget.
565 */
566 protected onAfterAttach(msg: Message): void;
567 /**
568 * A message handler invoked on a `'before-detach'`
569 * message
570 */
571 protected onBeforeDetach(msg: Message): void;
572 private _evtDragOver;
573 /**
574 * Handle the `paste` event for the widget
575 */
576 private _evtPaste;
577 /**
578 * Handle the `drop` event for the widget
579 */
580 private _evtNativeDrop;
581 /**
582 * Handle the `'lm-drop'` event for the widget.
583 */
584 private _evtDrop;
585 /**
586 * Attaches all DataTransferItems (obtained from
587 * clipboard or native drop events) to the cell
588 */
589 private _attachFiles;
590 /**
591 * Takes in a file object and adds it to
592 * the cell attachments
593 */
594 private _attachFile;
595 /**
596 * Generates a unique URI for a file
597 * while preserving the file extension.
598 */
599 private _generateURI;
600}
601/** ****************************************************************************
602 * MarkdownCell
603 ******************************************************************************/
604/**
605 * A widget for a Markdown cell.
606 *
607 * #### Notes
608 * Things get complicated if we want the rendered text to update
609 * any time the text changes, the text editor model changes,
610 * or the input area model changes. We don't support automatically
611 * updating the rendered text in all of these cases.
612 */
613export declare class MarkdownCell extends AttachmentsCell<IMarkdownCellModel> {
614 /**
615 * Construct a Markdown cell widget.
616 */
617 constructor(options: MarkdownCell.IOptions);
618 /**
619 * Text that represents the highest heading (i.e. lowest level) if cell is a heading.
620 * Returns empty string if not a heading.
621 */
622 get headingInfo(): {
623 text: string;
624 level: number;
625 };
626 get headings(): Cell.IHeading[];
627 /**
628 * Whether the heading is collapsed or not.
629 */
630 get headingCollapsed(): boolean;
631 set headingCollapsed(value: boolean);
632 /**
633 * Number of collapsed sub cells.
634 */
635 get numberChildNodes(): number;
636 set numberChildNodes(value: number);
637 /**
638 * Signal emitted when the cell collapsed state changes.
639 */
640 get headingCollapsedChanged(): ISignal<MarkdownCell, boolean>;
641 /**
642 * Whether the cell is rendered.
643 */
644 get rendered(): boolean;
645 set rendered(value: boolean);
646 /**
647 * Signal emitted when the markdown cell rendered state changes
648 */
649 get renderedChanged(): ISignal<MarkdownCell, boolean>;
650 get showEditorForReadOnly(): boolean;
651 set showEditorForReadOnly(value: boolean);
652 /**
653 * Renderer
654 */
655 get renderer(): IRenderMime.IRenderer;
656 /**
657 * Dispose of the resources held by the widget.
658 */
659 dispose(): void;
660 /**
661 * Create children widgets.
662 */
663 protected initializeDOM(): void;
664 protected maybeCreateCollapseButton(): void;
665 /**
666 * Create, update or remove the hidden cells button.
667 * Note that the actual visibility is controlled in Static Notebook by toggling jp-mod-showHiddenCellsButton class.
668 */
669 protected maybeCreateOrUpdateExpandButton(): void;
670 /**
671 * Callback on content changed
672 */
673 protected onContentChanged(): void;
674 /**
675 * Render the collapse button for heading cells,
676 * and for collapsed heading cells render the "expand hidden cells"
677 * button.
678 */
679 protected renderCollapseButtons(widget: Widget): void;
680 /**
681 * Render an input instead of the text editor.
682 */
683 protected renderInput(widget: Widget): void;
684 /**
685 * Show the text editor instead of rendered input.
686 */
687 protected showEditor(): void;
688 protected onUpdateRequest(msg: Message): void;
689 /**
690 * Modify the cell source to include a reference to the attachment.
691 */
692 protected updateCellSourceWithAttachment(attachmentName: string, URI?: string): void;
693 /**
694 * Handle the rendered state.
695 */
696 private _handleRendered;
697 /**
698 * Update the rendered input.
699 */
700 private _updateRenderedInput;
701 /**
702 * Clone the cell, using the same model.
703 */
704 clone(): MarkdownCell;
705 private _headingsCache;
706 private _headingCollapsed;
707 private _headingCollapsedChanged;
708 private _monitor;
709 private _numberChildNodes;
710 private _prevText;
711 private _renderer;
712 private _rendermime;
713 private _rendered;
714 private _renderedChanged;
715 private _showEditorForReadOnlyMarkdown;
716}
717/**
718 * The namespace for the `CodeCell` class statics.
719 */
720export declare namespace MarkdownCell {
721 /**
722 * An options object for initializing a base cell widget.
723 */
724 interface IOptions extends Cell.IOptions<IMarkdownCellModel> {
725 /**
726 * The mime renderer for the cell widget.
727 */
728 rendermime: IRenderMimeRegistry;
729 /**
730 * Show editor for read-only Markdown cells.
731 */
732 showEditorForReadOnlyMarkdown?: boolean;
733 }
734 /**
735 * Default value for showEditorForReadOnlyMarkdown.
736 */
737 const defaultShowEditorForReadOnlyMarkdown = true;
738}
739/** ****************************************************************************
740 * RawCell
741 ******************************************************************************/
742/**
743 * A widget for a raw cell.
744 */
745export declare class RawCell extends Cell<IRawCellModel> {
746 /**
747 * Construct a raw cell widget.
748 */
749 constructor(options: RawCell.IOptions);
750 /**
751 * Clone the cell, using the same model.
752 */
753 clone(): RawCell;
754}
755/**
756 * The namespace for the `RawCell` class statics.
757 */
758export declare namespace RawCell {
759 /**
760 * An options object for initializing a base cell widget.
761 */
762 interface IOptions extends Cell.IOptions<IRawCellModel> {
763 }
764}