-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
101 lines (94 loc) · 3.53 KB
/
index.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {DefineArgs} from './types';
import {html} from './html.js';
export {html} from './html.js';
import {Action, TemplMgmtProps} from 'trans-render/lib/types';
// export const doInitTransform = {
// cloneTemplate: {
// ifAllOf: ['mainTemplate'],
// ifKeyIn: ['noshadow', 'waitToInit']
// },
// doInitTransform: {
// ifAllOf: ['clonedTemplate'],
// }
// }
export const beTransformed = {
cloneTemplate: {
ifAllOf: ['mainTemplate'],
ifKeyIn: ['noshadow', 'waitToInit']
} as Action<TemplMgmtProps>,
doTemplMount: {
ifAllOf: ['clonedTemplate', 'transform'],
ifKeyIn: ['waitToInit'],
async: true,
} as Action<TemplMgmtProps>,
};
export function substrBetween(str: string, start: string, end: string): string {
const start_pos = str.indexOf(start);
if(start_pos === -1) return '';
const iPos = str.indexOf(end, start_pos + start.length);
return iPos === -1 ? str.substring(start_pos + start.length) : str.substring(start_pos + start.length, iPos);
}
export function define({innerHTML, encodeAndWrite, mode, dependencies, globalStylePaths, beDefinitiveProps}: DefineArgs){
if(dependencies === undefined) dependencies = [];
if(globalStylePaths === undefined) globalStylePaths = [];
switch(mode){
case '-js':{
const mainTemplate = html`<template ${{
beDefinitive: beDefinitiveProps
}}>${innerHTML}</template>`;
const js =
`import('be-definitive/be-definitive.js');
import('be-active/be-active.js');
${dependencies.map(d => `import('${d}');`).join('\n')}
if(!document.body){
document.addEventListener('DOMContentLoaded', () => {
document.body.insertAdjacentHTML('beforeend', \`${mainTemplate}\`);
});
}else{
document.body.insertAdjacentHTML('beforeend', \`${mainTemplate}\`);
}`;
encodeAndWrite(js);
}
break;
case '-html':{
let scriptRef = '';
let beExportable = '';
if(beDefinitiveProps.scriptPath !== undefined){
beExportable = html`
<script data-version=0.0.28 id="be-exportable/be-exportable.js" data-when=be-importing ></script>`;
if(beDefinitiveProps.scriptRef === undefined) beDefinitiveProps.scriptRef = 'a_' + (new Date()).valueOf();
scriptRef = html`<script id=${beDefinitiveProps.scriptRef} nomodule be-exportable src="${beDefinitiveProps.scriptPath}"></script>`;
}
const h = html`
<${beDefinitiveProps.config.tagName} t-a-i-l-b ${{
beDefinitive: beDefinitiveProps
}}>
<!---->
<template shadowroot="open">
<template be-active>
<script data-version=0.0.39 id="be-importing/be-importing.js"></script>
<script data-version=0.0.69 id="be-definitive/be-definitive.js" data-when=be-importing></script>${beExportable}
${dependencies.map(d => html`<script data-when=be-importing id="${d}"></script>`).join('\n')}
${globalStylePaths.map(p => html`<link rel="stylesheet" href="${p}">`).join('\n')}
</template>
${innerHTML}
</template>
</${beDefinitiveProps.config.tagName}>
${scriptRef}
<script type=module>
if(customElements.get('be-active') === undefined){
import('be-active/be-active.js').catch(err => {
import('https://esm.run/be-active@0.0.44/be-active.js');
});
}
</script>
`;
encodeAndWrite(h);
}
break;
case '-dfn':{
encodeAndWrite(JSON.stringify(beDefinitiveProps));
}
break;
}
}