Skip to content

editor in a WebComponent: viewImpl and codeEditorWidget #308

Closed as not planned

Description

@bana-io

classes referenced are vs/editor/browser/view/viewImpl and vs/editor/browser/widget/codeEditorWidget.
any idea why:
https://github.com/Microsoft/vscode/blob/master/src/vs/editor/browser/widget/codeEditorWidget.ts#L487 -> https://github.com/Microsoft/vscode/blob/master/src/vs/editor/browser/view/viewImpl.ts#L115

doesn't pass in the domElement from:
https://github.com/Microsoft/vscode/blob/master/src/vs/editor/browser/widget/codeEditorWidget.ts#L65?

i think calls like this.linesContent = document.createElement('div'); are a bit problematic to deal with in the realm of WebComponents. for some reason i can't get the textarea to display properly if the #container is not placed in the body.

that aside, could the contract be listed a bit more explicitly.
setting breakpoints on the dom tree, e.g., break on -> subtree modifcations or attributes modifcations shows the code touching other elements outside of the #container.

out of interest; do you test with shadow/shady dom enabled?


working:
here is a working version that is not ideal: http://bana.io/monaco-editor-polymer/.

<body>
  <monaco-editor-polymer-code-editor></monaco-editor-polymer-code-editor>
  <div class="code-editor-app">
    <div class="code-editor" id="container"></div>
  </div>
</body>

not working:

in this version the textarea seems to get a

<dom-module id="monaco-editor-polymer-code-editor">
    <template>
        ...
        <div class="code-editor" id="container"></div>
        ...
    </template>
    <script>
        var require = {
            baseUrl: '/monaco-editor-polymer/',
            paths: {
                'vs': 'node_modules/monaco-editor/min/vs'
            }
        };
    </script>
    <script src="node_modules/monaco-editor/min/vs/loader.js"></script>
    <script src="node_modules/monaco-editor/min/vs/editor/editor.main.nls.js"></script>
    <script src="node_modules/monaco-editor/min/vs/editor/editor.main.js"></script>

    <script src="elements/monaco-editor-polymer-code-editor.js"></script>
</dom-module>
...
<body>
  <monaco-editor-polymer-code-editor></monaco-editor-polymer-code-editor>
</body>
...

Activity

  1. bana-io commented on Jan 3, 2017

    @bana-io
    Author

    still failing but i see an uncoloured textbuffer now with the change below.

    ok i just saw something:
    https://github.com/Microsoft/vscode/blob/a2e26c85ab8e82fbe037718bf65f1a1f048bac2c/src/vs/base/browser/dom.ts#L49

    i need to do something like the below in WebComponents.
    override the isInDOM method to return shadow root's parent.

            let self = this;
            require([
                'vs/editor/editor.main'
            ], function (editorMain) {
                require([
                    'vs/base/browser/dom',
                ], function(browserDom) {
                    let isInDOMOld = browserDom.isInDOM;
                    browserDom.isInDOM = (node) => {
                        // here ...
                        return this._isInDOM(node, isInDOMOld);
                    };
                    self._addEditorToContainer(editorMain, browserDom);
                });
            });
    ...
        _findShadowRootParent(node) {
            let shadowRoot = Polymer.dom(node).getOwnerRoot();
            if (!shadowRoot) {
                let message = `!shadowRoot`;
                throw new Error(message);
            }
    
            let shadowRootParent = shadowRoot.host;
            if (!shadowRootParent) {
                let message = `!shadowRootParent`;
                throw new Error(message);
            }
    
            return shadowRootParent;
        }

    if anyone has any ideas, fire away.

  2. bana-io commented on Jan 3, 2017

    @bana-io
    Author

    ok i've got it to load now. http://bana.io/monaco-editor-polymer/.

    summary:
    seems like there is a dependency on the #container having body as the parentNode.
    effectively, i render #container into the light dom tree of the element and then proceed to construct Monaco. seems to work this way without hacks. e.g., the below snippet.

    if, #container is in the <monaco-editor-polymer> element - my ideal approach - it fails as there are places, not sure where, that doesn't play well with shadow dom.
    alexandrudima, hi, would you please confirm if this an OK approach?

    <body>
        <monaco-editor-polymer>
            <div class="code-editor-container" slot="code-editor-slot"></div>
        </monaco-editor-polymer>
    </body>
    
    
  3. alexdima commented on Jan 13, 2017

    @alexdima
    Member

    @mbana This is awesome! I have never tried loading the editor in a shadow dom / with polymer. Are the patches you needed to apply to vs/base/browser/dom.isInDOM something you could contribute via a PR in such a way that we use only dom API (i.e. no Polymer usage) and in such a way that it doesn't break the editor usage in the rest of the cases?

    Would you also consider contributing a PR to https://github.com/Microsoft/monaco-editor-samples with a working example?

  4. added
    feature-request
    on Jan 13, 2017
  5. paresy commented on Mar 6, 2017

    @paresy

    I did not fix the issue in the ts file yet. Just for reference on how you can patch the editor.main.js:

    function isInDOM(node) {
            while (node) {
                if (node === document.body) {
                    return true;
                } else if (node.host) {
                    return isInDOM(node.host);
                }
                node = node.parentNode;
            }
            return false;
    }
    

    If we are in a shodow dom we want to walk the shadow dom boundaries aswell.

    This is just one of a lot of issues regarding shadow dom. The editor is injecting scripts and sytles using document.* functions, which disregards the shadow dom boundaries and pollutes the document DOM.

  6. paresy commented on Mar 6, 2017

    @paresy

    Another thing that is totally broken is detection of CONTENT_TEXT. Therefore getting focus works only for CONTENT_EMPTY which is outside of the text area.

    "MouseTargetFactory._doHitTest" fails because we need to do hit tests against the shadow root instead of document. Unfortunately all current browsers do not implement caretRangeFromPoint or caretPositionFromPoint for shadow doms. See related ticket for TinyMCE which discusses the problem further: tinymce/tinymce#561

    caretRangeFromPoint() method is present only on document, but not on shadowRoot (mailing list, relevant Chromium issue, vote, please) and using document.caretRangeFromPoint() gives us just Shadow Root's host element. Because of this we can't find exact position where content was dropped.

  7. paulvarache commented on Jul 23, 2017

    @paulvarache

    I'm jumping in this issue, because I did a while ago a custom element with shadow DOM for the monaco-editor. I encountered the same issues as described here, but found work arounds.
    Here's a demo: https://paulvarache.github.io/monaco-editor-element

    I had to hotfix the isInDOM method as mentioned and I did a polyfill for caretRangeFromPoint in a shadow root. I also had to move some of the style tags inside the shadow root.

    I use monaco-editor as a node dependency and at build time copy the files in a dist folder and with some dirty use of sed I replace the bits of code that needs to be changed before uglifying all that.

  8. samccone commented on May 26, 2019

    @samccone

    I encountered similar problems with shadow dom and monaco. I was able to work around the problem by slotting the target DOM node into my application from the body level down.

  9. hediet commented on Feb 16, 2023

    @hediet
    Member

    Closing due to inactivity.

    Please create new issues for the remaining problems around monaco-editor and shadow dom.

    Also, please share monaco editor playground samples that make use of the shadow dom to demonstrate what does not work.

  10. locked and limited conversation to collaborators on Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestintegration

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions