@elementor/editor-elements 4.3.4 → 4.3.5

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.mjs CHANGED
@@ -175,6 +175,7 @@ function attachChildFromRule(parentId, rule, stash) {
175
175
  const currentChildren = getDirectChildData(parent);
176
176
  const alreadyPresent = currentChildren.some((child) => child.elType === rule.child_type);
177
177
  if (alreadyPresent) {
178
+ placeAnchoredChild(parentId, rule);
178
179
  return;
179
180
  }
180
181
  const { modelData, wasStashed } = resolveChildModelData(parentId, rule, stash);
@@ -204,6 +205,36 @@ function detachChildFromRule(parentId, rule, stash) {
204
205
  }
205
206
  requestNavigatorRefresh(parentId);
206
207
  }
208
+ function placeAnchoredChild(parentId, rule) {
209
+ if (rule.position.kind !== "after_type" && rule.position.kind !== "before_type") {
210
+ return;
211
+ }
212
+ let parent;
213
+ try {
214
+ parent = getContainer(parentId) ?? void 0;
215
+ } catch {
216
+ return;
217
+ }
218
+ const collection = parent?.model?.get?.("elements");
219
+ const models = collection?.models;
220
+ if (!models || !collection?.remove || !collection?.add) {
221
+ return;
222
+ }
223
+ const existingIndex = models.findIndex((model2) => model2.get("elType") === rule.child_type);
224
+ if (existingIndex < 0) {
225
+ return;
226
+ }
227
+ const siblings = models.map((model2) => ({ elType: model2.get("elType") }));
228
+ const remaining = siblings.filter((_, index) => index !== existingIndex);
229
+ const insertAt = resolveInsertIndex(rule.position, remaining);
230
+ if (insertAt === existingIndex) {
231
+ return;
232
+ }
233
+ const model = models[existingIndex];
234
+ collection.remove(model, { silent: true });
235
+ collection.add(model, { at: insertAt, silent: true });
236
+ requestNavigatorRefresh(parentId);
237
+ }
207
238
  function requestNavigatorRefresh(parentId) {
208
239
  if (typeof window === "undefined" || typeof window.dispatchEvent !== "function") {
209
240
  return;
@@ -235,6 +266,15 @@ function reconcileInitialChildren({
235
266
  const isMet = evaluateWhen(rule.when, settings);
236
267
  const existingIndex = elements.findIndex((element) => element.elType === rule.child_type);
237
268
  const isPresent = existingIndex >= 0;
269
+ if (isMet && isPresent && isAnchoredPosition(rule.position.kind)) {
270
+ const remaining = elements.filter((_, index) => index !== existingIndex);
271
+ const insertAt = resolveInsertIndex(rule.position, remaining);
272
+ if (insertAt !== existingIndex) {
273
+ const [moved] = elements.splice(existingIndex, 1);
274
+ elements.splice(insertAt, 0, moved);
275
+ }
276
+ return;
277
+ }
238
278
  if (isMet && !isPresent) {
239
279
  const { modelData, wasStashed } = resolveChildModelData(elementId, rule, stash);
240
280
  const insertAt = resolveInsertIndex(rule.position, elements);
@@ -253,6 +293,9 @@ function reconcileInitialChildren({
253
293
  });
254
294
  attributes.elements = elements;
255
295
  }
296
+ function isAnchoredPosition(kind) {
297
+ return kind === "after_type" || kind === "before_type";
298
+ }
256
299
 
257
300
  // src/hooks/use-element-children.ts
258
301
  import { __privateUseListenTo as useListenTo, commandEndEvent, v1ReadyEvent } from "@elementor/editor-v1-adapters";
@@ -687,6 +730,25 @@ var duplicateElements = ({
687
730
  return undoableDuplicate({ elementIds });
688
731
  };
689
732
 
733
+ // src/sync/clone-element-tree.ts
734
+ function isBackboneModel(item) {
735
+ return "toJSON" in item && "get" in item;
736
+ }
737
+ function cloneElementTree(item) {
738
+ const source = isBackboneModel(item) ? item.toJSON() : item;
739
+ return {
740
+ ...source,
741
+ id: generateElementId(),
742
+ settings: {
743
+ ...source.settings ?? {},
744
+ _element_id: ""
745
+ },
746
+ elements: (source.elements ?? []).map(
747
+ (child) => cloneElementTree(child)
748
+ )
749
+ };
750
+ }
751
+
690
752
  // src/sync/get-preview-element-dom.ts
691
753
  function getPreviewElementDOM(id) {
692
754
  try {
@@ -820,9 +882,28 @@ function getAllDescendants(container) {
820
882
  return [container, ...children];
821
883
  }
822
884
 
885
+ // src/sync/get-host-document-id.ts
886
+ function getHostDocumentId() {
887
+ const extendedWindow = window;
888
+ try {
889
+ return extendedWindow.elementor?.documents?.getInitialId?.() ?? null;
890
+ } catch {
891
+ return null;
892
+ }
893
+ }
894
+
895
+ // src/sync/get-host-document-container.ts
896
+ function getHostDocumentContainer() {
897
+ const extendedWindow = window;
898
+ const hostId = getHostDocumentId();
899
+ if (!hostId) {
900
+ return null;
901
+ }
902
+ return extendedWindow.elementor?.documents?.get?.(hostId)?.container ?? null;
903
+ }
904
+
823
905
  // src/sync/get-elements.ts
824
- function getElements(root) {
825
- const container = root ? getContainer(root) : getCurrentDocumentContainer();
906
+ function collectElements(container) {
826
907
  if (!container) {
827
908
  return [];
828
909
  }
@@ -831,6 +912,13 @@ function getElements(root) {
831
912
  );
832
913
  return [container, ...children];
833
914
  }
915
+ function getElements(root) {
916
+ const container = root ? getContainer(root) : getCurrentDocumentContainer();
917
+ return collectElements(container);
918
+ }
919
+ function getHostDocumentElements() {
920
+ return collectElements(getHostDocumentContainer());
921
+ }
834
922
 
835
923
  // src/sync/move-element.ts
836
924
  function moveElement({ element, targetContainer, options = {} }) {
@@ -1469,6 +1557,7 @@ export {
1469
1557
  ELEMENT_STYLE_CHANGE_EVENT,
1470
1558
  addModelToParent,
1471
1559
  bindSettingsReconcile,
1560
+ cloneElementTree,
1472
1561
  createElement,
1473
1562
  createElementStyle,
1474
1563
  createElements,
@@ -1499,6 +1588,8 @@ export {
1499
1588
  getElementTitle,
1500
1589
  getElementType,
1501
1590
  getElements,
1591
+ getHostDocumentElements,
1592
+ getHostDocumentId,
1502
1593
  getLinkInLinkRestriction,
1503
1594
  getPreviewElementDOM,
1504
1595
  getSelectedElements,