From b9798de6e8010ddc296ae134d19f30e169cf7529 Mon Sep 17 00:00:00 2001 From: cssho Date: Wed, 31 May 2017 13:36:45 +0900 Subject: [PATCH] fix #31 --- package.json | 34 ++++++++++++++++++++-------------- src/extension.ts | 26 ++++++++++++-------------- src/svgProvider.ts | 44 ++++++++++++++++++++++++-------------------- 3 files changed, 56 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index 1c0da87..8613d63 100644 --- a/package.json +++ b/package.json @@ -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" @@ -22,7 +22,8 @@ ], "main": "./out/src/extension", "contributes": { - "commands": [{ + "commands": [ + { "command": "svgviewer.open", "title": "SVG Viewer: View SVG", "icon": { @@ -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" }, @@ -150,4 +156,4 @@ "viewer", "image" ] -} \ No newline at end of file +} diff --git a/src/extension.ts b/src/extension.ts index fbf12ac..9c72547 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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'); @@ -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); @@ -33,7 +31,7 @@ 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)); } } }); @@ -41,10 +39,10 @@ export function activate(context: vscode.ExtensionContext) { 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); } } } @@ -52,8 +50,8 @@ export function activate(context: vscode.ExtensionContext) { 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); @@ -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); @@ -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); } } diff --git a/src/svgProvider.ts b/src/svgProvider.ts index 3f31d29..29e3864 100644 --- a/src/svgProvider.ts +++ b/src/svgProvider.ts @@ -3,11 +3,26 @@ 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(); + private _waiting: boolean = false; - public provideTextDocumentContent(uri: vscode.Uri): string { - return this.createSvgSnippet(); + public provideTextDocumentContent(uri: vscode.Uri): Thenable { + 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 { @@ -15,24 +30,13 @@ export class SvgDocumentContentProvider implements vscode.TextDocumentContentPro } 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 ` - - ${error} - `; + if (!this._waiting) { + this._waiting = true; + setTimeout(() => { + this._waiting = false; + this._onDidChange.fire(uri); + }, 300); + } } protected snippet(properties): string {