@elementor/editor-variables 4.1.0-beta1 → 4.1.0-beta2

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
@@ -1049,8 +1049,17 @@ var useVariablesManagerState = () => {
1049
1049
  }, []);
1050
1050
  const handleOnChange = useCallback3(
1051
1051
  (newVariables) => {
1052
- setVariables({ ...variables, ...newVariables });
1053
- setIsDirty(true);
1052
+ const hasChanges = Object.entries(newVariables).some(([id2, newVar]) => {
1053
+ const existingVar = variables[id2];
1054
+ if (!existingVar) {
1055
+ return true;
1056
+ }
1057
+ return existingVar.label !== newVar.label || existingVar.value !== newVar.value || existingVar.order !== newVar.order || existingVar.type !== newVar.type || (existingVar.sync_to_v3 ?? false) !== (newVar.sync_to_v3 ?? false);
1058
+ });
1059
+ if (hasChanges) {
1060
+ setVariables({ ...variables, ...newVariables });
1061
+ setIsDirty(true);
1062
+ }
1054
1063
  },
1055
1064
  [variables]
1056
1065
  );
@@ -1788,7 +1797,7 @@ var VariableRow = (props) => {
1788
1797
  value,
1789
1798
  onChange,
1790
1799
  onPropTypeKeyChange: (type) => {
1791
- if (!isDisabled) {
1800
+ if (!isDisabled && type !== row.type) {
1792
1801
  handleOnChange({
1793
1802
  ...variables,
1794
1803
  [row.id]: { ...variables[row.id], type }
@@ -3927,6 +3936,84 @@ var trackOpenVariablePopover = (path, variableType) => {
3927
3936
 
3928
3937
  // src/mcp/manage-variable-tool.ts
3929
3938
  import { z as z3 } from "@elementor/schema";
3939
+ import { isProActive as isProActive2 } from "@elementor/utils";
3940
+
3941
+ // src/mcp/variable-tool-prompt.ts
3942
+ import { toolPrompts } from "@elementor/editor-mcp";
3943
+ import { isProActive } from "@elementor/utils";
3944
+ var MANAGE_VARIABLES_GUIDE_URI = "elementor://variables/tools/manage-global-variable-guide";
3945
+ var generateVariablesPrompt = () => {
3946
+ const prompt = toolPrompts("manage-global-variable");
3947
+ const proIsActive = isProActive();
3948
+ const sizeVariableSection = proIsActive ? `- **global-size-variable** \u2014 A simple CSS length with a unit (Elementor Pro). Use this for fixed spacing, font sizes, or layout values. Example: \`16px\`, \`1.5rem\`, \`2em\`, \`10vh\`
3949
+ - **global-custom-size-variable** \u2014 Any CSS size expression that goes beyond a simple number + unit (Elementor Pro). Use this when the value is a CSS function, a keyword, or a combination of units that \`global-size-variable\` cannot represent. Example: \`auto\`, \`clamp(1rem, 2vw, 2rem)\`, \`calc(100% - 32px)\`, \`min(50vw, 600px)\`, \`300ms\`, \`2ch\`. When in doubt: if the value contains a function call or a keyword, use \`global-custom-size-variable\`.` : `- ~~global-size-variable~~ \u2014 requires Elementor Pro (not available on this site)
3950
+ - ~~global-custom-size-variable~~ \u2014 requires Elementor Pro (not available on this site)`;
3951
+ prompt.description(`
3952
+ # Purpose
3953
+ Create, update, or delete V4 global CSS variables. These are distinct from legacy v3 globals and map 1:1 to \`--css-var: VALUE\`.
3954
+
3955
+ # Available Types
3956
+ - **global-color-variable** \u2014 CSS color value. Example: \`#FF0000\`, \`rgba(255,0,0,1)\`, \`hsl(0,100%,50%)\`
3957
+ - **global-font-variable** \u2014 Font family name ONLY \u2014 NOT a size or px value. Example: \`Roboto\`, \`Open Sans\`. NEVER pass px/rem here.
3958
+ ${sizeVariableSection}
3959
+
3960
+ # Naming Rules
3961
+ - Labels must be **lowercase**, using only letters (a-z), numbers, digits (0-9), dashes (-), or underscores (_)
3962
+ - No spaces, no special characters
3963
+ - Example: "Headline Primary" \u2192 \`headline-primary\`
3964
+ - Labels must be unique \u2014 always check [elementor://global-variables] first
3965
+
3966
+ # Value Rules
3967
+ - Provide a **plain CSS value** only \u2014 do NOT pass JSON, legacy-globals object structures, or variable references
3968
+ - Values are inserted as-is: \`--css-var: <value>\`
3969
+ - NEVER store a px/rem value inside a \`global-font-variable\` \u2014 use \`global-size-variable\` (Pro) instead
3970
+
3971
+ # Operations
3972
+ - **create** \u2014 requires \`type\`, \`label\`, \`value\`. Label must be unique.
3973
+ - **update** \u2014 requires \`id\`, \`label\`, \`value\`. Get \`id\` from [elementor://global-variables]. When renaming: keep existing value. When changing value: keep exact existing label.
3974
+ - **delete** \u2014 requires \`id\`. DESTRUCTIVE \u2014 always confirm with user before executing.
3975
+ `);
3976
+ prompt.parameter("action", '"create", "update", or "delete".');
3977
+ prompt.parameter("type", "Variable type. Required for create. See Available Types above.");
3978
+ prompt.parameter("label", "Variable name (lowercase, dash-separated). Required for create/update.");
3979
+ prompt.parameter(
3980
+ "value",
3981
+ "Plain CSS value matching the variable type. Required for create/update. Do NOT pass JSON."
3982
+ );
3983
+ prompt.parameter("id", "Variable ID. Required for update/delete. Obtain from [elementor://global-variables].");
3984
+ prompt.example(`
3985
+ Create a brand color:
3986
+ { "action": "create", "type": "global-color-variable", "label": "brand-primary", "value": "#1A73E8" }
3987
+
3988
+ Create a heading font:
3989
+ { "action": "create", "type": "global-font-variable", "label": "font-heading", "value": "Playfair Display" }
3990
+
3991
+ Create a simple spacing size:
3992
+ { "action": "create", "type": "global-size-variable", "label": "spacing-md", "value": "16px" }
3993
+
3994
+ Create a fluid/responsive size using a CSS function (use global-custom-size-variable, NOT global-size-variable):
3995
+ { "action": "create", "type": "global-custom-size-variable", "label": "spacing-fluid", "value": "clamp(1rem, 2vw, 2rem)" }
3996
+
3997
+ Create a size that is a keyword:
3998
+ { "action": "create", "type": "global-custom-size-variable", "label": "width-auto", "value": "auto" }
3999
+
4000
+ Create a size using calc():
4001
+ { "action": "create", "type": "global-custom-size-variable", "label": "sidebar-width", "value": "calc(100% - 32px)" }
4002
+
4003
+ Update a variable's value (keep exact label):
4004
+ { "action": "update", "id": "abc123", "label": "brand-primary", "value": "#0D47A1" }
4005
+
4006
+ Rename a variable (keep existing value):
4007
+ { "action": "update", "id": "abc123", "label": "brand-secondary", "value": "#1A73E8" }
4008
+
4009
+ Delete a variable:
4010
+ { "action": "delete", "id": "abc123" }
4011
+ `);
4012
+ prompt.instruction(
4013
+ "Always read [elementor://global-variables] before creating to check existing variables and avoid duplicate labels."
4014
+ );
4015
+ return prompt.prompt();
4016
+ };
3930
4017
 
3931
4018
  // src/mcp/variables-resource.ts
3932
4019
  import { __privateListenTo as listenTo3, commandEndEvent } from "@elementor/editor-v1-adapters";
@@ -3969,17 +4056,52 @@ var initVariablesResource = (variablesMcpEntry, canvasMcpEntry) => {
3969
4056
  };
3970
4057
 
3971
4058
  // src/mcp/manage-variable-tool.ts
4059
+ var VARIABLE_TYPES = {
4060
+ COLOR: "global-color-variable",
4061
+ FONT: "global-font-variable",
4062
+ SIZE: "global-size-variable",
4063
+ CUSTOM_SIZE: "global-custom-size-variable"
4064
+ };
4065
+ var LENGTH_UNIT_PATTERN = /^(auto|\d+(\.\d+)?(px|rem|em|vh|vw|%|ch|s|ms))$/i;
4066
+ var COLOR_PATTERN = /^(#[0-9a-f]{3,8}|rgba?\(|hsl)/i;
4067
+ function validateValueForType(type, value) {
4068
+ if (type === VARIABLE_TYPES.FONT && LENGTH_UNIT_PATTERN.test(value.trim())) {
4069
+ return `Font variable value must be a font family name (e.g. "Roboto"), not a size value like "${value}". Use "global-size-variable" or "global-custom-size-variable" for spacing/size values.`;
4070
+ }
4071
+ if (type === VARIABLE_TYPES.COLOR && !COLOR_PATTERN.test(value.trim())) {
4072
+ return `Color variable value should be a CSS color (e.g. "#FF0000"), got "${value}".`;
4073
+ }
4074
+ if (type === VARIABLE_TYPES.SIZE && !LENGTH_UNIT_PATTERN.test(value.trim())) {
4075
+ return `Size variable value should include a CSS unit (e.g. "16px") or be "auto", got "${value}".`;
4076
+ }
4077
+ return null;
4078
+ }
3972
4079
  var initManageVariableTool = (reg) => {
3973
- const { addTool } = reg;
4080
+ const { addTool, resource } = reg;
4081
+ resource(
4082
+ "manage-global-variable-guide",
4083
+ MANAGE_VARIABLES_GUIDE_URI,
4084
+ {
4085
+ title: "Manage Global Variable Guide",
4086
+ description: "Detailed guide for using the manage-global-variable tool",
4087
+ mimeType: "text/plain"
4088
+ },
4089
+ async (uri) => ({
4090
+ contents: [{ uri: uri.href, mimeType: "text/plain", text: generateVariablesPrompt() }]
4091
+ })
4092
+ );
3974
4093
  addTool({
3975
4094
  name: "manage-global-variable",
4095
+ description: "Manage V4 global variables (color, font, size). Read the guide resource before use.",
3976
4096
  schema: {
3977
4097
  action: z3.enum(["create", "update", "delete"]).describe("Operation to perform"),
3978
- id: z3.string().optional().describe("Variable id (required for update/delete). Get from list-global-variables."),
3979
- type: z3.string().optional().describe('Variable type: "global-color-variable" or "global-font-variable" (required for create)'),
3980
- label: z3.string().optional().describe("Variable label (required for create/update)"),
4098
+ id: z3.string().optional().describe("Variable id \u2014 required for update/delete. Get from the global-variables resource."),
4099
+ type: z3.string().optional().describe(
4100
+ 'Variable type \u2014 required for create. One of: "global-color-variable", "global-font-variable", "global-size-variable", "global-custom-size-variable" (size types require Elementor Pro). NEVER store px/rem values in a font variable.'
4101
+ ),
4102
+ label: z3.string().optional().describe("Variable label (lowercase, dash-separated) \u2014 required for create/update."),
3981
4103
  value: z3.string().optional().describe(
3982
- "The variable value (required for create/update). Provide a plain CSS value matching the variable type (font: family name; color: CSS color; size: value with unit). Never JSON."
4104
+ 'Plain CSS value \u2014 required for create/update. Color: hex/rgba/hsl. Font: family name only, never px/rem. Size: value with unit e.g. "16px", or "auto" (Pro). Do NOT pass JSON.'
3983
4105
  )
3984
4106
  },
3985
4107
  outputSchema: {
@@ -3991,29 +4113,22 @@ var initManageVariableTool = (reg) => {
3991
4113
  speedPriority: 0.75
3992
4114
  },
3993
4115
  requiredResources: [
4116
+ { uri: MANAGE_VARIABLES_GUIDE_URI, description: "Full guide for variable types, naming rules, and usage" },
3994
4117
  {
3995
4118
  uri: GLOBAL_VARIABLES_URI,
3996
- description: "Global variables"
4119
+ description: "Current global variables \u2014 check before creating to avoid duplicates"
3997
4120
  }
3998
4121
  ],
3999
- description: `Create, update, or delete V4 global variables (distinct from legacy "globals").
4000
- - Values: any valid CSS value, inserted as-is (1:1 with \`--css-var: VALUE\`). Do NOT pass JSON or legacy-globals object structures.
4001
- - Names: lowercase, dash-separated (e.g. "Headline Primary" \u2192 "headline-primary").
4002
- - Update: when renaming, keep the existing value; when updating value, keep the exact label.
4003
- - Delete: destructive \u2014 confirm with user first.`,
4122
+ isDestructive: true,
4004
4123
  handler: async (params) => {
4005
4124
  const operations = getServiceActions(service);
4006
4125
  const op = operations[params.action];
4007
4126
  if (op) {
4008
4127
  await op(params);
4009
- return {
4010
- status: "ok"
4011
- };
4128
+ return { status: "ok" };
4012
4129
  }
4013
4130
  throw new Error(`Unknown action ${params.action}`);
4014
- },
4015
- isDestructive: true
4016
- // Because delete is destructive
4131
+ }
4017
4132
  });
4018
4133
  };
4019
4134
  function getServiceActions(svc) {
@@ -4022,10 +4137,17 @@ function getServiceActions(svc) {
4022
4137
  if (!type || !label || !value) {
4023
4138
  throw new Error("Create requires type, label, and value");
4024
4139
  }
4140
+ if ((type === VARIABLE_TYPES.SIZE || type === VARIABLE_TYPES.CUSTOM_SIZE) && !isProActive2()) {
4141
+ throw new Error("Creating size variables requires Elementor Pro.");
4142
+ }
4025
4143
  const labelError = validateLabel(label);
4026
4144
  if (labelError) {
4027
4145
  throw new Error(labelError);
4028
4146
  }
4147
+ const valueError = validateValueForType(type, value);
4148
+ if (valueError) {
4149
+ throw new Error(valueError);
4150
+ }
4029
4151
  return svc.create({ type, label, value });
4030
4152
  },
4031
4153
  update({ id: id2, label, value }) {
@@ -4036,6 +4158,13 @@ function getServiceActions(svc) {
4036
4158
  if (labelError) {
4037
4159
  throw new Error(labelError);
4038
4160
  }
4161
+ const existingVariable = svc.variables()[id2];
4162
+ if (existingVariable) {
4163
+ const valueError = validateValueForType(existingVariable.type, value);
4164
+ if (valueError) {
4165
+ throw new Error(valueError);
4166
+ }
4167
+ }
4039
4168
  return svc.update(id2, { label, value });
4040
4169
  },
4041
4170
  delete({ id: id2 }) {