-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathextension.js
More file actions
142 lines (123 loc) · 4.24 KB
/
extension.js
File metadata and controls
142 lines (123 loc) · 4.24 KB
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
const vscode = require('vscode')
const fs = require('fs')
const path = require('path')
const { homedir } = require('os')
const writeSerializedBlobToFile = (serializeBlob, fileName) => {
const bytes = new Uint8Array(serializeBlob.split(','))
fs.writeFileSync(fileName, Buffer.from(bytes))
}
const P_TITLE = 'Polacode 📸'
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const htmlPath = path.resolve(context.extensionPath, 'webview/index.html')
let lastUsedImageUri = vscode.Uri.file(path.resolve(homedir(), 'Desktop/code.png'))
let panel
vscode.window.registerWebviewPanelSerializer('polacode', {
async deserializeWebviewPanel(_panel, state) {
panel = _panel
panel.webview.html = getHtmlContent()
panel.webview.postMessage({
type: 'restore',
innerHTML: state.innerHTML,
bgColor: context.globalState.get('polacode.bgColor', '#2e3440')
})
const selectionListener = setupSelectionSync()
panel.onDidDispose(() => {
selectionListener.dispose()
})
setupMessageListeners()
}
})
vscode.commands.registerCommand('polacode.activate', () => {
panel = vscode.window.createWebviewPanel('polacode', P_TITLE, 2, {
enableScripts: true,
localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, 'webview'))]
})
panel.webview.html = getHtmlContent()
const selectionListener = setupSelectionSync()
panel.onDidDispose(() => {
selectionListener.dispose()
})
setupMessageListeners()
const fontFamily = vscode.workspace.getConfiguration('editor').fontFamily
const bgColor = context.globalState.get('polacode.bgColor', '#2e3440')
panel.webview.postMessage({
type: 'init',
fontFamily,
bgColor
})
syncSettings()
})
vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('polacode') || e.affectsConfiguration('editor')) {
syncSettings()
}
})
function setupMessageListeners() {
panel.webview.onDidReceiveMessage(({ type, data }) => {
switch (type) {
case 'shoot':
vscode.window
.showSaveDialog({
defaultUri: lastUsedImageUri,
filters: {
Images: ['png']
}
})
.then(uri => {
if (uri) {
writeSerializedBlobToFile(data.serializedBlob, uri.fsPath)
lastUsedImageUri = uri
}
})
break
case 'getAndUpdateCacheAndSettings':
panel.webview.postMessage({
type: 'restoreBgColor',
bgColor: context.globalState.get('polacode.bgColor', '#2e3440')
})
syncSettings()
break
case 'updateBgColor':
context.globalState.update('polacode.bgColor', data.bgColor)
break
case 'invalidPasteContent':
vscode.window.showInformationMessage(
'Pasted content is invalid. Only copy from VS Code and check if your shortcuts for copy/paste have conflicts.'
)
break
}
})
}
function syncSettings() {
const settings = vscode.workspace.getConfiguration('polacode')
const editorSettings = vscode.workspace.getConfiguration('editor', null)
panel.webview.postMessage({
type: 'updateSettings',
shadow: settings.get('shadow'),
transparentBackground: settings.get('transparentBackground'),
backgroundColor: settings.get('backgroundColor'),
target: settings.get('target'),
ligature: editorSettings.get('fontLigatures')
})
}
function setupSelectionSync() {
return vscode.window.onDidChangeTextEditorSelection(e => {
if (e.selections[0] && !e.selections[0].isEmpty) {
vscode.commands.executeCommand('editor.action.clipboardCopyWithSyntaxHighlightingAction')
panel.postMessage({
type: 'update'
})
}
})
}
function getHtmlContent() {
const htmlContent = fs.readFileSync(htmlPath, 'utf-8')
const absolutePath = vscode.Uri.file(context.asAbsolutePath('./webview'))
const webviewPath = panel.webview.asWebviewUri(absolutePath).toString()
return htmlContent.replace(/{{root}}/g, webviewPath);
}
}
exports.activate = activate