@solidpepper/solidpepper-solidgeneration 0.1.1 → 0.1.2

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.
@@ -482,13 +482,14 @@ class DataSheetSVG {
482
482
  // }
483
483
  // backgroundTpl += tag.XmlTag.outerHTML;
484
484
  // }
485
- let jFirstSku = lstSkus[0];
485
+ let jFirstSku = lstSkus && lstSkus.length > 0 ? lstSkus[0] : {};
486
+ const isWithoutSku = !lstSkus || lstSkus.length === 0;
486
487
  // Remplacer les tags du background
487
488
  let allBackgroundTagsLeft = this.GetSvgGroupToReplace(backgroundTplLeft);
488
- backgroundTplLeft = await this.ReplaceFieldsTemplateSVG(jFirstSku, backgroundTplLeft, allBackgroundTagsLeft);
489
+ backgroundTplLeft = await this.ReplaceFieldsTemplateSVG(jFirstSku, backgroundTplLeft, allBackgroundTagsLeft, 0, 0, isWithoutSku);
489
490
  backgroundTplLeft = this.PrepareBackgroundTpl(backgroundTplLeft);
490
491
  let allBackgroundTagsRight = this.GetSvgGroupToReplace(backgroundTplRight);
491
- backgroundTplRight = await this.ReplaceFieldsTemplateSVG(jFirstSku, backgroundTplRight, allBackgroundTagsRight);
492
+ backgroundTplRight = await this.ReplaceFieldsTemplateSVG(jFirstSku, backgroundTplRight, allBackgroundTagsRight, 0, 0, isWithoutSku);
492
493
  backgroundTplRight = this.PrepareBackgroundTpl(backgroundTplRight);
493
494
  let backgroundTpl = this.DataSheet.leftPage
494
495
  ? backgroundTplRight
@@ -836,15 +837,15 @@ class DataSheetSVG {
836
837
  return [];
837
838
  }
838
839
  }
839
- async GenerateProductBlock(tplProductBlock, templateGuid, publicationTemplateGuid, product, allTags = null, blocksHeight = 0, nbBlockInRow = 0) {
840
+ async GenerateProductBlock(tplProductBlock, templateGuid, publicationTemplateGuid, product, allTags = null, blocksHeight = 0, nbBlockInRow = 0, keepAttributes = false) {
840
841
  if (allTags == null)
841
842
  allTags = this.GetSvgGroupToReplace(tplProductBlock);
842
843
  let jRowSku = product;
843
- let newTplGroup = await this.ReplaceFieldsTemplateSVG(jRowSku, tplProductBlock, allTags, blocksHeight, nbBlockInRow);
844
+ let newTplGroup = await this.ReplaceFieldsTemplateSVG(jRowSku, tplProductBlock, allTags, blocksHeight, nbBlockInRow, keepAttributes);
844
845
  newTplGroup = this.RemoveUnwantedSVGTags(newTplGroup);
845
846
  return newTplGroup;
846
847
  }
847
- async GenerateOnlyProductBlock(publicationTemplateString, publicationTemplateVariantTableSetting, templateGuid, publicationTemplateGuid, productData, xStart = 0, yStart = 0) {
848
+ async GenerateOnlyProductBlock(publicationTemplateString, publicationTemplateVariantTableSetting, templateGuid, publicationTemplateGuid, productData, xStart = 0, yStart = 0, keepAttributes = false) {
848
849
  await this.LoadAttributesIfEmpty();
849
850
  let allTags = this.GetSvgGroupToReplace(publicationTemplateString);
850
851
  let blocksHeight = 0;
@@ -862,7 +863,7 @@ class DataSheetSVG {
862
863
  this.DataSheet.backgroundWidth = width;
863
864
  this.DataSheet.productBlockWidth = width;
864
865
  this.DataSheet.publicationTemplateBackgroundFooterY = 99999999;
865
- let newTplGroup = await this.GenerateProductBlock(publicationTemplateString, templateGuid, publicationTemplateGuid, productData, allTags, blocksHeight, nbBlockInRow);
866
+ let newTplGroup = await this.GenerateProductBlock(publicationTemplateString, templateGuid, publicationTemplateGuid, productData, allTags, blocksHeight, nbBlockInRow, keepAttributes);
866
867
  newTplGroup = this.ChangeAllYBis(newTplGroup, yStart);
867
868
  newTplGroup = this.ChangeAllX(newTplGroup, nbBlockInRow, xStart);
868
869
  const doc = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
@@ -1962,7 +1963,7 @@ class DataSheetSVG {
1962
1963
  return null;
1963
1964
  }
1964
1965
  }
1965
- async getNewValTable(jRowSku, tag, svgContent, allBlocksHeight, blockWidth, nbBlockInRow) {
1966
+ async getNewValTable(jRowSku, tag, svgContent, allBlocksHeight, blockWidth, nbBlockInRow, keepAttributes = false) {
1966
1967
  if (!jRowSku.hasOwnProperty('skus')) {
1967
1968
  return [];
1968
1969
  }
@@ -2187,7 +2188,7 @@ class DataSheetSVG {
2187
2188
  let allRowsWithoutStyle = [];
2188
2189
  // Préparer les cellules pour pouvoir supprimer les colonnes non voulus si besoin
2189
2190
  for (let iSku = 0; iSku < skus.length; iSku++) {
2190
- const { currentRow: row, currentRowWithoutStyle: rowWithoutStyle } = await this.generateRowHTML(skus[iSku], arrayField, SkuRows, iSku);
2191
+ const { currentRow: row, currentRowWithoutStyle: rowWithoutStyle } = await this.generateRowHTML(skus[iSku], arrayField, SkuRows, iSku, keepAttributes);
2191
2192
  allRows.push(row);
2192
2193
  allRowsWithoutStyle.push(rowWithoutStyle);
2193
2194
  }
@@ -2263,7 +2264,10 @@ class DataSheetSVG {
2263
2264
  const doc = parser.parseFromString(htmlContent, 'text/html');
2264
2265
  // Accéder au tableau par son ID
2265
2266
  const table = doc.getElementById(tableId);
2267
+ const style = doc.querySelector('style');
2266
2268
  table.style.maxWidth = widthOfTable + 'px';
2269
+ if (style)
2270
+ document.body.appendChild(style);
2267
2271
  document.body.appendChild(table);
2268
2272
  // Taille du tableau (largeur et hauteur)
2269
2273
  const rect = table.getBoundingClientRect();
@@ -2273,6 +2277,8 @@ class DataSheetSVG {
2273
2277
  : rect.width;
2274
2278
  const tableHeight = rect.height;
2275
2279
  document.body.removeChild(table);
2280
+ if (style)
2281
+ document.body.removeChild(style);
2276
2282
  newHeight = tableHeight > newHeight ? tableHeight : newHeight;
2277
2283
  newWidth = tableWidth > newWidth ? tableWidth : newWidth;
2278
2284
  if (nbTables == 0) {
@@ -2449,11 +2455,16 @@ class DataSheetSVG {
2449
2455
  const parser = new DOMParser();
2450
2456
  const doc = parser.parseFromString(`<html><head>${this.DataSheet.publicationTemplateSettingVariantsTableStyle}</head><body>${tables[i]}</body></html>`, 'text/html');
2451
2457
  const table = doc.getElementById(tableId);
2458
+ const style = doc.querySelector('style');
2452
2459
  if (table) {
2460
+ if (style)
2461
+ document.body.appendChild(style);
2453
2462
  document.body.appendChild(table);
2454
2463
  const rect = table.getBoundingClientRect();
2455
2464
  const tableHeight = rect.height;
2456
2465
  document.body.removeChild(table);
2466
+ if (style)
2467
+ document.body.removeChild(style);
2457
2468
  if (i == 0) {
2458
2469
  heightFirstTable = tableHeight;
2459
2470
  }
@@ -2479,12 +2490,12 @@ class DataSheetSVG {
2479
2490
  return null;
2480
2491
  }
2481
2492
  }
2482
- async generateRowHTML(jSku, arrayField, SkuRows, iSku) {
2493
+ async generateRowHTML(jSku, arrayField, SkuRows, iSku, keepAttributes = false) {
2483
2494
  let currentRow = '<tr>';
2484
2495
  let currentRowWithoutStyle = "<tr class='body'>";
2485
2496
  for (let i = 0; i < arrayField.length; i++) {
2486
2497
  const fieldObject = arrayField[i];
2487
- const val = await this.generateCellContent(fieldObject, jSku, SkuRows[i]);
2498
+ const val = await this.generateCellContent(fieldObject, jSku, SkuRows[i], keepAttributes);
2488
2499
  const tdStyle = this.generateCellStyle(this.DataSheet.publicationTemplateSettingVariants?.headerRow?.[i], this.DataSheet.publicationTemplateSettingVariants?.bodyRow?.[i], iSku);
2489
2500
  currentRow += `<td ${tdStyle}>${val}</td>`;
2490
2501
  currentRowWithoutStyle += `<td class="tdCol${i}">${val}</td>`;
@@ -2493,7 +2504,7 @@ class DataSheetSVG {
2493
2504
  currentRowWithoutStyle += '</tr>';
2494
2505
  return { currentRow, currentRowWithoutStyle };
2495
2506
  }
2496
- async generateCellContent(fieldObjectTable, skuValue, skuRowValue) {
2507
+ async generateCellContent(fieldObjectTable, skuValue, skuRowValue, keepAttributes = false) {
2497
2508
  let val = '';
2498
2509
  let valFinal = '';
2499
2510
  let i = 0;
@@ -2502,14 +2513,14 @@ class DataSheetSVG {
2502
2513
  let values = '';
2503
2514
  if (fieldName.includes('||')) {
2504
2515
  fieldName.split('||').forEach((field, index) => {
2505
- let valPart = this.getNewVal(field, skuValue, skuRowValue[i].split('//')[index]);
2516
+ let valPart = this.getNewVal(field, skuValue, skuRowValue[i].split('//')[index], null, true, false, ', ', keepAttributes);
2506
2517
  valPart = this.applyTextTransform(valPart, fieldObject.textTransform);
2507
2518
  values += this.replaceSpecialCharactersForHtml(valPart) + ' <hr /> ';
2508
2519
  });
2509
2520
  val = values.slice(0, -8);
2510
2521
  }
2511
2522
  else {
2512
- val = this.getNewVal(fieldName, skuValue, skuRowValue[i]);
2523
+ val = this.getNewVal(fieldName, skuValue, skuRowValue[i], null, true, false, ', ', keepAttributes);
2513
2524
  val = this.applyTextTransform(val, fieldObject.textTransform);
2514
2525
  val = this.replaceSpecialCharactersForHtml(val);
2515
2526
  }
@@ -2838,10 +2849,13 @@ class DataSheetSVG {
2838
2849
  return tableHtml;
2839
2850
  }
2840
2851
  }
2841
- getNewVal(field, jRowSku, idVal = '', tag = null, showText = true, showPicto = false, separator = ', ') {
2852
+ getNewVal(field, jRowSku, idVal = '', tag = null, showText = true, showPicto = false, separator = ', ', keepAttributes = false) {
2842
2853
  let newVal = '';
2843
- if (jRowSku == null) {
2844
- console.log('[DEBUG] jRowSku is null');
2854
+ if (jRowSku == null ||
2855
+ (Object.keys(jRowSku).length === 0 && keepAttributes)) {
2856
+ if (keepAttributes) {
2857
+ return `${idVal || field}`;
2858
+ }
2845
2859
  return '';
2846
2860
  }
2847
2861
  try {
@@ -3247,14 +3261,25 @@ class DataSheetSVG {
3247
3261
  }
3248
3262
  // HTML formatting to SVG
3249
3263
  if (newVal != null) {
3250
- newVal = newVal.replace(/<(?!br\s*\/?|img\b)[^>]+>/gi, '');
3264
+ newVal = newVal.replace(/<li[^>]*>/gi, '• ');
3265
+ newVal = newVal.replace(/<\/li>\s*/gi, '</li>');
3266
+ newVal = newVal.replace(/<\/li>/gi, '<br />');
3267
+ if (!keepAttributes) {
3268
+ newVal = newVal.replace(/<(?!br\s*\/?|img\b)[^>]+>/gi, '');
3269
+ }
3251
3270
  newVal = newVal.replace(/<br\s*>/gi, '<br />');
3252
3271
  newVal = newVal.replace(/(\r\n|\n|\r)/g, '<br />');
3253
3272
  }
3273
+ if ((newVal === '' || newVal === null) && keepAttributes) {
3274
+ return `${idVal || field}`;
3275
+ }
3254
3276
  return newVal;
3255
3277
  }
3256
3278
  catch (e) {
3257
3279
  console.error(e);
3280
+ if (keepAttributes) {
3281
+ return `${idVal || field}`;
3282
+ }
3258
3283
  return null;
3259
3284
  }
3260
3285
  }
@@ -3435,10 +3460,15 @@ class DataSheetSVG {
3435
3460
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
3436
3461
  }
3437
3462
  replaceSpecialCharactersForHtml(newVal) {
3463
+ if (newVal == null)
3464
+ return newVal;
3438
3465
  newVal = newVal.replace(/&/g, '&amp;');
3439
3466
  newVal = newVal.replace(/</g, '&lt;');
3440
3467
  newVal = newVal.replace(/>/g, '&gt;');
3441
3468
  newVal = newVal.replace(/"/g, '&quot;');
3469
+ // Re-allow structure tags
3470
+ newVal = newVal.replace(/&lt;br\s*\/?&gt;/gi, '<br />');
3471
+ newVal = newVal.replace(/&lt;hr\s*\/?&gt;/gi, '<hr />');
3442
3472
  return newVal;
3443
3473
  }
3444
3474
  PrepareBackgroundTpl(svg) {
@@ -3509,7 +3539,7 @@ class DataSheetSVG {
3509
3539
  delay(ms = 0) {
3510
3540
  return new Promise((resolve) => setTimeout(resolve, ms));
3511
3541
  }
3512
- async ReplaceFieldsTemplateSVG(jRowSku, tpl, allTags, allBlocksHeight = 0, nbBlockInRow = 0) {
3542
+ async ReplaceFieldsTemplateSVG(jRowSku, tpl, allTags, allBlocksHeight = 0, nbBlockInRow = 0, keepAttributes = false) {
3513
3543
  tpl = tpl.replace(/>\s+</g, '><');
3514
3544
  tpl = tpl.replace(/foreignObject\s*\.cellContent\s*div\s*\{[^}]*max-content[^}]*\}/g, function (match) {
3515
3545
  return match.replace('max-content', 'fit-content');
@@ -3547,7 +3577,7 @@ class DataSheetSVG {
3547
3577
  const blockWidth = blockTag.Width;
3548
3578
  let yVal = parseFloat(tag.RectPosition['y']);
3549
3579
  tag.RectPosition['y'] = Math.round(yVal + numberToAddToTableY);
3550
- const tables = await this.getNewValTable(jRowSku, tag, tpl, allBlocksHeight, blockWidth, nbBlockInRow);
3580
+ const tables = await this.getNewValTable(jRowSku, tag, tpl, allBlocksHeight, blockWidth, nbBlockInRow, keepAttributes);
3551
3581
  // concatener les tableaux et les mettre en string
3552
3582
  newVal = tables ? tables.join('') : '';
3553
3583
  // remplacer les foreignobject par foreignObject
@@ -3654,7 +3684,7 @@ class DataSheetSVG {
3654
3684
  const showPicto = tspan.getAttribute('data-show-picto') || 'false';
3655
3685
  const separator = tspan.getAttribute('data-separator') || ', ';
3656
3686
  const textTransform = tspan.getAttribute('data-text-transform');
3657
- let valueToInsert = this.getNewVal(field, jRowSku, idVal, tag, showText === 'true', showPicto === 'true', separator);
3687
+ let valueToInsert = this.getNewVal(field, jRowSku, idVal, tag, showText === 'true', showPicto === 'true', separator, keepAttributes);
3658
3688
  valueToInsert = this.applyTextTransform(valueToInsert, textTransform);
3659
3689
  if (valueToInsert != null &&
3660
3690
  innerHTML.includes(placeholder)) {
@@ -3672,7 +3702,7 @@ class DataSheetSVG {
3672
3702
  }
3673
3703
  }
3674
3704
  if (!replaced) {
3675
- let valueToInsert = this.getNewVal(field, jRowSku, idVal);
3705
+ let valueToInsert = this.getNewVal(field, jRowSku, idVal, null, true, false, ', ', keepAttributes);
3676
3706
  if (valueToInsert != null) {
3677
3707
  const placeholder = `{{${idVal}}}`;
3678
3708
  const textTransform = tag.XmlTag.getAttribute('data-text-transform');
@@ -3821,9 +3851,11 @@ class DataSheetSVG {
3821
3851
  tag.VariantsTable == false &&
3822
3852
  tag.Image == false) {
3823
3853
  if (newVal != '' && newVal != null) {
3824
- newVal = newVal.replace(/contenteditable="false"/g, '');
3825
- newVal = newVal.replace(/class="textAttribute"/g, '');
3826
- newVal = newVal.replace(/class="textAttribute warning"/g, '');
3854
+ if (!keepAttributes) {
3855
+ newVal = newVal.replace(/contenteditable="false"/g, '');
3856
+ newVal = newVal.replace(/class="textAttribute"/g, '');
3857
+ newVal = newVal.replace(/class="textAttribute warning"/g, '');
3858
+ }
3827
3859
  newVal = await this.deleteBlock(newVal);
3828
3860
  newVal = await this.deleteLine(newVal);
3829
3861
  tag.XmlTag.innerHTML = newVal;
@@ -3847,7 +3879,7 @@ class DataSheetSVG {
3847
3879
  const showPicto = tspan.getAttribute('data-show-picto') || 'false';
3848
3880
  const separator = tspan.getAttribute('data-separator') || ', ';
3849
3881
  const textTransform = tspan.getAttribute('data-text-transform');
3850
- let cellVal = this.getNewVal(fieldForCell, jRowSku, idVal, tag, showText === 'true', showPicto === 'true', separator);
3882
+ let cellVal = this.getNewVal(fieldForCell, jRowSku, idVal, tag, showText === 'true', showPicto === 'true', separator, keepAttributes);
3851
3883
  cellVal = this.applyTextTransform(cellVal, textTransform);
3852
3884
  const placeholder = document.createTextNode(cellVal ?? '');
3853
3885
  tspan.parentNode?.insertBefore(placeholder, tspan);
@@ -4093,7 +4125,7 @@ class DataSheetSVG {
4093
4125
  // Récupérer tous les objets ayant data-url
4094
4126
  let urlToWork = domSVG.querySelectorAll('[data-url]');
4095
4127
  urlToWork.forEach((element) => {
4096
- let url = this.GetTagUrl(element, jRowSku);
4128
+ let url = this.GetTagUrl(element, jRowSku, keepAttributes);
4097
4129
  if (url) {
4098
4130
  let typeElement = element.nodeName.toLowerCase();
4099
4131
  if (typeElement == 'foreignobject') {
@@ -4121,6 +4153,9 @@ class DataSheetSVG {
4121
4153
  gabaritSvgUpdated = gabaritSvgUpdated.replace(/<body.*?>|<\/body>/g, '');
4122
4154
  gabaritSvgUpdated = gabaritSvgUpdated.replace(/<head.*?>|<\/head>/g, '');
4123
4155
  tpl = gabaritSvgUpdated;
4156
+ if (!keepAttributes) {
4157
+ tpl = tpl.replace(/\{\{[^}]*\}\}/g, '');
4158
+ }
4124
4159
  return tpl;
4125
4160
  }
4126
4161
  optimizeProductTable(foEl, tpl) {
@@ -4744,7 +4779,7 @@ class DataSheetSVG {
4744
4779
  tag.XmlTag.setAttribute('height', height.toString());
4745
4780
  tag.Height = height;
4746
4781
  }
4747
- GetTagUrl(tag, jRowSku) {
4782
+ GetTagUrl(tag, jRowSku, keepAttributes = false) {
4748
4783
  try {
4749
4784
  if (!tag.XmlTag) {
4750
4785
  tag = {
@@ -4760,7 +4795,7 @@ class DataSheetSVG {
4760
4795
  catch (e) { }
4761
4796
  let fieldName = href.toLowerCase().replace('sku', '').toUpperCase();
4762
4797
  let field = this.getPropertyOfField(fieldName);
4763
- let newVal = this.getNewVal(field, jRowSku, fieldName);
4798
+ let newVal = this.getNewVal(field, jRowSku, fieldName, null, true, false, ', ', keepAttributes);
4764
4799
  if (newVal) {
4765
4800
  try {
4766
4801
  const url = new URL(newVal);