UNPKG

17.5 kBTypeScriptView Raw
1import { ISessionContext } from '@jupyterlab/apputils';
2import { IChangedArgs } from '@jupyterlab/coreutils';
3import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
4import { IObservableJSON, IObservableMap } from '@jupyterlab/observables';
5import { IOutputPrompt, IStdin, OutputArea, Stdin } from '@jupyterlab/outputarea';
6import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
7import { KernelMessage } from '@jupyterlab/services';
8import { JSONObject, JSONValue, PartialJSONValue } from '@lumino/coreutils';
9import { Message } from '@lumino/messaging';
10import { ISignal, Signal } from '@lumino/signaling';
11import { Widget } from '@lumino/widgets';
12import { ICellFooter, ICellHeader } from './headerfooter';
13import { IInputPrompt, InputArea } from './inputarea';
14import { IAttachmentsCellModel, ICellModel, ICodeCellModel, IMarkdownCellModel, IRawCellModel } from './model';
15export declare const MARKDOWN_HEADING_COLLAPSED = "jp-MarkdownHeadingCollapsed";
16/** ****************************************************************************
17 * Cell
18 ******************************************************************************/
19/**
20 * A base cell widget.
21 */
22export declare class Cell<T extends ICellModel = ICellModel> extends Widget {
23 /**
24 * Construct a new base cell widget.
25 */
26 constructor(options: Cell.IOptions<T>);
27 /**
28 * Initialize view state from model.
29 *
30 * #### Notes
31 * Should be called after construction. For convenience, returns this, so it
32 * can be chained in the construction, like `new Foo().initializeState();`
33 */
34 initializeState(): this;
35 /**
36 * The content factory used by the widget.
37 */
38 readonly contentFactory: Cell.IContentFactory;
39 /**
40 * Signal to indicate that widget has changed visibly (in size, in type, etc)
41 */
42 get displayChanged(): ISignal<this, void>;
43 /**
44 * Get the prompt node used by the cell.
45 */
46 get promptNode(): HTMLElement;
47 /**
48 * Get the CodeEditorWrapper used by the cell.
49 */
50 get editorWidget(): CodeEditorWrapper;
51 /**
52 * Get the CodeEditor used by the cell.
53 */
54 get editor(): CodeEditor.IEditor;
55 /**
56 * Get the model used by the cell.
57 */
58 get model(): T;
59 /**
60 * Get the input area for the cell.
61 */
62 get inputArea(): InputArea;
63 /**
64 * The read only state of the cell.
65 */
66 get readOnly(): boolean;
67 set readOnly(value: boolean);
68 /**
69 * Save view editable state to model
70 */
71 saveEditableState(): void;
72 /**
73 * Load view editable state from model.
74 */
75 loadEditableState(): void;
76 /**
77 * A promise that resolves when the widget renders for the first time.
78 */
79 get ready(): Promise<void>;
80 /**
81 * Set the prompt for the widget.
82 */
83 setPrompt(value: string): void;
84 /**
85 * The view state of input being hidden.
86 */
87 get inputHidden(): boolean;
88 set inputHidden(value: boolean);
89 /**
90 * Save view collapse state to model
91 */
92 saveCollapseState(): void;
93 /**
94 * Revert view collapse state from model.
95 */
96 loadCollapseState(): void;
97 /**
98 * Handle the input being hidden.
99 *
100 * #### Notes
101 * This is called by the `inputHidden` setter so that subclasses
102 * can perform actions upon the input being hidden without accessing
103 * private state.
104 */
105 protected handleInputHidden(value: boolean): void;
106 /**
107 * Whether to sync the collapse state to the cell model.
108 */
109 get syncCollapse(): boolean;
110 set syncCollapse(value: boolean);
111 /**
112 * Whether to sync the editable state to the cell model.
113 */
114 get syncEditable(): boolean;
115 set syncEditable(value: boolean);
116 /**
117 * Clone the cell, using the same model.
118 */
119 clone(): Cell<T>;
120 /**
121 * Dispose of the resources held by the widget.
122 */
123 dispose(): void;
124 /**
125 * Handle `after-attach` messages.
126 */
127 protected onAfterAttach(msg: Message): void;
128 /**
129 * Handle `'activate-request'` messages.
130 */
131 protected onActivateRequest(msg: Message): void;
132 /**
133 * Handle `fit-request` messages.
134 */
135 protected onFitRequest(msg: Message): void;
136 /**
137 * Handle `resize` messages.
138 */
139 protected onResize(msg: Widget.ResizeMessage): void;
140 /**
141 * Handle `update-request` messages.
142 */
143 protected onUpdateRequest(msg: Message): void;
144 /**
145 * Handle changes in the metadata.
146 */
147 protected onMetadataChanged(model: IObservableJSON, args: IObservableMap.IChangedArgs<PartialJSONValue | undefined>): void;
148 protected _displayChanged: Signal<this, void>;
149 private _readOnly;
150 private _model;
151 private _inputHidden;
152 private _input;
153 private _inputWrapper;
154 private _inputPlaceholder;
155 private _syncCollapse;
156 private _syncEditable;
157 private _resizeDebouncer;
158}
159/**
160 * The namespace for the `Cell` class statics.
161 */
162export declare namespace Cell {
163 /**
164 * An options object for initializing a cell widget.
165 */
166 interface IOptions<T extends ICellModel> {
167 /**
168 * The model used by the cell.
169 */
170 model: T;
171 /**
172 * The factory object for customizable cell children.
173 */
174 contentFactory?: IContentFactory;
175 /**
176 * The configuration options for the text editor widget.
177 */
178 editorConfig?: Partial<CodeEditor.IConfig>;
179 /**
180 * Whether to send an update request to the editor when it is shown.
181 */
182 updateEditorOnShow?: boolean;
183 /**
184 * The maximum number of output items to display in cell output.
185 */
186 maxNumberOutputs?: number;
187 /**
188 * Whether this cell is a placeholder for future rendering.
189 */
190 placeholder?: boolean;
191 }
192 /**
193 * The factory object for customizable cell children.
194 *
195 * This is used to allow users of cells to customize child content.
196 *
197 * This inherits from `OutputArea.IContentFactory` to avoid needless nesting and
198 * provide a single factory object for all notebook/cell/outputarea related
199 * widgets.
200 */
201 interface IContentFactory extends OutputArea.IContentFactory, InputArea.IContentFactory {
202 /**
203 * Create a new cell header for the parent widget.
204 */
205 createCellHeader(): ICellHeader;
206 /**
207 * Create a new cell header for the parent widget.
208 */
209 createCellFooter(): ICellFooter;
210 }
211 /**
212 * The default implementation of an `IContentFactory`.
213 *
214 * This includes a CodeMirror editor factory to make it easy to use out of the box.
215 */
216 class ContentFactory implements IContentFactory {
217 /**
218 * Create a content factory for a cell.
219 */
220 constructor(options?: ContentFactory.IOptions);
221 /**
222 * The readonly editor factory that create code editors
223 */
224 get editorFactory(): CodeEditor.Factory;
225 /**
226 * Create a new cell header for the parent widget.
227 */
228 createCellHeader(): ICellHeader;
229 /**
230 * Create a new cell header for the parent widget.
231 */
232 createCellFooter(): ICellFooter;
233 /**
234 * Create an input prompt.
235 */
236 createInputPrompt(): IInputPrompt;
237 /**
238 * Create the output prompt for the widget.
239 */
240 createOutputPrompt(): IOutputPrompt;
241 /**
242 * Create an stdin widget.
243 */
244 createStdin(options: Stdin.IOptions): IStdin;
245 private _editorFactory;
246 }
247 /**
248 * A namespace for cell content factory.
249 */
250 namespace ContentFactory {
251 /**
252 * Options for the content factory.
253 */
254 interface IOptions {
255 /**
256 * The editor factory used by the content factory.
257 *
258 * If this is not passed, a default CodeMirror editor factory
259 * will be used.
260 */
261 editorFactory?: CodeEditor.Factory;
262 }
263 }
264 /**
265 * The default content factory for cells.
266 */
267 const defaultContentFactory: ContentFactory;
268}
269/** ****************************************************************************
270 * CodeCell
271 ******************************************************************************/
272/**
273 * A widget for a code cell.
274 */
275export declare class CodeCell extends Cell<ICodeCellModel> {
276 /**
277 * Construct a code cell widget.
278 */
279 constructor(options: CodeCell.IOptions);
280 /**
281 * Initialize view state from model.
282 *
283 * #### Notes
284 * Should be called after construction. For convenience, returns this, so it
285 * can be chained in the construction, like `new Foo().initializeState();`
286 */
287 initializeState(): this;
288 /**
289 * Get the output area for the cell.
290 */
291 get outputArea(): OutputArea;
292 /**
293 * The view state of output being collapsed.
294 */
295 get outputHidden(): boolean;
296 set outputHidden(value: boolean);
297 /**
298 * Save view collapse state to model
299 */
300 saveCollapseState(): void;
301 /**
302 * Revert view collapse state from model.
303 *
304 * We consider the `collapsed` metadata key as the source of truth for outputs
305 * being hidden.
306 */
307 loadCollapseState(): void;
308 /**
309 * Whether the output is in a scrolled state?
310 */
311 get outputsScrolled(): boolean;
312 set outputsScrolled(value: boolean);
313 /**
314 * Save view collapse state to model
315 */
316 saveScrolledState(): void;
317 /**
318 * Revert view collapse state from model.
319 */
320 loadScrolledState(): void;
321 /**
322 * Whether to sync the scrolled state to the cell model.
323 */
324 get syncScrolled(): boolean;
325 set syncScrolled(value: boolean);
326 /**
327 * Handle the input being hidden.
328 *
329 * #### Notes
330 * This method is called by the case cell implementation and is
331 * subclasses here so the code cell can watch to see when input
332 * is hidden without accessing private state.
333 */
334 protected handleInputHidden(value: boolean): void;
335 /**
336 * Clone the cell, using the same model.
337 */
338 clone(): CodeCell;
339 /**
340 * Clone the OutputArea alone, returning a simplified output area, using the same model.
341 */
342 cloneOutputArea(): OutputArea;
343 /**
344 * Dispose of the resources used by the widget.
345 */
346 dispose(): void;
347 /**
348 * Handle changes in the model.
349 */
350 protected onStateChanged(model: ICellModel, args: IChangedArgs<any>): void;
351 /**
352 * Handle changes in the metadata.
353 */
354 protected onMetadataChanged(model: IObservableJSON, args: IObservableMap.IChangedArgs<JSONValue>): void;
355 /**
356 * Handle changes in the number of outputs in the output area.
357 */
358 private _outputLengthHandler;
359 private _rendermime;
360 private _outputHidden;
361 private _outputsScrolled;
362 private _outputWrapper;
363 private _outputPlaceholder;
364 private _output;
365 private _syncScrolled;
366 private _savingMetadata;
367}
368/**
369 * The namespace for the `CodeCell` class statics.
370 */
371export declare namespace CodeCell {
372 /**
373 * An options object for initializing a base cell widget.
374 */
375 interface IOptions extends Cell.IOptions<ICodeCellModel> {
376 /**
377 * The mime renderer for the cell widget.
378 */
379 rendermime: IRenderMimeRegistry;
380 }
381 /**
382 * Execute a cell given a client session.
383 */
384 function execute(cell: CodeCell, sessionContext: ISessionContext, metadata?: JSONObject): Promise<KernelMessage.IExecuteReplyMsg | void>;
385}
386/**
387 * `AttachmentsCell` - A base class for a cell widget that allows
388 * attachments to be drag/drop'd or pasted onto it
389 */
390export declare abstract class AttachmentsCell<T extends IAttachmentsCellModel> extends Cell<T> {
391 /**
392 * Handle the DOM events for the widget.
393 *
394 * @param event - The DOM event sent to the widget.
395 *
396 * #### Notes
397 * This method implements the DOM `EventListener` interface and is
398 * called in response to events on the notebook panel's node. It should
399 * not be called directly by user code.
400 */
401 handleEvent(event: Event): void;
402 /**
403 * Modify the cell source to include a reference to the attachment.
404 */
405 protected abstract updateCellSourceWithAttachment(attachmentName: string, URI?: string): void;
406 /**
407 * Handle `after-attach` messages for the widget.
408 */
409 protected onAfterAttach(msg: Message): void;
410 /**
411 * A message handler invoked on a `'before-detach'`
412 * message
413 */
414 protected onBeforeDetach(msg: Message): void;
415 private _evtDragOver;
416 /**
417 * Handle the `paste` event for the widget
418 */
419 private _evtPaste;
420 /**
421 * Handle the `drop` event for the widget
422 */
423 private _evtNativeDrop;
424 /**
425 * Handle the `'lm-drop'` event for the widget.
426 */
427 private _evtDrop;
428 /**
429 * Attaches all DataTransferItems (obtained from
430 * clipboard or native drop events) to the cell
431 */
432 private _attachFiles;
433 /**
434 * Takes in a file object and adds it to
435 * the cell attachments
436 */
437 private _attachFile;
438 /**
439 * Generates a unique URI for a file
440 * while preserving the file extension.
441 */
442 private _generateURI;
443}
444/** ****************************************************************************
445 * MarkdownCell
446 ******************************************************************************/
447/**
448 * A widget for a Markdown cell.
449 *
450 * #### Notes
451 * Things get complicated if we want the rendered text to update
452 * any time the text changes, the text editor model changes,
453 * or the input area model changes. We don't support automatically
454 * updating the rendered text in all of these cases.
455 */
456export declare class MarkdownCell extends AttachmentsCell<IMarkdownCellModel> {
457 /**
458 * Construct a Markdown cell widget.
459 */
460 constructor(options: MarkdownCell.IOptions);
461 /**
462 * A promise that resolves when the widget renders for the first time.
463 */
464 get ready(): Promise<void>;
465 /**
466 * Text that represents the heading if cell is a heading.
467 * Returns empty string if not a heading.
468 */
469 get headingInfo(): {
470 text: string;
471 level: number;
472 };
473 get headingCollapsed(): boolean;
474 set headingCollapsed(value: boolean);
475 get numberChildNodes(): number;
476 set numberChildNodes(value: number);
477 get toggleCollapsedSignal(): Signal<this, boolean>;
478 /**
479 * Whether the cell is rendered.
480 */
481 get rendered(): boolean;
482 set rendered(value: boolean);
483 get showEditorForReadOnly(): boolean;
484 set showEditorForReadOnly(value: boolean);
485 protected maybeCreateCollapseButton(): void;
486 protected maybeCreateOrUpdateExpandButton(): void;
487 /**
488 * Render the collapse button for heading cells,
489 * and for collapsed heading cells render the "expand hidden cells"
490 * button.
491 */
492 protected renderCollapseButtons(widget: Widget): void;
493 /**
494 * Render an input instead of the text editor.
495 */
496 protected renderInput(widget: Widget): void;
497 /**
498 * Show the text editor instead of rendered input.
499 */
500 protected showEditor(): void;
501 protected onUpdateRequest(msg: Message): void;
502 /**
503 * Modify the cell source to include a reference to the attachment.
504 */
505 protected updateCellSourceWithAttachment(attachmentName: string, URI?: string): void;
506 /**
507 * Handle the rendered state.
508 */
509 private _handleRendered;
510 /**
511 * Update the rendered input.
512 */
513 private _updateRenderedInput;
514 /**
515 * Clone the cell, using the same model.
516 */
517 clone(): MarkdownCell;
518 private _monitor;
519 private _numberChildNodes;
520 private _headingCollapsed;
521 private _toggleCollapsedSignal;
522 private _renderer;
523 private _rendermime;
524 private _rendered;
525 private _prevText;
526 private _ready;
527 private _showEditorForReadOnlyMarkdown;
528}
529/**
530 * The namespace for the `CodeCell` class statics.
531 */
532export declare namespace MarkdownCell {
533 /**
534 * An options object for initializing a base cell widget.
535 */
536 interface IOptions extends Cell.IOptions<IMarkdownCellModel> {
537 /**
538 * The mime renderer for the cell widget.
539 */
540 rendermime: IRenderMimeRegistry;
541 /**
542 * Show editor for read-only Markdown cells.
543 */
544 showEditorForReadOnlyMarkdown?: boolean;
545 }
546 /**
547 * Default value for showEditorForReadOnlyMarkdown.
548 */
549 const defaultShowEditorForReadOnlyMarkdown = true;
550}
551/** ****************************************************************************
552 * RawCell
553 ******************************************************************************/
554/**
555 * A widget for a raw cell.
556 */
557export declare class RawCell extends Cell<IRawCellModel> {
558 /**
559 * Construct a raw cell widget.
560 */
561 constructor(options: RawCell.IOptions);
562 /**
563 * Clone the cell, using the same model.
564 */
565 clone(): RawCell;
566}
567/**
568 * The namespace for the `RawCell` class statics.
569 */
570export declare namespace RawCell {
571 /**
572 * An options object for initializing a base cell widget.
573 */
574 interface IOptions extends Cell.IOptions<IRawCellModel> {
575 }
576}