@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.js CHANGED
@@ -1073,8 +1073,17 @@ var useVariablesManagerState = () => {
1073
1073
  }, []);
1074
1074
  const handleOnChange = (0, import_react5.useCallback)(
1075
1075
  (newVariables) => {
1076
- setVariables({ ...variables, ...newVariables });
1077
- setIsDirty(true);
1076
+ const hasChanges = Object.entries(newVariables).some(([id2, newVar]) => {
1077
+ const existingVar = variables[id2];
1078
+ if (!existingVar) {
1079
+ return true;
1080
+ }
1081
+ 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);
1082
+ });
1083
+ if (hasChanges) {
1084
+ setVariables({ ...variables, ...newVariables });
1085
+ setIsDirty(true);
1086
+ }
1078
1087
  },
1079
1088
  [variables]
1080
1089
  );
@@ -1804,7 +1813,7 @@ var VariableRow = (props) => {
1804
1813
  value,
1805
1814
  onChange,
1806
1815
  onPropTypeKeyChange: (type) => {
1807
- if (!isDisabled) {
1816
+ if (!isDisabled && type !== row.type) {
1808
1817
  handleOnChange({
1809
1818
  ...variables,
1810
1819
  [row.id]: { ...variables[row.id], type }
@@ -2412,7 +2421,7 @@ var StopSyncConfirmationDialog = ({ open, onClose, onConfirm }) => {
2412
2421
  // src/init.ts
2413
2422
  var import_editor = require("@elementor/editor");
2414
2423
  var import_editor_controls18 = require("@elementor/editor-controls");
2415
- var import_editor_mcp = require("@elementor/editor-mcp");
2424
+ var import_editor_mcp2 = require("@elementor/editor-mcp");
2416
2425
  var import_editor_panels2 = require("@elementor/editor-panels");
2417
2426
  var import_editor_props10 = require("@elementor/editor-props");
2418
2427
  var import_editor_v1_adapters8 = require("@elementor/editor-v1-adapters");
@@ -3924,6 +3933,84 @@ var trackOpenVariablePopover = (path, variableType) => {
3924
3933
 
3925
3934
  // src/mcp/manage-variable-tool.ts
3926
3935
  var import_schema3 = require("@elementor/schema");
3936
+ var import_utils4 = require("@elementor/utils");
3937
+
3938
+ // src/mcp/variable-tool-prompt.ts
3939
+ var import_editor_mcp = require("@elementor/editor-mcp");
3940
+ var import_utils3 = require("@elementor/utils");
3941
+ var MANAGE_VARIABLES_GUIDE_URI = "elementor://variables/tools/manage-global-variable-guide";
3942
+ var generateVariablesPrompt = () => {
3943
+ const prompt = (0, import_editor_mcp.toolPrompts)("manage-global-variable");
3944
+ const proIsActive = (0, import_utils3.isProActive)();
3945
+ 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\`
3946
+ - **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)
3947
+ - ~~global-custom-size-variable~~ \u2014 requires Elementor Pro (not available on this site)`;
3948
+ prompt.description(`
3949
+ # Purpose
3950
+ Create, update, or delete V4 global CSS variables. These are distinct from legacy v3 globals and map 1:1 to \`--css-var: VALUE\`.
3951
+
3952
+ # Available Types
3953
+ - **global-color-variable** \u2014 CSS color value. Example: \`#FF0000\`, \`rgba(255,0,0,1)\`, \`hsl(0,100%,50%)\`
3954
+ - **global-font-variable** \u2014 Font family name ONLY \u2014 NOT a size or px value. Example: \`Roboto\`, \`Open Sans\`. NEVER pass px/rem here.
3955
+ ${sizeVariableSection}
3956
+
3957
+ # Naming Rules
3958
+ - Labels must be **lowercase**, using only letters (a-z), numbers, digits (0-9), dashes (-), or underscores (_)
3959
+ - No spaces, no special characters
3960
+ - Example: "Headline Primary" \u2192 \`headline-primary\`
3961
+ - Labels must be unique \u2014 always check [elementor://global-variables] first
3962
+
3963
+ # Value Rules
3964
+ - Provide a **plain CSS value** only \u2014 do NOT pass JSON, legacy-globals object structures, or variable references
3965
+ - Values are inserted as-is: \`--css-var: <value>\`
3966
+ - NEVER store a px/rem value inside a \`global-font-variable\` \u2014 use \`global-size-variable\` (Pro) instead
3967
+
3968
+ # Operations
3969
+ - **create** \u2014 requires \`type\`, \`label\`, \`value\`. Label must be unique.
3970
+ - **update** \u2014 requires \`id\`, \`label\`, \`value\`. Get \`id\` from [elementor://global-variables]. When renaming: keep existing value. When changing value: keep exact existing label.
3971
+ - **delete** \u2014 requires \`id\`. DESTRUCTIVE \u2014 always confirm with user before executing.
3972
+ `);
3973
+ prompt.parameter("action", '"create", "update", or "delete".');
3974
+ prompt.parameter("type", "Variable type. Required for create. See Available Types above.");
3975
+ prompt.parameter("label", "Variable name (lowercase, dash-separated). Required for create/update.");
3976
+ prompt.parameter(
3977
+ "value",
3978
+ "Plain CSS value matching the variable type. Required for create/update. Do NOT pass JSON."
3979
+ );
3980
+ prompt.parameter("id", "Variable ID. Required for update/delete. Obtain from [elementor://global-variables].");
3981
+ prompt.example(`
3982
+ Create a brand color:
3983
+ { "action": "create", "type": "global-color-variable", "label": "brand-primary", "value": "#1A73E8" }
3984
+
3985
+ Create a heading font:
3986
+ { "action": "create", "type": "global-font-variable", "label": "font-heading", "value": "Playfair Display" }
3987
+
3988
+ Create a simple spacing size:
3989
+ { "action": "create", "type": "global-size-variable", "label": "spacing-md", "value": "16px" }
3990
+
3991
+ Create a fluid/responsive size using a CSS function (use global-custom-size-variable, NOT global-size-variable):
3992
+ { "action": "create", "type": "global-custom-size-variable", "label": "spacing-fluid", "value": "clamp(1rem, 2vw, 2rem)" }
3993
+
3994
+ Create a size that is a keyword:
3995
+ { "action": "create", "type": "global-custom-size-variable", "label": "width-auto", "value": "auto" }
3996
+
3997
+ Create a size using calc():
3998
+ { "action": "create", "type": "global-custom-size-variable", "label": "sidebar-width", "value": "calc(100% - 32px)" }
3999
+
4000
+ Update a variable's value (keep exact label):
4001
+ { "action": "update", "id": "abc123", "label": "brand-primary", "value": "#0D47A1" }
4002
+
4003
+ Rename a variable (keep existing value):
4004
+ { "action": "update", "id": "abc123", "label": "brand-secondary", "value": "#1A73E8" }
4005
+
4006
+ Delete a variable:
4007
+ { "action": "delete", "id": "abc123" }
4008
+ `);
4009
+ prompt.instruction(
4010
+ "Always read [elementor://global-variables] before creating to check existing variables and avoid duplicate labels."
4011
+ );
4012
+ return prompt.prompt();
4013
+ };
3927
4014
 
3928
4015
  // src/mcp/variables-resource.ts
3929
4016
  var import_editor_v1_adapters6 = require("@elementor/editor-v1-adapters");
@@ -3966,17 +4053,52 @@ var initVariablesResource = (variablesMcpEntry, canvasMcpEntry) => {
3966
4053
  };
3967
4054
 
3968
4055
  // src/mcp/manage-variable-tool.ts
4056
+ var VARIABLE_TYPES = {
4057
+ COLOR: "global-color-variable",
4058
+ FONT: "global-font-variable",
4059
+ SIZE: "global-size-variable",
4060
+ CUSTOM_SIZE: "global-custom-size-variable"
4061
+ };
4062
+ var LENGTH_UNIT_PATTERN = /^(auto|\d+(\.\d+)?(px|rem|em|vh|vw|%|ch|s|ms))$/i;
4063
+ var COLOR_PATTERN = /^(#[0-9a-f]{3,8}|rgba?\(|hsl)/i;
4064
+ function validateValueForType(type, value) {
4065
+ if (type === VARIABLE_TYPES.FONT && LENGTH_UNIT_PATTERN.test(value.trim())) {
4066
+ 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.`;
4067
+ }
4068
+ if (type === VARIABLE_TYPES.COLOR && !COLOR_PATTERN.test(value.trim())) {
4069
+ return `Color variable value should be a CSS color (e.g. "#FF0000"), got "${value}".`;
4070
+ }
4071
+ if (type === VARIABLE_TYPES.SIZE && !LENGTH_UNIT_PATTERN.test(value.trim())) {
4072
+ return `Size variable value should include a CSS unit (e.g. "16px") or be "auto", got "${value}".`;
4073
+ }
4074
+ return null;
4075
+ }
3969
4076
  var initManageVariableTool = (reg) => {
3970
- const { addTool } = reg;
4077
+ const { addTool, resource } = reg;
4078
+ resource(
4079
+ "manage-global-variable-guide",
4080
+ MANAGE_VARIABLES_GUIDE_URI,
4081
+ {
4082
+ title: "Manage Global Variable Guide",
4083
+ description: "Detailed guide for using the manage-global-variable tool",
4084
+ mimeType: "text/plain"
4085
+ },
4086
+ async (uri) => ({
4087
+ contents: [{ uri: uri.href, mimeType: "text/plain", text: generateVariablesPrompt() }]
4088
+ })
4089
+ );
3971
4090
  addTool({
3972
4091
  name: "manage-global-variable",
4092
+ description: "Manage V4 global variables (color, font, size). Read the guide resource before use.",
3973
4093
  schema: {
3974
4094
  action: import_schema3.z.enum(["create", "update", "delete"]).describe("Operation to perform"),
3975
- id: import_schema3.z.string().optional().describe("Variable id (required for update/delete). Get from list-global-variables."),
3976
- type: import_schema3.z.string().optional().describe('Variable type: "global-color-variable" or "global-font-variable" (required for create)'),
3977
- label: import_schema3.z.string().optional().describe("Variable label (required for create/update)"),
4095
+ id: import_schema3.z.string().optional().describe("Variable id \u2014 required for update/delete. Get from the global-variables resource."),
4096
+ type: import_schema3.z.string().optional().describe(
4097
+ '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.'
4098
+ ),
4099
+ label: import_schema3.z.string().optional().describe("Variable label (lowercase, dash-separated) \u2014 required for create/update."),
3978
4100
  value: import_schema3.z.string().optional().describe(
3979
- "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."
4101
+ '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.'
3980
4102
  )
3981
4103
  },
3982
4104
  outputSchema: {
@@ -3988,29 +4110,22 @@ var initManageVariableTool = (reg) => {
3988
4110
  speedPriority: 0.75
3989
4111
  },
3990
4112
  requiredResources: [
4113
+ { uri: MANAGE_VARIABLES_GUIDE_URI, description: "Full guide for variable types, naming rules, and usage" },
3991
4114
  {
3992
4115
  uri: GLOBAL_VARIABLES_URI,
3993
- description: "Global variables"
4116
+ description: "Current global variables \u2014 check before creating to avoid duplicates"
3994
4117
  }
3995
4118
  ],
3996
- description: `Create, update, or delete V4 global variables (distinct from legacy "globals").
3997
- - Values: any valid CSS value, inserted as-is (1:1 with \`--css-var: VALUE\`). Do NOT pass JSON or legacy-globals object structures.
3998
- - Names: lowercase, dash-separated (e.g. "Headline Primary" \u2192 "headline-primary").
3999
- - Update: when renaming, keep the existing value; when updating value, keep the exact label.
4000
- - Delete: destructive \u2014 confirm with user first.`,
4119
+ isDestructive: true,
4001
4120
  handler: async (params) => {
4002
4121
  const operations = getServiceActions(service);
4003
4122
  const op = operations[params.action];
4004
4123
  if (op) {
4005
4124
  await op(params);
4006
- return {
4007
- status: "ok"
4008
- };
4125
+ return { status: "ok" };
4009
4126
  }
4010
4127
  throw new Error(`Unknown action ${params.action}`);
4011
- },
4012
- isDestructive: true
4013
- // Because delete is destructive
4128
+ }
4014
4129
  });
4015
4130
  };
4016
4131
  function getServiceActions(svc) {
@@ -4019,10 +4134,17 @@ function getServiceActions(svc) {
4019
4134
  if (!type || !label || !value) {
4020
4135
  throw new Error("Create requires type, label, and value");
4021
4136
  }
4137
+ if ((type === VARIABLE_TYPES.SIZE || type === VARIABLE_TYPES.CUSTOM_SIZE) && !(0, import_utils4.isProActive)()) {
4138
+ throw new Error("Creating size variables requires Elementor Pro.");
4139
+ }
4022
4140
  const labelError = validateLabel(label);
4023
4141
  if (labelError) {
4024
4142
  throw new Error(labelError);
4025
4143
  }
4144
+ const valueError = validateValueForType(type, value);
4145
+ if (valueError) {
4146
+ throw new Error(valueError);
4147
+ }
4026
4148
  return svc.create({ type, label, value });
4027
4149
  },
4028
4150
  update({ id: id2, label, value }) {
@@ -4033,6 +4155,13 @@ function getServiceActions(svc) {
4033
4155
  if (labelError) {
4034
4156
  throw new Error(labelError);
4035
4157
  }
4158
+ const existingVariable = svc.variables()[id2];
4159
+ if (existingVariable) {
4160
+ const valueError = validateValueForType(existingVariable.type, value);
4161
+ if (valueError) {
4162
+ throw new Error(valueError);
4163
+ }
4164
+ }
4036
4165
  return svc.update(id2, { label, value });
4037
4166
  },
4038
4167
  delete({ id: id2 }) {
@@ -4557,7 +4686,7 @@ function init() {
4557
4686
  useProps: usePropVariableAction
4558
4687
  });
4559
4688
  service.init().then(() => {
4560
- const variablesMcpRegistry = (0, import_editor_mcp.getMCPByDomain)("variables", {
4689
+ const variablesMcpRegistry = (0, import_editor_mcp2.getMCPByDomain)("variables", {
4561
4690
  instructions: `Everything related to V4 ( Atomic ) variables.
4562
4691
  # Global variables
4563
4692
  - Create/update/delete global variables
@@ -4565,7 +4694,7 @@ function init() {
4565
4694
  - Get details of a global variable
4566
4695
  `
4567
4696
  });
4568
- initMcp(variablesMcpRegistry, (0, import_editor_mcp.getMCPByDomain)("canvas"));
4697
+ initMcp(variablesMcpRegistry, (0, import_editor_mcp2.getMCPByDomain)("canvas"));
4569
4698
  });
4570
4699
  (0, import_editor.injectIntoTop)({
4571
4700
  id: "canvas-style-variables-render",