Skip to content

Commit

Permalink
#2 added vega docs scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed Feb 6, 2019
1 parent 01304ef commit 160c7ea
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ in [VSCode](https://github.com/Microsoft/vscode)

# Features

- Create Vega document command
- Vega and Vega-Lite charts Preview
- Local data files support
- SVG and PNG export

# Usage

1. Run `Vega: Preview` command from `View -> Command Pallette...` menu
on open `.vega`, `.vg.json` or `.vl.json` Vega spec document
1. Run `Vega: Create Vega document` command from `View -> Command Pallette...` menu
to create and save new Vega or Vega-Lite document with the corresponding Vega JSON schema reference.

2. Save updated Vega spec JSON document to Preview updated Vega viz
2. Run `Vega: Preview` command from `View -> Command Pallette...` menu
on open `.vega`, `.vg.json` or `.vl.json` Vega spec document to Preview it.

3. Save updated Vega spec JSON document to Preview updated Vega viz.

## Example: [Vega Contour Plot Preview](https://vega.github.io/vega/examples/contour-plot/)

Expand Down
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-vega-viewer",
"displayName": "Vega Viewer",
"description": "Vega Viewer",
"version": "1.5.1",
"version": "1.6.0",
"publisher": "RandomFractalsInc",
"author": "Taras Novak",
"contributors": [
Expand Down Expand Up @@ -40,12 +40,21 @@
"command": "vega.preview",
"title": "Preview",
"category": "Vega"
},
{
"command": "vega.create",
"title": "Create Vega document",
"category": "Vega"
}
],
"keybindings": [
{
"command": "vega.preview",
"key": "ctrl+alt+v"
},
{
"command": "vega.create",
"key": "ctrl+alt+c"
}
],
"languages": [
Expand Down Expand Up @@ -90,21 +99,21 @@
"menus": {
"explorer/context": [
{
"when": "resourceLangId == json",
"when": "resourceLangId == json || resourceLangId == vega || resourceLangId == vg || resourceLangId == vl",
"command": "vega.preview",
"group": "navigation"
}
],
"editor/title": [
{
"when": "resourceLangId == json",
"when": "resourceLangId == json || resourceLangId == vega || resourceLangId == vg || resourceLangId == vl",
"command": "vega.preview",
"group": "navigation"
}
],
"editor/title/context": [
{
"when": "resourceLangId == json",
"when": "resourceLangId == json || resourceLangId == vega || resourceLangId == vg || resourceLangId == vl",
"command": "vega.preview",
"group": "navigation"
}
Expand Down
31 changes: 31 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TextDocument,
TextDocumentChangeEvent
} from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { VegaPreview, VegaPreviewSerializer } from './vega.preview';
import { previewManager } from './preview.manager';
Expand All @@ -34,6 +35,14 @@ export function activate(context: ExtensionContext) {
window.registerWebviewPanelSerializer('vega.preview',
new VegaPreviewSerializer(context.extensionPath, previewTemplate.content));

// Vega: Create Vega document
let createVegaDocumentCommand: Disposable = commands.registerCommand('vega.create', () =>
createVegaDocument(
templateManager.getTemplate('vega.vg.json').content,
templateManager.getTemplate('vega.lite.vl.json').content)
);
context.subscriptions.push(createVegaDocumentCommand);

// Vega: Preview command
let vegaWebview: Disposable = commands.registerCommand('vega.preview', (uri) => {
let resource: any = uri;
Expand Down Expand Up @@ -100,3 +109,25 @@ function getViewColumn(): ViewColumn {
const activeEditor = window.activeTextEditor;
return activeEditor ? (activeEditor.viewColumn + 1) : ViewColumn.One;
}

async function createVegaDocument(vegaTemplate: string, vegaLiteTemplate: string): Promise<void> {
const vegaFileUri: Uri = await window.showSaveDialog({
defaultUri: Uri.parse(path.join(workspace.rootPath, 'chart')).with({scheme: 'file'}),
filters: {
'Vega Document': ['vg'],
'Vega-Lite Document': ['vl']
}
});
if (vegaFileUri) {
const vegaContent: string = vegaFileUri.fsPath.endsWith('.vg') ? vegaTemplate : vegaLiteTemplate;
fs.writeFile(vegaFileUri.fsPath, vegaContent, (error) => {
if (error) {
window.showErrorMessage(`Failed to create Vega document: ${vegaFileUri.fsPath}`);
} else {
workspace.openTextDocument(vegaFileUri).then(document => {
window.showTextDocument(document);
});
}
});
}
}
8 changes: 4 additions & 4 deletions templates/vega.lite.vl.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"description": "A simple bar chart with embedded data.",
"description": "simple bar chart with embedded data",
"data": {
"values": [
{"a": "A","b": 28}, {"a": "B","b": 55}, {"a": "C","b": 43},
{"a": "D","b": 91}, {"a": "E","b": 81}, {"a": "F","b": 53},
{"a": "G","b": 19}, {"a": "H","b": 87}, {"a": "I","b": 52}
{"a": "A","b": 28},
{"a": "B","b": 55},
{"a": "C","b": 43}
]
},
"mark": "bar",
Expand Down

0 comments on commit 160c7ea

Please sign in to comment.