@cocreate/render 1.33.0 → 1.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/index.js +58 -36
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.34.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.33.0...v1.34.0) (2023-11-22)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* improved placeholder support ([165c450](https://github.com/CoCreate-app/CoCreate-render/commit/165c450b833c317ce5b55f9d2fc9d9de16d68f69))
|
|
7
|
+
|
|
1
8
|
# [1.33.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.32.0...v1.33.0) (2023-11-19)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -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
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
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
|
-
}
|
|
558
|
-
|
|
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
|
-
|
|
562
|
-
|
|
563
|
-
|
|
580
|
+
} catch (error) {
|
|
581
|
+
console.error(error)
|
|
582
|
+
}
|
|
564
583
|
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
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 }
|