shadow-dom.html 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  5. </head>
  6. <body>
  7. <h2>Monaco Editor Shadow DOM</h2>
  8. <div style="clear: both"></div>
  9. <div
  10. id="container"
  11. style="float: left; width: 800px; height: 450px; border: 1px solid grey"
  12. ></div>
  13. <div style="clear: both"></div>
  14. <script src="../../metadata.js"></script>
  15. <script src="dev-setup.js"></script>
  16. <script>
  17. const container = document.getElementById('container');
  18. const shadowRoot = container.attachShadow({
  19. mode: 'closed'
  20. });
  21. loadEditor(function () {
  22. const innerContainer = document.createElement('div');
  23. shadowRoot.appendChild(innerContainer);
  24. innerContainer.style.width = '800px';
  25. innerContainer.style.height = '450px';
  26. // We must move all CSS inside the shadow root, pick only link tags relevant to the editor
  27. const documentLinks = Array.prototype.slice
  28. .call(document.getElementsByTagName('link'), 0)
  29. .filter((documentLink) => {
  30. if (/vs\/(base|editor|platform)/.test(documentLink.getAttribute('href'))) {
  31. return true;
  32. }
  33. console.log(`Not moving: `, documentLink);
  34. return true;
  35. });
  36. documentLinks.forEach((documentLink) => shadowRoot.appendChild(documentLink));
  37. // We must define the font face outside the shadowroot
  38. const codiconCSS = `@font-face { font-family: "codicon"; src: url("${getCodiconPath()}") format("truetype"); }`;
  39. const style = document.createElement('style');
  40. style.type = 'text/css';
  41. style.media = 'screen';
  42. document.getElementsByTagName('head')[0].appendChild(style);
  43. style.innerHTML = codiconCSS;
  44. monaco.editor.create(innerContainer, {
  45. value: ['function hello() {', '\tconsole.log("hello world");', '}', ''].join('\n'),
  46. language: 'javascript',
  47. ariaContainerElement: innerContainer
  48. });
  49. });
  50. </script>
  51. </body>
  52. </html>