| 314 | } |
| 315 | |
| 316 | class CsskitViewer extends HTMLElement { |
| 317 | static define(tagName = "csskit-viewer") { |
| 318 | customElements.define(tagName, this); |
| 319 | } |
| 320 | |
| 321 | shadowRoot = this.attachShadow({ mode: "open" }); |
| 322 | |
| 323 | compartment = new Compartment(); |
| 324 | |
| 325 | extensions = [ |
| 326 | csskitLight, |
| 327 | EditorView.editable.of(false), |
| 328 | foldGutter(), |
| 329 | EditorView.lineWrapping, |
| 330 | this.compartment.of(css()), |
| 331 | // Change language according to the current view |
| 332 | EditorState.transactionExtender.of((tr) => { |
| 333 | if (!tr.docChanged) return null; |
| 334 | let format = |
| 335 | this.format === "minify" || this.format === "format" ? css() : json(); |
| 336 | return { |
| 337 | effects: this.compartment.reconfigure(format), |
| 338 | }; |
| 339 | }), |
| 340 | EditorView.domEventHandlers({ mouseover: (e) => this.handleEvent(e) }), |
| 341 | ]; |
| 342 | |
| 343 | get format() { |
| 344 | const form = new FormData( |
| 345 | document.getElementById(this.getAttribute("for-format")), |
| 346 | ); |
| 347 | return form.get("format"); |
| 348 | } |
| 349 | |
| 350 | get formatOptions() { |
| 351 | const form = new FormData( |
| 352 | document.getElementById(this.getAttribute("for-format")), |
| 353 | ); |
| 354 | const options = {}; |
| 355 | for (const [key, value] of form.entries()) { |
| 356 | if (key === "format" || value === "") continue; |
| 357 | if (key === "indent_width") { |
| 358 | const width = Number(value); |
| 359 | if (Number.isFinite(width)) { |
| 360 | options[key] = width; |
| 361 | } |
| 362 | continue; |
| 363 | } |
| 364 | options[key] = value; |
| 365 | } |
| 366 | return options; |
| 367 | } |
| 368 | |
| 369 | get code() { |
| 370 | return document.getElementById(this.getAttribute("for-editor")).code; |
| 371 | } |
| 372 | |
| 373 | handleEvent(event) { |
nothing calls this directly
no test coverage detected