(node, lang)
| 138 | let article = document.getElementsByTagName("article")[0] |
| 139 | |
| 140 | function activateCode(node, lang) { |
| 141 | let scrollPos = pageYOffset, rect = node.getBoundingClientRect() |
| 142 | if (rect.top < 0 && rect.height > 500) scrollPos -= Math.min(-rect.top, rect.height - 500) |
| 143 | let codeId = node.querySelector("a").id |
| 144 | let code = (window.localStorage && localStorage.getItem(codeId)) || node.textContent |
| 145 | let wrap = node.parentNode.insertBefore(elt("div", {"class": "editor-wrap"}), node) |
| 146 | let pollingScroll = null |
| 147 | function pollScroll() { |
| 148 | if (document.activeElement != editor.contentDOM) return |
| 149 | let rect = editor.dom.getBoundingClientRect() |
| 150 | if (rect.bottom < 0 || rect.top > innerHeight) editor.contentDOM.blur() |
| 151 | else pollingScroll = setTimeout(pollScroll, 500) |
| 152 | } |
| 153 | let sandbox = node.getAttribute("data-sandbox") |
| 154 | let context = { |
| 155 | wrap: wrap, |
| 156 | orig: node, |
| 157 | isHTML: lang == "html", |
| 158 | sandbox, |
| 159 | meta: node.getAttribute("data-meta") |
| 160 | } |
| 161 | let editorState = createState(code, lang, [ |
| 162 | extraKeys, |
| 163 | EditorView.domEventHandlers({ |
| 164 | focus: (e, view) => { |
| 165 | clearTimeout(pollingScroll) |
| 166 | pollingScroll = setTimeout(pollScroll, 500) |
| 167 | showEditorControls(view) |
| 168 | }, |
| 169 | blur: (e, view) => { |
| 170 | setTimeout(() => { |
| 171 | if (!view.hasFocus) hideEditorControls(view) |
| 172 | }, 100) |
| 173 | } |
| 174 | }), |
| 175 | EditorView.updateListener.of(debounce(update => { |
| 176 | if (update.docChanged && window.localStorage) |
| 177 | localStorage.setItem(codeId, editor.state.doc.toString()) |
| 178 | }, 250)), |
| 179 | contextFacet.of(context) |
| 180 | ]) |
| 181 | let editor = new EditorView({state: editorState, parent: wrap}) |
| 182 | let out = wrap.appendChild(elt("div", {"class": "sandbox-output", "aria-live": "polite"})) |
| 183 | context.output = new Sandbox.Output(out) |
| 184 | if (lang == "html" && !sandbox) { |
| 185 | sandbox = context.sandbox = "html" + nextID++ |
| 186 | node.setAttribute("data-sandbox", sandbox) |
| 187 | sandboxSnippets[sandbox] = node |
| 188 | } |
| 189 | node.style.display = "none" |
| 190 | // Cancel weird scroll stabilization magic from brower (which |
| 191 | // doesn't work at all for this) |
| 192 | window.scrollTo(pageXOffset, scrollPos) |
| 193 | setTimeout(() => window.scrollTo(pageXOffset, scrollPos), 20) |
| 194 | return editor |
| 195 | } |
| 196 | |
| 197 | function openMenu(editor, node) { |
no test coverage detected