Skip to content

Commit 518e956

Browse files
committed
Load the correct language extension based on the lang attribute
1 parent d179dc0 commit 518e956

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

script/code-interactive.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,30 @@ export default class CodeInteractiveElement extends HTMLElement {
5858
}
5959

6060
shadowRoot = this.attachShadow({ mode: "open" })
61-
#editor = new EditorView({
62-
extensions: [setup, javascript(), html(), css()],
63-
parent: this.shadowRoot,
64-
root: this.shadowRoot,
65-
doc: this.textContent,
66-
})
61+
62+
get lang() {
63+
return this.getAttribute("lang")
64+
}
6765

6866
connectedCallback() {
67+
const extensions = [setup]
68+
69+
if (this.lang === "html") {
70+
extensions.push(html())
71+
} else if (this.lang === "css") {
72+
extensions.push(css())
73+
} else {
74+
console.warn(`Language ${this.lang} is not supported. Defaulting to JavaScript`)
75+
extensions.push(javascript())
76+
}
77+
78+
new EditorView({
79+
extensions,
80+
parent: this.shadowRoot,
81+
root: this.shadowRoot,
82+
doc: this.textContent,
83+
})
84+
6985
this.shadowRoot.adoptedStyleSheets.push(styles)
7086
}
7187
}

0 commit comments

Comments
 (0)