|
1 | 1 | const vscode = require('vscode') |
| 2 | +const fs = require('fs') |
2 | 3 | const path = require('path') |
3 | | -const { writeFileSync } = require('fs') |
4 | 4 | const { homedir } = require('os') |
5 | 5 |
|
6 | 6 | const writeSerializedBlobToFile = (serializeBlob, fileName) => { |
7 | 7 | const bytes = new Uint8Array(serializeBlob.split(',')) |
8 | | - writeFileSync(fileName, new Buffer(bytes)) |
| 8 | + fs.writeFileSync(fileName, Buffer.from(bytes)) |
9 | 9 | } |
10 | 10 |
|
| 11 | +const P_TITLE = 'Polacode 📸' |
| 12 | + |
| 13 | +/** |
| 14 | + * @param {vscode.ExtensionContext} context |
| 15 | + */ |
11 | 16 | function activate(context) { |
12 | | - const htmlPath = path.resolve(context.extensionPath, 'src/webview/index.html') |
13 | | - const indexUri = vscode.Uri.file(htmlPath) |
| 17 | + const htmlPath = path.resolve(context.extensionPath, 'webview/index.html') |
14 | 18 |
|
15 | 19 | let lastUsedImageUri = vscode.Uri.file(path.resolve(homedir(), 'Desktop/code.png')) |
16 | | - vscode.commands.registerCommand('polacode.shoot', serializedBlob => { |
17 | | - vscode.window |
18 | | - .showSaveDialog({ |
19 | | - defaultUri: lastUsedImageUri, |
20 | | - filters: { |
21 | | - Images: ['png'] |
22 | | - } |
23 | | - }) |
24 | | - .then(uri => { |
25 | | - if (uri) { |
26 | | - writeSerializedBlobToFile(serializedBlob, uri.fsPath) |
27 | | - lastUsedImageUri = uri |
28 | | - } |
| 20 | + let panel |
| 21 | + |
| 22 | + vscode.window.registerWebviewPanelSerializer('polacode', { |
| 23 | + async deserializeWebviewPanel(_panel, state) { |
| 24 | + panel = _panel |
| 25 | + panel.webview.html = getHtmlContent(htmlPath) |
| 26 | + panel.webview.postMessage({ |
| 27 | + type: 'restore', |
| 28 | + innerHTML: state.innerHTML, |
| 29 | + bgColor: context.globalState.get('polacode.bgColor', '#2e3440') |
29 | 30 | }) |
| 31 | + setupSelectionSync() |
| 32 | + setupMessageListeners() |
| 33 | + } |
30 | 34 | }) |
31 | 35 |
|
32 | 36 | vscode.commands.registerCommand('polacode.activate', () => { |
33 | | - vscode.commands |
34 | | - .executeCommand('vscode.previewHtml', indexUri, 2, 'Polacode 📸', { |
35 | | - allowScripts: true |
36 | | - }) |
37 | | - .then(() => { |
38 | | - const fontFamily = vscode.workspace.getConfiguration('editor').fontFamily |
39 | | - const bgColor = context.globalState.get('polacode.bgColor', '#2e3440') |
40 | | - vscode.commands.executeCommand('_workbench.htmlPreview.postMessage', indexUri, { |
41 | | - type: 'init', |
42 | | - fontFamily, |
43 | | - bgColor |
44 | | - }) |
45 | | - }) |
| 37 | + panel = vscode.window.createWebviewPanel('polacode', P_TITLE, 2, { |
| 38 | + enableScripts: true, |
| 39 | + localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, 'webview'))] |
| 40 | + }) |
| 41 | + |
| 42 | + panel.webview.html = getHtmlContent(htmlPath) |
| 43 | + |
| 44 | + setupMessageListeners() |
| 45 | + |
| 46 | + const fontFamily = vscode.workspace.getConfiguration('editor').fontFamily |
| 47 | + const bgColor = context.globalState.get('polacode.bgColor', '#2e3440') |
| 48 | + panel.webview.postMessage({ |
| 49 | + type: 'init', |
| 50 | + fontFamily, |
| 51 | + bgColor |
| 52 | + }) |
| 53 | + |
| 54 | + syncSettings() |
46 | 55 | }) |
47 | 56 |
|
48 | | - vscode.window.onDidChangeTextEditorSelection(e => { |
49 | | - if (e.selections[0] && !e.selections[0].isEmpty) { |
50 | | - vscode.commands.executeCommand('editor.action.clipboardCopyAction') |
51 | | - vscode.commands.executeCommand('_workbench.htmlPreview.postMessage', indexUri, { |
52 | | - type: 'update' |
53 | | - }) |
| 57 | + vscode.workspace.onDidChangeConfiguration(e => { |
| 58 | + if (e.affectsConfiguration('polacode') || e.affectsConfiguration('editor')) { |
| 59 | + syncSettings() |
54 | 60 | } |
55 | 61 | }) |
56 | 62 |
|
57 | | - vscode.commands.registerCommand('polacode._onmessage', ({ type, data }) => { |
58 | | - if (type === 'updateBgColor') { |
59 | | - context.globalState.update('polacode.bgColor', data.bgColor) |
60 | | - } else if (type === 'invalidPasteContent') { |
61 | | - vscode.window.showInformationMessage( |
62 | | - 'Pasted content is invalid. Only copy from VS Code and check if your shortcuts for copy/paste have conflicts.' |
63 | | - ) |
64 | | - } |
| 63 | + setupSelectionSync() |
| 64 | + |
| 65 | + function setupMessageListeners() { |
| 66 | + panel.webview.onDidReceiveMessage(({ type, data }) => { |
| 67 | + switch (type) { |
| 68 | + case 'shoot': |
| 69 | + vscode.window |
| 70 | + .showSaveDialog({ |
| 71 | + defaultUri: lastUsedImageUri, |
| 72 | + filters: { |
| 73 | + Images: ['png'] |
| 74 | + } |
| 75 | + }) |
| 76 | + .then(uri => { |
| 77 | + if (uri) { |
| 78 | + writeSerializedBlobToFile(data.serializedBlob, uri.fsPath) |
| 79 | + lastUsedImageUri = uri |
| 80 | + } |
| 81 | + }) |
| 82 | + break |
| 83 | + case 'getAndUpdateCacheAndSettings': |
| 84 | + panel.webview.postMessage({ |
| 85 | + type: 'restoreBgColor', |
| 86 | + bgColor: context.globalState.get('polacode.bgColor', '#2e3440') |
| 87 | + }) |
| 88 | + |
| 89 | + syncSettings() |
| 90 | + break |
| 91 | + case 'updateBgColor': |
| 92 | + context.globalState.update('polacode.bgColor', data.bgColor) |
| 93 | + break |
| 94 | + case 'invalidPasteContent': |
| 95 | + vscode.window.showInformationMessage( |
| 96 | + 'Pasted content is invalid. Only copy from VS Code and check if your shortcuts for copy/paste have conflicts.' |
| 97 | + ) |
| 98 | + break |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + function syncSettings() { |
| 104 | + const settings = vscode.workspace.getConfiguration('polacode') |
| 105 | + const editorSettings = vscode.workspace.getConfiguration('editor') |
| 106 | + panel.webview.postMessage({ |
| 107 | + type: 'updateSettings', |
| 108 | + shadow: settings.get('shadow'), |
| 109 | + transparentBackground: settings.get('transparentBackground'), |
| 110 | + backgroundColor: settings.get('backgroundColor'), |
| 111 | + target: settings.get('target'), |
| 112 | + ligature: editorSettings.get('fontLigatures') |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + function setupSelectionSync() { |
| 117 | + vscode.window.onDidChangeTextEditorSelection(e => { |
| 118 | + if (e.selections[0] && !e.selections[0].isEmpty) { |
| 119 | + vscode.commands.executeCommand('editor.action.clipboardCopyAction') |
| 120 | + panel.postMessage({ |
| 121 | + type: 'update' |
| 122 | + }) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +function getHtmlContent(htmlPath) { |
| 129 | + const htmlContent = fs.readFileSync(htmlPath, 'utf-8') |
| 130 | + return htmlContent.replace(/script src="([^"]*)"/g, (match, src) => { |
| 131 | + const realSource = 'vscode-resource:' + path.resolve(htmlPath, '..', src) |
| 132 | + return `script src="${realSource}"` |
65 | 133 | }) |
66 | 134 | } |
67 | 135 |
|
|
0 commit comments