shadow-dom.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <title>Editor</title>
  7. <style type="text/css" media="screen">
  8. .scrollmargin {
  9. text-align: center;
  10. }
  11. .large-button {
  12. color: lightblue;
  13. cursor: pointer;
  14. font: 30px arial;
  15. padding: 20px;
  16. text-align: center;
  17. border: medium solid transparent;
  18. display: inline-block;
  19. }
  20. .large-button:hover {
  21. border: medium solid lightgray;
  22. border-radius: 10px 10px 10px 10px;
  23. box-shadow: 0 0 12px 0 lightblue;
  24. }
  25. body {
  26. transform: translateZ(0);
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="scrollmargin">
  32. <span onclick="add()" class="large-button">+</span>
  33. </div>
  34. <ace-playground></ace-playground>
  35. <!-- load ace -->
  36. <script src="../src/ace.js"></script>
  37. <script>
  38. var dom = require("ace/lib/dom");
  39. class AcePlayground extends HTMLElement {
  40. constructor() {
  41. super();
  42. var shadow = this.attachShadow({mode: "open"});
  43. var dom = require("ace/lib/dom");
  44. dom.buildDom(["div", {id: "host"},
  45. ["div", {id: "html"}],
  46. ["div", {id: "css"}],
  47. ["iframe", {id: "preview"}],
  48. ["style", `
  49. #host {
  50. border: solid 1px gray;
  51. display: grid;
  52. grid-template-areas: "html preview" "css preview";
  53. }
  54. #html {
  55. grid-area: html;
  56. height: 200px;
  57. }
  58. #css {
  59. grid-area: css;
  60. height: 200px;
  61. }
  62. #preview {
  63. grid-area: preview;
  64. width: 100%;
  65. height: 100%;
  66. border: none;
  67. }
  68. `]
  69. ], shadow);
  70. var htmlEditor = ace.edit(shadow.querySelector("#html"), {
  71. theme: "ace/theme/solarized_light",
  72. mode: "ace/mode/html",
  73. value: "<div>\n\thollow world!\n</div>\n<script><\/script>",
  74. autoScrollEditorIntoView: true
  75. });
  76. var cssEditor = ace.edit(shadow.querySelector("#css"), {
  77. theme: "ace/theme/solarized_dark",
  78. mode: "ace/mode/css",
  79. value: "*{\n\tcolor:red\n}",
  80. autoScrollEditorIntoView: true
  81. });
  82. var preview = shadow.querySelector("#preview");
  83. this.htmlEditor = htmlEditor;
  84. this.cssEditor = cssEditor;
  85. this.preview = preview;
  86. htmlEditor.renderer.attachToShadowRoot();
  87. this.updatePreview = this.updatePreview.bind(this)
  88. htmlEditor.on("input", this.updatePreview);
  89. cssEditor.on("input", this.updatePreview);
  90. this.updatePreview();
  91. }
  92. updatePreview() {
  93. var code = this.htmlEditor.getValue() + "<style>" + this.cssEditor.getValue() + "</style>";
  94. this.preview.src = "data:text/html," + encodeURIComponent(code)
  95. }
  96. }
  97. customElements.define('ace-playground', AcePlayground);
  98. window.add = function() {
  99. var el = document.createElement("ace-playground");
  100. document.body.appendChild(el);
  101. };
  102. </script>
  103. </body>
  104. </html>