notebookEditor.ts 25.7 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.
 *--------------------------------------------------------------------------------------------*/

R
rebornix 已提交
6
import { getZoomLevel } from 'vs/base/browser/browser';
P
Peng Lyu 已提交
7
import * as DOM from 'vs/base/browser/dom';
P
Peng Lyu 已提交
8
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
R
rebornix 已提交
9
import { CancellationToken } from 'vs/base/common/cancellation';
R
rebornix 已提交
10
import { DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle';
R
rebornix 已提交
11
import 'vs/css!./notebook';
12
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
R
rebornix 已提交
13
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
14
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
R
Rob Lourens 已提交
15
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
R
rebornix 已提交
16
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
R
rebornix 已提交
17 18 19 20 21 22
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { contrastBorder, editorBackground, focusBorder, foreground, textBlockQuoteBackground, textBlockQuoteBorder, textLinkActiveForeground, textLinkForeground, textPreformatForeground } from 'vs/platform/theme/common/colorRegistry';
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
R
rebornix 已提交
23
import { EditorOptions, IEditorMemento, ICompositeCodeEditor, IEditorCloseEvent } from 'vs/workbench/common/editor';
R
Rob Lourens 已提交
24
import { INotebookEditor, NotebookLayoutInfo, CellState, NOTEBOOK_EDITOR_FOCUSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
R
rebornix 已提交
25
import { NotebookEditorInput, NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
R
rebornix 已提交
26
import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
R
rebornix 已提交
27 28 29
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';
30
import { CELL_MARGIN, NotebookCellsSplice, IOutput, CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
31 32 33
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 已提交
34
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
35
import { IEditor } from 'vs/editor/common/editorCommon';
36
import { IResourceEditorInput } from 'vs/platform/editor/common/editor';
37
import { Emitter, Event } from 'vs/base/common/event';
R
rebornix 已提交
38 39 40
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 已提交
41
import { IEditorGroupView } from 'vs/workbench/browser/parts/editor/editor';
R
rebornix 已提交
42
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel';
R
rebornix 已提交
43
import { Range } from 'vs/editor/common/core/range';
P
Peng Lyu 已提交
44 45

const $ = DOM.$;
R
rebornix 已提交
46 47
const NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY = 'NotebookEditorViewState';

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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 已提交
63
export class NotebookCodeEditors implements ICompositeCodeEditor {
64

65
	private readonly _disposables = new DisposableStore();
66 67 68 69
	private readonly _onDidChangeActiveEditor = new Emitter<this>();
	readonly onDidChangeActiveEditor: Event<this> = this._onDidChangeActiveEditor.event;

	constructor(
R
rebornix 已提交
70
		private _list: NotebookCellList,
71
		private _renderedEditors: Map<CellViewModel, ICodeEditor | undefined>
72
	) {
J
Johannes Rieken 已提交
73
		_list.onDidChangeFocus(e => this._onDidChangeActiveEditor.fire(this), undefined, this._disposables);
74 75 76 77 78 79
	}

	dispose(): void {
		this._onDidChangeActiveEditor.dispose();
		this._disposables.dispose();
	}
80 81 82 83 84 85 86 87 88

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

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

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

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

R
rebornix 已提交
127 128 129 130
	get viewModel() {
		return this.notebookViewModel;
	}

P
Peng Lyu 已提交
131 132 133 134 135 136 137 138
	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 已提交
139
	//#region Editor Core
R
rebornix 已提交
140

R
rebornix 已提交
141 142 143 144 145

	public get isNotebookEditor() {
		return true;
	}

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

		this._register(this.onDidBlur(() => {
			this.editorFocus?.set(false);
		}));
158 159 160 161 162
	}

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

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

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

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

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

216
		this.control = new NotebookCodeEditors(this.list, this.renderedEditors);
R
rebornix 已提交
217
		this.webview = new BackLayerWebView(this.webviewService, this.notebookService, this, this.environmentSerice);
R
rebornix 已提交
218
		this.list.rowsContainer.appendChild(this.webview.element);
P
Peng Lyu 已提交
219 220 221
		this._register(this.list);
	}

222 223 224 225
	getControl() {
		return this.control;
	}

P
Peng Lyu 已提交
226
	onHide() {
R
rebornix 已提交
227
		this.editorFocus?.set(false);
228 229
		if (this.webview) {
			this.localStore.clear();
R
rebornix 已提交
230
			this.list?.rowsContainer.removeChild(this.webview?.element);
231 232 233 234 235
			this.webview?.dispose();
			this.webview = null;
		}

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

R
rebornix 已提交
237
		if (this.notebookViewModel && !this.notebookViewModel.isDirty()) {
R
rebornix 已提交
238
			this.notebookService.destoryNotebookDocument(this.notebookViewModel.viewType!, this.notebookViewModel!.notebookDocument);
R
rebornix 已提交
239
			this.notebookViewModel.dispose();
R
rebornix 已提交
240
			this.notebookViewModel = undefined;
R
rebornix 已提交
241 242
		}

243
		super.onHide();
P
Peng Lyu 已提交
244 245
	}

R
rebornix 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258
	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);
259 260 261
		}
	}

R
rebornix 已提交
262 263 264 265 266
	focus() {
		super.focus();
		this.editorFocus?.set(true);
	}

R
rebornix 已提交
267
	async setInput(input: NotebookEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
R
rebornix 已提交
268 269 270 271
		if (this.input instanceof NotebookEditorInput) {
			this.saveTextEditorViewState(this.input);
		}

R
rebornix 已提交
272 273
		await super.setInput(input, options, token);
		const model = await input.resolve();
P
Peng Lyu 已提交
274

275 276 277
		if (this.notebookViewModel === undefined || !this.notebookViewModel.equal(model) || this.webview === null) {
			this.detachModel();
			await this.attachModel(input, model);
R
rebornix 已提交
278
		}
P
Peng Lyu 已提交
279

280 281 282
		// reveal cell if editor options tell to do so
		if (options instanceof NotebookEditorOptions && options.cellOptions) {
			const cellOptions = options.cellOptions;
J
Johannes Rieken 已提交
283
			const cell = this.notebookViewModel!.viewCells.find(cell => cell.cell.uri.toString() === cellOptions.resource.toString());
284
			if (cell) {
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
				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();
					}
				}
300 301
			}
		}
R
rebornix 已提交
302
	}
303

R
rebornix 已提交
304 305 306 307 308 309 310 311
	clearInput(): void {
		if (this.input && this.input instanceof NotebookEditorInput && !this.input.isDisposed()) {
			this.saveTextEditorViewState(this.input);
		}

		super.clearInput();
	}

R
rebornix 已提交
312 313 314 315 316 317 318
	private detachModel() {
		this.localStore.clear();
		this.notebookViewModel?.dispose();
		this.notebookViewModel = undefined;
		this.webview?.clearInsets();
		this.webview?.clearPreloadsCache();
	}
R
rebornix 已提交
319

R
rebornix 已提交
320 321 322 323 324
	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);
		}
325

R
rebornix 已提交
326 327
		this.notebookViewModel = this.instantiationService.createInstance(NotebookViewModel, input.viewType!, model);
		const viewState = this.loadTextEditorViewState(input);
R
rebornix 已提交
328
		this.notebookViewModel.restoreEditorViewState(viewState);
329

R
rebornix 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
		this.localStore.add(this.notebookViewModel.onDidChangeCells((e) => {
			this.updateViewCells(e);
		}));

		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 已提交
357
					}
R
rebornix 已提交
358
				});
359

R
rebornix 已提交
360 361 362 363 364 365
				if (updateItems.length) {
					this.webview?.updateViewScrollTop(-scrollTop, updateItems);
				}
			}
		}));

J
Johannes Rieken 已提交
366
		this.localStore.add(this.list!.onDidChangeFocus((e) => {
R
rebornix 已提交
367 368 369 370 371
			if (e.elements.length > 0) {
				this.notebookService.updateNotebookActiveCell(input.viewType!, input.resource!, e.elements[0].cell.handle);
			}
		}));

R
rebornix 已提交
372
		this.list?.splice(0, this.list?.length || 0);
R
rebornix 已提交
373 374
		this.list?.splice(0, 0, this.notebookViewModel!.viewCells);
		this.list?.layout();
P
Peng Lyu 已提交
375 376
	}

R
rebornix 已提交
377
	private saveTextEditorViewState(input: NotebookEditorInput): void {
R
npe  
rebornix 已提交
378
		if (this.group && this.notebookViewModel) {
R
rebornix 已提交
379
			const state = this.notebookViewModel.saveEditorViewState();
R
rebornix 已提交
380
			this.editorMemento.saveEditorState(this.group, input.resource, state);
R
rebornix 已提交
381 382 383 384 385
		}
	}

	private loadTextEditorViewState(input: NotebookEditorInput): INotebookEditorViewState | undefined {
		if (this.group) {
R
rebornix 已提交
386
			return this.editorMemento.loadEditorState(this.group, input.resource);
R
rebornix 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
		}

		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 已提交
410 411
	//#region Editor Features

R
rebornix 已提交
412 413 414 415 416 417 418 419 420
	selectElement(cell: CellViewModel) {
		const index = this.notebookViewModel?.getViewCellIndex(cell);

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

R
rebornix 已提交
421
	revealInView(cell: CellViewModel) {
R
rebornix 已提交
422 423 424
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
R
rebornix 已提交
425
			this.list?.revealInView(index);
R
rebornix 已提交
426 427 428
		}
	}

R
rebornix 已提交
429
	revealInCenterIfOutsideViewport(cell: CellViewModel) {
R
rebornix 已提交
430 431 432
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
R
rebornix 已提交
433
			this.list?.revealInCenterIfOutsideViewport(index);
R
rebornix 已提交
434 435 436
		}
	}

R
rebornix 已提交
437
	revealInCenter(cell: CellViewModel) {
R
rebornix 已提交
438 439 440
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
R
rebornix 已提交
441 442 443 444 445 446 447 448 449
			this.list?.revealInCenter(index);
		}
	}

	revealLineInView(cell: CellViewModel, line: number): void {
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
			this.list?.revealLineInView(index, line);
R
rebornix 已提交
450 451
		}
	}
R
rebornix 已提交
452

R
rebornix 已提交
453 454 455 456
	revealLineInCenter(cell: CellViewModel, line: number) {
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
R
rebornix 已提交
457
			this.list?.revealLineInCenter(index, line);
R
rebornix 已提交
458 459 460 461 462 463 464 465 466 467 468
		}
	}

	revealLineInCenterIfOutsideViewport(cell: CellViewModel, line: number) {
		const index = this.notebookViewModel?.getViewCellIndex(cell);

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

R
rebornix 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	revealRangeInView(cell: CellViewModel, range: Range): void {
		const index = this.notebookViewModel?.getViewCellIndex(cell);

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

	revealRangeInCenter(cell: CellViewModel, range: Range): void {
		const index = this.notebookViewModel?.getViewCellIndex(cell);

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

	revealRangeInCenterIfOutsideViewport(cell: CellViewModel, range: Range): void {
		const index = this.notebookViewModel?.getViewCellIndex(cell);

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

R
rebornix 已提交
493
	setCellSelection(cell: CellViewModel, range: Range): void {
R
rebornix 已提交
494 495 496 497 498 499 500
		const index = this.notebookViewModel?.getViewCellIndex(cell);

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

R
rebornix 已提交
501 502 503 504 505 506
	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any {
		return this.notebookViewModel?.changeDecorations(callback);
	}

	//#endregion

R
rebornix 已提交
507 508 509 510 511 512 513 514
	//#region Find Delegate

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

	public hideFind() {
		this.findWidget.hide();
R
rebornix 已提交
515
		this.focus();
R
rebornix 已提交
516 517 518 519
	}

	//#endregion

R
rebornix 已提交
520
	//#region Cell operations
521
	layoutNotebookCell(cell: CellViewModel, height: number) {
R
rebornix 已提交
522
		let relayout = (cell: CellViewModel, height: number) => {
R
rebornix 已提交
523
			let index = this.notebookViewModel!.getViewCellIndex(cell);
R
rebornix 已提交
524
			if (index >= 0) {
R
rebornix 已提交
525
				this.list?.updateElementHeight(index, height);
R
rebornix 已提交
526
			}
R
rebornix 已提交
527 528
		};

R
rebornix 已提交
529
		DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
530
			relayout(cell, height);
R
rebornix 已提交
531
		});
532 533
	}

534
	updateViewCells(splices: NotebookCellsSplice[]) {
R
rebornix 已提交
535
		DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
536 537
			splices.reverse().forEach((diff) => {
				this.list?.splice(diff[0], diff[1], diff[2].map(cell => {
R
rebornix 已提交
538
					return this.instantiationService.createInstance(CellViewModel, this.notebookViewModel!.viewType, this.notebookViewModel!.handle, cell);
R
rebornix 已提交
539 540
				}));
			});
541
		});
R
rebornix 已提交
542 543
	}

544
	async insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below', initialText: string = ''): Promise<void> {
R
rebornix 已提交
545 546 547
		const newLanguages = this.notebookViewModel!.languages;
		const language = newLanguages && newLanguages.length ? newLanguages[0] : 'markdown';
		const index = this.notebookViewModel!.getViewCellIndex(cell);
P
Peng Lyu 已提交
548
		const insertIndex = direction === 'above' ? index : index + 1;
R
rebornix 已提交
549
		const newModeCell = await this.notebookService.createNotebookCell(this.notebookViewModel!.viewType, this.notebookViewModel!.uri, insertIndex, language, type);
550
		newModeCell!.source = initialText.split(/\r?\n/g);
551
		const newCell = this.notebookViewModel!.insertCell(insertIndex, newModeCell!);
P
Peng Lyu 已提交
552 553

		this.list?.splice(insertIndex, 0, [newCell]);
R
rebornix 已提交
554
		this.list?.setFocus([insertIndex]);
P
Peng Lyu 已提交
555

R
rebornix 已提交
556
		if (type === CellKind.Markdown) {
R
rebornix 已提交
557
			newCell.state = CellState.Editing;
P
Peng Lyu 已提交
558
		}
R
rebornix 已提交
559 560

		DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
561
			this.list?.revealInCenterIfOutsideViewport(insertIndex);
R
rebornix 已提交
562
		});
P
Peng Lyu 已提交
563 564
	}

R
rebornix 已提交
565 566
	async deleteNotebookCell(cell: CellViewModel): Promise<void> {
		const index = this.notebookViewModel!.getViewCellIndex(cell);
R
rebornix 已提交
567
		await this.notebookService.deleteNotebookCell(this.notebookViewModel!.viewType, this.notebookViewModel!.uri, index);
R
rebornix 已提交
568 569 570 571
		this.notebookViewModel!.deleteCell(index);
		this.list?.splice(index, 1);
	}

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	moveCellDown(cell: CellViewModel): void {
		const index = this.notebookViewModel!.getViewCellIndex(cell);
		const newIdx = index + 1;
		this.moveCellToIndex(cell, index, newIdx);
	}

	moveCellUp(cell: CellViewModel): void {
		const index = this.notebookViewModel!.getViewCellIndex(cell);
		const newIdx = index - 1;
		this.moveCellToIndex(cell, index, newIdx);
	}

	private moveCellToIndex(cell: CellViewModel, index: number, newIdx: number): void {
		if (!this.notebookViewModel!.moveCellToIdx(index, newIdx)) {
			return;
		}

		this.list?.splice(index, 1);
		this.list!.splice(newIdx, 0, [cell]);

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

R
rebornix 已提交
597
	editNotebookCell(cell: CellViewModel): void {
R
rebornix 已提交
598
		cell.state = CellState.Editing;
R
rebornix 已提交
599 600

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

R
rebornix 已提交
603
	saveNotebookCell(cell: CellViewModel): void {
R
rebornix 已提交
604
		cell.state = CellState.Read;
P
Peng Lyu 已提交
605 606
	}

R
rebornix 已提交
607 608 609 610 611 612 613 614 615 616
	getActiveCell() {
		let elements = this.list?.getFocusedElements();

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

		return undefined;
	}

R
rebornix 已提交
617
	focusNotebookCell(cell: CellViewModel, focusEditor: boolean) {
R
rebornix 已提交
618
		const index = this.notebookViewModel!.getViewCellIndex(cell);
R
rebornix 已提交
619 620 621 622

		if (focusEditor) {

		} else {
R
rebornix 已提交
623
			let itemDOM = this.list?.domElementAtIndex(index);
R
rebornix 已提交
624 625 626
			if (document.activeElement && itemDOM && itemDOM.contains(document.activeElement)) {
				(document.activeElement as HTMLElement).blur();
			}
627

R
rebornix 已提交
628
			cell.state = CellState.Read;
R
rebornix 已提交
629 630 631
		}

		this.list?.setFocus([index]);
R
rebornix 已提交
632
		this.list?.focusView();
R
rebornix 已提交
633 634
	}

R
rebornix 已提交
635 636 637 638
	//#endregion

	//#region MISC

R
rebornix 已提交
639 640 641 642 643 644 645 646 647 648 649
	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 已提交
650 651
	getFontInfo(): BareFontInfo | undefined {
		return this.fontInfo;
P
Peng Lyu 已提交
652
	}
R
rebornix 已提交
653

R
rebornix 已提交
654 655
	triggerScroll(event: IMouseWheelEvent) {
		this.list?.triggerScrollFromMouseWheelEvent(event);
R
rebornix 已提交
656 657
	}

658
	createInset(cell: CellViewModel, output: IOutput, shadowContent: string, offset: number) {
R
rebornix 已提交
659 660
		if (!this.webview) {
			return;
R
rebornix 已提交
661 662
		}

R
rebornix 已提交
663
		let preloads = this.notebookViewModel!.renderers;
R
rebornix 已提交
664

665
		if (!this.webview!.insetMapping.has(output)) {
R
rebornix 已提交
666
			let index = this.notebookViewModel!.getViewCellIndex(cell);
667 668 669
			let cellTop = this.list?.getAbsoluteTop(index) || 0;

			this.webview!.createInset(cell, output, cellTop, offset, shadowContent, preloads);
R
rebornix 已提交
670
		} else {
R
rebornix 已提交
671
			let index = this.notebookViewModel!.getViewCellIndex(cell);
672
			let cellTop = this.list?.getAbsoluteTop(index) || 0;
R
rebornix 已提交
673 674
			let scrollTop = this.list?.scrollTop || 0;

675
			this.webview!.updateViewScrollTop(-scrollTop, [{ cell: cell, output: output, cellTop: cellTop }]);
R
rebornix 已提交
676
		}
R
rebornix 已提交
677
	}
R
rebornix 已提交
678

R
rebornix 已提交
679 680 681 682 683 684 685 686
	removeInset(output: IOutput) {
		if (!this.webview) {
			return;
		}

		this.webview!.removeInset(output);
	}

R
rebornix 已提交
687 688
	getOutputRenderer(): OutputRenderer {
		return this.outputRenderer;
R
rebornix 已提交
689
	}
690

R
rebornix 已提交
691
	//#endregion
P
Peng Lyu 已提交
692 693 694 695 696 697 698
}

const embeddedEditorBackground = 'walkThrough.embeddedEditorBackground';

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 已提交
699 700
		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 已提交
701 702 703
	}
	const link = theme.getColor(textLinkForeground);
	if (link) {
704
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .cell a { color: ${link}; }`);
P
Peng Lyu 已提交
705 706 707
	}
	const activeLink = theme.getColor(textLinkActiveForeground);
	if (activeLink) {
708 709
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .cell a:hover,
			.monaco-workbench .part.editor > .content .notebook-editor .cell a:active { color: ${activeLink}; }`);
P
Peng Lyu 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	}
	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}; }`);
	}
728 729 730 731 732 733

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

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

	// 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; }`);
737
	collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .output { margin: 8px ${CELL_MARGIN}px; }`);
P
Peng Lyu 已提交
738
});