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

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

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

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

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

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

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

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

R
rebornix 已提交
65 66 67 68 69
export interface ICellViewModel {
	readonly id: string;
	handle: number;
	uri: URI;
	cellKind: CellKind;
70 71
	editState: CellEditState;
	runState: CellRunState;
R
rebornix 已提交
72 73
	focusMode: CellFocusMode;
	getText(): string;
R
Rob Lourens 已提交
74
	metadata: NotebookCellMetadata;
R
rebornix 已提交
75 76
}

77
export interface INotebookEditor {
R
rebornix 已提交
78 79 80 81

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

84 85
	isNotebookEditor: boolean;

R
rebornix 已提交
86 87 88
	/**
	 * Focus the notebook editor cell list
	 */
R
rebornix 已提交
89
	focus(): void;
R
rebornix 已提交
90

R
rebornix 已提交
91 92 93
	/**
	 * Select & focus cell
	 */
R
rebornix 已提交
94
	selectElement(cell: ICellViewModel): void;
R
rebornix 已提交
95

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

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

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

115 116 117
	/**
	 * Move a cell up one spot
	 */
118
	moveCellUp(cell: ICellViewModel): void;
119 120 121 122

	/**
	 * Move a cell down one spot
	 */
123
	moveCellDown(cell: ICellViewModel): void;
124

R
rebornix 已提交
125 126 127 128 129 130
	/**
	 * 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 已提交
131
	editNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
132 133 134 135

	/**
	 * Quit cell editing mode.
	 */
R
rebornix 已提交
136
	saveNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
137 138 139 140

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

143 144 145 146 147
	/**
	 * Execute the given notebook cell
	 */
	executeNotebookCell(cell: ICellViewModel): Promise<void>;

R
rebornix 已提交
148 149 150
	/**
	 * Get current active cell
	 */
R
rebornix 已提交
151
	getActiveCell(): ICellViewModel | undefined;
R
rebornix 已提交
152 153 154 155

	/**
	 * Layout the cell with a new height
	 */
R
rebornix 已提交
156
	layoutNotebookCell(cell: ICellViewModel, height: number): void;
R
rebornix 已提交
157 158 159 160

	/**
	 * Render the output in webview layer
	 */
R
rebornix 已提交
161
	createInset(cell: ICellViewModel, output: IOutput, shadowContent: string, offset: number): void;
R
rebornix 已提交
162 163 164 165

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

168 169 170 171 172
	/**
	 * Send message to the webview for outputs.
	 */
	postMessage(message: any): void;

R
rebornix 已提交
173 174 175
	/**
	 * Trigger the editor to scroll from scroll event programmatically
	 */
176
	triggerScroll(event: IMouseWheelEvent): void;
R
rebornix 已提交
177 178 179 180

	/**
	 * Reveal cell into viewport.
	 */
R
rebornix 已提交
181
	revealInView(cell: ICellViewModel): void;
R
rebornix 已提交
182 183 184 185

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

	/**
	 * Reveal cell into viewport center if cell is currently out of the viewport.
	 */
R
rebornix 已提交
191
	revealInCenterIfOutsideViewport(cell: ICellViewModel): void;
R
rebornix 已提交
192 193 194 195

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

R
rebornix 已提交
198 199 200
	/**
	 * Reveal a line in notebook cell into viewport center.
	 */
R
rebornix 已提交
201
	revealLineInCenter(cell: ICellViewModel, line: number): void;
R
rebornix 已提交
202 203 204 205

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

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

	/**
	 * Reveal a range in notebook cell into viewport center.
	 */
R
rebornix 已提交
216
	revealRangeInCenter(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
217 218 219 220

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

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

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

	/**
	 * Show Find Widget.
	 *
	 * Currently Find is still part of the NotebookEditor core
	 */
R
rebornix 已提交
236
	showFind(): void;
R
rebornix 已提交
237 238 239 240

	/**
	 * Hide Find Widget
	 */
R
rebornix 已提交
241
	hideFind(): void;
R
rebornix 已提交
242 243 244 245 246
}

export interface CellRenderTemplate {
	container: HTMLElement;
	cellContainer: HTMLElement;
R
Rob Lourens 已提交
247
	editorContainer?: HTMLElement;
248
	toolbar: ToolBar;
249
	focusIndicator?: HTMLElement;
R
Rob Lourens 已提交
250
	runToolbar?: ToolBar;
R
rebornix 已提交
251 252 253
	editingContainer?: HTMLElement;
	outputContainer?: HTMLElement;
	editor?: CodeEditorWidget;
R
Rob Lourens 已提交
254
	progressBar?: ProgressBar;
255
	disposables: DisposableStore;
256
	toJSON(): void;
R
rebornix 已提交
257
}
R
rebornix 已提交
258 259 260 261 262 263 264 265 266

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

	render(output: IOutput, container: HTMLElement, preferredMimeType: string | undefined): IRenderOutput;
}
R
rebornix 已提交
267 268 269 270 271 272

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

R
rebornix 已提交
273 274 275 276 277 278 279 280 281
export enum CellRevealType {
	Line,
	Range
}

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

283 284 285 286 287 288
export enum CellRunState {
	Idle,
	Running
}

export enum CellEditState {
R
rebornix 已提交
289 290 291 292 293
	/**
	 * Default state.
	 * For markdown cell, it's Markdown preview.
	 * For code cell, the browser focus should be on the container instead of the editor
	 */
294
	Preview,
R
rebornix 已提交
295 296 297 298 299 300 301


	/**
	 * Eding mode. Source for markdown or code is rendered in editors and the state will be persistent.
	 */
	Editing
}
R
rebornix 已提交
302 303 304 305 306 307 308 309 310 311 312 313

export enum CellFocusMode {
	Container,
	Editor
}

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