[add] element:section

This commit is contained in:
fenris 2024-02-08 11:10:07 +01:00
parent a8c321b60e
commit 0e26fe8078
6 changed files with 167 additions and 1 deletions

View file

@ -0,0 +1,30 @@
/**
*/
type type_element_list_data = {
items : Array<type_element>;
};
/**
*/
class type_element_list implements type_element
{
public readonly kind : string = "list";
public readonly data : type_element_list_data;
public constructor(data : type_element_list_data) {this.data = data;}
}
/**
*/
element_kind_register(
"list",
(data) => (
new type_element_list(
{
"items": data["items"],
}
)
)
);

View file

@ -0,0 +1,32 @@
/**
*/
type type_element_section_data = {
title : string;
content : type_element;
};
/**
*/
class type_element_section implements type_element
{
public readonly kind : string = "section";
public readonly data : type_element_section_data;
public constructor(data : type_element_section_data) {this.data = data;}
}
/**
*/
element_kind_register(
"section",
(data) => (
new type_element_section(
{
"title": data["title"],
"content": data["content"],
}
)
)
);

View file

@ -13,3 +13,57 @@ interface type_output<type_result> extends lib_plankton.call.type_coproduct
}
/**
*/
let output_kind_pool : Record<
string,
(
(
data : any,
make : ((raw : any) => type_output<any>)
)
=>
type_output<any>
)
> = {};
/**
*/
function output_kind_register<type_result>(
name : string,
factory : (
(data : any, make : ((raw : any) => type_output<type_result>))
=>
type_output<type_result>
)
) : void
{
if (name in output_kind_pool)
{
throw (new Error("kind '" + name + "' already registered"));
}
else
{
output_kind_pool[name] = factory;
}
}
/**
*/
function output_make<type_result>(
raw : any
) : type_output<type_result>
{
if (! (raw["kind"] in output_kind_pool))
{
throw (new Error("kind '" + raw["kind"] + "' not registered"));
}
else
{
return output_kind_pool[raw["kind"]](raw["data"], output_make<type_result>);
}
}

View file

@ -30,10 +30,32 @@ class type_output_html implements type_output<string>
+
"</div>\n"
),
"section": ({"title": title, "content": content}) => (
"<section class=\"sd-section\">\n"
+
(
"<header>"
+
title
+
"</header>"
)
+
this.render_element(content)
+
"</section>\n"
),
"list": ({"items": items}) => (
"<ul class=\"sd-list\">\n"
+
items.map(x => this.render_element(x)).map(x => ("<li>" + x + "</li>\n")).join("")
+
"</ul>\n"
),
},
{
"fallback": (element) => (
"<pre class=\"sd-unhandled\">"
"<pre class=\"sd-unhandled\" rel=\"" + element.kind + "\">"
+
JSON.stringify(element, undefined, " ")
+
@ -44,3 +66,16 @@ class type_output_html implements type_output<string>
}
}
/**
*/
output_kind_register(
"html",
(data, sub) => (
new type_output_html(
{
}
)
)
);

View file

@ -24,3 +24,16 @@ class type_output_json implements type_output<string>
}
}
/**
*/
output_kind_register(
"json",
(data, sub) => (
new type_output_json(
{
}
)
)
);

View file

@ -27,6 +27,8 @@ ${dir_temp}/sd-unlinked.js: \
${dir_source}/elements/base.ts \
${dir_source}/elements/implementations/text.ts \
${dir_source}/elements/implementations/group.ts \
${dir_source}/elements/implementations/section.ts \
${dir_source}/elements/implementations/list.ts \
${dir_source}/outputs/base.ts \
${dir_source}/outputs/implementations/json.ts \
${dir_source}/outputs/implementations/html.ts \