code-block example
This commit is contained in:
parent
3c982fe7de
commit
da36eb9c94
2 changed files with 16 additions and 31 deletions
|
@ -1,39 +1,17 @@
|
|||
// @ts-check
|
||||
|
||||
class CodeBlockComponent extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
const trimmed = this.innerHTML.trim();
|
||||
this.trimmed = this.innerHTML.trim();
|
||||
const lang = this.getAttribute("language");
|
||||
const lineNumbers = this.getAttribute("line-numbers") === "true";
|
||||
|
||||
this.css({
|
||||
color: "orange",
|
||||
margin: "1em 0",
|
||||
padding: "1em",
|
||||
fontWeight: "bold",
|
||||
fontSize: "x-large",
|
||||
display: "block",
|
||||
backgroundColor: "#eee",
|
||||
});
|
||||
console.log(lineNumbers, lang);
|
||||
|
||||
const template = `
|
||||
this.innerHTML = `
|
||||
<div>
|
||||
<pre class="language-${this.lang}" id="pre"><code id="code">${trimmed}</code></pre>
|
||||
<pre class="language-${lang} ${
|
||||
lineNumbers ? "line-numbers" : ""
|
||||
}" id="pre"><code id="code">${this.trimmed}</code></pre>
|
||||
</div>
|
||||
`;
|
||||
|
||||
this.innerHTML = template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply css to an element
|
||||
* @param {Partial<CSSStyleDeclaration>} style
|
||||
* @param {HTMLElement} element
|
||||
*/
|
||||
css(style, element = null) {
|
||||
let el = element ?? this;
|
||||
for (const property in style) el.style[property] = style[property];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<my-head>
|
||||
<link rel="stylesheet" href="/prism.css" />
|
||||
<script src="/prism.js"></script>
|
||||
<script server:setup>
|
||||
const name = "AYOs";
|
||||
let greeting = "hello";
|
||||
|
@ -25,7 +27,12 @@
|
|||
<p>some text: {{greeting}}</p>
|
||||
{{greeting}} hey<br />
|
||||
<clickable-text></clickable-text>
|
||||
<code-block lang="css"> const hey = "AYO" </code-block>
|
||||
<code-block language="js" line-numbers="true">
|
||||
<pre>
|
||||
const hey = "AYO";
|
||||
console.log(hey)
|
||||
</pre>
|
||||
</code-block>
|
||||
</main>
|
||||
<my-footer></my-footer>
|
||||
|
||||
|
|
Loading…
Reference in a new issue