1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-23 00:59:38 +01:00
deka-dom-el/docs_src/components/example.html.js

51 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-11-10 17:15:59 +01:00
import { styles } from "../ssr.js";
styles.scope(example).css`
2023-09-26 16:02:10 +02:00
:host{
2023-11-10 17:15:59 +01:00
grid-column: full-main;
2023-11-06 19:29:54 +01:00
width: 100%;
max-width: calc(9/5 * var(--body-max-width));
height: calc(3/5 * var(--body-max-width));
margin-inline: auto;
2023-09-26 16:02:10 +02:00
}
2023-11-10 17:15:59 +01:00
.CodeMirror, .CodeMirror-gutters {
background: #212121 !important;
border: 1px solid white;
}
2023-09-26 16:02:10 +02:00
`
import { el } from "deka-dom-el";
2023-11-10 17:15:59 +01:00
import { code } from "./code.html.js";
2023-11-07 15:10:55 +01:00
/**
* Prints code to the page and registers flems to make it interactive.
* @param {object} attrs
* @param {URL} attrs.src Example code file path
2023-11-10 17:15:59 +01:00
* @param {"js"|"ts"|"html"|"css"} [attrs.language="js"] Language of the code
2023-11-07 15:10:55 +01:00
* @param {string} attrs.page_id ID of the page
* */
2023-11-10 17:15:59 +01:00
export function example({ src, language= "js", page_id }){
registerClientPart(page_id);
const content= s.cat(src).toString()
2023-09-26 16:02:10 +02:00
.replaceAll(' from "../../../index-with-signals.js";', ' from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";');
const id= "code-"+Math.random().toString(36).slice(2, 7);
return el().append(
2023-11-10 17:15:59 +01:00
el(code, { id, content, language, className: example.name }),
elCode({ id, content, extension: "."+language })
2023-09-26 16:02:10 +02:00
);
}
2023-11-10 17:15:59 +01:00
function elCode({ id, content, extension: name }){
2023-10-09 13:51:55 +02:00
const options= JSON.stringify({
2023-11-10 17:15:59 +01:00
files: [{ name, content }],
2023-10-09 13:51:55 +02:00
toolbar: false
});
return el("script", `Flems(document.getElementById("${id}"), JSON.parse(${JSON.stringify(options)}));`);
2023-10-09 13:51:55 +02:00
}
2023-11-10 17:15:59 +01:00
let is_registered= {};
/** @param {string} page_id */
function registerClientPart(page_id){
2023-11-07 15:10:55 +01:00
if(is_registered[page_id]) return;
2023-09-26 16:02:10 +02:00
document.head.append(
2023-11-07 15:10:55 +01:00
el("script", { src: "https://flems.io/flems.html", type: "text/javascript", charset: "utf-8" }),
2023-09-26 16:02:10 +02:00
);
2023-11-07 15:10:55 +01:00
is_registered[page_id]= true;
2023-09-26 16:02:10 +02:00
}