Skip to content

Commit 165c450

Browse files
committed
feat: improved placeholder support
- {{...}} dotnotaion and valid crud json depreciating - {[]} dot notation - {()} crud json or css selctor to getValue
1 parent 0875c40 commit 165c450

File tree

1 file changed

+58
-36
lines changed

1 file changed

+58
-36
lines changed

src/index.js

+58-36
Original file line numberDiff line numberDiff line change
@@ -534,52 +534,74 @@ async function renderValues(node, data, key, renderAs, keyPath, parent) {
534534

535535
async function renderValue(node, data, placeholder, renderAs, renderedNode) {
536536
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)
556562
}
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
559578
}
560579

561-
if (value || value === "") {
562-
if (typeof value === "object")
563-
value = JSON.stringify(value, null, 2)
580+
} catch (error) {
581+
console.error(error)
582+
}
564583

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], "");
572592
} else {
573-
match = null
593+
match = null;
574594
}
575-
595+
} else {
596+
match = null;
576597
}
598+
}
599+
} while (match);
577600

578-
} while (match)
579-
}
580601
return output;
581602
}
582603

604+
583605
function getRenderValue(node, data, key, renderAs) {
584606
let value = getValueFromObject(data, key);
585607

@@ -777,4 +799,4 @@ Observer.init({
777799

778800
init()
779801

780-
export default { render, sources, renderedNodes }
802+
export default { render, renderValue, sources, renderedNodes }

0 commit comments

Comments
 (0)