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
|
|
|
`;
|
|
|
|
|
2024-05-22 21:43:49 +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
|
|
|
|
2023-11-05 21:49:32 +01: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()
|
2024-05-22 21:43:49 +02:00
|
|
|
.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);
|
2023-11-06 14:04:27 +01:00
|
|
|
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({
|
2024-05-22 21:43:49 +02:00
|
|
|
files: [{ name, content }, { name: "esm-with-signals.js", content: dde_content }],
|
2023-10-09 13:51:55 +02:00
|
|
|
toolbar: false
|
|
|
|
});
|
2023-11-05 21:49:32 +01:00
|
|
|
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
|
|
|
}
|
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);
|
|
|
|
return count.toString()+"-"+candidate;
|
|
|
|
}
|