@elementor/editor-variables 4.1.0-767 → 4.1.0-768

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
@@ -1062,6 +1062,30 @@ var restoreVariable = (restoreId, label, value, type) => {
1062
1062
  return service.restore(restoreId, label, value, type).then(extractId);
1063
1063
  };
1064
1064
 
1065
+ // src/utils/duplicate-label.ts
1066
+ var COPY_SUFFIX = "-Copy";
1067
+ var trimToFit = (base, suffix) => {
1068
+ const combined = base + suffix;
1069
+ if (combined.length <= VARIABLE_LABEL_MAX_LENGTH) {
1070
+ return combined;
1071
+ }
1072
+ return base.slice(0, VARIABLE_LABEL_MAX_LENGTH - suffix.length) + suffix;
1073
+ };
1074
+ var generateDuplicateLabel = (originalLabel, existingLabels) => {
1075
+ const labelsSet = new Set(existingLabels);
1076
+ const firstCandidate = trimToFit(originalLabel, COPY_SUFFIX);
1077
+ if (!labelsSet.has(firstCandidate)) {
1078
+ return firstCandidate;
1079
+ }
1080
+ for (let i = 2; i <= labelsSet.size + 1; i++) {
1081
+ const candidate = trimToFit(originalLabel, `${COPY_SUFFIX}-${i}`);
1082
+ if (!labelsSet.has(candidate)) {
1083
+ return candidate;
1084
+ }
1085
+ }
1086
+ return firstCandidate;
1087
+ };
1088
+
1065
1089
  // src/components/variables-manager/hooks/use-variables-manager-state.ts
1066
1090
  var useVariablesManagerState = () => {
1067
1091
  const [variables, setVariables] = (0, import_react6.useState)(() => getVariables(false));
@@ -1089,6 +1113,26 @@ var useVariablesManagerState = () => {
1089
1113
  setIsDirty(true);
1090
1114
  return newId;
1091
1115
  }, []);
1116
+ const duplicateVariable = (0, import_react6.useCallback)((sourceId) => {
1117
+ const newId = generateTempId();
1118
+ setVariables((prev) => {
1119
+ const source = prev[sourceId];
1120
+ if (!source || source.deleted) {
1121
+ return prev;
1122
+ }
1123
+ const existingLabels = Object.values(prev).filter((v) => !v.deleted).map((v) => v.label);
1124
+ return {
1125
+ ...prev,
1126
+ [newId]: {
1127
+ label: generateDuplicateLabel(source.label, existingLabels),
1128
+ value: source.value,
1129
+ type: source.type
1130
+ }
1131
+ };
1132
+ });
1133
+ setIsDirty(true);
1134
+ return newId;
1135
+ }, []);
1092
1136
  const handleDeleteVariable = (0, import_react6.useCallback)((itemId) => {
1093
1137
  setDeletedVariables((prev) => [...prev, itemId]);
1094
1138
  setVariables((prev) => ({ ...prev, [itemId]: { ...prev[itemId], deleted: true } }));
@@ -1137,6 +1181,7 @@ var useVariablesManagerState = () => {
1137
1181
  isSaveDisabled,
1138
1182
  handleOnChange,
1139
1183
  createVariable: createVariable2,
1184
+ duplicateVariable,
1140
1185
  handleDeleteVariable,
1141
1186
  handleStartSync,
1142
1187
  handleStopSync,
@@ -1952,6 +1997,7 @@ function VariablesManagerPanel() {
1952
1997
  isSaveDisabled,
1953
1998
  handleOnChange,
1954
1999
  createVariable: createVariable2,
2000
+ duplicateVariable,
1955
2001
  handleDeleteVariable,
1956
2002
  handleStartSync: startSyncFromState,
1957
2003
  handleStopSync: stopSyncFromState,
@@ -2059,6 +2105,20 @@ function VariablesManagerPanel() {
2059
2105
  onStopSync: handleStopSync
2060
2106
  }
2061
2107
  });
2108
+ const duplicateAction = {
2109
+ name: (0, import_i18n10.__)("Duplicate", "elementor"),
2110
+ icon: import_icons5.CopyIcon,
2111
+ color: "text.primary",
2112
+ onClick: (itemId) => {
2113
+ const newId = duplicateVariable(itemId);
2114
+ startAutoEdit(newId);
2115
+ const variableTypeOptions = getVariableType(variable.type);
2116
+ trackVariablesManagerEvent({
2117
+ action: "duplicate",
2118
+ varType: variableTypeOptions?.variableType
2119
+ });
2120
+ }
2121
+ };
2062
2122
  const deleteAction = {
2063
2123
  name: (0, import_i18n10.__)("Delete", "elementor"),
2064
2124
  icon: import_icons5.TrashIcon,
@@ -2072,9 +2132,9 @@ function VariablesManagerPanel() {
2072
2132
  }
2073
2133
  }
2074
2134
  };
2075
- return [...typeActions, deleteAction];
2135
+ return [...typeActions, duplicateAction, deleteAction];
2076
2136
  },
2077
- [variables, handleStartSync, handleStopSync]
2137
+ [variables, handleStartSync, handleStopSync, duplicateVariable, startAutoEdit]
2078
2138
  );
2079
2139
  const hasVariables = Object.keys(variables).length > 0;
2080
2140
  return /* @__PURE__ */ React14.createElement(import_editor_ui5.ThemeProvider, null, /* @__PURE__ */ React14.createElement(import_editor_panels.Panel, null, /* @__PURE__ */ React14.createElement(