Skip to content
This repository was archived by the owner on Jul 29, 2019. It is now read-only.

Commit e68b4c9

Browse files
Vincenzo ChianeseXVincentX
Vincenzo Chianese
authored and
XVincentX
committedNov 12, 2016
Merge pull request #65 from XVincentX/XVincentX/preview-api
Preview an API document on Apiary command closes #63
2 parents 56dbcc5 + c4f57bc commit e68b4c9

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed
 

‎client/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This changelog tracks changes starting from first public release.
44

5+
## v0.6.0
6+
7+
- It is now possible to preview the documentation you're working now currently in Apiary using the appropriate feature. Just look for Apiary: Preview command.
8+
59
## v0.5.0
610

711
- It is now possible to configure what you think it's a Symbol to report and not. All of them are extension parameters and are enabled by default.

‎client/package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "VSCode client for API Elements",
55
"author": "Vincenzo Chianese",
66
"license": "MIT",
7-
"version": "0.5.0",
7+
"version": "0.6.0",
88
"preview": false,
99
"icon": "logo.png",
1010
"bugs": {
@@ -17,7 +17,7 @@
1717
},
1818
"publisher": "vncz",
1919
"engines": {
20-
"vscode": "^1.5.0"
20+
"vscode": "^1.7.0"
2121
},
2222
"categories": [
2323
"Languages",
@@ -173,6 +173,12 @@
173173
"title": "Browses the current API on Apiary documentation",
174174
"category": "API Elements - Apiary",
175175
"icon": "./resources/apiary.ico"
176+
},
177+
{
178+
"command": "apiElements.apiary.preview",
179+
"title": "Previews the current API on Apiary documentation",
180+
"category": "API Elements - Apiary",
181+
"icon": "./resources/apiary.ico"
176182
}
177183
]
178184
},
@@ -188,6 +194,7 @@
188194
},
189195
"dependencies": {
190196
"axios": "^0.14.0",
197+
"lodash.escape": "^4.0.1",
191198
"vscode-languageclient": "^2.4.2"
192199
}
193200
}

‎client/src/commands.ts

+50-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import * as path from 'path';
44
import {killCurrentApiaryClient, requestApiaryClient} from './requestApiaryClient';
55
import {showMessage} from './showMessage';
66
import {showUntitledWindow} from './showUntitledWindow';
7-
import {ExtensionContext, Position, QuickPickItem, Range, TextEditor, Uri, commands, window} from 'vscode';
7+
import {ExtensionContext, Position, QuickPickItem, Range, TextEditor, Uri,
8+
ViewColumn, commands, window, workspace} from 'vscode';
89
import {LanguageClient} from 'vscode-languageclient';
910

1011
import axios from 'axios';
1112

13+
const escape = require('lodash.escape');
14+
1215
function selectApi(context: ExtensionContext) {
1316
return requestApiaryClient(context)
1417
.then(client => Promise.all([client.getApiList(), client]))
@@ -82,6 +85,32 @@ export function publishApi(context: ExtensionContext, textEditor: TextEditor) {
8285
);
8386
}
8487

88+
export function previewApi(context: ExtensionContext, textEditor: TextEditor) {
89+
const code = escape(textEditor.document.getText());
90+
const preview =
91+
`<!DOCTYPE html>
92+
<html lang="en">
93+
<head>
94+
<meta charset="UTF-8">
95+
<title>API Preview</title>
96+
</head>
97+
<body>
98+
<script src="https://api.apiary.io/seeds/embed.js"></script>
99+
<script>
100+
var embed = new Apiary.Embed({
101+
apiBlueprint: \`${code}\`,
102+
});
103+
</script>
104+
</body>
105+
</html>`;
106+
107+
const filePath = path.join(workspace.rootPath || context.extensionPath, 'preview.html');
108+
fs.writeFileSync(filePath, preview, 'utf8');
109+
110+
return commands.executeCommand('vscode.previewHtml', Uri.parse(`file:${filePath}`), getViewColumn())
111+
.then(() => fs.unlinkSync(filePath));
112+
}
113+
85114
export function logout(context: ExtensionContext) {
86115
const tokenFilePath = path.join(context.extensionPath, '.apiaryToken');
87116
if (fs.existsSync(path.join(context.extensionPath, '.apiaryToken'))) {
@@ -107,3 +136,23 @@ export function browse(context: ExtensionContext, textEditor: TextEditor) {
107136
})
108137
.then(uri => commands.executeCommand('vscode.open', uri), <any>showMessage);
109138
}
139+
140+
function getViewColumn(sideBySide = true): ViewColumn {
141+
const active = window.activeTextEditor;
142+
if (!active) {
143+
return ViewColumn.One;
144+
}
145+
146+
if (!sideBySide) {
147+
return active.viewColumn;
148+
}
149+
150+
switch (active.viewColumn) {
151+
case ViewColumn.One:
152+
return ViewColumn.Two;
153+
case ViewColumn.Two:
154+
return ViewColumn.Three;
155+
default:
156+
return active.viewColumn;
157+
}
158+
}

‎client/src/extension.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ function registerCommands(client: LanguageClient, context: ExtensionContext) {
1515
commands.registerCommand('apiElements.apiary.fetchApi', Commands.fetchApi.bind(this, context)),
1616
commands.registerCommand('apiElements.apiary.logout', Commands.logout.bind(this, context)),
1717
commands.registerTextEditorCommand('apiElements.apiary.publishApi', Commands.publishApi.bind(this, context)),
18-
commands.registerTextEditorCommand('apiElements.apiary.browse', Commands.browse.bind(this, context))
18+
commands.registerTextEditorCommand('apiElements.apiary.browse', Commands.browse.bind(this, context)),
19+
commands.registerTextEditorCommand('apiElements.apiary.preview', Commands.previewApi.bind(this, context))
1920
);
2021
}
2122

‎server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vscode-apielements",
33
"description": "VSCode language server for ApiElements",
4-
"version": "0.5.0",
4+
"version": "0.6.0",
55
"author": "Vincenzo Chianese",
66
"license": "MIT",
77
"engines": {

0 commit comments

Comments
 (0)
This repository has been archived.