UNPKG

22.2 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 _input;
202 private _inputHidden;
203 private _inputWrapper;
204 private _inputPlaceholder;
205 private _inViewport;
206 private _inViewportChanged;
207 private _model;
208 private _placeholder;
209 private _readOnly;
210 private _ready;
211 private _resizeDebouncer;
212 private _syncCollapse;
213 private _syncEditable;
214}
215/**
216 * The namespace for the `Cell` class statics.
217 */
218export declare namespace Cell {
219 /**
220 * An options object for initializing a cell widget.
221 */
222 interface IOptions<T extends ICellModel> {
223 /**
224 * The model used by the cell.
225 */
226 model: T;
227 /**
228 * The factory object for customizable cell children.
229 */
230 contentFactory: IContentFactory;
231 /**
232 * The configuration options for the text editor widget.
233 */
234 editorConfig?: Record<string, any>;
235 /**
236 * Editor extensions to be added.
237 */
238 editorExtensions?: Extension[];
239 /**
240 * Cell widget layout.
241 */
242 layout?: PanelLayout;
243 /**
244 * The maximum number of output items to display in cell output.
245 */
246 maxNumberOutputs?: number;
247 /**
248 * Whether to split stdin line history by kernel session or keep globally accessible.
249 */
250 inputHistoryScope?: 'global' | 'session';
251 /**
252 * Whether this cell is a placeholder for future rendering.
253 */
254 placeholder?: boolean;
255 /**
256 * The application language translator.
257 */
258 translator?: ITranslator;
259 }
260 /**
261 * Cell heading
262 */
263 interface IHeading {
264 /**
265 * Heading text.
266 */
267 text: string;
268 /**
269 * HTML heading level.
270 */
271 level: number;
272 /**
273 * Index of the output containing the heading
274 */
275 outputIndex?: number;
276 /**
277 * Type of heading
278 */
279 type: HeadingType;
280 }
281 /**
282 * Type of headings
283 */
284 enum HeadingType {
285 /**
286 * Heading from HTML output
287 */
288 HTML = 0,
289 /**
290 * Heading from Markdown cell or Markdown output
291 */
292 Markdown = 1
293 }
294 /**
295 * The factory object for customizable cell children.
296 *
297 * This is used to allow users of cells to customize child content.
298 *
299 * This inherits from `OutputArea.IContentFactory` to avoid needless nesting and
300 * provide a single factory object for all notebook/cell/outputarea related
301 * widgets.
302 */
303 interface IContentFactory extends OutputArea.IContentFactory, InputArea.IContentFactory {
304 /**
305 * Create a new cell header for the parent widget.
306 */
307 createCellHeader(): ICellHeader;
308 /**
309 * Create a new cell header for the parent widget.
310 */
311 createCellFooter(): ICellFooter;
312 }
313 /**
314 * The default implementation of an `IContentFactory`.
315 *
316 * This includes a CodeMirror editor factory to make it easy to use out of the box.
317 */
318 class ContentFactory implements IContentFactory {
319 /**
320 * Create a content factory for a cell.
321 */
322 constructor(options: ContentFactory.IOptions);
323 /**
324 * The readonly editor factory that create code editors
325 */
326 get editorFactory(): CodeEditor.Factory;
327 /**
328 * Create a new cell header for the parent widget.
329 */
330 createCellHeader(): ICellHeader;
331 /**
332 * Create a new cell footer for the parent widget.
333 */
334 createCellFooter(): ICellFooter;
335 /**
336 * Create an input prompt.
337 */
338 createInputPrompt(): IInputPrompt;
339 /**
340 * Create the output prompt for the widget.
341 */
342 createOutputPrompt(): IOutputPrompt;
343 /**
344 * Create an stdin widget.
345 */
346 createStdin(options: Stdin.IOptions): IStdin;
347 private _editorFactory;
348 }
349 /**
350 * A namespace for cell content factory.
351 */
352 namespace ContentFactory {
353 /**
354 * Options for the content factory.
355 */
356 interface IOptions {
357 /**
358 * The editor factory used by the content factory.
359 */
360 editorFactory: CodeEditor.Factory;
361 }
362 }
363}
364/** ****************************************************************************
365 * CodeCell
366 ******************************************************************************/
367/**
368 * Code cell layout
369 *
370 * It will not detached the output area when the cell is detached.
371 */
372export declare class CodeCellLayout extends PanelLayout {
373 /**
374 * A message handler invoked on a `'before-attach'` message.
375 *
376 * #### Notes
377 * The default implementation of this method forwards the message
378 * to all widgets. It assumes all widget nodes are attached to the
379 * parent widget node.
380 *
381 * This may be reimplemented by subclasses as needed.
382 */
383 protected onBeforeAttach(msg: Message): void;
384 /**
385 * A message handler invoked on an `'after-detach'` message.
386 *
387 * #### Notes
388 * The default implementation of this method forwards the message
389 * to all widgets. It assumes all widget nodes are attached to the
390 * parent widget node.
391 *
392 * This may be reimplemented by subclasses as needed.
393 */
394 protected onAfterDetach(msg: Message): void;
395}
396/**
397 * A widget for a code cell.
398 */
399export declare class CodeCell extends Cell<ICodeCellModel> {
400 /**
401 * Construct a code cell widget.
402 */
403 constructor(options: CodeCell.IOptions);
404 /**
405 * Maximum number of outputs to display.
406 */
407 protected maxNumberOutputs: number | undefined;
408 /**
409 * Create children widgets.
410 */
411 protected initializeDOM(): void;
412 protected getOutputPlaceholderText(): string | undefined;
413 /**
414 * Initialize view state from model.
415 *
416 * #### Notes
417 * Should be called after construction. For convenience, returns this, so it
418 * can be chained in the construction, like `new Foo().initializeState();`
419 */
420 initializeState(): this;
421 get headings(): Cell.IHeading[];
422 /**
423 * Get the output area for the cell.
424 */
425 get outputArea(): OutputArea;
426 /**
427 * The view state of output being collapsed.
428 */
429 get outputHidden(): boolean;
430 set outputHidden(value: boolean);
431 /**
432 * Save view collapse state to model
433 */
434 saveCollapseState(): void;
435 /**
436 * Revert view collapse state from model.
437 *
438 * We consider the `collapsed` metadata key as the source of truth for outputs
439 * being hidden.
440 */
441 loadCollapseState(): void;
442 /**
443 * Whether the output is in a scrolled state?
444 */
445 get outputsScrolled(): boolean;
446 set outputsScrolled(value: boolean);
447 /**
448 * Save view collapse state to model
449 */
450 saveScrolledState(): void;
451 /**
452 * Revert view collapse state from model.
453 */
454 loadScrolledState(): void;
455 /**
456 * Whether to sync the scrolled state to the cell model.
457 */
458 get syncScrolled(): boolean;
459 set syncScrolled(value: boolean);
460 /**
461 * Handle the input being hidden.
462 *
463 * #### Notes
464 * This method is called by the case cell implementation and is
465 * subclasses here so the code cell can watch to see when input
466 * is hidden without accessing private state.
467 */
468 protected handleInputHidden(value: boolean): void;
469 /**
470 * Clone the cell, using the same model.
471 */
472 clone(): CodeCell;
473 /**
474 * Clone the OutputArea alone, returning a simplified output area, using the same model.
475 */
476 cloneOutputArea(): OutputArea;
477 /**
478 * Dispose of the resources used by the widget.
479 */
480 dispose(): void;
481 /**
482 * Handle changes in the model.
483 */
484 protected onStateChanged(model: ICellModel, args: IChangedArgs<any>): void;
485 /**
486 * Callback on output changes
487 */
488 protected onOutputChanged(): void;
489 /**
490 * Handle changes in the metadata.
491 */
492 protected onMetadataChanged(model: CellModel, args: IMapChange): void;
493 /**
494 * Handle changes in the number of outputs in the output area.
495 */
496 private _outputLengthHandler;
497 private _headingsCache;
498 private _rendermime;
499 private _outputHidden;
500 private _outputsScrolled;
501 private _outputWrapper;
502 private _outputPlaceholder;
503 private _output;
504 private _syncScrolled;
505}
506/**
507 * The namespace for the `CodeCell` class statics.
508 */
509export declare namespace CodeCell {
510 /**
511 * An options object for initializing a base cell widget.
512 */
513 interface IOptions extends Cell.IOptions<ICodeCellModel> {
514 /**
515 * Code cell layout.
516 */
517 layout?: CodeCellLayout;
518 /**
519 * The mime renderer for the cell widget.
520 */
521 rendermime: IRenderMimeRegistry;
522 }
523 /**
524 * Execute a cell given a client session.
525 */
526 function execute(cell: CodeCell, sessionContext: ISessionContext, metadata?: JSONObject): Promise<KernelMessage.IExecuteReplyMsg | void>;
527}
528/**
529 * `AttachmentsCell` - A base class for a cell widget that allows
530 * attachments to be drag/drop'd or pasted onto it
531 */
532export declare abstract class AttachmentsCell<T extends IAttachmentsCellModel> extends Cell<T> {
533 /**
534 * Handle the DOM events for the widget.
535 *
536 * @param event - The DOM event sent to the widget.
537 *
538 * #### Notes
539 * This method implements the DOM `EventListener` interface and is
540 * called in response to events on the notebook panel's node. It should
541 * not be called directly by user code.
542 */
543 handleEvent(event: Event): void;
544 /**
545 * Get the editor options at initialization.
546 *
547 * @returns Editor options
548 */
549 protected getEditorOptions(): InputArea.IOptions['editorOptions'];
550 /**
551 * Modify the cell source to include a reference to the attachment.
552 */
553 protected abstract updateCellSourceWithAttachment(attachmentName: string, URI?: string): void;
554 /**
555 * Handle `after-attach` messages for the widget.
556 */
557 protected onAfterAttach(msg: Message): void;
558 /**
559 * A message handler invoked on a `'before-detach'`
560 * message
561 */
562 protected onBeforeDetach(msg: Message): void;
563 private _evtDragOver;
564 /**
565 * Handle the `paste` event for the widget
566 */
567 private _evtPaste;
568 /**
569 * Handle the `drop` event for the widget
570 */
571 private _evtNativeDrop;
572 /**
573 * Handle the `'lm-drop'` event for the widget.
574 */
575 private _evtDrop;
576 /**
577 * Attaches all DataTransferItems (obtained from
578 * clipboard or native drop events) to the cell
579 */
580 private _attachFiles;
581 /**
582 * Takes in a file object and adds it to
583 * the cell attachments
584 */
585 private _attachFile;
586 /**
587 * Generates a unique URI for a file
588 * while preserving the file extension.
589 */
590 private _generateURI;
591}
592/** ****************************************************************************
593 * MarkdownCell
594 ******************************************************************************/
595/**
596 * A widget for a Markdown cell.
597 *
598 * #### Notes
599 * Things get complicated if we want the rendered text to update
600 * any time the text changes, the text editor model changes,
601 * or the input area model changes. We don't support automatically
602 * updating the rendered text in all of these cases.
603 */
604export declare class MarkdownCell extends AttachmentsCell<IMarkdownCellModel> {
605 /**
606 * Construct a Markdown cell widget.
607 */
608 constructor(options: MarkdownCell.IOptions);
609 /**
610 * Text that represents the highest heading (i.e. lowest level) if cell is a heading.
611 * Returns empty string if not a heading.
612 */
613 get headingInfo(): {
614 text: string;
615 level: number;
616 };
617 get headings(): Cell.IHeading[];
618 /**
619 * Whether the heading is collapsed or not.
620 */
621 get headingCollapsed(): boolean;
622 set headingCollapsed(value: boolean);
623 /**
624 * Number of collapsed sub cells.
625 */
626 get numberChildNodes(): number;
627 set numberChildNodes(value: number);
628 /**
629 * Signal emitted when the cell collapsed state changes.
630 */
631 get headingCollapsedChanged(): ISignal<MarkdownCell, boolean>;
632 /**
633 * Whether the cell is rendered.
634 */
635 get rendered(): boolean;
636 set rendered(value: boolean);
637 /**
638 * Signal emitted when the markdown cell rendered state changes
639 */
640 get renderedChanged(): ISignal<MarkdownCell, boolean>;
641 get showEditorForReadOnly(): boolean;
642 set showEditorForReadOnly(value: boolean);
643 /**
644 * Renderer
645 */
646 get renderer(): IRenderMime.IRenderer;
647 /**
648 * Dispose of the resources held by the widget.
649 */
650 dispose(): void;
651 /**
652 * Create children widgets.
653 */
654 protected initializeDOM(): void;
655 protected maybeCreateCollapseButton(): void;
656 /**
657 * Create, update or remove the hidden cells button.
658 * Note that the actual visibility is controlled in Static Notebook by toggling jp-mod-showHiddenCellsButton class.
659 */
660 protected maybeCreateOrUpdateExpandButton(): void;
661 /**
662 * Callback on content changed
663 */
664 protected onContentChanged(): void;
665 /**
666 * Render the collapse button for heading cells,
667 * and for collapsed heading cells render the "expand hidden cells"
668 * button.
669 */
670 protected renderCollapseButtons(widget: Widget): void;
671 /**
672 * Render an input instead of the text editor.
673 */
674 protected renderInput(widget: Widget): void;
675 /**
676 * Show the text editor instead of rendered input.
677 */
678 protected showEditor(): void;
679 protected onUpdateRequest(msg: Message): void;
680 /**
681 * Modify the cell source to include a reference to the attachment.
682 */
683 protected updateCellSourceWithAttachment(attachmentName: string, URI?: string): void;
684 /**
685 * Handle the rendered state.
686 */
687 private _handleRendered;
688 /**
689 * Update the rendered input.
690 */
691 private _updateRenderedInput;
692 /**
693 * Clone the cell, using the same model.
694 */
695 clone(): MarkdownCell;
696 private _headingsCache;
697 private _headingCollapsed;
698 private _headingCollapsedChanged;
699 private _monitor;
700 private _numberChildNodes;
701 private _prevText;
702 private _renderer;
703 private _rendermime;
704 private _rendered;
705 private _renderedChanged;
706 private _showEditorForReadOnlyMarkdown;
707}
708/**
709 * The namespace for the `CodeCell` class statics.
710 */
711export declare namespace MarkdownCell {
712 /**
713 * An options object for initializing a base cell widget.
714 */
715 interface IOptions extends Cell.IOptions<IMarkdownCellModel> {
716 /**
717 * The mime renderer for the cell widget.
718 */
719 rendermime: IRenderMimeRegistry;
720 /**
721 * Show editor for read-only Markdown cells.
722 */
723 showEditorForReadOnlyMarkdown?: boolean;
724 }
725 /**
726 * Default value for showEditorForReadOnlyMarkdown.
727 */
728 const defaultShowEditorForReadOnlyMarkdown = true;
729}
730/** ****************************************************************************
731 * RawCell
732 ******************************************************************************/
733/**
734 * A widget for a raw cell.
735 */
736export declare class RawCell extends Cell<IRawCellModel> {
737 /**
738 * Construct a raw cell widget.
739 */
740 constructor(options: RawCell.IOptions);
741 /**
742 * Clone the cell, using the same model.
743 */
744 clone(): RawCell;
745}
746/**
747 * The namespace for the `RawCell` class statics.
748 */
749export declare namespace RawCell {
750 /**
751 * An options object for initializing a base cell widget.
752 */
753 interface IOptions extends Cell.IOptions<IRawCellModel> {
754 }
755}