@elementor/editor-variables 3.33.0-104 → 3.33.0-105
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 +120 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +121 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -12
- package/src/api.ts +24 -0
- package/src/batch-operations.ts +83 -0
- package/src/components/variables-manager/variables-manager-panel.tsx +31 -2
- package/src/service.ts +51 -0
package/dist/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { __registerPanel as registerPanel } from "@elementor/editor-panels";
|
|
|
5
5
|
|
|
6
6
|
// src/components/variables-manager/variables-manager-panel.tsx
|
|
7
7
|
import * as React8 from "react";
|
|
8
|
-
import { useEffect, useState as useState4 } from "react";
|
|
8
|
+
import { useCallback, useEffect, useState as useState4 } from "react";
|
|
9
9
|
import {
|
|
10
10
|
__createPanel as createPanel,
|
|
11
11
|
Panel,
|
|
@@ -86,9 +86,66 @@ var apiClient = {
|
|
|
86
86
|
payload.value = value;
|
|
87
87
|
}
|
|
88
88
|
return httpService().post(BASE_PATH + "/restore", payload);
|
|
89
|
+
},
|
|
90
|
+
batch: (payload) => {
|
|
91
|
+
return httpService().post(BASE_PATH + "/batch", payload);
|
|
89
92
|
}
|
|
90
93
|
};
|
|
91
94
|
|
|
95
|
+
// src/batch-operations.ts
|
|
96
|
+
var isTempId = (id2) => {
|
|
97
|
+
return id2.startsWith("tmp-");
|
|
98
|
+
};
|
|
99
|
+
var buildOperationsArray = (originalVariables, currentVariables) => {
|
|
100
|
+
const operations = [];
|
|
101
|
+
Object.entries(currentVariables).forEach(([id2, variable]) => {
|
|
102
|
+
if (isTempId(id2)) {
|
|
103
|
+
operations.push({
|
|
104
|
+
type: "create",
|
|
105
|
+
variable: {
|
|
106
|
+
...variable,
|
|
107
|
+
id: id2
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
} else if (originalVariables[id2]) {
|
|
111
|
+
const original = originalVariables[id2];
|
|
112
|
+
if (original.deleted && !variable.deleted) {
|
|
113
|
+
operations.push({
|
|
114
|
+
type: "restore",
|
|
115
|
+
id: id2,
|
|
116
|
+
...original.label !== variable.label && { label: variable.label },
|
|
117
|
+
...original.value !== variable.value && { value: variable.value }
|
|
118
|
+
});
|
|
119
|
+
} else if (!variable.deleted && (original.label !== variable.label || original.value !== variable.value)) {
|
|
120
|
+
operations.push({
|
|
121
|
+
type: "update",
|
|
122
|
+
id: id2,
|
|
123
|
+
variable: {
|
|
124
|
+
...original.label !== variable.label && { label: variable.label },
|
|
125
|
+
...original.value !== variable.value && { value: variable.value }
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
Object.entries(originalVariables).forEach(([id2, variable]) => {
|
|
132
|
+
if (isTempId(id2) || variable.deleted) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const currentVariable = currentVariables[id2];
|
|
136
|
+
if (!currentVariable || currentVariable.deleted) {
|
|
137
|
+
operations.push({
|
|
138
|
+
type: "delete",
|
|
139
|
+
id: id2
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
return operations.filter((op) => {
|
|
144
|
+
const id2 = op.id || op.variable?.id;
|
|
145
|
+
return id2 && !(isTempId(id2) && currentVariables[id2]?.deleted);
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
|
|
92
149
|
// src/storage.ts
|
|
93
150
|
var STORAGE_KEY = "elementor-global-variables";
|
|
94
151
|
var STORAGE_WATERMARK_KEY = "elementor-global-variables-watermark";
|
|
@@ -229,6 +286,9 @@ var service = {
|
|
|
229
286
|
variables: () => {
|
|
230
287
|
return storage.load();
|
|
231
288
|
},
|
|
289
|
+
getWatermark: () => {
|
|
290
|
+
return storage.state.watermark;
|
|
291
|
+
},
|
|
232
292
|
init: () => {
|
|
233
293
|
service.load();
|
|
234
294
|
},
|
|
@@ -331,6 +391,40 @@ var service = {
|
|
|
331
391
|
variable: restoredVariable
|
|
332
392
|
};
|
|
333
393
|
});
|
|
394
|
+
},
|
|
395
|
+
batchSave: (originalVariables, currentVariables) => {
|
|
396
|
+
const operations = buildOperationsArray(originalVariables, currentVariables);
|
|
397
|
+
const batchPayload = { operations, watermark: storage.state.watermark };
|
|
398
|
+
return apiClient.batch(batchPayload).then((response) => {
|
|
399
|
+
const { success, data: payload } = response.data;
|
|
400
|
+
if (!success) {
|
|
401
|
+
throw new Error("Unexpected response from server");
|
|
402
|
+
}
|
|
403
|
+
return payload;
|
|
404
|
+
}).then((data) => {
|
|
405
|
+
const { results, watermark } = data;
|
|
406
|
+
handleWatermark(OP_RW, watermark);
|
|
407
|
+
if (results) {
|
|
408
|
+
results.forEach((result) => {
|
|
409
|
+
if (result.variable) {
|
|
410
|
+
const { id: variableId, ...variableData } = result.variable;
|
|
411
|
+
if (result.type === "create") {
|
|
412
|
+
storage.add(variableId, variableData);
|
|
413
|
+
} else {
|
|
414
|
+
storage.update(variableId, variableData);
|
|
415
|
+
}
|
|
416
|
+
styleVariablesRepository.update({
|
|
417
|
+
[variableId]: variableData
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
return {
|
|
423
|
+
success: true,
|
|
424
|
+
watermark,
|
|
425
|
+
operations: operations.length
|
|
426
|
+
};
|
|
427
|
+
});
|
|
334
428
|
}
|
|
335
429
|
};
|
|
336
430
|
var handleWatermark = (operation, newWatermark) => {
|
|
@@ -978,7 +1072,21 @@ function VariablesManagerPanel() {
|
|
|
978
1072
|
const [isDirty, setIsDirty] = useState4(false);
|
|
979
1073
|
const [variables, setVariables] = useState4(getVariables(false));
|
|
980
1074
|
const [deletedVariables, setDeletedVariables] = useState4([]);
|
|
1075
|
+
const [isSaving, setIsSaving] = useState4(false);
|
|
981
1076
|
usePreventUnload(isDirty);
|
|
1077
|
+
const handleSave = useCallback(async () => {
|
|
1078
|
+
setIsSaving(true);
|
|
1079
|
+
const originalVariables = getVariables(false);
|
|
1080
|
+
const result = await service.batchSave(originalVariables, variables);
|
|
1081
|
+
if (result.success) {
|
|
1082
|
+
await service.load();
|
|
1083
|
+
const updatedVariables = service.variables();
|
|
1084
|
+
setVariables(updatedVariables);
|
|
1085
|
+
setIsDirty(false);
|
|
1086
|
+
setDeletedVariables([]);
|
|
1087
|
+
}
|
|
1088
|
+
setIsSaving(false);
|
|
1089
|
+
}, [variables]);
|
|
982
1090
|
const menuActions = [
|
|
983
1091
|
{
|
|
984
1092
|
name: __5("Delete", "elementor"),
|
|
@@ -1020,7 +1128,18 @@ function VariablesManagerPanel() {
|
|
|
1020
1128
|
onChange: handleOnChange
|
|
1021
1129
|
}
|
|
1022
1130
|
)
|
|
1023
|
-
), /* @__PURE__ */ React8.createElement(PanelFooter, null, /* @__PURE__ */ React8.createElement(
|
|
1131
|
+
), /* @__PURE__ */ React8.createElement(PanelFooter, null, /* @__PURE__ */ React8.createElement(
|
|
1132
|
+
Button,
|
|
1133
|
+
{
|
|
1134
|
+
fullWidth: true,
|
|
1135
|
+
size: "small",
|
|
1136
|
+
color: "global",
|
|
1137
|
+
variant: "contained",
|
|
1138
|
+
disabled: !isDirty || isSaving,
|
|
1139
|
+
onClick: handleSave
|
|
1140
|
+
},
|
|
1141
|
+
__5("Save changes", "elementor")
|
|
1142
|
+
)))));
|
|
1024
1143
|
}
|
|
1025
1144
|
var CloseButton = ({ onClose, ...props }) => /* @__PURE__ */ React8.createElement(IconButton3, { size: "small", color: "secondary", onClick: onClose, "aria-label": "Close", ...props }, /* @__PURE__ */ React8.createElement(XIcon, { fontSize: "small" }));
|
|
1026
1145
|
var ErrorBoundaryFallback = () => /* @__PURE__ */ React8.createElement(Box, { role: "alert", sx: { minHeight: "100%", p: 2 } }, /* @__PURE__ */ React8.createElement(Alert, { severity: "error", sx: { mb: 2, maxWidth: 400, textAlign: "center" } }, /* @__PURE__ */ React8.createElement("strong", null, __5("Something went wrong", "elementor"))));
|