@elementor/editor-elements 4.0.0-543 → 4.0.0-545

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
@@ -16,22 +16,7 @@ var selectElement = (elementId) => {
16
16
  }
17
17
  };
18
18
 
19
- // src/sync/get-model.ts
20
- function getModel(id, parentModel) {
21
- const extendedWindow = window;
22
- const container = extendedWindow.elementor?.getContainer?.(id) ?? null;
23
- if (container) {
24
- return { model: container.model };
25
- }
26
- if (parentModel) {
27
- const childModels = parentModel.get("elements") ?? [];
28
- const model = childModels.find((m) => m.get("id") === id);
29
- if (model) {
30
- return { model };
31
- }
32
- }
33
- return null;
34
- }
19
+ // src/sync/model-utils.ts
35
20
  function findChildRecursive(model, predicate) {
36
21
  const childModels = model.get("elements") ?? [];
37
22
  for (const childModel of childModels) {
@@ -229,11 +214,7 @@ function useSelectedElement() {
229
214
 
230
215
  // src/sync/create-element.ts
231
216
  import { __privateRunCommandSync as runCommandSync } from "@elementor/editor-v1-adapters";
232
- function createElement({ containerId, model, options }) {
233
- const container = getContainer(containerId);
234
- if (!container) {
235
- throw new Error(`Container with ID "${containerId}" not found`);
236
- }
217
+ function createElement({ container, model, options }) {
237
218
  return runCommandSync("document/elements/create", {
238
219
  container,
239
220
  model,
@@ -247,14 +228,7 @@ import { __ } from "@wordpress/i18n";
247
228
 
248
229
  // src/sync/delete-element.ts
249
230
  import { __privateRunCommand as runCommand2 } from "@elementor/editor-v1-adapters";
250
- function deleteElement({
251
- elementId,
252
- options = {}
253
- }) {
254
- const container = getContainer(elementId);
255
- if (!container) {
256
- throw new Error(`Element with ID "${elementId}" not found`);
257
- }
231
+ function deleteElement({ container, options = {} }) {
258
232
  return runCommand2("document/elements/delete", {
259
233
  container,
260
234
  options
@@ -271,48 +245,54 @@ var createElements = ({
271
245
  {
272
246
  do: ({ elements: elementsParam }) => {
273
247
  const createdElements = [];
274
- elementsParam.forEach((createParams) => {
275
- const { options, ...elementParams } = createParams;
248
+ elementsParam.forEach(({ container, options, ...elementParams }) => {
249
+ const parentContainer = container.lookup?.() ?? container;
250
+ if (!parentContainer) {
251
+ throw new Error("Parent container not found");
252
+ }
276
253
  const element = createElement({
254
+ container: parentContainer,
277
255
  ...elementParams,
278
256
  options: { ...options, useHistory: false }
279
257
  });
280
- const elementId = element.id;
281
258
  createdElements.push({
282
- elementId,
259
+ container: element,
260
+ parentContainer,
283
261
  model: element.model?.toJSON() || {},
284
- createParams: {
285
- ...createParams
286
- }
262
+ options
287
263
  });
288
264
  });
289
265
  return { createdElements };
290
266
  },
291
267
  undo: (_, { createdElements }) => {
292
- [...createdElements].reverse().forEach(({ elementId }) => {
293
- deleteElement({
294
- elementId,
295
- options: { useHistory: false }
296
- });
268
+ [...createdElements].reverse().forEach(({ container }) => {
269
+ const freshContainer = container.lookup?.();
270
+ if (freshContainer) {
271
+ deleteElement({
272
+ container: freshContainer,
273
+ options: { useHistory: false }
274
+ });
275
+ }
297
276
  });
298
277
  },
299
278
  redo: (_, { createdElements }) => {
300
279
  const newElements = [];
301
- createdElements.forEach(({ createParams, model }) => {
280
+ createdElements.forEach(({ parentContainer, model, options }) => {
281
+ const freshParent = parentContainer.lookup?.();
282
+ if (!freshParent) {
283
+ return;
284
+ }
302
285
  const element = createElement({
303
- containerId: createParams.containerId,
286
+ container: freshParent,
304
287
  model,
305
- options: { ...createParams.options, useHistory: false }
288
+ options: { ...options, useHistory: false }
289
+ });
290
+ newElements.push({
291
+ container: element,
292
+ parentContainer: freshParent,
293
+ model: element.model.toJSON(),
294
+ options
306
295
  });
307
- const elementId = element.id;
308
- const container = getContainer(elementId);
309
- if (container) {
310
- newElements.push({
311
- elementId,
312
- model: container.model.toJSON(),
313
- createParams
314
- });
315
- }
316
296
  });
317
297
  return { createdElements: newElements };
318
298
  }
@@ -341,15 +321,11 @@ function dropElement({ containerId, model, options }) {
341
321
 
342
322
  // src/sync/duplicate-element.ts
343
323
  import { __privateRunCommandSync as runCommandSync3 } from "@elementor/editor-v1-adapters";
344
- function duplicateElement({ elementId, options = {} }) {
345
- const elementToDuplicate = getContainer(elementId);
346
- if (!elementToDuplicate) {
347
- throw new Error(`Element with ID "${elementId}" not found`);
348
- }
349
- const currentIndex = elementToDuplicate.view?._index ?? 0;
324
+ function duplicateElement({ element, options = {} }) {
325
+ const currentIndex = element.view?._index ?? 0;
350
326
  const insertPosition = options.clone !== false ? currentIndex + 1 : void 0;
351
327
  return runCommandSync3("document/elements/duplicate", {
352
- container: elementToDuplicate,
328
+ container: element,
353
329
  options: { at: insertPosition, edit: false, ...options }
354
330
  });
355
331
  }
@@ -368,59 +344,56 @@ var duplicateElements = ({
368
344
  {
369
345
  do: ({ elementIds: elementIdsToDuplicate }) => {
370
346
  onDuplicateElements?.();
371
- const duplicatedElements = elementIdsToDuplicate.reduce((acc, elementId) => {
347
+ const duplicatedElements = [];
348
+ elementIdsToDuplicate.forEach((elementId) => {
372
349
  const originalContainer = getContainer(elementId);
373
- if (originalContainer?.parent) {
374
- const duplicatedElement = duplicateElement({
375
- elementId,
376
- options: { useHistory: false }
377
- });
378
- acc.push({
379
- id: duplicatedElement.id,
380
- model: duplicatedElement.model.toJSON(),
381
- originalElementId: elementId,
382
- modelToRestore: duplicatedElement.model.toJSON(),
383
- parentContainerId: duplicatedElement.parent?.id,
384
- at: duplicatedElement.view?._index
385
- });
350
+ if (!originalContainer?.parent) {
351
+ return;
386
352
  }
387
- return acc;
388
- }, []);
353
+ const duplicatedElement = duplicateElement({
354
+ element: originalContainer,
355
+ options: { useHistory: false }
356
+ });
357
+ if (!duplicatedElement.parent) {
358
+ return;
359
+ }
360
+ duplicatedElements.push({
361
+ container: duplicatedElement,
362
+ parentContainer: duplicatedElement.parent,
363
+ model: duplicatedElement.model.toJSON(),
364
+ at: duplicatedElement.view?._index
365
+ });
366
+ });
389
367
  return { duplicatedElements };
390
368
  },
391
369
  undo: (_, { duplicatedElements }) => {
392
370
  onRestoreElements?.();
393
- [...duplicatedElements].reverse().forEach(({ id }) => {
371
+ [...duplicatedElements].reverse().forEach(({ container }) => {
394
372
  deleteElement({
395
- elementId: id,
373
+ container,
396
374
  options: { useHistory: false }
397
375
  });
398
376
  });
399
377
  },
400
378
  redo: (_, { duplicatedElements: previousElements }) => {
401
379
  onDuplicateElements?.();
402
- const duplicatedElements = previousElements.reduce((acc, previousElement) => {
403
- if (previousElement.modelToRestore && previousElement.parentContainerId) {
380
+ const duplicatedElements = [];
381
+ previousElements.forEach(({ parentContainer, model, at }) => {
382
+ const freshParent = parentContainer.lookup?.();
383
+ if (freshParent) {
404
384
  const createdElement = createElement({
405
- containerId: previousElement.parentContainerId,
406
- model: previousElement.modelToRestore,
407
- options: {
408
- useHistory: false,
409
- clone: false,
410
- at: previousElement.at
411
- }
385
+ container: freshParent,
386
+ model,
387
+ options: { useHistory: false, clone: false, at }
412
388
  });
413
- acc.push({
414
- id: createdElement.id,
415
- model: createdElement.model.toJSON(),
416
- originalElementId: previousElement.originalElementId,
417
- modelToRestore: previousElement.modelToRestore,
418
- parentContainerId: previousElement.parentContainerId,
419
- at: previousElement.at
389
+ duplicatedElements.push({
390
+ container: createdElement,
391
+ parentContainer: freshParent,
392
+ model,
393
+ at
420
394
  });
421
395
  }
422
- return acc;
423
- }, []);
396
+ });
424
397
  return { duplicatedElements };
425
398
  }
426
399
  },
@@ -519,25 +492,23 @@ function getElements(root) {
519
492
  }
520
493
 
521
494
  // src/sync/move-element.ts
522
- function moveElement({ elementId, targetContainerId, options = {} }) {
523
- const container = getContainer(elementId);
524
- const target = getContainer(targetContainerId);
525
- if (!container) {
526
- throw new Error(`Element with ID "${elementId}" not found`);
495
+ function moveElement({ element, targetContainer, options = {} }) {
496
+ const resolvedElement = element.lookup?.();
497
+ const resolvedTarget = targetContainer.lookup?.();
498
+ if (!resolvedElement) {
499
+ throw new Error(`Element not found: ${element.id}`);
527
500
  }
528
- if (!target) {
529
- throw new Error(`Target container with ID "${targetContainerId}" not found`);
501
+ if (!resolvedTarget) {
502
+ throw new Error(`Target container not found: ${targetContainer.id}`);
530
503
  }
531
- const modelToRecreate = container.model.toJSON();
504
+ const modelToRecreate = resolvedElement.model.toJSON();
532
505
  deleteElement({
533
- elementId,
534
- // prevent inner history from being created
506
+ container: resolvedElement,
535
507
  options: { ...options, useHistory: false }
536
508
  });
537
509
  const newContainer = createElement({
538
- containerId: targetContainerId,
510
+ container: resolvedTarget,
539
511
  model: modelToRecreate,
540
- // prevent inner history from being created
541
512
  options: { edit: false, ...options, useHistory: false }
542
513
  });
543
514
  return newContainer;
@@ -558,39 +529,46 @@ var moveElements = ({
558
529
  do: ({ moves }) => {
559
530
  const movedElements = [];
560
531
  onMoveElements?.();
561
- moves.forEach((move) => {
562
- const { elementId } = move;
563
- const sourceContainer = getContainer(elementId);
564
- if (!sourceContainer) {
565
- throw new Error(`Element with ID "${elementId}" not found`);
532
+ moves.forEach(({ element, targetContainer, options }) => {
533
+ const sourceElement = element.lookup?.() ?? element;
534
+ const target = targetContainer.lookup?.() ?? targetContainer;
535
+ if (!sourceElement) {
536
+ throw new Error("Element not found");
537
+ }
538
+ if (!target) {
539
+ throw new Error("Target container not found");
540
+ }
541
+ if (!sourceElement.parent) {
542
+ throw new Error("Element has no parent container");
566
543
  }
567
- const originalContainerId = sourceContainer.parent?.id || "";
568
- const originalIndex = sourceContainer.parent?.children?.indexOf(sourceContainer) ?? -1;
569
- const originalPosition = {
570
- elementId,
571
- originalContainerId,
572
- originalIndex
573
- };
574
- const element = moveElement({
575
- ...move,
576
- options: { ...move.options, useHistory: false }
544
+ const originalContainer = sourceElement.parent;
545
+ const originalIndex = originalContainer.children?.indexOf(sourceElement) ?? -1;
546
+ const newElement = moveElement({
547
+ element: sourceElement,
548
+ targetContainer: target,
549
+ options: { ...options, useHistory: false }
577
550
  });
578
551
  movedElements.push({
579
- elementId,
580
- originalPosition,
581
- move,
582
- element
552
+ element: newElement,
553
+ originalContainer,
554
+ originalIndex,
555
+ targetContainer: target,
556
+ options
583
557
  });
584
558
  });
585
559
  return { movedElements };
586
560
  },
587
561
  undo: (_, { movedElements }) => {
588
562
  onRestoreElements?.();
589
- [...movedElements].reverse().forEach(({ originalPosition }) => {
590
- const { elementId, originalContainerId, originalIndex } = originalPosition;
563
+ [...movedElements].reverse().forEach(({ element, originalContainer, originalIndex }) => {
564
+ const freshElement = element.lookup?.();
565
+ const freshOriginalContainer = originalContainer.lookup?.();
566
+ if (!freshElement || !freshOriginalContainer) {
567
+ return;
568
+ }
591
569
  moveElement({
592
- elementId,
593
- targetContainerId: originalContainerId,
570
+ element: freshElement,
571
+ targetContainer: freshOriginalContainer,
594
572
  options: {
595
573
  useHistory: false,
596
574
  at: originalIndex >= 0 ? originalIndex : void 0
@@ -601,16 +579,24 @@ var moveElements = ({
601
579
  redo: (_, { movedElements }) => {
602
580
  const newMovedElements = [];
603
581
  onMoveElements?.();
604
- movedElements.forEach(({ move, originalPosition }) => {
605
- const element = moveElement({
606
- ...move,
607
- options: { ...move.options, useHistory: false }
582
+ movedElements.forEach(({ element, originalContainer, originalIndex, targetContainer, options }) => {
583
+ const freshElement = element.lookup?.();
584
+ const freshOriginalContainer = originalContainer.lookup?.();
585
+ const freshTarget = targetContainer.lookup?.();
586
+ if (!freshElement || !freshOriginalContainer || !freshTarget) {
587
+ return;
588
+ }
589
+ const newElement = moveElement({
590
+ element: freshElement,
591
+ targetContainer: freshTarget,
592
+ options: { ...options, useHistory: false }
608
593
  });
609
594
  newMovedElements.push({
610
- elementId: move.elementId,
611
- originalPosition,
612
- move,
613
- element
595
+ element: newElement,
596
+ originalContainer: freshOriginalContainer,
597
+ originalIndex,
598
+ targetContainer: freshTarget,
599
+ options
614
600
  });
615
601
  });
616
602
  return { movedElements: newMovedElements };
@@ -640,48 +626,58 @@ var removeElements = ({
640
626
  const removedElements = [];
641
627
  elementIdsParam.forEach((elementId) => {
642
628
  const container = getContainer(elementId);
643
- if (container) {
644
- const model = container.model.toJSON();
645
- const parent = container.parent;
646
- const at = container.view?._index ?? 0;
629
+ if (container?.parent) {
647
630
  removedElements.push({
648
- elementId,
649
- model,
650
- parent: parent ?? null,
651
- at
631
+ container,
632
+ parent: container.parent,
633
+ model: container.model.toJSON(),
634
+ at: container.view?._index ?? 0
652
635
  });
653
636
  }
654
637
  });
655
638
  onRemoveElements?.();
656
- elementIdsParam.forEach((elementId) => {
639
+ removedElements.forEach(({ container }) => {
657
640
  deleteElement({
658
- elementId,
641
+ container,
659
642
  options: { useHistory: false }
660
643
  });
661
644
  });
662
- return { elementIds: elementIdsParam, removedElements };
645
+ return { removedElements };
663
646
  },
664
647
  undo: (_, { removedElements }) => {
665
648
  onRestoreElements?.();
666
- [...removedElements].reverse().forEach(({ model, parent, at }) => {
667
- if (parent && model) {
649
+ [...removedElements].reverse().forEach(({ parent, model, at }) => {
650
+ const freshParent = parent.lookup?.();
651
+ if (freshParent) {
668
652
  createElement({
669
- containerId: parent.id,
653
+ container: freshParent,
670
654
  model,
671
655
  options: { useHistory: false, at }
672
656
  });
673
657
  }
674
658
  });
675
659
  },
676
- redo: (_, { elementIds: originalElementIds, removedElements }) => {
660
+ redo: (_, { removedElements }) => {
677
661
  onRemoveElements?.();
678
- originalElementIds.forEach((elementId) => {
662
+ const newRemovedElements = [];
663
+ removedElements.forEach(({ container, parent, model, at }) => {
664
+ const freshContainer = container.lookup?.();
665
+ const freshParent = parent.lookup?.();
666
+ if (!freshContainer || !freshParent) {
667
+ return;
668
+ }
679
669
  deleteElement({
680
- elementId,
670
+ container: freshContainer,
681
671
  options: { useHistory: false }
682
672
  });
673
+ newRemovedElements.push({
674
+ container: freshContainer,
675
+ parent: freshParent,
676
+ model,
677
+ at
678
+ });
683
679
  });
684
- return { elementIds: originalElementIds, removedElements };
680
+ return { removedElements: newRemovedElements };
685
681
  }
686
682
  },
687
683
  {
@@ -694,42 +690,42 @@ var removeElements = ({
694
690
 
695
691
  // src/sync/replace-element.ts
696
692
  var replaceElement = async ({ currentElement, newElement, withHistory = true }) => {
697
- const { containerId, index } = getNewElementContainer(currentElement, newElement);
693
+ const currentElementContainer = getContainer(currentElement.id);
694
+ if (!currentElementContainer) {
695
+ throw new ElementNotFoundError({ context: { elementId: currentElement.id } });
696
+ }
697
+ const { container, index } = getNewElementContainer(currentElementContainer, newElement);
698
698
  const newElementInstance = createElement({
699
- containerId,
699
+ container,
700
700
  model: newElement,
701
701
  options: { at: index, useHistory: withHistory }
702
702
  });
703
- await deleteElement({ elementId: currentElement.id, options: { useHistory: withHistory } });
703
+ await deleteElement({ container: currentElementContainer, options: { useHistory: withHistory } });
704
704
  return newElementInstance;
705
705
  };
706
- function getNewElementContainer(currentElement, newElement) {
707
- const currentElementContainer = getContainer(currentElement.id);
708
- if (!currentElementContainer) {
709
- throw new ElementNotFoundError({ context: { elementId: currentElement.id } });
710
- }
706
+ function getNewElementContainer(currentElementContainer, newElement) {
711
707
  const { parent } = currentElementContainer;
712
708
  if (!parent) {
713
- throw new ElementParentNotFoundError({ context: { elementId: currentElement.id } });
709
+ throw new ElementParentNotFoundError({ context: { elementId: currentElementContainer.id } });
714
710
  }
715
711
  const elementIndex = currentElementContainer.view?._index ?? 0;
716
712
  if (elementIndex === -1) {
717
- throw new ElementIndexNotFoundError({ context: { elementId: currentElement.id } });
713
+ throw new ElementIndexNotFoundError({ context: { elementId: currentElementContainer.id } });
718
714
  }
719
- let container = { containerId: parent.id, index: elementIndex };
715
+ let location = { container: parent, index: elementIndex };
720
716
  if (parent.id === "document" && newElement.elType === "widget") {
721
- container = createWrapperForWidget(parent.id, elementIndex);
717
+ location = createWrapperForWidget(parent, elementIndex);
722
718
  }
723
- return container;
719
+ return location;
724
720
  }
725
721
  var DEFAULT_CONTAINER_TYPE = "e-flexbox";
726
- function createWrapperForWidget(parentId, elementIndex) {
722
+ function createWrapperForWidget(parent, elementIndex) {
727
723
  const container = createElement({
728
- containerId: parentId,
724
+ container: parent,
729
725
  model: { elType: DEFAULT_CONTAINER_TYPE },
730
726
  options: { at: elementIndex, useHistory: false }
731
727
  });
732
- return { containerId: container.id, index: 0 };
728
+ return { container, index: 0 };
733
729
  }
734
730
 
735
731
  // src/sync/update-element-editor-settings.ts
@@ -744,16 +740,16 @@ var updateElementEditorSettings = ({
744
740
  }
745
741
  const editorSettings = element.model.get("editor_settings") ?? {};
746
742
  element.model.set("editor_settings", { ...editorSettings, ...settings });
747
- setDocumentModifiedStatus(true);
743
+ runCommandSync4("document/save/set-is-modified", { status: true }, { internal: true });
748
744
  };
749
- function setDocumentModifiedStatus(status) {
750
- runCommandSync4("document/save/set-is-modified", { status }, { internal: true });
751
- }
752
745
 
753
746
  // src/sync/update-element-settings.ts
754
747
  import { __privateRunCommandSync as runCommandSync5 } from "@elementor/editor-v1-adapters";
755
748
  var updateElementSettings = ({ id, props, withHistory = true }) => {
756
749
  const container = getContainer(id);
750
+ if (!container) {
751
+ return;
752
+ }
757
753
  const args = {
758
754
  container,
759
755
  settings: { ...props }
@@ -1068,16 +1064,13 @@ var updateElementInteractions = ({
1068
1064
  }
1069
1065
  element.model.set("interactions", interactions);
1070
1066
  window.dispatchEvent(new CustomEvent("elementor/element/update_interactions"));
1071
- setDocumentModifiedStatus2(true);
1067
+ runCommandSync7("document/save/set-is-modified", { status: true }, { internal: true });
1072
1068
  };
1073
1069
  var playElementInteractions = (elementId, interactionId) => {
1074
1070
  window.top?.dispatchEvent(
1075
1071
  new CustomEvent("atomic/play_interactions", { detail: { elementId, interactionId } })
1076
1072
  );
1077
1073
  };
1078
- function setDocumentModifiedStatus2(status) {
1079
- runCommandSync7("document/save/set-is-modified", { status }, { internal: true });
1080
- }
1081
1074
  export {
1082
1075
  ELEMENT_STYLE_CHANGE_EVENT,
1083
1076
  createElement,
@@ -1106,7 +1099,6 @@ export {
1106
1099
  getElementType,
1107
1100
  getElements,
1108
1101
  getLinkInLinkRestriction,
1109
- getModel,
1110
1102
  getSelectedElements,
1111
1103
  getWidgetsCache,
1112
1104
  isElementAnchored,