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

import { Emitter, Event } from 'vs/base/common/event';
import * as UUID from 'vs/base/common/uuid';
import * as model from 'vs/editor/common/model';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
11
import { ICellViewModel, CellFindMatch, MarkdownCellLayoutInfo, MarkdownCellLayoutChangeEvent, CellEditState, NotebookLayoutInfo } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
R
rebornix 已提交
12 13 14
import { MarkdownRenderer } from 'vs/workbench/contrib/notebook/browser/view/renderers/mdRenderer';
import { BaseCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/baseCellViewModel';
import { CellKind, ICell } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
15
import { CELL_MARGIN, CELL_RUN_GUTTER, BOTTOM_CELL_TOOLBAR_HEIGHT } from 'vs/workbench/contrib/notebook/browser/constants';
R
rebornix 已提交
16
import { NotebookEventDispatcher } from 'vs/workbench/contrib/notebook/browser/viewModel/eventDispatcher';
R
rebornix 已提交
17
import * as editorCommon from 'vs/editor/common/editorCommon';
R
rebornix 已提交
18 19 20 21 22 23 24 25

export class MarkdownCellViewModel extends BaseCellViewModel implements ICellViewModel {
	cellKind: CellKind.Markdown = CellKind.Markdown;
	private _mdRenderer: MarkdownRenderer | null = null;
	private _html: HTMLElement | null = null;
	private readonly _onDidChangeContent: Emitter<void> = this._register(new Emitter<void>());
	public readonly onDidChangeContent: Event<void> = this._onDidChangeContent.event;

R
rebornix 已提交
26 27 28 29 30 31
	private _layoutInfo: MarkdownCellLayoutInfo;

	get layoutInfo() {
		return this._layoutInfo;
	}

R
rebornix 已提交
32 33 34 35 36 37 38 39
	set totalHeight(newHeight: number) {
		this.layoutChange({ totalHeight: newHeight });
	}

	get totalHeight() {
		throw new Error('MarkdownCellViewModel.totalHeight is write only');
	}

R
rebornix 已提交
40 41
	protected readonly _onDidChangeLayout = new Emitter<MarkdownCellLayoutChangeEvent>();
	readonly onDidChangeLayout = this._onDidChangeLayout.event;
R
rebornix 已提交
42 43 44 45 46

	constructor(
		readonly viewType: string,
		readonly notebookHandle: number,
		readonly cell: ICell,
R
rebornix 已提交
47
		readonly eventDispatcher: NotebookEventDispatcher,
48
		initialNotebookLayoutInfo: NotebookLayoutInfo | null,
R
rebornix 已提交
49 50 51
		@IInstantiationService private readonly _instaService: IInstantiationService,
		@ITextModelService private readonly _modelService: ITextModelService) {
		super(viewType, notebookHandle, cell, UUID.generateUuid());
R
rebornix 已提交
52 53

		this._layoutInfo = {
54
			fontInfo: initialNotebookLayoutInfo?.fontInfo || null,
R
rebornix 已提交
55
			editorWidth: initialNotebookLayoutInfo?.width || 0,
R
rebornix 已提交
56 57
			bottomToolbarOffset: BOTTOM_CELL_TOOLBAR_HEIGHT,
			totalHeight: 0
R
rebornix 已提交
58 59
		};

R
rebornix 已提交
60 61 62
		this._register(eventDispatcher.onDidChangeLayout((e) => {
			if (e.source.width || e.source.fontInfo) {
				this.layoutChange({ outerWidth: e.value.width, font: e.value.fontInfo });
R
rebornix 已提交
63 64 65 66 67 68
			}
		}));
	}

	layoutChange(state: MarkdownCellLayoutChangeEvent) {
		// recompute
69
		const editorWidth = state.outerWidth !== undefined ? state.outerWidth - CELL_MARGIN * 2 - CELL_RUN_GUTTER : this._layoutInfo.editorWidth;
R
rebornix 已提交
70 71

		this._layoutInfo = {
72
			fontInfo: state.font || this._layoutInfo.fontInfo,
R
rebornix 已提交
73
			editorWidth,
R
rebornix 已提交
74 75
			bottomToolbarOffset: BOTTOM_CELL_TOOLBAR_HEIGHT,
			totalHeight: state.totalHeight === undefined ? this._layoutInfo.totalHeight : state.totalHeight
R
rebornix 已提交
76 77 78
		};

		this._onDidChangeLayout.fire(state);
R
rebornix 已提交
79 80
	}

R
rebornix 已提交
81 82 83 84 85 86 87 88 89 90 91 92
	restoreEditorViewState(editorViewStates: editorCommon.ICodeEditorViewState | null, totalHeight?: number) {
		super.restoreEditorViewState(editorViewStates);
		if (totalHeight !== undefined) {
			this._layoutInfo = {
				fontInfo: this._layoutInfo.fontInfo,
				editorWidth: this._layoutInfo.editorWidth,
				bottomToolbarOffset: this._layoutInfo.bottomToolbarOffset,
				totalHeight: totalHeight
			};
		}
	}

R
rebornix 已提交
93 94 95 96 97
	hasDynamicHeight() {
		return true;
	}

	getHeight(lineHeight: number) {
R
rebornix 已提交
98 99 100 101 102
		if (this._layoutInfo.totalHeight === 0) {
			return 100;
		} else {
			return this._layoutInfo.totalHeight;
		}
R
rebornix 已提交
103 104 105 106 107 108 109 110
	}

	setText(strs: string[]) {
		this.cell.source = strs;
		this._html = null;
	}

	save() {
111
		if (this._textModel && !this._textModel.isDisposed() && this.editState === CellEditState.Editing) {
R
rebornix 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
			let cnt = this._textModel.getLineCount();
			this.cell.source = this._textModel.getLinesContent().map((str, index) => str + (index !== cnt - 1 ? '\n' : ''));
		}
	}

	getHTML(): HTMLElement | null {
		if (this.cellKind === CellKind.Markdown) {
			if (this._html) {
				return this._html;
			}
			let renderer = this.getMarkdownRenderer();
			this._html = renderer.render({ value: this.getText(), isTrusted: true }).element;
			return this._html;
		}
		return null;
	}

	async resolveTextModel(): Promise<model.ITextModel> {
		if (!this._textModel) {
			const ref = await this._modelService.createModelReference(this.cell.uri);
			this._textModel = ref.object.textEditorModel;
			this._buffer = this._textModel.getTextBuffer();
			this._register(ref);
			this._register(this._textModel.onDidChangeContent(() => {
				this.cell.contentChange();
				this._html = null;
				this._onDidChangeContent.fire();
			}));
		}
		return this._textModel;
	}

	onDeselect() {
145
		this.editState = CellEditState.Preview;
R
rebornix 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	}

	getMarkdownRenderer() {
		if (!this._mdRenderer) {
			this._mdRenderer = this._instaService.createInstance(MarkdownRenderer);
		}
		return this._mdRenderer;
	}

	private readonly _hasFindResult = this._register(new Emitter<boolean>());
	public readonly hasFindResult: Event<boolean> = this._hasFindResult.event;

	startFind(value: string): CellFindMatch | null {
		const matches = super.cellStartFind(value);

		if (matches === null) {
			return null;
		}

		return {
			cell: this,
			matches
		};
	}
}