notebookEditor.ts 27.4 KB
Newer Older
P
Peng Lyu 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import * as nls from 'vs/nls';
R
rebornix 已提交
7
import { getZoomLevel } from 'vs/base/browser/browser';
P
Peng Lyu 已提交
8
import * as DOM from 'vs/base/browser/dom';
P
Peng Lyu 已提交
9
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
R
rebornix 已提交
10
import { CancellationToken } from 'vs/base/common/cancellation';
R
rebornix 已提交
11
import { DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle';
R
rebornix 已提交
12
import 'vs/css!./notebook';
13
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
R
rebornix 已提交
14
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
15
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
R
Rob Lourens 已提交
16
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
R
rebornix 已提交
17
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
R
rebornix 已提交
18 19 20
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
21
import { contrastBorder, editorBackground, focusBorder, foreground, textBlockQuoteBackground, textBlockQuoteBorder, textLinkActiveForeground, textLinkForeground, textPreformatForeground, registerColor } from 'vs/platform/theme/common/colorRegistry';
R
rebornix 已提交
22 23
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
24
import { EditorOptions, IEditorMemento, IEditorCloseEvent } from 'vs/workbench/common/editor';
25
import { INotebookEditor, NotebookLayoutInfo, CellState, NOTEBOOK_EDITOR_FOCUSED, CellFocusMode, ICellViewModel } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
R
rebornix 已提交
26
import { NotebookEditorInput, NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
R
rebornix 已提交
27
import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
R
rebornix 已提交
28 29 30
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
import { BackLayerWebView } from 'vs/workbench/contrib/notebook/browser/view/renderers/backLayerWebView';
import { CodeCellRenderer, MarkdownCellRenderer, NotebookCellListDelegate } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellRenderer';
R
rebornix 已提交
31
import { IOutput, CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
32 33 34
import { IWebviewService } from 'vs/workbench/contrib/webview/browser/webview';
import { getExtraColor } from 'vs/workbench/contrib/welcome/walkThrough/common/walkThroughUtils';
import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
J
Johannes Rieken 已提交
35
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
36
import { IEditor, ICompositeCodeEditor } from 'vs/editor/common/editorCommon';
37
import { IResourceEditorInput } from 'vs/platform/editor/common/editor';
38
import { Emitter, Event } from 'vs/base/common/event';
R
rebornix 已提交
39 40 41
import { NotebookCellList } from 'vs/workbench/contrib/notebook/browser/view/notebookCellList';
import { NotebookFindWidget } from 'vs/workbench/contrib/notebook/browser/contrib/notebookFindWidget';
import { NotebookViewModel, INotebookEditorViewState, IModelDecorationsChangeAccessor } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
R
rebornix 已提交
42
import { IEditorGroupView } from 'vs/workbench/browser/parts/editor/editor';
R
rebornix 已提交
43
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel';
R
rebornix 已提交
44
import { Range } from 'vs/editor/common/core/range';
R
Rob Lourens 已提交
45
import { CELL_MARGIN, RUN_BUTTON_WIDTH } from 'vs/workbench/contrib/notebook/browser/constants';
46
import { Color, RGBA } from 'vs/base/common/color';
P
Peng Lyu 已提交
47 48

const $ = DOM.$;
R
rebornix 已提交
49 50
const NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY = 'NotebookEditorViewState';

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
export class NotebookEditorOptions extends EditorOptions {

	readonly cellOptions?: IResourceEditorInput;

	constructor(options: Partial<NotebookEditorOptions>) {
		super();
		this.overwrite(options);
		this.cellOptions = options.cellOptions;
	}

	with(options: Partial<NotebookEditorOptions>): NotebookEditorOptions {
		return new NotebookEditorOptions({ ...this, ...options });
	}
}

R
rebornix 已提交
66
export class NotebookCodeEditors implements ICompositeCodeEditor {
67

68
	private readonly _disposables = new DisposableStore();
69 70 71 72
	private readonly _onDidChangeActiveEditor = new Emitter<this>();
	readonly onDidChangeActiveEditor: Event<this> = this._onDidChangeActiveEditor.event;

	constructor(
R
rebornix 已提交
73
		private _list: NotebookCellList,
R
rebornix 已提交
74
		private _renderedEditors: Map<ICellViewModel, ICodeEditor | undefined>
75
	) {
76
		_list.onDidChangeFocus(_e => this._onDidChangeActiveEditor.fire(this), undefined, this._disposables);
77 78 79 80 81 82
	}

	dispose(): void {
		this._onDidChangeActiveEditor.dispose();
		this._disposables.dispose();
	}
83 84 85 86 87 88 89 90 91

	get activeCodeEditor(): IEditor | undefined {
		const [focused] = this._list.getFocusedElements();
		return focused instanceof CellViewModel
			? this._renderedEditors.get(focused)
			: undefined;
	}
}

R
rebornix 已提交
92
export class NotebookEditor extends BaseEditor implements INotebookEditor {
P
Peng Lyu 已提交
93 94 95
	static readonly ID: string = 'workbench.editor.notebook';
	private rootElement!: HTMLElement;
	private body!: HTMLElement;
P
Peng Lyu 已提交
96
	private webview: BackLayerWebView | null = null;
R
rebornix 已提交
97
	private list: NotebookCellList | undefined;
98
	private control: ICompositeCodeEditor | undefined;
R
rebornix 已提交
99
	private renderedEditors: Map<ICellViewModel, ICodeEditor | undefined> = new Map();
R
rebornix 已提交
100
	private notebookViewModel: NotebookViewModel | undefined;
101
	private localStore: DisposableStore = this._register(new DisposableStore());
R
rebornix 已提交
102
	private editorMemento: IEditorMemento<INotebookEditorViewState>;
R
rebornix 已提交
103
	private readonly groupListener = this._register(new MutableDisposable());
104
	private fontInfo: BareFontInfo | undefined;
105
	private dimension: DOM.Dimension | null = null;
R
rebornix 已提交
106
	private editorFocus: IContextKey<boolean> | null = null;
R
rebornix 已提交
107
	private outputRenderer: OutputRenderer;
R
rebornix 已提交
108
	private findWidget: NotebookFindWidget;
P
Peng Lyu 已提交
109 110 111 112 113

	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
		@IThemeService themeService: IThemeService,
		@IInstantiationService private readonly instantiationService: IInstantiationService,
P
Peng Lyu 已提交
114
		@IStorageService storageService: IStorageService,
R
rebornix 已提交
115
		@IWebviewService private webviewService: IWebviewService,
R
rebornix 已提交
116
		@INotebookService private notebookService: INotebookService,
117
		@IEditorGroupsService editorGroupService: IEditorGroupsService,
R
rebornix 已提交
118
		@IConfigurationService private readonly configurationService: IConfigurationService,
R
rebornix 已提交
119
		@IEnvironmentService private readonly environmentSerice: IEnvironmentService,
R
rebornix 已提交
120
		@IContextKeyService private readonly contextKeyService: IContextKeyService,
P
Peng Lyu 已提交
121 122
	) {
		super(NotebookEditor.ID, telemetryService, themeService, storageService);
R
rebornix 已提交
123 124

		this.editorMemento = this.getEditorMemento<INotebookEditorViewState>(editorGroupService, NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY);
R
rebornix 已提交
125
		this.outputRenderer = new OutputRenderer(this, this.instantiationService);
R
rebornix 已提交
126
		this.findWidget = this.instantiationService.createInstance(NotebookFindWidget, this);
127
		this.findWidget.updateTheme(this.themeService.getColorTheme());
R
rebornix 已提交
128 129
	}

R
rebornix 已提交
130 131 132 133
	get viewModel() {
		return this.notebookViewModel;
	}

P
Peng Lyu 已提交
134 135 136 137 138 139 140 141
	get minimumWidth(): number { return 375; }
	get maximumWidth(): number { return Number.POSITIVE_INFINITY; }

	// these setters need to exist because this extends from BaseEditor
	set minimumWidth(value: number) { /*noop*/ }
	set maximumWidth(value: number) { /*noop*/ }


R
rebornix 已提交
142
	//#region Editor Core
R
rebornix 已提交
143

R
rebornix 已提交
144 145 146 147 148

	public get isNotebookEditor() {
		return true;
	}

P
Peng Lyu 已提交
149 150 151
	protected createEditor(parent: HTMLElement): void {
		this.rootElement = DOM.append(parent, $('.notebook-editor'));
		this.createBody(this.rootElement);
152
		this.generateFontInfo();
R
rebornix 已提交
153 154 155 156 157 158 159 160
		this.editorFocus = NOTEBOOK_EDITOR_FOCUSED.bindTo(this.contextKeyService);
		this._register(this.onDidFocus(() => {
			this.editorFocus?.set(true);
		}));

		this._register(this.onDidBlur(() => {
			this.editorFocus?.set(false);
		}));
161 162 163 164 165
	}

	private generateFontInfo(): void {
		const editorOptions = this.configurationService.getValue<IEditorOptions>('editor');
		this.fontInfo = BareFontInfo.createFromRawSettings(editorOptions, getZoomLevel());
P
Peng Lyu 已提交
166 167 168
	}

	private createBody(parent: HTMLElement): void {
P
Peng Lyu 已提交
169
		this.body = document.createElement('div');
P
Peng Lyu 已提交
170 171 172
		DOM.addClass(this.body, 'cell-list-container');
		this.createCellList();
		DOM.append(parent, this.body);
R
rebornix 已提交
173
		DOM.append(parent, this.findWidget.getDomNode());
P
Peng Lyu 已提交
174 175
	}

P
Peng Lyu 已提交
176 177
	private createCellList(): void {
		DOM.addClass(this.body, 'cell-list-container');
P
Peng Lyu 已提交
178

P
Peng Lyu 已提交
179
		const renders = [
180
			this.instantiationService.createInstance(CodeCellRenderer, this, this.renderedEditors),
P
Peng Lyu 已提交
181
			this.instantiationService.createInstance(MarkdownCellRenderer, this),
P
Peng Lyu 已提交
182 183
		];

R
rebornix 已提交
184
		this.list = <NotebookCellList>this.instantiationService.createInstance(
R
rebornix 已提交
185
			NotebookCellList,
P
Peng Lyu 已提交
186 187 188 189
			'NotebookCellList',
			this.body,
			this.instantiationService.createInstance(NotebookCellListDelegate),
			renders,
R
rebornix 已提交
190
			this.contextKeyService,
P
Peng Lyu 已提交
191 192
			{
				setRowLineHeight: false,
R
rebornix 已提交
193
				setRowHeight: false,
P
Peng Lyu 已提交
194 195 196
				supportDynamicHeights: true,
				horizontalScrolling: false,
				keyboardSupport: false,
R
rebornix 已提交
197
				mouseSupport: true,
P
Peng Lyu 已提交
198
				multipleSelectionSupport: false,
R
rebornix 已提交
199
				enableKeyboardNavigation: true,
P
Peng Lyu 已提交
200 201 202 203 204 205 206 207 208 209
				overrideStyles: {
					listBackground: editorBackground,
					listActiveSelectionBackground: editorBackground,
					listActiveSelectionForeground: foreground,
					listFocusAndSelectionBackground: editorBackground,
					listFocusAndSelectionForeground: foreground,
					listFocusBackground: editorBackground,
					listFocusForeground: foreground,
					listHoverForeground: foreground,
					listHoverBackground: editorBackground,
210 211
					listHoverOutline: focusBorder,
					listFocusOutline: focusBorder,
212 213 214 215
					listInactiveSelectionBackground: editorBackground,
					listInactiveSelectionForeground: foreground,
					listInactiveFocusBackground: editorBackground,
					listInactiveFocusOutline: editorBackground,
P
Peng Lyu 已提交
216
				}
R
rebornix 已提交
217
			},
P
Peng Lyu 已提交
218
		);
P
Peng Lyu 已提交
219

220
		this.control = new NotebookCodeEditors(this.list, this.renderedEditors);
R
rebornix 已提交
221
		this.webview = new BackLayerWebView(this.webviewService, this.notebookService, this, this.environmentSerice);
222 223 224 225 226
		this._register(this.webview.onMessage(message => {
			if (this.viewModel) {
				this.notebookService.onDidReceiveMessage(this.viewModel.viewType, this.viewModel.uri, message);
			}
		}));
R
rebornix 已提交
227
		this.list.rowsContainer.appendChild(this.webview.element);
P
Peng Lyu 已提交
228 229 230
		this._register(this.list);
	}

231 232 233 234
	getControl() {
		return this.control;
	}

P
Peng Lyu 已提交
235
	onHide() {
R
rebornix 已提交
236
		this.editorFocus?.set(false);
237 238
		if (this.webview) {
			this.localStore.clear();
R
rebornix 已提交
239
			this.list?.rowsContainer.removeChild(this.webview?.element);
240 241 242 243 244
			this.webview?.dispose();
			this.webview = null;
		}

		this.list?.splice(0, this.list?.length);
R
rebornix 已提交
245

R
rebornix 已提交
246
		if (this.notebookViewModel && !this.notebookViewModel.isDirty()) {
R
rebornix 已提交
247
			this.notebookService.destoryNotebookDocument(this.notebookViewModel.viewType!, this.notebookViewModel!.notebookDocument);
R
rebornix 已提交
248
			this.notebookViewModel.dispose();
R
rebornix 已提交
249
			this.notebookViewModel = undefined;
R
rebornix 已提交
250 251
		}

252
		super.onHide();
P
Peng Lyu 已提交
253 254
	}

R
rebornix 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267
	setEditorVisible(visible: boolean, group: IEditorGroup | undefined): void {
		super.setEditorVisible(visible, group);
		this.groupListener.value = ((group as IEditorGroupView).onWillCloseEditor(e => this.onWillCloseEditorInGroup(e)));
	}

	private onWillCloseEditorInGroup(e: IEditorCloseEvent): void {
		const editor = e.editor;
		if (!(editor instanceof NotebookEditorInput)) {
			return; // only handle files
		}

		if (editor === this.input) {
			this.saveTextEditorViewState(editor);
268 269 270
		}
	}

R
rebornix 已提交
271 272 273 274 275
	focus() {
		super.focus();
		this.editorFocus?.set(true);
	}

R
rebornix 已提交
276
	async setInput(input: NotebookEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
R
rebornix 已提交
277 278 279 280
		if (this.input instanceof NotebookEditorInput) {
			this.saveTextEditorViewState(this.input);
		}

R
rebornix 已提交
281 282
		await super.setInput(input, options, token);
		const model = await input.resolve();
P
Peng Lyu 已提交
283

284 285 286
		if (this.notebookViewModel === undefined || !this.notebookViewModel.equal(model) || this.webview === null) {
			this.detachModel();
			await this.attachModel(input, model);
R
rebornix 已提交
287
		}
P
Peng Lyu 已提交
288

289 290 291
		// reveal cell if editor options tell to do so
		if (options instanceof NotebookEditorOptions && options.cellOptions) {
			const cellOptions = options.cellOptions;
R
rebornix 已提交
292
			const cell = this.notebookViewModel!.viewCells.find(cell => cell.uri.toString() === cellOptions.resource.toString());
293
			if (cell) {
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
				this.revealInCenterIfOutsideViewport(cell);
				const editor = this.renderedEditors.get(cell)!;
				if (editor) {
					if (cellOptions.options?.selection) {
						const { selection } = cellOptions.options;
						editor.setSelection({
							...selection,
							endLineNumber: selection.endLineNumber || selection.startLineNumber,
							endColumn: selection.endColumn || selection.startColumn
						});
					}
					if (!cellOptions.options?.preserveFocus) {
						editor.focus();
					}
				}
309 310
			}
		}
R
rebornix 已提交
311
	}
312

R
rebornix 已提交
313 314 315 316 317 318 319 320
	clearInput(): void {
		if (this.input && this.input instanceof NotebookEditorInput && !this.input.isDisposed()) {
			this.saveTextEditorViewState(this.input);
		}

		super.clearInput();
	}

R
rebornix 已提交
321 322 323 324 325 326
	private detachModel() {
		this.localStore.clear();
		this.notebookViewModel?.dispose();
		this.notebookViewModel = undefined;
		this.webview?.clearInsets();
		this.webview?.clearPreloadsCache();
327
		this.findWidget.clear();
R
rebornix 已提交
328
	}
R
rebornix 已提交
329

R
rebornix 已提交
330 331 332 333 334
	private async attachModel(input: NotebookEditorInput, model: NotebookEditorModel) {
		if (!this.webview) {
			this.webview = new BackLayerWebView(this.webviewService, this.notebookService, this, this.environmentSerice);
			this.list?.rowsContainer.insertAdjacentElement('afterbegin', this.webview!.element);
		}
335

R
rebornix 已提交
336 337
		this.notebookViewModel = this.instantiationService.createInstance(NotebookViewModel, input.viewType!, model);
		const viewState = this.loadTextEditorViewState(input);
R
rebornix 已提交
338
		this.notebookViewModel.restoreEditorViewState(viewState);
339

R
rebornix 已提交
340 341 342 343 344 345 346 347 348 349 350 351
		this.localStore.add(this.notebookViewModel.onDidChangeViewCells((e) => {
			if (e.synchronous) {
				e.splices.reverse().forEach((diff) => {
					this.list?.splice(diff[0], diff[1], diff[2]);
				});
			} else {
				DOM.scheduleAtNextAnimationFrame(() => {
					e.splices.reverse().forEach((diff) => {
						this.list?.splice(diff[0], diff[1], diff[2]);
					});
				});
			}
R
rebornix 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
		}));

		this.webview?.updateRendererPreloads(this.notebookViewModel.renderers);

		this.localStore.add(this.list!.onWillScroll(e => {
			this.webview!.updateViewScrollTop(-e.scrollTop, []);
		}));

		this.localStore.add(this.list!.onDidChangeContentHeight(() => {
			const scrollTop = this.list?.scrollTop || 0;
			const scrollHeight = this.list?.scrollHeight || 0;
			this.webview!.element.style.height = `${scrollHeight}px`;
			let updateItems: { cell: CellViewModel, output: IOutput, cellTop: number }[] = [];

			if (this.webview?.insetMapping) {
				this.webview?.insetMapping.forEach((value, key) => {
					let cell = value.cell;
					let index = this.notebookViewModel!.getViewCellIndex(cell);
					let cellTop = this.list?.getAbsoluteTop(index) || 0;
					if (this.webview!.shouldUpdateInset(cell, key, cellTop)) {
						updateItems.push({
							cell: cell,
							output: key,
							cellTop: cellTop
						});
R
rebornix 已提交
377
					}
R
rebornix 已提交
378
				});
379

R
rebornix 已提交
380 381 382 383 384 385
				if (updateItems.length) {
					this.webview?.updateViewScrollTop(-scrollTop, updateItems);
				}
			}
		}));

J
Johannes Rieken 已提交
386
		this.localStore.add(this.list!.onDidChangeFocus((e) => {
R
rebornix 已提交
387
			if (e.elements.length > 0) {
R
rebornix 已提交
388
				this.notebookService.updateNotebookActiveCell(input.viewType!, input.resource!, e.elements[0].handle);
R
rebornix 已提交
389 390 391
			}
		}));

R
rebornix 已提交
392
		this.list?.splice(0, this.list?.length || 0);
393
		this.list?.splice(0, 0, this.notebookViewModel!.viewCells as CellViewModel[]);
R
rebornix 已提交
394
		this.list?.layout();
P
Peng Lyu 已提交
395 396
	}

R
rebornix 已提交
397
	private saveTextEditorViewState(input: NotebookEditorInput): void {
R
npe  
rebornix 已提交
398
		if (this.group && this.notebookViewModel) {
R
rebornix 已提交
399
			const state = this.notebookViewModel.saveEditorViewState();
R
rebornix 已提交
400
			this.editorMemento.saveEditorState(this.group, input.resource, state);
R
rebornix 已提交
401 402 403 404 405
		}
	}

	private loadTextEditorViewState(input: NotebookEditorInput): INotebookEditorViewState | undefined {
		if (this.group) {
R
rebornix 已提交
406
			return this.editorMemento.loadEditorState(this.group, input.resource);
R
rebornix 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
		}

		return;
	}

	layout(dimension: DOM.Dimension): void {
		this.dimension = new DOM.Dimension(dimension.width, dimension.height);
		DOM.toggleClass(this.rootElement, 'mid-width', dimension.width < 1000 && dimension.width >= 600);
		DOM.toggleClass(this.rootElement, 'narrow-width', dimension.width < 600);
		DOM.size(this.body, dimension.width, dimension.height);
		this.list?.layout(dimension.height, dimension.width);
	}

	protected saveState(): void {
		if (this.input instanceof NotebookEditorInput) {
			this.saveTextEditorViewState(this.input);
		}

		super.saveState();
	}

	//#endregion

R
rebornix 已提交
430 431
	//#region Editor Features

R
rebornix 已提交
432
	selectElement(cell: ICellViewModel) {
R
rebornix 已提交
433 434 435 436 437 438 439 440
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
			this.list?.setSelection([index]);
			this.list?.setFocus([index]);
		}
	}

R
rebornix 已提交
441
	revealInView(cell: ICellViewModel) {
R
rebornix 已提交
442 443 444
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
R
rebornix 已提交
445
			this.list?.revealInView(index);
R
rebornix 已提交
446 447 448
		}
	}

R
rebornix 已提交
449
	revealInCenterIfOutsideViewport(cell: ICellViewModel) {
R
rebornix 已提交
450 451 452
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
R
rebornix 已提交
453
			this.list?.revealInCenterIfOutsideViewport(index);
R
rebornix 已提交
454 455 456
		}
	}

R
rebornix 已提交
457
	revealInCenter(cell: ICellViewModel) {
R
rebornix 已提交
458 459 460
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
R
rebornix 已提交
461 462 463 464
			this.list?.revealInCenter(index);
		}
	}

R
rebornix 已提交
465
	revealLineInView(cell: ICellViewModel, line: number): void {
R
rebornix 已提交
466 467 468 469
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
			this.list?.revealLineInView(index, line);
R
rebornix 已提交
470 471
		}
	}
R
rebornix 已提交
472

R
rebornix 已提交
473
	revealLineInCenter(cell: ICellViewModel, line: number) {
R
rebornix 已提交
474 475 476
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
R
rebornix 已提交
477
			this.list?.revealLineInCenter(index, line);
R
rebornix 已提交
478 479 480
		}
	}

R
rebornix 已提交
481
	revealLineInCenterIfOutsideViewport(cell: ICellViewModel, line: number) {
R
rebornix 已提交
482 483 484 485 486 487 488
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
			this.list?.revealLineInCenterIfOutsideViewport(index, line);
		}
	}

R
rebornix 已提交
489
	revealRangeInView(cell: ICellViewModel, range: Range): void {
R
rebornix 已提交
490 491 492 493 494 495 496
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
			this.list?.revealRangeInView(index, range);
		}
	}

R
rebornix 已提交
497
	revealRangeInCenter(cell: ICellViewModel, range: Range): void {
R
rebornix 已提交
498 499 500 501 502 503 504
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
			this.list?.revealRangeInCenter(index, range);
		}
	}

R
rebornix 已提交
505
	revealRangeInCenterIfOutsideViewport(cell: ICellViewModel, range: Range): void {
R
rebornix 已提交
506 507 508 509 510 511 512
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
			this.list?.revealRangeInCenterIfOutsideViewport(index, range);
		}
	}

R
rebornix 已提交
513
	setCellSelection(cell: ICellViewModel, range: Range): void {
R
rebornix 已提交
514 515 516 517 518 519 520
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
			this.list?.setCellSelection(index, range);
		}
	}

R
rebornix 已提交
521 522 523 524 525 526
	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any {
		return this.notebookViewModel?.changeDecorations(callback);
	}

	//#endregion

R
rebornix 已提交
527 528 529 530 531 532 533 534
	//#region Find Delegate

	public showFind() {
		this.findWidget.reveal();
	}

	public hideFind() {
		this.findWidget.hide();
R
rebornix 已提交
535
		this.focus();
R
rebornix 已提交
536 537 538 539
	}

	//#endregion

R
rebornix 已提交
540
	//#region Cell operations
R
rebornix 已提交
541 542
	layoutNotebookCell(cell: ICellViewModel, height: number) {
		let relayout = (cell: ICellViewModel, height: number) => {
R
rebornix 已提交
543
			let index = this.notebookViewModel!.getViewCellIndex(cell);
R
rebornix 已提交
544
			if (index >= 0) {
R
rebornix 已提交
545
				this.list?.updateElementHeight(index, height);
R
rebornix 已提交
546
			}
R
rebornix 已提交
547 548
		};

R
rebornix 已提交
549
		DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
550
			relayout(cell, height);
R
rebornix 已提交
551
		});
552 553
	}

R
rebornix 已提交
554
	async insertNotebookCell(cell: ICellViewModel, type: CellKind, direction: 'above' | 'below', initialText: string = ''): Promise<void> {
R
rebornix 已提交
555 556 557
		const newLanguages = this.notebookViewModel!.languages;
		const language = newLanguages && newLanguages.length ? newLanguages[0] : 'markdown';
		const index = this.notebookViewModel!.getViewCellIndex(cell);
P
Peng Lyu 已提交
558
		const insertIndex = direction === 'above' ? index : index + 1;
R
rebornix 已提交
559
		const newModeCell = await this.notebookService.createNotebookCell(this.notebookViewModel!.viewType, this.notebookViewModel!.uri, insertIndex, language, type);
560
		newModeCell!.source = initialText.split(/\r?\n/g);
R
rebornix 已提交
561
		const newCell = this.notebookViewModel!.insertCell(insertIndex, newModeCell!, true);
R
rebornix 已提交
562
		this.list?.setFocus([insertIndex]);
P
Peng Lyu 已提交
563

R
rebornix 已提交
564
		if (type === CellKind.Markdown) {
R
rebornix 已提交
565
			newCell.state = CellState.Editing;
P
Peng Lyu 已提交
566
		}
R
rebornix 已提交
567 568

		DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
569
			this.list?.revealInCenterIfOutsideViewport(insertIndex);
R
rebornix 已提交
570
		});
P
Peng Lyu 已提交
571 572
	}

R
rebornix 已提交
573
	async deleteNotebookCell(cell: ICellViewModel): Promise<void> {
574
		(cell as CellViewModel).save();
R
rebornix 已提交
575
		const index = this.notebookViewModel!.getViewCellIndex(cell);
R
rebornix 已提交
576
		await this.notebookService.deleteNotebookCell(this.notebookViewModel!.viewType, this.notebookViewModel!.uri, index);
R
rebornix 已提交
577
		this.notebookViewModel!.deleteCell(index, true);
R
rebornix 已提交
578 579
	}

580
	moveCellDown(cell: ICellViewModel): void {
581 582 583 584 585
		const index = this.notebookViewModel!.getViewCellIndex(cell);
		const newIdx = index + 1;
		this.moveCellToIndex(cell, index, newIdx);
	}

586
	moveCellUp(cell: ICellViewModel): void {
587 588 589 590 591
		const index = this.notebookViewModel!.getViewCellIndex(cell);
		const newIdx = index - 1;
		this.moveCellToIndex(cell, index, newIdx);
	}

592
	private moveCellToIndex(cell: ICellViewModel, index: number, newIdx: number): void {
R
rebornix 已提交
593
		if (!this.notebookViewModel!.moveCellToIdx(index, newIdx, true)) {
594 595 596 597 598 599 600 601
			return;
		}

		DOM.scheduleAtNextAnimationFrame(() => {
			this.list?.revealInCenterIfOutsideViewport(index + 1);
		});
	}

R
rebornix 已提交
602
	editNotebookCell(cell: CellViewModel): void {
R
rebornix 已提交
603
		cell.state = CellState.Editing;
R
rebornix 已提交
604 605

		this.renderedEditors.get(cell)?.focus();
P
Peng Lyu 已提交
606 607
	}

R
rebornix 已提交
608
	saveNotebookCell(cell: ICellViewModel): void {
609
		cell.state = CellState.Preview;
P
Peng Lyu 已提交
610 611
	}

R
rebornix 已提交
612 613 614 615 616 617 618 619 620 621
	getActiveCell() {
		let elements = this.list?.getFocusedElements();

		if (elements && elements.length) {
			return elements[0];
		}

		return undefined;
	}

R
rebornix 已提交
622
	focusNotebookCell(cell: ICellViewModel, focusEditor: boolean) {
R
rebornix 已提交
623
		const index = this.notebookViewModel!.getViewCellIndex(cell);
R
rebornix 已提交
624 625

		if (focusEditor) {
R
rebornix 已提交
626 627 628
			this.list?.setFocus([index]);
			this.list?.setSelection([index]);
			this.list?.focusView();
R
rebornix 已提交
629

630
			cell.state = CellState.Editing;
R
rebornix 已提交
631
			cell.focusMode = CellFocusMode.Editor;
632
			this.revealInCenterIfOutsideViewport(cell);
R
rebornix 已提交
633
		} else {
R
rebornix 已提交
634
			let itemDOM = this.list?.domElementAtIndex(index);
R
rebornix 已提交
635 636 637
			if (document.activeElement && itemDOM && itemDOM.contains(document.activeElement)) {
				(document.activeElement as HTMLElement).blur();
			}
638

639 640
			cell.state = CellState.Preview;
			cell.focusMode = CellFocusMode.Editor;
R
rebornix 已提交
641

R
rebornix 已提交
642 643
			this.list?.setFocus([index]);
			this.list?.setSelection([index]);
644
			this.revealInCenterIfOutsideViewport(cell);
R
rebornix 已提交
645
			this.list?.focusView();
R
rebornix 已提交
646 647 648
		}
	}

R
rebornix 已提交
649 650 651 652
	//#endregion

	//#region MISC

R
rebornix 已提交
653 654 655 656 657 658 659 660 661 662 663
	getLayoutInfo(): NotebookLayoutInfo {
		if (!this.list) {
			throw new Error('Editor is not initalized successfully');
		}

		return {
			width: this.dimension!.width,
			height: this.dimension!.height,
			fontInfo: this.fontInfo!
		};
	}
R
rebornix 已提交
664 665
	getFontInfo(): BareFontInfo | undefined {
		return this.fontInfo;
P
Peng Lyu 已提交
666
	}
R
rebornix 已提交
667

R
rebornix 已提交
668 669
	triggerScroll(event: IMouseWheelEvent) {
		this.list?.triggerScrollFromMouseWheelEvent(event);
R
rebornix 已提交
670 671
	}

672
	createInset(cell: CellViewModel, output: IOutput, shadowContent: string, offset: number) {
R
rebornix 已提交
673 674
		if (!this.webview) {
			return;
R
rebornix 已提交
675 676
		}

R
rebornix 已提交
677
		let preloads = this.notebookViewModel!.renderers;
R
rebornix 已提交
678

679
		if (!this.webview!.insetMapping.has(output)) {
R
rebornix 已提交
680
			let index = this.notebookViewModel!.getViewCellIndex(cell);
681 682 683
			let cellTop = this.list?.getAbsoluteTop(index) || 0;

			this.webview!.createInset(cell, output, cellTop, offset, shadowContent, preloads);
R
rebornix 已提交
684
		} else {
R
rebornix 已提交
685
			let index = this.notebookViewModel!.getViewCellIndex(cell);
686
			let cellTop = this.list?.getAbsoluteTop(index) || 0;
R
rebornix 已提交
687 688
			let scrollTop = this.list?.scrollTop || 0;

689
			this.webview!.updateViewScrollTop(-scrollTop, [{ cell: cell, output: output, cellTop: cellTop }]);
R
rebornix 已提交
690
		}
R
rebornix 已提交
691
	}
R
rebornix 已提交
692

R
rebornix 已提交
693 694 695 696 697 698 699 700
	removeInset(output: IOutput) {
		if (!this.webview) {
			return;
		}

		this.webview!.removeInset(output);
	}

R
rebornix 已提交
701 702
	getOutputRenderer(): OutputRenderer {
		return this.outputRenderer;
R
rebornix 已提交
703
	}
704

705 706 707 708
	postMessage(message: any) {
		this.webview?.webview.sendMessage(message);
	}

R
rebornix 已提交
709
	//#endregion
710 711 712 713 714 715

	toJSON(): any {
		return {
			notebookHandle: this.viewModel?.handle
		};
	}
P
Peng Lyu 已提交
716 717 718 719
}

const embeddedEditorBackground = 'walkThrough.embeddedEditorBackground';

720 721 722 723 724 725 726
export const focusedCellIndicator = registerColor('notebook.focusedCellIndicator', {
	light: new Color(new RGBA(102, 175, 224)),
	dark: new Color(new RGBA(12, 125, 157)),
	hc: new Color(new RGBA(0, 73, 122))
}, nls.localize('notebook.focusedCellIndicator', "The color of the focused notebook cell indicator."));


P
Peng Lyu 已提交
727 728 729
registerThemingParticipant((theme, collector) => {
	const color = getExtraColor(theme, embeddedEditorBackground, { dark: 'rgba(0, 0, 0, .4)', extra_dark: 'rgba(200, 235, 255, .064)', light: '#f4f4f4', hc: null });
	if (color) {
R
rebornix 已提交
730 731
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .cell .monaco-editor-background,
			.monaco-workbench .part.editor > .content .notebook-editor .cell .margin-view-overlays { background: ${color}; }`);
P
Peng Lyu 已提交
732 733 734
	}
	const link = theme.getColor(textLinkForeground);
	if (link) {
R
Rob Lourens 已提交
735
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .cell .output a { color: ${link}; }`);
P
Peng Lyu 已提交
736 737 738
	}
	const activeLink = theme.getColor(textLinkActiveForeground);
	if (activeLink) {
R
Rob Lourens 已提交
739 740
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .cell .output a:hover,
			.monaco-workbench .part.editor > .content .notebook-editor .cell .output a:active { color: ${activeLink}; }`);
P
Peng Lyu 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	}
	const shortcut = theme.getColor(textPreformatForeground);
	if (shortcut) {
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor code,
			.monaco-workbench .part.editor > .content .notebook-editor .shortcut { color: ${shortcut}; }`);
	}
	const border = theme.getColor(contrastBorder);
	if (border) {
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .monaco-editor { border-color: ${border}; }`);
	}
	const quoteBackground = theme.getColor(textBlockQuoteBackground);
	if (quoteBackground) {
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor blockquote { background: ${quoteBackground}; }`);
	}
	const quoteBorder = theme.getColor(textBlockQuoteBorder);
	if (quoteBorder) {
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor blockquote { border-color: ${quoteBorder}; }`);
	}
759 760 761 762 763 764

	const inactiveListItem = theme.getColor('list.inactiveSelectionBackground');

	if (inactiveListItem) {
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .output { background-color: ${inactiveListItem}; }`);
	}
765

766 767 768 769 770 771
	const focusedCellIndicatorColor = theme.getColor(focusedCellIndicator);
	if (focusedCellIndicatorColor) {
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .monaco-list-row.focused .notebook-cell-focus-indicator { border-color: ${focusedCellIndicatorColor}; }`);
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .monaco-list-row.selected .notebook-cell-focus-indicator { border-color: ${focusedCellIndicatorColor}; }`);
	}

772 773
	// Cell Margin
	collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .monaco-list-row > div.cell { padding: 8px ${CELL_MARGIN}px 8px ${CELL_MARGIN}px; }`);
R
Rob Lourens 已提交
774 775 776
	collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .output { margin: 8px ${CELL_MARGIN}px 8px ${CELL_MARGIN + RUN_BUTTON_WIDTH}px }`);

	collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .cell .cell-editor-container { width: calc(100% - ${RUN_BUTTON_WIDTH}px); }`);
P
Peng Lyu 已提交
777
});