Skip to content

Commit 1e7a92e

Browse files
committed
Restore original functionality in webview API
1 parent 8cdd0bc commit 1e7a92e

File tree

3 files changed

+205
-199
lines changed

3 files changed

+205
-199
lines changed

src/extension.js

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,81 @@ const { homedir } = require('os')
66

77
const writeSerializedBlobToFile = (serializeBlob, fileName) => {
88
const bytes = new Uint8Array(serializeBlob.split(','))
9-
writeFileSync(fileName, new Buffer(bytes))
9+
writeFileSync(fileName, Buffer.from(bytes))
1010
}
1111

1212
const P_TITLE = 'Polacode 📸'
1313

1414
/**
15-
* @param {vscode.ExtensionContext} context
15+
* @param {vscode.ExtensionContext} context
1616
*/
1717
function activate(context) {
1818
const htmlPath = path.resolve(context.extensionPath, 'webview/index.html')
1919
const indexUri = vscode.Uri.file(htmlPath)
2020

2121
let lastUsedImageUri = vscode.Uri.file(path.resolve(homedir(), 'Desktop/code.png'))
22-
vscode.commands.registerCommand('polacode.shoot', serializedBlob => {
23-
vscode.window
24-
.showSaveDialog({
25-
defaultUri: lastUsedImageUri,
26-
filters: {
27-
Images: ['png']
28-
}
29-
})
30-
.then(uri => {
31-
if (uri) {
32-
writeSerializedBlobToFile(serializedBlob, uri.fsPath)
33-
lastUsedImageUri = uri
34-
}
35-
})
36-
})
22+
let panel
3723

3824
vscode.commands.registerCommand('polacode.activate', () => {
39-
const panel = vscode.window.createWebviewPanel('polacode', P_TITLE, 2, {
25+
panel = vscode.window.createWebviewPanel('polacode', P_TITLE, 2, {
4026
enableScripts: true,
41-
localResourceRoots: [
42-
vscode.Uri.file(path.join(context.extensionPath, 'webview'))
43-
]
44-
});
45-
46-
panel.webview.html = fs.readFileSync(htmlPath, 'utf-8')
27+
localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, 'webview'))]
28+
})
29+
30+
panel.webview.html = getHtmlContent(htmlPath)
4731

48-
// vscode.commands
49-
// .executeCommand('vscode.previewHtml', indexUri, 2, 'Polacode 📸', {
50-
// allowScripts: true,
51-
// })
52-
// .then(() => {
53-
// const fontFamily = vscode.workspace.getConfiguration('editor').fontFamily
54-
// const bgColor = context.globalState.get('polacode.bgColor', '#2e3440')
55-
// vscode.commands.executeCommand('_workbench.htmlPreview.postMessage', indexUri, {
56-
// type: 'init',
57-
// fontFamily,
58-
// bgColor
59-
// })
60-
// })
32+
panel.webview.onDidReceiveMessage(({ type, data }) => {
33+
switch (type) {
34+
case 'shoot':
35+
vscode.window
36+
.showSaveDialog({
37+
defaultUri: lastUsedImageUri,
38+
filters: {
39+
Images: ['png']
40+
}
41+
})
42+
.then(uri => {
43+
if (uri) {
44+
writeSerializedBlobToFile(data.serializedBlob, uri.fsPath)
45+
lastUsedImageUri = uri
46+
}
47+
})
48+
break
49+
case 'updateBgColor':
50+
context.globalState.update('polacode.bgColor', data.bgColor)
51+
break
52+
case 'invalidPasteContent':
53+
vscode.window.showInformationMessage(
54+
'Pasted content is invalid. Only copy from VS Code and check if your shortcuts for copy/paste have conflicts.'
55+
)
56+
break
57+
}
58+
})
59+
60+
const fontFamily = vscode.workspace.getConfiguration('editor').fontFamily
61+
const bgColor = context.globalState.get('polacode.bgColor', '#2e3440')
62+
panel.webview.postMessage({
63+
type: 'init',
64+
fontFamily,
65+
bgColor
66+
})
6167
})
6268

6369
vscode.window.onDidChangeTextEditorSelection(e => {
6470
if (e.selections[0] && !e.selections[0].isEmpty) {
6571
vscode.commands.executeCommand('editor.action.clipboardCopyAction')
66-
vscode.commands.executeCommand('_workbench.htmlPreview.postMessage', indexUri, {
72+
panel.postMessage({
6773
type: 'update'
6874
})
6975
}
7076
})
77+
}
7178

72-
vscode.commands.registerCommand('polacode._onmessage', ({ type, data }) => {
73-
if (type === 'updateBgColor') {
74-
context.globalState.update('polacode.bgColor', data.bgColor)
75-
} else if (type === 'invalidPasteContent') {
76-
vscode.window.showInformationMessage(
77-
'Pasted content is invalid. Only copy from VS Code and check if your shortcuts for copy/paste have conflicts.'
78-
)
79-
}
79+
function getHtmlContent(htmlPath) {
80+
const htmlContent = fs.readFileSync(htmlPath, 'utf-8')
81+
return htmlContent.replace(/script src="([^"]*)"/g, (match, src) => {
82+
const realSource = 'vscode-resource:' + path.resolve(htmlPath, '..', src)
83+
return `script src="${realSource}"`
8084
})
8185
}
8286

webview/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<html>
22
<head>
3-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src 'self' https: data:; media-src 'none'; child-src 'self'; object-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; connect-src 'self' https:; font-src 'self' https:;">
3+
<meta
4+
http-equiv="Content-Security-Policy"
5+
content="img-src vscode-resource: data: https:; script-src vscode-resource:; style-src 'unsafe-inline' vscode-resource:;"
6+
/>
47
<style>
58
html {
69
box-sizing: border-box;

0 commit comments

Comments
 (0)