@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/dist/index.mjs CHANGED
@@ -569,8 +569,18 @@ var service = {
569
569
  };
570
570
  });
571
571
  },
572
+ applyLocalChange: (id, variable, isNew) => {
573
+ if (isNew) {
574
+ storage.add(id, variable);
575
+ } else {
576
+ storage.update(id, variable);
577
+ }
578
+ styleVariablesRepository.update({ [id]: variable });
579
+ },
572
580
  batchSave: (originalVariables, currentVariables, deletedVariables) => {
573
- const operations = buildOperationsArray(originalVariables, currentVariables, deletedVariables);
581
+ return service.saveOperations(buildOperationsArray(originalVariables, currentVariables, deletedVariables));
582
+ },
583
+ saveOperations: (operations) => {
574
584
  const batchPayload = { operations, watermark: storage.state.watermark };
575
585
  if (operations.length === 0) {
576
586
  return Promise.resolve({
@@ -617,18 +627,6 @@ var handleWatermark = (operation, newWatermark) => {
617
627
  }
618
628
  storage.watermark(newWatermark);
619
629
  };
620
- var applyLocalMutation = (action, variableWithId, watermark) => {
621
- const { id: variableId, ...variable } = variableWithId;
622
- handleWatermark(OP_RW, watermark);
623
- if (action === "create") {
624
- storage.add(variableId, variable);
625
- } else {
626
- storage.update(variableId, variable);
627
- }
628
- styleVariablesRepository.update({
629
- [variableId]: variable
630
- });
631
- };
632
630
 
633
631
  // src/transformers/utils/resolve-css-variable.ts
634
632
  var resolveCssVariable = (id, variable) => {
@@ -3878,6 +3876,67 @@ import { httpService as httpService3 } from "@elementor/http-client";
3878
3876
  import { z as z3 } from "@elementor/schema";
3879
3877
  import { isProActive } from "@elementor/utils";
3880
3878
 
3879
+ // src/mcp/pending-variable-changes.ts
3880
+ import { __privateRunCommandSync as runCommandSync, registerDataHook } from "@elementor/editor-v1-adapters";
3881
+ var pendingChanges = [];
3882
+ var pendingSave = null;
3883
+ var getPendingAbilityOperations = () => pendingChanges.map((change) => ({ ...change }));
3884
+ var applyPlannedVariableChange = (change) => {
3885
+ const existing = service.variables()[change.id];
3886
+ if (change.action === "delete") {
3887
+ if (existing) {
3888
+ service.applyLocalChange(change.id, { ...existing, deleted: true }, false);
3889
+ }
3890
+ } else {
3891
+ service.applyLocalChange(
3892
+ change.id,
3893
+ {
3894
+ ...existing,
3895
+ type: change.type ?? existing?.type ?? "",
3896
+ label: change.label ?? existing?.label ?? "",
3897
+ value: change.value ?? existing?.value ?? ""
3898
+ },
3899
+ change.action === "create"
3900
+ );
3901
+ }
3902
+ pendingChanges.push(change);
3903
+ runCommandSync("document/save/set-is-modified", { status: true }, { internal: true });
3904
+ };
3905
+ var savePendingVariableChanges = () => {
3906
+ if (pendingSave) {
3907
+ return pendingSave;
3908
+ }
3909
+ if (!pendingChanges.length) {
3910
+ return Promise.resolve();
3911
+ }
3912
+ const changes = pendingChanges;
3913
+ pendingChanges = [];
3914
+ pendingSave = service.saveOperations(changes.map(toBatchOperation)).catch((error) => {
3915
+ pendingChanges = [...changes, ...pendingChanges];
3916
+ throw error;
3917
+ }).finally(() => {
3918
+ pendingSave = null;
3919
+ });
3920
+ return pendingSave;
3921
+ };
3922
+ var syncPendingVariableChangesWithDocumentSave = () => {
3923
+ registerDataHook("dependency", "document/save/save", () => {
3924
+ savePendingVariableChanges()?.catch(() => {
3925
+ });
3926
+ return true;
3927
+ });
3928
+ };
3929
+ function toBatchOperation({ action, id, type, label, value }) {
3930
+ switch (action) {
3931
+ case "create":
3932
+ return { type: "create", variable: { id, type, label, value } };
3933
+ case "update":
3934
+ return { type: "update", id, variable: { label, value } };
3935
+ case "delete":
3936
+ return { type: "delete", id };
3937
+ }
3938
+ }
3939
+
3881
3940
  // src/mcp/variable-tool-prompt.ts
3882
3941
  var MANAGE_VARIABLES_GUIDE_URI = "elementor://variables/tools/manage-global-variable-guide";
3883
3942
 
@@ -3921,6 +3980,7 @@ var initVariablesResource = (variablesMcpEntry, canvasMcpEntry) => {
3921
3980
  // src/mcp/manage-variable-tool.ts
3922
3981
  var MCP_PROXY_URL2 = "elementor/v1/mcp-proxy";
3923
3982
  var TOOL_NAME = "manage-global-variable";
3983
+ var MAX_OPERATIONS_PER_REQUEST = 50;
3924
3984
  var VARIABLE_TYPES = {
3925
3985
  COLOR: "global-color-variable",
3926
3986
  FONT: "global-font-variable",
@@ -3929,6 +3989,7 @@ var VARIABLE_TYPES = {
3929
3989
  };
3930
3990
  var initManageVariableTool = (reg) => {
3931
3991
  const { addTool, resource } = reg;
3992
+ syncPendingVariableChangesWithDocumentSave();
3932
3993
  const RUNTIME_ALLOWED_VARIABLE_TYPES = isProActive() ? [VARIABLE_TYPES.COLOR, VARIABLE_TYPES.FONT, VARIABLE_TYPES.SIZE, VARIABLE_TYPES.CUSTOM_SIZE] : [VARIABLE_TYPES.COLOR, VARIABLE_TYPES.FONT];
3933
3994
  resource(
3934
3995
  "manage-global-variable-guide",
@@ -3961,7 +4022,7 @@ var initManageVariableTool = (reg) => {
3961
4022
  },
3962
4023
  outputSchema: {
3963
4024
  status: z3.enum(["ok"]).describe("Operation status"),
3964
- message: z3.string().optional().describe("Error details if status is error")
4025
+ id: z3.string().describe("Id of the affected variable \u2014 use it for subsequent update/delete calls.")
3965
4026
  },
3966
4027
  requiredResources: [
3967
4028
  { uri: MANAGE_VARIABLES_GUIDE_URI, description: "Full guide for variable types, naming rules, and usage" },
@@ -3972,15 +4033,30 @@ var initManageVariableTool = (reg) => {
3972
4033
  ],
3973
4034
  isDestructive: true,
3974
4035
  handler: async (params) => {
4036
+ const pendingOperations = getPendingAbilityOperations();
4037
+ if (pendingOperations.length >= MAX_OPERATIONS_PER_REQUEST) {
4038
+ throw new Error("Too many unsaved variable changes. Save the page, then try again.");
4039
+ }
3975
4040
  const { data } = await httpService3().post(MCP_PROXY_URL2, {
3976
4041
  tool: TOOL_NAME,
3977
- input: params
4042
+ input: { operations: [...pendingOperations, params], dry_run: true }
3978
4043
  });
3979
- const payload = data.data;
3980
- if (payload.variable && typeof payload.watermark === "number") {
3981
- applyLocalMutation(params.action, payload.variable, payload.watermark);
4044
+ const result = data.data.results?.[pendingOperations.length];
4045
+ if (result?.status !== "ok") {
4046
+ throw new Error(result?.message || "Unexpected response from server");
4047
+ }
4048
+ const id = result.id ?? params.id;
4049
+ if (!id) {
4050
+ throw new Error("Variable id is missing from the response");
3982
4051
  }
3983
- return { status: "ok" };
4052
+ applyPlannedVariableChange({
4053
+ action: params.action,
4054
+ id,
4055
+ type: result.type ?? params.type,
4056
+ label: result.label ?? params.label,
4057
+ value: params.value
4058
+ });
4059
+ return { status: "ok", id };
3984
4060
  }
3985
4061
  });
3986
4062
  };