@jupyterlab/cells 4.0.0-alpha.1 → 4.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/widget.d.ts CHANGED
@@ -3,17 +3,16 @@ import { IChangedArgs } from '@jupyterlab/coreutils';
3
3
  import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
4
4
  import { IObservableJSON, IObservableMap } from '@jupyterlab/observables';
5
5
  import { IOutputPrompt, IStdin, OutputArea, Stdin } from '@jupyterlab/outputarea';
6
- import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
6
+ import { IRenderMime, IRenderMimeRegistry } from '@jupyterlab/rendermime';
7
7
  import { KernelMessage } from '@jupyterlab/services';
8
8
  import { ITranslator } from '@jupyterlab/translation';
9
9
  import { JSONObject, JSONValue, PartialJSONValue } from '@lumino/coreutils';
10
10
  import { Message } from '@lumino/messaging';
11
+ import { ISignal, Signal } from '@lumino/signaling';
11
12
  import { Widget } from '@lumino/widgets';
12
13
  import { ICellFooter, ICellHeader } from './headerfooter';
13
14
  import { IInputPrompt, InputArea } from './inputarea';
14
15
  import { IAttachmentsCellModel, ICellModel, ICodeCellModel, IMarkdownCellModel, IRawCellModel } from './model';
15
- import { Signal } from '@lumino/signaling';
16
- export declare const MARKDOWN_HEADING_COLLAPSED = "jp-MarkdownHeadingCollapsed";
17
16
  /** ****************************************************************************
18
17
  * Cell
19
18
  ******************************************************************************/
@@ -37,6 +36,10 @@ export declare class Cell<T extends ICellModel = ICellModel> extends Widget {
37
36
  * The content factory used by the widget.
38
37
  */
39
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>;
40
43
  /**
41
44
  * Get the prompt node used by the cell.
42
45
  */
@@ -62,6 +65,10 @@ export declare class Cell<T extends ICellModel = ICellModel> extends Widget {
62
65
  */
63
66
  get readOnly(): boolean;
64
67
  set readOnly(value: boolean);
68
+ /**
69
+ * Whether the cell is a placeholder that differ rendering
70
+ */
71
+ isPlaceholder(): boolean;
65
72
  /**
66
73
  * Save view editable state to model
67
74
  */
@@ -130,6 +137,10 @@ export declare class Cell<T extends ICellModel = ICellModel> extends Widget {
130
137
  * Handle `fit-request` messages.
131
138
  */
132
139
  protected onFitRequest(msg: Message): void;
140
+ /**
141
+ * Handle `resize` messages.
142
+ */
143
+ protected onResize(msg: Widget.ResizeMessage): void;
133
144
  /**
134
145
  * Handle `update-request` messages.
135
146
  */
@@ -139,14 +150,17 @@ export declare class Cell<T extends ICellModel = ICellModel> extends Widget {
139
150
  */
140
151
  protected onMetadataChanged(model: IObservableJSON, args: IObservableMap.IChangedArgs<PartialJSONValue | undefined>): void;
141
152
  protected translator: ITranslator;
153
+ protected _displayChanged: Signal<this, void>;
142
154
  private _readOnly;
143
155
  private _model;
144
156
  private _inputHidden;
145
157
  private _input;
146
158
  private _inputWrapper;
147
159
  private _inputPlaceholder;
160
+ private _isPlaceholder;
148
161
  private _syncCollapse;
149
162
  private _syncEditable;
163
+ private _resizeDebouncer;
150
164
  }
151
165
  /**
152
166
  * The namespace for the `Cell` class statics.
@@ -459,23 +473,42 @@ export declare class MarkdownCell extends AttachmentsCell<IMarkdownCellModel> {
459
473
  */
460
474
  get ready(): Promise<void>;
461
475
  /**
462
- * Text that represents the heading if cell is a heading.
476
+ * Text that represents the highest heading (i.e. lowest level) if cell is a heading.
463
477
  * Returns empty string if not a heading.
464
478
  */
465
479
  get headingInfo(): {
466
480
  text: string;
467
481
  level: number;
468
482
  };
483
+ /**
484
+ * Whether the heading is collapsed or not.
485
+ */
469
486
  get headingCollapsed(): boolean;
470
487
  set headingCollapsed(value: boolean);
488
+ /**
489
+ * Number of collapsed sub cells.
490
+ */
471
491
  get numberChildNodes(): number;
472
492
  set numberChildNodes(value: number);
473
- get toggleCollapsedSignal(): Signal<this, boolean>;
493
+ /**
494
+ * Signal emitted when the cell collapsed state changes.
495
+ */
496
+ get headingCollapsedChanged(): ISignal<MarkdownCell, boolean>;
474
497
  /**
475
498
  * Whether the cell is rendered.
476
499
  */
477
500
  get rendered(): boolean;
478
501
  set rendered(value: boolean);
502
+ /**
503
+ * Signal emitted when the markdown cell rendered state changes
504
+ */
505
+ get renderedChanged(): ISignal<MarkdownCell, boolean>;
506
+ get showEditorForReadOnly(): boolean;
507
+ set showEditorForReadOnly(value: boolean);
508
+ /**
509
+ * Renderer
510
+ */
511
+ get renderer(): IRenderMime.IRenderer;
479
512
  protected maybeCreateCollapseButton(): void;
480
513
  /**
481
514
  * Create, update or remove the hidden cells button.
@@ -516,12 +549,14 @@ export declare class MarkdownCell extends AttachmentsCell<IMarkdownCellModel> {
516
549
  private _monitor;
517
550
  private _numberChildNodes;
518
551
  private _headingCollapsed;
519
- private _toggleCollapsedSignal;
552
+ private _headingCollapsedChanged;
520
553
  private _renderer;
521
554
  private _rendermime;
522
555
  private _rendered;
556
+ private _renderedChanged;
523
557
  private _prevText;
524
558
  private _ready;
559
+ private _showEditorForReadOnlyMarkdown;
525
560
  }
526
561
  /**
527
562
  * The namespace for the `CodeCell` class statics.
@@ -535,7 +570,15 @@ export declare namespace MarkdownCell {
535
570
  * The mime renderer for the cell widget.
536
571
  */
537
572
  rendermime: IRenderMimeRegistry;
573
+ /**
574
+ * Show editor for read-only Markdown cells.
575
+ */
576
+ showEditorForReadOnlyMarkdown?: boolean;
538
577
  }
578
+ /**
579
+ * Default value for showEditorForReadOnlyMarkdown.
580
+ */
581
+ const defaultShowEditorForReadOnlyMarkdown = true;
539
582
  }
540
583
  /** ****************************************************************************
541
584
  * RawCell
package/lib/widget.js CHANGED
@@ -2,22 +2,23 @@
2
2
  | Copyright (c) Jupyter Development Team.
3
3
  | Distributed under the terms of the Modified BSD License.
4
4
  |----------------------------------------------------------------------------*/
5
- import marked from 'marked';
6
5
  import { AttachmentsResolver } from '@jupyterlab/attachments';
7
6
  import { ActivityMonitor, URLExt } from '@jupyterlab/coreutils';
8
7
  import { OutputArea, OutputPrompt, SimplifiedOutputArea, Stdin } from '@jupyterlab/outputarea';
9
8
  import { imageRendererFactory, MimeModel } from '@jupyterlab/rendermime';
9
+ import { TableOfContentsUtils } from '@jupyterlab/toc';
10
10
  import { nullTranslator } from '@jupyterlab/translation';
11
+ import { addIcon } from '@jupyterlab/ui-components';
11
12
  import { PromiseDelegate, UUID } from '@lumino/coreutils';
12
13
  import { filter, some, toArray } from '@lumino/algorithm';
14
+ import { Debouncer } from '@lumino/polling';
15
+ import { Signal } from '@lumino/signaling';
13
16
  import { Panel, PanelLayout, Widget } from '@lumino/widgets';
14
17
  import { InputCollapser, OutputCollapser } from './collapser';
15
18
  import { CellFooter, CellHeader } from './headerfooter';
16
19
  import { InputArea, InputPrompt } from './inputarea';
17
20
  import { InputPlaceholder, OutputPlaceholder } from './placeholder';
18
21
  import { ResizeHandle } from './resizeHandle';
19
- import { Signal } from '@lumino/signaling';
20
- import { addIcon } from '@jupyterlab/ui-components';
21
22
  /**
22
23
  * The CSS class added to cell widgets.
23
24
  */
@@ -74,7 +75,7 @@ const MARKDOWN_CELL_CLASS = 'jp-MarkdownCell';
74
75
  * The class name added to rendered markdown output widgets.
75
76
  */
76
77
  const MARKDOWN_OUTPUT_CLASS = 'jp-MarkdownOutput';
77
- export const MARKDOWN_HEADING_COLLAPSED = 'jp-MarkdownHeadingCollapsed';
78
+ const MARKDOWN_HEADING_COLLAPSED = 'jp-MarkdownHeadingCollapsed';
78
79
  const HEADING_COLLAPSER_CLASS = 'jp-collapseHeadingButton';
79
80
  const SHOW_HIDDEN_CELLS_CLASS = 'jp-showHiddenCellsButton';
80
81
  /**
@@ -109,11 +110,17 @@ export class Cell extends Widget {
109
110
  * Construct a new base cell widget.
110
111
  */
111
112
  constructor(options) {
113
+ var _a;
112
114
  super();
115
+ this._displayChanged = new Signal(this);
113
116
  this._readOnly = false;
114
117
  this._inputHidden = false;
118
+ this._isPlaceholder = false;
115
119
  this._syncCollapse = false;
116
120
  this._syncEditable = false;
121
+ this._resizeDebouncer = new Debouncer(() => {
122
+ this._displayChanged.emit();
123
+ }, 0);
117
124
  this.addClass(CELL_CLASS);
118
125
  const model = (this._model = options.model);
119
126
  const contentFactory = (this.contentFactory =
@@ -147,14 +154,10 @@ export class Cell extends Widget {
147
154
  const footer = this.contentFactory.createCellFooter();
148
155
  footer.addClass(CELL_FOOTER_CLASS);
149
156
  this.layout.addWidget(footer);
157
+ this._isPlaceholder = (_a = options.placeholder) !== null && _a !== void 0 ? _a : false;
150
158
  // Editor settings
151
159
  if (options.editorConfig) {
152
- let editorOptions = {};
153
- Object.keys(options.editorConfig).forEach((key) => {
154
- var _a, _b;
155
- editorOptions[key] = (_b = (_a = options.editorConfig) === null || _a === void 0 ? void 0 : _a[key]) !== null && _b !== void 0 ? _b : null;
156
- });
157
- this.editor.setOptions(editorOptions);
160
+ this.editor.setOptions(Object.assign({}, options.editorConfig));
158
161
  }
159
162
  model.metadata.changed.connect(this.onMetadataChanged, this);
160
163
  }
@@ -170,6 +173,12 @@ export class Cell extends Widget {
170
173
  this.loadEditableState();
171
174
  return this;
172
175
  }
176
+ /**
177
+ * Signal to indicate that widget has changed visibly (in size, in type, etc)
178
+ */
179
+ get displayChanged() {
180
+ return this._displayChanged;
181
+ }
173
182
  /**
174
183
  * Get the prompt node used by the cell.
175
184
  */
@@ -222,6 +231,12 @@ export class Cell extends Widget {
222
231
  }
223
232
  this.update();
224
233
  }
234
+ /**
235
+ * Whether the cell is a placeholder that differ rendering
236
+ */
237
+ isPlaceholder() {
238
+ return this._isPlaceholder;
239
+ }
225
240
  /**
226
241
  * Save view editable state to model
227
242
  */
@@ -397,6 +412,12 @@ export class Cell extends Widget {
397
412
  // need this for for when a theme changes font size
398
413
  this.editor.refresh();
399
414
  }
415
+ /**
416
+ * Handle `resize` messages.
417
+ */
418
+ onResize(msg) {
419
+ void this._resizeDebouncer.invoke();
420
+ }
400
421
  /**
401
422
  * Handle `update-request` messages.
402
423
  */
@@ -1119,13 +1140,14 @@ export class MarkdownCell extends AttachmentsCell {
1119
1140
  * Construct a Markdown cell widget.
1120
1141
  */
1121
1142
  constructor(options) {
1122
- var _a, _b;
1143
+ var _a, _b, _c;
1123
1144
  super(options);
1124
- this._toggleCollapsedSignal = new Signal(this);
1125
- this._renderer = null;
1145
+ this._headingCollapsedChanged = new Signal(this);
1126
1146
  this._rendered = true;
1147
+ this._renderedChanged = new Signal(this);
1127
1148
  this._prevText = '';
1128
1149
  this._ready = new PromiseDelegate();
1150
+ this._showEditorForReadOnlyMarkdown = true;
1129
1151
  this.addClass(MARKDOWN_CELL_CLASS);
1130
1152
  const trans = this.translator.load('jupyterlab');
1131
1153
  this.node.setAttribute('aria-label', trans.__('Markdown Cell Content'));
@@ -1136,6 +1158,8 @@ export class MarkdownCell extends AttachmentsCell {
1136
1158
  model: this.model.attachments
1137
1159
  })
1138
1160
  });
1161
+ this._renderer = this._rendermime.createRenderer('text/markdown');
1162
+ this._renderer.addClass(MARKDOWN_OUTPUT_CLASS);
1139
1163
  // Stop codemirror handling paste
1140
1164
  this.editor.setOption('handlePaste', false);
1141
1165
  // Check if heading cell is set to be collapsed
@@ -1155,6 +1179,8 @@ export class MarkdownCell extends AttachmentsCell {
1155
1179
  });
1156
1180
  this.renderCollapseButtons(this._renderer);
1157
1181
  this.renderInput(this._renderer);
1182
+ this._showEditorForReadOnlyMarkdown =
1183
+ (_c = options.showEditorForReadOnlyMarkdown) !== null && _c !== void 0 ? _c : MarkdownCell.defaultShowEditorForReadOnlyMarkdown;
1158
1184
  }
1159
1185
  /**
1160
1186
  * A promise that resolves when the widget renders for the first time.
@@ -1163,45 +1189,55 @@ export class MarkdownCell extends AttachmentsCell {
1163
1189
  return this._ready.promise;
1164
1190
  }
1165
1191
  /**
1166
- * Text that represents the heading if cell is a heading.
1192
+ * Text that represents the highest heading (i.e. lowest level) if cell is a heading.
1167
1193
  * Returns empty string if not a heading.
1168
1194
  */
1169
1195
  get headingInfo() {
1170
- let text = this.model.value.text;
1171
- const lines = marked.lexer(text);
1172
- let line;
1173
- for (line of lines) {
1174
- if (line.type === 'heading') {
1175
- return { text: line.text, level: line.depth };
1176
- }
1177
- else if (line.type === 'html') {
1178
- let match = line.raw.match(/<h([1-6])(.*?)>(.*?)<\/h\1>/);
1179
- if (match === null || match === void 0 ? void 0 : match[3]) {
1180
- return { text: match[3], level: parseInt(match[1]) };
1181
- }
1182
- return { text: '', level: -1 };
1183
- }
1196
+ // Use table of content algorithm for consistency
1197
+ const headings = TableOfContentsUtils.Markdown.getHeadings(this.model.value.text, {
1198
+ maximalDepth: 6,
1199
+ numberHeaders: false
1200
+ });
1201
+ if (headings.length > 0) {
1202
+ // Return the highest level
1203
+ const { text, level } = headings.reduce((prev, curr) => (prev.level <= curr.level ? prev : curr), headings[0]);
1204
+ return { text, level };
1205
+ }
1206
+ else {
1207
+ return { text: '', level: -1 };
1184
1208
  }
1185
- return { text: '', level: -1 };
1186
1209
  }
1210
+ /**
1211
+ * Whether the heading is collapsed or not.
1212
+ */
1187
1213
  get headingCollapsed() {
1188
1214
  return this._headingCollapsed;
1189
1215
  }
1190
1216
  set headingCollapsed(value) {
1191
- this._headingCollapsed = value;
1192
- if (value) {
1193
- this.model.metadata.set(MARKDOWN_HEADING_COLLAPSED, value);
1194
- }
1195
- else if (this.model.metadata.has(MARKDOWN_HEADING_COLLAPSED)) {
1196
- this.model.metadata.delete(MARKDOWN_HEADING_COLLAPSED);
1197
- }
1198
- const collapseButton = this.inputArea.promptNode.getElementsByClassName(HEADING_COLLAPSER_CLASS)[0];
1199
- if (collapseButton) {
1200
- collapseButton.setAttribute('style', `background:
1201
- ${value ? 'var(--jp-icon-caret-right)' : 'var(--jp-icon-caret-down)'} no-repeat center`);
1217
+ if (this._headingCollapsed !== value) {
1218
+ this._headingCollapsed = value;
1219
+ if (value) {
1220
+ this.model.metadata.set(MARKDOWN_HEADING_COLLAPSED, value);
1221
+ }
1222
+ else if (this.model.metadata.has(MARKDOWN_HEADING_COLLAPSED)) {
1223
+ this.model.metadata.delete(MARKDOWN_HEADING_COLLAPSED);
1224
+ }
1225
+ const collapseButton = this.inputArea.promptNode.getElementsByClassName(HEADING_COLLAPSER_CLASS)[0];
1226
+ if (collapseButton) {
1227
+ if (value) {
1228
+ collapseButton.classList.add('jp-mod-collapsed');
1229
+ }
1230
+ else {
1231
+ collapseButton.classList.remove('jp-mod-collapsed');
1232
+ }
1233
+ }
1234
+ this.renderCollapseButtons(this._renderer);
1235
+ this._headingCollapsedChanged.emit(this._headingCollapsed);
1202
1236
  }
1203
- this.renderCollapseButtons(this._renderer);
1204
1237
  }
1238
+ /**
1239
+ * Number of collapsed sub cells.
1240
+ */
1205
1241
  get numberChildNodes() {
1206
1242
  return this._numberChildNodes;
1207
1243
  }
@@ -1209,8 +1245,11 @@ export class MarkdownCell extends AttachmentsCell {
1209
1245
  this._numberChildNodes = value;
1210
1246
  this.renderCollapseButtons(this._renderer);
1211
1247
  }
1212
- get toggleCollapsedSignal() {
1213
- return this._toggleCollapsedSignal;
1248
+ /**
1249
+ * Signal emitted when the cell collapsed state changes.
1250
+ */
1251
+ get headingCollapsedChanged() {
1252
+ return this._headingCollapsedChanged;
1214
1253
  }
1215
1254
  /**
1216
1255
  * Whether the cell is rendered.
@@ -1219,6 +1258,10 @@ export class MarkdownCell extends AttachmentsCell {
1219
1258
  return this._rendered;
1220
1259
  }
1221
1260
  set rendered(value) {
1261
+ // Show cell as rendered when cell is not editable
1262
+ if (this.readOnly && this._showEditorForReadOnlyMarkdown === false) {
1263
+ value = true;
1264
+ }
1222
1265
  if (value === this._rendered) {
1223
1266
  return;
1224
1267
  }
@@ -1230,19 +1273,49 @@ export class MarkdownCell extends AttachmentsCell {
1230
1273
  if (!this._rendered) {
1231
1274
  this.editor.refresh();
1232
1275
  }
1276
+ // If the rendered state changed, raise an event.
1277
+ this._displayChanged.emit();
1278
+ this._renderedChanged.emit(this._rendered);
1279
+ }
1280
+ /**
1281
+ * Signal emitted when the markdown cell rendered state changes
1282
+ */
1283
+ get renderedChanged() {
1284
+ return this._renderedChanged;
1285
+ }
1286
+ /*
1287
+ * Whether the Markdown editor is visible in read-only mode.
1288
+ */
1289
+ get showEditorForReadOnly() {
1290
+ return this._showEditorForReadOnlyMarkdown;
1291
+ }
1292
+ set showEditorForReadOnly(value) {
1293
+ this._showEditorForReadOnlyMarkdown = value;
1294
+ if (value === false) {
1295
+ this.rendered = true;
1296
+ }
1297
+ }
1298
+ /**
1299
+ * Renderer
1300
+ */
1301
+ get renderer() {
1302
+ return this._renderer;
1233
1303
  }
1234
1304
  maybeCreateCollapseButton() {
1235
1305
  if (this.headingInfo.level > 0 &&
1236
1306
  this.inputArea.promptNode.getElementsByClassName(HEADING_COLLAPSER_CLASS)
1237
1307
  .length == 0) {
1238
1308
  let collapseButton = this.inputArea.promptNode.appendChild(document.createElement('button'));
1239
- collapseButton.className = `jp-Button jp-mod-minimal ${HEADING_COLLAPSER_CLASS}`;
1240
- collapseButton.style.background = `${this._headingCollapsed
1241
- ? 'var(--jp-icon-caret-right)'
1242
- : 'var(--jp-icon-caret-down)'} no-repeat center`;
1309
+ collapseButton.className = `jp-Button ${HEADING_COLLAPSER_CLASS}`;
1310
+ collapseButton.setAttribute('data-heading-level', this.headingInfo.level.toString());
1311
+ if (this._headingCollapsed) {
1312
+ collapseButton.classList.add('jp-mod-collapsed');
1313
+ }
1314
+ else {
1315
+ collapseButton.classList.remove('jp-mod-collapsed');
1316
+ }
1243
1317
  collapseButton.onclick = (event) => {
1244
1318
  this.headingCollapsed = !this.headingCollapsed;
1245
- this._toggleCollapsedSignal.emit(this._headingCollapsed);
1246
1319
  };
1247
1320
  }
1248
1321
  }
@@ -1266,7 +1339,6 @@ export class MarkdownCell extends AttachmentsCell {
1266
1339
  newShowHiddenCellsButton.appendChild(buttonTextElement);
1267
1340
  newShowHiddenCellsButton.onclick = () => {
1268
1341
  this.headingCollapsed = false;
1269
- this._toggleCollapsedSignal.emit(this._headingCollapsed);
1270
1342
  };
1271
1343
  this.node.appendChild(newShowHiddenCellsButton);
1272
1344
  }
@@ -1307,6 +1379,15 @@ export class MarkdownCell extends AttachmentsCell {
1307
1379
  showEditor() {
1308
1380
  this.removeClass(RENDERED_CLASS);
1309
1381
  this.inputArea.showEditor();
1382
+ // if this is going to be a heading, place the cursor accordingly
1383
+ let numHashAtStart = (this.model.value.text.match(/^#+/g) || [''])[0]
1384
+ .length;
1385
+ if (numHashAtStart > 0) {
1386
+ this.inputArea.editor.setCursorPosition({
1387
+ column: numHashAtStart + 1,
1388
+ line: 0
1389
+ });
1390
+ }
1310
1391
  }
1311
1392
  /*
1312
1393
  * Handle `update-request` messages.
@@ -1347,14 +1428,10 @@ export class MarkdownCell extends AttachmentsCell {
1347
1428
  // Do not re-render if the text has not changed.
1348
1429
  if (text !== this._prevText) {
1349
1430
  const mimeModel = new MimeModel({ data: { 'text/markdown': text } });
1350
- if (!this._renderer) {
1351
- this._renderer = this._rendermime.createRenderer('text/markdown');
1352
- this._renderer.addClass(MARKDOWN_OUTPUT_CLASS);
1353
- }
1354
1431
  this._prevText = text;
1355
1432
  return this._renderer.renderModel(mimeModel);
1356
1433
  }
1357
- return Promise.resolve(void 0);
1434
+ return Promise.resolve();
1358
1435
  }
1359
1436
  /**
1360
1437
  * Clone the cell, using the same model.
@@ -1370,6 +1447,15 @@ export class MarkdownCell extends AttachmentsCell {
1370
1447
  });
1371
1448
  }
1372
1449
  }
1450
+ /**
1451
+ * The namespace for the `CodeCell` class statics.
1452
+ */
1453
+ (function (MarkdownCell) {
1454
+ /**
1455
+ * Default value for showEditorForReadOnlyMarkdown.
1456
+ */
1457
+ MarkdownCell.defaultShowEditorForReadOnlyMarkdown = true;
1458
+ })(MarkdownCell || (MarkdownCell = {}));
1373
1459
  /** ****************************************************************************
1374
1460
  * RawCell
1375
1461
  ******************************************************************************/