@elementor/editor-global-classes 0.11.1 → 0.12.0

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.
@@ -30,7 +30,11 @@ export const ClassManagerIntroduction = () => {
30
30
  const IntroductionContent = () => {
31
31
  return (
32
32
  <Stack gap={ 1.5 } padding={ 3 }>
33
- <Image sx={ { width: '100%', height: '312px' } } src="" alt="Introduction" />
33
+ <Image
34
+ sx={ { width: '100%', height: '312px' } }
35
+ src="https://assets.elementor.com/packages/v1/images/class-manager-intro.svg"
36
+ alt=""
37
+ />
34
38
  <Box>
35
39
  <Typography variant={ 'body2' }>
36
40
  { __(
@@ -1,5 +1,10 @@
1
1
  import * as React from 'react';
2
- import { useEffect } from 'react';
2
+ import { useEffect, useState } from 'react';
3
+ import {
4
+ __useActiveDocument as useActiveDocument,
5
+ getDocumentModifiedStatus,
6
+ setDocumentModifiedStatus,
7
+ } from '@elementor/editor-documents';
3
8
  import {
4
9
  __createPanel as createPanel,
5
10
  Panel,
@@ -10,11 +15,12 @@ import {
10
15
  } from '@elementor/editor-panels';
11
16
  import { changeEditMode } from '@elementor/editor-v1-adapters';
12
17
  import { XIcon } from '@elementor/icons';
18
+ import { useMutation } from '@elementor/query';
13
19
  import { Alert, Box, Button, ErrorBoundary, IconButton, type IconButtonProps, Stack } from '@elementor/ui';
14
20
  import { __ } from '@wordpress/i18n';
15
21
 
16
22
  import { useDirtyState } from '../../hooks/use-dirty-state';
17
- import { publishGlobalClasses } from '../../publish-global-classes';
23
+ import { saveGlobalClasses } from '../../save-global-classes';
18
24
  import { ClassManagerIntroduction } from './class-manager-introduction';
19
25
  import { FlippedColorSwatchIcon } from './flipped-color-swatch-icon';
20
26
  import { GlobalClassesList } from './global-classes-list';
@@ -29,16 +35,29 @@ const id = 'global-classes-manager';
29
35
  export const { panel, usePanelActions } = createPanel( {
30
36
  id,
31
37
  component: ClassManagerPanel,
32
- onOpen: () => changeEditMode( id ),
33
- onClose: () => changeEditMode( 'edit' ),
34
38
  allowedEditModes: [ 'edit', id ],
39
+ onOpen: () => {
40
+ changeEditMode( id );
41
+
42
+ return getDocumentModifiedStatus();
43
+ },
44
+ onClose: ( documentModifiedState ) => {
45
+ changeEditMode( 'edit' );
46
+
47
+ if ( getDocumentModifiedStatus() !== documentModifiedState ) {
48
+ setDocumentModifiedStatus( documentModifiedState );
49
+ }
50
+ },
35
51
  } );
36
52
 
37
53
  export function ClassManagerPanel() {
38
54
  const isDirty = useDirtyState();
55
+
39
56
  const { close: closePanel } = usePanelActions();
40
57
  const { open: openSaveChangesDialog, close: closeSaveChangesDialog, isOpen: isSaveChangesDialogOpen } = useDialog();
41
58
 
59
+ const { mutateAsync: publish, isPending: isPublishing } = usePublish();
60
+
42
61
  usePreventUnload();
43
62
 
44
63
  return (
@@ -65,16 +84,17 @@ export function ClassManagerPanel() {
65
84
  </Stack>
66
85
  </PanelHeader>
67
86
  <PanelBody px={ 2 }>
68
- <GlobalClassesList />
87
+ <GlobalClassesList disabled={ isPublishing } />
69
88
  </PanelBody>
70
89
  <PanelFooter>
71
90
  <Button
72
91
  fullWidth
73
92
  size="small"
74
- variant="contained"
75
93
  color="global"
94
+ variant="contained"
95
+ onClick={ publish }
76
96
  disabled={ ! isDirty }
77
- onClick={ publishGlobalClasses }
97
+ loading={ isPublishing }
78
98
  >
79
99
  { __( 'Save changes', 'elementor' ) }
80
100
  </Button>
@@ -102,7 +122,7 @@ export function ClassManagerPanel() {
102
122
  confirm: {
103
123
  label: __( 'Save & Continue', 'elementor' ),
104
124
  action: async () => {
105
- await publishGlobalClasses();
125
+ await publish();
106
126
  closeSaveChangesDialog();
107
127
  closePanel();
108
128
  },
@@ -146,3 +166,17 @@ const usePreventUnload = () => {
146
166
  };
147
167
  }, [ isDirty ] );
148
168
  };
169
+
170
+ const usePublish = () => {
171
+ const document = useActiveDocument();
172
+ const [ initialDocumentStatus ] = useState( document?.isDirty ?? false );
173
+
174
+ return useMutation( {
175
+ mutationFn: () => saveGlobalClasses( { context: 'frontend' } ),
176
+ onSuccess: () => {
177
+ if ( initialDocumentStatus !== document?.isDirty ) {
178
+ setDocumentModifiedStatus( initialDocumentStatus );
179
+ }
180
+ },
181
+ } );
182
+ };
@@ -31,7 +31,7 @@ import { DeleteConfirmationProvider, useDeleteConfirmation } from './delete-conf
31
31
  import { FlippedColorSwatchIcon } from './flipped-color-swatch-icon';
32
32
  import { SortableItem, SortableProvider, SortableTrigger, type SortableTriggerProps } from './sortable';
33
33
 
34
- export const GlobalClassesList = () => {
34
+ export const GlobalClassesList = ( { disabled }: { disabled?: boolean } ) => {
35
35
  const cssClasses = useOrderedClasses();
36
36
 
37
37
  const [ classesOrder, reorderClasses ] = useReorder();
@@ -57,7 +57,7 @@ export const GlobalClassesList = () => {
57
57
  label={ label }
58
58
  renameClass={ renameClass }
59
59
  selected={ isDragged }
60
- disabled={ isDragPlaceholder }
60
+ disabled={ disabled || isDragPlaceholder }
61
61
  sortableTriggerProps={ { ...triggerProps, style: triggerStyle } }
62
62
  />
63
63
  ) }
@@ -0,0 +1,27 @@
1
+ import { __dispatch as dispatch, __getState as getState } from '@elementor/store';
2
+
3
+ import { apiClient, type UpdateContext } from './api';
4
+ import { type GlobalClassesState, slice } from './store';
5
+
6
+ type Options = {
7
+ context: UpdateContext;
8
+ };
9
+
10
+ export async function saveGlobalClasses( { context }: Options ) {
11
+ const state: GlobalClassesState = getState().globalClasses;
12
+
13
+ const data = {
14
+ items: state.items,
15
+ order: state.order,
16
+ };
17
+
18
+ if ( context === 'preview' ) {
19
+ await apiClient.saveDraft( data );
20
+
21
+ return;
22
+ }
23
+
24
+ await apiClient.publish( data );
25
+
26
+ dispatch( slice.actions.setPristine() );
27
+ }
@@ -1,7 +1,8 @@
1
- import { __privateRunCommandSync as runCommandSync, registerDataHook } from '@elementor/editor-v1-adapters';
1
+ import { setDocumentModifiedStatus } from '@elementor/editor-documents';
2
+ import { registerDataHook } from '@elementor/editor-v1-adapters';
2
3
  import { __getState as getState, __subscribeWithSelector as subscribeWithSelector } from '@elementor/store';
3
4
 
4
- import { publishGlobalClasses } from './publish-global-classes';
5
+ import { saveGlobalClasses } from './save-global-classes';
5
6
  import { selectIsDirty } from './store';
6
7
 
7
8
  export function syncWithDocumentSave() {
@@ -18,12 +19,16 @@ function syncDirtyState() {
18
19
  return;
19
20
  }
20
21
 
21
- runCommandSync( 'document/save/set-is-modified', { status: true }, { internal: true } );
22
+ setDocumentModifiedStatus( true );
22
23
  } );
23
24
  }
24
25
 
25
26
  function bindSaveAction() {
26
- registerDataHook( 'after', 'document/save/save', publishGlobalClasses );
27
+ registerDataHook( 'after', 'document/save/save', ( args ) => {
28
+ return saveGlobalClasses( {
29
+ context: args.status === 'publish' ? 'frontend' : 'preview',
30
+ } );
31
+ } );
27
32
  }
28
33
 
29
34
  function isDirty() {
@@ -1,23 +0,0 @@
1
- import { __dispatch as dispatch, __getState as getState } from '@elementor/store';
2
-
3
- import { apiClient } from './api';
4
- import { type GlobalClassesState, selectIsDirty, slice } from './store';
5
-
6
- export async function publishGlobalClasses() {
7
- if ( ! isDirty() ) {
8
- return;
9
- }
10
-
11
- const state: GlobalClassesState = getState().globalClasses;
12
-
13
- await apiClient.update( {
14
- items: state.items,
15
- order: state.order,
16
- } );
17
-
18
- dispatch( slice.actions.setPristine() );
19
- }
20
-
21
- function isDirty() {
22
- return selectIsDirty( getState() );
23
- }