Skip to content

Commit 42d5f35

Browse files
committed
A few settings
1 parent 3166662 commit 42d5f35

File tree

3 files changed

+103
-6
lines changed

3 files changed

+103
-6
lines changed

package.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,31 @@
3737
"command": "polacode.activate",
3838
"title": "Polacode 📸"
3939
}
40-
]
40+
],
41+
"configuration": {
42+
"title": "Polacode",
43+
"properties": {
44+
"polacode.shadow": {
45+
"type": "string",
46+
"description": "Shadow of the snippet node",
47+
"default": "rgba(0, 0, 0, 0.55) 0px 20px 68px"
48+
},
49+
"polacode.background": {
50+
"type": "string",
51+
"description": "Background of the container node",
52+
"default": "rgba(242, 242, 242, 1)"
53+
},
54+
"polacode.target": {
55+
"type": "string",
56+
"description": "Shoot with or without container",
57+
"default": "container",
58+
"enum": [
59+
"container",
60+
"snippet"
61+
]
62+
}
63+
}
64+
}
4165
},
4266
"devDependencies": {
4367
"@types/node": "^11.12.0",

src/extension.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ function activate(context) {
5656
}
5757
})
5858
break
59-
case 'getAndUpdateBgColor':
59+
case 'getAndUpdateCacheAndSettings':
6060
panel.webview.postMessage({
6161
type: 'restoreBgColor',
6262
bgColor: context.globalState.get('polacode.bgColor', '#2e3440')
6363
})
64+
65+
syncSettings()
6466
break
6567
case 'updateBgColor':
6668
context.globalState.update('polacode.bgColor', data.bgColor)
@@ -80,8 +82,20 @@ function activate(context) {
8082
fontFamily,
8183
bgColor
8284
})
85+
86+
syncSettings()
8387
})
8488

89+
function syncSettings() {
90+
const settings = vscode.workspace.getConfiguration('polacode')
91+
panel.webview.postMessage({
92+
type: 'updateSettings',
93+
shadow: settings.get('shadow'),
94+
background: settings.get('background'),
95+
target: settings.get('target')
96+
})
97+
}
98+
8599
vscode.window.onDidChangeTextEditorSelection(e => {
86100
if (e.selections[0] && !e.selections[0].isEmpty) {
87101
vscode.commands.executeCommand('editor.action.clipboardCopyAction')
@@ -90,6 +104,18 @@ function activate(context) {
90104
})
91105
}
92106
})
107+
108+
vscode.workspace.onDidChangeConfiguration(e => {
109+
if (e.affectsConfiguration('polacode')) {
110+
const settings = vscode.workspace.getConfiguration('polacode')
111+
panel.webview.postMessage({
112+
type: 'updateSettings',
113+
shadow: settings.get('shadow'),
114+
background: settings.get('background'),
115+
target: settings.get('target')
116+
})
117+
}
118+
})
93119
}
94120

95121
function getHtmlContent(htmlPath) {

webview/index.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
;(function() {
22
const vscode = acquireVsCodeApi()
33

4+
let target = 'container'
5+
46
vscode.postMessage({
5-
type: 'getAndUpdateBgColor'
7+
type: 'getAndUpdateCacheAndSettings'
68
})
79

810
const snippetNode = document.getElementById('snippet')
@@ -57,7 +59,7 @@
5759
}
5860
function getSnippetBgColor(html) {
5961
const match = html.match(/background-color: (#[a-fA-F0-9]+)/)
60-
return match[1] ? match[1] : undefined;
62+
return match ? match[1] : undefined;
6163
}
6264

6365
function updateEnvironment(snippetBgColor) {
@@ -124,23 +126,64 @@
124126
})
125127

126128
obturateur.addEventListener('click', () => {
129+
if (target === 'container') {
130+
shootAll()
131+
} else {
132+
shootSnippet()
133+
}
134+
})
135+
136+
function shootAll() {
127137
const width = snippetContainerNode.offsetWidth * 2
128138
const height = snippetContainerNode.offsetHeight * 2
129139
const config = {
130140
width,
131141
height,
132142
style: {
133143
transform: 'scale(2)',
134-
'transform-origin': 'left top'
144+
'transform-origin': 'center'
135145
}
136146
}
137147

148+
// Hide resizer before capture
149+
snippetNode.style.resize = 'none'
150+
snippetContainerNode.style.resize = 'none'
151+
138152
domtoimage.toBlob(snippetContainerNode, config).then(blob => {
153+
snippetNode.style.resize = ''
154+
snippetContainerNode.style.resize = ''
139155
serializeBlob(blob, serializedBlob => {
140156
shoot(serializedBlob)
141157
})
142158
})
143-
})
159+
}
160+
161+
function shootSnippet() {
162+
const width = snippetNode.offsetWidth * 2
163+
const height = snippetNode.offsetHeight * 2
164+
const config = {
165+
width,
166+
height,
167+
style: {
168+
transform: 'scale(2)',
169+
'transform-origin': 'center',
170+
padding: 0,
171+
background: 'none'
172+
}
173+
}
174+
175+
// Hide resizer before capture
176+
snippetNode.style.resize = 'none'
177+
snippetContainerNode.style.resize = 'none'
178+
179+
domtoimage.toBlob(snippetContainerNode, config).then(blob => {
180+
snippetNode.style.resize = ''
181+
snippetContainerNode.style.resize = ''
182+
serializeBlob(blob, serializedBlob => {
183+
shoot(serializedBlob)
184+
})
185+
})
186+
}
144187

145188
let isInAnimation = false
146189

@@ -190,6 +233,10 @@
190233
updateEnvironment(e.data.bgColor)
191234
} else if (e.data.type === 'restoreBgColor') {
192235
updateEnvironment(e.data.bgColor)
236+
} else if (e.data.type === 'updateSettings') {
237+
snippet.style.boxShadow = e.data.shadow
238+
target = e.data.target
239+
snippetContainerNode.style.background = e.data.background
193240
}
194241
}
195242
})

0 commit comments

Comments
 (0)