@elementor/editor-global-classes 4.2.0-921 → 4.2.0-923

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
@@ -6,7 +6,7 @@ import { setDocumentModifiedStatus } from "@elementor/editor-documents";
6
6
  import { PanelFooter } from "@elementor/editor-panels";
7
7
  import { ConfirmationDialog as ConfirmationDialog2, SaveChangesDialog, useDialog } from "@elementor/editor-ui";
8
8
  import { useMutation } from "@elementor/query";
9
- import { __dispatch as dispatch3 } from "@elementor/store";
9
+ import { __dispatch as dispatch5 } from "@elementor/store";
10
10
  import { Alert as Alert2, Box as Box11, Button as Button3, Chip as Chip4, DialogHeader as DialogHeader2, Divider as Divider4, ErrorBoundary, Stack as Stack9 } from "@elementor/ui";
11
11
  import { __ as __14 } from "@wordpress/i18n";
12
12
 
@@ -305,7 +305,7 @@ var selectOrderedClasses = createSelector(
305
305
  var selectClass = (state, id) => state[SLICE_NAME].data.items[id] ?? null;
306
306
  var selectEmptyCssClass = createSelector(
307
307
  selectData,
308
- ({ items }) => Object.values(items).filter((cssClass) => cssClass.variants.length === 0)
308
+ ({ items }) => Object.values(items).filter((cssClass) => (cssClass.variants?.length ?? 0) === 0)
309
309
  );
310
310
  var selectIsClassFetched = (state, id) => !!state[SLICE_NAME].initialData.preview.items[id] || !!state[SLICE_NAME].initialData.frontend.items[id] || false;
311
311
 
@@ -1169,6 +1169,115 @@ var useOrderedClasses = () => {
1169
1169
  return useSelector3(selectOrderedClasses);
1170
1170
  };
1171
1171
 
1172
+ // src/load-existing-classes.ts
1173
+ import { __dispatch as dispatch4, __getState as getState3 } from "@elementor/store";
1174
+
1175
+ // src/load-document-classes.ts
1176
+ import { getCurrentDocument } from "@elementor/editor-documents";
1177
+ import { __dispatch as dispatch3 } from "@elementor/store";
1178
+
1179
+ // src/utils/create-labels-for-classes.ts
1180
+ function createLabelsForClasses(entries) {
1181
+ return Object.fromEntries(entries.map((e) => [e.id, e.label]));
1182
+ }
1183
+
1184
+ // src/load-document-classes.ts
1185
+ function styleDefinitionsMapWithoutNull(map) {
1186
+ return Object.fromEntries(
1187
+ Object.entries(map).filter(
1188
+ (entry) => entry[1] !== null
1189
+ )
1190
+ );
1191
+ }
1192
+ function resetGlobalClassesState(globalOrder, classLabels) {
1193
+ dispatch3(
1194
+ slice.actions.load({
1195
+ preview: { items: {}, order: globalOrder },
1196
+ frontend: { items: {}, order: globalOrder },
1197
+ classLabels
1198
+ })
1199
+ );
1200
+ }
1201
+ async function loadCurrentDocumentClasses() {
1202
+ const [previewIndexRes, frontendIndexRes] = await Promise.all([
1203
+ apiClient.all("preview"),
1204
+ apiClient.all("frontend")
1205
+ ]);
1206
+ const previewIndex = previewIndexRes.data.data;
1207
+ const frontendIndex = frontendIndexRes.data.data;
1208
+ const classLabels = createLabelsForClasses(previewIndex);
1209
+ const previewOrder = previewIndex.map((e) => e.id);
1210
+ const frontendOrder = frontendIndex.map((e) => e.id);
1211
+ resetGlobalClassesState(previewOrder, classLabels);
1212
+ const postId = getCurrentDocument()?.id;
1213
+ if (!postId) {
1214
+ return;
1215
+ }
1216
+ const [previewPostRes, frontendPostRes] = await Promise.all([
1217
+ apiClient.getStylesForPost(postId, "preview"),
1218
+ apiClient.getStylesForPost(postId, "frontend")
1219
+ ]);
1220
+ const previewItems = styleDefinitionsMapWithoutNull(previewPostRes.data.data);
1221
+ const frontendItems = styleDefinitionsMapWithoutNull(frontendPostRes.data.data);
1222
+ dispatch3(
1223
+ slice.actions.load({
1224
+ preview: { items: previewItems, order: previewOrder },
1225
+ frontend: { items: frontendItems, order: frontendOrder },
1226
+ classLabels
1227
+ })
1228
+ );
1229
+ }
1230
+ async function addDocumentClasses(documentId) {
1231
+ const [previewPostRes, frontendPostRes] = await Promise.all([
1232
+ apiClient.getStylesForPost(documentId, "preview"),
1233
+ apiClient.getStylesForPost(documentId, "frontend")
1234
+ ]);
1235
+ const previewItems = styleDefinitionsMapWithoutNull(previewPostRes.data.data);
1236
+ const frontendItems = styleDefinitionsMapWithoutNull(frontendPostRes.data.data);
1237
+ dispatch3(
1238
+ slice.actions.mergeExistingClasses({
1239
+ preview: previewItems,
1240
+ frontend: frontendItems
1241
+ })
1242
+ );
1243
+ }
1244
+
1245
+ // src/load-existing-classes.ts
1246
+ var pendingLoad = null;
1247
+ var pendingIds = /* @__PURE__ */ new Set();
1248
+ async function loadExistingClasses(classIds) {
1249
+ const existingClasses = selectGlobalClasses(getState3());
1250
+ const missingIds = classIds.filter((id) => !(id in existingClasses));
1251
+ if (missingIds.length === 0) {
1252
+ return;
1253
+ }
1254
+ missingIds.forEach((id) => pendingIds.add(id));
1255
+ if (pendingLoad) {
1256
+ await pendingLoad;
1257
+ return loadExistingClasses(classIds);
1258
+ }
1259
+ pendingLoad = fetchAndMergeClasses();
1260
+ try {
1261
+ await pendingLoad;
1262
+ } finally {
1263
+ pendingLoad = null;
1264
+ }
1265
+ }
1266
+ async function fetchAndMergeClasses() {
1267
+ const idsToFetch = Array.from(pendingIds);
1268
+ pendingIds.clear();
1269
+ if (idsToFetch.length === 0) {
1270
+ return;
1271
+ }
1272
+ const [previewResponse, frontendResponse] = await Promise.all([
1273
+ apiClient.getStylesByIds(idsToFetch, "preview"),
1274
+ apiClient.getStylesByIds(idsToFetch, "frontend")
1275
+ ]);
1276
+ const previewItems = styleDefinitionsMapWithoutNull(previewResponse.data.data);
1277
+ const frontendItems = styleDefinitionsMapWithoutNull(frontendResponse.data.data);
1278
+ dispatch4(slice.actions.mergeExistingClasses({ preview: previewItems, frontend: frontendItems }));
1279
+ }
1280
+
1172
1281
  // src/components/class-manager/class-item.tsx
1173
1282
  import * as React15 from "react";
1174
1283
  import { useRef, useState as useState4 } from "react";
@@ -1882,6 +1991,12 @@ var GlobalClassesList = ({
1882
1991
  const dispatch7 = useDispatch();
1883
1992
  const filters = useFilters();
1884
1993
  const [draggedItemId, setDraggedItemId] = useState5(null);
1994
+ const [loading, setLoading] = useState5({});
1995
+ const addLoadingClass = (classId) => setLoading((prev) => ({ ...prev, [classId]: true }));
1996
+ const removeLoadingClass = (classId) => setLoading((prev) => {
1997
+ const { [classId]: _, ...rest } = prev;
1998
+ return rest;
1999
+ });
1885
2000
  const draggedItemLabel = cssClasses.find((cssClass) => cssClass.id === draggedItemId)?.label ?? "";
1886
2001
  const [classesOrder, reorderClasses] = useReorder(draggedItemId, setDraggedItemId, draggedItemLabel ?? "");
1887
2002
  const filteredCssClasses = useFilteredCssClasses();
@@ -1970,25 +2085,31 @@ var GlobalClassesList = ({
1970
2085
  {
1971
2086
  id: cssClass.id,
1972
2087
  label: cssClass.label,
1973
- renameClass: (newLabel) => {
1974
- trackGlobalClasses({
1975
- event: "classRenamed",
1976
- classId: cssClass.id,
1977
- oldValue: cssClass.label,
1978
- newValue: newLabel,
1979
- source: "class-manager"
1980
- });
1981
- dispatch7(
1982
- slice.actions.update({
1983
- style: {
1984
- id: cssClass.id,
1985
- label: newLabel
1986
- }
1987
- })
1988
- );
2088
+ renameClass: async (newLabel) => {
2089
+ addLoadingClass(cssClass.id);
2090
+ try {
2091
+ trackGlobalClasses({
2092
+ event: "classRenamed",
2093
+ classId: cssClass.id,
2094
+ oldValue: cssClass.label,
2095
+ newValue: newLabel,
2096
+ source: "class-manager"
2097
+ });
2098
+ void await loadExistingClasses([cssClass.id]);
2099
+ dispatch7(
2100
+ slice.actions.update({
2101
+ style: {
2102
+ id: cssClass.id,
2103
+ label: newLabel
2104
+ }
2105
+ })
2106
+ );
2107
+ } finally {
2108
+ removeLoadingClass(cssClass.id);
2109
+ }
1989
2110
  },
1990
2111
  selected: isDragged,
1991
- disabled: disabled || isDragPlaceholder,
2112
+ disabled: disabled || isDragPlaceholder || loading[cssClass.id],
1992
2113
  sortableTriggerProps: {
1993
2114
  ...triggerProps,
1994
2115
  style: triggerStyle
@@ -2173,7 +2294,7 @@ function ClassManagerPanelContent({
2173
2294
  const [scrollElement, setScrollElement] = useState7(null);
2174
2295
  const { mutateAsync: publish, isPending: isPublishing } = usePublish();
2175
2296
  const resetAndClosePanel = () => {
2176
- dispatch3(slice.actions.resetToInitialState({ context: "frontend" }));
2297
+ dispatch5(slice.actions.resetToInitialState({ context: "frontend" }));
2177
2298
  closeSaveChangesDialog();
2178
2299
  };
2179
2300
  const handleClosePanel = useCallback(() => {
@@ -2197,7 +2318,7 @@ function ClassManagerPanelContent({
2197
2318
  };
2198
2319
  }, []);
2199
2320
  const handleStopSync = useCallback((classId) => {
2200
- dispatch3(
2321
+ dispatch5(
2201
2322
  slice.actions.update({
2202
2323
  style: {
2203
2324
  id: classId,
@@ -2209,7 +2330,7 @@ function ClassManagerPanelContent({
2209
2330
  setStopSyncConfirmation(null);
2210
2331
  }, []);
2211
2332
  const handleStartSync = useCallback((classId) => {
2212
- dispatch3(
2333
+ dispatch5(
2213
2334
  slice.actions.update({
2214
2335
  style: {
2215
2336
  id: classId,
@@ -2415,110 +2536,6 @@ var getCapabilities = () => {
2415
2536
  }
2416
2537
  };
2417
2538
 
2418
- // src/load-existing-classes.ts
2419
- import { __dispatch as dispatch5, __getState as getState3 } from "@elementor/store";
2420
-
2421
- // src/load-document-classes.ts
2422
- import { getCurrentDocument } from "@elementor/editor-documents";
2423
- import { __dispatch as dispatch4 } from "@elementor/store";
2424
-
2425
- // src/utils/create-labels-for-classes.ts
2426
- function createLabelsForClasses(entries) {
2427
- return Object.fromEntries(entries.map((e) => [e.id, e.label]));
2428
- }
2429
-
2430
- // src/load-document-classes.ts
2431
- function styleDefinitionsMapWithoutNull(map) {
2432
- return Object.fromEntries(
2433
- Object.entries(map).filter(
2434
- (entry) => entry[1] !== null
2435
- )
2436
- );
2437
- }
2438
- function resetGlobalClassesState(globalOrder, classLabels) {
2439
- dispatch4(
2440
- slice.actions.load({
2441
- preview: { items: {}, order: globalOrder },
2442
- frontend: { items: {}, order: globalOrder },
2443
- classLabels
2444
- })
2445
- );
2446
- }
2447
- async function loadCurrentDocumentClasses() {
2448
- const previewIndexRes = await apiClient.all("preview");
2449
- const previewIndex = previewIndexRes.data.data;
2450
- const classLabels = createLabelsForClasses(previewIndex);
2451
- const globalOrder = previewIndex.map((e) => e.id);
2452
- resetGlobalClassesState(globalOrder, classLabels);
2453
- const postId = getCurrentDocument()?.id;
2454
- if (!postId) {
2455
- return;
2456
- }
2457
- const [previewPostRes, frontendPostRes] = await Promise.all([
2458
- apiClient.getStylesForPost(postId, "preview"),
2459
- apiClient.getStylesForPost(postId, "frontend")
2460
- ]);
2461
- const previewItems = styleDefinitionsMapWithoutNull(previewPostRes.data.data);
2462
- const frontendItems = styleDefinitionsMapWithoutNull(frontendPostRes.data.data);
2463
- dispatch4(
2464
- slice.actions.load({
2465
- preview: { items: previewItems, order: globalOrder },
2466
- frontend: { items: frontendItems, order: globalOrder },
2467
- classLabels
2468
- })
2469
- );
2470
- }
2471
- async function addDocumentClasses(documentId) {
2472
- const [previewPostRes, frontendPostRes] = await Promise.all([
2473
- apiClient.getStylesForPost(documentId, "preview"),
2474
- apiClient.getStylesForPost(documentId, "frontend")
2475
- ]);
2476
- const previewItems = styleDefinitionsMapWithoutNull(previewPostRes.data.data);
2477
- const frontendItems = styleDefinitionsMapWithoutNull(frontendPostRes.data.data);
2478
- dispatch4(
2479
- slice.actions.mergeExistingClasses({
2480
- preview: previewItems,
2481
- frontend: frontendItems
2482
- })
2483
- );
2484
- }
2485
-
2486
- // src/load-existing-classes.ts
2487
- var pendingLoad = null;
2488
- var pendingIds = /* @__PURE__ */ new Set();
2489
- async function loadExistingClasses(classIds) {
2490
- const existingClasses = selectGlobalClasses(getState3());
2491
- const missingIds = classIds.filter((id) => !(id in existingClasses));
2492
- if (missingIds.length === 0) {
2493
- return;
2494
- }
2495
- missingIds.forEach((id) => pendingIds.add(id));
2496
- if (pendingLoad) {
2497
- await pendingLoad;
2498
- return loadExistingClasses(classIds);
2499
- }
2500
- pendingLoad = fetchAndMergeClasses();
2501
- try {
2502
- await pendingLoad;
2503
- } finally {
2504
- pendingLoad = null;
2505
- }
2506
- }
2507
- async function fetchAndMergeClasses() {
2508
- const idsToFetch = Array.from(pendingIds);
2509
- pendingIds.clear();
2510
- if (idsToFetch.length === 0) {
2511
- return;
2512
- }
2513
- const [previewResponse, frontendResponse] = await Promise.all([
2514
- apiClient.getStylesByIds(idsToFetch, "preview"),
2515
- apiClient.getStylesByIds(idsToFetch, "frontend")
2516
- ]);
2517
- const previewItems = styleDefinitionsMapWithoutNull(previewResponse.data.data);
2518
- const frontendItems = styleDefinitionsMapWithoutNull(frontendResponse.data.data);
2519
- dispatch5(slice.actions.mergeExistingClasses({ preview: previewItems, frontend: frontendItems }));
2520
- }
2521
-
2522
2539
  // src/global-classes-styles-provider.ts
2523
2540
  var MAX_CLASSES = 1e3;
2524
2541
  var GLOBAL_CLASSES_PROVIDER_KEY = "global-classes";