@@ -8,11 +8,13 @@ import ko from "knockout";
8
8
import $t from "mage/translate" ;
9
9
import widgetInitializer from "Magento_PageBuilder/js/widget-initializer" ;
10
10
import mageUtils from "mageUtils" ;
11
+ import _ from "underscore" ;
11
12
import Config from "../../config" ;
12
13
import ContentTypeInterface from "../../content-type" ;
13
14
import ContentTypeConfigInterface from "../../content-type-config.types" ;
14
15
import HideShowOption from "../../content-type-menu/hide-show-option" ;
15
16
import { OptionsInterface } from "../../content-type-menu/option.types" ;
17
+ import { pbStyleAttribute } from "../../content-type/style-registry" ;
16
18
import { DataObject } from "../../data-store" ;
17
19
import { get } from "../../utils/object" ;
18
20
import ObservableUpdater from "../observable-updater" ;
@@ -169,7 +171,7 @@ export default class Preview extends BasePreview {
169
171
let content : string = "" ;
170
172
if ( response . data . content ) {
171
173
this . showBlockPreview ( true ) ;
172
- content = this . processBackgroundImages ( response . data . content ) ;
174
+ content = this . processContent ( response . data . content ) ;
173
175
this . data . main . html ( content ) ;
174
176
this . initializeWidgets ( this . element ) ;
175
177
} else if ( response . data . error ) {
@@ -198,6 +200,19 @@ export default class Preview extends BasePreview {
198
200
this . displayingBlockPreview ( isShow ) ;
199
201
}
200
202
203
+ /**
204
+ * Adapt content to view it on stage.
205
+ *
206
+ * @param content
207
+ */
208
+ private processContent ( content : string ) : string {
209
+ let processedContent = this . processBackgroundImages ( content ) ;
210
+
211
+ processedContent = this . processBreakpointStyles ( processedContent ) ;
212
+
213
+ return processedContent ;
214
+ }
215
+
201
216
/**
202
217
* Generate styles for background images.
203
218
*
@@ -240,4 +255,44 @@ export default class Preview extends BasePreview {
240
255
241
256
return content ;
242
257
}
258
+
259
+ /**
260
+ * Replace media queries with viewport classes.
261
+ *
262
+ * @param {string } content
263
+ * @return string
264
+ */
265
+ private processBreakpointStyles ( content : string ) : string {
266
+ const document = new DOMParser ( ) . parseFromString ( content , "text/html" ) ;
267
+ const styleBlocks = document . querySelectorAll ( "style" ) ;
268
+ const mediaStyleBlock = document . createElement ( "style" ) ;
269
+ const viewports = Config . getConfig ( "viewports" ) ;
270
+
271
+ styleBlocks . forEach ( ( styleBlock : HTMLStyleElement ) => {
272
+ const cssRules = ( styleBlock . sheet as CSSStyleSheet ) . cssRules ;
273
+
274
+ Array . from ( cssRules ) . forEach ( ( rule : CSSMediaRule ) => {
275
+ const mediaScope = rule instanceof CSSMediaRule && _ . findKey ( viewports , ( viewport : any ) => {
276
+ return rule . conditionText === viewport . media ;
277
+ } ) ;
278
+
279
+ if ( mediaScope ) {
280
+ Array . from ( rule . cssRules ) . forEach ( ( mediaRule : CSSStyleRule , index : number ) => {
281
+ if ( mediaRule . selectorText . indexOf ( pbStyleAttribute ) !== - 1 ) {
282
+ const selector = mediaRule . selectorText . replace ( " " , ` .${ mediaScope } -viewport ` ) ;
283
+
284
+ mediaStyleBlock . append ( `${ selector } {${ mediaRule . style . cssText } }` ) ;
285
+ }
286
+ } ) ;
287
+ }
288
+ } ) ;
289
+ } ) ;
290
+
291
+ if ( mediaStyleBlock . innerText . length ) {
292
+ document . body . append ( mediaStyleBlock ) ;
293
+ content = document . head . innerHTML + document . body . innerHTML ;
294
+ }
295
+
296
+ return content ;
297
+ }
243
298
}
0 commit comments