From 5a5cfaf9ed069378696ae83527494009532db744 Mon Sep 17 00:00:00 2001 From: cosho Date: Thu, 19 May 2016 02:22:18 +0900 Subject: [PATCH] (refs #15)Support SVGZ format. --- src/extension.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/extension.ts b/src/extension.ts index 92a7f67..6c2dd1a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -11,6 +11,8 @@ const cp = require('copy-paste'); const svgexport = require('svgexport'); const path = require('path'); const phantomjs = require('phantomjs-prebuilt'); +const zlib = require('zlib'); + export function activate(context: vscode.ExtensionContext) { // Check PhantomJS Binary @@ -44,6 +46,37 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(open); + let openz = vscode.commands.registerCommand('svgviewer.opensvgz', () => { + vscode.window.showInputBox({ + prompt: `Set svgz file path.`, + validateInput: checkSVGZPath + }).then(path => { + if (path) { + fs.readFile(path).then(file => { + zlib.gunzip(file, function (err, data) { + if (err) { + vscode.window.showErrorMessage('Gunzip: ' + err.toString()); + return; + } + let svg = (new Buffer(data, 'utf8')).toString() + vscode.commands.executeCommand('workbench.action.files.newUntitledFile'); + let editor = vscode.window.activeTextEditor; + while (!editor) { + console.log(Date.now()); + } + //setText(svg).then(console.log, console.error); + editor.edit(eb => { eb.insert(new vscode.Position(0, 0), svg); }); + // vscode.TextEdit.insert(new vscode.Position(0, 0), svg); + //editor.document.languageId = 'xml'; + + }); + }); + } + }) + }); + + context.subscriptions.push(openz); + let saveas = vscode.commands.registerTextEditorCommand('svgviewer.saveas', (te, t) => { if (checkNoSvg(te.document)) return; let editor = vscode.window.activeTextEditor; @@ -164,6 +197,15 @@ function exportPng(tmpobj: any, text: string, pngpath: string, w?: number, h?: n }) .catch(e => vscode.window.showErrorMessage(e.message)); } +function checkSVGZPath(value: string): string { + try { + fs.accessSync(value, fs.F_OK); + if (value.toLowerCase().endsWith('.svgz')) return null; + } catch (e) { + + } + return 'Please set a correct svgz file path.'; +} export function deactivate() { }