Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split out Webview from WebviewEditor in proposed API #47370

Merged
merged 3 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions extensions/markdown-language-features/src/features/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class MarkdownPreview {

public static viewType = 'markdown.preview';

private readonly webview: vscode.Webview;
private readonly editor: vscode.WebviewEditor;
private throttleTimer: any;
private line: number | undefined = undefined;
private readonly disposables: vscode.Disposable[] = [];
Expand All @@ -32,7 +32,7 @@ export class MarkdownPreview {


public static async revive(
webview: vscode.Webview,
webview: vscode.WebviewEditor,
state: any,
contentProvider: MarkdownContentProvider,
previewConfigurations: MarkdownPreviewConfigurationManager,
Expand Down Expand Up @@ -69,7 +69,7 @@ export class MarkdownPreview {
topmostLineMonitor: MarkdownFileTopmostLineMonitor,
contributions: MarkdownContributions
): MarkdownPreview {
const webview = vscode.window.createWebview(
const webview = vscode.window.createWebviewEditor(
MarkdownPreview.viewType,
MarkdownPreview.getPreviewTitle(resource, locked),
previewColumn, {
Expand All @@ -90,25 +90,25 @@ export class MarkdownPreview {
}

private constructor(
webview: vscode.Webview,
webview: vscode.WebviewEditor,
private _resource: vscode.Uri,
public locked: boolean,
private readonly contentProvider: MarkdownContentProvider,
private readonly previewConfigurations: MarkdownPreviewConfigurationManager,
private readonly logger: Logger,
topmostLineMonitor: MarkdownFileTopmostLineMonitor
) {
this.webview = webview;
this.editor = webview;

this.webview.onDidDispose(() => {
this.editor.onDidDispose(() => {
this.dispose();
}, null, this.disposables);

this.webview.onDidChangeViewState(e => {
this.editor.onDidChangeViewState(e => {
this._onDidChangeViewStateEmitter.fire(e);
}, null, this.disposables);

this.webview.onDidReceiveMessage(e => {
this.editor.webview.onDidReceiveMessage(e => {
if (e.source !== this._resource.toString()) {
return;
}
Expand Down Expand Up @@ -155,7 +155,7 @@ export class MarkdownPreview {
private readonly _onDisposeEmitter = new vscode.EventEmitter<void>();
public readonly onDispose = this._onDisposeEmitter.event;

private readonly _onDidChangeViewStateEmitter = new vscode.EventEmitter<vscode.WebviewOnDidChangeViewStateEvent>();
private readonly _onDidChangeViewStateEmitter = new vscode.EventEmitter<vscode.WebviewEditorOnDidChangeViewStateEvent>();
public readonly onDidChangeViewState = this._onDidChangeViewStateEmitter.event;

public get resource(): vscode.Uri {
Expand All @@ -180,7 +180,7 @@ export class MarkdownPreview {

this._onDisposeEmitter.dispose();
this._onDidChangeViewStateEmitter.dispose();
this.webview.dispose();
this.editor.dispose();

disposeAll(this.disposables);
}
Expand Down Expand Up @@ -224,15 +224,15 @@ export class MarkdownPreview {
}

public get viewColumn(): vscode.ViewColumn | undefined {
return this.webview.viewColumn;
return this.editor.viewColumn;
}

public isPreviewOf(resource: vscode.Uri): boolean {
return this._resource.fsPath === resource.fsPath;
}

public isWebviewOf(webview: vscode.Webview): boolean {
return this.webview === webview;
public isWebviewOf(webview: vscode.WebviewEditor): boolean {
return this.editor === webview;
}

public matchesResource(
Expand All @@ -256,12 +256,12 @@ export class MarkdownPreview {
}

public reveal(viewColumn: vscode.ViewColumn) {
this.webview.reveal(viewColumn);
this.editor.reveal(viewColumn);
}

public toggleLock() {
this.locked = !this.locked;
this.webview.title = MarkdownPreview.getPreviewTitle(this._resource, this.locked);
this.editor.webview.title = MarkdownPreview.getPreviewTitle(this._resource, this.locked);
}

private static getPreviewTitle(resource: vscode.Uri, locked: boolean): string {
Expand Down Expand Up @@ -293,7 +293,7 @@ export class MarkdownPreview {

private postMessage(msg: any) {
if (!this._disposed) {
this.webview.postMessage(msg);
this.editor.webview.postMessage(msg);
}
}

Expand All @@ -315,8 +315,8 @@ export class MarkdownPreview {
this.currentVersion = { resource, version: document.version };
const content = await this.contentProvider.provideTextDocumentContent(document, this.previewConfigurations, this.line);
if (this._resource === resource) {
this.webview.title = MarkdownPreview.getPreviewTitle(this._resource, this.locked);
this.webview.html = content;
this.editor.webview.title = MarkdownPreview.getPreviewTitle(this._resource, this.locked);
this.editor.webview.html = content;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { isMarkdownFile } from '../util/file';
import { MarkdownPreviewConfigurationManager } from './previewConfig';
import { MarkdownContributions } from '../markdownExtensions';

export class MarkdownPreviewManager implements vscode.WebviewSerializer {
export class MarkdownPreviewManager implements vscode.WebviewEditorSerializer {
private static readonly markdownPreviewActiveContextKey = 'markdownPreviewFocus';

private readonly topmostLineMonitor = new MarkdownFileTopmostLineMonitor();
Expand All @@ -36,7 +36,7 @@ export class MarkdownPreviewManager implements vscode.WebviewSerializer {
}
}, null, this.disposables);

this.disposables.push(vscode.window.registerWebviewSerializer(MarkdownPreview.viewType, this));
this.disposables.push(vscode.window.registerWebviewEditorSerializer(MarkdownPreview.viewType, this));
}

public dispose(): void {
Expand Down Expand Up @@ -88,8 +88,8 @@ export class MarkdownPreviewManager implements vscode.WebviewSerializer {
}
}

public async deserializeWebview(
webview: vscode.Webview,
public async deserializeWebviewEditor(
webview: vscode.WebviewEditor,
state: any
): Promise<void> {
const preview = await MarkdownPreview.revive(
Expand All @@ -103,8 +103,8 @@ export class MarkdownPreviewManager implements vscode.WebviewSerializer {
this.registerPreview(preview);
}

public async serializeWebview(
webview: vscode.Webview,
public async serializeWebviewEditor(
webview: vscode.WebviewEditor,
): Promise<any> {
const preview = this.previews.find(preview => preview.isWebviewOf(webview));
return preview ? preview.state : undefined;
Expand Down