@elementor/editor-variables 4.4.0-beta3 → 4.4.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elementor/editor-variables",
3
- "version": "4.4.0-beta3",
3
+ "version": "4.4.1",
4
4
  "private": false,
5
5
  "author": "Elementor Team",
6
6
  "homepage": "https://elementor.com/",
@@ -39,22 +39,23 @@
39
39
  "dev": "tsup --config=../../tsup.dev.ts"
40
40
  },
41
41
  "dependencies": {
42
- "@elementor/editor": "4.4.0-beta3",
43
- "@elementor/editor-canvas": "4.4.0-beta3",
44
- "@elementor/editor-controls": "4.4.0-beta3",
45
- "@elementor/editor-current-user": "4.4.0-beta3",
46
- "@elementor/editor-mcp": "4.4.0-beta3",
47
- "@elementor/editor-panels": "4.4.0-beta3",
48
- "@elementor/editor-props": "4.4.0-beta3",
49
- "@elementor/editor-ui": "4.4.0-beta3",
50
- "@elementor/editor-v1-adapters": "4.4.0-beta3",
51
- "@elementor/menus": "4.4.0-beta3",
52
- "@elementor/http-client": "4.4.0-beta3",
42
+ "@elementor/editor": "4.4.1",
43
+ "@elementor/editor-canvas": "4.4.1",
44
+ "@elementor/editor-controls": "4.4.1",
45
+ "@elementor/editor-current-user": "4.4.1",
46
+ "@elementor/editor-mcp": "4.4.1",
47
+ "@elementor/editor-panels": "4.4.1",
48
+ "@elementor/editor-props": "4.4.1",
49
+ "@elementor/editor-ui": "4.4.1",
50
+ "@elementor/editor-v1-adapters": "4.4.1",
51
+ "@elementor/locations": "4.4.1",
52
+ "@elementor/menus": "4.4.1",
53
+ "@elementor/http-client": "4.4.1",
53
54
  "@elementor/icons": "~1.78.1",
54
- "@elementor/events": "4.4.0-beta3",
55
- "@elementor/schema": "4.4.0-beta3",
55
+ "@elementor/events": "4.4.1",
56
+ "@elementor/schema": "4.4.1",
56
57
  "@elementor/ui": "1.41.4",
57
- "@elementor/utils": "4.4.0-beta3",
58
+ "@elementor/utils": "4.4.1",
58
59
  "@wordpress/i18n": "^5.13.0"
59
60
  },
60
61
  "peerDependencies": {
@@ -13,6 +13,7 @@ import { useVariableBoundProp } from '../hooks/use-variable-bound-prop';
13
13
  import { ERROR_MESSAGES, labelHint, mapServerError } from '../utils/validations';
14
14
  import { LabelField, useLabelError } from './fields/label-field';
15
15
  import { FormField } from './ui/form-field';
16
+ import { VariableFormHeaderSlot } from './variable-form-header-location';
16
17
 
17
18
  const SIZE = 'tiny';
18
19
 
@@ -22,7 +23,7 @@ type Props = {
22
23
  };
23
24
 
24
25
  export const VariableCreation = ( { onGoBack, onClose }: Props ) => {
25
- const { icon: VariableIcon, valueField: ValueField, propTypeUtil } = useVariableType();
26
+ const { icon: VariableIcon, valueField: ValueField, propTypeUtil, variableType } = useVariableType();
26
27
 
27
28
  const { setVariableValue: setVariable, path } = useVariableBoundProp();
28
29
  const { propType } = useBoundProp();
@@ -117,6 +118,13 @@ export const VariableCreation = ( { onGoBack, onClose }: Props ) => {
117
118
  }
118
119
  title={ __( 'Create variable', 'elementor' ) }
119
120
  onClose={ closePopover }
121
+ actions={ [
122
+ <VariableFormHeaderSlot
123
+ key="variable-form-header"
124
+ variableType={ variableType }
125
+ label={ label || undefined }
126
+ />,
127
+ ] }
120
128
  />
121
129
 
122
130
  <Divider />
@@ -17,6 +17,7 @@ import { LabelField, useLabelError } from './fields/label-field';
17
17
  import { DeleteConfirmationDialog } from './ui/delete-confirmation-dialog';
18
18
  import { EDIT_CONFIRMATION_DIALOG_ID, EditConfirmationDialog } from './ui/edit-confirmation-dialog';
19
19
  import { FormField } from './ui/form-field';
20
+ import { VariableFormHeaderSlot } from './variable-form-header-location';
20
21
 
21
22
  const SIZE = 'tiny';
22
23
  const DELETE_LABEL = __( 'Delete variable', 'elementor' );
@@ -125,7 +126,14 @@ export const VariableEdit = ( { onClose, onGoBack, onSubmit, editId }: Props ) =
125
126
  setEditConfirmation( false );
126
127
  };
127
128
 
128
- const actions = [];
129
+ const actions: React.ReactNode[] = [
130
+ <VariableFormHeaderSlot
131
+ key="variable-form-header"
132
+ variableType={ variableType }
133
+ variableId={ editId }
134
+ label={ label }
135
+ />,
136
+ ];
129
137
 
130
138
  if ( userPermissions.canDelete() ) {
131
139
  actions.push(
@@ -0,0 +1,13 @@
1
+ import { createLocation } from '@elementor/locations';
2
+
3
+ export type VariableFormHeaderProps = {
4
+ variableType: string;
5
+ variableId?: string;
6
+ label?: string;
7
+ };
8
+
9
+ export const {
10
+ Slot: VariableFormHeaderSlot,
11
+ inject: injectIntoVariableFormHeader,
12
+ useInjections: useVariableFormHeaderInjections,
13
+ } = createLocation< VariableFormHeaderProps >();
@@ -13,6 +13,7 @@ import { getMenuActionsForVariable, getVariableType } from '../../variables-regi
13
13
  import { DeleteConfirmationDialog } from '../ui/delete-confirmation-dialog';
14
14
  import { EmptyState } from '../ui/empty-state';
15
15
  import { NoSearchResults } from '../ui/no-search-results';
16
+ import { VariablesManagerHeaderSlot } from '../variables-manager-header-location';
16
17
  import { useAutoEdit } from './hooks/use-auto-edit';
17
18
  import { useErrorNavigation } from './hooks/use-error-navigation';
18
19
  import { useVariablesManagerState } from './hooks/use-variables-manager-state';
@@ -272,7 +273,8 @@ function VariablesManagerPanelContent( { onRequestClose, onExposeCloseAttempt }:
272
273
  alignSelf: 'stretch',
273
274
  } }
274
275
  />
275
- <Box sx={ { display: 'flex', flexShrink: 0, alignItems: 'center' } }>
276
+ <Box sx={ { display: 'flex', flexShrink: 0, alignItems: 'center', gap: 0.5 } }>
277
+ <VariablesManagerHeaderSlot />
276
278
  <VariableManagerCreateMenu
277
279
  outlinedTrigger
278
280
  onCreate={ handleCreateVariable }
@@ -0,0 +1,7 @@
1
+ import { createLocation } from '@elementor/locations';
2
+
3
+ export const {
4
+ Slot: VariablesManagerHeaderSlot,
5
+ inject: injectIntoVariablesManagerHeader,
6
+ useInjections: useVariablesManagerHeaderInjections,
7
+ } = createLocation();
package/src/index.ts CHANGED
@@ -3,6 +3,8 @@ export {
3
3
  type VariablesManagerPanelEmbeddedProps,
4
4
  } from './components/variables-manager/variables-manager-panel';
5
5
  export { init } from './init';
6
+ export { injectIntoVariableFormHeader, type VariableFormHeaderProps } from './components/variable-form-header-location';
7
+ export { injectIntoVariablesManagerHeader } from './components/variables-manager-header-location';
6
8
  export { GLOBAL_VARIABLES_URI } from './mcp/variables-resource';
7
9
  export { sizeVariablePropTypeUtil } from './prop-types/size-variable-prop-type';
8
10
  export { registerVariableTypes } from './register-variable-types';
@@ -3,18 +3,29 @@ import { type HttpResponse, httpService } from '@elementor/http-client';
3
3
  import { z } from '@elementor/schema';
4
4
  import { isProActive } from '@elementor/utils';
5
5
 
6
- import { applyLocalMutation } from '../service';
7
- import { type TVariable } from '../storage';
6
+ import {
7
+ applyPlannedVariableChange,
8
+ getPendingAbilityOperations,
9
+ syncPendingVariableChangesWithDocumentSave,
10
+ } from './pending-variable-changes';
8
11
  import { MANAGE_VARIABLES_GUIDE_URI } from './variable-tool-prompt';
9
12
  import { GLOBAL_VARIABLES_URI } from './variables-resource';
10
13
 
11
14
  const MCP_PROXY_URL = 'elementor/v1/mcp-proxy';
12
15
  const TOOL_NAME = 'manage-global-variable';
16
+ const MAX_OPERATIONS_PER_REQUEST = 50;
17
+
18
+ type ManageVariableResult = {
19
+ status: 'ok' | 'error';
20
+ id?: string;
21
+ type?: string;
22
+ label?: string;
23
+ message?: string;
24
+ };
13
25
 
14
26
  type ManageVariableResponse = {
15
27
  status: string;
16
- variable?: TVariable & { id: string };
17
- watermark?: number;
28
+ results?: ManageVariableResult[];
18
29
  };
19
30
 
20
31
  const VARIABLE_TYPES = {
@@ -26,6 +37,8 @@ const VARIABLE_TYPES = {
26
37
 
27
38
  export const initManageVariableTool = ( reg: MCPRegistryEntry ) => {
28
39
  const { addTool, resource } = reg;
40
+
41
+ syncPendingVariableChangesWithDocumentSave();
29
42
  const RUNTIME_ALLOWED_VARIABLE_TYPES = isProActive()
30
43
  ? ( [ VARIABLE_TYPES.COLOR, VARIABLE_TYPES.FONT, VARIABLE_TYPES.SIZE, VARIABLE_TYPES.CUSTOM_SIZE ] as const )
31
44
  : ( [ VARIABLE_TYPES.COLOR, VARIABLE_TYPES.FONT ] as const );
@@ -70,7 +83,7 @@ export const initManageVariableTool = ( reg: MCPRegistryEntry ) => {
70
83
  },
71
84
  outputSchema: {
72
85
  status: z.enum( [ 'ok' ] ).describe( 'Operation status' ),
73
- message: z.string().optional().describe( 'Error details if status is error' ),
86
+ id: z.string().describe( 'Id of the affected variable — use it for subsequent update/delete calls.' ),
74
87
  },
75
88
  requiredResources: [
76
89
  { uri: MANAGE_VARIABLES_GUIDE_URI, description: 'Full guide for variable types, naming rules, and usage' },
@@ -81,18 +94,38 @@ export const initManageVariableTool = ( reg: MCPRegistryEntry ) => {
81
94
  ],
82
95
  isDestructive: true,
83
96
  handler: async ( params ) => {
97
+ const pendingOperations = getPendingAbilityOperations();
98
+
99
+ if ( pendingOperations.length >= MAX_OPERATIONS_PER_REQUEST ) {
100
+ throw new Error( 'Too many unsaved variable changes. Save the page, then try again.' );
101
+ }
102
+
84
103
  const { data } = await httpService().post< HttpResponse< ManageVariableResponse > >( MCP_PROXY_URL, {
85
104
  tool: TOOL_NAME,
86
- input: params,
105
+ input: { operations: [ ...pendingOperations, params ], dry_run: true },
87
106
  } );
88
107
 
89
- const payload = data.data;
108
+ const result = data.data.results?.[ pendingOperations.length ];
90
109
 
91
- if ( payload.variable && typeof payload.watermark === 'number' ) {
92
- applyLocalMutation( params.action, payload.variable, payload.watermark );
110
+ if ( result?.status !== 'ok' ) {
111
+ throw new Error( result?.message || 'Unexpected response from server' );
93
112
  }
94
113
 
95
- return { status: 'ok' };
114
+ const id = result.id ?? params.id;
115
+
116
+ if ( ! id ) {
117
+ throw new Error( 'Variable id is missing from the response' );
118
+ }
119
+
120
+ applyPlannedVariableChange( {
121
+ action: params.action,
122
+ id,
123
+ type: result.type ?? params.type,
124
+ label: result.label ?? params.label,
125
+ value: params.value,
126
+ } );
127
+
128
+ return { status: 'ok' as const, id };
96
129
  },
97
130
  } );
98
131
  };
@@ -0,0 +1,93 @@
1
+ import { __privateRunCommandSync as runCommandSync, registerDataHook } from '@elementor/editor-v1-adapters';
2
+
3
+ import { type BatchOperation } from '../api';
4
+ import { service } from '../service';
5
+
6
+ export type PlannedVariableChange = {
7
+ action: 'create' | 'update' | 'delete';
8
+ id: string;
9
+ type?: string;
10
+ label?: string;
11
+ value?: string;
12
+ };
13
+
14
+ export type AbilityOperation = PlannedVariableChange;
15
+
16
+ let pendingChanges: PlannedVariableChange[] = [];
17
+ let pendingSave: Promise< unknown > | null = null;
18
+
19
+ export const getPendingAbilityOperations = (): AbilityOperation[] =>
20
+ pendingChanges.map( ( change ) => ( { ...change } ) );
21
+
22
+ export const applyPlannedVariableChange = ( change: PlannedVariableChange ) => {
23
+ const existing = service.variables()[ change.id ];
24
+
25
+ if ( change.action === 'delete' ) {
26
+ if ( existing ) {
27
+ service.applyLocalChange( change.id, { ...existing, deleted: true }, false );
28
+ }
29
+ } else {
30
+ service.applyLocalChange(
31
+ change.id,
32
+ {
33
+ ...existing,
34
+ type: change.type ?? existing?.type ?? '',
35
+ label: change.label ?? existing?.label ?? '',
36
+ value: change.value ?? existing?.value ?? '',
37
+ },
38
+ change.action === 'create'
39
+ );
40
+ }
41
+
42
+ pendingChanges.push( change );
43
+ runCommandSync( 'document/save/set-is-modified', { status: true }, { internal: true } );
44
+ };
45
+
46
+ export const savePendingVariableChanges = () => {
47
+ if ( pendingSave ) {
48
+ return pendingSave;
49
+ }
50
+
51
+ if ( ! pendingChanges.length ) {
52
+ return Promise.resolve();
53
+ }
54
+
55
+ const changes = pendingChanges;
56
+ pendingChanges = [];
57
+
58
+ pendingSave = service
59
+ .saveOperations( changes.map( toBatchOperation ) )
60
+ .catch( ( error: unknown ) => {
61
+ pendingChanges = [ ...changes, ...pendingChanges ];
62
+ throw error;
63
+ } )
64
+ .finally( () => {
65
+ pendingSave = null;
66
+ } );
67
+
68
+ return pendingSave;
69
+ };
70
+
71
+ export const syncPendingVariableChangesWithDocumentSave = () => {
72
+ registerDataHook( 'dependency', 'document/save/save', () => {
73
+ savePendingVariableChanges()?.catch( () => {} );
74
+
75
+ return true;
76
+ } );
77
+ };
78
+
79
+ export const resetPendingVariableChanges = () => {
80
+ pendingChanges = [];
81
+ pendingSave = null;
82
+ };
83
+
84
+ function toBatchOperation( { action, id, type, label, value }: PlannedVariableChange ): BatchOperation {
85
+ switch ( action ) {
86
+ case 'create':
87
+ return { type: 'create', variable: { id, type, label, value } };
88
+ case 'update':
89
+ return { type: 'update', id, variable: { label, value } };
90
+ case 'delete':
91
+ return { type: 'delete', id };
92
+ }
93
+ }
package/src/service.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { __ } from '@wordpress/i18n';
2
2
 
3
- import { apiClient } from './api';
3
+ import { apiClient, type BatchOperation } from './api';
4
4
  import { buildOperationsArray, type OperationResult } from './batch-operations';
5
5
  import { OP_RW, Storage, type TVariable, type TVariablesList } from './storage';
6
6
  import { styleVariablesRepository } from './style-variables-repository';
@@ -216,8 +216,21 @@ export const service = {
216
216
  } );
217
217
  },
218
218
 
219
+ applyLocalChange: ( id: string, variable: TVariable, isNew: boolean ) => {
220
+ if ( isNew ) {
221
+ storage.add( id, variable );
222
+ } else {
223
+ storage.update( id, variable );
224
+ }
225
+
226
+ styleVariablesRepository.update( { [ id ]: variable } );
227
+ },
228
+
219
229
  batchSave: ( originalVariables: TVariablesList, currentVariables: TVariablesList, deletedVariables: string[] ) => {
220
- const operations = buildOperationsArray( originalVariables, currentVariables, deletedVariables );
230
+ return service.saveOperations( buildOperationsArray( originalVariables, currentVariables, deletedVariables ) );
231
+ },
232
+
233
+ saveOperations: ( operations: BatchOperation[] ) => {
221
234
  const batchPayload = { operations, watermark: storage.state.watermark };
222
235
 
223
236
  if ( operations.length === 0 ) {
@@ -277,23 +290,3 @@ const handleWatermark = ( operation: string, newWatermark: number ) => {
277
290
  }
278
291
  storage.watermark( newWatermark );
279
292
  };
280
-
281
- export const applyLocalMutation = (
282
- action: 'create' | 'update' | 'delete',
283
- variableWithId: TVariable & { id: string },
284
- watermark: number
285
- ) => {
286
- const { id: variableId, ...variable } = variableWithId;
287
-
288
- handleWatermark( OP_RW, watermark );
289
-
290
- if ( action === 'create' ) {
291
- storage.add( variableId, variable );
292
- } else {
293
- storage.update( variableId, variable );
294
- }
295
-
296
- styleVariablesRepository.update( {
297
- [ variableId ]: variable,
298
- } );
299
- };