1
1
import * as vscode from 'vscode' ;
2
2
import * as path from 'path' ;
3
3
4
+ let position : { x :0 , y :0 } = {
5
+ x : 0 ,
6
+ y : 0
7
+ } ;
8
+
4
9
export function previewAsyncAPI ( context : vscode . ExtensionContext ) {
5
10
return async ( uri : vscode . Uri ) => {
6
11
uri = uri || ( await promptForAsyncapiFile ( ) ) as vscode . Uri ;
@@ -49,8 +54,25 @@ export function openAsyncAPI(context: vscode.ExtensionContext, uri: vscode.Uri)
49
54
retainContextWhenHidden : true ,
50
55
localResourceRoots,
51
56
} ) ;
57
+
52
58
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
+ ) ;
54
76
55
77
panel . onDidDispose ( ( ) => {
56
78
delete openAsyncapiFiles [ uri . fsPath ] ;
@@ -68,13 +90,13 @@ async function promptForAsyncapiFile() {
68
90
canSelectMany : false ,
69
91
openLabel : 'Open AsyncAPI file' ,
70
92
filters : {
71
- AsyncAPI : [ 'yml' , 'yaml' , 'json' ] ,
93
+ asyncAPI : [ 'yml' , 'yaml' , 'json' ] ,
72
94
} ,
73
95
} ) ;
74
96
return uris ?. [ 0 ] ;
75
97
}
76
98
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 } ) {
78
100
const asyncapiComponentJs = webview . asWebviewUri (
79
101
vscode . Uri . joinPath ( context . extensionUri , 'dist/node_modules/@asyncapi/react-component/browser/standalone/index.js' )
80
102
) ;
@@ -89,6 +111,9 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web
89
111
<head>
90
112
<link rel="stylesheet" href="${ asyncapiComponentCss } ">
91
113
<style>
114
+ html{
115
+ scroll-behavior: smooth;
116
+ }
92
117
body {
93
118
color: #121212;
94
119
background-color: #fff;
@@ -105,6 +130,7 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web
105
130
106
131
<script src="${ asyncapiComponentJs } "></script>
107
132
<script>
133
+ const vscode = acquireVsCodeApi();
108
134
AsyncApiStandalone.render({
109
135
schema: {
110
136
url: '${ asyncapiWebviewUri } ',
@@ -118,6 +144,19 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web
118
144
parserOptions: { path: '${ asyncapiBasePath } ' }
119
145
},
120
146
}, 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
+
121
160
</script>
122
161
123
162
</body>
0 commit comments