notebookBrowser.ts 8.4 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 } 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 22

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

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

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

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

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

export interface MarkdownCellLayoutInfo {
	readonly fontInfo: BareFontInfo | null;
	readonly editorWidth: number;
}

export interface MarkdownCellLayoutChangeEvent {
R
rebornix 已提交
64 65
	font?: BareFontInfo;
	outerWidth?: number;
R
rebornix 已提交
66 67
}

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

82
export interface INotebookEditor {
R
rebornix 已提交
83 84 85 86

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

89 90
	isNotebookEditor: boolean;

R
rebornix 已提交
91 92 93
	/**
	 * Focus the notebook editor cell list
	 */
R
rebornix 已提交
94
	focus(): void;
R
rebornix 已提交
95

R
rebornix 已提交
96 97 98
	/**
	 * Select & focus cell
	 */
R
rebornix 已提交
99
	selectElement(cell: ICellViewModel): void;
R
rebornix 已提交
100

R
rebornix 已提交
101 102 103
	/**
	 * Layout info for the notebook editor
	 */
R
rebornix 已提交
104
	getLayoutInfo(): NotebookLayoutInfo;
R
rebornix 已提交
105 106 107
	/**
	 * Fetch the output renderers for notebook outputs.
	 */
R
rebornix 已提交
108
	getOutputRenderer(): OutputRenderer;
R
rebornix 已提交
109 110 111 112

	/**
	 * Insert a new cell around `cell`
	 */
R
rebornix 已提交
113
	insertNotebookCell(cell: ICellViewModel, type: CellKind, direction: 'above' | 'below', initialText?: string): Promise<void>;
R
rebornix 已提交
114 115 116 117

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

120 121 122
	/**
	 * Move a cell up one spot
	 */
123
	moveCellUp(cell: ICellViewModel): void;
124 125 126 127

	/**
	 * Move a cell down one spot
	 */
128
	moveCellDown(cell: ICellViewModel): void;
129

R
rebornix 已提交
130 131 132 133 134 135
	/**
	 * 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 已提交
136
	editNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
137 138 139 140

	/**
	 * Quit cell editing mode.
	 */
R
rebornix 已提交
141
	saveNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
142 143 144 145

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

148 149 150 151 152
	/**
	 * Execute the given notebook cell
	 */
	executeNotebookCell(cell: ICellViewModel): Promise<void>;

R
rebornix 已提交
153 154 155
	/**
	 * Get current active cell
	 */
R
rebornix 已提交
156
	getActiveCell(): ICellViewModel | undefined;
R
rebornix 已提交
157 158 159 160

	/**
	 * Layout the cell with a new height
	 */
161
	layoutNotebookCell(cell: ICellViewModel, height: number): Promise<void>;
R
rebornix 已提交
162 163 164 165

	/**
	 * Render the output in webview layer
	 */
R
rebornix 已提交
166
	createInset(cell: ICellViewModel, output: IOutput, shadowContent: string, offset: number): void;
R
rebornix 已提交
167 168 169 170

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

173 174 175 176 177
	/**
	 * Send message to the webview for outputs.
	 */
	postMessage(message: any): void;

R
rebornix 已提交
178 179 180
	/**
	 * Trigger the editor to scroll from scroll event programmatically
	 */
181
	triggerScroll(event: IMouseWheelEvent): void;
R
rebornix 已提交
182 183 184 185

	/**
	 * Reveal cell into viewport.
	 */
R
rebornix 已提交
186
	revealInView(cell: ICellViewModel): void;
R
rebornix 已提交
187 188 189 190

	/**
	 * Reveal cell into viewport center.
	 */
R
rebornix 已提交
191
	revealInCenter(cell: ICellViewModel): void;
R
rebornix 已提交
192 193 194 195

	/**
	 * Reveal cell into viewport center if cell is currently out of the viewport.
	 */
R
rebornix 已提交
196
	revealInCenterIfOutsideViewport(cell: ICellViewModel): void;
R
rebornix 已提交
197 198 199 200

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

R
rebornix 已提交
203 204 205
	/**
	 * Reveal a line in notebook cell into viewport center.
	 */
R
rebornix 已提交
206
	revealLineInCenter(cell: ICellViewModel, line: number): void;
R
rebornix 已提交
207 208 209 210

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

R
rebornix 已提交
213 214 215
	/**
	 * Reveal a range in notebook cell into viewport with minimal scrolling.
	 */
R
rebornix 已提交
216
	revealRangeInView(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
217 218 219 220

	/**
	 * Reveal a range in notebook cell into viewport center.
	 */
R
rebornix 已提交
221
	revealRangeInCenter(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
222 223 224 225

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

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

R
rebornix 已提交
230 231 232 233
	/**
	 * Change the decorations on cells.
	 * The notebook is virtualized and this method should be called to create/delete editor decorations safely.
	 */
R
rebornix 已提交
234
	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any;
R
rebornix 已提交
235 236 237 238 239 240

	/**
	 * Show Find Widget.
	 *
	 * Currently Find is still part of the NotebookEditor core
	 */
R
rebornix 已提交
241
	showFind(): void;
R
rebornix 已提交
242 243 244 245

	/**
	 * Hide Find Widget
	 */
R
rebornix 已提交
246
	hideFind(): void;
R
rebornix 已提交
247 248
}

249
export interface BaseCellRenderTemplate {
R
rebornix 已提交
250 251
	container: HTMLElement;
	cellContainer: HTMLElement;
252
	toolbar: ToolBar;
253
	focusIndicator: HTMLElement;
254
	disposables: DisposableStore;
255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

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 已提交
269
	betweenCellContainer: HTMLElement;
R
rebornix 已提交
270
}
R
rebornix 已提交
271 272 273 274 275 276 277 278 279

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

	render(output: IOutput, container: HTMLElement, preferredMimeType: string | undefined): IRenderOutput;
}
R
rebornix 已提交
280 281 282 283 284 285

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

R
rebornix 已提交
286 287 288 289 290 291 292 293 294
export enum CellRevealType {
	Line,
	Range
}

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

296 297 298 299 300 301
export enum CellRunState {
	Idle,
	Running
}

export enum CellEditState {
R
rebornix 已提交
302 303 304 305 306
	/**
	 * Default state.
	 * For markdown cell, it's Markdown preview.
	 * For code cell, the browser focus should be on the container instead of the editor
	 */
307
	Preview,
R
rebornix 已提交
308 309 310 311 312 313 314


	/**
	 * Eding mode. Source for markdown or code is rendered in editors and the state will be persistent.
	 */
	Editing
}
R
rebornix 已提交
315 316 317 318 319 320 321 322 323 324 325 326

export enum CellFocusMode {
	Container,
	Editor
}

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