Skip to content

Commit 286a369

Browse files
committed
Always restore bg color
1 parent 8ea4c93 commit 286a369

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/extension.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function activate(context) {
2626
panel.webview.postMessage({
2727
type: 'restore',
2828
innerHTML: state.innerHTML,
29+
bgColor: context.globalState.get('polacode.bgColor', '#2e3440')
2930
})
3031
}
3132
})
@@ -55,6 +56,12 @@ function activate(context) {
5556
}
5657
})
5758
break
59+
case 'getAndUpdateBgColor':
60+
panel.webview.postMessage({
61+
type: 'restoreBgColor',
62+
bgColor: context.globalState.get('polacode.bgColor', '#2e3440')
63+
})
64+
break
5865
case 'updateBgColor':
5966
context.globalState.update('polacode.bgColor', data.bgColor)
6067
break

webview/index.js

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

4+
vscode.postMessage({
5+
type: 'getAndUpdateBgColor'
6+
})
7+
48
const snippetNode = document.getElementById('snippet')
59
const snippetContainerNode = document.getElementById('snippet-container')
610
const obturateur = document.getElementById('save')
@@ -52,7 +56,8 @@
5256
return getBrightness(hexColor) < 128
5357
}
5458
function getSnippetBgColor(html) {
55-
return html.match(/background-color: (#[a-fA-F0-9]+)/)[1]
59+
const match = html.match(/background-color: (#[a-fA-F0-9]+)/)
60+
return match[1] ? match[1] : undefined;
5661
}
5762

5863
function updateEnvironment(snippetBgColor) {
@@ -99,13 +104,15 @@
99104
const minIndent = getMinIndent(code)
100105

101106
const snippetBgColor = getSnippetBgColor(innerHTML)
102-
vscode.postMessage({
103-
type: 'updateBgColor',
104-
data: {
105-
bgColor: snippetBgColor
106-
}
107-
})
108-
updateEnvironment(snippetBgColor)
107+
if (snippetBgColor) {
108+
vscode.postMessage({
109+
type: 'updateBgColor',
110+
data: {
111+
bgColor: snippetBgColor
112+
}
113+
})
114+
updateEnvironment(snippetBgColor)
115+
}
109116

110117
if (minIndent !== 0) {
111118
snippetNode.innerHTML = stripInitialIndent(innerHTML, minIndent)
@@ -180,6 +187,9 @@
180187
document.execCommand('paste')
181188
} else if (e.data.type === 'restore') {
182189
snippetNode.innerHTML = e.data.innerHTML
190+
updateEnvironment(e.data.bgColor)
191+
} else if (e.data.type === 'restoreBgColor') {
192+
updateEnvironment(e.data.bgColor)
183193
}
184194
}
185195
})

0 commit comments

Comments
 (0)