From 11d6d21277a6d060747bd9df4ca91751dc103a5c Mon Sep 17 00:00:00 2001 From: nikhilkalburgi <70331875+nikhilkalburgi@users.noreply.github.com> Date: Mon, 12 Feb 2024 13:37:12 +0530 Subject: [PATCH] fix: preserve scroll position on preview reload (#206) --- src/PreviewWebPanel.ts | 45 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/PreviewWebPanel.ts b/src/PreviewWebPanel.ts index 86467b9..fabac06 100644 --- a/src/PreviewWebPanel.ts +++ b/src/PreviewWebPanel.ts @@ -1,6 +1,11 @@ import * as vscode from 'vscode'; import * as path from 'path'; +let position : {x:0,y:0} = { + x: 0, + y: 0 +}; + export function previewAsyncAPI(context: vscode.ExtensionContext) { return async (uri: vscode.Uri) => { uri = uri || (await promptForAsyncapiFile()) as vscode.Uri; @@ -49,8 +54,25 @@ export function openAsyncAPI(context: vscode.ExtensionContext, uri: vscode.Uri) retainContextWhenHidden: true, localResourceRoots, }); + panel.title = path.basename(uri.fsPath); - panel.webview.html = getWebviewContent(context, panel.webview, uri); + panel.webview.html = getWebviewContent(context, panel.webview, uri, position); + + panel.webview.onDidReceiveMessage( + message => { + switch (message.type) { + case 'position':{ + position = { + x: message.scrollX, + y: message.scrollY + }; + + } + } + }, + undefined, + context.subscriptions + ); panel.onDidDispose(() => { delete openAsyncapiFiles[uri.fsPath]; @@ -68,13 +90,13 @@ async function promptForAsyncapiFile() { canSelectMany: false, openLabel: 'Open AsyncAPI file', filters: { - AsyncAPI: ['yml', 'yaml', 'json'], + asyncAPI: ['yml', 'yaml', 'json'], }, }); return uris?.[0]; } -function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Webview, asyncapiFile: vscode.Uri) { +function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Webview, asyncapiFile: vscode.Uri, position: {x:0,y:0}) { const asyncapiComponentJs = webview.asWebviewUri( vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/@asyncapi/react-component/browser/standalone/index.js') ); @@ -89,6 +111,9 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web