-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMount.ts
150 lines (136 loc) · 5 KB
/
Mount.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {O, OConfig} from './froop/O.js';
import { MntCfg, MountActions, MountProps, PMP, ProPMP } from './ts-refs/trans-render/types.js';
export {MntCfg, MountProps, MountActions} from './ts-refs/trans-render/types.js';
import {MntCfgMxn} from './MntCfgMxn.js';
export class Mount<TProps extends {}, TActions = TProps, ETProps = TProps>
extends O<TProps, TActions>
implements MountActions<TProps, TActions, ETProps>{
static mntCfgMxn: OConfig<MountProps, MountActions> = MntCfgMxn;
#root: ShadowRoot | HTMLElement;
#csr = false;
get csr(){
return (this.config.assumeCSR && !this.hasAttribute('no-csr')) || this.#csr || this.hasAttribute('csr');
}
get config(){
return (<any>this.constructor).config as MntCfg;
}
// get mntConfig(){
// return (<any>this.constructor).mntCfgMxn as OConfig;
// }
get xform(){
return this.config.xform;
}
get xxform(){
return this.config.xxform;
}
constructor(){
super();
const {config} = this;
const {shadowRootInit, styles} = config;
if(shadowRootInit){
if(this.shadowRoot === null){
this.attachShadow(shadowRootInit);
this.#csr = true;
}else if(!styles){
const declarativeStyles = Array.from(this.shadowRoot.querySelectorAll('style[adopt]'));
config.styles = declarativeStyles.map(x => x.innerHTML);
}
this.#root = this.shadowRoot!;
let stringStyles: Array<string> | undefined;
if(typeof(styles) === 'string'){
stringStyles = [styles];
}else if(Array.isArray(styles) && styles.length > 0 && typeof(styles[0]) === 'string'){
stringStyles = styles as Array<string>;
}else if(typeof(styles) === undefined){
stringStyles = [''];
}
if(stringStyles !== undefined){
stringStyles = stringStyles.map(x => x.replace("<style>", "").replace("</style>", ""));
stringStyles[0] += String.raw `
* {
--attrs-to-reflect: initial;
}
`;
const CSSStyleSheets: CSSStyleSheet[] = [];
for(const stringSyleSheet of stringStyles){
const newSheet = new CSSStyleSheet();
newSheet.replaceSync(stringSyleSheet);
CSSStyleSheets.push(newSheet);
}
config.styles = CSSStyleSheets;
}
if(this.#csr){
let compiledStyles = config.styles as CSSStyleSheet[] | undefined;
if(compiledStyles !== undefined){
this.shadowRoot!.adoptedStyleSheets = [...compiledStyles];
}
}
}else{
this.#root = this;
}
}
cloneMT(self: this): Partial<MountProps> {
const {config} = this;
let {mainTemplate} = config;
if(typeof mainTemplate === 'string'){
const templ = document.createElement('template');
templ.innerHTML = mainTemplate;
config.mainTemplate = templ;
mainTemplate = templ;
}
const clonedTemplate = mainTemplate.content.cloneNode(true);
return {
clonedTemplate
} as Partial<MountProps>
}
async initCSRXform(self: this): ProPMP<TProps, TActions, ETProps> {
const {clonedTemplate, xform, propagator, xxform} = self;
const {Transform} = await import('./Transform.js');
if(xform !== undefined){
await Transform<any, any>(clonedTemplate!, this, xform, {
propagator,
propagatorIsReady: true,
});
}
if(xxform !== undefined){
await Transform<any, any>(clonedTemplate!, this, xxform, {
propagator,
propagatorIsReady: true,
useViewTransition: true,
});
}
return {
hydrated: true,
}
}
mountClone(self: this): PMP<TProps, TActions, ETProps> {
const {clonedTemplate} = self;
this.#root.appendChild(clonedTemplate!);
return {
}
}
async initSSRXform(self: this): ProPMP<TProps, TActions, ETProps> {
const root = self.#root;
const {xform, propagator, xxform} = self;
const {Transform} = await import('./Transform.js');
if(xform !== undefined){
await Transform<any, any>(root, this, xform!, {
propagator
});
}
if(xxform !== undefined){
await Transform<any, any>(root, this, xform!, {
propagator, useViewTransition: true
});
}
return {
hydrated: true
}
}
async onNoXForm(self: this): ProPMP<TProps, TActions, ETProps> {
return {
hydrated: true,
}
}
}
export interface Mount<TProps = any, TActions = TProps> extends MountProps{}