1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-23 17:19:37 +01:00
deka-dom-el/docs/components/example.html.js

69 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2023-11-10 17:15:59 +01:00
import { styles } from "../ssr.js";
2023-11-23 16:30:20 +01:00
const host= "."+example.name;
styles.css`
${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;
}
2024-05-17 22:13:16 +02:00
`;
const dde_content= s.cat(new URL("../../dist/esm-with-signals.js", import.meta.url)).toString();
2024-05-17 22:13:16 +02:00
import { el } from "deka-dom-el";
2023-11-10 17:15:59 +01:00
import { code } from "./code.html.js";
2023-11-23 16:30:20 +01:00
import { relative } from "node:path";
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()
.replaceAll(/ from "deka-dom-el(\/signals)?";/g, ' from "./esm-with-signals.js";');
2023-11-23 16:30:20 +01:00
const id= "code-example-"+generateCodeId(src);
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({
files: [{ name, content }, { name: "esm-with-signals.js", content: dde_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;
2024-11-22 16:26:42 +01:00
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
}
2023-11-23 16:30:20 +01:00
const store_prev= new Map();
/** @param {URL} src */
function generateCodeId(src){
const candidate= parseInt(relative((new URL("..", import.meta.url)).pathname, src.pathname)
.split("")
.map(ch=> ch.charCodeAt(0))
.join(""), 10)
.toString(36)
.replace(/000+/g, "");
const count= 1 + ( store_prev.get(candidate) || 0 );
store_prev.set(candidate, count);
2024-11-22 16:26:42 +01:00
return count.toString()+"-"+candidate;
2023-11-23 16:30:20 +01:00
}