@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 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/render",
3
- "version": "1.33.0",
3
+ "version": "1.34.0",
4
4
  "description": "A simple render component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "render",
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
- if (placeholder.match(/{{(.*?)}}/)) {
539
- if (renderedNode) {
540
- renderedNodes.set(node, renderedNode)
541
- }
542
-
543
- let match
544
- do {
545
- match = output.match(/{{([A-Za-z0-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 }