@cocreate/render 1.32.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,22 @@
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
+
8
+ # [1.33.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.32.0...v1.33.0) (2023-11-19)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * use action.form and action.params ([90f9428](https://github.com/CoCreate-app/CoCreate-render/commit/90f9428a5c87473d51aec4111b15a0f194f46cb1))
14
+
15
+
16
+ ### Features
17
+
18
+ * update dependecies for th latest features and bug fixes ([733e8cf](https://github.com/CoCreate-app/CoCreate-render/commit/733e8cfa7b601c2c5b494fb132588a67f6d6f433))
19
+
1
20
  # [1.32.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.31.0...v1.32.0) (2023-11-16)
2
21
 
3
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/render",
3
- "version": "1.32.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",
@@ -58,10 +58,10 @@
58
58
  "webpack-log": "^3.0.1"
59
59
  },
60
60
  "dependencies": {
61
- "@cocreate/actions": "^1.12.3",
62
- "@cocreate/api": "^1.15.11",
63
- "@cocreate/element-prototype": "^1.13.0",
64
- "@cocreate/observer": "^1.11.9",
65
- "@cocreate/utils": "^1.27.1"
61
+ "@cocreate/actions": "^1.12.4",
62
+ "@cocreate/api": "^1.15.12",
63
+ "@cocreate/element-prototype": "^1.13.2",
64
+ "@cocreate/observer": "^1.12.0",
65
+ "@cocreate/utils": "^1.27.4"
66
66
  }
67
67
  }
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
 
@@ -652,9 +674,12 @@ function getRenderValue(node, data, key, renderAs) {
652
674
  return value
653
675
  }
654
676
 
655
- async function renderKey(element, params) {
677
+ async function renderKey(action) {
656
678
  // TODO: custom render-keys
657
- const form = element.closest("form") || document;
679
+ let element = action.element
680
+ let form = action.form || document
681
+ let params = action.params
682
+
658
683
  if (!params)
659
684
  params = 'render-key'
660
685
 
@@ -683,7 +708,7 @@ async function renderKey(element, params) {
683
708
  Actions.init({
684
709
  name: "renderKey",
685
710
  callback: (action) => {
686
- renderKey(action.element, action.params);
711
+ renderKey(action);
687
712
  }
688
713
  });
689
714
 
@@ -774,4 +799,4 @@ Observer.init({
774
799
 
775
800
  init()
776
801
 
777
- export default { render, sources, renderedNodes }
802
+ export default { render, renderValue, sources, renderedNodes }