microsoft / azuredatastudio

Azure Data Studio is a data management and development tool with connectivity to popular cloud and on-premises databases. Azure Data Studio supports Windows, macOS, and Linux, with immediate capability to connect to Azure SQL and SQL Server. Browse the extension library for more database support options including MySQL, PostgreSQL, and MongoDB.
https://learn.microsoft.com/sql/azure-data-studio
MIT License
7.54k stars 898 forks source link

azdata.dashboard.registerWebviewProvider (sqlops) not render html #5175

Open bahs opened 5 years ago

bahs commented 5 years ago
  1. Run: https://github.com/Microsoft/azuredatastudio/tree/master/samples/extensionSamples

We are making extension using webview, as a reference we were using sample from https://github.com/Microsoft/azuredatastudio/tree/master/samples/extensionSamples. From version 1.6.0 both for out extension and in a sample extension webview stop render html.

The way to create, for example

const counterHtml = fs.readFileSync(path.join(__dirname, 'counter.html')).toString(); sqlops.dashboard.registerWebviewProvider('webview-count', e => { e.html = counterHtml; countWidget = e;

jamesliberatii commented 1 year ago

I've seen this same issue myself. In my case it is accompanied by a message like:

Cannot read properties of undefined (reading 'addEventListener'): TypeError: Cannot read properties of undefined (reading 'addEventListener')
    at new DomListener (vscode-file://vscode-app/<temp>/azuredatastudio/out/vs/base/browser/dom.js:28:24)

If I run the development version against Electron 22+ then it works. If I use a lower version (including the version it is built with: 19.1.8) then this error occurs.

The webview system is undergoing an overhaul which I think explains this. It can be fixed with the following hacky patch:

diff --git a/src/sql/workbench/contrib/modelView/browser/webview.component.ts b/src/sql/workbench/contrib/modelView/browser/webview.component.ts
index 89c8acd0f5e..edcd5495081 100644
--- a/src/sql/workbench/contrib/modelView/browser/webview.component.ts
+++ b/src/sql/workbench/contrib/modelView/browser/webview.component.ts
@@ -82,13 +82,14 @@ export default class WebViewComponent extends ComponentBase<WebViewProperties> i
                this._webview.mountTo(this._el.nativeElement);

                this._ready = new Promise(resolve => {
-                       let webview = (<any>this._webview)._webview;
+                       let webview = (<any>this._webview)._webview || (<any>this._webview)._element;
                        const subscription = this._register(addDisposableListener(webview, 'ipc-message', (event) => {
                                if (event.channel === 'webview-ready') {
                                        subscription.dispose();
                                        resolve();
                                }
                        }));
+                       setTimeout(resolve, 100);
                });

                this._ready.then(() => {

The reason it was failing is that the older electron does not have a _webview element and the _element field does not receive the ipc-message. However, the webview code in azuredatastudio requires the newer version of Electron despite being bundled with an older one.

Unfortunately Electron 20.3.9+ breaks ref-napi Issue 75 so for my purposes I'll be using this hack, but it looks like the correct fix is to upgrade the packaged version of electron.