Put authority into webview resource uri

This lets us handle the case where a webview needs to load both local and remote resources
This commit is contained in:
Matt Bierner
2021-05-10 20:40:07 -07:00
parent d60cbb0eae
commit 3d978d4dde
6 changed files with 50 additions and 77 deletions

View File

@@ -10,17 +10,24 @@ export interface WebviewInitData {
readonly isExtensionDevelopmentDebug: boolean;
readonly webviewResourceRoot: string;
readonly webviewCspSource: string;
readonly remote: { readonly authority: string | undefined };
}
/**
* Construct a uri that can load resources inside a webview
*
* We encode the resource component of the uri so that on the main thread
* we know where to load the resource from (remote or truly local):
*
* /authority?/scheme/path...
*/
export function asWebviewUri(
initData: WebviewInitData,
uuid: string,
resource: vscode.Uri,
): vscode.Uri {
const uri = initData.webviewResourceRoot
// Make sure we preserve the scheme of the resource but convert it into a normal path segment
// The scheme is important as we need to know if we are requesting a local or a remote resource.
.replace('{{resource}}', resource.scheme + withoutScheme(resource))
.replace('{{resource}}', (initData.remote.authority ?? '') + '/' + resource.scheme + withoutScheme(resource))
.replace('{{uuid}}', uuid);
return URI.parse(uri);
}