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

60 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-11-07 15:10:55 +01:00
let is_registered= {};
2023-09-26 16:02:10 +02:00
import { styles } from "../index.css.js";
export const css= styles().scope(example).css`
:host{
2023-11-07 15:10:55 +01:00
grid-column: 1/-1;
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
}
`
import { el } from "deka-dom-el";
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
* @param {string} [attrs.language="javascript"] Language of the code
* @param {string} attrs.page_id ID of the page
* @param {import("../types.d.ts").registerClientFile} attrs.registerClientFile
* */
export function example({ src, language= "javascript", page_id, registerClientFile }){
registerClientPart(page_id, registerClientFile);
const code= 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-09-26 16:02:10 +02:00
el("div", { id, className: example.name }).append(
el("pre").append(
2023-11-06 13:45:43 +01:00
el("code", { className: "language-"+language, textContent: code })
2023-09-26 16:02:10 +02:00
)
),
2023-10-09 13:51:55 +02:00
elCode({ id, content: code })
2023-09-26 16:02:10 +02:00
);
}
2023-10-09 13:51:55 +02:00
function elCode({ id, content }){
const options= JSON.stringify({
files: [{ name: ".js", content }],
toolbar: false
});
return el("script", `Flems(document.getElementById("${id}"), JSON.parse(${JSON.stringify(options)}));`);
2023-10-09 13:51:55 +02:00
}
2023-11-07 15:10:55 +01:00
function registerClientPart(page_id, registerClientFile){
if(is_registered[page_id]) return;
//★ Highlite code when no flems?
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" }),
//★ el("script", { src: "https://cdn.jsdelivr.net/npm/shiki", defer: true }),
2023-09-26 16:02:10 +02:00
);
2023-11-08 14:53:18 +01:00
const d= el("div");
/** @param {HTMLDivElement} a */
const f= (a)=> a;
f(d); //←
2023-11-07 15:10:55 +01:00
//★ egisterClientFile(
//★ new URL("./example.js.js", import.meta.url),
//★ el("script", { type: "module" })
//★ ;
is_registered[page_id]= true;
2023-09-26 16:02:10 +02:00
}