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

Commit

Permalink
(refs #15)Support SVGZ format.
Browse files Browse the repository at this point in the history
  • Loading branch information
cssho committed May 26, 2016
1 parent a25e7f4 commit 295a52a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
11 changes: 10 additions & 1 deletion 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.1.11",
"version": "1.2.0",
"publisher": "cssho",
"engines": {
"vscode": "^0.10.8"
Expand All @@ -12,6 +12,7 @@
],
"activationEvents": [
"onCommand:svgviewer.open",
"onCommand:svgviewer.opensvgz",
"onCommand:svgviewer.saveas",
"onCommand:svgviewer.saveassize",
"onCommand:svgviewer.copydui"
Expand All @@ -23,6 +24,10 @@
"command": "svgviewer.open",
"title": "SVG Viewer: View SVG"
},
{
"command": "svgviewer.opensvgz",
"title": "SVG Viewer: View SVGZ"
},
{
"command": "svgviewer.saveas",
"title": "SVG Viewer: Export PNG"
Expand All @@ -41,6 +46,10 @@
"command": "svgviewer.open",
"key": "alt+s o"
},
{
"command": "svgviewer.opensvgz",
"key": "alt+s z"
},
{
"command": "svgviewer.saveas",
"key": "alt+s e"
Expand Down
41 changes: 41 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fs = require('pn/fs');
const svg2png = require('svg2png');
const tmp = require('tmp');
const cp = require('copy-paste');
const zlib = require('zlib');

export function activate(context: vscode.ExtensionContext) {

Expand Down Expand Up @@ -81,6 +82,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)) return;
let editor = vscode.window.activeTextEditor;
Expand Down Expand Up @@ -154,6 +186,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() {
}

0 comments on commit 295a52a

Please sign in to comment.