Skip to content

Commit

Permalink
Developer can style content types output differently per viewport (#2694
Browse files Browse the repository at this point in the history
)

* magento/magento2-page-builder#558: Developer can style content types output differently per viewport
- convert style blocks to inline styles to prevent BIC changes

* magento/magento2-page-builder#558: Developer can style content types output differently per viewport
- fix code style

Co-authored-by: Devagouda <40405790+dpatil-magento@users.noreply.github.com>
  • Loading branch information
omiroshnichenko and dpatil-magento authored Nov 2, 2020
1 parent d855ae9 commit 890276b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/pagebuilder/lib/parseStorageHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,48 @@ const walk = (rootEl, contentTypeStructureObj) => {
return contentTypeStructureObj;
};

const pbStyleAttribute = 'data-pb-style';
const bodyId = 'html-body';

/**
* Convert styles block to inline styles.
* @param {HTMLDocument} document
*/
const convertToInlineStyles = document => {
const styleBlocks = document.getElementsByTagName('style');
const styles = {};

if (styleBlocks.length > 0) {
Array.from(styleBlocks).forEach(styleBlock => {
const cssRules = styleBlock.sheet.cssRules;

Array.from(cssRules).forEach(rule => {
const selectors = rule.selectorText
.split(',')
.map(selector => selector.trim());
selectors.forEach(selector => {
if (!styles[selector]) {
styles[selector] = [];
}
styles[selector].push(rule.style);
});
});
});
}

Object.keys(styles).map(selector => {
const element = document.querySelector(selector);

styles[selector].map(style => {
element.setAttribute(
'style',
element.style.cssText + style.cssText
);
});
element.removeAttribute(pbStyleAttribute);
});
};

/**
* Parse the master format storage HTML
*
Expand All @@ -85,6 +127,9 @@ const parseStorageHtml = htmlStr => {

const stageContentType = createContentTypeObject('root-container');

container.body.id = bodyId;
convertToInlineStyles(container);

return walk(container.body, stageContentType);
};

Expand Down

0 comments on commit 890276b

Please sign in to comment.