From 813d299c8c9fb3754d610d4c590a835a22f99050 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 14 Aug 2024 21:13:44 +0800 Subject: [PATCH] allow prefix and suffix with double braces --- client/src/pages/printing/printing.tsx | 29 +++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/client/src/pages/printing/printing.tsx b/client/src/pages/printing/printing.tsx index 297e4e40..ebf6276d 100644 --- a/client/src/pages/printing/printing.tsx +++ b/client/src/pages/printing/printing.tsx @@ -95,12 +95,35 @@ function applyTextFormatting(text: string): JSX.Element[] { }); return elements; } + export function renderLabelContents(template: string, spool: ISpool): JSX.Element { // Find all {tags} in the template string and loop over them - let result = template.replace(/\{(.*?)\}/g, function (_, tag) { - return getTagValue(tag, spool); + // let matches = [...template.matchAll(/(?:{(.*?))?{(.*?)}(.*?)(?:}(.*?))?/gs)]; + let matches = [...template.matchAll(/{(?:[^}{]|{[^}{]*})*}/gs)]; +// console.log(matches){(?:[^}{]|{[^}{]*})*} + let label_text = template; + matches.forEach((match) => { + // console.log(match) + if ((match[0].match(/{/g)||[]).length == 1) { + let tag = match[0].replace(/[{}]/g, ""); + // console.log(tag) + let tagValue = getTagValue(tag, spool) + label_text = label_text.replace(match[0], tagValue); + } + else if ((match[0].match(/{/g)||[]).length == 2) { + let structure = match[0].match(/{(.*?){(.*?)}(.*?)}/); + if (structure != null) { + const tag = structure[2]; + let tagValue = getTagValue(tag, spool); + if (tagValue == "?") { + label_text = label_text.replace(match[0], ""); + } else { + label_text = label_text.replace(match[0], structure[1] + tagValue + structure[3]); + } + } + } }); // Split string on \n into individual lines - return <>{applyTextFormatting(result)}; + return <>{applyTextFormatting(label_text)}; }