markdownCellViewModel.ts 4.9 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 18 19 20 21 22 23 24

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 已提交
25 26 27 28 29 30 31 32
	private _layoutInfo: MarkdownCellLayoutInfo;

	get layoutInfo() {
		return this._layoutInfo;
	}

	protected readonly _onDidChangeLayout = new Emitter<MarkdownCellLayoutChangeEvent>();
	readonly onDidChangeLayout = this._onDidChangeLayout.event;
R
rebornix 已提交
33 34 35 36 37

	constructor(
		readonly viewType: string,
		readonly notebookHandle: number,
		readonly cell: ICell,
R
rebornix 已提交
38
		readonly eventDispatcher: NotebookEventDispatcher,
39
		initialNotebookLayoutInfo: NotebookLayoutInfo | null,
R
rebornix 已提交
40 41 42
		@IInstantiationService private readonly _instaService: IInstantiationService,
		@ITextModelService private readonly _modelService: ITextModelService) {
		super(viewType, notebookHandle, cell, UUID.generateUuid());
R
rebornix 已提交
43 44

		this._layoutInfo = {
45
			fontInfo: initialNotebookLayoutInfo?.fontInfo || null,
R
rebornix 已提交
46 47
			editorWidth: initialNotebookLayoutInfo?.width || 0,
			bottomToolbarOffset: BOTTOM_CELL_TOOLBAR_HEIGHT
R
rebornix 已提交
48 49
		};

R
rebornix 已提交
50 51 52
		this._register(eventDispatcher.onDidChangeLayout((e) => {
			if (e.source.width || e.source.fontInfo) {
				this.layoutChange({ outerWidth: e.value.width, font: e.value.fontInfo });
R
rebornix 已提交
53 54 55 56 57 58
			}
		}));
	}

	layoutChange(state: MarkdownCellLayoutChangeEvent) {
		// recompute
59
		const editorWidth = state.outerWidth !== undefined ? state.outerWidth - CELL_MARGIN * 2 - CELL_RUN_GUTTER : 0;
R
rebornix 已提交
60 61

		this._layoutInfo = {
R
rebornix 已提交
62
			fontInfo: state.font || null,
R
rebornix 已提交
63 64
			editorWidth,
			bottomToolbarOffset: BOTTOM_CELL_TOOLBAR_HEIGHT
R
rebornix 已提交
65 66 67
		};

		this._onDidChangeLayout.fire(state);
R
rebornix 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	}

	hasDynamicHeight() {
		return true;
	}

	getHeight(lineHeight: number) {
		return 100;
	}

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

	save() {
84
		if (this._textModel && !this._textModel.isDisposed() && this.editState === CellEditState.Editing) {
R
rebornix 已提交
85 86 87 88 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
			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() {
118
		this.editState = CellEditState.Preview;
R
rebornix 已提交
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
	}

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