@elementor/editor-variables 4.5.0-1179 → 4.5.0-1180

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.5.0-1179",
3
+ "version": "4.5.0-1180",
4
4
  "private": false,
5
5
  "author": "Elementor Team",
6
6
  "homepage": "https://elementor.com/",
@@ -39,22 +39,22 @@
39
39
  "dev": "tsup --config=../../tsup.dev.ts"
40
40
  },
41
41
  "dependencies": {
42
- "@elementor/editor": "4.5.0-1179",
43
- "@elementor/editor-canvas": "4.5.0-1179",
44
- "@elementor/editor-controls": "4.5.0-1179",
45
- "@elementor/editor-current-user": "4.5.0-1179",
46
- "@elementor/editor-mcp": "4.5.0-1179",
47
- "@elementor/editor-panels": "4.5.0-1179",
48
- "@elementor/editor-props": "4.5.0-1179",
49
- "@elementor/editor-ui": "4.5.0-1179",
50
- "@elementor/editor-v1-adapters": "4.5.0-1179",
51
- "@elementor/menus": "4.5.0-1179",
52
- "@elementor/http-client": "4.5.0-1179",
42
+ "@elementor/editor": "4.5.0-1180",
43
+ "@elementor/editor-canvas": "4.5.0-1180",
44
+ "@elementor/editor-controls": "4.5.0-1180",
45
+ "@elementor/editor-current-user": "4.5.0-1180",
46
+ "@elementor/editor-mcp": "4.5.0-1180",
47
+ "@elementor/editor-panels": "4.5.0-1180",
48
+ "@elementor/editor-props": "4.5.0-1180",
49
+ "@elementor/editor-ui": "4.5.0-1180",
50
+ "@elementor/editor-v1-adapters": "4.5.0-1180",
51
+ "@elementor/menus": "4.5.0-1180",
52
+ "@elementor/http-client": "4.5.0-1180",
53
53
  "@elementor/icons": "~1.78.1",
54
- "@elementor/events": "4.5.0-1179",
55
- "@elementor/schema": "4.5.0-1179",
54
+ "@elementor/events": "4.5.0-1180",
55
+ "@elementor/schema": "4.5.0-1180",
56
56
  "@elementor/ui": "1.41.4",
57
- "@elementor/utils": "4.5.0-1179",
57
+ "@elementor/utils": "4.5.0-1180",
58
58
  "@wordpress/i18n": "^5.13.0"
59
59
  },
60
60
  "peerDependencies": {
@@ -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
- };