notebookBrowser.ts 15.8 KB
Newer Older
R
rebornix 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
R
rebornix 已提交
7 8
import { IListEvent, IListMouseEvent } from 'vs/base/browser/ui/list/list';
import { IListOptions, IListStyles } from 'vs/base/browser/ui/list/listWidget';
9
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
10
import { ToolBar } from 'vs/base/browser/ui/toolbar/toolbar';
11
import { CancellationTokenSource } from 'vs/base/common/cancellation';
R
rebornix 已提交
12 13 14
import { Event } from 'vs/base/common/event';
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { ScrollEvent } from 'vs/base/common/scrollable';
R
rebornix 已提交
15
import { URI } from 'vs/base/common/uri';
16 17 18 19
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
import { Range } from 'vs/editor/common/core/range';
import { FindMatch } from 'vs/editor/common/model';
20
import { RawContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
21
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
22
import { CellViewModel, IModelDecorationsChangeAccessor, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
R
rebornix 已提交
23
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
R
rebornix 已提交
24
import { CellKind, IOutput, IRenderOutput, NotebookCellMetadata, NotebookDocumentMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
25
import { Webview } from 'vs/workbench/contrib/webview/browser/webview';
26
import { CellLanguageStatusBarItem } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellRenderer';
R
rebornix 已提交
27 28

export const KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED = new RawContextKey<boolean>('notebookFindWidgetFocused', false);
R
rebornix 已提交
29

30 31 32
// Is Notebook
export const NOTEBOOK_IS_ACTIVE_EDITOR = ContextKeyExpr.equals('activeEditor', 'workbench.editor.notebook');

33
// Editor keys
34
export const NOTEBOOK_EDITOR_FOCUSED = new RawContextKey<boolean>('notebookEditorFocused', false);
35 36 37
export const NOTEBOOK_EDITOR_EDITABLE = new RawContextKey<boolean>('notebookEditable', true);
export const NOTEBOOK_EDITOR_RUNNABLE = new RawContextKey<boolean>('notebookRunnable', true);
export const NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK = new RawContextKey<boolean>('notebookExecuting', false);
38

39 40 41 42 43 44 45
// Cell keys
export const NOTEBOOK_VIEW_TYPE = new RawContextKey<string>('notebookViewType', undefined);
export const NOTEBOOK_CELL_TYPE = new RawContextKey<string>('notebookCellType', undefined); // code, markdown
export const NOTEBOOK_CELL_EDITABLE = new RawContextKey<boolean>('notebookCellEditable', false); // bool
export const NOTEBOOK_CELL_RUNNABLE = new RawContextKey<boolean>('notebookCellRunnable', false); // bool
export const NOTEBOOK_CELL_MARKDOWN_EDIT_MODE = new RawContextKey<boolean>('notebookCellMarkdownEditMode', false); // bool
export const NOTEBOOK_CELL_RUN_STATE = new RawContextKey<string>('notebookCellRunState', undefined); // idle, running
R
rebornix 已提交
46
export const NOTEBOOK_CELL_HAS_OUTPUTS = new RawContextKey<boolean>('notebookCellHasOutputs', false); // bool
47

R
rebornix 已提交
48 49 50 51 52 53
export interface NotebookLayoutInfo {
	width: number;
	height: number;
	fontInfo: BareFontInfo;
}

R
rebornix 已提交
54 55 56 57 58 59 60 61 62 63 64
export interface NotebookLayoutChangeEvent {
	width?: boolean;
	height?: boolean;
	fontInfo?: boolean;
}

export interface CodeCellLayoutInfo {
	readonly fontInfo: BareFontInfo | null;
	readonly editorHeight: number;
	readonly editorWidth: number;
	readonly totalHeight: number;
65
	readonly outputContainerOffset: number;
R
rebornix 已提交
66 67
	readonly outputTotalHeight: number;
	readonly indicatorHeight: number;
R
rebornix 已提交
68
	readonly bottomToolbarOffset: number;
R
rebornix 已提交
69 70 71 72 73 74
}

export interface CodeCellLayoutChangeEvent {
	editorHeight?: boolean;
	outputHeight?: boolean;
	totalHeight?: boolean;
R
rebornix 已提交
75 76
	outerWidth?: number;
	font?: BareFontInfo;
R
rebornix 已提交
77 78 79 80 81
}

export interface MarkdownCellLayoutInfo {
	readonly fontInfo: BareFontInfo | null;
	readonly editorWidth: number;
R
rebornix 已提交
82
	readonly bottomToolbarOffset: number;
R
rebornix 已提交
83
	readonly totalHeight: number;
R
rebornix 已提交
84 85 86
}

export interface MarkdownCellLayoutChangeEvent {
R
rebornix 已提交
87 88
	font?: BareFontInfo;
	outerWidth?: number;
R
rebornix 已提交
89
	totalHeight?: number;
R
rebornix 已提交
90 91
}

R
rebornix 已提交
92
export interface ICellViewModel {
R
rebornix 已提交
93
	readonly model: NotebookCellTextModel;
R
rebornix 已提交
94
	readonly id: string;
95
	dragging: boolean;
R
rebornix 已提交
96 97 98
	handle: number;
	uri: URI;
	cellKind: CellKind;
99
	editState: CellEditState;
100 101
	readonly runState: CellRunState;
	currentTokenSource: CancellationTokenSource | undefined;
R
rebornix 已提交
102 103
	focusMode: CellFocusMode;
	getText(): string;
104
	metadata: NotebookCellMetadata | undefined;
R
rebornix 已提交
105
	getEvaluatedMetadata(documentMetadata: NotebookDocumentMetadata | undefined): NotebookCellMetadata;
R
rebornix 已提交
106 107
}

R
rebornix 已提交
108 109 110 111 112
export interface INotebookEditorMouseEvent {
	readonly event: MouseEvent;
	readonly target: CellViewModel;
}

R
rebornix 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
export interface INotebookEditorContribution {
	/**
	 * Dispose this contribution.
	 */
	dispose(): void;
	/**
	 * Store view state.
	 */
	saveViewState?(): any;
	/**
	 * Restore view state.
	 */
	restoreViewState?(state: any): void;
}

128
export interface INotebookEditor {
R
rebornix 已提交
129 130 131 132

	/**
	 * Notebook view model attached to the current editor
	 */
R
rebornix 已提交
133
	viewModel: NotebookViewModel | undefined;
R
rebornix 已提交
134

R
rebornix 已提交
135 136 137 138 139
	/**
	 * An event emitted when the model of this editor has changed.
	 * @event
	 */
	readonly onDidChangeModel: Event<void>;
140 141
	isNotebookEditor: boolean;

R
rebornix 已提交
142
	getDomNode(): HTMLElement;
R
rebornix 已提交
143 144
	getInnerWebview(): Webview | undefined;

R
rebornix 已提交
145 146 147
	/**
	 * Focus the notebook editor cell list
	 */
R
rebornix 已提交
148
	focus(): void;
R
rebornix 已提交
149

R
rebornix 已提交
150 151 152
	/**
	 * Select & focus cell
	 */
R
rebornix 已提交
153
	selectElement(cell: ICellViewModel): void;
R
rebornix 已提交
154

R
rebornix 已提交
155 156 157
	/**
	 * Layout info for the notebook editor
	 */
R
rebornix 已提交
158
	getLayoutInfo(): NotebookLayoutInfo;
R
rebornix 已提交
159 160 161
	/**
	 * Fetch the output renderers for notebook outputs.
	 */
R
rebornix 已提交
162
	getOutputRenderer(): OutputRenderer;
R
rebornix 已提交
163 164 165 166

	/**
	 * Insert a new cell around `cell`
	 */
167
	insertNotebookCell(cell: ICellViewModel | undefined, type: CellKind, direction?: 'above' | 'below', initialText?: string): CellViewModel | null;
R
rebornix 已提交
168 169 170 171

	/**
	 * Delete a cell from the notebook
	 */
172
	deleteNotebookCell(cell: ICellViewModel): Promise<boolean>;
R
rebornix 已提交
173

174 175 176
	/**
	 * Move a cell up one spot
	 */
177
	moveCellUp(cell: ICellViewModel): Promise<boolean>;
178 179 180 181

	/**
	 * Move a cell down one spot
	 */
182
	moveCellDown(cell: ICellViewModel): Promise<boolean>;
183

R
Rob Lourens 已提交
184 185 186
	/**
	 * Move a cell above or below another cell
	 */
187
	moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<boolean>;
R
Rob Lourens 已提交
188

R
rebornix 已提交
189 190 191 192 193 194
	/**
	 * Switch the cell into editing mode.
	 *
	 * For code cell, the monaco editor will be focused.
	 * For markdown cell, it will switch from preview mode to editing mode, which focuses the monaco editor.
	 */
R
rebornix 已提交
195
	editNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
196 197 198 199

	/**
	 * Quit cell editing mode.
	 */
R
rebornix 已提交
200
	saveNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
201 202 203 204

	/**
	 * Focus the container of a cell (the monaco editor inside is not focused).
	 */
R
rebornix 已提交
205
	focusNotebookCell(cell: ICellViewModel, focusEditor: boolean): void;
R
rebornix 已提交
206

207 208 209 210 211
	/**
	 * Execute the given notebook cell
	 */
	executeNotebookCell(cell: ICellViewModel): Promise<void>;

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	/**
	 * Cancel the cell execution
	 */
	cancelNotebookCellExecution(cell: ICellViewModel): void;

	/**
	 * Executes all notebook cells in order
	 */
	executeNotebook(): Promise<void>;

	/**
	 * Cancel the notebook execution
	 */
	cancelNotebookExecution(): void;

R
rebornix 已提交
227 228 229
	/**
	 * Get current active cell
	 */
R
rebornix 已提交
230
	getActiveCell(): ICellViewModel | undefined;
R
rebornix 已提交
231 232 233 234

	/**
	 * Layout the cell with a new height
	 */
235
	layoutNotebookCell(cell: ICellViewModel, height: number): Promise<void>;
R
rebornix 已提交
236 237 238 239

	/**
	 * Render the output in webview layer
	 */
R
rebornix 已提交
240
	createInset(cell: ICellViewModel, output: IOutput, shadowContent: string, offset: number): void;
R
rebornix 已提交
241 242 243 244

	/**
	 * Remove the output from the webview layer
	 */
R
rebornix 已提交
245
	removeInset(output: IOutput): void;
R
rebornix 已提交
246

247 248 249 250 251
	/**
	 * Send message to the webview for outputs.
	 */
	postMessage(message: any): void;

R
rebornix 已提交
252 253 254
	/**
	 * Trigger the editor to scroll from scroll event programmatically
	 */
255
	triggerScroll(event: IMouseWheelEvent): void;
R
rebornix 已提交
256 257 258 259

	/**
	 * Reveal cell into viewport.
	 */
R
rebornix 已提交
260
	revealInView(cell: ICellViewModel): void;
R
rebornix 已提交
261 262 263 264

	/**
	 * Reveal cell into viewport center.
	 */
R
rebornix 已提交
265
	revealInCenter(cell: ICellViewModel): void;
R
rebornix 已提交
266 267 268 269

	/**
	 * Reveal cell into viewport center if cell is currently out of the viewport.
	 */
R
rebornix 已提交
270
	revealInCenterIfOutsideViewport(cell: ICellViewModel): void;
R
rebornix 已提交
271 272 273 274

	/**
	 * Reveal a line in notebook cell into viewport with minimal scrolling.
	 */
R
rebornix 已提交
275
	revealLineInView(cell: ICellViewModel, line: number): void;
R
rebornix 已提交
276

R
rebornix 已提交
277 278 279
	/**
	 * Reveal a line in notebook cell into viewport center.
	 */
R
rebornix 已提交
280
	revealLineInCenter(cell: ICellViewModel, line: number): void;
R
rebornix 已提交
281 282 283 284

	/**
	 * Reveal a line in notebook cell into viewport center.
	 */
R
rebornix 已提交
285
	revealLineInCenterIfOutsideViewport(cell: ICellViewModel, line: number): void;
R
rebornix 已提交
286

R
rebornix 已提交
287 288 289
	/**
	 * Reveal a range in notebook cell into viewport with minimal scrolling.
	 */
R
rebornix 已提交
290
	revealRangeInView(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
291 292 293 294

	/**
	 * Reveal a range in notebook cell into viewport center.
	 */
R
rebornix 已提交
295
	revealRangeInCenter(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
296 297 298 299

	/**
	 * Reveal a range in notebook cell into viewport center.
	 */
R
rebornix 已提交
300
	revealRangeInCenterIfOutsideViewport(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
301

R
rebornix 已提交
302 303 304 305 306
	/**
	 * Set hidden areas on cell text models.
	 */
	setHiddenAreas(_ranges: ICellRange[]): boolean;

R
rebornix 已提交
307
	setCellSelection(cell: ICellViewModel, selection: Range): void;
R
rebornix 已提交
308

R
rebornix 已提交
309 310 311 312
	/**
	 * Change the decorations on cells.
	 * The notebook is virtualized and this method should be called to create/delete editor decorations safely.
	 */
R
rebornix 已提交
313
	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any;
R
rebornix 已提交
314

R
rebornix 已提交
315 316 317 318 319 320 321 322 323 324 325
	/**
	 * An event emitted on a "mouseup".
	 * @event
	 */
	onMouseUp(listener: (e: INotebookEditorMouseEvent) => void): IDisposable;

	/**
	 * An event emitted on a "mousedown".
	 * @event
	 */
	onMouseDown(listener: (e: INotebookEditorMouseEvent) => void): IDisposable;
R
rebornix 已提交
326 327 328 329 330 331 332

	/**
	 * Get a contribution of this editor.
	 * @id Unique identifier of the contribution.
	 * @return The contribution or null if contribution not found.
	 */
	getContribution<T extends INotebookEditorContribution>(id: string): T;
R
rebornix 已提交
333 334
}

R
rebornix 已提交
335 336 337 338 339 340 341 342 343 344
export interface INotebookCellList {
	onWillScroll: Event<ScrollEvent>;
	onDidChangeFocus: Event<IListEvent<ICellViewModel>>;
	onDidChangeContentHeight: Event<number>;
	scrollTop: number;
	scrollHeight: number;
	scrollLeft: number;
	length: number;
	rowsContainer: HTMLElement;
	readonly onDidRemoveOutput: Event<IOutput>;
345
	readonly onDidHideOutput: Event<IOutput>;
R
rebornix 已提交
346 347
	readonly onMouseUp: Event<IListMouseEvent<CellViewModel>>;
	readonly onMouseDown: Event<IListMouseEvent<CellViewModel>>;
R
rebornix 已提交
348 349 350
	detachViewModel(): void;
	attachViewModel(viewModel: NotebookViewModel): void;
	clear(): void;
351
	getViewIndex(cell: ICellViewModel): number | undefined;
R
rebornix 已提交
352 353 354 355 356 357 358 359 360 361 362 363
	focusElement(element: ICellViewModel): void;
	selectElement(element: ICellViewModel): void;
	getFocusedElements(): ICellViewModel[];
	revealElementInView(element: ICellViewModel): void;
	revealElementInCenterIfOutsideViewport(element: ICellViewModel): void;
	revealElementInCenter(element: ICellViewModel): void;
	revealElementLineInView(element: ICellViewModel, line: number): void;
	revealElementLineInCenter(element: ICellViewModel, line: number): void;
	revealElementLineInCenterIfOutsideViewport(element: ICellViewModel, line: number): void;
	revealElementRangeInView(element: ICellViewModel, range: Range): void;
	revealElementRangeInCenter(element: ICellViewModel, range: Range): void;
	revealElementRangeInCenterIfOutsideViewport(element: ICellViewModel, range: Range): void;
R
rebornix 已提交
364
	setHiddenAreas(_ranges: ICellRange[], triggerViewUpdate: boolean): boolean;
R
rebornix 已提交
365 366 367 368 369 370 371 372 373 374 375
	domElementOfElement(element: ICellViewModel): HTMLElement | null;
	focusView(): void;
	getAbsoluteTopOfElement(element: ICellViewModel): number;
	triggerScrollFromMouseWheelEvent(browserEvent: IMouseWheelEvent): void;
	updateElementHeight2(element: ICellViewModel, size: number): void;
	domFocus(): void;
	setCellSelection(element: ICellViewModel, range: Range): void;
	style(styles: IListStyles): void;
	updateOptions(options: IListOptions<ICellViewModel>): void;
	layout(height?: number, width?: number): void;
	dispose(): void;
376 377 378 379 380

	// TODO resolve differences between List<CellViewModel> and INotebookCellList<ICellViewModel>
	getFocus(): number[];
	setFocus(indexes: number[]): void;
	setSelection(indexes: number[]): void;
R
rebornix 已提交
381 382
}

383
export interface BaseCellRenderTemplate {
R
rebornix 已提交
384 385
	container: HTMLElement;
	cellContainer: HTMLElement;
386
	toolbar: ToolBar;
387
	focusIndicator: HTMLElement;
R
Rob Lourens 已提交
388
	insertionIndicatorTop: HTMLElement;
389
	disposables: DisposableStore;
390
	elementDisposables: DisposableStore;
R
rebornix 已提交
391
	bottomCellContainer: HTMLElement;
R
Rob Lourens 已提交
392
	currentRenderedCell?: ICellViewModel;
393 394
	statusBarContainer: HTMLElement;
	languageStatusBarItem: CellLanguageStatusBarItem;
R
rebornix 已提交
395
	toJSON: () => any;
396 397 398
}

export interface MarkdownCellRenderTemplate extends BaseCellRenderTemplate {
399 400
	editorPart: HTMLElement;
	editorContainer: HTMLElement;
R
rebornix 已提交
401
	foldingIndicator: HTMLElement;
402 403 404
}

export interface CodeCellRenderTemplate extends BaseCellRenderTemplate {
R
Rob Lourens 已提交
405 406
	cellRunStatusContainer: HTMLElement;
	cellStatusMessageContainer: HTMLElement;
407 408 409 410 411 412
	runToolbar: ToolBar;
	runButtonContainer: HTMLElement;
	executionOrderLabel: HTMLElement;
	outputContainer: HTMLElement;
	editor: CodeEditorWidget;
	progressBar: ProgressBar;
R
rebornix 已提交
413
}
R
rebornix 已提交
414 415 416 417 418 419 420 421 422

export interface IOutputTransformContribution {
	/**
	 * Dispose this contribution.
	 */
	dispose(): void;

	render(output: IOutput, container: HTMLElement, preferredMimeType: string | undefined): IRenderOutput;
}
R
rebornix 已提交
423 424 425 426 427 428

export interface CellFindMatch {
	cell: CellViewModel;
	matches: FindMatch[];
}

R
rebornix 已提交
429 430 431 432 433 434 435 436 437
export enum CellRevealType {
	Line,
	Range
}

export enum CellRevealPosition {
	Top,
	Center
}
R
rebornix 已提交
438

439 440 441 442 443 444
export enum CellRunState {
	Idle,
	Running
}

export enum CellEditState {
R
rebornix 已提交
445 446 447 448 449
	/**
	 * Default state.
	 * For markdown cell, it's Markdown preview.
	 * For code cell, the browser focus should be on the container instead of the editor
	 */
450
	Preview,
R
rebornix 已提交
451 452 453 454 455 456 457


	/**
	 * Eding mode. Source for markdown or code is rendered in editors and the state will be persistent.
	 */
	Editing
}
R
rebornix 已提交
458 459 460 461 462 463 464 465 466 467 468 469

export enum CellFocusMode {
	Container,
	Editor
}

export enum CursorAtBoundary {
	None,
	Top,
	Bottom,
	Both
}
R
rebornix 已提交
470

471 472 473 474 475 476 477
export interface CellViewModelStateChangeEvent {
	metadataChanged?: boolean;
	selectionChanged?: boolean;
	focusModeChanged?: boolean;
	runStateChanged?: boolean;
	editStateChanged?: boolean;
	languageChanged?: boolean;
R
rebornix 已提交
478 479
	foldingStateChanged?: boolean;
	contentChanged?: boolean;
480
	outputIsHoveredChanged?: boolean;
481 482
}

R
rebornix 已提交
483
/**
R
rebornix 已提交
484
 * [start, end]
R
rebornix 已提交
485 486
 */
export interface ICellRange {
R
rebornix 已提交
487 488 489
	/**
	 * zero based index
	 */
R
rebornix 已提交
490
	start: number;
R
rebornix 已提交
491

R
rebornix 已提交
492
	/**
R
rebornix 已提交
493
	 * zero based index
R
rebornix 已提交
494
	 */
R
rebornix 已提交
495
	end: number;
R
rebornix 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509
}


/**
 * @param _ranges
 */
export function reduceCellRanges(_ranges: ICellRange[]): ICellRange[] {
	if (!_ranges.length) {
		return [];
	}

	let ranges = _ranges.sort((a, b) => a.start - b.start);
	let result: ICellRange[] = [];
	let currentRangeStart = ranges[0].start;
R
rebornix 已提交
510
	let currentRangeEnd = ranges[0].end + 1;
R
rebornix 已提交
511 512 513 514 515

	for (let i = 0, len = ranges.length; i < len; i++) {
		let range = ranges[i];

		if (range.start > currentRangeEnd) {
R
rebornix 已提交
516
			result.push({ start: currentRangeStart, end: currentRangeEnd - 1 });
R
rebornix 已提交
517
			currentRangeStart = range.start;
R
rebornix 已提交
518 519 520
			currentRangeEnd = range.end + 1;
		} else if (range.end + 1 > currentRangeEnd) {
			currentRangeEnd = range.end + 1;
R
rebornix 已提交
521 522 523
		}
	}

R
rebornix 已提交
524
	result.push({ start: currentRangeStart, end: currentRangeEnd - 1 });
R
rebornix 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	return result;
}

export function getVisibleCells(cells: CellViewModel[], hiddenRanges: ICellRange[]) {
	if (!hiddenRanges.length) {
		return cells;
	}

	let start = 0;
	let hiddenRangeIndex = 0;
	let result: any[] = [];

	while (start < cells.length && hiddenRangeIndex < hiddenRanges.length) {
		if (start < hiddenRanges[hiddenRangeIndex].start) {
			result.push(...cells.slice(start, hiddenRanges[hiddenRangeIndex].start));
		}

R
rebornix 已提交
542
		start = hiddenRanges[hiddenRangeIndex].end + 1;
R
rebornix 已提交
543 544 545 546 547 548 549 550 551
		hiddenRangeIndex++;
	}

	if (start < cells.length) {
		result.push(...cells.slice(start));
	}

	return result;
}