@uniformdev/canvas 19.55.1-alpha.8 → 19.55.2-alpha.14

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.mjs CHANGED
@@ -922,13 +922,92 @@ function isPromise(obj) {
922
922
  return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
923
923
  }
924
924
 
925
- // src/enhancement/walkComponentTree.ts
926
- function walkComponentTree(component, visitor, initialContext) {
927
- var _a;
925
+ // src/enhancement/getComponentPath.ts
926
+ function getComponentPath(ancestorsAndSelf) {
927
+ const path = [];
928
+ for (let i = ancestorsAndSelf.length - 1; i >= 0; i--) {
929
+ const currentLocation = ancestorsAndSelf[i];
930
+ const parentLocation = ancestorsAndSelf[i + 1];
931
+ if ("type" in currentLocation && currentLocation.type !== "slot") {
932
+ if (currentLocation.type === "block") {
933
+ const { fieldName, blockIndex } = currentLocation;
934
+ if (fieldName && blockIndex !== void 0) {
935
+ const noun = parentLocation && "type" in parentLocation && parentLocation.type === "block" ? "fields" : "parameters";
936
+ path.push(`${noun}.${fieldName}.value[${blockIndex}]`);
937
+ }
938
+ } else {
939
+ }
940
+ continue;
941
+ }
942
+ const { parentSlot, parentSlotIndex } = currentLocation;
943
+ if (parentSlot && parentSlotIndex !== void 0) {
944
+ path.push(`${parentSlot}[${parentSlotIndex}]`);
945
+ }
946
+ }
947
+ return `.${path.join(".")}`;
948
+ }
949
+
950
+ // src/utils/constants.ts
951
+ var CANVAS_PERSONALIZE_TYPE = "$personalization";
952
+ var CANVAS_TEST_TYPE = "$test";
953
+ var CANVAS_LOCALIZATION_TYPE = "$localization";
954
+ var CANVAS_INTENT_TAG_PARAM = "intentTag";
955
+ var CANVAS_LOCALE_TAG_PARAM = "locale";
956
+ var CANVAS_BLOCK_PARAM_TYPE = "$block";
957
+ var CANVAS_PERSONALIZE_SLOT = "pz";
958
+ var CANVAS_TEST_SLOT = "test";
959
+ var CANVAS_LOCALIZATION_SLOT = "localized";
960
+ var CANVAS_DRAFT_STATE = 0;
961
+ var CANVAS_PUBLISHED_STATE = 64;
962
+ var CANVAS_EDITOR_STATE = 63;
963
+ var CANVAS_PERSONALIZATION_PARAM = "$pzCrit";
964
+ var CANVAS_TEST_VARIANT_PARAM = "$tstVrnt";
965
+ var CANVAS_ENRICHMENT_TAG_PARAM = "$enr";
966
+ var SECRET_QUERY_STRING_PARAM = "secret";
967
+ var IN_CONTEXT_EDITOR_QUERY_STRING_PARAM = "is_incontext_editing_mode";
968
+ var IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM = "is_incontext_editing_playground";
969
+ var IN_CONTEXT_EDITOR_CONFIG_CHECK_QUERY_STRING_PARAM = "is_config_check";
970
+ var IN_CONTEXT_EDITOR_COMPONENT_START_ROLE = "uniform-component-start";
971
+ var IN_CONTEXT_EDITOR_COMPONENT_END_ROLE = "uniform-component-end";
972
+ var IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID = "uniform-canvas-preview-script";
973
+ var IS_RENDERED_BY_UNIFORM_ATTRIBUTE = "data-is-rendered-by-uniform";
974
+ var PLACEHOLDER_ID = "placeholder";
975
+ var EMPTY_COMPOSITION = {
976
+ _id: "_empty_composition_id",
977
+ _name: "An empty composition used for contextual editing",
978
+ type: "_empty_composition_type"
979
+ };
980
+ var EDGE_MIN_CACHE_TTL = 10;
981
+ var EDGE_MAX_CACHE_TTL = 24 * 60 * 60;
982
+ var EDGE_DEFAULT_CACHE_TTL = 30;
983
+ var EDGE_CACHE_DISABLED = -1;
984
+
985
+ // src/utils/entryConverter.ts
986
+ function convertEntryToPutEntry(entry) {
987
+ return {
988
+ entry: {
989
+ type: entry.entry.type,
990
+ _dataResources: entry.entry._dataResources,
991
+ _id: entry.entry._id,
992
+ _name: entry.entry._name,
993
+ _slug: entry.entry._slug,
994
+ fields: entry.entry.fields
995
+ },
996
+ state: entry.state,
997
+ projectId: entry.projectId
998
+ };
999
+ }
1000
+ function getPropertiesValue(entity) {
1001
+ return "parameters" in entity && entity.parameters ? entity.parameters : "fields" in entity && entity.fields ? entity.fields : void 0;
1002
+ }
1003
+
1004
+ // src/enhancement/walkNodeTree.ts
1005
+ function walkNodeTree(node, visitor, options) {
1006
+ var _a, _b;
928
1007
  const componentQueue = [
929
1008
  {
930
- ancestorsAndSelf: [{ component, parentSlot: void 0, parentSlotIndex: void 0 }],
931
- context: initialContext
1009
+ ancestorsAndSelf: Array.isArray(node) ? node : [{ node, type: "root" }],
1010
+ context: options == null ? void 0 : options.initialContext
932
1011
  }
933
1012
  ];
934
1013
  const childContexts = /* @__PURE__ */ new Map();
@@ -938,81 +1017,182 @@ function walkComponentTree(component, visitor, initialContext) {
938
1017
  continue;
939
1018
  const currentComponent = currentQueueEntry.ancestorsAndSelf[0];
940
1019
  let visitDescendants = true;
941
- let descendantContext = (_a = childContexts.get(currentComponent.component)) != null ? _a : currentQueueEntry.context;
942
- visitor(
943
- currentComponent.component,
944
- currentQueueEntry.ancestorsAndSelf,
945
- {
946
- replaceComponent: (replacementComponent) => {
947
- Object.assign(currentComponent.component, replacementComponent);
948
- const propertiesToCheck = [
949
- "parameters",
950
- "variant",
951
- "slots",
952
- "data",
953
- "_pattern",
954
- "_patternError"
955
- ];
956
- propertiesToCheck.forEach((property) => {
957
- if (!replacementComponent[property]) {
958
- delete currentComponent.component[property];
1020
+ let descendantContext = (_a = childContexts.get(currentComponent.node)) != null ? _a : currentQueueEntry.context;
1021
+ let visitorInfo;
1022
+ if (currentComponent.type === "root" || currentComponent.type === "slot") {
1023
+ visitorInfo = {
1024
+ type: "component",
1025
+ node: currentComponent.node,
1026
+ ancestorsAndSelf: currentQueueEntry.ancestorsAndSelf,
1027
+ actions: {
1028
+ replace: (replacementComponent) => {
1029
+ Object.assign(currentComponent.node, replacementComponent);
1030
+ const propertiesToCheck = [
1031
+ "parameters",
1032
+ "variant",
1033
+ "slots",
1034
+ "data",
1035
+ "_pattern",
1036
+ "_patternError",
1037
+ "_dataResources",
1038
+ "_overridability",
1039
+ "_overrides",
1040
+ "_patternDataResources"
1041
+ ];
1042
+ propertiesToCheck.forEach((property) => {
1043
+ if (!replacementComponent[property]) {
1044
+ delete currentComponent.node[property];
1045
+ }
1046
+ });
1047
+ },
1048
+ remove: () => {
1049
+ const currentComponentLocation = currentQueueEntry.ancestorsAndSelf[0];
1050
+ const parentComponent = currentQueueEntry.ancestorsAndSelf[1];
1051
+ if (currentComponentLocation.type === "root") {
1052
+ throw new Error("Unable to delete root node.");
959
1053
  }
960
- });
961
- },
962
- removeComponent: () => {
963
- const { parentSlot, parentSlotIndex } = currentQueueEntry.ancestorsAndSelf[0];
964
- const parentComponent = currentQueueEntry.ancestorsAndSelf[1];
965
- if (parentSlot && typeof parentSlotIndex !== "undefined") {
966
- parentComponent.component.slots[parentSlot].splice(parentSlotIndex, 1);
967
- } else {
968
- throw new Error("Unable to delete composition.");
1054
+ if (currentComponentLocation.type === "slot") {
1055
+ const { parentSlot, parentSlotIndex } = currentComponentLocation;
1056
+ parentComponent.node.slots[parentSlot].splice(parentSlotIndex, 1);
1057
+ } else {
1058
+ throw new Error("Unknown node type");
1059
+ }
1060
+ },
1061
+ insertAfter: (nodes) => {
1062
+ const nodesToInsert = Array.isArray(nodes) ? nodes : [nodes];
1063
+ const currentNodeInfo = currentQueueEntry.ancestorsAndSelf[0];
1064
+ if (currentNodeInfo.type === "root") {
1065
+ throw new Error("Unable to insert after root node.");
1066
+ }
1067
+ if (currentNodeInfo.type === "slot") {
1068
+ const { parentSlot, parentSlotIndex } = currentNodeInfo;
1069
+ const parentComponent = currentQueueEntry.ancestorsAndSelf[1];
1070
+ if (parentSlot && typeof parentSlotIndex !== "undefined") {
1071
+ parentComponent.node.slots[parentSlot].splice(
1072
+ parentSlotIndex + 1,
1073
+ 0,
1074
+ ...nodesToInsert
1075
+ );
1076
+ componentQueue.unshift(
1077
+ ...nodesToInsert.map((enqueueingComponent) => ({
1078
+ type: "slot",
1079
+ ancestorsAndSelf: [
1080
+ {
1081
+ type: "slot",
1082
+ node: enqueueingComponent,
1083
+ parentSlot,
1084
+ get parentSlotIndex() {
1085
+ return parentComponent.node.slots[parentSlot].findIndex(
1086
+ (x) => x === enqueueingComponent
1087
+ );
1088
+ }
1089
+ },
1090
+ // slice removes 'self' since we are inserting a peer of self
1091
+ ...currentQueueEntry.ancestorsAndSelf.slice(1)
1092
+ ],
1093
+ context: descendantContext
1094
+ }))
1095
+ );
1096
+ }
1097
+ } else {
1098
+ throw new Error("Unknown type");
1099
+ }
1100
+ },
1101
+ stopProcessingDescendants() {
1102
+ visitDescendants = false;
1103
+ },
1104
+ setDescendantsContext(context) {
1105
+ descendantContext = context;
1106
+ },
1107
+ setChildContext(child, context) {
1108
+ childContexts.set(child, context);
969
1109
  }
970
1110
  },
971
- insertAfter: (components) => {
972
- const componentsToInsert = Array.isArray(components) ? components : [components];
973
- const { parentSlot, parentSlotIndex } = currentQueueEntry.ancestorsAndSelf[0];
974
- const parentComponent = currentQueueEntry.ancestorsAndSelf[1];
975
- if (parentSlot && typeof parentSlotIndex !== "undefined") {
976
- parentComponent.component.slots[parentSlot].splice(
977
- parentSlotIndex + 1,
978
- 0,
979
- ...componentsToInsert
980
- );
981
- componentQueue.unshift(
982
- ...componentsToInsert.map((enqueueingComponent) => ({
983
- ancestorsAndSelf: [
984
- {
985
- component: enqueueingComponent,
986
- parentSlot,
987
- get parentSlotIndex() {
988
- return parentComponent.component.slots[parentSlot].findIndex(
989
- (x) => x === enqueueingComponent
990
- );
991
- }
992
- },
993
- ...currentQueueEntry.ancestorsAndSelf
994
- ],
995
- context: descendantContext
996
- }))
997
- );
998
- } else {
999
- throw new Error("Unable to insert after a component not in a slot.");
1111
+ context: descendantContext
1112
+ };
1113
+ } else {
1114
+ visitorInfo = {
1115
+ type: "entry",
1116
+ node: currentComponent.node,
1117
+ ancestorsAndSelf: currentQueueEntry.ancestorsAndSelf,
1118
+ actions: {
1119
+ replace: (replacementNode) => {
1120
+ Object.assign(currentComponent.node, replacementNode);
1121
+ const propertiesToCheck = ["fields", "_dataResources", "_author"];
1122
+ propertiesToCheck.forEach((property) => {
1123
+ if (!replacementNode[property]) {
1124
+ delete currentComponent.node[property];
1125
+ }
1126
+ });
1127
+ },
1128
+ remove: () => {
1129
+ const currentComponentLocation = currentQueueEntry.ancestorsAndSelf[0];
1130
+ const parentComponent = currentQueueEntry.ancestorsAndSelf[1];
1131
+ if (currentComponentLocation.type === "block") {
1132
+ const { fieldName, blockIndex } = currentComponentLocation;
1133
+ const blockValue = getBlockValue(parentComponent.node, fieldName);
1134
+ blockValue.splice(blockIndex, 1);
1135
+ if (blockValue.length === 0) {
1136
+ const properties2 = getPropertiesValue(parentComponent.node);
1137
+ delete properties2[fieldName];
1138
+ }
1139
+ } else {
1140
+ throw new Error("Unknown node type");
1141
+ }
1142
+ },
1143
+ insertAfter: (nodes) => {
1144
+ const currentNodeInfo = currentQueueEntry.ancestorsAndSelf[0];
1145
+ if (currentNodeInfo.type !== "block") {
1146
+ throw new Error("Unknown type");
1147
+ }
1148
+ const { fieldName, blockIndex } = currentNodeInfo;
1149
+ const parentComponent = currentQueueEntry.ancestorsAndSelf[1];
1150
+ const nodesToInsert = Array.isArray(nodes) ? nodes : [nodes];
1151
+ if (fieldName && typeof blockIndex !== "undefined") {
1152
+ getPropertiesValue(parentComponent.node)[fieldName].value.splice(
1153
+ blockIndex + 1,
1154
+ 0,
1155
+ ...nodesToInsert
1156
+ );
1157
+ componentQueue.unshift(
1158
+ ...nodesToInsert.map((enqueueingComponent) => ({
1159
+ ancestorsAndSelf: [
1160
+ {
1161
+ type: "block",
1162
+ node: enqueueingComponent,
1163
+ fieldName,
1164
+ get blockIndex() {
1165
+ const parentArray = getPropertiesValue(parentComponent.node)[fieldName].value;
1166
+ return parentArray.findIndex((x) => x === enqueueingComponent);
1167
+ }
1168
+ },
1169
+ // slice removes 'self' since we are inserting a peer of self
1170
+ ...currentQueueEntry.ancestorsAndSelf.slice(1)
1171
+ ],
1172
+ context: descendantContext
1173
+ }))
1174
+ );
1175
+ }
1176
+ },
1177
+ stopProcessingDescendants() {
1178
+ visitDescendants = false;
1179
+ },
1180
+ setDescendantsContext(context) {
1181
+ descendantContext = context;
1182
+ },
1183
+ setChildContext(child, context) {
1184
+ childContexts.set(child, context);
1000
1185
  }
1001
1186
  },
1002
- stopProcessingDescendants() {
1003
- visitDescendants = false;
1004
- },
1005
- setDescendantsContext(context) {
1006
- descendantContext = context;
1007
- },
1008
- setChildContext(child, context) {
1009
- childContexts.set(child, context);
1010
- }
1011
- },
1012
- descendantContext
1013
- );
1014
- const slots = currentComponent.component.slots;
1015
- if (visitDescendants && slots) {
1187
+ context: descendantContext
1188
+ };
1189
+ }
1190
+ visitor(visitorInfo);
1191
+ if (!visitDescendants) {
1192
+ continue;
1193
+ }
1194
+ const slots = "slots" in currentComponent.node && currentComponent.node.slots;
1195
+ if (slots) {
1016
1196
  const slotKeys = Object.keys(slots);
1017
1197
  for (let slotIndex = slotKeys.length - 1; slotIndex >= 0; slotIndex--) {
1018
1198
  const slotKey = slotKeys[slotIndex];
@@ -1022,10 +1202,11 @@ function walkComponentTree(component, visitor, initialContext) {
1022
1202
  componentQueue.push({
1023
1203
  ancestorsAndSelf: [
1024
1204
  {
1025
- component: enqueueingComponent,
1205
+ type: "slot",
1206
+ node: enqueueingComponent,
1026
1207
  parentSlot: slotKey,
1027
1208
  get parentSlotIndex() {
1028
- return currentComponent.component.slots[slotKey].findIndex(
1209
+ return currentComponent.node.slots[slotKey].findIndex(
1029
1210
  (x) => x === enqueueingComponent
1030
1211
  );
1031
1212
  }
@@ -1037,27 +1218,44 @@ function walkComponentTree(component, visitor, initialContext) {
1037
1218
  }
1038
1219
  }
1039
1220
  }
1040
- } while (componentQueue.length > 0);
1041
- }
1042
- function getComponentPath(ancestorsAndSelf) {
1043
- const path = [];
1044
- for (let i = ancestorsAndSelf.length - 1; i >= 0; i--) {
1045
- const { parentSlot, parentSlotIndex } = ancestorsAndSelf[i];
1046
- if (parentSlot && parentSlotIndex !== void 0) {
1047
- path.push(`${parentSlot}[${parentSlotIndex}]`);
1221
+ const properties = getPropertiesValue(currentComponent.node);
1222
+ if (properties) {
1223
+ const propertyEntries = Object.entries(properties);
1224
+ for (let propIndex = propertyEntries.length - 1; propIndex >= 0; propIndex--) {
1225
+ const [propKey, propObject] = propertyEntries[propIndex];
1226
+ if (propObject.type !== CANVAS_BLOCK_PARAM_TYPE)
1227
+ continue;
1228
+ const blocks = (_b = propObject.value) != null ? _b : [];
1229
+ for (let blockIndex = blocks.length - 1; blockIndex >= 0; blockIndex--) {
1230
+ const enqueueingBlock = blocks[blockIndex];
1231
+ componentQueue.push({
1232
+ ancestorsAndSelf: [
1233
+ {
1234
+ type: "block",
1235
+ node: enqueueingBlock,
1236
+ fieldName: propKey,
1237
+ get blockIndex() {
1238
+ return getBlockValue(currentComponent.node, propKey).findIndex(
1239
+ (x) => x === enqueueingBlock
1240
+ );
1241
+ }
1242
+ },
1243
+ ...currentQueueEntry.ancestorsAndSelf
1244
+ ],
1245
+ context: descendantContext
1246
+ });
1247
+ }
1248
+ }
1048
1249
  }
1049
- }
1050
- return `.${path.join(".")}`;
1250
+ } while (componentQueue.length > 0);
1051
1251
  }
1052
- function getComponentJsonPointer(ancestorsAndSelf, { withSlots = false } = {}) {
1053
- const path = [];
1054
- for (let i = ancestorsAndSelf.length - 1; i >= 0; i--) {
1055
- const { parentSlot, parentSlotIndex } = ancestorsAndSelf[i];
1056
- if (parentSlot && parentSlotIndex !== void 0) {
1057
- path.push(`${parentSlot}/${parentSlotIndex}`);
1058
- }
1252
+ function getBlockValue(component, parameterName) {
1253
+ var _a;
1254
+ const parameter = (_a = getPropertiesValue(component)) == null ? void 0 : _a[parameterName];
1255
+ if ((parameter == null ? void 0 : parameter.value) && parameter.type === CANVAS_BLOCK_PARAM_TYPE && Array.isArray(parameter.value)) {
1256
+ return parameter.value;
1059
1257
  }
1060
- return withSlots ? `/slots/${path.join("/slots/")}` : `/${path.join("/")}`;
1258
+ return [];
1061
1259
  }
1062
1260
 
1063
1261
  // src/enhancement/enhance.ts
@@ -1077,19 +1275,21 @@ async function enhance({
1077
1275
  const promises = [];
1078
1276
  const usedComponentEnhancers = /* @__PURE__ */ new Set();
1079
1277
  const usedParameterEnhancers = /* @__PURE__ */ new Set();
1080
- walkComponentTree(composition, (currentComponent, componentContext) => {
1278
+ walkNodeTree(composition, ({ type, node, ancestorsAndSelf, actions }) => {
1081
1279
  var _a;
1082
- Object.entries((_a = currentComponent.parameters) != null ? _a : {}).forEach(([paramName, paramValue]) => {
1083
- const enhancer = enhancers.resolveParameterEnhancer(currentComponent, paramName, paramValue);
1280
+ if (type !== "component") {
1281
+ actions.stopProcessingDescendants();
1282
+ return;
1283
+ }
1284
+ Object.entries((_a = node.parameters) != null ? _a : {}).forEach(([paramName, paramValue]) => {
1285
+ const enhancer = enhancers.resolveParameterEnhancer(node, paramName, paramValue);
1084
1286
  if (enhancer) {
1085
1287
  usedParameterEnhancers.add(enhancer);
1086
- promises.push(
1087
- enhanceParameter(currentComponent, componentContext, paramName, paramValue, enhancer, context)
1088
- );
1288
+ promises.push(enhanceParameter(node, ancestorsAndSelf, paramName, paramValue, enhancer, context));
1089
1289
  }
1090
1290
  });
1091
- const componentEnhancers = enhancers.resolveComponentEnhancers(currentComponent);
1092
- promises.push(enhanceComponent(currentComponent, componentContext, componentEnhancers, context));
1291
+ const componentEnhancers = enhancers.resolveComponentEnhancers(node);
1292
+ promises.push(enhanceComponent(node, ancestorsAndSelf, componentEnhancers, context));
1093
1293
  usedComponentEnhancers.add(componentEnhancers);
1094
1294
  });
1095
1295
  promises.push(
@@ -1307,41 +1507,30 @@ var EnhancerBuilder = class {
1307
1507
  }
1308
1508
  };
1309
1509
 
1310
- // src/utils/constants.ts
1311
- var CANVAS_PERSONALIZE_TYPE = "$personalization";
1312
- var CANVAS_TEST_TYPE = "$test";
1313
- var CANVAS_LOCALIZATION_TYPE = "$localization";
1314
- var CANVAS_INTENT_TAG_PARAM = "intentTag";
1315
- var CANVAS_LOCALE_TAG_PARAM = "locale";
1316
- var CANVAS_PERSONALIZE_SLOT = "pz";
1317
- var CANVAS_TEST_SLOT = "test";
1318
- var CANVAS_LOCALIZATION_SLOT = "localized";
1319
- var CANVAS_DRAFT_STATE = 0;
1320
- var CANVAS_PUBLISHED_STATE = 64;
1321
- var CANVAS_EDITOR_STATE = 63;
1322
- var CANVAS_PERSONALIZATION_PARAM = "$pzCrit";
1323
- var CANVAS_TEST_VARIANT_PARAM = "$tstVrnt";
1324
- var CANVAS_ENRICHMENT_TAG_PARAM = "$enr";
1325
- var IN_CONTEXT_EDITOR_QUERY_STRING_PARAM = "is_incontext_editing_mode";
1326
- var IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM = "is_incontext_editing_playground";
1327
- var IN_CONTEXT_EDITOR_CONFIG_CHECK_QUERY_STRING_PARAM = "is_config_check";
1328
- var IN_CONTEXT_EDITOR_COMPONENT_START_ROLE = "uniform-component-start";
1329
- var IN_CONTEXT_EDITOR_COMPONENT_END_ROLE = "uniform-component-end";
1330
- var IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID = "uniform-canvas-preview-script";
1331
- var IS_RENDERED_BY_UNIFORM_ATTRIBUTE = "data-is-rendered-by-uniform";
1332
- var PLACEHOLDER_ID = "placeholder";
1333
- var EMPTY_COMPOSITION = {
1334
- _id: "_empty_composition_id",
1335
- _name: "An empty composition used for contextual editing",
1336
- type: "_empty_composition_type"
1337
- };
1338
- var EDGE_MIN_CACHE_TTL = 15;
1339
- var EDGE_MAX_CACHE_TTL = 600;
1340
- var EDGE_DEFAULT_CACHE_TTL = 30;
1341
- var EDGE_CACHE_DISABLED = -1;
1342
- var EDGE_MIN_L2_CACHE_TTL_IN_HOURS = 1;
1343
- var EDGE_MAX_L2_CACHE_TTL_IN_HOURS = 4 * 7 * 24;
1344
- var EDGE_DEFAULT_L2_CACHE_TTL_IN_HOURS = 24;
1510
+ // src/enhancement/getComponentJsonPointer.ts
1511
+ function getComponentJsonPointer(ancestorsAndSelf) {
1512
+ const path = [];
1513
+ for (let i = ancestorsAndSelf.length - 1; i >= 0; i--) {
1514
+ const currentLocation = ancestorsAndSelf[i];
1515
+ const parentLocation = ancestorsAndSelf[i + 1];
1516
+ if ("type" in currentLocation && currentLocation.type !== "slot") {
1517
+ if (currentLocation.type === "block") {
1518
+ const { fieldName: parameterName, blockIndex } = currentLocation;
1519
+ if (parameterName && blockIndex !== void 0) {
1520
+ const noun = parentLocation && "type" in parentLocation && parentLocation.type === "block" ? "fields" : "parameters";
1521
+ path.push(`${noun}/${parameterName}/value/${blockIndex}`);
1522
+ }
1523
+ } else {
1524
+ }
1525
+ continue;
1526
+ }
1527
+ const { parentSlot, parentSlotIndex } = currentLocation;
1528
+ if (parentSlot && parentSlotIndex !== void 0) {
1529
+ path.push(`slots/${parentSlot}/${parentSlotIndex}`);
1530
+ }
1531
+ }
1532
+ return `/${path.join("/")}`;
1533
+ }
1345
1534
 
1346
1535
  // src/enhancement/localize.ts
1347
1536
  function extractLocales({ component }) {
@@ -1362,22 +1551,26 @@ function localize({
1362
1551
  composition,
1363
1552
  locale
1364
1553
  }) {
1365
- walkComponentTree(composition, (currentComponent, _componentContext, actions) => {
1366
- if (currentComponent.type === CANVAS_LOCALIZATION_TYPE) {
1367
- const locales = extractLocales({ component: currentComponent });
1368
- const resolvedLocale = typeof locale === "string" ? locale : locale({ component: currentComponent, locales });
1554
+ walkNodeTree(composition, ({ type, node, actions }) => {
1555
+ if (type !== "component") {
1556
+ actions.stopProcessingDescendants();
1557
+ return;
1558
+ }
1559
+ if (node.type === CANVAS_LOCALIZATION_TYPE) {
1560
+ const locales = extractLocales({ component: node });
1561
+ const resolvedLocale = typeof locale === "string" ? locale : locale({ component: node, locales });
1369
1562
  let replaceComponent;
1370
1563
  if (resolvedLocale) {
1371
1564
  replaceComponent = locales[resolvedLocale];
1372
1565
  }
1373
1566
  if (replaceComponent == null ? void 0 : replaceComponent.length) {
1374
1567
  const [first, ...rest] = replaceComponent;
1375
- actions.replaceComponent(first);
1568
+ actions.replace(first);
1376
1569
  if (rest.length) {
1377
1570
  actions.insertAfter(rest);
1378
1571
  }
1379
1572
  } else {
1380
- actions.removeComponent();
1573
+ actions.remove();
1381
1574
  }
1382
1575
  }
1383
1576
  });
@@ -1410,6 +1603,124 @@ var UniqueBatchEntries = class {
1410
1603
  }
1411
1604
  };
1412
1605
 
1606
+ // src/enhancement/walkComponentTree.ts
1607
+ function walkComponentTree(component, visitor, initialContext) {
1608
+ var _a;
1609
+ const componentQueue = [
1610
+ {
1611
+ ancestorsAndSelf: [{ component, parentSlot: void 0, parentSlotIndex: void 0 }],
1612
+ context: initialContext
1613
+ }
1614
+ ];
1615
+ const childContexts = /* @__PURE__ */ new Map();
1616
+ do {
1617
+ const currentQueueEntry = componentQueue.pop();
1618
+ if (!currentQueueEntry)
1619
+ continue;
1620
+ const currentComponent = currentQueueEntry.ancestorsAndSelf[0];
1621
+ let visitDescendants = true;
1622
+ let descendantContext = (_a = childContexts.get(currentComponent.component)) != null ? _a : currentQueueEntry.context;
1623
+ visitor(
1624
+ currentComponent.component,
1625
+ currentQueueEntry.ancestorsAndSelf,
1626
+ {
1627
+ replaceComponent: (replacementComponent) => {
1628
+ Object.assign(currentComponent.component, replacementComponent);
1629
+ const propertiesToCheck = [
1630
+ "parameters",
1631
+ "variant",
1632
+ "slots",
1633
+ "data",
1634
+ "_pattern",
1635
+ "_patternError"
1636
+ ];
1637
+ propertiesToCheck.forEach((property) => {
1638
+ if (!replacementComponent[property]) {
1639
+ delete currentComponent.component[property];
1640
+ }
1641
+ });
1642
+ },
1643
+ removeComponent: () => {
1644
+ const { parentSlot, parentSlotIndex } = currentQueueEntry.ancestorsAndSelf[0];
1645
+ const parentComponent = currentQueueEntry.ancestorsAndSelf[1];
1646
+ if (parentSlot && typeof parentSlotIndex !== "undefined") {
1647
+ parentComponent.component.slots[parentSlot].splice(parentSlotIndex, 1);
1648
+ } else {
1649
+ throw new Error("Unable to delete composition.");
1650
+ }
1651
+ },
1652
+ insertAfter: (components) => {
1653
+ const componentsToInsert = Array.isArray(components) ? components : [components];
1654
+ const { parentSlot, parentSlotIndex } = currentQueueEntry.ancestorsAndSelf[0];
1655
+ const parentComponent = currentQueueEntry.ancestorsAndSelf[1];
1656
+ if (parentSlot && typeof parentSlotIndex !== "undefined") {
1657
+ parentComponent.component.slots[parentSlot].splice(
1658
+ parentSlotIndex + 1,
1659
+ 0,
1660
+ ...componentsToInsert
1661
+ );
1662
+ componentQueue.unshift(
1663
+ ...componentsToInsert.map((enqueueingComponent) => ({
1664
+ ancestorsAndSelf: [
1665
+ {
1666
+ component: enqueueingComponent,
1667
+ parentSlot,
1668
+ get parentSlotIndex() {
1669
+ return parentComponent.component.slots[parentSlot].findIndex(
1670
+ (x) => x === enqueueingComponent
1671
+ );
1672
+ }
1673
+ },
1674
+ ...currentQueueEntry.ancestorsAndSelf
1675
+ ],
1676
+ context: descendantContext
1677
+ }))
1678
+ );
1679
+ } else {
1680
+ throw new Error("Unable to insert after a component not in a slot.");
1681
+ }
1682
+ },
1683
+ stopProcessingDescendants() {
1684
+ visitDescendants = false;
1685
+ },
1686
+ setDescendantsContext(context) {
1687
+ descendantContext = context;
1688
+ },
1689
+ setChildContext(child, context) {
1690
+ childContexts.set(child, context);
1691
+ }
1692
+ },
1693
+ descendantContext
1694
+ );
1695
+ const slots = currentComponent.component.slots;
1696
+ if (visitDescendants && slots) {
1697
+ const slotKeys = Object.keys(slots);
1698
+ for (let slotIndex = slotKeys.length - 1; slotIndex >= 0; slotIndex--) {
1699
+ const slotKey = slotKeys[slotIndex];
1700
+ const components = slots[slotKey];
1701
+ for (let componentIndex = components.length - 1; componentIndex >= 0; componentIndex--) {
1702
+ const enqueueingComponent = components[componentIndex];
1703
+ componentQueue.push({
1704
+ ancestorsAndSelf: [
1705
+ {
1706
+ component: enqueueingComponent,
1707
+ parentSlot: slotKey,
1708
+ get parentSlotIndex() {
1709
+ return currentComponent.component.slots[slotKey].findIndex(
1710
+ (x) => x === enqueueingComponent
1711
+ );
1712
+ }
1713
+ },
1714
+ ...currentQueueEntry.ancestorsAndSelf
1715
+ ],
1716
+ context: descendantContext
1717
+ });
1718
+ }
1719
+ }
1720
+ }
1721
+ } while (componentQueue.length > 0);
1722
+ }
1723
+
1413
1724
  // src/utils/hash.ts
1414
1725
  var generateHash = ({
1415
1726
  composition,
@@ -1435,6 +1746,9 @@ var isSelectComponentMessage = (message) => {
1435
1746
  var isReadyMessage = (message) => {
1436
1747
  return message.type === "ready";
1437
1748
  };
1749
+ var isComponentActionMessage = (message) => {
1750
+ return message.type === "trigger-component-action";
1751
+ };
1438
1752
  var isUpdateCompositionMessage = (message) => {
1439
1753
  return message.type === "update-composition";
1440
1754
  };
@@ -1459,6 +1773,9 @@ var isTriggerCompositionActionMessage = (message) => {
1459
1773
  var isUpdatePreviewSettingsMessage = (message) => {
1460
1774
  return message.type === "update-preview-settings";
1461
1775
  };
1776
+ var isUpdateFeatureFlagsMessage = (message) => {
1777
+ return message.type === "update-feature-flags";
1778
+ };
1462
1779
  var isUpdateContextualEditingStateInternalMessage = (message) => {
1463
1780
  return message.type === "update-contextual-editing-state-internal";
1464
1781
  };
@@ -1468,6 +1785,12 @@ var isReportRenderedCompositionsMessage = (message) => {
1468
1785
  var isSelectParameterMessage = (message) => {
1469
1786
  return message.type === "select-parameter";
1470
1787
  };
1788
+ var isOpenParameterEditorMessage = (message) => {
1789
+ return message.type === "open-parameter-editor";
1790
+ };
1791
+ var isUpdateComponentReferencesMessage = (message) => {
1792
+ return message.type === "update-component-references";
1793
+ };
1471
1794
  var createCanvasChannel = ({
1472
1795
  listenTo,
1473
1796
  broadcastTo
@@ -1528,6 +1851,13 @@ var createCanvasChannel = ({
1528
1851
  };
1529
1852
  postMessage(message);
1530
1853
  };
1854
+ const triggerComponentAction = (options) => {
1855
+ const message = {
1856
+ ...options,
1857
+ type: "trigger-component-action"
1858
+ };
1859
+ postMessage(message);
1860
+ };
1531
1861
  const addComponent = (options) => {
1532
1862
  const message = {
1533
1863
  ...options,
@@ -1591,12 +1921,33 @@ var createCanvasChannel = ({
1591
1921
  };
1592
1922
  postMessage(message);
1593
1923
  };
1924
+ const openParameterEditor = (options) => {
1925
+ const message = {
1926
+ ...options,
1927
+ type: "open-parameter-editor"
1928
+ };
1929
+ postMessage(message);
1930
+ };
1594
1931
  const editorStateUpdated = () => {
1595
1932
  const message = {
1596
1933
  type: "editor-state-updated"
1597
1934
  };
1598
1935
  postMessage(message);
1599
1936
  };
1937
+ const updateComponentReferences = (options) => {
1938
+ const message = {
1939
+ ...options,
1940
+ type: "update-component-references"
1941
+ };
1942
+ postMessage(message);
1943
+ };
1944
+ const updateFeatureFlags = (options) => {
1945
+ const message = {
1946
+ ...options,
1947
+ type: "update-feature-flags"
1948
+ };
1949
+ postMessage(message);
1950
+ };
1600
1951
  const messageEventListener = (event) => {
1601
1952
  if (typeof event.data !== "string") {
1602
1953
  return;
@@ -1626,6 +1977,7 @@ var createCanvasChannel = ({
1626
1977
  return {
1627
1978
  ready,
1628
1979
  destroy,
1980
+ triggerComponentAction,
1629
1981
  selectComponent,
1630
1982
  updateComposition,
1631
1983
  updateCompositionInternal,
@@ -1636,10 +1988,13 @@ var createCanvasChannel = ({
1636
1988
  dismissPlaceholder,
1637
1989
  triggerCompositionAction,
1638
1990
  updatePreviewSettings,
1991
+ updateFeatureFlags,
1639
1992
  updateContextualEditingStateInternal,
1640
1993
  selectParameter,
1994
+ openParameterEditor,
1641
1995
  reportRenderedCompositions,
1642
- editorStateUpdated
1996
+ editorStateUpdated,
1997
+ updateComponentReferences
1643
1998
  };
1644
1999
  };
1645
2000
 
@@ -1721,10 +2076,50 @@ function subscribeToComposition({
1721
2076
  };
1722
2077
  }
1723
2078
 
1724
- // src/RouteClient.ts
2079
+ // src/PromptClient.ts
1725
2080
  import { ApiClient as ApiClient7 } from "@uniformdev/context/api";
2081
+ var PromptUrl = "/api/v1/prompt";
2082
+ var PromptsUrl = "/api/v1/prompts";
2083
+ var PromptClient = class extends ApiClient7 {
2084
+ constructor(options) {
2085
+ super(options);
2086
+ }
2087
+ /** Fetches all Prompts for a project */
2088
+ async get(options) {
2089
+ const { projectId } = this.options;
2090
+ const fetchUri = this.createUrl(PromptUrl, { ...options, projectId });
2091
+ return await this.apiClient(fetchUri);
2092
+ }
2093
+ /** Fetches all Prompts for a project */
2094
+ async getList(options) {
2095
+ const { projectId } = this.options;
2096
+ const fetchUri = this.createUrl(PromptsUrl, { ...options, projectId });
2097
+ return await this.apiClient(fetchUri);
2098
+ }
2099
+ /** Updates or creates (based on id) a Prompt */
2100
+ async upsert(body) {
2101
+ const fetchUri = this.createUrl(PromptUrl);
2102
+ await this.apiClient(fetchUri, {
2103
+ method: "PUT",
2104
+ body: JSON.stringify({ ...body, projectId: this.options.projectId }),
2105
+ expectNoContent: true
2106
+ });
2107
+ }
2108
+ /** Deletes a Prompt */
2109
+ async remove(body) {
2110
+ const fetchUri = this.createUrl(PromptUrl);
2111
+ await this.apiClient(fetchUri, {
2112
+ method: "DELETE",
2113
+ body: JSON.stringify({ ...body, projectId: this.options.projectId }),
2114
+ expectNoContent: true
2115
+ });
2116
+ }
2117
+ };
2118
+
2119
+ // src/RouteClient.ts
2120
+ import { ApiClient as ApiClient8 } from "@uniformdev/context/api";
1726
2121
  var ROUTE_URL = "/api/v1/route";
1727
- var RouteClient = class extends ApiClient7 {
2122
+ var RouteClient = class extends ApiClient8 {
1728
2123
  constructor(options) {
1729
2124
  var _a;
1730
2125
  if (!options.limitPolicy) {
@@ -1763,20 +2158,6 @@ var createUniformApiEnhancer = ({ apiUrl }) => {
1763
2158
  };
1764
2159
  };
1765
2160
 
1766
- // src/utils/entryConverter.ts
1767
- function convertEntryToPutEntry(entry) {
1768
- return {
1769
- entry: {
1770
- type: entry.entry.type,
1771
- _dataResources: entry.entry._dataResources,
1772
- _id: entry.entry._id,
1773
- fields: entry.entry.fields
1774
- },
1775
- state: entry.state,
1776
- projectId: entry.projectId
1777
- };
1778
- }
1779
-
1780
2161
  // src/utils/getParameterAttributes.ts
1781
2162
  var ATTRIBUTE_COMPONENT_ID = "data-uniform-component-id";
1782
2163
  var ATTRIBUTE_PARAMETER_ID = "data-uniform-parameter-id";
@@ -1807,6 +2188,11 @@ var getParameterAttributes = ({
1807
2188
  };
1808
2189
  };
1809
2190
 
2191
+ // src/utils/isAllowedReferrer.ts
2192
+ var isAllowedReferrer = (referrer) => {
2193
+ return Boolean(referrer == null ? void 0 : referrer.match(/(^https:\/\/|\.)(uniform.app|uniform.wtf|localhost:\d{4})\//));
2194
+ };
2195
+
1810
2196
  // src/utils/isSystemComponentDefinition.ts
1811
2197
  var isSystemComponentDefinition = (componentType) => {
1812
2198
  return componentType.startsWith("$");
@@ -1851,7 +2237,10 @@ var isComponentPlaceholderId = (id) => {
1851
2237
  }
1852
2238
  return id == null ? void 0 : id.startsWith(PLACEHOLDER_ID);
1853
2239
  };
1854
- var generateComponentPlaceholderId = (randomId) => {
2240
+ var generateComponentPlaceholderId = (randomId, sdkVersion) => {
2241
+ if (typeof sdkVersion === "undefined" || sdkVersion === 1) {
2242
+ return PLACEHOLDER_ID;
2243
+ }
1855
2244
  return `${PLACEHOLDER_ID}_${randomId}`;
1856
2245
  };
1857
2246
 
@@ -1990,7 +2379,15 @@ function bindVariablesToObjectRecursive({
1990
2379
  boundCount += bindResult.boundCount;
1991
2380
  draft[property] = bindResult.result;
1992
2381
  if (bindResult.errors) {
1993
- errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
2382
+ errors.push(
2383
+ ...bindResult.errors.map((e) => {
2384
+ if (typeof e === "string") {
2385
+ return `${currentObjectPath}: ${e}`;
2386
+ }
2387
+ e.message = `${currentObjectPath}: ${e.message}`;
2388
+ return e;
2389
+ })
2390
+ );
1994
2391
  }
1995
2392
  }
1996
2393
  return;
@@ -2041,6 +2438,7 @@ export {
2041
2438
  ATTRIBUTE_PLACEHOLDER,
2042
2439
  ApiClientError2 as ApiClientError,
2043
2440
  BatchEntry,
2441
+ CANVAS_BLOCK_PARAM_TYPE,
2044
2442
  CANVAS_DRAFT_STATE,
2045
2443
  CANVAS_EDITOR_STATE,
2046
2444
  CANVAS_ENRICHMENT_TAG_PARAM,
@@ -2064,11 +2462,8 @@ export {
2064
2462
  DataTypeClient,
2065
2463
  EDGE_CACHE_DISABLED,
2066
2464
  EDGE_DEFAULT_CACHE_TTL,
2067
- EDGE_DEFAULT_L2_CACHE_TTL_IN_HOURS,
2068
2465
  EDGE_MAX_CACHE_TTL,
2069
- EDGE_MAX_L2_CACHE_TTL_IN_HOURS,
2070
2466
  EDGE_MIN_CACHE_TTL,
2071
- EDGE_MIN_L2_CACHE_TTL_IN_HOURS,
2072
2467
  EMPTY_COMPOSITION,
2073
2468
  EnhancerBuilder,
2074
2469
  IN_CONTEXT_EDITOR_COMPONENT_END_ROLE,
@@ -2079,7 +2474,9 @@ export {
2079
2474
  IN_CONTEXT_EDITOR_QUERY_STRING_PARAM,
2080
2475
  IS_RENDERED_BY_UNIFORM_ATTRIBUTE,
2081
2476
  PLACEHOLDER_ID,
2477
+ PromptClient,
2082
2478
  RouteClient,
2479
+ SECRET_QUERY_STRING_PARAM,
2083
2480
  UncachedCanvasClient,
2084
2481
  UncachedCategoryClient,
2085
2482
  UncachedContentClient,
@@ -2098,14 +2495,19 @@ export {
2098
2495
  extractLocales,
2099
2496
  generateComponentPlaceholderId,
2100
2497
  generateHash,
2498
+ getBlockValue,
2101
2499
  getChannelName,
2102
2500
  getComponentJsonPointer,
2103
2501
  getComponentPath,
2104
2502
  getParameterAttributes,
2503
+ getPropertiesValue,
2105
2504
  isAddComponentMessage,
2505
+ isAllowedReferrer,
2506
+ isComponentActionMessage,
2106
2507
  isComponentPlaceholderId,
2107
2508
  isDismissPlaceholderMessage,
2108
2509
  isMovingComponentMessage,
2510
+ isOpenParameterEditorMessage,
2109
2511
  isReadyMessage,
2110
2512
  isReportRenderedCompositionsMessage,
2111
2513
  isSelectComponentMessage,
@@ -2113,9 +2515,11 @@ export {
2113
2515
  isSystemComponentDefinition,
2114
2516
  isTriggerCompositionActionMessage,
2115
2517
  isUpdateComponentParameterMessage,
2518
+ isUpdateComponentReferencesMessage,
2116
2519
  isUpdateCompositionInternalMessage,
2117
2520
  isUpdateCompositionMessage,
2118
2521
  isUpdateContextualEditingStateInternalMessage,
2522
+ isUpdateFeatureFlagsMessage,
2119
2523
  isUpdatePreviewSettingsMessage,
2120
2524
  localize,
2121
2525
  mapSlotToPersonalizedVariations,
@@ -2124,5 +2528,6 @@ export {
2124
2528
  parseVariableExpression,
2125
2529
  subscribeToComposition,
2126
2530
  unstable_CompositionRelationshipClient,
2127
- walkComponentTree
2531
+ walkComponentTree,
2532
+ walkNodeTree
2128
2533
  };