@elementor/editor-elements 4.2.0-beta1 → 4.3.0-1000
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 +49 -2
- package/dist/index.d.ts +49 -2
- package/dist/index.js +310 -56
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +291 -43
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -7
- package/src/children-dependencies/bind-settings-reconcile.ts +147 -0
- package/src/children-dependencies/index.ts +4 -0
- package/src/children-dependencies/reconcile-initial-children.ts +59 -0
- package/src/children-dependencies/stash.ts +40 -0
- package/src/children-dependencies/types.ts +20 -0
- package/src/children-dependencies/utils.ts +44 -0
- package/src/index.ts +12 -0
- package/src/sync/get-element-icon.ts +14 -0
- package/src/sync/get-element-title.ts +48 -0
- package/src/sync/resolve-insert-index.ts +34 -0
- package/src/sync/types.ts +13 -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,250 @@ 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
|
+
|
|
111
|
+
// src/sync/generate-element-id.ts
|
|
112
|
+
var generateElementId = () => {
|
|
113
|
+
const extendedWindow = window;
|
|
114
|
+
return extendedWindow.elementorCommon?.helpers?.getUniqueId?.() ?? `el-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/children-dependencies/utils.ts
|
|
118
|
+
function evaluateWhen(when, settings) {
|
|
119
|
+
return isDependencyMet(when, settings).isMet;
|
|
120
|
+
}
|
|
121
|
+
function ensureModelId(model) {
|
|
122
|
+
const { skipDefaultChildren: _skipDefaultChildren, ...rest } = model;
|
|
123
|
+
return rest.id ? rest : { ...rest, id: generateElementId() };
|
|
124
|
+
}
|
|
125
|
+
function resolveChildModelData(elementId, rule, stash) {
|
|
126
|
+
const stashed = rule.stash ? stash.get(elementId, rule.child_type) : void 0;
|
|
127
|
+
const modelData = ensureModelId(
|
|
128
|
+
stashed ?? rule.default_model ?? { elType: rule.child_type }
|
|
129
|
+
);
|
|
130
|
+
return { modelData, wasStashed: Boolean(stashed) };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/children-dependencies/bind-settings-reconcile.ts
|
|
134
|
+
function bindSettingsReconcile({ model, elementConfig }) {
|
|
135
|
+
const stash = createChildrenStash();
|
|
136
|
+
const rules = elementConfig?.children_dependencies;
|
|
137
|
+
if (!rules?.length) {
|
|
138
|
+
return () => {
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
const settingsModel = model.get("settings");
|
|
142
|
+
const elementId = model.get("id");
|
|
143
|
+
if (!settingsModel?.on || !settingsModel?.off || !elementId) {
|
|
144
|
+
return () => {
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const lastMet = /* @__PURE__ */ new Map();
|
|
148
|
+
rules.forEach((rule) => {
|
|
149
|
+
lastMet.set(rule.child_type, evaluateWhen(rule.when, settingsModel.toJSON()));
|
|
150
|
+
});
|
|
151
|
+
const onChange = () => {
|
|
152
|
+
const currentSettings = settingsModel.toJSON();
|
|
153
|
+
rules.forEach((rule) => {
|
|
154
|
+
const previous = lastMet.get(rule.child_type) ?? false;
|
|
155
|
+
const current = evaluateWhen(rule.when, currentSettings);
|
|
156
|
+
if (previous === current) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
lastMet.set(rule.child_type, current);
|
|
160
|
+
if (current) {
|
|
161
|
+
attachChildFromRule(elementId, rule, stash);
|
|
162
|
+
} else {
|
|
163
|
+
detachChildFromRule(elementId, rule, stash);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
settingsModel.on("change", onChange);
|
|
168
|
+
return () => {
|
|
169
|
+
settingsModel.off?.("change", onChange);
|
|
170
|
+
stash.clearAllForElement(elementId);
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function attachChildFromRule(parentId, rule, stash) {
|
|
174
|
+
const parent = getContainer(parentId) ?? void 0;
|
|
175
|
+
const currentChildren = getDirectChildData(parent);
|
|
176
|
+
const alreadyPresent = currentChildren.some((child) => child.elType === rule.child_type);
|
|
177
|
+
if (alreadyPresent) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const { modelData, wasStashed } = resolveChildModelData(parentId, rule, stash);
|
|
181
|
+
const insertAt = resolveInsertIndex(rule.position, currentChildren);
|
|
182
|
+
const attached = addModelToParent(parentId, modelData, { at: insertAt });
|
|
183
|
+
if (!attached) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (wasStashed) {
|
|
187
|
+
stash.clear(parentId, rule.child_type);
|
|
188
|
+
}
|
|
189
|
+
requestNavigatorRefresh(parentId);
|
|
190
|
+
}
|
|
191
|
+
function detachChildFromRule(parentId, rule, stash) {
|
|
192
|
+
const parent = getContainer(parentId) ?? void 0;
|
|
193
|
+
const child = parent?.children?.find((candidate) => candidate.model.get("elType") === rule.child_type);
|
|
194
|
+
if (!child) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const childSnapshot = child.model.toJSON();
|
|
198
|
+
const removed = removeModelFromParent(parentId, child.id);
|
|
199
|
+
if (!removed) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (rule.stash) {
|
|
203
|
+
stash.save(parentId, rule.child_type, childSnapshot);
|
|
204
|
+
}
|
|
205
|
+
requestNavigatorRefresh(parentId);
|
|
206
|
+
}
|
|
207
|
+
function requestNavigatorRefresh(parentId) {
|
|
208
|
+
if (typeof window === "undefined" || typeof window.dispatchEvent !== "function") {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
window.dispatchEvent(
|
|
212
|
+
new CustomEvent("elementor/navigator/refresh-children", {
|
|
213
|
+
detail: { elementId: parentId }
|
|
214
|
+
})
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
function getDirectChildData(parent) {
|
|
218
|
+
return (parent?.children ?? []).map((child) => child.model.toJSON());
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/children-dependencies/reconcile-initial-children.ts
|
|
222
|
+
function reconcileInitialChildren({
|
|
223
|
+
elementId,
|
|
224
|
+
elementConfig,
|
|
225
|
+
attributes
|
|
226
|
+
}) {
|
|
227
|
+
const stash = createChildrenStash();
|
|
228
|
+
const rules = elementConfig?.children_dependencies;
|
|
229
|
+
if (!rules?.length) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const elements = [...attributes.elements ?? []];
|
|
233
|
+
const settings = attributes.settings ?? {};
|
|
234
|
+
rules.forEach((rule) => {
|
|
235
|
+
const isMet = evaluateWhen(rule.when, settings);
|
|
236
|
+
const existingIndex = elements.findIndex((element) => element.elType === rule.child_type);
|
|
237
|
+
const isPresent = existingIndex >= 0;
|
|
238
|
+
if (isMet && !isPresent) {
|
|
239
|
+
const { modelData, wasStashed } = resolveChildModelData(elementId, rule, stash);
|
|
240
|
+
const insertAt = resolveInsertIndex(rule.position, elements);
|
|
241
|
+
elements.splice(insertAt, 0, modelData);
|
|
242
|
+
if (wasStashed) {
|
|
243
|
+
stash.clear(elementId, rule.child_type);
|
|
244
|
+
}
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (!isMet && isPresent) {
|
|
248
|
+
const [removed] = elements.splice(existingIndex, 1);
|
|
249
|
+
if (rule.stash && removed) {
|
|
250
|
+
stash.save(elementId, rule.child_type, removed);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
attributes.elements = elements;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// src/hooks/use-element-children.ts
|
|
258
|
+
import { __privateUseListenTo as useListenTo, commandEndEvent, v1ReadyEvent } from "@elementor/editor-v1-adapters";
|
|
259
|
+
|
|
19
260
|
// src/sync/model-utils.ts
|
|
20
261
|
function findChildRecursive(model, predicate) {
|
|
21
262
|
const childModels = model.get("elements") ?? [];
|
|
@@ -242,40 +483,6 @@ function deleteElement({ container, options = {} }) {
|
|
|
242
483
|
});
|
|
243
484
|
}
|
|
244
485
|
|
|
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
486
|
// src/sync/create-elements.ts
|
|
280
487
|
var createElements = ({
|
|
281
488
|
elements,
|
|
@@ -480,12 +687,6 @@ var duplicateElements = ({
|
|
|
480
687
|
return undoableDuplicate({ elementIds });
|
|
481
688
|
};
|
|
482
689
|
|
|
483
|
-
// src/sync/generate-element-id.ts
|
|
484
|
-
var generateElementId = () => {
|
|
485
|
-
const extendedWindow = window;
|
|
486
|
-
return extendedWindow.elementorCommon?.helpers?.getUniqueId?.() ?? `el-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
487
|
-
};
|
|
488
|
-
|
|
489
690
|
// src/sync/get-current-document-container.ts
|
|
490
691
|
function getCurrentDocumentContainer() {
|
|
491
692
|
const extendedWindow = window;
|
|
@@ -502,6 +703,16 @@ function getCurrentDocumentId() {
|
|
|
502
703
|
}
|
|
503
704
|
}
|
|
504
705
|
|
|
706
|
+
// src/sync/get-element-icon.ts
|
|
707
|
+
function getElementIcon(elementId) {
|
|
708
|
+
const container = getContainer(elementId);
|
|
709
|
+
const type = container?.model.get("widgetType") || container?.model.get("elType");
|
|
710
|
+
if (!type) {
|
|
711
|
+
return null;
|
|
712
|
+
}
|
|
713
|
+
return getWidgetsCache()?.[type]?.icon ?? null;
|
|
714
|
+
}
|
|
715
|
+
|
|
505
716
|
// src/errors.ts
|
|
506
717
|
import { createError } from "@elementor/utils";
|
|
507
718
|
var ElementNotFoundError = createError({
|
|
@@ -546,6 +757,37 @@ function getElementLabel(elementId) {
|
|
|
546
757
|
return label;
|
|
547
758
|
}
|
|
548
759
|
|
|
760
|
+
// src/sync/get-element-title.ts
|
|
761
|
+
function extractString(value) {
|
|
762
|
+
if (typeof value === "string") {
|
|
763
|
+
return value || null;
|
|
764
|
+
}
|
|
765
|
+
if (value && typeof value === "object" && "value" in value && typeof value.value === "string") {
|
|
766
|
+
return value.value || null;
|
|
767
|
+
}
|
|
768
|
+
return null;
|
|
769
|
+
}
|
|
770
|
+
function getElementTitle(elementId) {
|
|
771
|
+
const editorSettings = getElementEditorSettings(elementId);
|
|
772
|
+
const editorTitle = extractString(editorSettings?.title);
|
|
773
|
+
if (editorTitle) {
|
|
774
|
+
return editorTitle;
|
|
775
|
+
}
|
|
776
|
+
const legacyTitle = extractString(getElementSetting(elementId, "_title"));
|
|
777
|
+
if (legacyTitle) {
|
|
778
|
+
return legacyTitle;
|
|
779
|
+
}
|
|
780
|
+
const presetTitle = extractString(getElementSetting(elementId, "presetTitle"));
|
|
781
|
+
if (presetTitle) {
|
|
782
|
+
return presetTitle;
|
|
783
|
+
}
|
|
784
|
+
try {
|
|
785
|
+
return getElementLabel(elementId);
|
|
786
|
+
} catch {
|
|
787
|
+
return null;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
549
791
|
// src/sync/get-element-styles.ts
|
|
550
792
|
var getElementStyles = (elementID) => {
|
|
551
793
|
const container = getContainer(elementID);
|
|
@@ -1175,6 +1417,7 @@ var playElementInteractions = (elementId, interactionId) => {
|
|
|
1175
1417
|
export {
|
|
1176
1418
|
ELEMENT_STYLE_CHANGE_EVENT,
|
|
1177
1419
|
addModelToParent,
|
|
1420
|
+
bindSettingsReconcile,
|
|
1178
1421
|
createElement,
|
|
1179
1422
|
createElementStyle,
|
|
1180
1423
|
createElements,
|
|
@@ -1183,6 +1426,7 @@ export {
|
|
|
1183
1426
|
dropElement,
|
|
1184
1427
|
duplicateElement,
|
|
1185
1428
|
duplicateElements,
|
|
1429
|
+
evaluateWhen,
|
|
1186
1430
|
findChildRecursive,
|
|
1187
1431
|
findModelInDocument,
|
|
1188
1432
|
generateElementId,
|
|
@@ -1194,11 +1438,13 @@ export {
|
|
|
1194
1438
|
getCurrentDocumentId,
|
|
1195
1439
|
getElementChildren as getElementChildrenWithFallback,
|
|
1196
1440
|
getElementEditorSettings,
|
|
1441
|
+
getElementIcon,
|
|
1197
1442
|
getElementInteractions,
|
|
1198
1443
|
getElementLabel,
|
|
1199
1444
|
getElementSetting,
|
|
1200
1445
|
getElementSettings,
|
|
1201
1446
|
getElementStyles,
|
|
1447
|
+
getElementTitle,
|
|
1202
1448
|
getElementType,
|
|
1203
1449
|
getElements,
|
|
1204
1450
|
getLinkInLinkRestriction,
|
|
@@ -1208,10 +1454,12 @@ export {
|
|
|
1208
1454
|
moveElement,
|
|
1209
1455
|
moveElements,
|
|
1210
1456
|
playElementInteractions,
|
|
1457
|
+
reconcileInitialChildren,
|
|
1211
1458
|
removeElements,
|
|
1212
1459
|
removeModelFromParent,
|
|
1213
1460
|
replaceElement,
|
|
1214
1461
|
resolveContainer,
|
|
1462
|
+
resolveInsertIndex,
|
|
1215
1463
|
selectElement,
|
|
1216
1464
|
shouldCreateNewLocalStyle,
|
|
1217
1465
|
styleRerenderEvents,
|