| 1 |
|
| 2 |
const addBlockEditorDynamicStyles = ( styleTagId, styling ) => { |
| 3 |
|
| 4 |
setTimeout( () => { |
| 5 |
|
| 6 |
// Static Editor CSS. |
| 7 |
|
| 8 |
const editorStaticCSSStylesTag = document.getElementById( 'uagb-editor-styles' ); |
| 9 |
let cloneEditorStaticCSSStylesTag = false; |
| 10 |
|
| 11 |
if ( editorStaticCSSStylesTag ) { |
| 12 |
|
| 13 |
cloneEditorStaticCSSStylesTag = editorStaticCSSStylesTag.cloneNode( true ); |
| 14 |
} |
| 15 |
|
| 16 |
// Dashicons Editor CSS. |
| 17 |
|
| 18 |
const editorDashiconsCSSStylesTag = document.getElementById( 'dashicons-css' ); |
| 19 |
let cloneEditorDashiconsCSSStylesTag = false; |
| 20 |
|
| 21 |
if ( editorDashiconsCSSStylesTag ) { |
| 22 |
|
| 23 |
cloneEditorDashiconsCSSStylesTag = editorDashiconsCSSStylesTag.cloneNode( true ); |
| 24 |
} |
| 25 |
|
| 26 |
// Dashicons Editor CSS Ends. |
| 27 |
|
| 28 |
// Static CSS. |
| 29 |
|
| 30 |
const staticCSSStylesTag = document.getElementById( 'uagb-block-css-css' ); |
| 31 |
let cloneStaticCSSStylesTag = false; |
| 32 |
|
| 33 |
if ( staticCSSStylesTag ) { |
| 34 |
|
| 35 |
cloneStaticCSSStylesTag = staticCSSStylesTag.cloneNode( true ); |
| 36 |
} |
| 37 |
|
| 38 |
// Static CSS Ends. |
| 39 |
|
| 40 |
|
| 41 |
// Slick CSS. |
| 42 |
const slickStaticCSSStylesTag = document.getElementById( 'uagb-slick-css-css' ); |
| 43 |
let cloneSlickStaticCSSStylesTag = false; |
| 44 |
|
| 45 |
if ( slickStaticCSSStylesTag ) { |
| 46 |
|
| 47 |
cloneSlickStaticCSSStylesTag = slickStaticCSSStylesTag.cloneNode( true ); |
| 48 |
} |
| 49 |
|
| 50 |
// Slick CSS Ends. |
| 51 |
|
| 52 |
// Block Editor Spacing CSS. |
| 53 |
const blockEditorSpacingCSSStylesTag = document.getElementById( 'uagb-blocks-editor-spacing-style' ); |
| 54 |
let cloneBlockEditorSpacingCSSStylesTag = false; |
| 55 |
|
| 56 |
if ( blockEditorSpacingCSSStylesTag ) { |
| 57 |
|
| 58 |
cloneBlockEditorSpacingCSSStylesTag = blockEditorSpacingCSSStylesTag.cloneNode( true ); |
| 59 |
} |
| 60 |
|
| 61 |
// Block Editor Spacing CSS Ends. |
| 62 |
|
| 63 |
// Desktop. |
| 64 |
const element = document.getElementById( |
| 65 |
styleTagId |
| 66 |
); |
| 67 |
|
| 68 |
|
| 69 |
if ( null === element || undefined === element ) { |
| 70 |
|
| 71 |
const $style = document.createElement( 'style' ); |
| 72 |
$style.setAttribute( |
| 73 |
'id', |
| 74 |
styleTagId |
| 75 |
); |
| 76 |
|
| 77 |
$style.innerHTML = styling; |
| 78 |
document.head.appendChild( $style ); |
| 79 |
} |
| 80 |
|
| 81 |
if ( null !== element && undefined !== element ) { |
| 82 |
element.innerHTML = styling; |
| 83 |
} |
| 84 |
// Desktop ends. |
| 85 |
|
| 86 |
// Tablet / Mobile Starts. |
| 87 |
const tabletPreview = document.getElementsByClassName( 'is-tablet-preview' ); |
| 88 |
const mobilePreview = document.getElementsByClassName( 'is-mobile-preview' ); |
| 89 |
const twentyTwentyEditorIframe = document.getElementsByClassName( 'edit-site-visual-editor__editor-canvas' ); |
| 90 |
|
| 91 |
if ( 0 !== tabletPreview.length || 0 !== mobilePreview.length || 0 !== twentyTwentyEditorIframe.length ) { |
| 92 |
|
| 93 |
const preview = tabletPreview[0] || mobilePreview[0]; |
| 94 |
|
| 95 |
let iframe = false; |
| 96 |
|
| 97 |
if ( 0 !== twentyTwentyEditorIframe.length ) { |
| 98 |
iframe = twentyTwentyEditorIframe[0]; |
| 99 |
} else if ( preview ) { |
| 100 |
iframe = preview.getElementsByTagName( 'iframe' )[0]; |
| 101 |
} |
| 102 |
|
| 103 |
const iframeDocument = iframe?.contentWindow.document || iframe?.contentDocument; |
| 104 |
|
| 105 |
if ( ! iframe || ! iframeDocument ) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
// Static CSS. |
| 110 |
if ( cloneStaticCSSStylesTag ) { |
| 111 |
const iframeStaticCSSStylesTag = iframeDocument.getElementById( 'uagb-block-css-css' ); |
| 112 |
if ( ! iframeStaticCSSStylesTag ) { |
| 113 |
iframeDocument.head.appendChild( cloneStaticCSSStylesTag ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Static Editor CSS. |
| 118 |
if ( cloneEditorStaticCSSStylesTag ) { |
| 119 |
const iframeEditorStaticCSSStylesTag = iframeDocument.getElementById( 'uagb-editor-styles' ); |
| 120 |
if ( iframeEditorStaticCSSStylesTag ) { |
| 121 |
iframeDocument.head.removeChild( iframeEditorStaticCSSStylesTag ); |
| 122 |
} |
| 123 |
iframeDocument.head.appendChild( cloneEditorStaticCSSStylesTag ); |
| 124 |
} |
| 125 |
|
| 126 |
// Dashicons CSS. |
| 127 |
if ( cloneEditorDashiconsCSSStylesTag ) { |
| 128 |
const iframeEditorDashiconsCSSStylesTag = iframeDocument.getElementById( 'dashicons-css' ); |
| 129 |
if ( iframeEditorDashiconsCSSStylesTag ) { |
| 130 |
iframeDocument.head.removeChild( iframeEditorDashiconsCSSStylesTag ); |
| 131 |
} |
| 132 |
iframeDocument.head.appendChild( cloneEditorDashiconsCSSStylesTag ); |
| 133 |
} |
| 134 |
|
| 135 |
// Slick CSS. |
| 136 |
if ( cloneSlickStaticCSSStylesTag ) { |
| 137 |
const iframeSlickStaticCSSStylesTag = iframeDocument.getElementById( 'uagb-slick-css-css' ); |
| 138 |
if ( ! iframeSlickStaticCSSStylesTag ) { |
| 139 |
iframeDocument.head.appendChild( cloneSlickStaticCSSStylesTag ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// Block Editor Spacing CSS. |
| 144 |
if ( cloneBlockEditorSpacingCSSStylesTag ) { |
| 145 |
const iframeBlockEditorSpacingCSSStylesTag = iframeDocument.getElementById( 'uagb-blocks-editor-spacing-style' ); |
| 146 |
if ( ! iframeBlockEditorSpacingCSSStylesTag ) { |
| 147 |
iframeDocument.head.appendChild( cloneBlockEditorSpacingCSSStylesTag ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
let iframeElement = iframeDocument.getElementById( |
| 152 |
styleTagId |
| 153 |
); |
| 154 |
|
| 155 |
if ( null === iframeElement || undefined === iframeElement ) { |
| 156 |
|
| 157 |
const $style = document.createElement( 'style' ); |
| 158 |
$style.setAttribute( |
| 159 |
'id', |
| 160 |
styleTagId |
| 161 |
); |
| 162 |
|
| 163 |
// Dynamic CSS. |
| 164 |
iframeDocument.head.appendChild( $style ); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
iframeElement = iframeDocument.getElementById( |
| 169 |
styleTagId |
| 170 |
); |
| 171 |
|
| 172 |
if ( null !== iframeElement && undefined !== iframeElement ) { |
| 173 |
iframeElement.innerHTML = styling; |
| 174 |
} |
| 175 |
} |
| 176 |
} ); |
| 177 |
} |
| 178 |
|
| 179 |
export default addBlockEditorDynamicStyles; |
| 180 |
|
| 181 |
|