66 OnUnmount ,
77 OnUnmounted ,
88 PulsAdapter ,
9- resetLifecycleHooks
9+ resetLifecycleHooks ,
10+ currentLifecycleDefines
1011} from "pulsjs-adapter" ;
1112
1213export type ValueTransformer < T > = {
@@ -28,6 +29,25 @@ export class PulsDOMAdapter extends PulsAdapter<Node[]>{
2829 return this . documentOverride ?? window . document
2930 }
3031
32+ setLifecycleDefines ( tag : any , attributes : Record < string , any > , slot : any ) {
33+ currentLifecycleDefines . exports = { }
34+ currentLifecycleDefines . props = attributes
35+ currentLifecycleDefines . slot = slot
36+ currentLifecycleDefines . emitsFunction = ( name : string , ...args : any ) => {
37+ const fn = attributes [ `@${ name } ` ]
38+ if ( fn ) {
39+ return fn ( ...args )
40+ }
41+ }
42+ }
43+
44+ clearLifecycleDefines ( ) {
45+ currentLifecycleDefines . emitsFunction = undefined
46+ currentLifecycleDefines . props = { }
47+ currentLifecycleDefines . emitsFunction = undefined
48+ currentLifecycleDefines . slot = undefined
49+ }
50+
3151 partTransformers : Record < string , ( part : ParserOutput ) => any > = {
3252 'text' : ( part ) => [ this . document . createTextNode ( ( part as ParserText ) . value ) ] ,
3353 'element' : ( part ) => {
@@ -39,16 +59,34 @@ export class PulsDOMAdapter extends PulsAdapter<Node[]>{
3959 if ( 'prototype' in conf . tag && conf . tag . prototype instanceof HTMLElement ) {
4060 out = this . createFromValue ( this . createElement ( conf ) )
4161 } else {
42- out = this . createFromValue ( ( conf . tag as ( values : any ) => any ) ( {
43- ...conf . attributes . reduce ( ( acc , [ key , value ] ) => ( {
44- ...acc ,
45- [ key ] : value
46- } ) , { } ) ,
62+ // Function Components Implementation
63+
64+ for ( const [ key , value ] of conf . attributes ) {
65+ if ( key === ':if' || key === ':else-if' || key === ':else' ) {
66+ return this . setConditionFlowAttribute ( key , value , conf )
67+ }
68+ }
69+
70+ const attributes : Record < string , any > = conf . attributes . reduce ( ( acc , [ key , value ] ) => ( {
71+ ...acc ,
72+ [ key ] : value
73+ } ) , { } )
4774
48- ... ( conf . attributes . find ( ( [ k ] ) => k === ':props' ) ?. [ 1 ] || { } ) ,
75+ const slot = ( conf . body . length > 0 ? ( new ( this . constructor as new ( b : ParserOutput [ ] ) => PulsDOMAdapter ) ( conf . body ) ) . render ( ) : undefined )
4976
50- $slot : ( conf . body . length > 0 ? ( new ( this . constructor as new ( b : ParserOutput [ ] ) => PulsDOMAdapter ) ( conf . body ) ) . render ( ) : undefined ) ,
77+ this . setLifecycleDefines ( conf . tag , attributes , slot )
78+
79+ // Call function component
80+ out = this . createFromValue ( ( conf . tag as ( values : any ) => any ) ( {
81+ ...attributes ,
82+ $slot : slot
5183 } ) )
84+
85+ this . clearLifecycleDefines ( )
86+
87+ if ( ':ref' in attributes ) {
88+ attributes [ ':ref' ] ( ...( out || [ ] ) , currentLifecycleDefines . exports )
89+ }
5290 }
5391
5492 let lifeCycleComment : undefined | Comment = undefined
@@ -181,41 +219,45 @@ export class PulsDOMAdapter extends PulsAdapter<Node[]>{
181219 }
182220 }
183221
184- setAttribute ( el : Element | undefined , key : string , value : any , parserTag : ParserTag ) : Node | undefined {
185- if ( key === ':if' || key === ':else-if' || key === ':else' ) {
186- if ( key === ':if' ) {
187- this . currentControlFlow = this . controlFlows . push ( [ value ] ) - 1
188- if ( ! value ) {
189- return this . document . createComment ( 'if' )
190- }
222+ setConditionFlowAttribute ( key : string , value : any , parserTag : ParserTag ) {
223+ if ( key === ':if' ) {
224+ this . currentControlFlow = this . controlFlows . push ( [ value ] ) - 1
225+ if ( ! value ) {
226+ return this . document . createComment ( 'if' )
227+ }
228+ }
229+ if ( key === ':else-if' ) {
230+ if ( typeof this . controlFlows [ this . currentControlFlow ] === 'undefined' ) {
231+ throw new Error ( 'else-if without if' )
191232 }
192- if ( key === ':else-if' ) {
193- if ( typeof this . controlFlows [ this . currentControlFlow ] === 'undefined' ) {
194- throw new Error ( 'else-if without if' )
195- }
196233
197- let isElse = ! this . controlFlows [ this . currentControlFlow ] . includes ( true )
198- this . controlFlows [ this . currentControlFlow ] . push ( value )
234+ let isElse = ! this . controlFlows [ this . currentControlFlow ] . includes ( true )
235+ this . controlFlows [ this . currentControlFlow ] . push ( value )
199236
200- if ( ! ( isElse && value ) ) {
201- return this . document . createComment ( 'if' )
202- }
237+ if ( ! ( isElse && value ) ) {
238+ return this . document . createComment ( 'if' )
239+ }
240+ }
241+ if ( key === ':else' ) {
242+ if ( typeof this . controlFlows [ this . currentControlFlow ] === 'undefined' ) {
243+ throw new Error ( 'else without if before' )
203244 }
204- if ( key === ':else' ) {
205- if ( typeof this . controlFlows [ this . currentControlFlow ] === 'undefined' ) {
206- throw new Error ( 'else without if before' )
207- }
208245
209- let isElse = ! this . controlFlows [ this . currentControlFlow ] . includes ( true )
246+ let isElse = ! this . controlFlows [ this . currentControlFlow ] . includes ( true )
210247
211- if ( ! isElse ) {
212- return this . document . createComment ( 'if' )
213- }
248+ if ( ! isElse ) {
249+ return this . document . createComment ( 'if' )
214250 }
215- return this . createElement ( {
216- ...parserTag ,
217- attributes : parserTag . attributes . filter ( ( [ k ] ) => k !== key )
218- } )
251+ }
252+ return this . createElement ( {
253+ ...parserTag ,
254+ attributes : parserTag . attributes . filter ( ( [ k ] ) => k !== key )
255+ } )
256+ }
257+
258+ setAttribute ( el : Element | undefined , key : string , value : any , parserTag : ParserTag ) : Node | undefined {
259+ if ( key === ':if' || key === ':else-if' || key === ':else' ) {
260+ return this . setConditionFlowAttribute ( key , value , parserTag )
219261 }
220262
221263 if ( el === undefined ) return ;
0 commit comments