@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.
@@ -82,6 +82,7 @@ function attachChildFromRule( parentId: string, rule: ChildDependencyRule, stash
82
82
  const alreadyPresent = currentChildren.some( ( child ) => child.elType === rule.child_type );
83
83
 
84
84
  if ( alreadyPresent ) {
85
+ placeAnchoredChild( parentId, rule );
85
86
  return;
86
87
  }
87
88
 
@@ -123,6 +124,53 @@ function detachChildFromRule( parentId: string, rule: ChildDependencyRule, stash
123
124
  requestNavigatorRefresh( parentId );
124
125
  }
125
126
 
127
+ function placeAnchoredChild( parentId: string, rule: ChildDependencyRule ): void {
128
+ if ( rule.position.kind !== 'after_type' && rule.position.kind !== 'before_type' ) {
129
+ return;
130
+ }
131
+
132
+ let parent: V1Element | undefined;
133
+
134
+ try {
135
+ parent = getContainer( parentId ) ?? undefined;
136
+ } catch {
137
+ return;
138
+ }
139
+
140
+ const collection = parent?.model?.get?.( 'elements' ) as
141
+ | {
142
+ models?: Array< { get: ( key: string ) => unknown } >;
143
+ remove?: ( model: unknown, options?: { silent?: boolean } ) => void;
144
+ add?: ( model: unknown, options?: { at?: number; silent?: boolean } ) => void;
145
+ }
146
+ | undefined;
147
+ const models = collection?.models;
148
+
149
+ if ( ! models || ! collection?.remove || ! collection?.add ) {
150
+ return;
151
+ }
152
+
153
+ const existingIndex = models.findIndex( ( model ) => model.get( 'elType' ) === rule.child_type );
154
+
155
+ if ( existingIndex < 0 ) {
156
+ return;
157
+ }
158
+
159
+ const siblings = models.map( ( model ) => ( { elType: model.get( 'elType' ) } ) as V1ElementData );
160
+ const remaining = siblings.filter( ( _, index ) => index !== existingIndex );
161
+ const insertAt = resolveInsertIndex( rule.position, remaining );
162
+
163
+ if ( insertAt === existingIndex ) {
164
+ return;
165
+ }
166
+
167
+ const model = models[ existingIndex ];
168
+
169
+ collection.remove( model, { silent: true } );
170
+ collection.add( model, { at: insertAt, silent: true } );
171
+ requestNavigatorRefresh( parentId );
172
+ }
173
+
126
174
  // Backbone `add`/`remove` on the parent elements collection is silent (see
127
175
  // assets/dev/js/editor/document/component.js addModelToParent / removeModelFromParent),
128
176
  // so the Marionette Navigator CompositeView never observes our mutation. The
@@ -33,6 +33,18 @@ export function reconcileInitialChildren( {
33
33
  const existingIndex = elements.findIndex( ( element ) => element.elType === rule.child_type );
34
34
  const isPresent = existingIndex >= 0;
35
35
 
36
+ if ( isMet && isPresent && isAnchoredPosition( rule.position.kind ) ) {
37
+ const remaining = elements.filter( ( _, index ) => index !== existingIndex );
38
+ const insertAt = resolveInsertIndex( rule.position, remaining );
39
+
40
+ if ( insertAt !== existingIndex ) {
41
+ const [ moved ] = elements.splice( existingIndex, 1 );
42
+ elements.splice( insertAt, 0, moved );
43
+ }
44
+
45
+ return;
46
+ }
47
+
36
48
  if ( isMet && ! isPresent ) {
37
49
  const { modelData, wasStashed } = resolveChildModelData( elementId, rule, stash );
38
50
  const insertAt = resolveInsertIndex( rule.position, elements );
@@ -57,3 +69,7 @@ export function reconcileInitialChildren( {
57
69
 
58
70
  attributes.elements = elements;
59
71
  }
72
+
73
+ function isAnchoredPosition( kind: string ): boolean {
74
+ return kind === 'after_type' || kind === 'before_type';
75
+ }
package/src/index.ts CHANGED
@@ -35,6 +35,7 @@ export {
35
35
  type DuplicatedElementsResult,
36
36
  type DuplicateElementsParams,
37
37
  } from './sync/duplicate-elements';
38
+ export { cloneElementTree } from './sync/clone-element-tree';
38
39
  export { generateElementId } from './sync/generate-element-id';
39
40
  export { getContainer, selectElement } from './sync/get-container';
40
41
  export { getPreviewElementDOM } from './sync/get-preview-element-dom';
@@ -54,7 +55,8 @@ export {
54
55
  getElementChildren as getElementChildrenWithFallback,
55
56
  type ModelResult,
56
57
  } from './sync/model-utils';
57
- export { getElements } from './sync/get-elements';
58
+ export { getElements, getHostDocumentElements } from './sync/get-elements';
59
+ export { getHostDocumentId } from './sync/get-host-document-id';
58
60
  export { getSelectedElements } from './sync/get-selected-elements';
59
61
  export { getWidgetsCache } from './sync/get-widgets-cache';
60
62
  export { moveElement, type MoveElementParams } from './sync/move-element';
@@ -0,0 +1,24 @@
1
+ import { generateElementId } from './generate-element-id';
2
+ import { type BackboneModel, type V1ElementModelProps, type V1ElementSettingsProps } from './types';
3
+
4
+ type CloneableElement = Partial< V1ElementModelProps > | BackboneModel;
5
+
6
+ function isBackboneModel( item: CloneableElement ): item is BackboneModel {
7
+ return 'toJSON' in item && 'get' in item;
8
+ }
9
+
10
+ export function cloneElementTree( item: CloneableElement ): V1ElementModelProps {
11
+ const source = isBackboneModel( item ) ? ( item.toJSON() as Partial< V1ElementModelProps > ) : item;
12
+
13
+ return {
14
+ ...source,
15
+ id: generateElementId(),
16
+ settings: {
17
+ ...( source.settings ?? {} ),
18
+ _element_id: '',
19
+ } as V1ElementSettingsProps,
20
+ elements: ( source.elements ?? [] ).map( ( child ) =>
21
+ cloneElementTree( child as Partial< V1ElementModelProps > )
22
+ ),
23
+ } as unknown as V1ElementModelProps;
24
+ }
@@ -1,11 +1,10 @@
1
1
  import { type ElementID } from '../types';
2
2
  import { getContainer } from './get-container';
3
3
  import { getCurrentDocumentContainer } from './get-current-document-container';
4
+ import { getHostDocumentContainer } from './get-host-document-container';
4
5
  import { type V1Element } from './types';
5
6
 
6
- export function getElements( root?: ElementID ): V1Element[] {
7
- const container = root ? getContainer( root ) : getCurrentDocumentContainer();
8
-
7
+ function collectElements( container: V1Element | null ): V1Element[] {
9
8
  if ( ! container ) {
10
9
  return [];
11
10
  }
@@ -16,3 +15,13 @@ export function getElements( root?: ElementID ): V1Element[] {
16
15
 
17
16
  return [ container, ...children ];
18
17
  }
18
+
19
+ export function getElements( root?: ElementID ): V1Element[] {
20
+ const container = root ? getContainer( root ) : getCurrentDocumentContainer();
21
+
22
+ return collectElements( container );
23
+ }
24
+
25
+ export function getHostDocumentElements(): V1Element[] {
26
+ return collectElements( getHostDocumentContainer() );
27
+ }
@@ -0,0 +1,13 @@
1
+ import { getHostDocumentId } from './get-host-document-id';
2
+ import { type ExtendedWindow, type V1Element } from './types';
3
+
4
+ export function getHostDocumentContainer(): V1Element | null {
5
+ const extendedWindow = window as unknown as ExtendedWindow;
6
+ const hostId = getHostDocumentId();
7
+
8
+ if ( ! hostId ) {
9
+ return null;
10
+ }
11
+
12
+ return extendedWindow.elementor?.documents?.get?.( hostId )?.container ?? null;
13
+ }
@@ -0,0 +1,11 @@
1
+ import { type ExtendedWindow } from './types';
2
+
3
+ export function getHostDocumentId() {
4
+ const extendedWindow = window as unknown as ExtendedWindow;
5
+
6
+ try {
7
+ return extendedWindow.elementor?.documents?.getInitialId?.() ?? null;
8
+ } catch {
9
+ return null;
10
+ }
11
+ }
@@ -40,7 +40,7 @@ export function findModelInDocument( id: string ): BackboneModel | null {
40
40
  export function addModelToParent(
41
41
  parentId: string,
42
42
  childData: V1ElementModelProps,
43
- options?: { at?: number }
43
+ options?: { at?: number; clone?: boolean }
44
44
  ): boolean {
45
45
  return getDocumentUtils()?.addModelToParent?.( parentId, childData, options ) ?? false;
46
46
  }
package/src/sync/types.ts CHANGED
@@ -10,7 +10,11 @@ export type ExtendedWindow = Window & {
10
10
  get?: ( name: string ) => {
11
11
  utils?: {
12
12
  findModelById?: ( id: string, collection?: unknown ) => BackboneModel | null;
13
- addModelToParent?: ( parentId: string, childData: unknown, options?: { at?: number } ) => boolean;
13
+ addModelToParent?: (
14
+ parentId: string,
15
+ childData: unknown,
16
+ options?: { at?: number; clone?: boolean }
17
+ ) => boolean;
14
18
  removeModelFromParent?: ( parentId: string, childId: string ) => boolean;
15
19
  };
16
20
  };
@@ -28,6 +32,12 @@ export type ExtendedWindow = Window & {
28
32
  }
29
33
  | undefined;
30
34
  getCurrentId?: () => number;
35
+ getInitialId?: () => number;
36
+ get?: ( id: number ) =>
37
+ | {
38
+ container: V1Element;
39
+ }
40
+ | undefined;
31
41
  };
32
42
  getContainer?: ( id: string ) => V1Element | undefined;
33
43
  getPreviewContainer?: () => V1Element | undefined;
@@ -183,6 +193,7 @@ export type V1ElementEditorSettingsProps = {
183
193
  component_uid?: string;
184
194
  grid_outline?: boolean;
185
195
  empty_state_preview?: boolean;
196
+ decorative?: boolean;
186
197
  };
187
198
 
188
199
  export type V1ElementSettingsProps = Record< string, PropValue >;
@@ -199,6 +210,7 @@ export type V1ElementConfig< T = object, TChild = unknown > = {
199
210
  dependencies_per_target_mapping?: Record< string, string[] >;
200
211
  twig_templates?: Record< string, string >;
201
212
  twig_main_template?: string;
213
+ template_context?: Record< string, unknown >;
202
214
  base_styles?: Record< string, StyleDefinition >;
203
215
  base_styles_dictionary?: Record< string, string >;
204
216
  base_settings?: Record< string, PropValue >;