-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeXtalElement.mjs
67 lines (66 loc) · 2.06 KB
/
makeXtalElement.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// @ts-check
/** @import {EndUserProps} from './node_modules/xtal-element/types.d.ts' */
export class HTMLElement {
}
/**
* @template Props
* @template Actions
* @param {EndUserProps<Props, Actions>} xtalElementProps
* @param {((...data: any) => void)=} writer
* @param {Array<string>=} scripts
* @param {string=} className
*/
export function makeXtalElement(xtalElementProps, writer=console.log, scripts, className) {
const {
inherits, propDefaults, xform, lcXform, aka, actions, fa, inferProps,
propInferenceCriteria, propInfo, shadowRootMode, targetScope, mainTemplate,
compacts
} = xtalElementProps;
if (scripts) {
writer(String.raw `<template onload=blow-dry-to-head><script type=module>
${scripts.map(x => x.toString() + '\n\r').join('\n\r')};
${className && inherits ? `customElements.define('${inherits}', ${className})` : ''}
</script></template>
`);
}
if(typeof mainTemplate !== 'string') throw 400;
const end = String.raw `<!--end--><!--end-->`;
const split = mainTemplate.split(end);
writer(split[0]);
writer('<xtal-element');
if (inherits)
writer(` inherits=${inherits} `);
if (propDefaults) {
writer(` prop-defaults='${JSON.stringify(propDefaults, undefined, 3)}'`);
}
if (propInfo) {
writer(` prop-info='${JSON.stringify(propInfo, undefined, 3)}' `);
}
if (xform) {
writer(` xform='${JSON.stringify(xform, undefined, 3)}' `);
}
if (lcXform) {
writer(` lc-xform='${JSON.stringify(lcXform, undefined, 3)}' `);
}
if (actions) {
writer(` actions='${JSON.stringify(actions, undefined, 3)}' `);
}
if(compacts){
writer(` compacts='${JSON.stringify(compacts, undefined, 3)}'`);
}
if (fa) {
writer(` form-associated `);
}
if (inferProps) {
writer(` infer-props `);
}
if (shadowRootMode) {
writer(` shadow-root-mode=${shadowRootMode} `);
}
if (aka) {
writer(` aka=${aka} `);
}
writer('></xtal-element>');
writer(end);
writer(split[1]);
}