| 202 | } |
| 203 | |
| 204 | class CsskitEditor extends HTMLElement { |
| 205 | static define(tagName = "csskit-editor") { |
| 206 | customElements.define(tagName, this); |
| 207 | } |
| 208 | |
| 209 | get code() { |
| 210 | return this.view.state.doc.toString(); |
| 211 | } |
| 212 | |
| 213 | shadowRoot = this.attachShadow({ mode: "open" }); |
| 214 | |
| 215 | extensions = [ |
| 216 | basicSetup, |
| 217 | EditorView.lineWrapping, |
| 218 | csskitLight, |
| 219 | css(), |
| 220 | lintGutter(), |
| 221 | EditorView.domEventHandlers({ |
| 222 | mouseover: (e) => this.handleEvent(e), |
| 223 | click: (e) => this.handleEvent(e), |
| 224 | }), |
| 225 | EditorView.updateListener.of((view) => { |
| 226 | if (view.startState.doc.toString() !== view.state.doc.toString()) { |
| 227 | this.dispatchEvent(new EditorChangeEvent(this.code)); |
| 228 | } |
| 229 | }), |
| 230 | ]; |
| 231 | |
| 232 | async connectedCallback() { |
| 233 | let doc = |
| 234 | await (codeFromUrl.has() |
| 235 | ? codeFromUrl.get() |
| 236 | : codeFromLocal.has() |
| 237 | ? codeFromLocal.get() |
| 238 | : this.textContent); |
| 239 | this.view = new EditorView({ |
| 240 | extensions: this.extensions, |
| 241 | parent: this.shadowRoot, |
| 242 | root: this.shadowRoot, |
| 243 | doc, |
| 244 | }); |
| 245 | requestAnimationFrame(() => |
| 246 | this.dispatchEvent(new EditorChangeEvent(this.code)) |
| 247 | ); |
| 248 | this.ownerDocument.addEventListener("diagnostic", this); |
| 249 | this.ownerDocument.addEventListener("viewer-highlight", this); |
| 250 | this.ownerDocument.addEventListener("load-code", this); |
| 251 | this.addEventListener("focusout", this); |
| 252 | this.addEventListener("focusin", this); |
| 253 | } |
| 254 | |
| 255 | handleEvent(event) { |
| 256 | if (event.type == "diagnostic") { |
| 257 | this.view.dispatch(setDiagnostics(this.view.state, event.diagnostics)); |
| 258 | } else if (event.type == "focusout") { |
| 259 | codeFromUrl.set(event.target.code); |
| 260 | codeFromLocal.set(event.target.code); |
| 261 | } else if (event.type == "viewer-highlight") { |
nothing calls this directly
no test coverage detected