-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathPreviewWebPanel.ts
169 lines (151 loc) · 4.88 KB
/
PreviewWebPanel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
let position : {x:0,y:0} = {
x: 0,
y: 0
};
export function previewAsyncAPI(context: vscode.ExtensionContext) {
return async (uri: vscode.Uri) => {
uri = uri || (await promptForAsyncapiFile()) as vscode.Uri;
if (uri) {
console.log('Opening asyncapi file', uri.fsPath);
openAsyncAPI(context, uri);
}
};
}
export const openAsyncapiFiles: { [id: string]: vscode.WebviewPanel } = {}; // vscode.Uri.fsPath => vscode.WebviewPanel
export function isAsyncAPIFile(document?: vscode.TextDocument) {
if (!document) {
return false;
}
if (document.languageId === 'json') {
try {
const json = JSON.parse(document.getText());
return json.asyncapi;
} catch (e) {
return false;
}
}
if (document.languageId === 'yml' || document.languageId === 'yaml') {
return document.getText().match('^asyncapi:') !== null;
}
return false;
}
export function openAsyncAPI(context: vscode.ExtensionContext, uri: vscode.Uri) {
const localResourceRoots = [
vscode.Uri.file(path.dirname(uri.fsPath)),
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/browser/standalone'),
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/styles'),
];
if (vscode.workspace.workspaceFolders) {
vscode.workspace.workspaceFolders.forEach(folder => {
localResourceRoots.push(folder.uri);
});
}
const panel: vscode.WebviewPanel =
openAsyncapiFiles[uri.fsPath] ||
vscode.window.createWebviewPanel('asyncapi-preview', '', vscode.ViewColumn.Two, {
enableScripts: true,
retainContextWhenHidden: true,
localResourceRoots,
});
panel.title = path.basename(uri.fsPath);
panel.webview.html = getWebviewContent(context, panel.webview, uri, position);
panel.webview.onDidReceiveMessage(
message => {
switch (message.type) {
case 'position':{
position = {
x: message.scrollX,
y: message.scrollY
};
}
}
},
undefined,
context.subscriptions
);
panel.onDidDispose(() => {
delete openAsyncapiFiles[uri.fsPath];
});
openAsyncapiFiles[uri.fsPath] = panel;
}
export async function promptForAsyncapiFile() {
if (isAsyncAPIFile(vscode.window.activeTextEditor?.document)) {
return vscode.window.activeTextEditor?.document.uri;
}
const uris = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
openLabel: 'Open AsyncAPI file',
filters: {
asyncAPI: ['yml', 'yaml', 'json'],
},
});
return uris?.[0];
}
function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Webview, asyncapiFile: vscode.Uri, position: {x:0,y:0}) {
const asyncapiComponentJs = webview.asWebviewUri(
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/browser/standalone/index.js')
);
const asyncapiComponentCss = webview.asWebviewUri(
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/styles/default.min.css')
);
const asyncapiWebviewUri = webview.asWebviewUri(asyncapiFile);
const asyncapiBasePath = asyncapiWebviewUri.toString().replace('%2B', '+'); // this is loaded by a different library so it requires unescaping the + character
const asyncapiContent = fs.readFileSync(asyncapiFile.fsPath, 'utf-8');
const html = `
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="${asyncapiComponentCss}">
<style>
html{
scroll-behavior: smooth;
}
body {
color: #121212;
background-color: #fff;
word-wrap: break-word;
}
h1 {
color: #121212;
}
</style>
</head>
<body x-timestamp="${Date.now()}">
<div id="asyncapi"></div>
<script src="${asyncapiComponentJs}"></script>
<script>
const vscode = acquireVsCodeApi();
AsyncApiStandalone.render({
schema: {
url: '${asyncapiWebviewUri}',
options: { method: "GET", mode: "cors" },
},
config: {
show: {
sidebar: true,
errors: true,
},
parserOptions: { path: '${asyncapiBasePath}' }
},
}, document.getElementById('asyncapi'));
window.addEventListener('scrollend', event => {
vscode.postMessage({
type: 'position',
scrollX: window.scrollX || 0,
scrollY: window.scrollY || 0
});
});
window.addEventListener("load", (event) => {
setTimeout(()=>{window.scrollBy('${position.x}','${position.y}')},1000)
});
</script>
</body>
</html>
`;
return html;
}