Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add themes + update deps #177

Merged
merged 3 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions extension/ReactWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ReactWebView {
'Preview: openrpc.json',
vscode.ViewColumn.Two,
{
enableScripts: true
enableScripts: true,
}
);

Expand All @@ -53,10 +53,11 @@ export class ReactWebView {
const init = async () => {
if (vscode.window.activeTextEditor) {
try {
this.updateContent(await parseDocument(vscode.window.activeTextEditor.document.getText()));
setTimeout(() => {
const text = await parseDocument(vscode.window.activeTextEditor.document.getText());
setTimeout(async () => {
this.updateContent(text);
vscode.commands.executeCommand('workbench.action.navigateBack');
}, 300);
}, 1000);
} catch (error) {
// vscode.window.showErrorMessage(`Error parsing openrpc.json: ${error.message}`);
}
Expand All @@ -69,35 +70,47 @@ export class ReactWebView {

updateContent(data: any) {
if (ReactWebView.currentPanel) {
ReactWebView.currentPanel.webview.postMessage(data);
ReactWebView.currentPanel.webview.postMessage({
method: "updateSchema",
params: {
schema: data
}
});
}
}

getWebviewContent(extensionPath: string) {
const manifest = require(path.join(extensionPath, 'build', 'asset-manifest.json'));

// get main script file name
const mainScript = manifest.files['main.js'];

// get runtime script file name
const runtimeMainScript = manifest.files['runtime~main.js'];

// get all generated chunks names
const chunksRegex = /^(static)+(\/js)+(.)+(chunk\.js)$/;
const chunksRegex = /^((?!\.map|\.css|\.html).)*$/;
const chunkNames = Object.keys(manifest.files).filter(key => chunksRegex.test(key));

// Use a nonce to whitelist which scripts can be run
const nonce = v4();

const scripts = [mainScript, runtimeMainScript, ...chunkNames]
.map(scriptName => {
const scripts = [...chunkNames]
.map((scriptName) => {
const scriptUri = vscode.Uri
.file(path.join(extensionPath, 'build', scriptName))
.file(path.join(extensionPath, 'build', manifest.files[scriptName]))
.with({ scheme: 'vscode-resource' });

return `<script nonce="${nonce}" src="${scriptUri}"></script>`;
})
.join('');
.join("");

// get all generated chunks names
const cssChunksRegex = /\.css$/;
const cssChunkNames = Object.keys(manifest.files).filter(key => cssChunksRegex.test(key));
const cssIncludes = [...cssChunkNames]
.map((scriptName) => {
const cssUri = vscode.Uri
.file(path.join(extensionPath, 'build', manifest.files[scriptName]))
.with({ scheme: 'vscode-resource' });

return `<link nonce="${nonce}" href="${cssUri}" rel="stylesheet" type="text/css"/>`;
})
.join("");

return `<!DOCTYPE html>
<html lang="en">
Expand All @@ -106,15 +119,10 @@ export class ReactWebView {
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<title>React App</title>
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src vscode-resource: https:; script-src 'nonce-${nonce}';style-src vscode-resource: 'unsafe-inline' http: https: data:;">
<base href="${vscode.Uri.file(path.join(extensionPath, 'build')).with({ scheme: 'vscode-resource' })}/">
<style>
body {
background: white;
}
</style>
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src vscode-resource: https:; script-src 'nonce-${nonce}';style-src vscode-resource: 'unsafe-inline' http: https: data:;">
<base href="${vscode.Uri.file(path.join(extensionPath, 'build')).with({ scheme: 'vscode-resource' })}/">
${cssIncludes}
</head>

<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
Expand Down
6 changes: 5 additions & 1 deletion extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import createOrRevealWebView from './ReactWebview';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand('openrpc.previewDoc', () => {
createOrRevealWebView(context);
try {
createOrRevealWebView(context);
} catch (e) {
console.log("error", e);
}
})
);
}
Expand Down
Loading