Skip to content

Commit 11d6d21

Browse files
fix: preserve scroll position on preview reload (#206)
1 parent 353a924 commit 11d6d21

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/PreviewWebPanel.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
33

4+
let position : {x:0,y:0} = {
5+
x: 0,
6+
y: 0
7+
};
8+
49
export function previewAsyncAPI(context: vscode.ExtensionContext) {
510
return async (uri: vscode.Uri) => {
611
uri = uri || (await promptForAsyncapiFile()) as vscode.Uri;
@@ -49,8 +54,25 @@ export function openAsyncAPI(context: vscode.ExtensionContext, uri: vscode.Uri)
4954
retainContextWhenHidden: true,
5055
localResourceRoots,
5156
});
57+
5258
panel.title = path.basename(uri.fsPath);
53-
panel.webview.html = getWebviewContent(context, panel.webview, uri);
59+
panel.webview.html = getWebviewContent(context, panel.webview, uri, position);
60+
61+
panel.webview.onDidReceiveMessage(
62+
message => {
63+
switch (message.type) {
64+
case 'position':{
65+
position = {
66+
x: message.scrollX,
67+
y: message.scrollY
68+
};
69+
70+
}
71+
}
72+
},
73+
undefined,
74+
context.subscriptions
75+
);
5476

5577
panel.onDidDispose(() => {
5678
delete openAsyncapiFiles[uri.fsPath];
@@ -68,13 +90,13 @@ async function promptForAsyncapiFile() {
6890
canSelectMany: false,
6991
openLabel: 'Open AsyncAPI file',
7092
filters: {
71-
AsyncAPI: ['yml', 'yaml', 'json'],
93+
asyncAPI: ['yml', 'yaml', 'json'],
7294
},
7395
});
7496
return uris?.[0];
7597
}
7698

77-
function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Webview, asyncapiFile: vscode.Uri) {
99+
function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Webview, asyncapiFile: vscode.Uri, position: {x:0,y:0}) {
78100
const asyncapiComponentJs = webview.asWebviewUri(
79101
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/browser/standalone/index.js')
80102
);
@@ -89,6 +111,9 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web
89111
<head>
90112
<link rel="stylesheet" href="${asyncapiComponentCss}">
91113
<style>
114+
html{
115+
scroll-behavior: smooth;
116+
}
92117
body {
93118
color: #121212;
94119
background-color: #fff;
@@ -105,6 +130,7 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web
105130
106131
<script src="${asyncapiComponentJs}"></script>
107132
<script>
133+
const vscode = acquireVsCodeApi();
108134
AsyncApiStandalone.render({
109135
schema: {
110136
url: '${asyncapiWebviewUri}',
@@ -118,6 +144,19 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web
118144
parserOptions: { path: '${asyncapiBasePath}' }
119145
},
120146
}, document.getElementById('asyncapi'));
147+
148+
window.addEventListener('scrollend', event => {
149+
vscode.postMessage({
150+
type: 'position',
151+
scrollX: window.scrollX || 0,
152+
scrollY: window.scrollY || 0
153+
});
154+
});
155+
156+
window.addEventListener("load", (event) => {
157+
setTimeout(()=>{window.scrollBy('${position.x}','${position.y}')},1000)
158+
});
159+
121160
</script>
122161
123162
</body>

0 commit comments

Comments
 (0)