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

import 'vs/css!./notebook';
import * as DOM from 'vs/base/browser/dom';
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { IStorageService } from 'vs/platform/storage/common/storage';
P
Peng Lyu 已提交
12
import { NotebookEditorInput, NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
R
rebornix 已提交
13
import { EditorOptions, IEditorMemento } from 'vs/workbench/common/editor';
P
Peng Lyu 已提交
14 15 16 17
import { CancellationToken } from 'vs/base/common/cancellation';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IModelService } from 'vs/editor/common/services/modelService';
import { getExtraColor } from 'vs/workbench/contrib/welcome/walkThrough/common/walkThroughUtils';
P
Peng Lyu 已提交
18 19
import { textLinkForeground, textLinkActiveForeground, focusBorder, textPreformatForeground, contrastBorder, textBlockQuoteBackground, textBlockQuoteBorder, editorBackground, foreground } from 'vs/platform/theme/common/colorRegistry';
import { WorkbenchList } from 'vs/platform/list/browser/listService';
P
Peng Lyu 已提交
20
import { IModeService } from 'vs/editor/common/services/modeService';
R
rebornix 已提交
21 22
import { MarkdownCellRenderer, CodeCellRenderer, NotebookCellListDelegate } from 'vs/workbench/contrib/notebook/browser/renderers/cellRenderer';
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/renderers/cellViewModel';
R
rebornix 已提交
23
import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
P
Peng Lyu 已提交
24
import { IWebviewService } from 'vs/workbench/contrib/webview/browser/webview';
R
💄  
rebornix 已提交
25
import { BackLayerWebView } from 'vs/workbench/contrib/notebook/browser/renderers/contentWidget';
P
Peng Lyu 已提交
26
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
27
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
R
rebornix 已提交
28
import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
29 30 31 32
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { getZoomLevel } from 'vs/base/browser/browser';
R
rebornix 已提交
33
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
R
rebornix 已提交
34
import { IContextKeyService, IContextKey, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
R
rebornix 已提交
35
import { IOpenerService } from 'vs/platform/opener/common/opener';
R
rebornix 已提交
36 37 38
import { INotebook, CELL_MARGIN } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { NotebookHandler } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/output/outputRenderer';
P
Peng Lyu 已提交
39 40

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

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

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

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

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

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

		this.editorMemento = this.getEditorMemento<INotebookEditorViewState>(editorGroupService, NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY);
R
rebornix 已提交
87 88 89 90 91
		this.outputRenderer = new OutputRenderer(this, this.instantiationService);
	}

	getOutputRenderer(): OutputRenderer {
		return this.outputRenderer;
P
Peng Lyu 已提交
92
	}
P
Peng Lyu 已提交
93

P
Peng Lyu 已提交
94 95 96 97 98 99 100 101 102 103 104
	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*/ }


	protected createEditor(parent: HTMLElement): void {
		this.rootElement = DOM.append(parent, $('.notebook-editor'));
		this.createBody(this.rootElement);
105
		this.generateFontInfo();
R
rebornix 已提交
106 107 108 109 110 111 112 113
		this.editorFocus = NOTEBOOK_EDITOR_FOCUSED.bindTo(this.contextKeyService);
		this._register(this.onDidFocus(() => {
			this.editorFocus?.set(true);
		}));

		this._register(this.onDidBlur(() => {
			this.editorFocus?.set(false);
		}));
114 115 116 117 118
	}

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

	private createBody(parent: HTMLElement): void {
P
Peng Lyu 已提交
122
		this.body = document.createElement('div');
P
Peng Lyu 已提交
123 124 125
		DOM.addClass(this.body, 'cell-list-container');
		this.createCellList();
		DOM.append(parent, this.body);
P
Peng Lyu 已提交
126 127 128 129

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

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

P
Peng Lyu 已提交
135
		const renders = [
P
Peng Lyu 已提交
136 137
			this.instantiationService.createInstance(MarkdownCellRenderer, this),
			this.instantiationService.createInstance(CodeCellRenderer, this)
P
Peng Lyu 已提交
138 139
		];

R
rebornix 已提交
140
		this.list = this.instantiationService.createInstance<typeof WorkbenchList, WorkbenchList<CellViewModel>>(
P
Peng Lyu 已提交
141 142 143 144 145 146 147
			WorkbenchList,
			'NotebookCellList',
			this.body,
			this.instantiationService.createInstance(NotebookCellListDelegate),
			renders,
			{
				setRowLineHeight: false,
R
rebornix 已提交
148
				setRowHeight: false,
P
Peng Lyu 已提交
149 150 151
				supportDynamicHeights: true,
				horizontalScrolling: false,
				keyboardSupport: false,
R
rebornix 已提交
152
				mouseSupport: true,
P
Peng Lyu 已提交
153
				multipleSelectionSupport: false,
R
rebornix 已提交
154
				enableKeyboardNavigation: true,
P
Peng Lyu 已提交
155 156 157 158 159 160 161 162 163 164
				overrideStyles: {
					listBackground: editorBackground,
					listActiveSelectionBackground: editorBackground,
					listActiveSelectionForeground: foreground,
					listFocusAndSelectionBackground: editorBackground,
					listFocusAndSelectionForeground: foreground,
					listFocusBackground: editorBackground,
					listFocusForeground: foreground,
					listHoverForeground: foreground,
					listHoverBackground: editorBackground,
165 166
					listHoverOutline: focusBorder,
					listFocusOutline: focusBorder,
167 168 169 170
					listInactiveSelectionBackground: editorBackground,
					listInactiveSelectionForeground: foreground,
					listInactiveFocusBackground: editorBackground,
					listInactiveFocusOutline: editorBackground,
P
Peng Lyu 已提交
171 172 173
				}
			}
		);
P
Peng Lyu 已提交
174

R
rebornix 已提交
175
		this.webview = new BackLayerWebView(this.webviewService, this.notebookService, this, this.environmentSerice);
P
Peng Lyu 已提交
176
		this.list.view.rowsContainer.appendChild(this.webview.element);
P
Peng Lyu 已提交
177 178 179
		this._register(this.list);
	}

180 181 182 183
	getFontInfo(): BareFontInfo | undefined {
		return this.fontInfo;
	}

184 185 186 187
	getListDimension(): DOM.Dimension | null {
		return this.dimension;
	}

P
Peng Lyu 已提交
188 189
	triggerWheel(event: IMouseWheelEvent) {
		this.list?.triggerScrollFromMouseWheelEvent(event);
P
Peng Lyu 已提交
190 191
	}

R
rebornix 已提交
192
	createContentWidget(cell: CellViewModel, outputIndex: number, shadowContent: string, offset: number) {
193 194 195 196
		if (!this.webview) {
			return;
		}

197 198
		let preloads = this.notebook!.renderers;

P
Peng Lyu 已提交
199 200
		if (!this.webview!.mapping.has(cell.id)) {
			let index = this.model!.getNotebook().cells.indexOf(cell.cell);
R
rebornix 已提交
201
			let top = this.list?.getAbsoluteTop(index) || 0;
202
			this.webview!.createContentWidget(cell, offset, shadowContent, top + offset, preloads);
R
rebornix 已提交
203 204 205
			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);
R
rebornix 已提交
206
			let top = this.list?.getAbsoluteTop(index) || 0;
R
rebornix 已提交
207
			this.webview!.outputMapping.set(cell.id + `-${outputIndex}`, true);
208
			this.webview!.createContentWidget(cell, offset, shadowContent, top + offset, preloads);
P
Peng Lyu 已提交
209
		} else {
P
Peng Lyu 已提交
210
			let index = this.model!.getNotebook().cells.indexOf(cell.cell);
R
rebornix 已提交
211
			let top = this.list?.getAbsoluteTop(index) || 0;
P
Peng Lyu 已提交
212 213
			let scrollTop = this.list?.scrollTop || 0;

214
			this.webview!.updateViewScrollTop(-scrollTop, [{ id: cell.id, top: top + offset }]);
P
Peng Lyu 已提交
215
		}
P
Peng Lyu 已提交
216
	}
P
Peng Lyu 已提交
217

R
rebornix 已提交
218
	disposeViewCell(cell: CellViewModel) {
P
Peng Lyu 已提交
219 220
	}

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

		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 已提交
236 237 238 239 240 241 242 243

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

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

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

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

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

272
				this.localStore.clear();
P
Peng Lyu 已提交
273 274 275 276
				this.viewCells.forEach(cell => {
					cell.save();
				});

277 278
				if (this.webview) {
					this.webview?.clearContentWidgets();
279
					this.webview?.clearPreloadsCache();
280
				} else {
R
rebornix 已提交
281
					this.webview = new BackLayerWebView(this.webviewService, this.notebookService, this, this.environmentSerice);
282 283 284
					this.list?.view.rowsContainer.insertAdjacentElement('afterbegin', this.webview!.element);
				}

P
Peng Lyu 已提交
285
				this.model = model;
R
rebornix 已提交
286 287 288 289
				this.localStore.add(this.model.onDidChangeCells(() => {
					this.updateViewCells();
				}));

R
rebornix 已提交
290
				let viewState = this.loadTextEditorViewState(input);
R
rebornix 已提交
291
				this.notebook = model.getNotebook();
292
				this.webview.updateRendererPreloads(this.notebook.renderers);
R
rebornix 已提交
293
				this.viewType = input.viewType;
R
rebornix 已提交
294
				this.viewCells = this.notebook!.cells.map(cell => {
R
rebornix 已提交
295
					const isEditing = viewState && viewState.editingCells[cell.handle];
R
rebornix 已提交
296
					return new CellViewModel(input.viewType!, this.notebook!.handle, cell, !!isEditing, this.modelService, this.modeService, this.openerService, this.notebookService, this.themeService);
P
Peng Lyu 已提交
297
				});
298 299 300 301 302 303

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

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

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

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

336 337
				this.list?.splice(0, this.list?.length);
				this.list?.splice(0, 0, this.viewCells);
338 339
				this.list?.layout();
			});
P
Peng Lyu 已提交
340 341
	}

R
rebornix 已提交
342 343
	layoutElement(cell: CellViewModel, height: number) {
		let relayout = (cell: CellViewModel, height: number) => {
P
Peng Lyu 已提交
344
			let index = this.model!.getNotebook().cells.indexOf(cell.cell);
R
rebornix 已提交
345 346 347
			if (index >= 0) {
				this.list?.updateDynamicHeight(index, cell, height);
			}
R
rebornix 已提交
348 349 350
		};

		if (this.list?.view.isRendering) {
351 352 353 354 355
			if (this.relayoutDisposable) {
				this.relayoutDisposable.dispose();
				this.relayoutDisposable = null;
			}
			this.relayoutDisposable = DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
356
				relayout(cell, height);
357
				this.relayoutDisposable = null;
R
rebornix 已提交
358 359 360 361
			});
		} else {
			relayout(cell, height);
		}
362 363
	}

R
rebornix 已提交
364
	updateViewCells() {
R
rebornix 已提交
365
		if (this.list?.view.isRendering) {
366 367 368 369 370 371
			if (this.relayoutDisposable) {
				this.relayoutDisposable.dispose();
				this.relayoutDisposable = null;
			}

			this.relayoutDisposable = DOM.scheduleAtNextAnimationFrame(() => {
R
rebornix 已提交
372
				this.list?.rerender();
373
				this.relayoutDisposable = null;
R
rebornix 已提交
374 375
			});
		} else {
R
rebornix 已提交
376
			this.list?.rerender();
R
rebornix 已提交
377
		}
R
rebornix 已提交
378 379
	}

R
rebornix 已提交
380
	async insertEmptyNotebookCell(listIndex: number | undefined, cell: CellViewModel, type: 'code' | 'markdown', direction: 'above' | 'below'): Promise<void> {
R
rebornix 已提交
381 382 383 384 385
		let newLanguages = this.notebook!.languages;
		let language = 'markdown';
		if (newLanguages && newLanguages.length) {
			language = newLanguages[0];
		}
P
Peng Lyu 已提交
386

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

R
rebornix 已提交
390
		let newModeCell = await this.notebookService.createNotebookCell(this.viewType!, this.notebook!.uri, insertIndex, language, type);
R
rebornix 已提交
391
		let newCell = new CellViewModel(this.viewType!, this.notebook!.handle, newModeCell!, false, this.modelService, this.modeService, this.openerService, this.notebookService, this.themeService);
R
rebornix 已提交
392

P
Peng Lyu 已提交
393 394
		this.viewCells!.splice(insertIndex, 0, newCell);
		this.model!.insertCell(newCell.cell, insertIndex);
P
Peng Lyu 已提交
395
		this.list?.splice(insertIndex, 0, [newCell]);
P
Peng Lyu 已提交
396 397 398 399

		if (type === 'markdown') {
			newCell.isEditing = true;
		}
R
rebornix 已提交
400 401 402 403

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

R
rebornix 已提交
406
	editNotebookCell(listIndex: number | undefined, cell: CellViewModel): void {
P
Peng Lyu 已提交
407 408 409
		cell.isEditing = true;
	}

R
rebornix 已提交
410
	saveNotebookCell(listIndex: number | undefined, cell: CellViewModel): void {
P
Peng Lyu 已提交
411
		cell.isEditing = false;
P
Peng Lyu 已提交
412 413
	}

R
rebornix 已提交
414 415 416 417 418 419 420 421 422 423
	getActiveCell() {
		let elements = this.list?.getFocusedElements();

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

		return undefined;
	}

R
rebornix 已提交
424
	focusNotebookCell(cell: CellViewModel, focusEditor: boolean) {
R
rebornix 已提交
425 426 427 428 429 430 431 432 433
		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();
			}
434 435

			cell.isEditing = false;
R
rebornix 已提交
436 437 438 439 440 441
		}

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

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

R
rebornix 已提交
445 446
		// 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 已提交
447 448
		this.viewCells!.splice(index, 1);
		this.model!.deleteCell(cell.cell);
P
Peng Lyu 已提交
449 450 451
		this.list?.splice(index, 1);
	}

P
Peng Lyu 已提交
452
	layout(dimension: DOM.Dimension): void {
453
		this.dimension = new DOM.Dimension(dimension.width, dimension.height);
P
Peng Lyu 已提交
454 455
		DOM.toggleClass(this.rootElement, 'mid-width', dimension.width < 1000 && dimension.width >= 600);
		DOM.toggleClass(this.rootElement, 'narrow-width', dimension.width < 600);
456 457
		DOM.size(this.body, dimension.width, dimension.height);
		this.list?.layout(dimension.height, dimension.width);
P
Peng Lyu 已提交
458
	}
R
rebornix 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

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

		super.saveState();
	}

	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;
	}
485

P
Peng Lyu 已提交
486 487 488 489 490 491 492
}

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 已提交
493 494
		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 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	}
	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}; }`);
	}
522 523 524 525 526 527

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

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

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