notebookEditor.ts 23.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
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, CellFindMatch } 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';
R
rebornix 已提交
30
import { CELL_MARGIN, NotebookCellsSplice, IOutput, parseCellUri, 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';
P
Peng Lyu 已提交
43 44

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

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

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

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

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

	dispose(): void {
		this._onDidChangeActiveEditor.dispose();
		this._disposables.dispose();
	}
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;
	}

89
	activate(input: IResourceEditorInput): ICodeEditor | undefined {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		const data = parseCellUri(input.resource);
		if (!data) {
			return undefined;
		}
		// find the CellViewModel which represents the cell with the
		// given uri, scroll it into view so that the editor is alive,
		// and then set selection et al..
		for (let i = 0; i < this._list.length; i++) {
			const item = this._list.element(i);
			if (item.cell.uri.toString() === input.resource.toString()) {
				this._list.reveal(i, 0.2);
				this._list.setFocus([i]);
				const editor = this._renderedEditors.get(item);
				if (!editor) {
					break;
				}
				if (input.options?.selection) {
					const { selection } = input.options;
					editor.setSelection({
						...selection,
						endLineNumber: selection.endLineNumber || selection.startLineNumber,
						endColumn: selection.endColumn || selection.startColumn
					});
				}
				if (!input.options?.preserveFocus) {
					editor.focus();
				}
				return editor;
			}
		}
		return undefined;
	}
}

R
rebornix 已提交
124
export class NotebookEditor extends BaseEditor implements INotebookEditor {
P
Peng Lyu 已提交
125 126 127
	static readonly ID: string = 'workbench.editor.notebook';
	private rootElement!: HTMLElement;
	private body!: HTMLElement;
P
Peng Lyu 已提交
128
	private webview: BackLayerWebView | null = null;
R
rebornix 已提交
129
	private list: NotebookCellList<CellViewModel> | undefined;
130
	private control: ICompositeCodeEditor | undefined;
J
Johannes Rieken 已提交
131
	private renderedEditors: Map<CellViewModel, ICodeEditor | undefined> = new Map();
R
rebornix 已提交
132
	private notebookViewModel: NotebookViewModel | undefined;
133
	private localStore: DisposableStore = this._register(new DisposableStore());
R
rebornix 已提交
134
	private editorMemento: IEditorMemento<INotebookEditorViewState>;
R
rebornix 已提交
135
	private readonly groupListener = this._register(new MutableDisposable());
136
	private fontInfo: BareFontInfo | undefined;
137
	private dimension: DOM.Dimension | null = null;
R
rebornix 已提交
138
	private editorFocus: IContextKey<boolean> | null = null;
R
rebornix 已提交
139
	private outputRenderer: OutputRenderer;
R
rebornix 已提交
140
	private findWidget: NotebookFindWidget;
P
Peng Lyu 已提交
141 142 143 144 145

	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
		@IThemeService themeService: IThemeService,
		@IInstantiationService private readonly instantiationService: IInstantiationService,
P
Peng Lyu 已提交
146
		@IStorageService storageService: IStorageService,
R
rebornix 已提交
147
		@IWebviewService private webviewService: IWebviewService,
R
rebornix 已提交
148
		@INotebookService private notebookService: INotebookService,
149
		@IEditorGroupsService editorGroupService: IEditorGroupsService,
R
rebornix 已提交
150
		@IConfigurationService private readonly configurationService: IConfigurationService,
R
rebornix 已提交
151
		@IEnvironmentService private readonly environmentSerice: IEnvironmentService,
R
rebornix 已提交
152
		@IContextKeyService private readonly contextKeyService: IContextKeyService,
P
Peng Lyu 已提交
153 154
	) {
		super(NotebookEditor.ID, telemetryService, themeService, storageService);
R
rebornix 已提交
155 156

		this.editorMemento = this.getEditorMemento<INotebookEditorViewState>(editorGroupService, NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY);
R
rebornix 已提交
157
		this.outputRenderer = new OutputRenderer(this, this.instantiationService);
R
rebornix 已提交
158
		this.findWidget = this.instantiationService.createInstance(NotebookFindWidget, this);
159
		this.findWidget.updateTheme(this.themeService.getColorTheme());
R
rebornix 已提交
160 161
	}

R
rebornix 已提交
162 163 164 165
	get viewModel() {
		return this.notebookViewModel;
	}

P
Peng Lyu 已提交
166 167 168 169 170 171 172
	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 已提交
173 174
	get viewType() { return this.notebookViewModel?.viewType; }

P
Peng Lyu 已提交
175

R
rebornix 已提交
176
	//#region Editor Core
R
rebornix 已提交
177

R
rebornix 已提交
178 179 180 181 182

	public get isNotebookEditor() {
		return true;
	}

P
Peng Lyu 已提交
183 184 185
	protected createEditor(parent: HTMLElement): void {
		this.rootElement = DOM.append(parent, $('.notebook-editor'));
		this.createBody(this.rootElement);
186
		this.generateFontInfo();
R
rebornix 已提交
187 188 189 190 191 192 193 194
		this.editorFocus = NOTEBOOK_EDITOR_FOCUSED.bindTo(this.contextKeyService);
		this._register(this.onDidFocus(() => {
			this.editorFocus?.set(true);
		}));

		this._register(this.onDidBlur(() => {
			this.editorFocus?.set(false);
		}));
195 196 197 198 199
	}

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

	private createBody(parent: HTMLElement): void {
P
Peng Lyu 已提交
203
		this.body = document.createElement('div');
P
Peng Lyu 已提交
204 205 206
		DOM.addClass(this.body, 'cell-list-container');
		this.createCellList();
		DOM.append(parent, this.body);
R
rebornix 已提交
207
		DOM.append(this.body, this.findWidget.getDomNode());
P
Peng Lyu 已提交
208 209
	}

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

P
Peng Lyu 已提交
213
		const renders = [
214
			this.instantiationService.createInstance(CodeCellRenderer, this, this.renderedEditors),
P
Peng Lyu 已提交
215
			this.instantiationService.createInstance(MarkdownCellRenderer, this),
P
Peng Lyu 已提交
216 217
		];

R
rebornix 已提交
218 219
		this.list = <NotebookCellList<CellViewModel>>this.instantiationService.createInstance(
			NotebookCellList,
P
Peng Lyu 已提交
220 221 222 223 224 225
			'NotebookCellList',
			this.body,
			this.instantiationService.createInstance(NotebookCellListDelegate),
			renders,
			{
				setRowLineHeight: false,
R
rebornix 已提交
226
				setRowHeight: false,
P
Peng Lyu 已提交
227 228 229
				supportDynamicHeights: true,
				horizontalScrolling: false,
				keyboardSupport: false,
R
rebornix 已提交
230
				mouseSupport: true,
P
Peng Lyu 已提交
231
				multipleSelectionSupport: false,
R
rebornix 已提交
232
				enableKeyboardNavigation: true,
P
Peng Lyu 已提交
233 234 235 236 237 238 239 240 241 242
				overrideStyles: {
					listBackground: editorBackground,
					listActiveSelectionBackground: editorBackground,
					listActiveSelectionForeground: foreground,
					listFocusAndSelectionBackground: editorBackground,
					listFocusAndSelectionForeground: foreground,
					listFocusBackground: editorBackground,
					listFocusForeground: foreground,
					listHoverForeground: foreground,
					listHoverBackground: editorBackground,
243 244
					listHoverOutline: focusBorder,
					listFocusOutline: focusBorder,
245 246 247 248
					listInactiveSelectionBackground: editorBackground,
					listInactiveSelectionForeground: foreground,
					listInactiveFocusBackground: editorBackground,
					listInactiveFocusOutline: editorBackground,
P
Peng Lyu 已提交
249 250 251
				}
			}
		);
P
Peng Lyu 已提交
252

253
		this.control = new NotebookCodeEditors(this.list, this.renderedEditors);
R
rebornix 已提交
254
		this.webview = new BackLayerWebView(this.webviewService, this.notebookService, this, this.environmentSerice);
R
rebornix 已提交
255
		this.list.rowsContainer.appendChild(this.webview.element);
P
Peng Lyu 已提交
256 257 258
		this._register(this.list);
	}

259 260 261 262
	getControl() {
		return this.control;
	}

P
Peng Lyu 已提交
263
	onHide() {
R
rebornix 已提交
264
		this.editorFocus?.set(false);
265 266
		if (this.webview) {
			this.localStore.clear();
R
rebornix 已提交
267
			this.list?.rowsContainer.removeChild(this.webview?.element);
268 269 270 271 272
			this.webview?.dispose();
			this.webview = null;
		}

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

R
rebornix 已提交
274 275
		if (this.notebookViewModel && !this.notebookViewModel.isDirty()) {
			this.notebookService.destoryNotebookDocument(this.viewType!, this.notebookViewModel!.notebookDocument);
R
rebornix 已提交
276
			this.notebookViewModel.dispose();
R
rebornix 已提交
277
			this.notebookViewModel = undefined;
R
rebornix 已提交
278 279
		}

280
		super.onHide();
P
Peng Lyu 已提交
281 282
	}

R
rebornix 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295
	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);
296 297 298
		}
	}

R
rebornix 已提交
299 300 301 302 303
	focus() {
		super.focus();
		this.editorFocus?.set(true);
	}

R
rebornix 已提交
304
	async setInput(input: NotebookEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
R
rebornix 已提交
305 306 307 308
		if (this.input instanceof NotebookEditorInput) {
			this.saveTextEditorViewState(this.input);
		}

R
rebornix 已提交
309 310
		await super.setInput(input, options, token);
		const model = await input.resolve();
P
Peng Lyu 已提交
311

312 313 314
		if (this.notebookViewModel === undefined || !this.notebookViewModel.equal(model) || this.webview === null) {
			this.detachModel();
			await this.attachModel(input, model);
R
rebornix 已提交
315
		}
P
Peng Lyu 已提交
316

317
		if (options instanceof NotebookEditorOptions) {
318 319
			if (options.cellOptions) {
				this.control?.activate(options.cellOptions);
320 321
			}
		}
R
rebornix 已提交
322
	}
323

R
rebornix 已提交
324 325 326 327 328 329 330 331
	clearInput(): void {
		if (this.input && this.input instanceof NotebookEditorInput && !this.input.isDisposed()) {
			this.saveTextEditorViewState(this.input);
		}

		super.clearInput();
	}

R
rebornix 已提交
332 333 334 335 336 337 338
	private detachModel() {
		this.localStore.clear();
		this.notebookViewModel?.dispose();
		this.notebookViewModel = undefined;
		this.webview?.clearInsets();
		this.webview?.clearPreloadsCache();
	}
R
rebornix 已提交
339

R
rebornix 已提交
340 341 342 343 344
	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);
		}
345

R
rebornix 已提交
346 347
		this.notebookViewModel = this.instantiationService.createInstance(NotebookViewModel, input.viewType!, model);
		const viewState = this.loadTextEditorViewState(input);
R
rebornix 已提交
348
		this.notebookViewModel.restoreEditorViewState(viewState);
349

R
rebornix 已提交
350 351 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.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 已提交
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 388 389 390 391
			if (e.elements.length > 0) {
				this.notebookService.updateNotebookActiveCell(input.viewType!, input.resource!, e.elements[0].cell.handle);
			}
		}));

R
rebornix 已提交
392
		this.list?.splice(0, this.list?.length || 0);
R
rebornix 已提交
393 394
		this.list?.splice(0, 0, this.notebookViewModel!.viewCells);
		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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	//#region Editor Features

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

		if (index !== undefined) {
			this.list?.revealInView(index, offset);
		}
	}

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

		if (index !== undefined) {
			this.list?.revealInCenterIfOutsideViewport(index, offset);
		}
	}

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

		if (index !== undefined) {
			this.list?.revealInCenter(index, offset);
		}
	}
R
rebornix 已提交
455 456 457 458 459 460 461

	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any {
		return this.notebookViewModel?.changeDecorations(callback);
	}

	//#endregion

R
rebornix 已提交
462
	//#region Find Delegate
R
rebornix 已提交
463
	focusNext(match: CellFindMatch, matchIndex: number) {
R
rebornix 已提交
464
		let cell = match.cell;
R
rebornix 已提交
465
		let index = this.notebookViewModel!.viewCells.indexOf(cell);
R
rebornix 已提交
466

R
rebornix 已提交
467
		this.list?.revealInView(index);
R
rebornix 已提交
468 469 470 471 472 473 474 475 476 477 478 479
	}

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

	public hideFind() {
		this.findWidget.hide();
	}

	//#endregion

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

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

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

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

R
rebornix 已提交
512
		this.notebookViewModel!.insertCell(insertIndex, newCell);
P
Peng Lyu 已提交
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 527 528 529 530 531 532
	async deleteNotebookCell(cell: CellViewModel): Promise<void> {
		const index = this.notebookViewModel!.getViewCellIndex(cell);
		await this.notebookService.deleteNotebookCell(this.viewType!, this.notebookViewModel!.uri, index);
		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 574 575
	//#endregion

	//#region MISC

	getFontInfo(): BareFontInfo | undefined {
		return this.fontInfo;
P
Peng Lyu 已提交
576
	}
R
rebornix 已提交
577

R
rebornix 已提交
578 579 580
	getListDimension(): DOM.Dimension | null {
		return this.dimension;
	}
R
rebornix 已提交
581

R
rebornix 已提交
582 583
	triggerScroll(event: IMouseWheelEvent) {
		this.list?.triggerScrollFromMouseWheelEvent(event);
R
rebornix 已提交
584 585
	}

586
	createInset(cell: CellViewModel, output: IOutput, shadowContent: string, offset: number) {
R
rebornix 已提交
587 588
		if (!this.webview) {
			return;
R
rebornix 已提交
589 590
		}

R
rebornix 已提交
591
		let preloads = this.notebookViewModel!.renderers;
R
rebornix 已提交
592

593
		if (!this.webview!.insetMapping.has(output)) {
R
rebornix 已提交
594
			let index = this.notebookViewModel!.getViewCellIndex(cell);
595 596 597
			let cellTop = this.list?.getAbsoluteTop(index) || 0;

			this.webview!.createInset(cell, output, cellTop, offset, shadowContent, preloads);
R
rebornix 已提交
598
		} else {
R
rebornix 已提交
599
			let index = this.notebookViewModel!.getViewCellIndex(cell);
600
			let cellTop = this.list?.getAbsoluteTop(index) || 0;
R
rebornix 已提交
601 602
			let scrollTop = this.list?.scrollTop || 0;

603
			this.webview!.updateViewScrollTop(-scrollTop, [{ cell: cell, output: output, cellTop: cellTop }]);
R
rebornix 已提交
604
		}
R
rebornix 已提交
605
	}
R
rebornix 已提交
606

R
rebornix 已提交
607 608 609 610 611 612 613 614
	removeInset(output: IOutput) {
		if (!this.webview) {
			return;
		}

		this.webview!.removeInset(output);
	}

R
rebornix 已提交
615 616
	getOutputRenderer(): OutputRenderer {
		return this.outputRenderer;
R
rebornix 已提交
617
	}
618

R
rebornix 已提交
619
	//#endregion
P
Peng Lyu 已提交
620 621 622 623 624 625 626
}

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 已提交
627 628
		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 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
	}
	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}; }`);
	}
656 657 658 659 660 661

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

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

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