Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass object field instead of method argument #8833

Merged
merged 1 commit into from
Dec 15, 2020
Merged

Conversation

vzhukovs
Copy link
Contributor

@vzhukovs vzhukovs commented Dec 7, 2020

What it does

This simple change proposal modifies the behavior of webview html setter to pass object field instead of method argument to the proxy delegator. With this approach it is possible to override current setter via method bind to add custom logic between setter and proxy delegation call.

Signed-off-by: Vladyslav Zhukovskyi vzhukovs@redhat.com

How to test

Build and run Theia from this PR and try to open any webview. Behavior should be the same as in master branch.

Review checklist

Reminder for reviewers

Signed-off-by: Vladyslav Zhukovskyi <vzhukovs@redhat.com>
@vzhukovs vzhukovs added enhancement issues that are enhancements to current functionality - nice to haves webviews issues related to webviews labels Dec 7, 2020
@vzhukovs vzhukovs self-assigned this Dec 7, 2020
@paul-marechal
Copy link
Member

paul-marechal commented Dec 7, 2020

It feels odd to rely on the current setter to access _html eventually calling a getter, would it be better to override the setter instead?

Example

That or wrap the call to the proxy into an overrideable method? The goal is to control what is sent to the proxy right?

@kittaakos
Copy link
Contributor

Does not require to be tested

😅 nice.

@vzhukovs, please help me to understand this PR. Once this is merged, how do you make the customization on your side?

@vzhukovs
Copy link
Contributor Author

vzhukovs commented Dec 9, 2020

Hi @kittaakos, sorry for the late answer.

Well, I'll try to describe the purpose of this change. In our downstream project we have to run extensions in isolated containers. There are wrappers around rpc and websocket. In short, there is a plugin deployer and dedicate extension (vs code). This extension might provide a webview. The main task is to have the correct loading webview's resources, e.g. css and js files. Due to architectural limitations we can not easily extend WebviewsExtImpl. And what we can do is to get WebviewsExtImpl object in runtime and rebind some methods. We modify the links like vscode-resource:/file/ in webview's html by adding the URI scheme to identify that this resource is remote ones and should be loaded from, links become like vscode-resource:/file-sidecar-${containerID}/. As far as theia.Webview has field html and it is implemented in WebviewImpl via setters and getters what we can do is to redefine setter property:

    Object.defineProperty(webview, '_html', {
      get: function () {
        // @ts-ignore
        return this._html;
      }.bind(this),
      set: function (value: string) {
        const sideCarScheme = `file-sidecar-${process.env.CHE_MACHINE_NAME}`;
        // @ts-ignore
        this._html = value.replace(
          /(["'])(vscode|theia)-resource:(\/\/([^\s\/'"]+?)(?=\/))?([^\s'"]+?)(["'])/gi,
          (_, startQuote, resourceType, _1, scheme, path, endQuote) => {
            if (scheme) {
              return _;
            }
            return `${startQuote}${resourceType}-resource://${sideCarScheme}${path}${endQuote}`;
          }
        );
      }.bind(this),
    });

this approach works only if we'll pass this._html to the the proxy:

this.proxy.$setHtml(this.viewId, this._html);

instead of:

this.proxy.$setHtml(this.viewId, value);

In the original case (in master branch), html content is modified on the plugin side, but not modified on the browser side.

@kittaakos
Copy link
Contributor

I'll try to describe the purpose of this change.

Thank you so much for the details, @vzhukovs 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement issues that are enhancements to current functionality - nice to haves webviews issues related to webviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants