notebookBrowser.ts 8.3 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 47 48 49 50 51 52
	readonly outputTotalHeight: number;
	readonly indicatorHeight: number;
}

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

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

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

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

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

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

88 89
	isNotebookEditor: boolean;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 已提交
268
}
R
rebornix 已提交
269 270 271 272 273 274 275 276 277

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

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

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

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

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

294 295 296 297 298 299
export enum CellRunState {
	Idle,
	Running
}

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


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

export enum CellFocusMode {
	Container,
	Editor
}

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