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 Jan 30, 2017
1 parent 45fdeb4 commit 5a5cfaf
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
}

0 comments on commit 5a5cfaf

Please sign in to comment.