@dotcms/client 0.0.1-alpha.46 → 0.0.1-alpha.47

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/index.cjs.js CHANGED
@@ -1064,68 +1064,110 @@ const ErrorMessages = {
1064
1064
  503: 'Service Unavailable. Try again later.'
1065
1065
  };
1066
1066
 
1067
+ /**
1068
+ * Actions received from the dotcms editor
1069
+ *
1070
+ * @export
1071
+ * @enum {number}
1072
+ */
1073
+ exports.NOTIFY_CLIENT = void 0;
1074
+ (function (NOTIFY_CLIENT) {
1075
+ /**
1076
+ * Request to page to reload
1077
+ */
1078
+ NOTIFY_CLIENT["UVE_RELOAD_PAGE"] = "uve-reload-page";
1079
+ /**
1080
+ * Request the bounds for the elements
1081
+ */
1082
+ NOTIFY_CLIENT["UVE_REQUEST_BOUNDS"] = "uve-request-bounds";
1083
+ /**
1084
+ * Received pong from the editor
1085
+ */
1086
+ NOTIFY_CLIENT["UVE_EDITOR_PONG"] = "uve-editor-pong";
1087
+ /**
1088
+ * Received scroll event trigger from the editor
1089
+ */
1090
+ NOTIFY_CLIENT["UVE_SCROLL_INSIDE_IFRAME"] = "uve-scroll-inside-iframe";
1091
+ /**
1092
+ * Set the page data
1093
+ */
1094
+ NOTIFY_CLIENT["UVE_SET_PAGE_DATA"] = "uve-set-page-data";
1095
+ /**
1096
+ * Copy contentlet inline editing success
1097
+ */
1098
+ NOTIFY_CLIENT["UVE_COPY_CONTENTLET_INLINE_EDITING_SUCCESS"] = "uve-copy-contentlet-inline-editing-success";
1099
+ })(exports.NOTIFY_CLIENT || (exports.NOTIFY_CLIENT = {}));
1100
+
1101
+ const INITIAL_DOT_UVE = {
1102
+ editContentlet,
1103
+ lastScrollYPosition: 0
1104
+ };
1067
1105
  /**
1068
1106
  * Actions send to the dotcms editor
1069
1107
  *
1070
1108
  * @export
1071
1109
  * @enum {number}
1072
1110
  */
1073
- exports.CUSTOMER_ACTIONS = void 0;
1074
- (function (CUSTOMER_ACTIONS) {
1111
+ exports.CLIENT_ACTIONS = void 0;
1112
+ (function (CLIENT_ACTIONS) {
1075
1113
  /**
1076
1114
  * Tell the dotcms editor that page change
1077
1115
  */
1078
- CUSTOMER_ACTIONS["NAVIGATION_UPDATE"] = "set-url";
1116
+ CLIENT_ACTIONS["NAVIGATION_UPDATE"] = "set-url";
1079
1117
  /**
1080
1118
  * Send the element position of the rows, columnsm containers and contentlets
1081
1119
  */
1082
- CUSTOMER_ACTIONS["SET_BOUNDS"] = "set-bounds";
1120
+ CLIENT_ACTIONS["SET_BOUNDS"] = "set-bounds";
1083
1121
  /**
1084
1122
  * Send the information of the hovered contentlet
1085
1123
  */
1086
- CUSTOMER_ACTIONS["SET_CONTENTLET"] = "set-contentlet";
1124
+ CLIENT_ACTIONS["SET_CONTENTLET"] = "set-contentlet";
1087
1125
  /**
1088
1126
  * Tell the editor that the page is being scrolled
1089
1127
  */
1090
- CUSTOMER_ACTIONS["IFRAME_SCROLL"] = "scroll";
1128
+ CLIENT_ACTIONS["IFRAME_SCROLL"] = "scroll";
1091
1129
  /**
1092
1130
  * Tell the editor that the page has stopped scrolling
1093
1131
  */
1094
- CUSTOMER_ACTIONS["IFRAME_SCROLL_END"] = "scroll-end";
1132
+ CLIENT_ACTIONS["IFRAME_SCROLL_END"] = "scroll-end";
1095
1133
  /**
1096
1134
  * Ping the editor to see if the page is inside the editor
1097
1135
  */
1098
- CUSTOMER_ACTIONS["PING_EDITOR"] = "ping-editor";
1136
+ CLIENT_ACTIONS["PING_EDITOR"] = "ping-editor";
1099
1137
  /**
1100
1138
  * Tell the editor to init the inline editing editor.
1101
1139
  */
1102
- CUSTOMER_ACTIONS["INIT_INLINE_EDITING"] = "init-inline-editing";
1140
+ CLIENT_ACTIONS["INIT_INLINE_EDITING"] = "init-inline-editing";
1103
1141
  /**
1104
1142
  * Tell the editor to open the Copy-contentlet dialog
1105
1143
  * To copy a content and then edit it inline.
1106
1144
  */
1107
- CUSTOMER_ACTIONS["COPY_CONTENTLET_INLINE_EDITING"] = "copy-contentlet-inline-editing";
1145
+ CLIENT_ACTIONS["COPY_CONTENTLET_INLINE_EDITING"] = "copy-contentlet-inline-editing";
1108
1146
  /**
1109
1147
  * Tell the editor to save inline edited contentlet
1110
1148
  */
1111
- CUSTOMER_ACTIONS["UPDATE_CONTENTLET_INLINE_EDITING"] = "update-contentlet-inline-editing";
1149
+ CLIENT_ACTIONS["UPDATE_CONTENTLET_INLINE_EDITING"] = "update-contentlet-inline-editing";
1112
1150
  /**
1113
1151
  * Tell the editor to trigger a menu reorder
1114
1152
  */
1115
- CUSTOMER_ACTIONS["REORDER_MENU"] = "reorder-menu";
1153
+ CLIENT_ACTIONS["REORDER_MENU"] = "reorder-menu";
1116
1154
  /**
1117
1155
  * Tell the editor to send the page info to iframe
1118
1156
  */
1119
- CUSTOMER_ACTIONS["GET_PAGE_DATA"] = "get-page-data";
1157
+ CLIENT_ACTIONS["GET_PAGE_DATA"] = "get-page-data";
1120
1158
  /**
1121
1159
  * Tell the editor an user send a graphql query
1122
1160
  */
1123
- CUSTOMER_ACTIONS["CLIENT_READY"] = "client-ready";
1161
+ CLIENT_ACTIONS["CLIENT_READY"] = "client-ready";
1162
+ /**
1163
+ * Tell the editor to edit a contentlet
1164
+ */
1165
+ CLIENT_ACTIONS["EDIT_CONTENTLET"] = "edit-contentlet";
1124
1166
  /**
1125
1167
  * Tell the editor to do nothing
1126
1168
  */
1127
- CUSTOMER_ACTIONS["NOOP"] = "noop";
1128
- })(exports.CUSTOMER_ACTIONS || (exports.CUSTOMER_ACTIONS = {}));
1169
+ CLIENT_ACTIONS["NOOP"] = "noop";
1170
+ })(exports.CLIENT_ACTIONS || (exports.CLIENT_ACTIONS = {}));
1129
1171
  /**
1130
1172
  * Post message to dotcms page editor
1131
1173
  *
@@ -1137,32 +1179,6 @@ function postMessageToEditor(message) {
1137
1179
  window.parent.postMessage(message, '*');
1138
1180
  }
1139
1181
 
1140
- /**
1141
- * Actions received from the dotcms editor
1142
- *
1143
- * @export
1144
- * @enum {number}
1145
- */
1146
- var NOTIFY_CUSTOMER;
1147
- (function (NOTIFY_CUSTOMER) {
1148
- /**
1149
- * Request to page to reload
1150
- */
1151
- NOTIFY_CUSTOMER["EMA_RELOAD_PAGE"] = "ema-reload-page";
1152
- /**
1153
- * Request the bounds for the elements
1154
- */
1155
- NOTIFY_CUSTOMER["EMA_REQUEST_BOUNDS"] = "ema-request-bounds";
1156
- /**
1157
- * Received pong from the editor
1158
- */
1159
- NOTIFY_CUSTOMER["EMA_EDITOR_PONG"] = "ema-editor-pong";
1160
- /**
1161
- * Received scroll event trigger from the editor
1162
- */
1163
- NOTIFY_CUSTOMER["EMA_SCROLL_INSIDE_IFRAME"] = "scroll-inside-iframe";
1164
- })(NOTIFY_CUSTOMER || (NOTIFY_CUSTOMER = {}));
1165
-
1166
1182
  /**
1167
1183
  * Calculates the bounding information for each page element within the given containers.
1168
1184
  *
@@ -1356,7 +1372,7 @@ function setBounds() {
1356
1372
  const containers = Array.from(document.querySelectorAll('[data-dot-object="container"]'));
1357
1373
  const positionData = getPageElementBound(containers);
1358
1374
  postMessageToEditor({
1359
- action: exports.CUSTOMER_ACTIONS.SET_BOUNDS,
1375
+ action: exports.CLIENT_ACTIONS.SET_BOUNDS,
1360
1376
  payload: positionData
1361
1377
  });
1362
1378
  }
@@ -1368,23 +1384,26 @@ function setBounds() {
1368
1384
  */
1369
1385
  function listenEditorMessages() {
1370
1386
  const messageCallback = (event) => {
1371
- switch (event.data) {
1372
- case NOTIFY_CUSTOMER.EMA_REQUEST_BOUNDS: {
1387
+ const ACTIONS_NOTIFICATION = {
1388
+ [exports.NOTIFY_CLIENT.UVE_RELOAD_PAGE]: () => {
1389
+ window.location.reload();
1390
+ },
1391
+ [exports.NOTIFY_CLIENT.UVE_REQUEST_BOUNDS]: () => {
1373
1392
  setBounds();
1374
- break;
1375
- }
1376
- }
1377
- if (event.data.name === NOTIFY_CUSTOMER.EMA_SCROLL_INSIDE_IFRAME) {
1378
- const direction = event.data.direction;
1379
- if ((window.scrollY === 0 && direction === 'up') ||
1380
- (scrollIsInBottom() && direction === 'down')) {
1381
- // If the iframe scroll is at the top or bottom, do not send anything.
1382
- // This avoids losing the scrollend event.
1383
- return;
1393
+ },
1394
+ [exports.NOTIFY_CLIENT.UVE_SCROLL_INSIDE_IFRAME]: () => {
1395
+ const direction = event.data.direction;
1396
+ if ((window.scrollY === 0 && direction === 'up') ||
1397
+ (scrollIsInBottom() && direction === 'down')) {
1398
+ // If the iframe scroll is at the top or bottom, do not send anything.
1399
+ // This avoids losing the scrollend event.
1400
+ return;
1401
+ }
1402
+ const scrollY = direction === 'up' ? -120 : 120;
1403
+ window.scrollBy({ left: 0, top: scrollY, behavior: 'smooth' });
1384
1404
  }
1385
- const scrollY = direction === 'up' ? -120 : 120;
1386
- window.scrollBy({ left: 0, top: scrollY, behavior: 'smooth' });
1387
- }
1405
+ };
1406
+ ACTIONS_NOTIFICATION[event.data.name]?.();
1388
1407
  };
1389
1408
  window.addEventListener('message', messageCallback);
1390
1409
  subscriptions.push({
@@ -1436,7 +1455,7 @@ function listenHoveredContentlet() {
1436
1455
  vtlFiles
1437
1456
  };
1438
1457
  postMessageToEditor({
1439
- action: exports.CUSTOMER_ACTIONS.SET_CONTENTLET,
1458
+ action: exports.CLIENT_ACTIONS.SET_CONTENTLET,
1440
1459
  payload: {
1441
1460
  x,
1442
1461
  y,
@@ -1463,13 +1482,17 @@ function listenHoveredContentlet() {
1463
1482
  function scrollHandler() {
1464
1483
  const scrollCallback = () => {
1465
1484
  postMessageToEditor({
1466
- action: exports.CUSTOMER_ACTIONS.IFRAME_SCROLL
1485
+ action: exports.CLIENT_ACTIONS.IFRAME_SCROLL
1467
1486
  });
1468
- window.lastScrollYPosition = window.scrollY;
1487
+ // In case it doesn't have a dotUVE object, we create it with the initial values.
1488
+ window.dotUVE = {
1489
+ ...(window.dotUVE ?? INITIAL_DOT_UVE),
1490
+ lastScrollYPosition: window.scrollY
1491
+ };
1469
1492
  };
1470
1493
  const scrollEndCallback = () => {
1471
1494
  postMessageToEditor({
1472
- action: exports.CUSTOMER_ACTIONS.IFRAME_SCROLL_END
1495
+ action: exports.CLIENT_ACTIONS.IFRAME_SCROLL_END
1473
1496
  });
1474
1497
  };
1475
1498
  window.addEventListener('scroll', scrollCallback);
@@ -1493,7 +1516,7 @@ function scrollHandler() {
1493
1516
  */
1494
1517
  function fetchPageDataFromInsideUVE(pathname) {
1495
1518
  postMessageToEditor({
1496
- action: exports.CUSTOMER_ACTIONS.GET_PAGE_DATA,
1519
+ action: exports.CLIENT_ACTIONS.GET_PAGE_DATA,
1497
1520
  payload: {
1498
1521
  pathname
1499
1522
  }
@@ -1510,12 +1533,27 @@ function fetchPageDataFromInsideUVE(pathname) {
1510
1533
  */
1511
1534
  function updateNavigation(pathname) {
1512
1535
  postMessageToEditor({
1513
- action: exports.CUSTOMER_ACTIONS.NAVIGATION_UPDATE,
1536
+ action: exports.CLIENT_ACTIONS.NAVIGATION_UPDATE,
1514
1537
  payload: {
1515
1538
  url: pathname === '/' ? 'index' : pathname?.replace('/', '')
1516
1539
  }
1517
1540
  });
1518
1541
  }
1542
+ /**
1543
+ * You can use this function to edit a contentlet in the editor.
1544
+ *
1545
+ * Calling this function inside the editor, will prompt the UVE to open a dialog to edit the contentlet.
1546
+ *
1547
+ * @export
1548
+ * @template T
1549
+ * @param {Contentlet<T>} contentlet - The contentlet to edit.
1550
+ */
1551
+ function editContentlet(contentlet) {
1552
+ postMessageToEditor({
1553
+ action: exports.CLIENT_ACTIONS.EDIT_CONTENTLET,
1554
+ payload: contentlet
1555
+ });
1556
+ }
1519
1557
  /**
1520
1558
  * Checks if the code is running inside an editor.
1521
1559
  *
@@ -1535,6 +1573,9 @@ function isInsideEditor() {
1535
1573
  }
1536
1574
  return window.parent !== window;
1537
1575
  }
1576
+ function initDotUVE() {
1577
+ window.dotUVE = INITIAL_DOT_UVE;
1578
+ }
1538
1579
  /**
1539
1580
  * Initializes the DotCMS page editor.
1540
1581
  *
@@ -1546,6 +1587,7 @@ function isInsideEditor() {
1546
1587
  * ```
1547
1588
  */
1548
1589
  function initEditor(config) {
1590
+ initDotUVE();
1549
1591
  fetchPageDataFromInsideUVE(config.pathname);
1550
1592
  listenEditorMessages();
1551
1593
  listenHoveredContentlet();
@@ -1704,7 +1746,7 @@ class DotCmsClient {
1704
1746
  }
1705
1747
  if (action === 'changes') {
1706
1748
  const messageCallback = (event) => {
1707
- if (event.data.name === 'SET_PAGE_DATA') {
1749
+ if (event.data.name === exports.NOTIFY_CLIENT.UVE_SET_PAGE_DATA) {
1708
1750
  callbackFn(event.data.payload);
1709
1751
  }
1710
1752
  };
@@ -1945,6 +1987,7 @@ const getPageRequestParams = ({ path = '', params = {} }) => {
1945
1987
 
1946
1988
  exports.DotCmsClient = DotCmsClient;
1947
1989
  exports.destroyEditor = destroyEditor;
1990
+ exports.editContentlet = editContentlet;
1948
1991
  exports.getPageRequestParams = getPageRequestParams;
1949
1992
  exports.graphqlToPageEntity = graphqlToPageEntity;
1950
1993
  exports.initEditor = initEditor;
package/index.esm.js CHANGED
@@ -1062,68 +1062,110 @@ const ErrorMessages = {
1062
1062
  503: 'Service Unavailable. Try again later.'
1063
1063
  };
1064
1064
 
1065
+ /**
1066
+ * Actions received from the dotcms editor
1067
+ *
1068
+ * @export
1069
+ * @enum {number}
1070
+ */
1071
+ var NOTIFY_CLIENT;
1072
+ (function (NOTIFY_CLIENT) {
1073
+ /**
1074
+ * Request to page to reload
1075
+ */
1076
+ NOTIFY_CLIENT["UVE_RELOAD_PAGE"] = "uve-reload-page";
1077
+ /**
1078
+ * Request the bounds for the elements
1079
+ */
1080
+ NOTIFY_CLIENT["UVE_REQUEST_BOUNDS"] = "uve-request-bounds";
1081
+ /**
1082
+ * Received pong from the editor
1083
+ */
1084
+ NOTIFY_CLIENT["UVE_EDITOR_PONG"] = "uve-editor-pong";
1085
+ /**
1086
+ * Received scroll event trigger from the editor
1087
+ */
1088
+ NOTIFY_CLIENT["UVE_SCROLL_INSIDE_IFRAME"] = "uve-scroll-inside-iframe";
1089
+ /**
1090
+ * Set the page data
1091
+ */
1092
+ NOTIFY_CLIENT["UVE_SET_PAGE_DATA"] = "uve-set-page-data";
1093
+ /**
1094
+ * Copy contentlet inline editing success
1095
+ */
1096
+ NOTIFY_CLIENT["UVE_COPY_CONTENTLET_INLINE_EDITING_SUCCESS"] = "uve-copy-contentlet-inline-editing-success";
1097
+ })(NOTIFY_CLIENT || (NOTIFY_CLIENT = {}));
1098
+
1099
+ const INITIAL_DOT_UVE = {
1100
+ editContentlet,
1101
+ lastScrollYPosition: 0
1102
+ };
1065
1103
  /**
1066
1104
  * Actions send to the dotcms editor
1067
1105
  *
1068
1106
  * @export
1069
1107
  * @enum {number}
1070
1108
  */
1071
- var CUSTOMER_ACTIONS;
1072
- (function (CUSTOMER_ACTIONS) {
1109
+ var CLIENT_ACTIONS;
1110
+ (function (CLIENT_ACTIONS) {
1073
1111
  /**
1074
1112
  * Tell the dotcms editor that page change
1075
1113
  */
1076
- CUSTOMER_ACTIONS["NAVIGATION_UPDATE"] = "set-url";
1114
+ CLIENT_ACTIONS["NAVIGATION_UPDATE"] = "set-url";
1077
1115
  /**
1078
1116
  * Send the element position of the rows, columnsm containers and contentlets
1079
1117
  */
1080
- CUSTOMER_ACTIONS["SET_BOUNDS"] = "set-bounds";
1118
+ CLIENT_ACTIONS["SET_BOUNDS"] = "set-bounds";
1081
1119
  /**
1082
1120
  * Send the information of the hovered contentlet
1083
1121
  */
1084
- CUSTOMER_ACTIONS["SET_CONTENTLET"] = "set-contentlet";
1122
+ CLIENT_ACTIONS["SET_CONTENTLET"] = "set-contentlet";
1085
1123
  /**
1086
1124
  * Tell the editor that the page is being scrolled
1087
1125
  */
1088
- CUSTOMER_ACTIONS["IFRAME_SCROLL"] = "scroll";
1126
+ CLIENT_ACTIONS["IFRAME_SCROLL"] = "scroll";
1089
1127
  /**
1090
1128
  * Tell the editor that the page has stopped scrolling
1091
1129
  */
1092
- CUSTOMER_ACTIONS["IFRAME_SCROLL_END"] = "scroll-end";
1130
+ CLIENT_ACTIONS["IFRAME_SCROLL_END"] = "scroll-end";
1093
1131
  /**
1094
1132
  * Ping the editor to see if the page is inside the editor
1095
1133
  */
1096
- CUSTOMER_ACTIONS["PING_EDITOR"] = "ping-editor";
1134
+ CLIENT_ACTIONS["PING_EDITOR"] = "ping-editor";
1097
1135
  /**
1098
1136
  * Tell the editor to init the inline editing editor.
1099
1137
  */
1100
- CUSTOMER_ACTIONS["INIT_INLINE_EDITING"] = "init-inline-editing";
1138
+ CLIENT_ACTIONS["INIT_INLINE_EDITING"] = "init-inline-editing";
1101
1139
  /**
1102
1140
  * Tell the editor to open the Copy-contentlet dialog
1103
1141
  * To copy a content and then edit it inline.
1104
1142
  */
1105
- CUSTOMER_ACTIONS["COPY_CONTENTLET_INLINE_EDITING"] = "copy-contentlet-inline-editing";
1143
+ CLIENT_ACTIONS["COPY_CONTENTLET_INLINE_EDITING"] = "copy-contentlet-inline-editing";
1106
1144
  /**
1107
1145
  * Tell the editor to save inline edited contentlet
1108
1146
  */
1109
- CUSTOMER_ACTIONS["UPDATE_CONTENTLET_INLINE_EDITING"] = "update-contentlet-inline-editing";
1147
+ CLIENT_ACTIONS["UPDATE_CONTENTLET_INLINE_EDITING"] = "update-contentlet-inline-editing";
1110
1148
  /**
1111
1149
  * Tell the editor to trigger a menu reorder
1112
1150
  */
1113
- CUSTOMER_ACTIONS["REORDER_MENU"] = "reorder-menu";
1151
+ CLIENT_ACTIONS["REORDER_MENU"] = "reorder-menu";
1114
1152
  /**
1115
1153
  * Tell the editor to send the page info to iframe
1116
1154
  */
1117
- CUSTOMER_ACTIONS["GET_PAGE_DATA"] = "get-page-data";
1155
+ CLIENT_ACTIONS["GET_PAGE_DATA"] = "get-page-data";
1118
1156
  /**
1119
1157
  * Tell the editor an user send a graphql query
1120
1158
  */
1121
- CUSTOMER_ACTIONS["CLIENT_READY"] = "client-ready";
1159
+ CLIENT_ACTIONS["CLIENT_READY"] = "client-ready";
1160
+ /**
1161
+ * Tell the editor to edit a contentlet
1162
+ */
1163
+ CLIENT_ACTIONS["EDIT_CONTENTLET"] = "edit-contentlet";
1122
1164
  /**
1123
1165
  * Tell the editor to do nothing
1124
1166
  */
1125
- CUSTOMER_ACTIONS["NOOP"] = "noop";
1126
- })(CUSTOMER_ACTIONS || (CUSTOMER_ACTIONS = {}));
1167
+ CLIENT_ACTIONS["NOOP"] = "noop";
1168
+ })(CLIENT_ACTIONS || (CLIENT_ACTIONS = {}));
1127
1169
  /**
1128
1170
  * Post message to dotcms page editor
1129
1171
  *
@@ -1135,32 +1177,6 @@ function postMessageToEditor(message) {
1135
1177
  window.parent.postMessage(message, '*');
1136
1178
  }
1137
1179
 
1138
- /**
1139
- * Actions received from the dotcms editor
1140
- *
1141
- * @export
1142
- * @enum {number}
1143
- */
1144
- var NOTIFY_CUSTOMER;
1145
- (function (NOTIFY_CUSTOMER) {
1146
- /**
1147
- * Request to page to reload
1148
- */
1149
- NOTIFY_CUSTOMER["EMA_RELOAD_PAGE"] = "ema-reload-page";
1150
- /**
1151
- * Request the bounds for the elements
1152
- */
1153
- NOTIFY_CUSTOMER["EMA_REQUEST_BOUNDS"] = "ema-request-bounds";
1154
- /**
1155
- * Received pong from the editor
1156
- */
1157
- NOTIFY_CUSTOMER["EMA_EDITOR_PONG"] = "ema-editor-pong";
1158
- /**
1159
- * Received scroll event trigger from the editor
1160
- */
1161
- NOTIFY_CUSTOMER["EMA_SCROLL_INSIDE_IFRAME"] = "scroll-inside-iframe";
1162
- })(NOTIFY_CUSTOMER || (NOTIFY_CUSTOMER = {}));
1163
-
1164
1180
  /**
1165
1181
  * Calculates the bounding information for each page element within the given containers.
1166
1182
  *
@@ -1354,7 +1370,7 @@ function setBounds() {
1354
1370
  const containers = Array.from(document.querySelectorAll('[data-dot-object="container"]'));
1355
1371
  const positionData = getPageElementBound(containers);
1356
1372
  postMessageToEditor({
1357
- action: CUSTOMER_ACTIONS.SET_BOUNDS,
1373
+ action: CLIENT_ACTIONS.SET_BOUNDS,
1358
1374
  payload: positionData
1359
1375
  });
1360
1376
  }
@@ -1366,23 +1382,26 @@ function setBounds() {
1366
1382
  */
1367
1383
  function listenEditorMessages() {
1368
1384
  const messageCallback = (event) => {
1369
- switch (event.data) {
1370
- case NOTIFY_CUSTOMER.EMA_REQUEST_BOUNDS: {
1385
+ const ACTIONS_NOTIFICATION = {
1386
+ [NOTIFY_CLIENT.UVE_RELOAD_PAGE]: () => {
1387
+ window.location.reload();
1388
+ },
1389
+ [NOTIFY_CLIENT.UVE_REQUEST_BOUNDS]: () => {
1371
1390
  setBounds();
1372
- break;
1373
- }
1374
- }
1375
- if (event.data.name === NOTIFY_CUSTOMER.EMA_SCROLL_INSIDE_IFRAME) {
1376
- const direction = event.data.direction;
1377
- if ((window.scrollY === 0 && direction === 'up') ||
1378
- (scrollIsInBottom() && direction === 'down')) {
1379
- // If the iframe scroll is at the top or bottom, do not send anything.
1380
- // This avoids losing the scrollend event.
1381
- return;
1391
+ },
1392
+ [NOTIFY_CLIENT.UVE_SCROLL_INSIDE_IFRAME]: () => {
1393
+ const direction = event.data.direction;
1394
+ if ((window.scrollY === 0 && direction === 'up') ||
1395
+ (scrollIsInBottom() && direction === 'down')) {
1396
+ // If the iframe scroll is at the top or bottom, do not send anything.
1397
+ // This avoids losing the scrollend event.
1398
+ return;
1399
+ }
1400
+ const scrollY = direction === 'up' ? -120 : 120;
1401
+ window.scrollBy({ left: 0, top: scrollY, behavior: 'smooth' });
1382
1402
  }
1383
- const scrollY = direction === 'up' ? -120 : 120;
1384
- window.scrollBy({ left: 0, top: scrollY, behavior: 'smooth' });
1385
- }
1403
+ };
1404
+ ACTIONS_NOTIFICATION[event.data.name]?.();
1386
1405
  };
1387
1406
  window.addEventListener('message', messageCallback);
1388
1407
  subscriptions.push({
@@ -1434,7 +1453,7 @@ function listenHoveredContentlet() {
1434
1453
  vtlFiles
1435
1454
  };
1436
1455
  postMessageToEditor({
1437
- action: CUSTOMER_ACTIONS.SET_CONTENTLET,
1456
+ action: CLIENT_ACTIONS.SET_CONTENTLET,
1438
1457
  payload: {
1439
1458
  x,
1440
1459
  y,
@@ -1461,13 +1480,17 @@ function listenHoveredContentlet() {
1461
1480
  function scrollHandler() {
1462
1481
  const scrollCallback = () => {
1463
1482
  postMessageToEditor({
1464
- action: CUSTOMER_ACTIONS.IFRAME_SCROLL
1483
+ action: CLIENT_ACTIONS.IFRAME_SCROLL
1465
1484
  });
1466
- window.lastScrollYPosition = window.scrollY;
1485
+ // In case it doesn't have a dotUVE object, we create it with the initial values.
1486
+ window.dotUVE = {
1487
+ ...(window.dotUVE ?? INITIAL_DOT_UVE),
1488
+ lastScrollYPosition: window.scrollY
1489
+ };
1467
1490
  };
1468
1491
  const scrollEndCallback = () => {
1469
1492
  postMessageToEditor({
1470
- action: CUSTOMER_ACTIONS.IFRAME_SCROLL_END
1493
+ action: CLIENT_ACTIONS.IFRAME_SCROLL_END
1471
1494
  });
1472
1495
  };
1473
1496
  window.addEventListener('scroll', scrollCallback);
@@ -1491,7 +1514,7 @@ function scrollHandler() {
1491
1514
  */
1492
1515
  function fetchPageDataFromInsideUVE(pathname) {
1493
1516
  postMessageToEditor({
1494
- action: CUSTOMER_ACTIONS.GET_PAGE_DATA,
1517
+ action: CLIENT_ACTIONS.GET_PAGE_DATA,
1495
1518
  payload: {
1496
1519
  pathname
1497
1520
  }
@@ -1508,12 +1531,27 @@ function fetchPageDataFromInsideUVE(pathname) {
1508
1531
  */
1509
1532
  function updateNavigation(pathname) {
1510
1533
  postMessageToEditor({
1511
- action: CUSTOMER_ACTIONS.NAVIGATION_UPDATE,
1534
+ action: CLIENT_ACTIONS.NAVIGATION_UPDATE,
1512
1535
  payload: {
1513
1536
  url: pathname === '/' ? 'index' : pathname?.replace('/', '')
1514
1537
  }
1515
1538
  });
1516
1539
  }
1540
+ /**
1541
+ * You can use this function to edit a contentlet in the editor.
1542
+ *
1543
+ * Calling this function inside the editor, will prompt the UVE to open a dialog to edit the contentlet.
1544
+ *
1545
+ * @export
1546
+ * @template T
1547
+ * @param {Contentlet<T>} contentlet - The contentlet to edit.
1548
+ */
1549
+ function editContentlet(contentlet) {
1550
+ postMessageToEditor({
1551
+ action: CLIENT_ACTIONS.EDIT_CONTENTLET,
1552
+ payload: contentlet
1553
+ });
1554
+ }
1517
1555
  /**
1518
1556
  * Checks if the code is running inside an editor.
1519
1557
  *
@@ -1533,6 +1571,9 @@ function isInsideEditor() {
1533
1571
  }
1534
1572
  return window.parent !== window;
1535
1573
  }
1574
+ function initDotUVE() {
1575
+ window.dotUVE = INITIAL_DOT_UVE;
1576
+ }
1536
1577
  /**
1537
1578
  * Initializes the DotCMS page editor.
1538
1579
  *
@@ -1544,6 +1585,7 @@ function isInsideEditor() {
1544
1585
  * ```
1545
1586
  */
1546
1587
  function initEditor(config) {
1588
+ initDotUVE();
1547
1589
  fetchPageDataFromInsideUVE(config.pathname);
1548
1590
  listenEditorMessages();
1549
1591
  listenHoveredContentlet();
@@ -1702,7 +1744,7 @@ class DotCmsClient {
1702
1744
  }
1703
1745
  if (action === 'changes') {
1704
1746
  const messageCallback = (event) => {
1705
- if (event.data.name === 'SET_PAGE_DATA') {
1747
+ if (event.data.name === NOTIFY_CLIENT.UVE_SET_PAGE_DATA) {
1706
1748
  callbackFn(event.data.payload);
1707
1749
  }
1708
1750
  };
@@ -1941,4 +1983,4 @@ const getPageRequestParams = ({ path = '', params = {} }) => {
1941
1983
  };
1942
1984
  };
1943
1985
 
1944
- export { CUSTOMER_ACTIONS, DotCmsClient, destroyEditor, getPageRequestParams, graphqlToPageEntity, initEditor, isInsideEditor, postMessageToEditor, updateNavigation };
1986
+ export { CLIENT_ACTIONS, DotCmsClient, NOTIFY_CLIENT, destroyEditor, editContentlet, getPageRequestParams, graphqlToPageEntity, initEditor, isInsideEditor, postMessageToEditor, updateNavigation };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcms/client",
3
- "version": "0.0.1-alpha.46",
3
+ "version": "0.0.1-alpha.47",
4
4
  "description": "Official JavaScript library for interacting with DotCMS REST APIs.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ClientConfig, DotCmsClient } from './lib/client/sdk-js-client';
2
- import { CUSTOMER_ACTIONS, postMessageToEditor } from './lib/editor/models/client.model';
2
+ import { CLIENT_ACTIONS, postMessageToEditor } from './lib/editor/models/client.model';
3
3
  import { CustomClientParams, DotCMSPageEditorConfig, EditorConfig } from './lib/editor/models/editor.model';
4
- import { destroyEditor, initEditor, isInsideEditor, updateNavigation } from './lib/editor/sdk-editor';
4
+ import { NOTIFY_CLIENT } from './lib/editor/models/listeners.model';
5
+ import { destroyEditor, editContentlet, initEditor, isInsideEditor, updateNavigation } from './lib/editor/sdk-editor';
5
6
  import { getPageRequestParams, graphqlToPageEntity } from './lib/utils';
6
- export { graphqlToPageEntity, getPageRequestParams, isInsideEditor, DotCmsClient, DotCMSPageEditorConfig, CUSTOMER_ACTIONS, CustomClientParams, postMessageToEditor, EditorConfig, initEditor, updateNavigation, destroyEditor, ClientConfig };
7
+ export { graphqlToPageEntity, getPageRequestParams, isInsideEditor, editContentlet, DotCmsClient, DotCMSPageEditorConfig, CLIENT_ACTIONS, NOTIFY_CLIENT, CustomClientParams, postMessageToEditor, EditorConfig, initEditor, updateNavigation, destroyEditor, ClientConfig };
@@ -1,9 +1,4 @@
1
1
  import { DotCMSPageEditorSubscription } from '../models/listeners.model';
2
- declare global {
3
- interface Window {
4
- lastScrollYPosition: number;
5
- }
6
- }
7
2
  /**
8
3
  * Represents an array of DotCMSPageEditorSubscription objects.
9
4
  * Used to store the subscriptions for the editor and unsubscribe later.
@@ -1,10 +1,17 @@
1
+ import { editContentlet } from '../sdk-editor';
2
+ declare global {
3
+ interface Window {
4
+ dotUVE: DotUVE;
5
+ }
6
+ }
7
+ export declare const INITIAL_DOT_UVE: DotUVE;
1
8
  /**
2
9
  * Actions send to the dotcms editor
3
10
  *
4
11
  * @export
5
12
  * @enum {number}
6
13
  */
7
- export declare enum CUSTOMER_ACTIONS {
14
+ export declare enum CLIENT_ACTIONS {
8
15
  /**
9
16
  * Tell the dotcms editor that page change
10
17
  */
@@ -54,6 +61,10 @@ export declare enum CUSTOMER_ACTIONS {
54
61
  * Tell the editor an user send a graphql query
55
62
  */
56
63
  CLIENT_READY = "client-ready",
64
+ /**
65
+ * Tell the editor to edit a contentlet
66
+ */
67
+ EDIT_CONTENTLET = "edit-contentlet",
57
68
  /**
58
69
  * Tell the editor to do nothing
59
70
  */
@@ -67,7 +78,7 @@ export declare enum CUSTOMER_ACTIONS {
67
78
  * @interface PostMessageProps
68
79
  */
69
80
  type PostMessageProps<T> = {
70
- action: CUSTOMER_ACTIONS;
81
+ action: CLIENT_ACTIONS;
71
82
  payload?: T;
72
83
  };
73
84
  /**
@@ -78,4 +89,8 @@ type PostMessageProps<T> = {
78
89
  * @param {PostMessageProps<T>} message
79
90
  */
80
91
  export declare function postMessageToEditor<T = unknown>(message: PostMessageProps<T>): void;
92
+ export interface DotUVE {
93
+ editContentlet: typeof editContentlet;
94
+ lastScrollYPosition: number;
95
+ }
81
96
  export {};
@@ -4,23 +4,31 @@
4
4
  * @export
5
5
  * @enum {number}
6
6
  */
7
- export declare enum NOTIFY_CUSTOMER {
7
+ export declare enum NOTIFY_CLIENT {
8
8
  /**
9
9
  * Request to page to reload
10
10
  */
11
- EMA_RELOAD_PAGE = "ema-reload-page",
11
+ UVE_RELOAD_PAGE = "uve-reload-page",
12
12
  /**
13
13
  * Request the bounds for the elements
14
14
  */
15
- EMA_REQUEST_BOUNDS = "ema-request-bounds",
15
+ UVE_REQUEST_BOUNDS = "uve-request-bounds",
16
16
  /**
17
17
  * Received pong from the editor
18
18
  */
19
- EMA_EDITOR_PONG = "ema-editor-pong",
19
+ UVE_EDITOR_PONG = "uve-editor-pong",
20
20
  /**
21
21
  * Received scroll event trigger from the editor
22
22
  */
23
- EMA_SCROLL_INSIDE_IFRAME = "scroll-inside-iframe"
23
+ UVE_SCROLL_INSIDE_IFRAME = "uve-scroll-inside-iframe",
24
+ /**
25
+ * Set the page data
26
+ */
27
+ UVE_SET_PAGE_DATA = "uve-set-page-data",
28
+ /**
29
+ * Copy contentlet inline editing success
30
+ */
31
+ UVE_COPY_CONTENTLET_INLINE_EDITING_SUCCESS = "uve-copy-contentlet-inline-editing-success"
24
32
  }
25
33
  type ListenerCallbackMessage = (event: MessageEvent) => void;
26
34
  type ListenerCallbackPointer = (event: PointerEvent) => void;
@@ -1,6 +1 @@
1
- declare global {
2
- interface Window {
3
- lastScrollYPosition: number;
4
- }
5
- }
6
1
  export {};
@@ -1,4 +1,5 @@
1
1
  import { DotCMSPageEditorConfig } from './models/editor.model';
2
+ import { Contentlet } from '../client/content/shared/types';
2
3
  /**
3
4
  * Updates the navigation in the editor.
4
5
  *
@@ -8,6 +9,16 @@ import { DotCMSPageEditorConfig } from './models/editor.model';
8
9
  * updateNavigation('/home'); // Sends a message to the editor to update the navigation to '/home'
9
10
  */
10
11
  export declare function updateNavigation(pathname: string): void;
12
+ /**
13
+ * You can use this function to edit a contentlet in the editor.
14
+ *
15
+ * Calling this function inside the editor, will prompt the UVE to open a dialog to edit the contentlet.
16
+ *
17
+ * @export
18
+ * @template T
19
+ * @param {Contentlet<T>} contentlet - The contentlet to edit.
20
+ */
21
+ export declare function editContentlet<T>(contentlet: Contentlet<T>): void;
11
22
  /**
12
23
  * Checks if the code is running inside an editor.
13
24
  *
@@ -22,6 +33,7 @@ export declare function updateNavigation(pathname: string): void;
22
33
  * ```
23
34
  */
24
35
  export declare function isInsideEditor(): boolean;
36
+ export declare function initDotUVE(): void;
25
37
  /**
26
38
  * Initializes the DotCMS page editor.
27
39
  *