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

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,114 @@ 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";
1166
+ /**
1167
+ * Tell the editor to open the block editor sidebar
1168
+ */
1169
+ CLIENT_ACTIONS["INIT_BLOCK_EDITOR_INLINE_EDITING"] = "init-editor-inline-editing";
1124
1170
  /**
1125
1171
  * Tell the editor to do nothing
1126
1172
  */
1127
- CUSTOMER_ACTIONS["NOOP"] = "noop";
1128
- })(exports.CUSTOMER_ACTIONS || (exports.CUSTOMER_ACTIONS = {}));
1173
+ CLIENT_ACTIONS["NOOP"] = "noop";
1174
+ })(exports.CLIENT_ACTIONS || (exports.CLIENT_ACTIONS = {}));
1129
1175
  /**
1130
1176
  * Post message to dotcms page editor
1131
1177
  *
@@ -1137,32 +1183,6 @@ function postMessageToEditor(message) {
1137
1183
  window.parent.postMessage(message, '*');
1138
1184
  }
1139
1185
 
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
1186
  /**
1167
1187
  * Calculates the bounding information for each page element within the given containers.
1168
1188
  *
@@ -1356,7 +1376,7 @@ function setBounds() {
1356
1376
  const containers = Array.from(document.querySelectorAll('[data-dot-object="container"]'));
1357
1377
  const positionData = getPageElementBound(containers);
1358
1378
  postMessageToEditor({
1359
- action: exports.CUSTOMER_ACTIONS.SET_BOUNDS,
1379
+ action: exports.CLIENT_ACTIONS.SET_BOUNDS,
1360
1380
  payload: positionData
1361
1381
  });
1362
1382
  }
@@ -1368,23 +1388,26 @@ function setBounds() {
1368
1388
  */
1369
1389
  function listenEditorMessages() {
1370
1390
  const messageCallback = (event) => {
1371
- switch (event.data) {
1372
- case NOTIFY_CUSTOMER.EMA_REQUEST_BOUNDS: {
1391
+ const ACTIONS_NOTIFICATION = {
1392
+ [exports.NOTIFY_CLIENT.UVE_RELOAD_PAGE]: () => {
1393
+ window.location.reload();
1394
+ },
1395
+ [exports.NOTIFY_CLIENT.UVE_REQUEST_BOUNDS]: () => {
1373
1396
  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;
1397
+ },
1398
+ [exports.NOTIFY_CLIENT.UVE_SCROLL_INSIDE_IFRAME]: () => {
1399
+ const direction = event.data.direction;
1400
+ if ((window.scrollY === 0 && direction === 'up') ||
1401
+ (scrollIsInBottom() && direction === 'down')) {
1402
+ // If the iframe scroll is at the top or bottom, do not send anything.
1403
+ // This avoids losing the scrollend event.
1404
+ return;
1405
+ }
1406
+ const scrollY = direction === 'up' ? -120 : 120;
1407
+ window.scrollBy({ left: 0, top: scrollY, behavior: 'smooth' });
1384
1408
  }
1385
- const scrollY = direction === 'up' ? -120 : 120;
1386
- window.scrollBy({ left: 0, top: scrollY, behavior: 'smooth' });
1387
- }
1409
+ };
1410
+ ACTIONS_NOTIFICATION[event.data.name]?.();
1388
1411
  };
1389
1412
  window.addEventListener('message', messageCallback);
1390
1413
  subscriptions.push({
@@ -1436,7 +1459,7 @@ function listenHoveredContentlet() {
1436
1459
  vtlFiles
1437
1460
  };
1438
1461
  postMessageToEditor({
1439
- action: exports.CUSTOMER_ACTIONS.SET_CONTENTLET,
1462
+ action: exports.CLIENT_ACTIONS.SET_CONTENTLET,
1440
1463
  payload: {
1441
1464
  x,
1442
1465
  y,
@@ -1463,13 +1486,17 @@ function listenHoveredContentlet() {
1463
1486
  function scrollHandler() {
1464
1487
  const scrollCallback = () => {
1465
1488
  postMessageToEditor({
1466
- action: exports.CUSTOMER_ACTIONS.IFRAME_SCROLL
1489
+ action: exports.CLIENT_ACTIONS.IFRAME_SCROLL
1467
1490
  });
1468
- window.lastScrollYPosition = window.scrollY;
1491
+ // In case it doesn't have a dotUVE object, we create it with the initial values.
1492
+ window.dotUVE = {
1493
+ ...(window.dotUVE ?? INITIAL_DOT_UVE),
1494
+ lastScrollYPosition: window.scrollY
1495
+ };
1469
1496
  };
1470
1497
  const scrollEndCallback = () => {
1471
1498
  postMessageToEditor({
1472
- action: exports.CUSTOMER_ACTIONS.IFRAME_SCROLL_END
1499
+ action: exports.CLIENT_ACTIONS.IFRAME_SCROLL_END
1473
1500
  });
1474
1501
  };
1475
1502
  window.addEventListener('scroll', scrollCallback);
@@ -1493,7 +1520,7 @@ function scrollHandler() {
1493
1520
  */
1494
1521
  function fetchPageDataFromInsideUVE(pathname) {
1495
1522
  postMessageToEditor({
1496
- action: exports.CUSTOMER_ACTIONS.GET_PAGE_DATA,
1523
+ action: exports.CLIENT_ACTIONS.GET_PAGE_DATA,
1497
1524
  payload: {
1498
1525
  pathname
1499
1526
  }
@@ -1510,12 +1537,27 @@ function fetchPageDataFromInsideUVE(pathname) {
1510
1537
  */
1511
1538
  function updateNavigation(pathname) {
1512
1539
  postMessageToEditor({
1513
- action: exports.CUSTOMER_ACTIONS.NAVIGATION_UPDATE,
1540
+ action: exports.CLIENT_ACTIONS.NAVIGATION_UPDATE,
1514
1541
  payload: {
1515
1542
  url: pathname === '/' ? 'index' : pathname?.replace('/', '')
1516
1543
  }
1517
1544
  });
1518
1545
  }
1546
+ /**
1547
+ * You can use this function to edit a contentlet in the editor.
1548
+ *
1549
+ * Calling this function inside the editor, will prompt the UVE to open a dialog to edit the contentlet.
1550
+ *
1551
+ * @export
1552
+ * @template T
1553
+ * @param {Contentlet<T>} contentlet - The contentlet to edit.
1554
+ */
1555
+ function editContentlet(contentlet) {
1556
+ postMessageToEditor({
1557
+ action: exports.CLIENT_ACTIONS.EDIT_CONTENTLET,
1558
+ payload: contentlet
1559
+ });
1560
+ }
1519
1561
  /**
1520
1562
  * Checks if the code is running inside an editor.
1521
1563
  *
@@ -1535,6 +1577,9 @@ function isInsideEditor() {
1535
1577
  }
1536
1578
  return window.parent !== window;
1537
1579
  }
1580
+ function initDotUVE() {
1581
+ window.dotUVE = INITIAL_DOT_UVE;
1582
+ }
1538
1583
  /**
1539
1584
  * Initializes the DotCMS page editor.
1540
1585
  *
@@ -1546,6 +1591,7 @@ function isInsideEditor() {
1546
1591
  * ```
1547
1592
  */
1548
1593
  function initEditor(config) {
1594
+ initDotUVE();
1549
1595
  fetchPageDataFromInsideUVE(config.pathname);
1550
1596
  listenEditorMessages();
1551
1597
  listenHoveredContentlet();
@@ -1704,7 +1750,7 @@ class DotCmsClient {
1704
1750
  }
1705
1751
  if (action === 'changes') {
1706
1752
  const messageCallback = (event) => {
1707
- if (event.data.name === 'SET_PAGE_DATA') {
1753
+ if (event.data.name === exports.NOTIFY_CLIENT.UVE_SET_PAGE_DATA) {
1708
1754
  callbackFn(event.data.payload);
1709
1755
  }
1710
1756
  };
@@ -1945,6 +1991,7 @@ const getPageRequestParams = ({ path = '', params = {} }) => {
1945
1991
 
1946
1992
  exports.DotCmsClient = DotCmsClient;
1947
1993
  exports.destroyEditor = destroyEditor;
1994
+ exports.editContentlet = editContentlet;
1948
1995
  exports.getPageRequestParams = getPageRequestParams;
1949
1996
  exports.graphqlToPageEntity = graphqlToPageEntity;
1950
1997
  exports.initEditor = initEditor;
package/index.esm.js CHANGED
@@ -1062,68 +1062,114 @@ 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";
1164
+ /**
1165
+ * Tell the editor to open the block editor sidebar
1166
+ */
1167
+ CLIENT_ACTIONS["INIT_BLOCK_EDITOR_INLINE_EDITING"] = "init-editor-inline-editing";
1122
1168
  /**
1123
1169
  * Tell the editor to do nothing
1124
1170
  */
1125
- CUSTOMER_ACTIONS["NOOP"] = "noop";
1126
- })(CUSTOMER_ACTIONS || (CUSTOMER_ACTIONS = {}));
1171
+ CLIENT_ACTIONS["NOOP"] = "noop";
1172
+ })(CLIENT_ACTIONS || (CLIENT_ACTIONS = {}));
1127
1173
  /**
1128
1174
  * Post message to dotcms page editor
1129
1175
  *
@@ -1135,32 +1181,6 @@ function postMessageToEditor(message) {
1135
1181
  window.parent.postMessage(message, '*');
1136
1182
  }
1137
1183
 
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
1184
  /**
1165
1185
  * Calculates the bounding information for each page element within the given containers.
1166
1186
  *
@@ -1354,7 +1374,7 @@ function setBounds() {
1354
1374
  const containers = Array.from(document.querySelectorAll('[data-dot-object="container"]'));
1355
1375
  const positionData = getPageElementBound(containers);
1356
1376
  postMessageToEditor({
1357
- action: CUSTOMER_ACTIONS.SET_BOUNDS,
1377
+ action: CLIENT_ACTIONS.SET_BOUNDS,
1358
1378
  payload: positionData
1359
1379
  });
1360
1380
  }
@@ -1366,23 +1386,26 @@ function setBounds() {
1366
1386
  */
1367
1387
  function listenEditorMessages() {
1368
1388
  const messageCallback = (event) => {
1369
- switch (event.data) {
1370
- case NOTIFY_CUSTOMER.EMA_REQUEST_BOUNDS: {
1389
+ const ACTIONS_NOTIFICATION = {
1390
+ [NOTIFY_CLIENT.UVE_RELOAD_PAGE]: () => {
1391
+ window.location.reload();
1392
+ },
1393
+ [NOTIFY_CLIENT.UVE_REQUEST_BOUNDS]: () => {
1371
1394
  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;
1395
+ },
1396
+ [NOTIFY_CLIENT.UVE_SCROLL_INSIDE_IFRAME]: () => {
1397
+ const direction = event.data.direction;
1398
+ if ((window.scrollY === 0 && direction === 'up') ||
1399
+ (scrollIsInBottom() && direction === 'down')) {
1400
+ // If the iframe scroll is at the top or bottom, do not send anything.
1401
+ // This avoids losing the scrollend event.
1402
+ return;
1403
+ }
1404
+ const scrollY = direction === 'up' ? -120 : 120;
1405
+ window.scrollBy({ left: 0, top: scrollY, behavior: 'smooth' });
1382
1406
  }
1383
- const scrollY = direction === 'up' ? -120 : 120;
1384
- window.scrollBy({ left: 0, top: scrollY, behavior: 'smooth' });
1385
- }
1407
+ };
1408
+ ACTIONS_NOTIFICATION[event.data.name]?.();
1386
1409
  };
1387
1410
  window.addEventListener('message', messageCallback);
1388
1411
  subscriptions.push({
@@ -1434,7 +1457,7 @@ function listenHoveredContentlet() {
1434
1457
  vtlFiles
1435
1458
  };
1436
1459
  postMessageToEditor({
1437
- action: CUSTOMER_ACTIONS.SET_CONTENTLET,
1460
+ action: CLIENT_ACTIONS.SET_CONTENTLET,
1438
1461
  payload: {
1439
1462
  x,
1440
1463
  y,
@@ -1461,13 +1484,17 @@ function listenHoveredContentlet() {
1461
1484
  function scrollHandler() {
1462
1485
  const scrollCallback = () => {
1463
1486
  postMessageToEditor({
1464
- action: CUSTOMER_ACTIONS.IFRAME_SCROLL
1487
+ action: CLIENT_ACTIONS.IFRAME_SCROLL
1465
1488
  });
1466
- window.lastScrollYPosition = window.scrollY;
1489
+ // In case it doesn't have a dotUVE object, we create it with the initial values.
1490
+ window.dotUVE = {
1491
+ ...(window.dotUVE ?? INITIAL_DOT_UVE),
1492
+ lastScrollYPosition: window.scrollY
1493
+ };
1467
1494
  };
1468
1495
  const scrollEndCallback = () => {
1469
1496
  postMessageToEditor({
1470
- action: CUSTOMER_ACTIONS.IFRAME_SCROLL_END
1497
+ action: CLIENT_ACTIONS.IFRAME_SCROLL_END
1471
1498
  });
1472
1499
  };
1473
1500
  window.addEventListener('scroll', scrollCallback);
@@ -1491,7 +1518,7 @@ function scrollHandler() {
1491
1518
  */
1492
1519
  function fetchPageDataFromInsideUVE(pathname) {
1493
1520
  postMessageToEditor({
1494
- action: CUSTOMER_ACTIONS.GET_PAGE_DATA,
1521
+ action: CLIENT_ACTIONS.GET_PAGE_DATA,
1495
1522
  payload: {
1496
1523
  pathname
1497
1524
  }
@@ -1508,12 +1535,27 @@ function fetchPageDataFromInsideUVE(pathname) {
1508
1535
  */
1509
1536
  function updateNavigation(pathname) {
1510
1537
  postMessageToEditor({
1511
- action: CUSTOMER_ACTIONS.NAVIGATION_UPDATE,
1538
+ action: CLIENT_ACTIONS.NAVIGATION_UPDATE,
1512
1539
  payload: {
1513
1540
  url: pathname === '/' ? 'index' : pathname?.replace('/', '')
1514
1541
  }
1515
1542
  });
1516
1543
  }
1544
+ /**
1545
+ * You can use this function to edit a contentlet in the editor.
1546
+ *
1547
+ * Calling this function inside the editor, will prompt the UVE to open a dialog to edit the contentlet.
1548
+ *
1549
+ * @export
1550
+ * @template T
1551
+ * @param {Contentlet<T>} contentlet - The contentlet to edit.
1552
+ */
1553
+ function editContentlet(contentlet) {
1554
+ postMessageToEditor({
1555
+ action: CLIENT_ACTIONS.EDIT_CONTENTLET,
1556
+ payload: contentlet
1557
+ });
1558
+ }
1517
1559
  /**
1518
1560
  * Checks if the code is running inside an editor.
1519
1561
  *
@@ -1533,6 +1575,9 @@ function isInsideEditor() {
1533
1575
  }
1534
1576
  return window.parent !== window;
1535
1577
  }
1578
+ function initDotUVE() {
1579
+ window.dotUVE = INITIAL_DOT_UVE;
1580
+ }
1536
1581
  /**
1537
1582
  * Initializes the DotCMS page editor.
1538
1583
  *
@@ -1544,6 +1589,7 @@ function isInsideEditor() {
1544
1589
  * ```
1545
1590
  */
1546
1591
  function initEditor(config) {
1592
+ initDotUVE();
1547
1593
  fetchPageDataFromInsideUVE(config.pathname);
1548
1594
  listenEditorMessages();
1549
1595
  listenHoveredContentlet();
@@ -1702,7 +1748,7 @@ class DotCmsClient {
1702
1748
  }
1703
1749
  if (action === 'changes') {
1704
1750
  const messageCallback = (event) => {
1705
- if (event.data.name === 'SET_PAGE_DATA') {
1751
+ if (event.data.name === NOTIFY_CLIENT.UVE_SET_PAGE_DATA) {
1706
1752
  callbackFn(event.data.payload);
1707
1753
  }
1708
1754
  };
@@ -1941,4 +1987,4 @@ const getPageRequestParams = ({ path = '', params = {} }) => {
1941
1987
  };
1942
1988
  };
1943
1989
 
1944
- export { CUSTOMER_ACTIONS, DotCmsClient, destroyEditor, getPageRequestParams, graphqlToPageEntity, initEditor, isInsideEditor, postMessageToEditor, updateNavigation };
1990
+ 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.48",
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,14 @@ 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",
68
+ /**
69
+ * Tell the editor to open the block editor sidebar
70
+ */
71
+ INIT_BLOCK_EDITOR_INLINE_EDITING = "init-editor-inline-editing",
57
72
  /**
58
73
  * Tell the editor to do nothing
59
74
  */
@@ -67,7 +82,7 @@ export declare enum CUSTOMER_ACTIONS {
67
82
  * @interface PostMessageProps
68
83
  */
69
84
  type PostMessageProps<T> = {
70
- action: CUSTOMER_ACTIONS;
85
+ action: CLIENT_ACTIONS;
71
86
  payload?: T;
72
87
  };
73
88
  /**
@@ -78,4 +93,8 @@ type PostMessageProps<T> = {
78
93
  * @param {PostMessageProps<T>} message
79
94
  */
80
95
  export declare function postMessageToEditor<T = unknown>(message: PostMessageProps<T>): void;
96
+ export interface DotUVE {
97
+ editContentlet: typeof editContentlet;
98
+ lastScrollYPosition: number;
99
+ }
81
100
  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
  *
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Listen for block editor inline event.
3
+ *
4
+ */
5
+ export declare const listenBlockEditorInlineEvent: () => void;