Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
fix #31
Browse files Browse the repository at this point in the history
  • Loading branch information
cssho committed May 31, 2017
1 parent 9b2f15d commit b9798de
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 48 deletions.
34 changes: 20 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-svgviewer",
"displayName": "SVG Viewer",
"description": "SVG Viewer for Visual Studio Code.",
"version": "1.4.1",
"version": "1.4.2",
"publisher": "cssho",
"engines": {
"vscode": "^1.9.0"
Expand All @@ -22,7 +22,8 @@
],
"main": "./out/src/extension",
"contributes": {
"commands": [{
"commands": [
{
"command": "svgviewer.open",
"title": "SVG Viewer: View SVG",
"icon": {
Expand Down Expand Up @@ -52,18 +53,23 @@
}
],
"menus": {
"editor/title": [{
"when": "resourceLangId == xml",
"command": "svgviewer.open",
"group": "navigation"
}],
"explorer/context": [{
"command": "svgviewer.openfile",
"group": "extension",
"when": "resourceLangId == xml"
}]
"editor/title": [
{
"when": "resourceLangId == xml",
"command": "svgviewer.open",
"group": "navigation"
}
],
"explorer/context": [
{
"command": "svgviewer.openfile",
"group": "extension",
"when": "resourceLangId == xml"
}
]
},
"keybindings": [{
"keybindings": [
{
"command": "svgviewer.open",
"key": "alt+shift+s o"
},
Expand Down Expand Up @@ -150,4 +156,4 @@
"viewer",
"image"
]
}
}
26 changes: 12 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import * as vscode from 'vscode';
import { SvgDocumentContentProvider, SvgFileContentProvider } from './svgProvider';
import { SvgDocumentContentProvider, SvgFileContentProvider, getSvgUri } from './svgProvider';
import { ExportDocumentContentProvider } from './exportProvider';

const exec = require('sync-exec');
Expand All @@ -23,8 +23,6 @@ export function activate(context: vscode.ExtensionContext) {
path.join(path.dirname(phantomjs.path), 'phantom', 'bin', 'phantomjs');
}

let previewUri = vscode.Uri.parse('svg-preview://authority/svg-preview');

let provider = new SvgDocumentContentProvider();
let registration = vscode.workspace.registerTextDocumentContentProvider('svg-preview', provider);

Expand All @@ -33,27 +31,27 @@ export function activate(context: vscode.ExtensionContext) {
vscode.workspace.onDidChangeTextDocument((e: vscode.TextDocumentChangeEvent) => {
if (vscode.window.activeTextEditor) {
if (e.document === vscode.window.activeTextEditor.document && !checkNoSvg(vscode.window.activeTextEditor.document, false)) {
provider.update(previewUri);
provider.update(getSvgUri(e.document.uri));
}
}
});

vscode.window.onDidChangeActiveTextEditor((textEditor: vscode.TextEditor) => {
if (vscode.window.activeTextEditor) {
if (textEditor.document === vscode.window.activeTextEditor.document && !checkNoSvg(vscode.window.activeTextEditor.document, false)) {
provider.update(previewUri);
provider.update(getSvgUri(textEditor.document.uri))
let auto = vscode.workspace.getConfiguration('svgviewer').get('enableautopreview');
if (auto) {
return openPreview(previewUri, textEditor.document.fileName);
return openPreview(textEditor.document.uri, textEditor.document.fileName);
}
}
}
});

let open = vscode.commands.registerTextEditorCommand('svgviewer.open', (te, t) => {
if (checkNoSvg(te.document)) return;
provider.update(previewUri);
return openPreview(previewUri, te.document.fileName);
provider.update(getSvgUri(te.document.uri))
return openPreview(te.document.uri, te.document.fileName);
});

context.subscriptions.push(open);
Expand All @@ -71,15 +69,15 @@ export function activate(context: vscode.ExtensionContext) {
let fName = vscode.workspace.asRelativePath(document.fileName);
let fileUriProvider = fileUriProviders.get(fName);
if (fileUriProvider == undefined) {
let fileUri = vscode.Uri.parse('svg-preview-' + fName.replace(/(\\|\/)/g, '-') + '://authority/svg-preview');
let fileUri = getSvgUri(uri);
let fileProvider = new SvgFileContentProvider(fileUri, document.fileName);
let fileRegistration = vscode.workspace.registerTextDocumentContentProvider('svg-preview-' + fName.replace(/(\\|\/)/g, '-'), fileProvider);
fileUriProviders.set(fName, { uri: fileUri, provider: fileProvider, registration: fileRegistration });
return openPreview(fileUri, fName);
let fileRegistration = vscode.workspace.registerTextDocumentContentProvider('svg-preview', fileProvider);
fileUriProvider = { uri: fileUri, provider: fileProvider, registration: fileRegistration };
fileUriProviders.set(fName, fileUriProvider);
} else {
fileUriProvider.provider.update(fileUriProvider.uri);
return openPreview(fileUriProvider.uri, fName);
}
return openPreview(fileUriProvider.uri, fName);
});

context.subscriptions.push(openfile);
Expand Down Expand Up @@ -226,7 +224,7 @@ function openPreview(previewUri: vscode.Uri, fileName: string) {
break;
}
if (viewColumn) {
return vscode.commands.executeCommand('vscode.previewHtml', previewUri, viewColumn, `Preview : ${fileName}`)
return vscode.commands.executeCommand('vscode.previewHtml', getSvgUri(previewUri), viewColumn, `Preview : ${fileName}`)
.then(s => console.log('done.'), vscode.window.showErrorMessage);
}
}
Expand Down
44 changes: 24 additions & 20 deletions src/svgProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,40 @@
import * as vscode from 'vscode';
import fs = require('fs')

export function getSvgUri(uri: vscode.Uri) {
if (uri.scheme === 'svg-preview') {
return uri;
}

return uri.with({
scheme: 'svg-preview',
path: uri.path + '.rendered',
query: uri.toString()
});
}

export class SvgDocumentContentProvider implements vscode.TextDocumentContentProvider {
private _onDidChange = new vscode.EventEmitter<vscode.Uri>();
private _waiting: boolean = false;

public provideTextDocumentContent(uri: vscode.Uri): string {
return this.createSvgSnippet();
public provideTextDocumentContent(uri: vscode.Uri): Thenable<string> {
let sourceUri = vscode.Uri.parse(uri.query);
console.log(sourceUri);
return vscode.workspace.openTextDocument(sourceUri).then(document => this.snippet(document.getText()));
}

get onDidChange(): vscode.Event<vscode.Uri> {
return this._onDidChange.event;
}

public update(uri: vscode.Uri) {
this._onDidChange.fire(uri);
}

private createSvgSnippet() {
return this.extractSnippet();
}

protected extractSnippet(): string {
let editor = vscode.window.activeTextEditor;
let text = editor ? editor.document.getText() : '';
return this.snippet(text);
}

private errorSnippet(error: string): string {
return `
<body>
${error}
</body>`;
if (!this._waiting) {
this._waiting = true;
setTimeout(() => {
this._waiting = false;
this._onDidChange.fire(uri);
}, 300);
}
}

protected snippet(properties): string {
Expand Down

0 comments on commit b9798de

Please sign in to comment.