@elementor/editor-elements 4.3.0-974 → 4.3.0-975
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.d.mts +43 -2
- package/dist/index.d.ts +43 -2
- package/dist/index.js +248 -50
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +231 -37
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -7
- package/src/children-dependencies/bind-settings-reconcile.ts +148 -0
- package/src/children-dependencies/index.ts +4 -0
- package/src/children-dependencies/reconcile-initial-children.ts +60 -0
- package/src/children-dependencies/stash.ts +40 -0
- package/src/children-dependencies/types.ts +20 -0
- package/src/children-dependencies/utils.ts +5 -0
- package/src/index.ts +10 -0
- package/src/sync/resolve-insert-index.ts +34 -0
- package/src/sync/types.ts +11 -0
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// src/hooks/use-element-children.ts
|
|
2
|
-
import { __privateUseListenTo as useListenTo, commandEndEvent, v1ReadyEvent } from "@elementor/editor-v1-adapters";
|
|
3
|
-
|
|
4
1
|
// src/sync/get-container.ts
|
|
5
2
|
import { __privateRunCommand as runCommand } from "@elementor/editor-v1-adapters";
|
|
6
3
|
function getContainer(id) {
|
|
@@ -16,6 +13,233 @@ var selectElement = (elementId) => {
|
|
|
16
13
|
}
|
|
17
14
|
};
|
|
18
15
|
|
|
16
|
+
// src/sync/resolve-element.ts
|
|
17
|
+
function isConnected(container) {
|
|
18
|
+
if (!container) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (!container.view?.el) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return container.view.el.isConnected;
|
|
25
|
+
}
|
|
26
|
+
function resolveContainer(container, id) {
|
|
27
|
+
const looked = container.lookup?.();
|
|
28
|
+
if (isConnected(looked)) {
|
|
29
|
+
return looked;
|
|
30
|
+
}
|
|
31
|
+
const byId = getContainer(id);
|
|
32
|
+
if (isConnected(byId)) {
|
|
33
|
+
return byId;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function getDocumentUtils() {
|
|
38
|
+
return window.$e?.components?.get?.("document")?.utils;
|
|
39
|
+
}
|
|
40
|
+
function findModelInDocument(id) {
|
|
41
|
+
return getDocumentUtils()?.findModelById?.(id) ?? null;
|
|
42
|
+
}
|
|
43
|
+
function addModelToParent(parentId, childData, options) {
|
|
44
|
+
return getDocumentUtils()?.addModelToParent?.(parentId, childData, options) ?? false;
|
|
45
|
+
}
|
|
46
|
+
function removeModelFromParent(parentId, childId) {
|
|
47
|
+
return getDocumentUtils()?.removeModelFromParent?.(parentId, childId) ?? false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/sync/resolve-insert-index.ts
|
|
51
|
+
function resolveInsertIndex(position, elements) {
|
|
52
|
+
const lastIndex = elements.length;
|
|
53
|
+
switch (position.kind) {
|
|
54
|
+
case "first":
|
|
55
|
+
return 0;
|
|
56
|
+
case "last":
|
|
57
|
+
return lastIndex;
|
|
58
|
+
case "index": {
|
|
59
|
+
const index = typeof position.value === "number" ? position.value : lastIndex;
|
|
60
|
+
return Math.max(0, Math.min(index, lastIndex));
|
|
61
|
+
}
|
|
62
|
+
case "after_type": {
|
|
63
|
+
const anchor = elements.findIndex((element) => element.elType === position.value);
|
|
64
|
+
return anchor >= 0 ? anchor + 1 : lastIndex;
|
|
65
|
+
}
|
|
66
|
+
case "before_type": {
|
|
67
|
+
const anchor = elements.findIndex((element) => element.elType === position.value);
|
|
68
|
+
return anchor >= 0 ? anchor : lastIndex;
|
|
69
|
+
}
|
|
70
|
+
default:
|
|
71
|
+
return lastIndex;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/children-dependencies/stash.ts
|
|
76
|
+
import { getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem } from "@elementor/session";
|
|
77
|
+
var STASH_KEY_PREFIX = "elementor/editor-state";
|
|
78
|
+
var STASH_KEY_SEGMENT = "children-deps";
|
|
79
|
+
function createChildrenStash() {
|
|
80
|
+
return {
|
|
81
|
+
get(elementId, childType) {
|
|
82
|
+
return getSessionStorageItem(buildStashKey(elementId, childType));
|
|
83
|
+
},
|
|
84
|
+
save(elementId, childType, data) {
|
|
85
|
+
setSessionStorageItem(buildStashKey(elementId, childType), data);
|
|
86
|
+
},
|
|
87
|
+
clear(elementId, childType) {
|
|
88
|
+
removeSessionStorageItem(buildStashKey(elementId, childType));
|
|
89
|
+
},
|
|
90
|
+
clearAllForElement(elementId) {
|
|
91
|
+
const prefix = buildElementStashPrefix(elementId);
|
|
92
|
+
for (let index = sessionStorage.length - 1; index >= 0; index--) {
|
|
93
|
+
const key = sessionStorage.key(index);
|
|
94
|
+
if (key?.startsWith(prefix)) {
|
|
95
|
+
removeSessionStorageItem(key);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function buildStashKey(elementId, childType) {
|
|
102
|
+
return `${STASH_KEY_PREFIX}/${elementId}/${STASH_KEY_SEGMENT}/${childType}`;
|
|
103
|
+
}
|
|
104
|
+
function buildElementStashPrefix(elementId) {
|
|
105
|
+
return `${STASH_KEY_PREFIX}/${elementId}/${STASH_KEY_SEGMENT}/`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/children-dependencies/utils.ts
|
|
109
|
+
import { isDependencyMet } from "@elementor/editor-props";
|
|
110
|
+
function evaluateWhen(when, settings) {
|
|
111
|
+
return isDependencyMet(when, settings).isMet;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/children-dependencies/bind-settings-reconcile.ts
|
|
115
|
+
function bindSettingsReconcile({ model, elementConfig }) {
|
|
116
|
+
const stash = createChildrenStash();
|
|
117
|
+
const rules = elementConfig?.children_dependencies;
|
|
118
|
+
if (!rules?.length) {
|
|
119
|
+
return () => {
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const settingsModel = model.get("settings");
|
|
123
|
+
const elementId = model.get("id");
|
|
124
|
+
if (!settingsModel?.on || !settingsModel?.off || !elementId) {
|
|
125
|
+
return () => {
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
const lastMet = /* @__PURE__ */ new Map();
|
|
129
|
+
rules.forEach((rule) => {
|
|
130
|
+
lastMet.set(rule.child_type, evaluateWhen(rule.when, settingsModel.toJSON()));
|
|
131
|
+
});
|
|
132
|
+
const onChange = () => {
|
|
133
|
+
const currentSettings = settingsModel.toJSON();
|
|
134
|
+
rules.forEach((rule) => {
|
|
135
|
+
const previous = lastMet.get(rule.child_type) ?? false;
|
|
136
|
+
const current = evaluateWhen(rule.when, currentSettings);
|
|
137
|
+
if (previous === current) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
lastMet.set(rule.child_type, current);
|
|
141
|
+
if (current) {
|
|
142
|
+
attachChildFromRule(elementId, rule, stash);
|
|
143
|
+
} else {
|
|
144
|
+
detachChildFromRule(elementId, rule, stash);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
settingsModel.on("change", onChange);
|
|
149
|
+
return () => {
|
|
150
|
+
settingsModel.off?.("change", onChange);
|
|
151
|
+
stash.clearAllForElement(elementId);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function attachChildFromRule(parentId, rule, stash) {
|
|
155
|
+
const parent = getContainer(parentId) ?? void 0;
|
|
156
|
+
const currentChildren = getDirectChildData(parent);
|
|
157
|
+
const alreadyPresent = currentChildren.some((child) => child.elType === rule.child_type);
|
|
158
|
+
if (alreadyPresent) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const stashed = rule.stash ? stash.get(parentId, rule.child_type) : void 0;
|
|
162
|
+
const modelData = stashed ?? rule.default_model ?? { elType: rule.child_type };
|
|
163
|
+
const insertAt = resolveInsertIndex(rule.position, currentChildren);
|
|
164
|
+
const attached = addModelToParent(parentId, modelData, { at: insertAt });
|
|
165
|
+
if (!attached) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (stashed) {
|
|
169
|
+
stash.clear(parentId, rule.child_type);
|
|
170
|
+
}
|
|
171
|
+
requestNavigatorRefresh(parentId);
|
|
172
|
+
}
|
|
173
|
+
function detachChildFromRule(parentId, rule, stash) {
|
|
174
|
+
const parent = getContainer(parentId) ?? void 0;
|
|
175
|
+
const child = parent?.children?.find((candidate) => candidate.model.get("elType") === rule.child_type);
|
|
176
|
+
if (!child) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const childSnapshot = child.model.toJSON();
|
|
180
|
+
const removed = removeModelFromParent(parentId, child.id);
|
|
181
|
+
if (!removed) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (rule.stash) {
|
|
185
|
+
stash.save(parentId, rule.child_type, childSnapshot);
|
|
186
|
+
}
|
|
187
|
+
requestNavigatorRefresh(parentId);
|
|
188
|
+
}
|
|
189
|
+
function requestNavigatorRefresh(parentId) {
|
|
190
|
+
if (typeof window === "undefined" || typeof window.dispatchEvent !== "function") {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
window.dispatchEvent(
|
|
194
|
+
new CustomEvent("elementor/navigator/refresh-children", {
|
|
195
|
+
detail: { elementId: parentId }
|
|
196
|
+
})
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
function getDirectChildData(parent) {
|
|
200
|
+
return (parent?.children ?? []).map((child) => child.model.toJSON());
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/children-dependencies/reconcile-initial-children.ts
|
|
204
|
+
function reconcileInitialChildren({
|
|
205
|
+
elementId,
|
|
206
|
+
elementConfig,
|
|
207
|
+
attributes
|
|
208
|
+
}) {
|
|
209
|
+
const stash = createChildrenStash();
|
|
210
|
+
const rules = elementConfig?.children_dependencies;
|
|
211
|
+
if (!rules?.length) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const elements = [...attributes.elements ?? []];
|
|
215
|
+
const settings = attributes.settings ?? {};
|
|
216
|
+
rules.forEach((rule) => {
|
|
217
|
+
const isMet = evaluateWhen(rule.when, settings);
|
|
218
|
+
const existingIndex = elements.findIndex((element) => element.elType === rule.child_type);
|
|
219
|
+
const isPresent = existingIndex >= 0;
|
|
220
|
+
if (isMet && !isPresent) {
|
|
221
|
+
const stashed = rule.stash ? stash.get(elementId, rule.child_type) : void 0;
|
|
222
|
+
const modelData = stashed ?? rule.default_model ?? { elType: rule.child_type };
|
|
223
|
+
const insertAt = resolveInsertIndex(rule.position, elements);
|
|
224
|
+
elements.splice(insertAt, 0, modelData);
|
|
225
|
+
if (stashed) {
|
|
226
|
+
stash.clear(elementId, rule.child_type);
|
|
227
|
+
}
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (!isMet && isPresent) {
|
|
231
|
+
const [removed] = elements.splice(existingIndex, 1);
|
|
232
|
+
if (rule.stash && removed) {
|
|
233
|
+
stash.save(elementId, rule.child_type, removed);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
attributes.elements = elements;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// src/hooks/use-element-children.ts
|
|
241
|
+
import { __privateUseListenTo as useListenTo, commandEndEvent, v1ReadyEvent } from "@elementor/editor-v1-adapters";
|
|
242
|
+
|
|
19
243
|
// src/sync/model-utils.ts
|
|
20
244
|
function findChildRecursive(model, predicate) {
|
|
21
245
|
const childModels = model.get("elements") ?? [];
|
|
@@ -242,40 +466,6 @@ function deleteElement({ container, options = {} }) {
|
|
|
242
466
|
});
|
|
243
467
|
}
|
|
244
468
|
|
|
245
|
-
// src/sync/resolve-element.ts
|
|
246
|
-
function isConnected(container) {
|
|
247
|
-
if (!container) {
|
|
248
|
-
return false;
|
|
249
|
-
}
|
|
250
|
-
if (!container.view?.el) {
|
|
251
|
-
return true;
|
|
252
|
-
}
|
|
253
|
-
return container.view.el.isConnected;
|
|
254
|
-
}
|
|
255
|
-
function resolveContainer(container, id) {
|
|
256
|
-
const looked = container.lookup?.();
|
|
257
|
-
if (isConnected(looked)) {
|
|
258
|
-
return looked;
|
|
259
|
-
}
|
|
260
|
-
const byId = getContainer(id);
|
|
261
|
-
if (isConnected(byId)) {
|
|
262
|
-
return byId;
|
|
263
|
-
}
|
|
264
|
-
return null;
|
|
265
|
-
}
|
|
266
|
-
function getDocumentUtils() {
|
|
267
|
-
return window.$e?.components?.get?.("document")?.utils;
|
|
268
|
-
}
|
|
269
|
-
function findModelInDocument(id) {
|
|
270
|
-
return getDocumentUtils()?.findModelById?.(id) ?? null;
|
|
271
|
-
}
|
|
272
|
-
function addModelToParent(parentId, childData, options) {
|
|
273
|
-
return getDocumentUtils()?.addModelToParent?.(parentId, childData, options) ?? false;
|
|
274
|
-
}
|
|
275
|
-
function removeModelFromParent(parentId, childId) {
|
|
276
|
-
return getDocumentUtils()?.removeModelFromParent?.(parentId, childId) ?? false;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
469
|
// src/sync/create-elements.ts
|
|
280
470
|
var createElements = ({
|
|
281
471
|
elements,
|
|
@@ -1216,6 +1406,7 @@ var playElementInteractions = (elementId, interactionId) => {
|
|
|
1216
1406
|
export {
|
|
1217
1407
|
ELEMENT_STYLE_CHANGE_EVENT,
|
|
1218
1408
|
addModelToParent,
|
|
1409
|
+
bindSettingsReconcile,
|
|
1219
1410
|
createElement,
|
|
1220
1411
|
createElementStyle,
|
|
1221
1412
|
createElements,
|
|
@@ -1224,6 +1415,7 @@ export {
|
|
|
1224
1415
|
dropElement,
|
|
1225
1416
|
duplicateElement,
|
|
1226
1417
|
duplicateElements,
|
|
1418
|
+
evaluateWhen,
|
|
1227
1419
|
findChildRecursive,
|
|
1228
1420
|
findModelInDocument,
|
|
1229
1421
|
generateElementId,
|
|
@@ -1251,10 +1443,12 @@ export {
|
|
|
1251
1443
|
moveElement,
|
|
1252
1444
|
moveElements,
|
|
1253
1445
|
playElementInteractions,
|
|
1446
|
+
reconcileInitialChildren,
|
|
1254
1447
|
removeElements,
|
|
1255
1448
|
removeModelFromParent,
|
|
1256
1449
|
replaceElement,
|
|
1257
1450
|
resolveContainer,
|
|
1451
|
+
resolveInsertIndex,
|
|
1258
1452
|
selectElement,
|
|
1259
1453
|
shouldCreateNewLocalStyle,
|
|
1260
1454
|
styleRerenderEvents,
|