notebookBrowser.ts 9.0 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 23

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

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

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

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

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

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

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

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

87
export interface INotebookEditor {
R
rebornix 已提交
88 89 90 91

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

94 95
	isNotebookEditor: boolean;

R
rebornix 已提交
96 97
	getInnerWebview(): Webview | undefined;

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

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

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

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

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

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

	/**
	 * Move a cell down one spot
	 */
135
	moveCellDown(cell: ICellViewModel): void;
136

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 已提交
293
}
R
rebornix 已提交
294 295 296 297 298 299 300 301 302

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

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

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

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

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

319 320 321 322 323 324
export enum CellRunState {
	Idle,
	Running
}

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


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

export enum CellFocusMode {
	Container,
	Editor
}

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