Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developer can style content types output differently per viewport #2694

Merged
merged 6 commits into from
Nov 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability: New line after const and/or before forEach loop.

if (!styles[selector]) {
styles[selector] = [];
}
styles[selector].push(rule.style);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability: New line after if block.

});
});
});
}

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