@puckeditor/plugin-ai 0.5.0-canary.dd1cead3 → 0.5.1-canary.2caa830b

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.
package/dist/index.js CHANGED
@@ -1419,6 +1419,35 @@ var getSelectorForId = (state, id) => {
1419
1419
  return { zone: zoneCompound, index };
1420
1420
  };
1421
1421
  var getItemById = (state, id) => state.indexes.nodes[id].data;
1422
+ var applyArrayDefaults = (oldProps, newProps, fields) => {
1423
+ const updatedProps = {
1424
+ ...oldProps,
1425
+ ...newProps
1426
+ };
1427
+ for (const fieldName in fields) {
1428
+ const field = fields[fieldName];
1429
+ if (field.type === "array") {
1430
+ const arrayField = field;
1431
+ const arrayFields = arrayField.arrayFields;
1432
+ updatedProps[fieldName] = updatedProps[fieldName].map(
1433
+ (item, index) => {
1434
+ const newItem = {};
1435
+ const defaultValue = typeof arrayField.defaultItemProps === "function" ? arrayField.defaultItemProps(index) : arrayField.defaultItemProps;
1436
+ for (const arrayFieldName in arrayFields) {
1437
+ const subField = arrayFields[arrayFieldName];
1438
+ if (subField.type === "slot") {
1439
+ newItem[arrayFieldName] = item[arrayFieldName] ?? oldProps[fieldName]?.[index]?.[arrayFieldName] ?? defaultValue?.[arrayFieldName];
1440
+ } else {
1441
+ newItem[arrayFieldName] = item[arrayFieldName] ?? defaultValue?.[arrayFieldName];
1442
+ }
1443
+ }
1444
+ return newItem;
1445
+ }
1446
+ );
1447
+ }
1448
+ }
1449
+ return updatedProps;
1450
+ };
1422
1451
  var dispatchOp = (operation, {
1423
1452
  getState,
1424
1453
  dispatchAction,
@@ -1442,17 +1471,19 @@ var dispatchOp = (operation, {
1442
1471
  `Tried to update an item that doesn't exist: ${operation.id}`
1443
1472
  );
1444
1473
  }
1474
+ const newData = {
1475
+ ...existing,
1476
+ props: applyArrayDefaults(
1477
+ existing.props,
1478
+ operation.props,
1479
+ config.components[existing.type].fields
1480
+ )
1481
+ };
1445
1482
  dispatchAction({
1446
1483
  type: "replace",
1447
1484
  destinationIndex: operation.index,
1448
1485
  destinationZone: operation.zone,
1449
- data: {
1450
- ...existing,
1451
- props: {
1452
- ...existing.props,
1453
- ...operation.props
1454
- }
1455
- },
1486
+ data: newData,
1456
1487
  recordHistory: false
1457
1488
  });
1458
1489
  }
@@ -1464,17 +1495,19 @@ var dispatchOp = (operation, {
1464
1495
  `Tried to update an item that doesn't exist: ${operation.id}`
1465
1496
  );
1466
1497
  }
1498
+ const newData = {
1499
+ ...existing,
1500
+ props: applyArrayDefaults(
1501
+ existing.props,
1502
+ operation.props,
1503
+ config.components[existing.type].fields
1504
+ )
1505
+ };
1467
1506
  dispatchAction({
1468
1507
  type: "replace",
1469
1508
  destinationIndex: selector.index,
1470
1509
  destinationZone: selector.zone,
1471
- data: {
1472
- ...existing,
1473
- props: {
1474
- ...existing.props,
1475
- ...operation.props
1476
- }
1477
- },
1510
+ data: newData,
1478
1511
  recordHistory: false
1479
1512
  });
1480
1513
  } else if (operation.op === "updateRoot") {
package/dist/index.mjs CHANGED
@@ -1421,6 +1421,35 @@ var getSelectorForId = (state, id) => {
1421
1421
  return { zone: zoneCompound, index };
1422
1422
  };
1423
1423
  var getItemById = (state, id) => state.indexes.nodes[id].data;
1424
+ var applyArrayDefaults = (oldProps, newProps, fields) => {
1425
+ const updatedProps = {
1426
+ ...oldProps,
1427
+ ...newProps
1428
+ };
1429
+ for (const fieldName in fields) {
1430
+ const field = fields[fieldName];
1431
+ if (field.type === "array") {
1432
+ const arrayField = field;
1433
+ const arrayFields = arrayField.arrayFields;
1434
+ updatedProps[fieldName] = updatedProps[fieldName].map(
1435
+ (item, index) => {
1436
+ const newItem = {};
1437
+ const defaultValue = typeof arrayField.defaultItemProps === "function" ? arrayField.defaultItemProps(index) : arrayField.defaultItemProps;
1438
+ for (const arrayFieldName in arrayFields) {
1439
+ const subField = arrayFields[arrayFieldName];
1440
+ if (subField.type === "slot") {
1441
+ newItem[arrayFieldName] = item[arrayFieldName] ?? oldProps[fieldName]?.[index]?.[arrayFieldName] ?? defaultValue?.[arrayFieldName];
1442
+ } else {
1443
+ newItem[arrayFieldName] = item[arrayFieldName] ?? defaultValue?.[arrayFieldName];
1444
+ }
1445
+ }
1446
+ return newItem;
1447
+ }
1448
+ );
1449
+ }
1450
+ }
1451
+ return updatedProps;
1452
+ };
1424
1453
  var dispatchOp = (operation, {
1425
1454
  getState,
1426
1455
  dispatchAction,
@@ -1444,17 +1473,19 @@ var dispatchOp = (operation, {
1444
1473
  `Tried to update an item that doesn't exist: ${operation.id}`
1445
1474
  );
1446
1475
  }
1476
+ const newData = {
1477
+ ...existing,
1478
+ props: applyArrayDefaults(
1479
+ existing.props,
1480
+ operation.props,
1481
+ config.components[existing.type].fields
1482
+ )
1483
+ };
1447
1484
  dispatchAction({
1448
1485
  type: "replace",
1449
1486
  destinationIndex: operation.index,
1450
1487
  destinationZone: operation.zone,
1451
- data: {
1452
- ...existing,
1453
- props: {
1454
- ...existing.props,
1455
- ...operation.props
1456
- }
1457
- },
1488
+ data: newData,
1458
1489
  recordHistory: false
1459
1490
  });
1460
1491
  }
@@ -1466,17 +1497,19 @@ var dispatchOp = (operation, {
1466
1497
  `Tried to update an item that doesn't exist: ${operation.id}`
1467
1498
  );
1468
1499
  }
1500
+ const newData = {
1501
+ ...existing,
1502
+ props: applyArrayDefaults(
1503
+ existing.props,
1504
+ operation.props,
1505
+ config.components[existing.type].fields
1506
+ )
1507
+ };
1469
1508
  dispatchAction({
1470
1509
  type: "replace",
1471
1510
  destinationIndex: selector.index,
1472
1511
  destinationZone: selector.zone,
1473
- data: {
1474
- ...existing,
1475
- props: {
1476
- ...existing.props,
1477
- ...operation.props
1478
- }
1479
- },
1512
+ data: newData,
1480
1513
  recordHistory: false
1481
1514
  });
1482
1515
  } else if (operation.op === "updateRoot") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puckeditor/plugin-ai",
3
- "version": "0.5.0-canary.dd1cead3",
3
+ "version": "0.5.1-canary.2caa830b",
4
4
  "author": "Chris Villa <chris@puckeditor.com>",
5
5
  "repository": "puckeditor/puck",
6
6
  "bugs": "https://github.com/puckeditor/puck/issues",
@@ -54,7 +54,7 @@
54
54
  "typescript": "^5.5.4"
55
55
  },
56
56
  "peerDependencies": {
57
- "@puckeditor/core": "0.21.0-canary.de0baf39",
57
+ "@puckeditor/core": "^0.21.0 || 0.21.0-canary.de0baf39",
58
58
  "react": "^18.0.0 || ^19.0.0"
59
59
  }
60
60
  }