@solidpepper/solidpepper-solidgeneration 0.2.0 → 0.2.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.
@@ -474,6 +474,7 @@ class DataSheetSingleton {
474
474
  class DataSheetSVG {
475
475
  constructor(dataSheet) {
476
476
  this.previewId = 0;
477
+ this.debugTable = false;
477
478
  if (!dataSheet) {
478
479
  throw new Error('DataSheet instance is required to create a DataSheetSVG');
479
480
  }
@@ -782,9 +783,9 @@ class DataSheetSVG {
782
783
  let widths = this.DataSheet.publicationTemplateBackgroundLeftMargin;
783
784
  for (let i = 0; i < nbBlockInRow; i++) {
784
785
  widths +=
785
- aWidths[i] +
786
- aGabaritMarginRights[i] +
787
- aGabaritMarginLefts[i + 1];
786
+ (aWidths[i] || 0) +
787
+ (aGabaritMarginRights[i] || 0) +
788
+ (aGabaritMarginLefts[i + 1] || 0);
788
789
  }
789
790
  let RowTotalWidth = widths + blockWidth - (nbBlockInRow > 0 ? gMarginLeft : 0);
790
791
  const willWrap = RowTotalWidth >
@@ -816,8 +817,7 @@ class DataSheetSVG {
816
817
  blocksHeight = 0;
817
818
  }
818
819
  else {
819
- let blockY = this.DataSheet.GetBlockY(allTags);
820
- blocksHeight = currentBlockYTop + blockY;
820
+ blocksHeight = currentBlockYTop;
821
821
  }
822
822
  let newTplGroup = await this.GenerateProductBlock(tplProductBlock, gabarit.templateGuid, gabarit.guid, jObject, allTags, blocksHeight, nbBlockInRow);
823
823
  blockTag = allTags.find((x) => x.IdTag && x.IdTag.toLowerCase() === 'block');
@@ -834,6 +834,10 @@ class DataSheetSVG {
834
834
  : 0;
835
835
  }
836
836
  blockY = this.DataSheet.GetBlockY(allTags);
837
+ const origBlockHeight = blockHeight;
838
+ const heightRes = this.getActualBlockHeight(newTplGroup, blockHeight, blockY);
839
+ blockHeight = heightRes.actualHeight;
840
+ newTplGroup = heightRes.updatedSvg;
837
841
  let allBlockHeight = 0;
838
842
  heights = 0;
839
843
  for (let i = 0; i < nbRow; i++) {
@@ -857,9 +861,9 @@ class DataSheetSVG {
857
861
  widths = this.DataSheet.publicationTemplateBackgroundLeftMargin;
858
862
  for (let i = 0; i < nbBlockInRow; i++) {
859
863
  widths +=
860
- aWidths[i] +
861
- aGabaritMarginRights[i] +
862
- aGabaritMarginLefts[i + 1];
864
+ (aWidths[i] || 0) +
865
+ (aGabaritMarginRights[i] || 0) +
866
+ (aGabaritMarginLefts[i + 1] || 0);
863
867
  }
864
868
  RowTotalWidth =
865
869
  widths +
@@ -869,8 +873,14 @@ class DataSheetSVG {
869
873
  const pattern = /<foreignObject id="tableVariant".*?<\/foreignObject>/gs;
870
874
  const matches = newTplGroup.match(pattern);
871
875
  // Manage the variants tables if they are more than 1 (change of column / page)
872
- if (matches && matches.length > 1) {
876
+ if (this.debugTable) {
877
+ console.log(`[GEN-DEBUG] Matches check for Item ${currentIdx + 1}: matches=${matches?.length}, splitVariantTable=${jObject.splitVariantTable}, heights=${heights}, blockHeight=${blockHeight}`);
878
+ }
879
+ if (matches &&
880
+ matches.length > 1 &&
881
+ jObject.splitVariantTable !== false) {
873
882
  let otherPage = newTplGroup;
883
+ const priorHeights = heights;
874
884
  skipPart = true;
875
885
  for (let i = 0; i < matches.length; i++) {
876
886
  newTplGroup = otherPage;
@@ -879,6 +889,11 @@ class DataSheetSVG {
879
889
  newTplGroup = newTplGroup.replace(matches[n], '');
880
890
  }
881
891
  }
892
+ const matchHeightRes = this.getActualBlockHeight(newTplGroup, origBlockHeight, blockY);
893
+ const matchBlockHeight = matchHeightRes.actualHeight;
894
+ if (this.debugTable) {
895
+ console.log(`[GEN-DEBUG] Item ${currentIdx + 1} match ${i + 1}/${matches.length}: matchBlockHeight=${matchBlockHeight}, origBlockHeight=${origBlockHeight}, priorHeights=${priorHeights}`);
896
+ }
882
897
  // Go on a new column
883
898
  let newColumn = false;
884
899
  if (newColumn) {
@@ -927,13 +942,12 @@ class DataSheetSVG {
927
942
  allBlockHeight =
928
943
  this.DataSheet.publicationTemplateProductBlockY +
929
944
  newRowHeights +
930
- this.DataSheet.variantTableFirstHeight -
945
+ matchBlockHeight -
931
946
  blockY;
932
947
  if ((allBlockHeight <=
933
948
  this.DataSheet.publicationTemplateBackgroundFooterY ||
934
949
  (nbRow === 0 && aHeightsRows.length === 0)) &&
935
- allBlockHeight != 0 &&
936
- i == 0) {
950
+ allBlockHeight != 0) {
937
951
  const diffY = newRowHeights - heights;
938
952
  heights = newRowHeights;
939
953
  newTplGroup = this.ChangeAllIdsGroup(newTplGroup, nbBlock);
@@ -949,7 +963,7 @@ class DataSheetSVG {
949
963
  aGabaritMarginRights = [];
950
964
  aGabaritMarginRights.push(gMarginRight);
951
965
  nbRow++;
952
- aHeightsRows.push(this.DataSheet.variantTableFirstHeight);
966
+ aHeightsRows.push(Math.max(origBlockHeight, matchBlockHeight));
953
967
  aRowMarginTops.push(gMarginTop);
954
968
  aRowMarginBottoms.push(gMarginBottom);
955
969
  }
@@ -978,7 +992,7 @@ class DataSheetSVG {
978
992
  };
979
993
  fileList.push(json);
980
994
  newTplGroup = this.ChangeAllX(newTplGroup, nbBlockInRow, this.DataSheet.publicationTemplateBackgroundLeftMargin);
981
- newTplGroup = this.ChangeAllYBis(newTplGroup, -heights);
995
+ newTplGroup = this.ChangeAllYBis(newTplGroup, -priorHeights);
982
996
  AllTplProductsBlock = '';
983
997
  newTplGroup = this.wrapTemplateGroup(newTplGroup, jObject, gabarit, nbBlock);
984
998
  tplRowProductsBlocks = newTplGroup;
@@ -989,7 +1003,7 @@ class DataSheetSVG {
989
1003
  aGabaritMarginRights = [];
990
1004
  aGabaritMarginRights.push(gMarginRight);
991
1005
  aHeightsRows = [];
992
- aHeightsRows.push(blockHeight);
1006
+ aHeightsRows.push(Math.max(origBlockHeight, matchBlockHeight));
993
1007
  aRowMarginTops = [];
994
1008
  aRowMarginTops.push(gMarginTop);
995
1009
  aRowMarginBottoms = [];
@@ -998,6 +1012,7 @@ class DataSheetSVG {
998
1012
  nbRow = 0;
999
1013
  nbBlock = 0;
1000
1014
  nbBlockInRow = 0;
1015
+ heights = 0;
1001
1016
  widths =
1002
1017
  this.DataSheet.publicationTemplateBackgroundLeftMargin +
1003
1018
  blockWidth;
@@ -1025,7 +1040,7 @@ class DataSheetSVG {
1025
1040
  tplRowProductsBlocks += newTplGroup;
1026
1041
  if (aHeightsRows.length == 0) {
1027
1042
  const rowInfo = getRowInfo(currentIdx);
1028
- currentRowMaxHeight = rowInfo.maxHeight;
1043
+ currentRowMaxHeight = Math.max(rowInfo.maxHeight, blockHeight);
1029
1044
  currentRowMaxMarginTop = rowInfo.maxMarginTop;
1030
1045
  currentRowMaxMarginBottom = rowInfo.maxMarginBottom;
1031
1046
  aHeightsRows.push(currentRowMaxHeight);
@@ -1047,7 +1062,7 @@ class DataSheetSVG {
1047
1062
  }
1048
1063
  else if (RowTotalWidth != 0) {
1049
1064
  const rowInfo = getRowInfo(currentIdx);
1050
- const currentRowMaxHeight = rowInfo.maxHeight;
1065
+ const currentRowMaxHeight = Math.max(rowInfo.maxHeight, blockHeight);
1051
1066
  const currentRowMaxMarginTop = rowInfo.maxMarginTop;
1052
1067
  const currentRowMaxMarginBottom = rowInfo.maxMarginBottom;
1053
1068
  const newRowHeights = heights +
@@ -1082,8 +1097,7 @@ class DataSheetSVG {
1082
1097
  aHeightsRows.push(currentRowMaxHeight);
1083
1098
  aRowMarginTops.push(currentRowMaxMarginTop);
1084
1099
  aRowMarginBottoms.push(currentRowMaxMarginBottom);
1085
- nbBlockInRow = 0;
1086
- nbBlock++;
1100
+ nbBlockInRow = 1;
1087
1101
  }
1088
1102
  // Go on a new page (418)
1089
1103
  else if (allBlockHeight != 0) {
@@ -1129,10 +1143,9 @@ class DataSheetSVG {
1129
1143
  count++;
1130
1144
  nbRow = 0;
1131
1145
  nbBlock = 0;
1132
- nbBlockInRow = 0;
1146
+ nbBlockInRow = 1;
1147
+ heights = 0;
1133
1148
  }
1134
- nbBlockInRow = 0;
1135
- nbBlock++;
1136
1149
  }
1137
1150
  else {
1138
1151
  console.log('[ERROR] RowTotalWidth is 0, this should not happen.');
@@ -1600,6 +1613,11 @@ class DataSheetSVG {
1600
1613
  }
1601
1614
  }
1602
1615
  ChangeAllYBis(tplGroup, blockHeight) {
1616
+ if (typeof blockHeight !== 'number' ||
1617
+ isNaN(blockHeight) ||
1618
+ blockHeight === 0) {
1619
+ return tplGroup;
1620
+ }
1603
1621
  const patternTag = /<[\w\W]+?.>/g;
1604
1622
  const patternHeight = /height\s*=\s*"([^"]*)"/;
1605
1623
  const patternY = /(?<![a-zA-Z])y\s*=\s*"-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?"/g;
@@ -2650,8 +2668,9 @@ class DataSheetSVG {
2650
2668
  skus = jRowSku['skus'] && jRowSku['skus'][0] ? jRowSku['skus'][0] : [];
2651
2669
  let currentRow = '';
2652
2670
  let currentRowWithoutStyle = '';
2653
- let startWithNewPage = jRowSku['splitVariantTable'] === true ? false : true;
2671
+ let startWithNewPage = jRowSku['splitVariantTable'] === false ? true : false;
2654
2672
  let startY = 0;
2673
+ let accumulatedTableHeight = 0;
2655
2674
  let allRows = [];
2656
2675
  let allRowsWithoutStyle = [];
2657
2676
  // Préparer les cellules pour pouvoir supprimer les colonnes non voulus si besoin
@@ -2754,32 +2773,18 @@ class DataSheetSVG {
2754
2773
  }
2755
2774
  if (iSku === 0) {
2756
2775
  if (!startWithNewPage) {
2757
- let currentWidthLine = this.DataSheet.currentWidth +
2758
- this.DataSheet.publicationTemplateBackgroundLeftMargin +
2759
- this.DataSheet.publicationTemplateBackgroundRightMargin;
2760
- if (currentWidthLine + blockWidth >
2761
- this.DataSheet.backgroundWidth) {
2762
- if (allBlocksHeight == 0) {
2763
- allBlocksHeight =
2764
- this.DataSheet.publicationTemplateBackgroundHeightHeader;
2765
- }
2766
- startY = allBlocksHeight + this.DataSheet.lastGabaritHeight;
2767
- }
2768
- else {
2769
- startY = allBlocksHeight;
2770
- }
2771
- if (startY + newHeight >
2772
- this.DataSheet.publicationTemplateBackgroundFooterY) {
2773
- startY = this.DataSheet.publicationTemplateBackgroundHeightHeader;
2774
- }
2776
+ startY = allBlocksHeight;
2775
2777
  }
2776
2778
  else {
2777
2779
  startY = this.DataSheet.publicationTemplateBackgroundHeightHeader;
2778
2780
  }
2779
2781
  }
2780
- // console.log(tag.RectPosition["y"] + " " + newHeight + " " + allBlocksHeight + " > " + this.DataSheet.publicationTemplateBackgroundFooterY);
2781
- // console.log(tag.RectPosition["y"] + newHeight + " " + startY + " > " + this.DataSheet.publicationTemplateBackgroundFooterY);
2782
- // console.log("----------")
2782
+ const currentTableYPos = parseFloat(tag.RectPosition['y']) || 0;
2783
+ const totalBottomPos = startY + currentTableYPos + newHeight;
2784
+ const isOverflow = totalBottomPos > this.DataSheet.publicationTemplateBackgroundFooterY;
2785
+ if (this.debugTable) {
2786
+ console.log(`[TABLE-SPLIT-LOG] SKU ${iSku + 1}/${skus.length} (${sku.code || sku.skuCode || 'sku'}): startY=${startY}, tableY=${currentTableYPos}, newHeight=${newHeight}, totalBottom=${totalBottomPos}, FooterY=${this.DataSheet.publicationTemplateBackgroundFooterY}, overflow=${isOverflow}, nbTables=${nbTables}`);
2787
+ }
2783
2788
  if (skus[skus.length - 1] === sku && skus.length === 1) {
2784
2789
  tables[nbTables] =
2785
2790
  newTag +
@@ -2797,15 +2802,8 @@ class DataSheetSVG {
2797
2802
  currentRow +
2798
2803
  '</tbody></table></foreignObject>';
2799
2804
  }
2800
- else if ((tag.RectPosition['y'] + newHeight + startY >
2801
- this.DataSheet.publicationTemplateBackgroundFooterY &&
2802
- nbTables < maxBlockInRow - nbBlockInRow) ||
2803
- tag.RectPosition['y'] + newHeight + startY >
2804
- this.DataSheet.publicationTemplateBackgroundFooterY ||
2805
- (tag.RectPosition['y'] + newHeight + startY >
2806
- this.DataSheet.publicationTemplateBackgroundFooterY &&
2807
- maxBlockInRow === 1 &&
2808
- nbTables === 0)) {
2805
+ else if (startY + (parseFloat(tag.RectPosition['y']) || 0) + newHeight >
2806
+ this.DataSheet.publicationTemplateBackgroundFooterY) {
2809
2807
  htmlContent = htmlContent.replace(currentRowWithoutStyle, '');
2810
2808
  const parser = new DOMParser();
2811
2809
  const doc = parser.parseFromString(htmlContent, 'text/html');
@@ -2840,6 +2838,7 @@ class DataSheetSVG {
2840
2838
  tables[nbTables] +
2841
2839
  '</tbody></table></foreignObject>';
2842
2840
  nbTables++;
2841
+ accumulatedTableHeight += newHeight;
2843
2842
  tableWithoutStyle = currentRowWithoutStyle;
2844
2843
  tables.push(currentRow);
2845
2844
  }
@@ -2890,6 +2889,7 @@ class DataSheetSVG {
2890
2889
  }
2891
2890
  }
2892
2891
  else if (skus[skus.length - 1] === sku) {
2892
+ const currentTableY = parseFloat(tag.RectPosition['y']);
2893
2893
  tables[nbTables] =
2894
2894
  newTag +
2895
2895
  ' width="' +
@@ -2899,7 +2899,7 @@ class DataSheetSVG {
2899
2899
  '" x="' +
2900
2900
  tag.RectPosition['x'] +
2901
2901
  '" y="' +
2902
- tag.RectPosition['y'] +
2902
+ currentTableY +
2903
2903
  '">' +
2904
2904
  newVal +
2905
2905
  tables[nbTables] +
@@ -3445,19 +3445,16 @@ class DataSheetSVG {
3445
3445
  newVal = '';
3446
3446
  }
3447
3447
  }
3448
- let urlBase = '';
3449
- if (jRowSku['skuImagesDetail'] != null &&
3450
- jRowSku['skuImagesDetail'] != '') {
3451
- if (jRowSku['skuImagesDetail'][0] != null &&
3452
- jRowSku['skuImagesDetail'][0] != '') {
3453
- urlBase = jRowSku['skuImagesDetail'][0].assetUrlOriginal;
3454
- urlBase = urlBase.substring(0, urlBase.lastIndexOf('/') + 1);
3455
- }
3456
- }
3448
+ let urlBase = this.getUrlBase(jRowSku);
3457
3449
  if (urlBase != null &&
3458
3450
  urlBase != '' &&
3459
3451
  newVal != null &&
3460
- newVal != '') {
3452
+ newVal != '' &&
3453
+ typeof newVal === 'string' &&
3454
+ !newVal.startsWith('http://') &&
3455
+ !newVal.startsWith('https://') &&
3456
+ !newVal.startsWith('data:') &&
3457
+ !newVal.startsWith('/')) {
3461
3458
  newVal = urlBase + newVal;
3462
3459
  }
3463
3460
  }
@@ -3478,7 +3475,6 @@ class DataSheetSVG {
3478
3475
  const matchedKey = Object.keys(foundObj).find((k) => (field && k.toUpperCase() === field.toUpperCase()) ||
3479
3476
  (idVal && k.toUpperCase() === idVal.toUpperCase()));
3480
3477
  newVal = matchedKey ? foundObj[matchedKey] : '';
3481
- console.log(newVal);
3482
3478
  let valuePairs = new Map();
3483
3479
  let i = 0;
3484
3480
  if (newVal != null) {
@@ -3515,19 +3511,16 @@ class DataSheetSVG {
3515
3511
  newVal = '';
3516
3512
  }
3517
3513
  }
3518
- let urlBase = '';
3519
- if (jRowSku['skuImagesDetail'] != null &&
3520
- jRowSku['skuImagesDetail'] != '') {
3521
- if (jRowSku['skuImagesDetail'][0] != null &&
3522
- jRowSku['skuImagesDetail'][0] != '') {
3523
- urlBase = jRowSku['skuImagesDetail'][0].assetUrlOriginal;
3524
- urlBase = urlBase.substring(0, urlBase.lastIndexOf('/') + 1);
3525
- }
3526
- }
3514
+ let urlBase = this.getUrlBase(jRowSku);
3527
3515
  if (urlBase != null &&
3528
3516
  urlBase != '' &&
3529
3517
  newVal != null &&
3530
- newVal != '') {
3518
+ newVal != '' &&
3519
+ typeof newVal === 'string' &&
3520
+ !newVal.startsWith('http://') &&
3521
+ !newVal.startsWith('https://') &&
3522
+ !newVal.startsWith('data:') &&
3523
+ !newVal.startsWith('/')) {
3531
3524
  newVal = urlBase + newVal;
3532
3525
  }
3533
3526
  }
@@ -4378,20 +4371,79 @@ class DataSheetSVG {
4378
4371
  let skuImagesDetail = [];
4379
4372
  if (jRowSku &&
4380
4373
  jRowSku['skuImagesDetail'] &&
4374
+ Array.isArray(jRowSku['skuImagesDetail']) &&
4381
4375
  jRowSku['skuImagesDetail'].length > 0) {
4382
- skuImagesDetail = jRowSku['skuImagesDetail'].filter((image) => image && image.assetName === newVal);
4383
- if (skuImagesDetail.length > 0) {
4384
- if (this.DataSheet.preview &&
4385
- this.DataSheet.imageFastPreview == true) {
4386
- newVal = skuImagesDetail[0].assetUrlSmall;
4376
+ const cleanNewVal = this.getCleanFilename(newVal);
4377
+ const cleanNewValNoExt = this.getFileNameWithoutExt(newVal).toLowerCase();
4378
+ skuImagesDetail = jRowSku['skuImagesDetail'].filter((image) => {
4379
+ if (!image)
4380
+ return false;
4381
+ if (image.assetName === newVal || image.assetGuid === newVal)
4382
+ return true;
4383
+ if (image.assetName && this.getCleanFilename(image.assetName).toLowerCase() === cleanNewVal.toLowerCase()) {
4384
+ return true;
4385
+ }
4386
+ if (image.assetName && this.getFileNameWithoutExt(image.assetName).toLowerCase() === cleanNewValNoExt) {
4387
+ return true;
4387
4388
  }
4388
- else if (this.DataSheet.highDefinition == false) {
4389
- newVal = skuImagesDetail[0].assetUrlMedium;
4390
- urlHD = skuImagesDetail[0].assetUrlOriginal;
4389
+ const urls = [
4390
+ image.assetUrlOriginal,
4391
+ image.assetUrlMedium,
4392
+ image.assetUrlSmall,
4393
+ image.assetUrlBig,
4394
+ ].filter(Boolean);
4395
+ for (const url of urls) {
4396
+ if (url === newVal)
4397
+ return true;
4398
+ if (this.getCleanFilename(url).toLowerCase() === cleanNewVal.toLowerCase())
4399
+ return true;
4400
+ if (this.getFileNameWithoutExt(url).toLowerCase() === cleanNewValNoExt)
4401
+ return true;
4402
+ }
4403
+ return false;
4404
+ });
4405
+ }
4406
+ if (skuImagesDetail.length === 0 && jRowSku && jRowSku['skuDefaultImage']) {
4407
+ const defaultImg = jRowSku['skuDefaultImage'];
4408
+ const cleanNewValNoExt = this.getFileNameWithoutExt(newVal).toLowerCase();
4409
+ const defaultNameNoExt = this.getFileNameWithoutExt(defaultImg.assetName || defaultImg.assetUrlOriginal || '').toLowerCase();
4410
+ if (!cleanNewValNoExt || cleanNewValNoExt === defaultNameNoExt || defaultImg.assetGuid === newVal) {
4411
+ skuImagesDetail = [defaultImg];
4412
+ }
4413
+ }
4414
+ if (skuImagesDetail.length > 0) {
4415
+ const matchedAsset = skuImagesDetail[0];
4416
+ if (this.DataSheet.preview &&
4417
+ this.DataSheet.imageFastPreview == true) {
4418
+ newVal = matchedAsset.assetUrlSmall || matchedAsset.assetUrlMedium || matchedAsset.assetUrlBig || matchedAsset.assetUrlOriginal || newVal;
4419
+ urlHD = matchedAsset.assetUrlOriginal || matchedAsset.assetUrlBig || matchedAsset.assetUrlMedium || matchedAsset.assetUrlSmall || newVal;
4420
+ }
4421
+ else if (this.DataSheet.highDefinition == false) {
4422
+ newVal = matchedAsset.assetUrlMedium || matchedAsset.assetUrlSmall || matchedAsset.assetUrlBig || matchedAsset.assetUrlOriginal || newVal;
4423
+ urlHD = matchedAsset.assetUrlOriginal || matchedAsset.assetUrlBig || matchedAsset.assetUrlMedium || matchedAsset.assetUrlSmall || newVal;
4424
+ }
4425
+ else {
4426
+ newVal = matchedAsset.assetUrlOriginal || matchedAsset.assetUrlBig || matchedAsset.assetUrlMedium || matchedAsset.assetUrlSmall || newVal;
4427
+ urlHD = matchedAsset.assetUrlOriginal || matchedAsset.assetUrlBig || matchedAsset.assetUrlMedium || matchedAsset.assetUrlSmall || newVal;
4428
+ }
4429
+ }
4430
+ else {
4431
+ let urlBase = this.getUrlBase(jRowSku);
4432
+ if (urlBase && typeof newVal === 'string' && !newVal.startsWith('http://') && !newVal.startsWith('https://') && !newVal.startsWith('data:') && !newVal.startsWith('/')) {
4433
+ newVal = urlBase + newVal;
4434
+ }
4435
+ urlHD = newVal;
4436
+ if (typeof newVal === 'string' &&
4437
+ (this.DataSheet.highDefinition == false || (this.DataSheet.preview && this.DataSheet.imageFastPreview == true))) {
4438
+ if (this.DataSheet.preview && this.DataSheet.imageFastPreview == true) {
4439
+ newVal = newVal
4440
+ .replace(/\/original\//i, '/small/')
4441
+ .replace(/\.[a-zA-Z0-9]+(?=\?|#|$)/i, '.webp');
4391
4442
  }
4392
4443
  else {
4393
- newVal = skuImagesDetail[0].assetUrlOriginal;
4394
- urlHD = skuImagesDetail[0].assetUrlOriginal;
4444
+ newVal = newVal
4445
+ .replace(/\/original\//i, '/medium/')
4446
+ .replace(/\.[a-zA-Z0-9]+(?=\?|#|$)/i, '.webp');
4395
4447
  }
4396
4448
  }
4397
4449
  }
@@ -4721,7 +4773,29 @@ class DataSheetSVG {
4721
4773
  const size = this.optimizeProductTable(tag.XmlTag, tpl);
4722
4774
  tag.XmlTag.style.setProperty('overflow', 'visible', 'important');
4723
4775
  if (size.height) {
4776
+ const oldHeight = parseFloat(tag.XmlTag.getAttribute('height')) || 0;
4724
4777
  tag.XmlTag.setAttribute('height', String(size.height));
4778
+ const newHeight = size.height;
4779
+ if (newHeight > oldHeight && oldHeight > 0) {
4780
+ const deltaY = newHeight - oldHeight;
4781
+ const tableY = parseFloat(tag.XmlTag.getAttribute('y')) || 0;
4782
+ const tableBottom = tableY + oldHeight;
4783
+ allTags.forEach((otherTag) => {
4784
+ if (otherTag !== tag && otherTag.XmlTag) {
4785
+ if (otherTag.XmlTag.hasAttribute('data-anchor-target')) {
4786
+ return;
4787
+ }
4788
+ const otherY = parseFloat(otherTag.XmlTag.getAttribute('y')) || 0;
4789
+ if (otherY > tableY + 10) {
4790
+ const newOtherY = otherY + deltaY;
4791
+ otherTag.XmlTag.setAttribute('y', String(newOtherY));
4792
+ if (otherTag.RectPosition) {
4793
+ otherTag.RectPosition.y = newOtherY;
4794
+ }
4795
+ }
4796
+ }
4797
+ });
4798
+ }
4725
4799
  }
4726
4800
  if (size.width) {
4727
4801
  tag.XmlTag.setAttribute('width', String(size.width));
@@ -5094,6 +5168,56 @@ class DataSheetSVG {
5094
5168
  const textareaElement = tempDiv.querySelector(`#${textareaId}`);
5095
5169
  if (textareaElement) {
5096
5170
  let parentFo = textareaElement.closest('foreignObject');
5171
+ const splitDivAtBr = (div) => {
5172
+ const br = div.querySelector('br');
5173
+ if (!br || !div.parentNode)
5174
+ return false;
5175
+ const nextDiv = div.cloneNode(true);
5176
+ let currInDiv = div.querySelector('br');
5177
+ while (currInDiv && currInDiv !== div) {
5178
+ const parent = currInDiv.parentNode;
5179
+ if (!parent)
5180
+ break;
5181
+ while (currInDiv.nextSibling) {
5182
+ parent.removeChild(currInDiv.nextSibling);
5183
+ }
5184
+ const toRemove = currInDiv;
5185
+ currInDiv = parent;
5186
+ if (toRemove.nodeName.toLowerCase() === 'br') {
5187
+ parent.removeChild(toRemove);
5188
+ }
5189
+ }
5190
+ let currInNext = nextDiv.querySelector('br');
5191
+ while (currInNext && currInNext !== nextDiv) {
5192
+ const parent = currInNext.parentNode;
5193
+ if (!parent)
5194
+ break;
5195
+ while (currInNext.previousSibling) {
5196
+ parent.removeChild(currInNext.previousSibling);
5197
+ }
5198
+ const toRemove = currInNext;
5199
+ currInNext = parent;
5200
+ if (toRemove.nodeName.toLowerCase() === 'br') {
5201
+ parent.removeChild(toRemove);
5202
+ }
5203
+ }
5204
+ div.parentNode.insertBefore(nextDiv, div.nextSibling);
5205
+ if (!nextDiv.textContent || !nextDiv.textContent.trim()) {
5206
+ nextDiv.remove();
5207
+ }
5208
+ return true;
5209
+ };
5210
+ let hasBr = true;
5211
+ while (hasBr) {
5212
+ hasBr = false;
5213
+ const divsWithBr = Array.from(textareaElement.querySelectorAll('div')).filter((d) => d.querySelector('br') !== null);
5214
+ for (const d of divsWithBr) {
5215
+ if (splitDivAtBr(d)) {
5216
+ hasBr = true;
5217
+ break;
5218
+ }
5219
+ }
5220
+ }
5097
5221
  // Split long text wrapping into separate divs to prevent overlap and highlight blockiness
5098
5222
  const lineDivs = Array.from(textareaElement.querySelectorAll('div')).filter((d) => {
5099
5223
  return d.querySelectorAll('div').length === 0;
@@ -5809,6 +5933,84 @@ class DataSheetSVG {
5809
5933
  reader.readAsDataURL(blob);
5810
5934
  });
5811
5935
  }
5936
+ getActualBlockHeight(svgContent, initialBlockHeight, blockY = 0) {
5937
+ if (!svgContent)
5938
+ return { actualHeight: initialBlockHeight, updatedSvg: svgContent };
5939
+ const hasVariantTable = /id=["']tableVariant["']/i.test(svgContent) ||
5940
+ /data-info=["']variantTable["']/i.test(svgContent);
5941
+ if (!hasVariantTable) {
5942
+ return { actualHeight: initialBlockHeight, updatedSvg: svgContent };
5943
+ }
5944
+ let maxBottom = 0;
5945
+ try {
5946
+ // 1. Measure foreignObjects (tables, textareas, etc.)
5947
+ const foRegex = /<foreignObject[\s\S]*?>/gi;
5948
+ let match;
5949
+ while ((match = foRegex.exec(svgContent)) !== null) {
5950
+ const foTag = match[0];
5951
+ const yMatch = foTag.match(/\by=["']([^"']+)["']/i);
5952
+ const heightMatch = foTag.match(/\bheight=["']([^"']+)["']/i);
5953
+ const foY = yMatch ? parseFloat(yMatch[1]) : 0;
5954
+ const foHeight = heightMatch ? parseFloat(heightMatch[1]) : 0;
5955
+ if (!isNaN(foHeight) && foHeight > 0) {
5956
+ const foBottom = (isNaN(foY) ? 0 : foY) + foHeight;
5957
+ if (foBottom > maxBottom) {
5958
+ maxBottom = foBottom;
5959
+ }
5960
+ }
5961
+ }
5962
+ // 2. Measure images, text, path, rects (excluding background block rect)
5963
+ const elemRegex = /<(rect|image|text|path|ellipse|circle)[\s\S]*?>/gi;
5964
+ while ((match = elemRegex.exec(svgContent)) !== null) {
5965
+ const tagStr = match[0];
5966
+ const idMatch = tagStr.match(/\bid=["']([^"']+)["']/i);
5967
+ if (idMatch && idMatch[1].toLowerCase().includes('block')) {
5968
+ continue;
5969
+ }
5970
+ const yMatch = tagStr.match(/\b(y|cy)=["']([^"']+)["']/i);
5971
+ const heightMatch = tagStr.match(/\b(height|r|ry)=["']([^"']+)["']/i);
5972
+ const eY = yMatch ? parseFloat(yMatch[2]) : 0;
5973
+ const eH = heightMatch ? parseFloat(heightMatch[2]) : 0;
5974
+ if (!isNaN(eH) && eH > 0) {
5975
+ const eBottom = (isNaN(eY) ? 0 : eY) + eH;
5976
+ if (eBottom > maxBottom) {
5977
+ maxBottom = eBottom;
5978
+ }
5979
+ }
5980
+ else if (!isNaN(eY) && eY > 0) {
5981
+ const eBottom = eY + 20;
5982
+ if (eBottom > maxBottom) {
5983
+ maxBottom = eBottom;
5984
+ }
5985
+ }
5986
+ }
5987
+ }
5988
+ catch (e) {
5989
+ console.error('Error calculating actual block height:', e);
5990
+ }
5991
+ let calculatedHeight = initialBlockHeight;
5992
+ if (maxBottom > 0) {
5993
+ calculatedHeight = Math.max(initialBlockHeight, Math.ceil(maxBottom - blockY));
5994
+ }
5995
+ // Update background block rect height in svgContent if present
5996
+ let updatedSvg = svgContent;
5997
+ try {
5998
+ const blockRectRegex = /(<rect[^>]*?\bid=["']block["'][^>]*?\bheight=["'])([^"']+)(["'])/i;
5999
+ if (blockRectRegex.test(updatedSvg)) {
6000
+ updatedSvg = updatedSvg.replace(blockRectRegex, `$1${calculatedHeight}$3`);
6001
+ }
6002
+ else {
6003
+ const blockRectAlt = /(<rect[^>]*?\bheight=["'])([^"']+)(["'][^>]*?\bid=["']block["'])/i;
6004
+ if (blockRectAlt.test(updatedSvg)) {
6005
+ updatedSvg = updatedSvg.replace(blockRectAlt, `$1${calculatedHeight}$3`);
6006
+ }
6007
+ }
6008
+ }
6009
+ catch (e) {
6010
+ console.error('Error updating block rect height in SVG:', e);
6011
+ }
6012
+ return { actualHeight: calculatedHeight, updatedSvg };
6013
+ }
5812
6014
  // DVG TAG
5813
6015
  ChangeBlockHeight(svgContents, newHeight, oldHeight, allTags, currentTag) {
5814
6016
  let updateHeight = false;
@@ -6236,6 +6438,47 @@ class DataSheetSVG {
6236
6438
  el.setAttribute('y', newY.toString());
6237
6439
  });
6238
6440
  }
6441
+ getUrlBase(sku) {
6442
+ if (!sku)
6443
+ return '';
6444
+ if (Array.isArray(sku['skuImagesDetail']) &&
6445
+ sku['skuImagesDetail'].length > 0) {
6446
+ for (const img of sku['skuImagesDetail']) {
6447
+ if (!img)
6448
+ continue;
6449
+ const url = img.assetUrlOriginal ||
6450
+ img.assetUrlMedium ||
6451
+ img.assetUrlSmall ||
6452
+ img.assetUrlBig;
6453
+ if (url && typeof url === 'string' && url.includes('/')) {
6454
+ return url.substring(0, url.lastIndexOf('/') + 1);
6455
+ }
6456
+ }
6457
+ }
6458
+ if (sku['skuDefaultImage']) {
6459
+ const defaultImg = sku['skuDefaultImage'];
6460
+ const url = defaultImg.assetUrlOriginal ||
6461
+ defaultImg.assetUrlMedium ||
6462
+ defaultImg.assetUrlSmall ||
6463
+ defaultImg.assetUrlBig;
6464
+ if (url && typeof url === 'string' && url.includes('/')) {
6465
+ return url.substring(0, url.lastIndexOf('/') + 1);
6466
+ }
6467
+ }
6468
+ return '';
6469
+ }
6470
+ getCleanFilename(str) {
6471
+ if (!str || typeof str !== 'string')
6472
+ return '';
6473
+ const noQuery = str.split('?')[0].split('#')[0];
6474
+ const lastSlash = noQuery.lastIndexOf('/');
6475
+ return lastSlash !== -1 ? noQuery.substring(lastSlash + 1) : noQuery;
6476
+ }
6477
+ getFileNameWithoutExt(str) {
6478
+ const fn = this.getCleanFilename(str);
6479
+ const lastDot = fn.lastIndexOf('.');
6480
+ return lastDot !== -1 ? fn.substring(0, lastDot) : fn;
6481
+ }
6239
6482
  }
6240
6483
 
6241
6484
  class DataSheetSku {