Skip to content

Commit 618ac34

Browse files
committed
Implement persistence
1 parent 1e7a92e commit 618ac34

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

jsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "es2015",
4-
"module": "commonjs"
4+
"module": "commonjs",
5+
"moduleResolution": "node"
56
}
67
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"vscode": "^1.32.0"
2828
},
2929
"activationEvents": [
30-
"onCommand:polacode.activate"
30+
"onCommand:polacode.activate",
31+
"onWebviewPanel:polacode"
3132
],
3233
"main": "./src/extension",
3334
"contributes": {

src/extension.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,26 @@ const P_TITLE = 'Polacode 📸'
1616
*/
1717
function activate(context) {
1818
const htmlPath = path.resolve(context.extensionPath, 'webview/index.html')
19-
const indexUri = vscode.Uri.file(htmlPath)
2019

2120
let lastUsedImageUri = vscode.Uri.file(path.resolve(homedir(), 'Desktop/code.png'))
2221
let panel
2322

23+
vscode.window.registerWebviewPanelSerializer('polacode', {
24+
async deserializeWebviewPanel(panel, state) {
25+
panel = panel
26+
panel.webview.html = getHtmlContent(htmlPath)
27+
panel.webview.postMessage({
28+
type: 'restore',
29+
innerHTML: state.innerHTML,
30+
})
31+
}
32+
})
33+
2434
vscode.commands.registerCommand('polacode.activate', () => {
2535
panel = vscode.window.createWebviewPanel('polacode', P_TITLE, 2, {
2636
enableScripts: true,
27-
localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, 'webview'))]
37+
localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, 'webview'))],
38+
// retainContextWhenHidden: true
2839
})
2940

3041
panel.webview.html = getHtmlContent(htmlPath)

webview/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
const snippetContainerNode = document.getElementById('snippet-container')
66
const obturateur = document.getElementById('save')
77

8+
snippetContainerNode.style.opacity = '1'
9+
const oldState = vscode.getState();
10+
if (oldState && oldState.innerHTML) {
11+
snippetNode.innerHTML = oldState.innerHTML
12+
}
13+
814
const getInitialHtml = fontFamily => {
915
const cameraWithFlashEmoji = String.fromCodePoint(128248)
1016
const monoFontStack = `${fontFamily},SFMono-Regular,Consolas,DejaVu Sans Mono,Ubuntu Mono,Liberation Mono,Menlo,Courier,monospace`
@@ -106,6 +112,8 @@
106112
} else {
107113
snippetNode.innerHTML = innerHTML
108114
}
115+
116+
vscode.setState({ innerHTML })
109117
})
110118

111119
obturateur.addEventListener('click', () => {
@@ -158,6 +166,7 @@
158166

159167
const initialHtml = getInitialHtml(fontFamily)
160168
snippetNode.innerHTML = initialHtml
169+
vscode.setState({ innerHTML: initialHtml })
161170

162171
// update backdrop color, using bgColor from last pasted snippet
163172
// cannot deduce from initialHtml since it's always using Nord color
@@ -167,9 +176,10 @@
167176
snippetContainerNode.style.background = 'none'
168177
}
169178

170-
snippetContainerNode.style.opacity = '1'
171179
} else if (e.data.type === 'update') {
172180
document.execCommand('paste')
181+
} else if (e.data.type === 'restore') {
182+
snippetNode.innerHTML = e.data.innerHTML
173183
}
174184
}
175185
})

0 commit comments

Comments
 (0)