@kubik-cms/wysiwyg 0.1.25 → 0.1.26

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.
@@ -10426,6 +10426,93 @@ function applyMediaMetadataFromGallery(data, resourceIdPath, payload) {
10426
10426
  });
10427
10427
  return result2;
10428
10428
  }
10429
+ const REPEATER_FIELD_NAME = /^(\w+)\[repeated_items\]\[(\d+)\]\.(.+)$/;
10430
+ function parseRepeaterFieldName(name) {
10431
+ const match = name == null ? void 0 : name.match(REPEATER_FIELD_NAME);
10432
+ if (!match)
10433
+ return null;
10434
+ return { tab: match[1], index: Number(match[2]), field: match[3] };
10435
+ }
10436
+ function assignRepeaterField(item, fieldPath, value) {
10437
+ const next = { ...item };
10438
+ if (fieldPath === "image" || fieldPath.endsWith(".id")) {
10439
+ const parentKey = fieldPath === "image" ? "image" : fieldPath.slice(0, -3);
10440
+ next[parentKey] = { ...next[parentKey] || {}, id: value };
10441
+ } else {
10442
+ next[fieldPath] = value;
10443
+ }
10444
+ return next;
10445
+ }
10446
+ function applyRepeaterFieldValue(dataValue, fieldName, value) {
10447
+ const parsed = parseRepeaterFieldName(fieldName);
10448
+ if (!parsed)
10449
+ return null;
10450
+ const tabData = { ...dataValue[parsed.tab] || {} };
10451
+ const items = [...tabData.repeated_items || []];
10452
+ const item = assignRepeaterField(items[parsed.index] || {}, parsed.field, value);
10453
+ items[parsed.index] = item;
10454
+ tabData.repeated_items = items;
10455
+ return { ...dataValue, [parsed.tab]: tabData };
10456
+ }
10457
+ function applyRepeaterMediaMetadata(dataValue, fieldName, payload) {
10458
+ var _a, _b;
10459
+ const parsed = parseRepeaterFieldName(fieldName);
10460
+ if (!parsed)
10461
+ return dataValue;
10462
+ const info = parseAdditionalInformation(payload);
10463
+ if (!info)
10464
+ return dataValue;
10465
+ const mappings = [
10466
+ ["alt_text", info.alt_text],
10467
+ ["caption", (_a = info.img_title) != null ? _a : info.caption],
10468
+ ["credit", (_b = info.img_credit) != null ? _b : info.credit]
10469
+ ];
10470
+ let next = dataValue;
10471
+ mappings.forEach(([field, value]) => {
10472
+ if (value == null || String(value).trim() === "")
10473
+ return;
10474
+ const fullName = `${parsed.tab}[repeated_items][${parsed.index}].${field}`;
10475
+ next = applyRepeaterFieldValue(next, fullName, String(value).trim()) || next;
10476
+ });
10477
+ return next;
10478
+ }
10479
+ function ensureRepeaterItemExpanded(dataValue, fieldName) {
10480
+ const parsed = parseRepeaterFieldName(fieldName);
10481
+ if (!parsed)
10482
+ return dataValue;
10483
+ const tabData = { ...dataValue[parsed.tab] || {} };
10484
+ const items = [...tabData.repeated_items || []];
10485
+ const item = items[parsed.index] || {};
10486
+ items[parsed.index] = {
10487
+ ...item,
10488
+ _ui: { ...item._ui || {}, expanded: true }
10489
+ };
10490
+ tabData.repeated_items = items;
10491
+ return { ...dataValue, [parsed.tab]: tabData };
10492
+ }
10493
+ function syncRepeaterExpandedFromDom(dataValue, root2) {
10494
+ if (!root2)
10495
+ return dataValue;
10496
+ let next = dataValue;
10497
+ root2.querySelectorAll(".kubik-wysiwyg-repeated-item[data-kubik-repeater-tab-value]").forEach((element) => {
10498
+ const tab = element.dataset.kubikRepeaterTabValue;
10499
+ const index2 = Number(element.dataset.kubikRepeaterIndexValue);
10500
+ if (!tab || Number.isNaN(index2))
10501
+ return;
10502
+ const expanded = element.classList.contains("kubik-widget-expanded");
10503
+ const tabData = { ...next[tab] || {} };
10504
+ const items = [...tabData.repeated_items || []];
10505
+ if (!items[index2])
10506
+ return;
10507
+ items[index2] = {
10508
+ ...items[index2],
10509
+ _ui: { ...items[index2]._ui || {}, expanded }
10510
+ };
10511
+ tabData.repeated_items = items;
10512
+ next = { ...next, [tab]: tabData };
10513
+ });
10514
+ return next;
10515
+ }
10429
10516
  class KubikWidgetController extends Controller {
10430
10517
  connect() {
10431
10518
  this.syncExpandedFromData();
@@ -10585,7 +10672,16 @@ class KubikWidgetController extends Controller {
10585
10672
  selectResult(event) {
10586
10673
  const target = event.currentTarget;
10587
10674
  const returnObject = JSON.parse(target.dataset.returnObject);
10588
- const duplicateData = set_1(this.dataValue, target.dataset.fieldName, returnObject.id);
10675
+ const fieldName = target.dataset.fieldName;
10676
+ const repeaterData = applyRepeaterFieldValue(this.dataValue, fieldName, returnObject.id);
10677
+ if (repeaterData) {
10678
+ let next = applyRepeaterMediaMetadata(repeaterData, fieldName, returnObject);
10679
+ next = ensureRepeaterItemExpanded(next, fieldName);
10680
+ this.dataValue = next;
10681
+ this.getNewWidget();
10682
+ return;
10683
+ }
10684
+ const duplicateData = set_1(this.dataValue, fieldName, returnObject.id);
10589
10685
  this.dataValue = duplicateData;
10590
10686
  this.getNewWidget();
10591
10687
  }
@@ -10598,16 +10694,22 @@ class KubikWidgetController extends Controller {
10598
10694
  receiveModalReturn(return_value) {
10599
10695
  const returnObject = return_value;
10600
10696
  const fieldPath = returnObject["return_payload"]["field_name"];
10601
- let duplicateData = set_1(this.dataValue, fieldPath, returnObject["payload"]["id"]);
10602
- duplicateData = applyMediaMetadataFromGallery(
10603
- duplicateData,
10604
- fieldPath,
10605
- returnObject["payload"]
10606
- );
10697
+ const payload = returnObject["payload"];
10698
+ const repeaterData = applyRepeaterFieldValue(this.dataValue, fieldPath, payload["id"]);
10699
+ if (repeaterData) {
10700
+ let next = applyRepeaterMediaMetadata(repeaterData, fieldPath, payload);
10701
+ next = ensureRepeaterItemExpanded(next, fieldPath);
10702
+ this.dataValue = next;
10703
+ this.getNewWidget();
10704
+ return;
10705
+ }
10706
+ let duplicateData = set_1(this.dataValue, fieldPath, payload["id"]);
10707
+ duplicateData = applyMediaMetadataFromGallery(duplicateData, fieldPath, payload);
10607
10708
  this.dataValue = duplicateData;
10608
10709
  this.getNewWidget();
10609
10710
  }
10610
10711
  getNewWidget() {
10712
+ this.dataValue = syncRepeaterExpandedFromDom(this.dataValue, this.element);
10611
10713
  fetch(`${this.setupValue["src"]}`, {
10612
10714
  method: "POST",
10613
10715
  headers: {
@@ -10676,7 +10778,9 @@ __publicField(KubikRepeaterController, "values", {
10676
10778
  });
10677
10779
  class KubikKeyValueRepeaterController extends Controller {
10678
10780
  connect() {
10781
+ this.dataValue = this.compactRows(this.dataValue);
10679
10782
  this.renderFields();
10783
+ this.updateAddButton();
10680
10784
  }
10681
10785
  dataValueChanged() {
10682
10786
  this.dispatch("updateData", {
@@ -10686,6 +10790,12 @@ class KubikKeyValueRepeaterController extends Controller {
10686
10790
  }
10687
10791
  });
10688
10792
  }
10793
+ compactRows(rows) {
10794
+ return rows.filter((row) => !this.rowIsEmpty(row));
10795
+ }
10796
+ rowIsEmpty(row) {
10797
+ return this.fieldsValue.every((field) => (row[field] || "").trim() === "");
10798
+ }
10689
10799
  renderFields() {
10690
10800
  this.valuesContainerTarget.innerHTML = "";
10691
10801
  this.dataValue.forEach((item, index2) => {
@@ -10721,6 +10831,13 @@ class KubikKeyValueRepeaterController extends Controller {
10721
10831
  `
10722
10832
  );
10723
10833
  });
10834
+ this.updateAddButton();
10835
+ }
10836
+ updateAddButton() {
10837
+ if (!this.hasAddButtonTarget)
10838
+ return;
10839
+ const hasRows = this.dataValue.length > 0;
10840
+ this.addButtonTarget.textContent = hasRows ? "Add another" : "Add";
10724
10841
  }
10725
10842
  deleteField(event) {
10726
10843
  const index2 = event.target.dataset.index;
@@ -10749,9 +10866,10 @@ class KubikKeyValueRepeaterController extends Controller {
10749
10866
  }, {});
10750
10867
  this.dataValue = [...this.dataValue, newField];
10751
10868
  this.renderFields();
10869
+ this.updateAddButton();
10752
10870
  }
10753
10871
  }
10754
- __publicField(KubikKeyValueRepeaterController, "targets", ["valuesContainer"]);
10872
+ __publicField(KubikKeyValueRepeaterController, "targets", ["valuesContainer", "addButton"]);
10755
10873
  __publicField(KubikKeyValueRepeaterController, "values", {
10756
10874
  data: Array,
10757
10875
  widgetId: String,