notebookEditor.ts 23.8 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
rebornix 已提交
15
import { IContextKey, IContextKeyService, RawContextKey } 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
rebornix 已提交
24
import { INotebookEditor, NotebookLayoutInfo } 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';
43
import { isEqual } from 'vs/base/common/resources';
P
Peng Lyu 已提交
44 45

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

R
rebornix 已提交
48 49
export const NOTEBOOK_EDITOR_FOCUSED = new RawContextKey<boolean>('notebookEditorFocused', false);

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

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

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

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

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

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

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

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

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

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

R
rebornix 已提交
143 144 145 146 147

	public get isNotebookEditor() {
		return true;
	}

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

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

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

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

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

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

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

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

224 225 226 227
	getControl() {
		return this.control;
	}

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

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

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

245
		super.onHide();
P
Peng Lyu 已提交
246 247
	}

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

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

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

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

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

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

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

		super.clearInput();
	}

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

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

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

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

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

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

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

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

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

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

R
rebornix 已提交
414
	revealInView(cell: CellViewModel) {
R
rebornix 已提交
415 416 417
		const index = this.notebookViewModel?.getViewCellIndex(cell);

		if (index !== undefined) {
R
rebornix 已提交
418
			this.list?.revealInView(index);
R
rebornix 已提交
419 420 421
		}
	}

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

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

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

		if (index !== undefined) {
R
rebornix 已提交
434 435 436 437 438 439 440 441 442
			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 已提交
443 444
		}
	}
R
rebornix 已提交
445

R
rebornix 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	revealLineInCenter(cell: CellViewModel, line: number) {
		const index = this.notebookViewModel?.getViewCellIndex(cell);

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

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

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

R
rebornix 已提交
462 463 464 465 466 467
	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any {
		return this.notebookViewModel?.changeDecorations(callback);
	}

	//#endregion

R
rebornix 已提交
468 469 470 471 472 473 474 475
	//#region Find Delegate

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

	public hideFind() {
		this.findWidget.hide();
R
rebornix 已提交
476
		this.focus();
R
rebornix 已提交
477 478 479 480
	}

	//#endregion

R
rebornix 已提交
481
	//#region Cell operations
482
	layoutNotebookCell(cell: CellViewModel, height: number) {
R
rebornix 已提交
483
		let relayout = (cell: CellViewModel, height: number) => {
R
rebornix 已提交
484
			let index = this.notebookViewModel!.getViewCellIndex(cell);
R
rebornix 已提交
485
			if (index >= 0) {
R
rebornix 已提交
486
				this.list?.updateElementHeight(index, height);
R
rebornix 已提交
487
			}
R
rebornix 已提交
488 489
		};

R
rebornix 已提交
490
		DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
491
			relayout(cell, height);
R
rebornix 已提交
492
		});
493 494
	}

495
	updateViewCells(splices: NotebookCellsSplice[]) {
R
rebornix 已提交
496
		DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
497 498
			splices.reverse().forEach((diff) => {
				this.list?.splice(diff[0], diff[1], diff[2].map(cell => {
R
rebornix 已提交
499
					return this.instantiationService.createInstance(CellViewModel, this.notebookViewModel!.viewType, this.notebookViewModel!.handle, cell);
R
rebornix 已提交
500 501
				}));
			});
502
		});
R
rebornix 已提交
503 504
	}

R
rebornix 已提交
505
	async insertEmptyNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise<void> {
R
rebornix 已提交
506 507 508
		const newLanguages = this.notebookViewModel!.languages;
		const language = newLanguages && newLanguages.length ? newLanguages[0] : 'markdown';
		const index = this.notebookViewModel!.getViewCellIndex(cell);
P
Peng Lyu 已提交
509
		const insertIndex = direction === 'above' ? index : index + 1;
R
rebornix 已提交
510
		const newModeCell = await this.notebookService.createNotebookCell(this.notebookViewModel!.viewType, this.notebookViewModel!.uri, insertIndex, language, type);
511
		const newCell = this.notebookViewModel!.insertCell(insertIndex, newModeCell!);
P
Peng Lyu 已提交
512 513

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

R
rebornix 已提交
516
		if (type === CellKind.Markdown) {
P
Peng Lyu 已提交
517 518
			newCell.isEditing = true;
		}
R
rebornix 已提交
519 520

		DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
521
			this.list?.revealInCenterIfOutsideViewport(insertIndex);
R
rebornix 已提交
522
		});
P
Peng Lyu 已提交
523 524
	}

R
rebornix 已提交
525 526
	async deleteNotebookCell(cell: CellViewModel): Promise<void> {
		const index = this.notebookViewModel!.getViewCellIndex(cell);
R
rebornix 已提交
527
		await this.notebookService.deleteNotebookCell(this.notebookViewModel!.viewType, this.notebookViewModel!.uri, index);
R
rebornix 已提交
528 529 530 531 532
		this.notebookViewModel!.deleteCell(index);
		this.list?.splice(index, 1);
	}

	editNotebookCell(cell: CellViewModel): void {
P
Peng Lyu 已提交
533
		cell.isEditing = true;
R
rebornix 已提交
534 535

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

R
rebornix 已提交
538
	saveNotebookCell(cell: CellViewModel): void {
P
Peng Lyu 已提交
539
		cell.isEditing = false;
P
Peng Lyu 已提交
540 541
	}

R
rebornix 已提交
542 543 544 545 546 547 548 549 550 551
	getActiveCell() {
		let elements = this.list?.getFocusedElements();

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

		return undefined;
	}

R
rebornix 已提交
552
	focusNotebookCell(cell: CellViewModel, focusEditor: boolean) {
R
rebornix 已提交
553
		const index = this.notebookViewModel!.getViewCellIndex(cell);
R
rebornix 已提交
554 555 556 557

		if (focusEditor) {

		} else {
R
rebornix 已提交
558
			let itemDOM = this.list?.domElementAtIndex(index);
R
rebornix 已提交
559 560 561
			if (document.activeElement && itemDOM && itemDOM.contains(document.activeElement)) {
				(document.activeElement as HTMLElement).blur();
			}
562 563

			cell.isEditing = false;
R
rebornix 已提交
564 565 566
		}

		this.list?.setFocus([index]);
R
rebornix 已提交
567
		this.list?.focusView();
R
rebornix 已提交
568 569
	}

R
rebornix 已提交
570 571 572 573
	//#endregion

	//#region MISC

R
rebornix 已提交
574 575 576 577 578 579 580 581 582 583 584
	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 已提交
585 586
	getFontInfo(): BareFontInfo | undefined {
		return this.fontInfo;
P
Peng Lyu 已提交
587
	}
R
rebornix 已提交
588

R
rebornix 已提交
589 590
	triggerScroll(event: IMouseWheelEvent) {
		this.list?.triggerScrollFromMouseWheelEvent(event);
R
rebornix 已提交
591 592
	}

593
	createInset(cell: CellViewModel, output: IOutput, shadowContent: string, offset: number) {
R
rebornix 已提交
594 595
		if (!this.webview) {
			return;
R
rebornix 已提交
596 597
		}

R
rebornix 已提交
598
		let preloads = this.notebookViewModel!.renderers;
R
rebornix 已提交
599

600
		if (!this.webview!.insetMapping.has(output)) {
R
rebornix 已提交
601
			let index = this.notebookViewModel!.getViewCellIndex(cell);
602 603 604
			let cellTop = this.list?.getAbsoluteTop(index) || 0;

			this.webview!.createInset(cell, output, cellTop, offset, shadowContent, preloads);
R
rebornix 已提交
605
		} else {
R
rebornix 已提交
606
			let index = this.notebookViewModel!.getViewCellIndex(cell);
607
			let cellTop = this.list?.getAbsoluteTop(index) || 0;
R
rebornix 已提交
608 609
			let scrollTop = this.list?.scrollTop || 0;

610
			this.webview!.updateViewScrollTop(-scrollTop, [{ cell: cell, output: output, cellTop: cellTop }]);
R
rebornix 已提交
611
		}
R
rebornix 已提交
612
	}
R
rebornix 已提交
613

R
rebornix 已提交
614 615 616 617 618 619 620 621
	removeInset(output: IOutput) {
		if (!this.webview) {
			return;
		}

		this.webview!.removeInset(output);
	}

R
rebornix 已提交
622 623
	getOutputRenderer(): OutputRenderer {
		return this.outputRenderer;
R
rebornix 已提交
624
	}
625

R
rebornix 已提交
626
	//#endregion
P
Peng Lyu 已提交
627 628 629 630 631 632 633
}

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 已提交
634 635
		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 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
	}
	const link = theme.getColor(textLinkForeground);
	if (link) {
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor a { color: ${link}; }`);
	}
	const activeLink = theme.getColor(textLinkActiveForeground);
	if (activeLink) {
		collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor a:hover,
			.monaco-workbench .part.editor > .content .notebook-editor a:active { color: ${activeLink}; }`);
	}
	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}; }`);
	}
663 664 665 666 667 668

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

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

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