notebookEditor.ts 22.1 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';
10
import { DisposableStore, IDisposable } 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 23
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { WorkbenchList } from 'vs/platform/list/browser/listService';
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';
24
import { EditorOptions, IEditorMemento } from 'vs/workbench/common/editor';
25
import { INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
R
rebornix 已提交
26
import { NotebookEditorInput, NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
27
import { INotebookService, parseCellUri } from 'vs/workbench/contrib/notebook/browser/notebookService';
R
rebornix 已提交
28
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/output/outputRenderer';
R
rebornix 已提交
29 30 31
import { BackLayerWebView } from 'vs/workbench/contrib/notebook/browser/renderers/backLayerWebView';
import { CodeCellRenderer, MarkdownCellRenderer, NotebookCellListDelegate } from 'vs/workbench/contrib/notebook/browser/renderers/cellRenderer';
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/renderers/cellViewModel';
32
import { CELL_MARGIN, INotebook, NotebookCellsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
33 34 35
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 已提交
36
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
37 38
import { IEditorNavigation } from 'vs/workbench/services/editor/common/editorService';
import { IEditor } from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
39
import { IResourceInput } from 'vs/platform/editor/common/editor';
P
Peng Lyu 已提交
40 41

const $ = DOM.$;
R
rebornix 已提交
42 43
const NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY = 'NotebookEditorViewState';

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

R
rebornix 已提交
46 47 48
interface INotebookEditorViewState {
	editingCells: { [key: number]: boolean };
}
P
Peng Lyu 已提交
49

50
export class NotebookEditor extends BaseEditor implements INotebookEditor {
P
Peng Lyu 已提交
51 52 53
	static readonly ID: string = 'workbench.editor.notebook';
	private rootElement!: HTMLElement;
	private body!: HTMLElement;
P
Peng Lyu 已提交
54
	private contentWidgets!: HTMLElement;
P
Peng Lyu 已提交
55
	private webview: BackLayerWebView | null = null;
P
Peng Lyu 已提交
56

R
rebornix 已提交
57
	private list: WorkbenchList<CellViewModel> | undefined;
J
Johannes Rieken 已提交
58
	private renderedEditors: Map<CellViewModel, ICodeEditor | undefined> = new Map();
P
Peng Lyu 已提交
59
	private model: NotebookEditorModel | undefined;
R
rebornix 已提交
60
	private notebook: INotebook | undefined;
R
rebornix 已提交
61
	viewType: string | undefined;
R
rebornix 已提交
62
	private viewCells: CellViewModel[] = [];
63
	private localStore: DisposableStore = new DisposableStore();
R
rebornix 已提交
64
	private editorMemento: IEditorMemento<INotebookEditorViewState>;
65
	private fontInfo: BareFontInfo | undefined;
66
	// private relayoutDisposable: IDisposable | null = null;
67
	private dimension: DOM.Dimension | null = null;
R
rebornix 已提交
68
	private editorFocus: IContextKey<boolean> | null = null;
R
rebornix 已提交
69
	private outputRenderer: OutputRenderer;
P
Peng Lyu 已提交
70

71 72
	readonly inEditorNavigation: IEditorNavigation;

P
Peng Lyu 已提交
73 74 75 76
	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
		@IThemeService themeService: IThemeService,
		@IInstantiationService private readonly instantiationService: IInstantiationService,
P
Peng Lyu 已提交
77
		@IStorageService storageService: IStorageService,
R
rebornix 已提交
78
		@IWebviewService private webviewService: IWebviewService,
R
rebornix 已提交
79
		@INotebookService private notebookService: INotebookService,
80
		@IEditorGroupsService editorGroupService: IEditorGroupsService,
R
rebornix 已提交
81
		@IConfigurationService private readonly configurationService: IConfigurationService,
R
rebornix 已提交
82
		@IEnvironmentService private readonly environmentSerice: IEnvironmentService,
R
rebornix 已提交
83
		@IContextKeyService private readonly contextKeyService: IContextKeyService,
P
Peng Lyu 已提交
84 85
	) {
		super(NotebookEditor.ID, telemetryService, themeService, storageService);
R
rebornix 已提交
86 87

		this.editorMemento = this.getEditorMemento<INotebookEditorViewState>(editorGroupService, NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY);
R
rebornix 已提交
88
		this.outputRenderer = new OutputRenderer(this, this.instantiationService);
89 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 124 125 126 127 128 129 130 131 132 133

		this.inEditorNavigation = {
			getActiveCodeEditor: (): IEditor | undefined => {
				const focused = this.list?.getFocusedElements()[0];
				return focused instanceof CellViewModel
					? this.renderedEditors.get(focused)
					: undefined;
			},
			openCodeEditor: async (input: IResourceInput, source?: IEditor | undefined): Promise<IEditor | undefined> => {
				const data = parseCellUri(input.resource);
				if (!data || this.notebook?.uri.toString() !== data.notebook.toString()) {
					return undefined;
				}
				for (let i = 0; i < this.list!.length; i++) {
					const item = this.list!.element(i);

					// 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..
					if (item.cell.uri.toString() === input.resource.toString()) {
						this.list!.reveal(i, 0.2);
						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) {
							this.list?.setFocus([i]);
							editor.focus();
						}

						return editor;
					}
				}

				return undefined;
			}
		};
R
rebornix 已提交
134 135
	}

P
Peng Lyu 已提交
136

P
Peng Lyu 已提交
137 138 139 140 141 142 143 144
	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 已提交
145 146
	//#region Editor

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

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

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

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

		this.contentWidgets = document.createElement('div');
		DOM.addClass(this.contentWidgets, 'notebook-content-widgets');
		DOM.append(this.body, this.contentWidgets);
P
Peng Lyu 已提交
175 176
	}

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

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

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

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

P
Peng Lyu 已提交
225
	onHide() {
R
rebornix 已提交
226 227 228 229 230
		this.viewCells.forEach(cell => {
			if (cell.getText() !== '') {
				cell.isEditing = false;
			}
		});
231 232 233 234 235 236 237 238 239

		if (this.webview) {
			this.localStore.clear();
			this.list?.view.rowsContainer.removeChild(this.webview?.element);
			this.webview?.dispose();
			this.webview = null;
		}

		this.list?.splice(0, this.list?.length);
R
rebornix 已提交
240 241 242 243 244 245 246 247

		if (this.model && !this.model.isDirty()) {
			this.notebookService.destoryNotebookDocument(this.viewType!, this.notebook!);
			this.model = undefined;
			this.notebook = undefined;
			this.viewType = undefined;
		}

248
		super.onHide();
P
Peng Lyu 已提交
249 250
	}

251
	setVisible(visible: boolean, group?: IEditorGroup): void {
R
rebornix 已提交
252
		super.setVisible(visible, group);
253
		if (!visible) {
R
rebornix 已提交
254 255 256 257 258
			this.viewCells.forEach(cell => {
				if (cell.getText() !== '') {
					cell.isEditing = false;
				}
			});
259 260 261
		}
	}

P
Peng Lyu 已提交
262
	setInput(input: NotebookEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
R
rebornix 已提交
263 264 265 266
		if (this.input instanceof NotebookEditorInput) {
			this.saveTextEditorViewState(this.input);
		}

267 268 269 270
		return super.setInput(input, options, token)
			.then(() => {
				return input.resolve();
			})
271
			.then(async model => {
272
				if (this.model !== undefined && this.model.textModel === model.textModel && this.webview !== null) {
P
Peng Lyu 已提交
273 274 275
					return;
				}

276
				this.localStore.clear();
P
Peng Lyu 已提交
277 278 279 280
				this.viewCells.forEach(cell => {
					cell.save();
				});

281
				if (this.webview) {
R
rebornix 已提交
282
					this.webview?.clearInsets();
283
					this.webview?.clearPreloadsCache();
284
				} else {
R
rebornix 已提交
285
					this.webview = new BackLayerWebView(this.webviewService, this.notebookService, this, this.environmentSerice);
286 287 288
					this.list?.view.rowsContainer.insertAdjacentElement('afterbegin', this.webview!.element);
				}

P
Peng Lyu 已提交
289
				this.model = model;
290 291
				this.localStore.add(this.model.onDidChangeCells((e) => {
					this.updateViewCells(e);
R
rebornix 已提交
292 293
				}));

R
rebornix 已提交
294
				let viewState = this.loadTextEditorViewState(input);
R
rebornix 已提交
295
				this.notebook = model.getNotebook();
296
				this.webview.updateRendererPreloads(this.notebook.renderers);
R
rebornix 已提交
297
				this.viewType = input.viewType;
298
				this.viewCells = await Promise.all(this.notebook!.cells.map(async cell => {
R
rebornix 已提交
299
					const isEditing = viewState && viewState.editingCells[cell.handle];
300
					return this.instantiationService.createInstance(CellViewModel, input.viewType!, this.notebook!.handle, cell, !!isEditing);
301
				}));
302 303 304 305 306 307

				const updateScrollPosition = () => {
					let scrollTop = this.list?.scrollTop || 0;
					this.webview!.element.style.top = `${scrollTop}px`;
					let updateItems: { top: number, id: string }[] = [];

R
rebornix 已提交
308
					// const date = new Date();
309 310
					this.webview?.mapping.forEach((item) => {
						let index = this.model!.getNotebook().cells.indexOf(item.cell.cell);
R
rebornix 已提交
311
						let top = this.list?.getAbsoluteTop(index) || 0;
R
rebornix 已提交
312
						let newTop = this.webview!.shouldRenderInset(item.cell.id, top);
313 314 315 316 317 318 319 320 321 322

						if (newTop !== undefined) {
							updateItems.push({
								top: newTop,
								id: item.cell.id
							});
						}
					});

					if (updateItems.length > 0) {
R
rebornix 已提交
323
						// console.log('----- did scroll ----  ', date.getMinutes() + ':' + date.getSeconds() + ':' + date.getMilliseconds());
324 325 326 327
						this.webview?.updateViewScrollTop(-scrollTop, updateItems);
					}
				};
				this.localStore.add(this.list!.onWillScroll(e => {
R
rebornix 已提交
328 329
					// const date = new Date();
					// console.log('----- will scroll ----  ', date.getMinutes() + ':' + date.getSeconds() + ':' + date.getMilliseconds());
330 331 332 333
					this.webview?.updateViewScrollTop(-e.scrollTop, []);
				}));
				this.localStore.add(this.list!.onDidScroll(() => updateScrollPosition()));
				this.localStore.add(this.list!.onDidChangeContentHeight(() => updateScrollPosition()));
R
rebornix 已提交
334 335 336 337 338
				this.localStore.add(this.list!.onFocusChange((e) => {
					if (e.elements.length > 0) {
						this.notebookService.updateNotebookActiveCell(input.viewType!, input.getResource()!, e.elements[0].cell.handle);
					}
				}));
339

340 341
				this.list?.splice(0, this.list?.length);
				this.list?.splice(0, 0, this.viewCells);
342 343
				this.list?.layout();
			});
P
Peng Lyu 已提交
344 345
	}

R
rebornix 已提交
346 347 348 349 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 377 378 379 380 381 382
	private saveTextEditorViewState(input: NotebookEditorInput): void {
		if (this.group) {
			let state: { [key: number]: boolean } = {};
			this.viewCells.filter(cell => cell.isEditing).forEach(cell => state[cell.cell.handle] = true);
			this.editorMemento.saveEditorState(this.group, input, {
				editingCells: state
			});
		}
	}

	private loadTextEditorViewState(input: NotebookEditorInput): INotebookEditorViewState | undefined {
		if (this.group) {
			return this.editorMemento.loadEditorState(this.group, input);
		}

		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

	//#region Cell operations
383
	layoutNotebookCell(cell: CellViewModel, height: number) {
R
rebornix 已提交
384
		let relayout = (cell: CellViewModel, height: number) => {
P
Peng Lyu 已提交
385
			let index = this.model!.getNotebook().cells.indexOf(cell.cell);
R
rebornix 已提交
386 387 388
			if (index >= 0) {
				this.list?.updateDynamicHeight(index, cell, height);
			}
R
rebornix 已提交
389 390 391
		};

		if (this.list?.view.isRendering) {
392 393 394 395 396
			// if (this.relayoutDisposable) {
			// 	this.relayoutDisposable.dispose();
			// 	this.relayoutDisposable = null;
			// }
			DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
397
				relayout(cell, height);
398
				// this.relayoutDisposable = null;
R
rebornix 已提交
399 400 401 402
			});
		} else {
			relayout(cell, height);
		}
403 404
	}

405 406 407 408 409 410 411
	updateViewCells(splices: NotebookCellsSplice[]) {
		let update = () => splices.reverse().forEach((diff) => {
			this.list?.splice(diff[0], diff[1], diff[2].map(cell => {
				return this.instantiationService.createInstance(CellViewModel, this.viewType!, this.notebook!.handle, cell, false);
			}));
		});

R
rebornix 已提交
412
		if (this.list?.view.isRendering) {
413 414 415 416
			// if (this.relayoutDisposable) {
			// 	this.relayoutDisposable.dispose();
			// 	this.relayoutDisposable = null;
			// }
417

418
			DOM.scheduleAtNextAnimationFrame(() => {
419
				update();
420
				// this.relayoutDisposable = null;
R
rebornix 已提交
421 422
			});
		} else {
423
			update();
R
rebornix 已提交
424
		}
R
rebornix 已提交
425 426
	}

R
rebornix 已提交
427
	async insertEmptyNotebookCell(listIndex: number | undefined, cell: CellViewModel, type: 'code' | 'markdown', direction: 'above' | 'below'): Promise<void> {
R
rebornix 已提交
428 429 430 431 432
		let newLanguages = this.notebook!.languages;
		let language = 'markdown';
		if (newLanguages && newLanguages.length) {
			language = newLanguages[0];
		}
P
Peng Lyu 已提交
433

434
		let index = listIndex ? listIndex : this.model!.getNotebook().cells.indexOf(cell.cell);
P
Peng Lyu 已提交
435 436
		const insertIndex = direction === 'above' ? index : index + 1;

R
rebornix 已提交
437
		let newModeCell = await this.notebookService.createNotebookCell(this.viewType!, this.notebook!.uri, insertIndex, language, type);
438
		let newCell = this.instantiationService.createInstance(CellViewModel, this.viewType!, this.notebook!.handle, newModeCell!, false);
R
rebornix 已提交
439

P
Peng Lyu 已提交
440 441
		this.viewCells!.splice(insertIndex, 0, newCell);
		this.model!.insertCell(newCell.cell, insertIndex);
P
Peng Lyu 已提交
442
		this.list?.splice(insertIndex, 0, [newCell]);
P
Peng Lyu 已提交
443 444 445 446

		if (type === 'markdown') {
			newCell.isEditing = true;
		}
R
rebornix 已提交
447 448 449 450

		DOM.scheduleAtNextAnimationFrame(() => {
			this.list?.reveal(insertIndex, 0.33);
		});
P
Peng Lyu 已提交
451 452
	}

R
rebornix 已提交
453
	editNotebookCell(listIndex: number | undefined, cell: CellViewModel): void {
P
Peng Lyu 已提交
454 455 456
		cell.isEditing = true;
	}

R
rebornix 已提交
457
	saveNotebookCell(listIndex: number | undefined, cell: CellViewModel): void {
P
Peng Lyu 已提交
458
		cell.isEditing = false;
P
Peng Lyu 已提交
459 460
	}

R
rebornix 已提交
461 462 463 464 465 466 467 468 469 470
	getActiveCell() {
		let elements = this.list?.getFocusedElements();

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

		return undefined;
	}

R
rebornix 已提交
471
	focusNotebookCell(cell: CellViewModel, focusEditor: boolean) {
R
rebornix 已提交
472 473 474 475 476 477 478 479 480
		let index = this.model!.getNotebook().cells.indexOf(cell.cell);

		if (focusEditor) {

		} else {
			let itemDOM = this.list?.view.domElement(index);
			if (document.activeElement && itemDOM && itemDOM.contains(document.activeElement)) {
				(document.activeElement as HTMLElement).blur();
			}
481 482

			cell.isEditing = false;
R
rebornix 已提交
483 484 485 486 487 488
		}

		this.list?.setFocus([index]);
		this.list?.view.domNode.focus();
	}

R
rebornix 已提交
489
	async deleteNotebookCell(listIndex: number | undefined, cell: CellViewModel): Promise<void> {
P
Peng Lyu 已提交
490
		let index = this.model!.getNotebook().cells.indexOf(cell.cell);
P
Peng Lyu 已提交
491

R
rebornix 已提交
492 493
		// await this.notebookService.createNotebookCell(this.viewType!, this.notebook!.uri, insertIndex, language, type);
		await this.notebookService.deleteNotebookCell(this.viewType!, this.notebook!.uri, index);
P
Peng Lyu 已提交
494 495
		this.viewCells!.splice(index, 1);
		this.model!.deleteCell(cell.cell);
P
Peng Lyu 已提交
496 497 498
		this.list?.splice(index, 1);
	}

R
rebornix 已提交
499 500 501 502 503 504
	//#endregion

	//#region MISC

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

R
rebornix 已提交
507 508 509
	getListDimension(): DOM.Dimension | null {
		return this.dimension;
	}
R
rebornix 已提交
510

R
rebornix 已提交
511 512
	triggerScroll(event: IMouseWheelEvent) {
		this.list?.triggerScrollFromMouseWheelEvent(event);
R
rebornix 已提交
513 514
	}

R
rebornix 已提交
515 516 517
	createInset(cell: CellViewModel, outputIndex: number, shadowContent: string, offset: number) {
		if (!this.webview) {
			return;
R
rebornix 已提交
518 519
		}

R
rebornix 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
		let preloads = this.notebook!.renderers;

		if (!this.webview!.mapping.has(cell.id)) {
			let index = this.model!.getNotebook().cells.indexOf(cell.cell);
			let top = this.list?.getAbsoluteTop(index) || 0;
			this.webview!.createInset(cell, offset, shadowContent, top + offset, preloads);
			this.webview!.outputMapping.set(cell.id + `-${outputIndex}`, true);
		} else if (!this.webview!.outputMapping.has(cell.id + `-${outputIndex}`)) {
			let index = this.model!.getNotebook().cells.indexOf(cell.cell);
			let top = this.list?.getAbsoluteTop(index) || 0;
			this.webview!.outputMapping.set(cell.id + `-${outputIndex}`, true);
			this.webview!.createInset(cell, offset, shadowContent, top + offset, preloads);
		} else {
			let index = this.model!.getNotebook().cells.indexOf(cell.cell);
			let top = this.list?.getAbsoluteTop(index) || 0;
			let scrollTop = this.list?.scrollTop || 0;

			this.webview!.updateViewScrollTop(-scrollTop, [{ id: cell.id, top: top + offset }]);
R
rebornix 已提交
538
		}
R
rebornix 已提交
539
	}
R
rebornix 已提交
540

R
rebornix 已提交
541 542
	getOutputRenderer(): OutputRenderer {
		return this.outputRenderer;
R
rebornix 已提交
543
	}
544

R
rebornix 已提交
545
	//#endregion
P
Peng Lyu 已提交
546 547 548 549 550 551 552
}

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 已提交
553 554
		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 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	}
	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}; }`);
	}
582 583 584 585 586 587

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

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

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