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

Webview API prototype 3 #44307

Merged
merged 21 commits into from
Feb 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions extensions/markdown/media/csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
return;
}
didShow = true;
const args = [settings.previewUri];

const notification = document.createElement('a');
notification.innerText = strings.cspAlertMessageText;
Expand All @@ -25,8 +24,12 @@

notification.setAttribute('role', 'button');
notification.setAttribute('aria-label', strings.cspAlertMessageLabel);
notification.setAttribute('href', `command:markdown.showPreviewSecuritySelector?${encodeURIComponent(JSON.stringify(args))}`);

notification.onclick = () => {
window.parent.postMessage({
command: 'markdown.showPreviewSecuritySelector',
args: [settings.source]
}, '*');
};
document.body.appendChild(notification);
};

Expand Down
17 changes: 11 additions & 6 deletions extensions/markdown/media/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
} else {
scrollTo = previous.element.getBoundingClientRect().top;
}
window.scroll(0, window.scrollY + scrollTo + getSourceRevealAddedOffset());
window.scroll(0, Math.max(1, window.scrollY + scrollTo + getSourceRevealAddedOffset()));
}
}

Expand Down Expand Up @@ -193,13 +193,13 @@

function onLoad() {
if (settings.scrollPreviewWithEditorSelection) {
const initialLine = +settings.line;
if (!isNaN(initialLine)) {
setTimeout(() => {
setTimeout(() => {
const initialLine = +settings.line;
if (!isNaN(initialLine)) {
scrollDisabled = true;
scrollToRevealSourceLine(initialLine);
}, 0);
}
}
}, 0);
}
}

Expand All @@ -220,8 +220,13 @@
scrollToRevealSourceLine(line);
}, 50);
return event => {
if (event.data.source !== settings.source) {
return;
}

const line = +event.data.line;
if (!isNaN(line)) {
settings.line = line;
doScroll(line);
}
};
Expand Down
16 changes: 4 additions & 12 deletions extensions/markdown/src/commands/refreshPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { Command } from '../commandManager';
import { isMarkdownFile, getMarkdownUri, MarkdownPreviewWebviewManager } from '../features/previewContentProvider';
import { MarkdownPreviewManager } from '../features/previewContentProvider';

export class RefreshPreviewCommand implements Command {
public readonly id = 'markdown.refreshPreview';

public constructor(
private readonly webviewManager: MarkdownPreviewWebviewManager
private readonly webviewManager: MarkdownPreviewManager
) { }

public execute(resource: string | undefined) {
if (resource) {
const source = vscode.Uri.parse(resource);
this.webviewManager.update(source);
} else if (vscode.window.activeTextEditor && isMarkdownFile(vscode.window.activeTextEditor.document)) {
this.webviewManager.update(getMarkdownUri(vscode.window.activeTextEditor.document.uri));
} else {
this.webviewManager.updateAll();
}
public execute() {
this.webviewManager.refresh();
}
}
17 changes: 8 additions & 9 deletions extensions/markdown/src/commands/showPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as vscode from 'vscode';

import { Command } from '../commandManager';
import { MarkdownPreviewWebviewManager, } from '../features/previewContentProvider';
import { MarkdownPreviewManager, } from '../features/previewContentProvider';
import { TelemetryReporter } from '../telemetryReporter';


Expand All @@ -30,12 +30,12 @@ function getViewColumn(sideBySide: boolean): vscode.ViewColumn | undefined {
return active.viewColumn;
}

function showPreview(
webviewManager: MarkdownPreviewWebviewManager,
async function showPreview(
webviewManager: MarkdownPreviewManager,
telemetryReporter: TelemetryReporter,
uri?: vscode.Uri,
sideBySide: boolean = false,
) {
): Promise<any> {
let resource = uri;
if (!(resource instanceof vscode.Uri)) {
if (vscode.window.activeTextEditor) {
Expand All @@ -53,23 +53,22 @@ function showPreview(
return;
}

const view = webviewManager.create(
webviewManager.preview(
resource,
(vscode.window.activeTextEditor && vscode.window.activeTextEditor.viewColumn) || vscode.ViewColumn.One,
getViewColumn(sideBySide) || vscode.ViewColumn.Active);

telemetryReporter.sendTelemetryEvent('openPreview', {
where: sideBySide ? 'sideBySide' : 'inPlace',
how: (uri instanceof vscode.Uri) ? 'action' : 'pallete'
});

return view;
}

export class ShowPreviewCommand implements Command {
public readonly id = 'markdown.showPreview';

public constructor(
private readonly webviewManager: MarkdownPreviewWebviewManager,
private readonly webviewManager: MarkdownPreviewManager,
private readonly telemetryReporter: TelemetryReporter
) { }

Expand All @@ -84,7 +83,7 @@ export class ShowPreviewToSideCommand implements Command {
public readonly id = 'markdown.showPreviewToSide';

public constructor(
private readonly webviewManager: MarkdownPreviewWebviewManager,
private readonly webviewManager: MarkdownPreviewManager,
private readonly telemetryReporter: TelemetryReporter
) { }

Expand Down
29 changes: 8 additions & 21 deletions extensions/markdown/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { loadDefaultTelemetryReporter } from './telemetryReporter';
import { loadMarkdownExtensions } from './markdownExtensions';
import LinkProvider from './features/documentLinkProvider';
import MDDocumentSymbolProvider from './features/documentSymbolProvider';
import { MarkdownContentProvider, getMarkdownUri, isMarkdownFile, MarkdownPreviewWebviewManager } from './features/previewContentProvider';
import { MarkdownContentProvider, MarkdownPreviewManager } from './features/previewContentProvider';


export function activate(context: vscode.ExtensionContext) {
Expand All @@ -30,20 +30,20 @@ export function activate(context: vscode.ExtensionContext) {
const contentProvider = new MarkdownContentProvider(engine, context, cspArbiter, logger);
loadMarkdownExtensions(contentProvider, engine);

const webviewManager = new MarkdownPreviewWebviewManager(contentProvider);
context.subscriptions.push(webviewManager);
const previewManager = new MarkdownPreviewManager(contentProvider, logger);
context.subscriptions.push(previewManager);

context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(selector, new MDDocumentSymbolProvider(engine)));
context.subscriptions.push(vscode.languages.registerDocumentLinkProvider(selector, new LinkProvider()));

const previewSecuritySelector = new PreviewSecuritySelector(cspArbiter, webviewManager);
const previewSecuritySelector = new PreviewSecuritySelector(cspArbiter, previewManager);

const commandManager = new CommandManager();
context.subscriptions.push(commandManager);
commandManager.register(new commands.ShowPreviewCommand(webviewManager, telemetryReporter));
commandManager.register(new commands.ShowPreviewToSideCommand(webviewManager, telemetryReporter));
commandManager.register(new commands.ShowPreviewCommand(previewManager, telemetryReporter));
commandManager.register(new commands.ShowPreviewToSideCommand(previewManager, telemetryReporter));
commandManager.register(new commands.ShowSourceCommand());
commandManager.register(new commands.RefreshPreviewCommand(webviewManager));
commandManager.register(new commands.RefreshPreviewCommand(previewManager));
commandManager.register(new commands.RevealLineCommand(logger));
commandManager.register(new commands.MoveCursorToPositionCommand());
commandManager.register(new commands.ShowPreviewSecuritySelectorCommand(previewSecuritySelector));
Expand All @@ -53,19 +53,6 @@ export function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => {
logger.updateConfiguration();
webviewManager.updateConfiguration();
}));

context.subscriptions.push(vscode.window.onDidChangeTextEditorSelection(event => {
if (isMarkdownFile(event.textEditor.document)) {
const markdownFile = getMarkdownUri(event.textEditor.document.uri);
logger.log('updatePreviewForSelection', { markdownFile: markdownFile.toString() });

vscode.commands.executeCommand('_workbench.htmlPreview.postMessage',
markdownFile,
{
line: event.selections[0].active.line
});
}
previewManager.updateConfiguration();
}));
}
Loading