| 1 |
import { $eType, ElementorType } from '../types/types'; |
| 2 |
|
| 3 |
/** |
| 4 |
* Add element to the page using model and parent container. |
| 5 |
* @param {Object} props |
| 6 |
* @param {Object} props.model |
| 7 |
* @param {string | null} props.container |
| 8 |
* @param {boolean} props.isContainerASection |
| 9 |
* @return {string | undefined} |
| 10 |
*/ |
| 11 |
|
| 12 |
let parent: unknown; |
| 13 |
let elementor: ElementorType; |
| 14 |
let $e: $eType; |
| 15 |
export const addElement = ( props: { model: unknown, container: null | string, isContainerASection: boolean } ): string | undefined => { |
| 16 |
if ( props.container ) { |
| 17 |
parent = elementor.getContainer( props.container ); |
| 18 |
} else { |
| 19 |
// If a `container` isn't supplied - create a new Section. |
| 20 |
parent = $e.run( |
| 21 |
'document/elements/create', |
| 22 |
{ |
| 23 |
model: { elType: 'section' }, |
| 24 |
columns: 1, |
| 25 |
container: elementor.getContainer( 'document' ), |
| 26 |
}, |
| 27 |
); |
| 28 |
|
| 29 |
props.isContainerASection = true; |
| 30 |
} |
| 31 |
|
| 32 |
if ( props.isContainerASection && 'object' === typeof parent && 'children' in parent ) { |
| 33 |
parent = parent.children[ 0 ]; |
| 34 |
} |
| 35 |
|
| 36 |
const element = $e.run( |
| 37 |
'document/elements/create', |
| 38 |
{ |
| 39 |
model: props.model, |
| 40 |
container: parent, |
| 41 |
}, |
| 42 |
); |
| 43 |
|
| 44 |
if ( element && 'object' === typeof element && 'id' in element && 'string' === typeof element.id ) { |
| 45 |
return element.id; |
| 46 |
} |
| 47 |
return undefined; |
| 48 |
}; |
| 49 |
|
| 50 |
/** |
| 51 |
* Make an Elementor element CSS selector using Container ID. |
| 52 |
* |
| 53 |
* @param {string} id - Container ID. |
| 54 |
* |
| 55 |
* @return {string} css selector |
| 56 |
*/ |
| 57 |
export const getElementSelector = ( id: string ) => { |
| 58 |
return `[data-id = "${ id }"]`; |
| 59 |
}; |
| 60 |
|
| 61 |
export function compareVersions( a: string, b: string ): number { |
| 62 |
const aParts = a.split( '.' ).map( Number ); |
| 63 |
const bParts = b.split( '.' ).map( Number ); |
| 64 |
|
| 65 |
for ( let i = 0; i < Math.max( aParts.length, bParts.length ); i++ ) { |
| 66 |
const aVal = isNaN( aParts[ i ] ) ? Infinity : ( aParts[ i ] || 0 ); |
| 67 |
const bVal = isNaN( bParts[ i ] ) ? Infinity : ( bParts[ i ] || 0 ); |
| 68 |
if ( aVal !== bVal ) { |
| 69 |
return aVal - bVal; |
| 70 |
} |
| 71 |
} |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|