notebookBrowser.ts 9.1 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';
7
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
8
import { ToolBar } from 'vs/base/browser/ui/toolbar/toolbar';
9
import { CancellationTokenSource } from 'vs/base/common/cancellation';
10
import { DisposableStore } from 'vs/base/common/lifecycle';
R
rebornix 已提交
11
import { URI } from 'vs/base/common/uri';
12 13 14 15 16
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';
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
17
import { NOTEBOOK_EDITABLE_CONTEXT_KEY, NOTEBOOK_EXECUTING_KEY } from 'vs/workbench/contrib/notebook/browser/constants';
18
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
19
import { CellViewModel, IModelDecorationsChangeAccessor, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
R
rebornix 已提交
20
import { CellKind, IOutput, IRenderOutput, NotebookCellMetadata, NotebookDocumentMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
21
import { Webview } from 'vs/workbench/contrib/webview/browser/webview';
R
rebornix 已提交
22
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
R
rebornix 已提交
23 24

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

26
export const NOTEBOOK_EDITOR_FOCUSED = new RawContextKey<boolean>('notebookEditorFocused', false);
R
rebornix 已提交
27
export const NOTEBOOK_EDITOR_EDITABLE = new RawContextKey<boolean>(NOTEBOOK_EDITABLE_CONTEXT_KEY, true);
28
export const NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK = new RawContextKey<boolean>(NOTEBOOK_EXECUTING_KEY, false);
29

R
rebornix 已提交
30 31 32 33 34 35
export interface NotebookLayoutInfo {
	width: number;
	height: number;
	fontInfo: BareFontInfo;
}

R
rebornix 已提交
36 37 38 39 40 41 42 43 44 45 46
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;
47
	readonly outputContainerOffset: number;
R
rebornix 已提交
48 49
	readonly outputTotalHeight: number;
	readonly indicatorHeight: number;
R
rebornix 已提交
50
	readonly bottomToolbarOffset: number;
R
rebornix 已提交
51 52 53 54 55 56
}

export interface CodeCellLayoutChangeEvent {
	editorHeight?: boolean;
	outputHeight?: boolean;
	totalHeight?: boolean;
R
rebornix 已提交
57 58
	outerWidth?: number;
	font?: BareFontInfo;
R
rebornix 已提交
59 60 61 62 63
}

export interface MarkdownCellLayoutInfo {
	readonly fontInfo: BareFontInfo | null;
	readonly editorWidth: number;
R
rebornix 已提交
64
	readonly bottomToolbarOffset: number;
R
rebornix 已提交
65
	readonly totalHeight: number;
R
rebornix 已提交
66 67 68
}

export interface MarkdownCellLayoutChangeEvent {
R
rebornix 已提交
69 70
	font?: BareFontInfo;
	outerWidth?: number;
R
rebornix 已提交
71
	totalHeight?: number;
R
rebornix 已提交
72 73
}

R
rebornix 已提交
74
export interface ICellViewModel {
R
rebornix 已提交
75
	readonly model: NotebookCellTextModel;
R
rebornix 已提交
76 77 78 79
	readonly id: string;
	handle: number;
	uri: URI;
	cellKind: CellKind;
80
	editState: CellEditState;
81 82
	readonly runState: CellRunState;
	currentTokenSource: CancellationTokenSource | undefined;
R
rebornix 已提交
83 84
	focusMode: CellFocusMode;
	getText(): string;
85
	metadata: NotebookCellMetadata | undefined;
R
rebornix 已提交
86
	getEvaluatedMetadata(documentMetadata: NotebookDocumentMetadata | undefined): NotebookCellMetadata;
R
rebornix 已提交
87 88
}

89
export interface INotebookEditor {
R
rebornix 已提交
90 91 92 93

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

96 97
	isNotebookEditor: boolean;

R
rebornix 已提交
98 99
	getInnerWebview(): Webview | undefined;

R
rebornix 已提交
100 101 102
	/**
	 * Focus the notebook editor cell list
	 */
R
rebornix 已提交
103
	focus(): void;
R
rebornix 已提交
104

R
rebornix 已提交
105 106 107
	/**
	 * Select & focus cell
	 */
R
rebornix 已提交
108
	selectElement(cell: ICellViewModel): void;
R
rebornix 已提交
109

R
rebornix 已提交
110 111 112
	/**
	 * Layout info for the notebook editor
	 */
R
rebornix 已提交
113
	getLayoutInfo(): NotebookLayoutInfo;
R
rebornix 已提交
114 115 116
	/**
	 * Fetch the output renderers for notebook outputs.
	 */
R
rebornix 已提交
117
	getOutputRenderer(): OutputRenderer;
R
rebornix 已提交
118 119 120 121

	/**
	 * Insert a new cell around `cell`
	 */
R
rebornix 已提交
122
	insertNotebookCell(cell: ICellViewModel, type: CellKind, direction: 'above' | 'below', initialText?: string): Promise<void>;
R
rebornix 已提交
123 124 125 126

	/**
	 * Delete a cell from the notebook
	 */
R
rebornix 已提交
127
	deleteNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
128

129 130 131
	/**
	 * Move a cell up one spot
	 */
132
	moveCellUp(cell: ICellViewModel): void;
133 134 135 136

	/**
	 * Move a cell down one spot
	 */
137
	moveCellDown(cell: ICellViewModel): void;
138

R
rebornix 已提交
139 140 141 142 143 144
	/**
	 * 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 已提交
145
	editNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
146 147 148 149

	/**
	 * Quit cell editing mode.
	 */
R
rebornix 已提交
150
	saveNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
151 152 153 154

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

157 158 159 160 161
	/**
	 * Execute the given notebook cell
	 */
	executeNotebookCell(cell: ICellViewModel): Promise<void>;

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	/**
	 * 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 已提交
177 178 179
	/**
	 * Get current active cell
	 */
R
rebornix 已提交
180
	getActiveCell(): ICellViewModel | undefined;
R
rebornix 已提交
181 182 183 184

	/**
	 * Layout the cell with a new height
	 */
185
	layoutNotebookCell(cell: ICellViewModel, height: number): Promise<void>;
R
rebornix 已提交
186 187 188 189

	/**
	 * Render the output in webview layer
	 */
R
rebornix 已提交
190
	createInset(cell: ICellViewModel, output: IOutput, shadowContent: string, offset: number): void;
R
rebornix 已提交
191 192 193 194

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

197 198 199 200 201
	/**
	 * Send message to the webview for outputs.
	 */
	postMessage(message: any): void;

R
rebornix 已提交
202 203 204
	/**
	 * Trigger the editor to scroll from scroll event programmatically
	 */
205
	triggerScroll(event: IMouseWheelEvent): void;
R
rebornix 已提交
206 207 208 209

	/**
	 * Reveal cell into viewport.
	 */
R
rebornix 已提交
210
	revealInView(cell: ICellViewModel): void;
R
rebornix 已提交
211 212 213 214

	/**
	 * Reveal cell into viewport center.
	 */
R
rebornix 已提交
215
	revealInCenter(cell: ICellViewModel): void;
R
rebornix 已提交
216 217 218 219

	/**
	 * Reveal cell into viewport center if cell is currently out of the viewport.
	 */
R
rebornix 已提交
220
	revealInCenterIfOutsideViewport(cell: ICellViewModel): void;
R
rebornix 已提交
221 222 223 224

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

R
rebornix 已提交
227 228 229
	/**
	 * Reveal a line in notebook cell into viewport center.
	 */
R
rebornix 已提交
230
	revealLineInCenter(cell: ICellViewModel, line: number): void;
R
rebornix 已提交
231 232 233 234

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

R
rebornix 已提交
237 238 239
	/**
	 * Reveal a range in notebook cell into viewport with minimal scrolling.
	 */
R
rebornix 已提交
240
	revealRangeInView(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
241 242 243 244

	/**
	 * Reveal a range in notebook cell into viewport center.
	 */
R
rebornix 已提交
245
	revealRangeInCenter(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
246 247 248 249

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

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

R
rebornix 已提交
254 255 256 257
	/**
	 * Change the decorations on cells.
	 * The notebook is virtualized and this method should be called to create/delete editor decorations safely.
	 */
R
rebornix 已提交
258
	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any;
R
rebornix 已提交
259 260 261 262 263 264

	/**
	 * Show Find Widget.
	 *
	 * Currently Find is still part of the NotebookEditor core
	 */
R
rebornix 已提交
265
	showFind(): void;
R
rebornix 已提交
266 267 268 269

	/**
	 * Hide Find Widget
	 */
R
rebornix 已提交
270
	hideFind(): void;
R
rebornix 已提交
271 272
}

273
export interface BaseCellRenderTemplate {
R
rebornix 已提交
274 275
	container: HTMLElement;
	cellContainer: HTMLElement;
276
	toolbar: ToolBar;
277
	focusIndicator: HTMLElement;
278
	disposables: DisposableStore;
R
rebornix 已提交
279
	bottomCellContainer: HTMLElement;
R
rebornix 已提交
280
	toJSON: () => any;
281 282 283 284 285 286 287 288 289 290 291 292 293 294
}

export interface MarkdownCellRenderTemplate extends BaseCellRenderTemplate {
	editingContainer: HTMLElement;
}

export interface CodeCellRenderTemplate extends BaseCellRenderTemplate {
	editorContainer: HTMLElement;
	runToolbar: ToolBar;
	runButtonContainer: HTMLElement;
	executionOrderLabel: HTMLElement;
	outputContainer: HTMLElement;
	editor: CodeEditorWidget;
	progressBar: ProgressBar;
R
rebornix 已提交
295
}
R
rebornix 已提交
296 297 298 299 300 301 302 303 304

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

	render(output: IOutput, container: HTMLElement, preferredMimeType: string | undefined): IRenderOutput;
}
R
rebornix 已提交
305 306 307 308 309 310

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

R
rebornix 已提交
311 312 313 314 315 316 317 318 319
export enum CellRevealType {
	Line,
	Range
}

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

321 322 323 324 325 326
export enum CellRunState {
	Idle,
	Running
}

export enum CellEditState {
R
rebornix 已提交
327 328 329 330 331
	/**
	 * Default state.
	 * For markdown cell, it's Markdown preview.
	 * For code cell, the browser focus should be on the container instead of the editor
	 */
332
	Preview,
R
rebornix 已提交
333 334 335 336 337 338 339


	/**
	 * Eding mode. Source for markdown or code is rendered in editors and the state will be persistent.
	 */
	Editing
}
R
rebornix 已提交
340 341 342 343 344 345 346 347 348 349 350 351

export enum CellFocusMode {
	Container,
	Editor
}

export enum CursorAtBoundary {
	None,
	Top,
	Bottom,
	Both
}