@@ -534,52 +534,74 @@ async function renderValues(node, data, key, renderAs, keyPath, parent) {
534
534
535
535
async function renderValue ( node , data , placeholder , renderAs , renderedNode ) {
536
536
let output = placeholder ;
537
-
538
- if ( placeholder . match ( / { { ( .* ?) } } / ) ) {
539
- if ( renderedNode ) {
540
- renderedNodes . set ( node , renderedNode )
541
- }
542
-
543
- let match
544
- do {
545
- match = output . match ( / { { ( [ A - Z a - z 0 - 9 _ . , \[ \] \- ] * ) } } / ) ; // works by getting inner matches first
546
- // match = output.match(/{{([^}]*)} }/);
547
- // match = output.match(/{{(.*?)}}/);
548
- if ( match ) {
549
- let value
550
- try {
551
- let Data = JSON . parse ( '{' + match [ 1 ] . replace ( / ' / g, '"' ) + '}' ) ;
552
- if ( Data . storage || Data . database || Data . array || Data . object || Data . index ) {
553
- Data . method = 'object.read'
554
- value = await CoCreate . crud . send ( Data )
555
- value = value . object [ 0 ] [ Data . key ]
537
+ let regex = / \{ ( [ ^ { } ] + ) \} /
538
+ let match ;
539
+ do {
540
+ match = output . match ( regex ) ;
541
+ if ( match ) {
542
+ let value ;
543
+ try {
544
+ if ( match [ 1 ] . startsWith ( '[' ) && match [ 1 ] . endsWith ( ']' ) ) { // {[]} - Dot-notation
545
+ match [ 1 ] = match [ 1 ] . slice ( 1 , - 1 ) ;
546
+ value = getRenderValue ( node , data , match [ 1 ] , renderAs ) ;
547
+ } else if ( match [ 1 ] . startsWith ( '(' ) && match [ 1 ] . endsWith ( ')' ) ) { // {()} - CSS Selector and JSON Structure
548
+ match [ 1 ] = match [ 1 ] . slice ( 1 , - 1 ) ;
549
+ // TODO: utils.queryElements(match[1])
550
+ // element.getValue()
551
+ } else if ( output . includes ( '{(' + match [ 0 ] + ')}' ) ) { // {()} - JSON Structure
552
+ match [ 0 ] = '{(' + match [ 0 ] + ')}'
553
+ try {
554
+ let Data = JSON . parse ( '{' + match [ 1 ] . replace ( / ' / g, '"' ) + '}' ) ;
555
+ if ( Data . storage || Data . database || Data . array || Data . object || Data . index ) {
556
+ Data . method = 'object.read'
557
+ value = await CoCreate . crud . send ( Data )
558
+ value = value . object [ 0 ] [ Data . key ]
559
+ }
560
+ } catch ( error ) {
561
+ value = getRenderValue ( node , data , match [ 1 ] , renderAs )
556
562
}
557
- } catch ( error ) {
558
- value = getRenderValue ( node , data , match [ 1 ] , renderAs )
563
+ } else if ( output . includes ( '{{' + match [ 1 ] + '}}' ) ) { // {{}} - Dot-notation && JSON Structure
564
+ match [ 0 ] = '{{' + match [ 1 ] + '}}'
565
+ try {
566
+ let Data = JSON . parse ( '{' + match [ 1 ] . replace ( / ' / g, '"' ) + '}' ) ;
567
+ if ( Data . storage || Data . database || Data . array || Data . object || Data . index ) {
568
+ Data . method = 'object.read'
569
+ value = await CoCreate . crud . send ( Data )
570
+ value = value . object [ 0 ] [ Data . key ]
571
+ }
572
+ } catch ( error ) {
573
+ value = getRenderValue ( node , data , match [ 1 ] , renderAs )
574
+ }
575
+ } else {
576
+ // Otherwise, retun original ouptut
577
+ return output
559
578
}
560
579
561
- if ( value || value === "" ) {
562
- if ( typeof value === "object" )
563
- value = JSON . stringify ( value , null , 2 )
580
+ } catch ( error ) {
581
+ console . error ( error )
582
+ }
564
583
565
- output = output . replace ( match [ 0 ] , value ) ;
566
- } else if ( renderAs ) {
567
- if ( match [ 0 ] . startsWith ( `{{${ renderAs } .` ) ) {
568
- output = output . replace ( match [ 0 ] , "" ) ;
569
- } else {
570
- match = null
571
- }
584
+ if ( value || value === "" ) {
585
+ if ( typeof value === "object" ) {
586
+ value = JSON . stringify ( value , null , 2 ) ;
587
+ }
588
+ output = output . replace ( match [ 0 ] , value ) ;
589
+ } else if ( renderAs ) {
590
+ if ( match [ 0 ] . startsWith ( `{{${ renderAs } .` ) ) {
591
+ output = output . replace ( match [ 0 ] , "" ) ;
572
592
} else {
573
- match = null
593
+ match = null ;
574
594
}
575
-
595
+ } else {
596
+ match = null ;
576
597
}
598
+ }
599
+ } while ( match ) ;
577
600
578
- } while ( match )
579
- }
580
601
return output ;
581
602
}
582
603
604
+
583
605
function getRenderValue ( node , data , key , renderAs ) {
584
606
let value = getValueFromObject ( data , key ) ;
585
607
@@ -777,4 +799,4 @@ Observer.init({
777
799
778
800
init ( )
779
801
780
- export default { render, sources, renderedNodes }
802
+ export default { render, renderValue , sources, renderedNodes }
0 commit comments